2021-03-18 02:11:52 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Graphql
|
|
|
|
module Authorize
|
|
|
|
class ObjectAuthorization
|
2021-04-28 11:09:35 -04:00
|
|
|
attr_reader :abilities, :permitted_scopes
|
2021-03-18 02:11:52 -04:00
|
|
|
|
2021-04-28 11:09:35 -04:00
|
|
|
def initialize(abilities, scopes = %i[api read_api])
|
2021-03-18 02:11:52 -04:00
|
|
|
@abilities = Array.wrap(abilities).flatten
|
2021-04-28 11:09:35 -04:00
|
|
|
@permitted_scopes = Array.wrap(scopes)
|
2021-03-18 02:11:52 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def none?
|
|
|
|
abilities.empty?
|
|
|
|
end
|
|
|
|
|
|
|
|
def any?
|
|
|
|
abilities.present?
|
|
|
|
end
|
|
|
|
|
2021-04-28 11:09:35 -04:00
|
|
|
def ok?(object, current_user, scope_validator: nil)
|
|
|
|
scopes_ok?(scope_validator) && abilities_ok?(object, current_user)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def abilities_ok?(object, current_user)
|
2021-03-18 02:11:52 -04:00
|
|
|
return true if none?
|
|
|
|
|
2021-04-06 20:09:26 -04:00
|
|
|
subject = object.try(:declarative_policy_subject) || object
|
2021-03-18 02:11:52 -04:00
|
|
|
abilities.all? do |ability|
|
2021-04-06 20:09:26 -04:00
|
|
|
Ability.allowed?(current_user, ability, subject)
|
2021-03-18 02:11:52 -04:00
|
|
|
end
|
|
|
|
end
|
2021-04-28 11:09:35 -04:00
|
|
|
|
|
|
|
def scopes_ok?(validator)
|
|
|
|
return true unless validator.present?
|
|
|
|
|
|
|
|
validator.valid_for?(permitted_scopes)
|
|
|
|
end
|
2021-03-18 02:11:52 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|