Class: AccessGranted::Role
- Inherits:
-
Object
- Object
- AccessGranted::Role
- Defined in:
- lib/access-granted/role.rb
Instance Attribute Summary collapse
-
#conditions ⇒ Object
readonly
Returns the value of attribute conditions.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#permissions ⇒ Object
readonly
Returns the value of attribute permissions.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Instance Method Summary collapse
- #add_permission(granted, action, subject, conditions, block) ⇒ Object
- #applies_to?(user) ⇒ Boolean
- #can(action, subject = nil, conditions = {}, &block) ⇒ Object
- #cannot(action, subject, conditions = {}, &block) ⇒ Object
- #configure ⇒ Object
- #find_permission(action, subject) ⇒ Object
-
#initialize(name, conditions = nil, user = nil, block = nil) ⇒ Role
constructor
A new instance of Role.
- #matches_hash?(user, conditions = {}) ⇒ Boolean
Constructor Details
#initialize(name, conditions = nil, user = nil, block = nil) ⇒ Role
Returns a new instance of Role.
5 6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/access-granted/role.rb', line 5 def initialize(name, conditions = nil, user = nil, block = nil) @user = user @name = name @conditions = conditions @block = block @permissions = [] @permissions_by_action = {} if @block instance_eval(&@block) else configure end end |
Instance Attribute Details
#conditions ⇒ Object (readonly)
Returns the value of attribute conditions.
3 4 5 |
# File 'lib/access-granted/role.rb', line 3 def conditions @conditions end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/access-granted/role.rb', line 3 def name @name end |
#permissions ⇒ Object (readonly)
Returns the value of attribute permissions.
3 4 5 |
# File 'lib/access-granted/role.rb', line 3 def @permissions end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
3 4 5 |
# File 'lib/access-granted/role.rb', line 3 def user @user end |
Instance Method Details
#add_permission(granted, action, subject, conditions, block) ⇒ Object
53 54 55 56 57 58 59 60 |
# File 'lib/access-granted/role.rb', line 53 def (granted, action, subject, conditions, block) prepare_actions(action).each do |a| raise DuplicatePermission if (a, subject) @permissions << Permission.new(granted, a, subject, @user, conditions, block) @permissions_by_action[a] ||= [] @permissions_by_action[a] << @permissions.size - 1 end end |
#applies_to?(user) ⇒ Boolean
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/access-granted/role.rb', line 36 def applies_to?(user) case @conditions when Hash matches_hash?(user, @conditions) when Proc @conditions.call(user) else true end end |
#can(action, subject = nil, conditions = {}, &block) ⇒ Object
22 23 24 |
# File 'lib/access-granted/role.rb', line 22 def can(action, subject = nil, conditions = {}, &block) (true, action, subject, conditions, block) end |
#cannot(action, subject, conditions = {}, &block) ⇒ Object
26 27 28 |
# File 'lib/access-granted/role.rb', line 26 def cannot(action, subject, conditions = {}, &block) (false, action, subject, conditions, block) end |
#configure ⇒ Object
19 20 |
# File 'lib/access-granted/role.rb', line 19 def configure end |
#find_permission(action, subject) ⇒ Object
30 31 32 33 34 |
# File 'lib/access-granted/role.rb', line 30 def (action, subject) (action).detect do || .matches_subject?(subject) && .matches_conditions?(subject) end end |
#matches_hash?(user, conditions = {}) ⇒ Boolean
47 48 49 50 51 |
# File 'lib/access-granted/role.rb', line 47 def matches_hash?(user, conditions = {}) conditions.all? do |name, value| user.send(name) == value end end |