class Lamassu::Guardian

Guardian object for authorizing a subject

Attributes

container[R]
policies[R]

Public Class Methods

new(container: PolicyContainer.new) click to toggle source
# File lib/lamassu/guardian.rb, line 16
def initialize(container: PolicyContainer.new)
  @container = container
  @namespace_resolver = Lamassu.namespace_resolver
end

Public Instance Methods

authorize(subject, target, *policies) click to toggle source

Check authorization for subject on target for one or more policies

If more than one policy is specified, it will return the last Success if all policies are successful. Otherwise, it will return the first Failure

:reek: LongParameterList @param [Object] subject Subject for authorization check @param [Object,Module] target Target for authorization check @param [Symbol,String] policies Policy or policies to check @return [Dry::Result]

# File lib/lamassu/guardian.rb, line 33
def authorize(subject, target, *policies)
  case policies.length
  when 0
    raise ArgumentError, 'No policy given'
  when 1
    authorize_one(subject, target, *policies)
  else
    authorize_many(subject, target, *policies)
  end
end

Private Instance Methods

authorize_many(subject, target, *policies) { |call| ... } click to toggle source
# File lib/lamassu/guardian.rb, line 53
def authorize_many(subject, target, *policies)
  namespace = @namespace_resolver.call(target)

  Success(
    List.new(policies)
      .fmap { |policy| "#{namespace}.#{policy}" }
      .fmap(container.method(:resolve))
      .fmap { |policy| yield policy.call(subject, target) }
  )
end
authorize_one(subject, target, policy) click to toggle source
# File lib/lamassu/guardian.rb, line 46
def authorize_one(subject, target, policy)
  namespace = @namespace_resolver.call(target)
  policy = container.resolve("#{namespace}.#{policy}")

  policy.call(subject, target).to_result
end