Class: Aclize::Acl::ControllersRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/aclize/acl/controllers_registry.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeControllersRegistry

Returns a new instance of ControllersRegistry.



13
14
15
16
# File 'lib/aclize/acl/controllers_registry.rb', line 13

def initialize
  @permitted = {"*" => []}.nested_under_indifferent_access
  @denied    = {"*" => []}.nested_under_indifferent_access
end

Instance Attribute Details

#deniedObject (readonly)

Returns the value of attribute denied.



11
12
13
# File 'lib/aclize/acl/controllers_registry.rb', line 11

def denied
  @denied
end

#permittedObject (readonly)

Returns the value of attribute permitted.



11
12
13
# File 'lib/aclize/acl/controllers_registry.rb', line 11

def permitted
  @permitted
end

Instance Method Details

#permit(controller, only: nil, except: nil) ⇒ Object

add a new permit rule to controllers registry

Raises:

  • (ArgumentError)


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/aclize/acl/controllers_registry.rb', line 19

def permit(controller, only: nil, except: nil)
  @permitted[controller] ||= []
  @denied[controller]    ||= []

  raise ArgumentError.new("#permit cannot accept both :only and :except. At most one of them can be specified!") if only && except

  if except
    @permitted[controller] = ["*"]
    @denied[controller]    = normalize(except)
  elsif only
    @denied[controller]    = []
    @permitted[controller] = normalize(only)
  else
    @permitted[controller] = ["*"]
    @denied[controller]    = []
  end
end

#permitted?(controller, *args) ⇒ Boolean

check if each action in the list is allowed for the specified controller

Returns:

  • (Boolean)


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/aclize/acl/controllers_registry.rb', line 38

def permitted?(controller, *args)
  @permitted[controller] ||= []
  @denied[controller]    ||= []
  actions = normalize(args)

  if actions.empty?
    return controller_permitted?(controller)
  elsif controller_permitted?(controller)
    # we know the there's at least one permitted action for this controller,
    # so return false if there's at least one denied action in the list of actions to check
    return false unless (actions & @denied[controller]).empty?

    # we know that the actions aren't denied at controller level, so we could
    # return true if all the actions are also permitted at controller level
    return true if @permitted[controller].include?("*") || (actions & @permitted[controller]) == actions

    # the actions aren't permitted at controller level, so if any of them is
    # denied at global level, we will return false
    return false unless (actions & @denied["*"]).empty?

    # the actions aren't denied at global level, so we have to check if them
    # are allowed at global level and return true if so
    return true if @permitted["*"].include?("*") || (actions & @permitted["*"]) == actions
  end

  return false
end