Class: Role
- Inherits:
-
Object
- Object
- Role
- Defined in:
- lib/turnstile/role.rb
Overview
Role class Provides all needed methods to handle a role and its rules
Constant Summary collapse
- @@roles =
Class variable to keep control of all roles
{}
- @@default =
Class variable to keep the default role, it means that if there is no current_role than the default is used
nil
Instance Attribute Summary collapse
-
#name ⇒ Object
Returns the value of attribute name.
-
#rules ⇒ Object
Returns the value of attribute rules.
Class Method Summary collapse
-
.add_role(role) ⇒ Object
Class methods Adds a role to the roles hash.
- .all_roles ⇒ Object
-
.clear ⇒ Object
Remove all role from memory, used for tests so far.
- .default_role ⇒ Object
-
.find(role_sym) ⇒ Object
Find a role by its name Role.find(:admin) returns Role or nil.
- .first ⇒ Object
- .set_default_role(role) ⇒ Object
Instance Method Summary collapse
-
#accessible_controllers ⇒ Object
Return all(array) controllers that an user can access.
-
#allowed_actions_in(controller) ⇒ Object
Return the allowed actions in a controller for the current_role.
-
#denied_actions_in(controller) ⇒ Object
Return the denied actions in a controller for the current_role.
-
#initialize(role) ⇒ Role
constructor
A new instance of Role.
-
#is_allowed_to?(action, controller) ⇒ Boolean
Return if a role is allowed to perform an action in a controller current_role.is_allowed_to? :create, :posts.
-
#merge_rules(new_rules) ⇒ Object
Merges a set of rules with the current_role rules current_role.merge_rules(set_of_rules[]) Used to apply rules to an user and for inheritance.
Constructor Details
#initialize(role) ⇒ Role
Returns a new instance of Role.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/turnstile/role.rb', line 48 def initialize(role) @name = role[:name] @rules = role[:rules] # Helper for each initialized role # is_role? for a role with name 'role' # is_admin? when admin role is instantiated Role.class_eval <<-METHOD def is_#{@name}? @name == '#{@name}' end METHOD Role.add_role self end |
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
5 6 7 |
# File 'lib/turnstile/role.rb', line 5 def name @name end |
#rules ⇒ Object
Returns the value of attribute rules.
5 6 7 |
# File 'lib/turnstile/role.rb', line 5 def rules @rules end |
Class Method Details
.add_role(role) ⇒ Object
Class methods Adds a role to the roles hash
16 17 18 |
# File 'lib/turnstile/role.rb', line 16 def self.add_role(role) @@roles[role.name.to_sym] = role end |
.all_roles ⇒ Object
20 21 22 |
# File 'lib/turnstile/role.rb', line 20 def self.all_roles @@roles end |
.clear ⇒ Object
Remove all role from memory, used for tests so far
36 37 38 |
# File 'lib/turnstile/role.rb', line 36 def self.clear @@roles = {} end |
.default_role ⇒ Object
44 45 46 |
# File 'lib/turnstile/role.rb', line 44 def self.default_role @@default end |
.find(role_sym) ⇒ Object
Find a role by its name Role.find(:admin) returns Role or nil
27 28 29 |
# File 'lib/turnstile/role.rb', line 27 def self.find(role_sym) @@roles[role_sym] end |
.first ⇒ Object
31 32 33 |
# File 'lib/turnstile/role.rb', line 31 def self.first @@roles.first end |
.set_default_role(role) ⇒ Object
40 41 42 |
# File 'lib/turnstile/role.rb', line 40 def self.set_default_role(role) @@default = role end |
Instance Method Details
#accessible_controllers ⇒ Object
Return all(array) controllers that an user can access
66 67 68 69 70 71 72 73 74 |
# File 'lib/turnstile/role.rb', line 66 def accessible_controllers controllers = [] @rules.each do |rule| if rule.allows? controllers << rule.controller end end controllers.uniq end |
#allowed_actions_in(controller) ⇒ Object
Return the allowed actions in a controller for the current_role
77 78 79 80 81 82 83 84 85 |
# File 'lib/turnstile/role.rb', line 77 def allowed_actions_in(controller) actions = [] @rules.each do |rule| if rule.controller == controller.to_s and rule.allows? actions << rule.action end end actions.uniq end |
#denied_actions_in(controller) ⇒ Object
Return the denied actions in a controller for the current_role
88 89 90 91 92 93 94 95 96 |
# File 'lib/turnstile/role.rb', line 88 def denied_actions_in(controller) actions = [] @rules.each do |rule| if rule.controller == controller.to_s and rule.denies? actions << rule.action end end actions.uniq end |
#is_allowed_to?(action, controller) ⇒ Boolean
Return if a role is allowed to perform an action in a controller current_role.is_allowed_to? :create, :posts
100 101 102 103 104 105 106 107 |
# File 'lib/turnstile/role.rb', line 100 def is_allowed_to?(action, controller) @rules.each do |rule| if rule.action == action.to_s and rule.controller == controller.to_s return rule.allows? end end false end |
#merge_rules(new_rules) ⇒ Object
Merges a set of rules with the current_role rules current_role.merge_rules(set_of_rules[]) Used to apply rules to an user and for inheritance
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/turnstile/role.rb', line 112 def merge_rules(new_rules) self.rules ||= [] new_set = new_rules overwritten_set = remove_set = [] self.rules.each do |rule| new_rules.each do |included_rule| if included_rule.action == rule.action and included_rule.controller == rule.controller overwritten_set << included_rule remove_set << rule new_set.delete(included_rule) end end end self.rules = self.rules - remove_set + overwritten_set + new_set self.rules end |