Module: Roadblock::Authorizer

Defined in:
lib/roadblock/authorizer.rb

Instance Method Summary collapse

Instance Method Details

#can?(action, object) ⇒ true, false

Returns whether the current auth_object can perform the given action on the provided object.

Parameters:

  • action (Symbol)

    the action to check. Most often :read or :write.

  • object (Object)

    the object to authorize against.

Returns:

  • (true, false)


22
23
24
25
26
27
28
29
30
31
# File 'lib/roadblock/authorizer.rb', line 22

def can?(action, object)
  if block_given?
    yield(object)
  else
    objects = [*object]
    objects
      .map { |obj| send("can_#{action}?", obj) }
      .all?
  end
end

#initialize(auth_object, scopes: []) ⇒ self

Creates an authorizer for the given object and any provided scopes.

Parameters:

  • auth_object (Object)

    the object (usually a user) to authorize for.

  • scopes (Array<Symbol>) (defaults to: [])

    the scopes (if any) associated with the auth_object.

Returns:

  • (self)


10
11
12
13
# File 'lib/roadblock/authorizer.rb', line 10

def initialize(auth_object, scopes: [])
  self.auth_object = auth_object
  self.scopes = scopes
end