Module: Lockdown::Rules

Included in:
System
Defined in:
lib/lockdown/rules.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#controller_classesObject

Returns the value of attribute controller_classes.



8
9
10
# File 'lib/lockdown/rules.rb', line 8

def controller_classes
  @controller_classes
end

#optionsObject

Returns the value of attribute options.



5
6
7
# File 'lib/lockdown/rules.rb', line 5

def options
  @options
end

#permission_objectsObject (readonly)

Returns the value of attribute permission_objects.



13
14
15
# File 'lib/lockdown/rules.rb', line 13

def permission_objects
  @permission_objects
end

#permissionsObject

Returns the value of attribute permissions.



6
7
8
# File 'lib/lockdown/rules.rb', line 6

def permissions
  @permissions
end

#protected_accessObject (readonly)

Returns the value of attribute protected_access.



10
11
12
# File 'lib/lockdown/rules.rb', line 10

def protected_access
  @protected_access
end

#public_accessObject (readonly)

Returns the value of attribute public_access.



11
12
13
# File 'lib/lockdown/rules.rb', line 11

def public_access
  @public_access
end

#user_groupsObject

Returns the value of attribute user_groups.



7
8
9
# File 'lib/lockdown/rules.rb', line 7

def user_groups
  @user_groups
end

Instance Method Details

#access_rights_for_permission(perm) ⇒ Object

Return array of controller/action for a permission



173
174
175
176
177
178
179
# File 'lib/lockdown/rules.rb', line 173

def access_rights_for_permission(perm)
  sym = Lockdown.get_symbol(perm)

  permissions[sym]
rescue 
  raise SecurityError, "Permission requested is not defined: #{sym}"
end

#access_rights_for_user(usr) ⇒ Object

Return array of controller/action values user can access.



158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/lockdown/rules.rb', line 158

def access_rights_for_user(usr)
  return unless usr
  return :all if administrator?(usr)

  rights = standard_authorized_user_rights
    
  usr.user_groups.each do |grp|
    permissions_for_user_group(grp).each do |perm|
      rights += access_rights_for_permission(perm) 
    end
  end
  rights
end

#administrator?(usr) ⇒ Boolean

Test user for administrator rights

Returns:

  • (Boolean)


183
184
185
# File 'lib/lockdown/rules.rb', line 183

def administrator?(usr)
  user_has_user_group?(usr, Lockdown.administrator_group_symbol)
end

#get_permissionsObject

Returns array of permission names as symbols



100
101
102
# File 'lib/lockdown/rules.rb', line 100

def get_permissions
  permissions.keys
end

#get_user_groupsObject

Returns array of user group names as symbols



127
128
129
# File 'lib/lockdown/rules.rb', line 127

def get_user_groups
  user_groups.keys
end

#make_user_administrator(usr) ⇒ Object

Pass in a user object to be associated to the administrator user group The group will be created if it doesn’t exist



146
147
148
149
# File 'lib/lockdown/rules.rb', line 146

def make_user_administrator(usr)
  usr.user_groups << UserGroup.
    find_or_create_by_name(Lockdown.administrator_group_string)
end

#permission_assigned_automatically?(permmision_symbol) ⇒ Boolean

These permissions are assigned by the system

Returns:

  • (Boolean)


122
123
124
# File 'lib/lockdown/rules.rb', line 122

def permission_assigned_automatically?(permmision_symbol)
  public_access?(permmision_symbol) || protected_access?(permmision_symbol)
end

#permission_exists?(permission_symbol) ⇒ Boolean Also known as: has_permission?

Is the permission defined?

Returns:

  • (Boolean)


105
106
107
# File 'lib/lockdown/rules.rb', line 105

def permission_exists?(permission_symbol)
  get_permissions.include?(permission_symbol)
end

#permissions_assignable_for_user(usr) ⇒ Object

Similar to user_groups_assignable_for_user, this method should be used to restrict users from creating a user group with more power than they have been allowed.



217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/lockdown/rules.rb', line 217

def permissions_assignable_for_user(usr)
  return [] if usr.nil?
  if administrator?(usr)
    get_permissions.collect do |k| 
      ::Permission.find_by_name(Lockdown.get_string(k))
    end.compact
  else
    user_groups_assignable_for_user(usr).collect do |g| 
      g.permissions
    end.flatten.compact
  end
end

#permissions_for_user_group(ug) ⇒ Object

Returns and array of permission symbols for the user group



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/lockdown/rules.rb', line 231

def permissions_for_user_group(ug)
  sym = Lockdown.get_symbol(ug)
  perm_array = []  

  if has_user_group?(sym)
    permissions = user_groups[sym] || []
  else
    permissions = ug.permissions
  end


  permissions.each do |perm|
    perm_sym = Lockdown.get_symbol(perm)

    unless permission_exists?(perm_sym)
      msg = "Permission associated to User Group is invalid: #{perm}"
      raise SecurityError, msg
    end

    perm_array << perm_sym
  end

  perm_array 
