Class: Rolypoly::RoleGatekeeper
- Inherits:
-
Object
- Object
- Rolypoly::RoleGatekeeper
- Defined in:
- lib/rolypoly/role_gatekeeper.rb
Instance Attribute Summary collapse
-
#roles ⇒ Object
readonly
Returns the value of attribute roles.
Instance Method Summary collapse
- #action?(check_actions) ⇒ Boolean
- #all_public ⇒ Object
-
#allow(*roles) ⇒ Object
on(resource).allow(*roles).to_access(*actions).
- #allow?(current_roles, action, options = {}) ⇒ Boolean
- #allowed_roles(current_roles, action, options = {}) ⇒ Object
-
#initialize(roles, actions, resource = nil) ⇒ RoleGatekeeper
constructor
A new instance of RoleGatekeeper.
- #initialize_copy(other) ⇒ Object
-
#on(resource) ⇒ Object
allow(*roles).on(resource).to_access(*actions).
- #public? ⇒ Boolean
-
#restrict(*actions) ⇒ Object
on(resource).restrict(*actions).to(*roles).
- #role?(check_roles, options = {}) ⇒ Boolean
-
#to(*roles) ⇒ Object
restrict(*actions).to *roles.
-
#to_access(*actions) ⇒ Object
allow(*roles).to_access *actions.
-
#to_all ⇒ Object
allow role access to all actions allow(*roles).to_all.
-
#to_none ⇒ Object
make actions public basically restrict(:index).to_none.
Constructor Details
#initialize(roles, actions, resource = nil) ⇒ RoleGatekeeper
Returns a new instance of RoleGatekeeper.
5 6 7 8 9 10 11 |
# File 'lib/rolypoly/role_gatekeeper.rb', line 5 def initialize(roles, actions, resource = nil) self.roles = Set.new Array(roles).map(&:to_s) self.actions = Set.new Array(actions).map(&:to_s) self.resource = resource self.all_actions = false self.public = false end |
Instance Attribute Details
#roles ⇒ Object
Returns the value of attribute roles.
4 5 6 |
# File 'lib/rolypoly/role_gatekeeper.rb', line 4 def roles @roles end |
Instance Method Details
#action?(check_actions) ⇒ Boolean
103 104 105 106 |
# File 'lib/rolypoly/role_gatekeeper.rb', line 103 def action?(check_actions) check_actions = Set.new Array(check_actions).map(&:to_s) all_actions? || !(check_actions & actions).empty? end |
#all_public ⇒ Object
67 68 69 70 |
# File 'lib/rolypoly/role_gatekeeper.rb', line 67 def all_public self.public = true self.all_actions = true end |
#allow(*roles) ⇒ Object
on(resource).allow(*roles).to_access(*actions)
19 20 21 22 |
# File 'lib/rolypoly/role_gatekeeper.rb', line 19 def allow(*roles) to(*roles) self end |
#allow?(current_roles, action, options = {}) ⇒ Boolean
58 59 60 |
# File 'lib/rolypoly/role_gatekeeper.rb', line 58 def allow?(current_roles, action, = {}) action?(action) && role?(current_roles, ) end |
#allowed_roles(current_roles, action, options = {}) ⇒ Object
62 63 64 65 |
# File 'lib/rolypoly/role_gatekeeper.rb', line 62 def allowed_roles(current_roles, action, = {}) return [] if public? || !action?(action) match_roles(current_roles, ) end |
#initialize_copy(other) ⇒ Object
13 14 15 16 |
# File 'lib/rolypoly/role_gatekeeper.rb', line 13 def initialize_copy(other) @roles = @roles.dup @actions = @actions.dup end |
#on(resource) ⇒ Object
allow(*roles).on(resource).to_access(*actions)
31 32 33 34 |
# File 'lib/rolypoly/role_gatekeeper.rb', line 31 def on(resource) self.resource = resource self end |
#public? ⇒ Boolean
108 109 110 |
# File 'lib/rolypoly/role_gatekeeper.rb', line 108 def public? !!public end |
#restrict(*actions) ⇒ Object
on(resource).restrict(*actions).to(*roles)
25 26 27 28 |
# File 'lib/rolypoly/role_gatekeeper.rb', line 25 def restrict(*actions) to_access(*actions) self end |
#role?(check_roles, options = {}) ⇒ Boolean
72 73 74 75 76 77 78 79 |
# File 'lib/rolypoly/role_gatekeeper.rb', line 72 def role?(check_roles, = {}) return true if public? required_resource = find_required_resource() Array(check_roles).any? do |check_role| allowed_role?(check_role) && allowed_resource?(check_role, required_resource) end end |
#to(*roles) ⇒ Object
restrict(*actions).to *roles
37 38 39 |
# File 'lib/rolypoly/role_gatekeeper.rb', line 37 def to(*roles) self.roles = self.roles.merge roles.flatten.compact.map(&:to_s) end |
#to_access(*actions) ⇒ Object
allow(*roles).to_access *actions
48 49 50 |
# File 'lib/rolypoly/role_gatekeeper.rb', line 48 def to_access(*actions) self.actions = self.actions.merge actions.flatten.compact.map(&:to_s) end |
#to_all ⇒ Object
allow role access to all actions allow(*roles).to_all
54 55 56 |
# File 'lib/rolypoly/role_gatekeeper.rb', line 54 def to_all self.all_actions = true end |
#to_none ⇒ Object
make actions public basically restrict(:index).to_none
43 44 45 |
# File 'lib/rolypoly/role_gatekeeper.rb', line 43 def to_none self.public = true end |