Class: Aclize::Acl::PathsRegistry

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePathsRegistry

Returns a new instance of PathsRegistry.



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

def initialize
  @permitted = []
  @denied    = []
end

Instance Attribute Details

#deniedObject (readonly)

Returns the value of attribute denied.



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

def denied
  @denied
end

#permittedObject (readonly)

Returns the value of attribute permitted.



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

def permitted
  @permitted
end

Instance Method Details

#denied?(*args) ⇒ Boolean

Check if any of the paths is explicitly denied

Returns:

  • (Boolean)


61
62
63
64
65
66
67
68
69
# File 'lib/aclize/acl/paths_registry.rb', line 61

def denied?(*args)
  @denied.each do |denied_path|
    args.flatten.each do |path|
      return true if path.match(/^#{denied_path}$/)
    end
  end

  return false
end

#deny(*paths) ⇒ Object

deny a path



27
28
29
30
# File 'lib/aclize/acl/paths_registry.rb', line 27

def deny(*paths)
  @denied += normalize(paths)
  @denied.uniq!
end

#permit(*paths) ⇒ Object

permit a new path



20
21
22
23
# File 'lib/aclize/acl/paths_registry.rb', line 20

def permit(*paths)
  @permitted += normalize(paths)
  @permitted.uniq!
end

#permitted?(*args) ⇒ Boolean

Check if the paths are permitted. This method should return true only if each path passed as argument is permitted (isn’t denied and have an explicit permission).

Returns:

  • (Boolean)


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/aclize/acl/paths_registry.rb', line 36

def permitted?(*args)
  permitted = false

  return false if denied?(args)

  # each path should have an explicit permission in order to return true
  args.flatten.each do |path|
    # we assume that the path isn't permitted
    permitted = false

    # iterate over permitted paths and check if any of them matches the current one
    @permitted.each do |permitted_path|
      permitted ||= !!path.match(/^#{permitted_path}$/)
      # stop iteration if the path is permitted
      break if permitted
    end
    #return false if the path isn't permitted
    return false unless permitted
  end

  return permitted
end