Module: Lockdown::Rules

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



3
4
5
# File 'lib/lockdown/rules.rb', line 3

def options
  @options
end

#permission_objectsObject (readonly)

Returns the value of attribute permission_objects.



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

def permission_objects
  @permission_objects
end

#permissionsObject

Returns the value of attribute permissions.



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

def permissions
  @permissions
end

#protected_accessObject (readonly)

Returns the value of attribute protected_access.



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

def protected_access
  @protected_access
end

#public_accessObject (readonly)

Returns the value of attribute public_access.



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

def public_access
  @public_access
end

#user_groupsObject

Returns the value of attribute user_groups.



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

def user_groups
  @user_groups
end

Instance Method Details

#access_rights_for_permission(perm) ⇒ Object

Return array of controller/action for a permission



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

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.



162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/lockdown/rules.rb', line 162

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

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

#access_rights_for_user_group(user_group_sym) ⇒ Object

Return array of controller/action for a user group



178
179
180
181
182
183
184
# File 'lib/lockdown/rules.rb', line 178

def access_rights_for_user_group(user_group_sym)
  res = []
  permissions_for_user_group(user_group_sym).each do |perm|
    res << access_rights_for_permission(perm)
  end
  res.flatten
end

#administrator?(usr) ⇒ Boolean

Test user for administrator rights

Returns:

  • (Boolean)


197
198
199
# File 'lib/lockdown/rules.rb', line 197

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

#get_permissionsObject

Returns array of permission names as symbols



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

def get_permissions
  permissions.keys
end

#get_user_groupsObject

Returns array of user group names as symbols



130
131
132
# File 'lib/lockdown/rules.rb', line 130

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



149
150
151
152
153
# File 'lib/lockdown/rules.rb', line 149

def make_user_administrator(usr)
  user_groups = usr.send(Lockdown.user_groups_hbtm_reference)
  user_groups << Lockdown.user_group_class.
    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)


125
126
127
# File 'lib/lockdown/rules.rb', line 125

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)


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

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.



238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/lockdown/rules.rb', line 238

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



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/lockdown/rules.rb', line 252

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

  if has_user_group?(sym)
    permissions = user_groups[sym] || []
  else
    if ug.respond_to?(:permissions)
      permissions = ug.permissions
    else
      raise GroupUndefinedError, "#{ug} not found in init.rb and does not respond to #permissions"
    end
  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



281
282
283
284
# File 'lib/lockdown/rules.rb', line 281

def process_rules
  parse_permissions
  validate_user_groups
end

#protected_access?(perm_symbol) ⇒ Boolean

returns true if the permission is public

Returns:

  • (Boolean)


119
120
121
122
# File 'lib/lockdown/rules.rb', line 119

def protected_access?(perm_symbol)
  obj = find_permission_object(perm_symbol)
  obj.nil? ? false : obj.protected_access?
end

#public_access?(perm_symbol) ⇒ Boolean

returns true if the permission is public

Returns:

  • (Boolean)


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

def public_access?(perm_symbol)
  obj = find_permission_object(perm_symbol)
  obj.nil? ? false : obj.public_access? 
end

#set_defaultsObject



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

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

  @permission_objects = {}

  @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 => ' | ',
    :user_group_model => "UserGroup",
    :user_model => "User" 
  }
end

#set_permission(name) ⇒ Object

Creates new permission object

Refer to the Permission object for the full functionality


43
44
45
# File 'lib/lockdown/rules.rb', line 43

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)


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

def set_protected_access(*perms)
  perms.each do |perm_symbol|
    perm = find_permission_object(perm_symbol)
    if perm
      perm.set_as_protected_access 
    else
      msg = "Permission not found: #{perm_symbol}"
      raise Lockdown::InvalidRuleAssignment, msg
    end
  end
end

#set_public_access(*perms) ⇒ Object

Defines public access by the permission symbols

Example

set_public_access(:permission_one, :permission_two)


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

def set_public_access(*perms)
  perms.each do |perm_symbol|
    perm = find_permission_object(perm_symbol)
    if perm
      perm.set_as_public_access 
    else
      msg = "Permission not found: #{perm_symbol}"
      raise Lockdown::InvalidRuleAssignment, 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)


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

def set_user_group(name, *perms)
  user_groups[name] ||= []
  perms.each do |perm|
    if permission_assigned_automatically?(perm)
      raise Lockdown::InvalidPermissionAssignment, "Permission is assigned automatically.  Please remove it from #{name} user group"
    end
    user_groups[name].push(perm)
  end
end

#standard_authorized_user_rightsObject

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



157
158
159
# File 'lib/lockdown/rules.rb', line 157

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)


136
137
138
139
# File 'lib/lockdown/rules.rb', line 136

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.



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/lockdown/rules.rb', line 212

def user_groups_assignable_for_user(usr)
  return [] if usr.nil?
  ug_table = Lockdown.user_groups_hbtm_reference.to_s
  if administrator?(usr)
    Lockdown.user_group_class.find_by_sql "      select \#{ug_table}.* from \#{ug_table} order by \#{ug_table}.name\n    SQL\n  else\n    usr_table = Lockdown.users_hbtm_reference.to_s\n    if usr_table < ug_table\n      join_table = \"\#{usr_table}_\#{ug_table}\"\n    else\n      join_table = \"\#{ug_table}_\#{usr_table}\"\n    end\n    Lockdown.user_group_class.find_by_sql <<-SQL\n        select \#{ug_table}.* from \#{ug_table}, \#{join_table}\n         where \#{ug_table}.id = \#{join_table}.\#{Lockdown.user_group_id_reference}\n         and \#{join_table}.\#{Lockdown.user_id_reference} = \#{usr.id}   \n         order by \#{ug_table}.name\n    SQL\n  end\nend\n"

#user_has_user_group?(usr, sym) ⇒ Boolean

Pass in user object and symbol for name of user group

Returns:

  • (Boolean)


202
203
204
205
206
207
# File 'lib/lockdown/rules.rb', line 202

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