Class: Role

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/turnstile/role.rb', line 5

def name
  @name
end

#rulesObject

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_rolesObject



20
21
22
# File 'lib/turnstile/role.rb', line 20

def self.all_roles
  @@roles
end

.clearObject

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_roleObject



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

.firstObject



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_controllersObject

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

Returns:

  • (Boolean)


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