Class: Roadblock::Stack

Inherits:
Object
  • Object
show all
Defined in:
lib/roadblock/stack.rb

Constant Summary collapse

NULL_AUTHORIZER_PROC =
lambda { |object| false }

Instance Method Summary collapse

Constructor Details

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

The Stack allows you to create middleware layers of authorizer to reduce duplication, escape early, etc.

Parameters:

  • auth_object (Object)

    the object to authorize for. Usually a user.

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

    the scopes (if any) associated with the auth_object.



13
14
15
16
17
# File 'lib/roadblock/stack.rb', line 13

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/roadblock/stack.rb', line 56

def method_missing(method, *args)
  match = /can_(.*?)\?/.match(method)

  if match
    can?(match[1], *args)
  else
    super
  end
end

Instance Method Details

#add(*auths) ⇒ Array<Authorizer>

Adds one or more authorizers to the Stack.

Parameters:

  • *auths (Authorizer)

    the authorizer(s) to add to the Stack.

Returns:



24
25
26
# File 'lib/roadblock/stack.rb', line 24

def add(*auths)
  self.authorizers = authorizers + auths
end

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

Checks if the given action can be performed on all the objects.

Parameters:

  • action (Symbol)

    the action the authorize for.

  • objects (Array<Object>)

    the objects to authorize against.

Returns:

  • (true, false)


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/roadblock/stack.rb', line 34

def can?(action, objects)
  objects = [*objects]

  stack = authorizers.reverse.inject(NULL_AUTHORIZER_PROC) do |authorizer_proc, authorizer_klass|
    lambda { |obj|
      authorizer = authorizer_klass.new(auth_object, scopes: scopes)

      authorizer.can?(action, obj) do |inner_obj|
        authorizer.send("can_#{action}?", inner_obj, &authorizer_proc)
      end
    }
  end

  objects
    .map { |object| stack.call(object) }
    .all?
end

#respond_to?(method) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/roadblock/stack.rb', line 52

def respond_to?(method)
  /can_(.*?)\?/.match(method) ? true : false
end