end

#process_rulesObject



256
257
258
259
# File 'lib/lockdown/rules.rb', line 256

def process_rules
  parse_permissions
  validate_user_groups
end

#protected_access?(permmision_symbol) ⇒ Boolean

returns true if the permission is public

Returns:

  • (Boolean)


117
118
119
# File 'lib/lockdown/rules.rb', line 117

def protected_access?(permmision_symbol)
  protected_access.include?(permmision_symbol)
end

#public_access?(permmision_symbol) ⇒ Boolean

returns true if the permission is public

Returns:

  • (Boolean)


112
113
114
# File 'lib/lockdown/rules.rb', line 112

def public_access?(permmision_symbol)
  public_access.include?(permmision_symbol)
end

#set_defaultsObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/lockdown/rules.rb', line 15

def set_defaults
  @permissions  = {}
  @user_groups  = {}
  @options      = {}

  @permission_objects = {}

  @controller_classes = []
  @public_access      = []
  @protected_access   = []

  @options = {
    :session_timeout => (60 * 60),
    :who_did_it => :current_user_id,
    :default_who_did_it => 1,
    :logout_on_access_violation => false,
    :access_denied_path => "/",
    :successful_login_path => "/",
    :subdirectory => nil,
    :skip_db_sync_in => ["test"],
    :link_separator => ' | '
  }
end

#set_permission(name) ⇒ Object

Creates new permission object

Refer to the Permission object for the full functionality


45
46
47
# File 'lib/lockdown/rules.rb', line 45

def set_permission(name)
  @permission_objects[name] = Lockdown::Permission.new(name)
end

#set_protected_access(*perms) ⇒ Object

Defines protected access by the permission symbols

Example

set_public_access(:permission_one, :permission_two)


71
72
73
74
75
76
77
78
79
80
81
# File 'lib/lockdown/rules.rb', line 71

def set_protected_access(*perms)
  perms.each do |perm_symbol|
    perm = permission_objects.find{|name, pobj| pobj.name == perm_symbol}
    if perm
      perm[1].set_as_protected_access 
    else
      msg = "Permission not found: #{perm_symbol}"
      raise InvalidRuleAssigment, msg
    end
  end
end

#set_public_access(*perms) ⇒ Object

Defines public access by the permission symbols

Example

set_public_access(:permission_one, :permission_two)


54
55
56
57
58
59
60
61
62
63
64
# File 'lib/lockdown/rules.rb', line 54

def set_public_access(*perms)
  perms.each do |perm_symbol|
    perm = permission_objects.find{|name, pobj| pobj.name == perm_symbol}
    if perm
      perm[1].set_as_public_access 
    else
      msg = "Permission not found: #{perm_symbol}"
      raise InvalidRuleAssigment, msg
    end
  end
end

#set_user_group(name, *perms) ⇒ Object

Define a user groups by name and permission symbol(s)

Example

set_user_group(:managment_group, :permission_one, :permission_two)


88
89
90
91
92
93
# File 'lib/lockdown/rules.rb', line 88

def set_user_group(name, *perms)
  user_groups[name] ||= []
  perms.each do |perm|
    user_groups[name].push(perm)
  end
end

#standard_authorized_user_rightsObject

Returns array of controller/action values all logged in users can access.



153
154
155
# File 'lib/lockdown/rules.rb', line 153

def standard_authorized_user_rights
  public_access + protected_access 
end

#user_group_exists?(user_group_symbol) ⇒ Boolean Also known as: has_user_group?

Is the user group defined?

The :administrators user group always exists

Returns:

  • (Boolean)


133
134
135
136
# File 'lib/lockdown/rules.rb', line 133

def user_group_exists?(user_group_symbol)
  return true if user_group_symbol == Lockdown.administrator_group_symbol
  get_user_groups.include?(user_group_symbol)
end

#user_groups_assignable_for_user(usr) ⇒ Object

Use this for the management screen to restrict user group list to the user. This will prevent a user from creating a user with more power than him/her self.



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/lockdown/rules.rb', line 197

def user_groups_assignable_for_user(usr)
  return [] if usr.nil?
    
  if administrator?(usr)
    UserGroup.find_by_sql <<-SQL
      select user_groups.* from user_groups order by user_groups.name
    SQL
  else
    UserGroup.find_by_sql <<-SQL
        select user_groups.* from user_groups, user_groups_users
         where user_groups.id = user_groups_users.user_group_id
         and user_groups_users.user_id = #{usr.id}	 
         order by user_groups.name
    SQL
  end
end

#user_has_user_group?(usr, sym) ⇒ Boolean

Pass in user object and symbol for name of user group

Returns:

  • (Boolean)


188
189
190
191
192
# File 'lib/lockdown/rules.rb', line 188

def user_has_user_group?(usr, sym)
  usr.user_groups.any? do |ug|
    Lockdown.convert_reference_name(ug.name) == sym
  end
end