lockdown 0.7.1 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. data/.DS_Store +0 -0
  2. data/History.txt +3 -0
  3. data/README.txt +1 -1
  4. data/Rakefile +16 -1
  5. data/lib/lockdown/context.rb +41 -0
  6. data/lib/lockdown/database.rb +11 -14
  7. data/lib/lockdown/frameworks/rails/controller.rb +57 -4
  8. data/lib/lockdown/frameworks/rails/view.rb +1 -1
  9. data/lib/lockdown/frameworks/rails.rb +21 -10
  10. data/lib/lockdown/helper.rb +1 -1
  11. data/lib/lockdown/permission.rb +204 -0
  12. data/lib/lockdown/rules.rb +287 -0
  13. data/lib/lockdown/session.rb +8 -6
  14. data/lib/lockdown/system.rb +35 -88
  15. data/lib/lockdown.rb +52 -49
  16. data/rails_generators/.DS_Store +0 -0
  17. data/rails_generators/lockdown/.DS_Store +0 -0
  18. data/rails_generators/lockdown/lockdown_generator.rb +5 -5
  19. data/rails_generators/lockdown/templates/.DS_Store +0 -0
  20. data/rails_generators/lockdown/templates/lib/.DS_Store +0 -0
  21. data/rails_generators/lockdown/templates/lib/lockdown/init.rb +27 -19
  22. data/rails_generators/lockdown/templates/lib/lockdown/session.rb +1 -3
  23. data/spec/lockdown/database_spec.rb +158 -0
  24. data/spec/lockdown/frameworks/rails/controller_spec.rb +220 -0
  25. data/spec/lockdown/frameworks/rails/view_spec.rb +87 -0
  26. data/spec/lockdown/frameworks/rails_spec.rb +170 -0
  27. data/spec/lockdown/permission_spec.rb +156 -0
  28. data/spec/lockdown/rules_spec.rb +109 -0
  29. data/spec/lockdown/session_spec.rb +88 -0
  30. data/spec/lockdown/system_spec.rb +59 -0
  31. data/spec/lockdown_spec.rb +19 -0
  32. data/spec/rcov.opts +5 -0
  33. data/spec/spec.opts +3 -0
  34. data/spec/spec_helper.rb +1 -0
  35. data/tasks/post_load.rake +2 -7
  36. data/tasks/setup.rb +24 -3
  37. metadata +23 -12
  38. data/.gitignore +0 -5
  39. data/Manifest.txt +0 -51
  40. data/lib/lockdown/controller.rb +0 -64
  41. data/lib/lockdown/frameworks/merb/controller.rb +0 -63
  42. data/lib/lockdown/frameworks/merb/view.rb +0 -32
  43. data/lib/lockdown/frameworks/merb.rb +0 -84
  44. data/lib/lockdown/orms/data_mapper.rb +0 -70
  45. data/lib/lockdown/rights.rb +0 -208
  46. data/tasks/manifest.rake +0 -48
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lockdown
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.1
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Stone
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-01-17 00:00:00 -05:00
12
+ date: 2009-02-08 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -20,7 +20,7 @@ dependencies:
20
20
  requirements:
21
21
  - - ">="
22
22
  - !ruby/object:Gem::Version
23
- version: 2.2.0
23
+ version: 2.4.0
24
24
  version:
25
25
  description: Lockdown is a authentication/authorization system for RubyOnRails (ver >= 2.1).
26
26
  email: andy@stonean.com
@@ -32,27 +32,26 @@ extra_rdoc_files:
32
32
  - History.txt
33
33
  - README.txt
34
34
  files:
35
- - .gitignore
35
+ - .DS_Store
36
36
  - History.txt
37
- - Manifest.txt
38
37
  - README.txt
39
38
  - Rakefile
40
39
  - lib/lockdown.rb
41
- - lib/lockdown/controller.rb
40
+ - lib/lockdown/context.rb
42
41
  - lib/lockdown/database.rb
43
- - lib/lockdown/frameworks/merb.rb
44
- - lib/lockdown/frameworks/merb/controller.rb
45
- - lib/lockdown/frameworks/merb/view.rb
46
42
  - lib/lockdown/frameworks/rails.rb
47
43
  - lib/lockdown/frameworks/rails/controller.rb
48
44
  - lib/lockdown/frameworks/rails/view.rb
49
45
  - lib/lockdown/helper.rb
50
46
  - lib/lockdown/orms/active_record.rb
51
- - lib/lockdown/orms/data_mapper.rb
52
- - lib/lockdown/rights.rb
47
+ - lib/lockdown/permission.rb
48
+ - lib/lockdown/rules.rb
53
49
  - lib/lockdown/session.rb
54
50
  - lib/lockdown/system.rb
51
+ - rails_generators/.DS_Store
52
+ - rails_generators/lockdown/.DS_Store
55
53
  - rails_generators/lockdown/lockdown_generator.rb
54
+ - rails_generators/lockdown/templates/.DS_Store
56
55
  - rails_generators/lockdown/templates/app/controllers/permissions_controller.rb
57
56
  - rails_generators/lockdown/templates/app/controllers/sessions_controller.rb
58
57
  - rails_generators/lockdown/templates/app/controllers/user_groups_controller.rb
@@ -81,13 +80,25 @@ files:
81
80
  - rails_generators/lockdown/templates/db/migrate/create_profiles.rb
82
81
  - rails_generators/lockdown/templates/db/migrate/create_user_groups.rb
83
82
  - rails_generators/lockdown/templates/db/migrate/create_users.rb
83
+ - rails_generators/lockdown/templates/lib/.DS_Store
84
84
  - rails_generators/lockdown/templates/lib/lockdown/init.rb
85
85
  - rails_generators/lockdown/templates/lib/lockdown/session.rb
86
+ - spec/lockdown/database_spec.rb
87
+ - spec/lockdown/frameworks/rails/controller_spec.rb
88
+ - spec/lockdown/frameworks/rails/view_spec.rb
89
+ - spec/lockdown/frameworks/rails_spec.rb
90
+ - spec/lockdown/permission_spec.rb
91
+ - spec/lockdown/rules_spec.rb
92
+ - spec/lockdown/session_spec.rb
93
+ - spec/lockdown/system_spec.rb
94
+ - spec/lockdown_spec.rb
95
+ - spec/rcov.opts
96
+ - spec/spec.opts
97
+ - spec/spec_helper.rb
86
98
  - tasks/ann.rake
87
99
  - tasks/bones.rake
88
100
  - tasks/gem.rake
89
101
  - tasks/git.rake
90
- - tasks/manifest.rake
91
102
  - tasks/notes.rake
92
103
  - tasks/post_load.rake
93
104
  - tasks/rdoc.rake
data/.gitignore DELETED
@@ -1,5 +0,0 @@
1
- *.DS_Store
2
- *.swp
3
- pkg/**
4
- doc/**
5
- email.txt
data/Manifest.txt DELETED
@@ -1,51 +0,0 @@
1
- .gitignore
2
- History.txt
3
- Manifest.txt
4
- README.txt
5
- Rakefile
6
- lib/lockdown.rb
7
- lib/lockdown/controller.rb
8
- lib/lockdown/database.rb
9
- lib/lockdown/frameworks/merb.rb
10
- lib/lockdown/frameworks/merb/controller.rb
11
- lib/lockdown/frameworks/merb/view.rb
12
- lib/lockdown/frameworks/rails.rb
13
- lib/lockdown/frameworks/rails/controller.rb
14
- lib/lockdown/frameworks/rails/view.rb
15
- lib/lockdown/helper.rb
16
- lib/lockdown/orms/active_record.rb
17
- lib/lockdown/orms/data_mapper.rb
18
- lib/lockdown/rights.rb
19
- lib/lockdown/session.rb
20
- lib/lockdown/system.rb
21
- rails_generators/lockdown/lockdown_generator.rb
22
- rails_generators/lockdown/templates/app/controllers/permissions_controller.rb
23
- rails_generators/lockdown/templates/app/controllers/sessions_controller.rb
24
- rails_generators/lockdown/templates/app/controllers/user_groups_controller.rb
25
- rails_generators/lockdown/templates/app/controllers/users_controller.rb
26
- rails_generators/lockdown/templates/app/helpers/permissions_helper.rb
27
- rails_generators/lockdown/templates/app/helpers/user_groups_helper.rb
28
- rails_generators/lockdown/templates/app/helpers/users_helper.rb
29
- rails_generators/lockdown/templates/app/models/permission.rb
30
- rails_generators/lockdown/templates/app/models/profile.rb
31
- rails_generators/lockdown/templates/app/models/user.rb
32
- rails_generators/lockdown/templates/app/models/user_group.rb
33
- rails_generators/lockdown/templates/app/views/permissions/index.html.erb
34
- rails_generators/lockdown/templates/app/views/permissions/show.html.erb
35
- rails_generators/lockdown/templates/app/views/sessions/new.html.erb
36
- rails_generators/lockdown/templates/app/views/user_groups/edit.html.erb
37
- rails_generators/lockdown/templates/app/views/user_groups/index.html.erb
38
- rails_generators/lockdown/templates/app/views/user_groups/new.html.erb
39
- rails_generators/lockdown/templates/app/views/user_groups/show.html.erb
40
- rails_generators/lockdown/templates/app/views/users/edit.html.erb
41
- rails_generators/lockdown/templates/app/views/users/index.html.erb
42
- rails_generators/lockdown/templates/app/views/users/new.html.erb
43
- rails_generators/lockdown/templates/app/views/users/show.html.erb
44
- rails_generators/lockdown/templates/config/initializers/lockit.rb
45
- rails_generators/lockdown/templates/db/migrate/create_admin_user.rb
46
- rails_generators/lockdown/templates/db/migrate/create_permissions.rb
47
- rails_generators/lockdown/templates/db/migrate/create_profiles.rb
48
- rails_generators/lockdown/templates/db/migrate/create_user_groups.rb
49
- rails_generators/lockdown/templates/db/migrate/create_users.rb
50
- rails_generators/lockdown/templates/lib/lockdown/init.rb
51
- rails_generators/lockdown/templates/lib/lockdown/session.rb
@@ -1,64 +0,0 @@
1
- module Lockdown
2
- module Controller
3
- module Core
4
- def configure_lockdown
5
- check_session_expiry
6
- store_location
7
- end
8
-
9
- def set_current_user
10
- login_from_basic_auth? unless logged_in?
11
- if logged_in?
12
- Thread.current[:profile_id] = current_profile_id
13
- Thread.current[:client_id] = current_client_id if respond_to? :current_client_id
14
- end
15
- end
16
-
17
- def check_request_authorization
18
- unless authorized?(path_from_hash(params))
19
- raise SecurityError, "Authorization failed for params #{params.inspect}"
20
- end
21
- end
22
-
23
- def path_allowed?(url)
24
- session[:access_rights] ||= Lockdown::System.public_access
25
- session[:access_rights].include?(url)
26
- end
27
-
28
- def check_session_expiry
29
- if session[:expiry_time] && session[:expiry_time] < Time.now
30
- nil_lockdown_values
31
- timeout_method = Lockdown::System.fetch(:session_timeout_method)
32
- if timeout_method.is_a?(Symbol) && self.respond_to?(timeout_method)
33
- send(timeout_method)
34
- end
35
- end
36
- session[:expiry_time] = Time.now + Lockdown::System.fetch(:session_timeout)
37
- end
38
-
39
- def store_location
40
- if (request.method == :get) && (session[:thispage] != sent_from_uri)
41
- session[:prevpage] = session[:thispage] || ''
42
- session[:thispage] = sent_from_uri
43
- end
44
- end
45
-
46
- # Called from current_user. Now, attempt to login by
47
- # basic authentication information.
48
- def login_from_basic_auth?
49
- username, passwd = get_auth_data
50
- if username && passwd
51
- set_session_user User.authenticate(username, passwd)
52
- end
53
- end
54
-
55
- @@http_auth_headers = %w(X-HTTP_AUTHORIZATION HTTP_AUTHORIZATION Authorization)
56
- # gets BASIC auth info
57
- def get_auth_data
58
- auth_key = @@http_auth_headers.detect { |h| request.env.has_key?(h) }
59
- auth_data = request.env[auth_key].to_s.split unless auth_key.blank?
60
- return auth_data && auth_data[0] == 'Basic' ? Base64.decode64(auth_data[1]).split(':')[0..1] : [nil, nil]
61
- end
62
- end # Core
63
- end # Controller
64
- end # Lockdown
@@ -1,63 +0,0 @@
1
- module Lockdown
2
- module Frameworks
3
- module Merb
4
- module Controller
5
-
6
- def available_actions(klass)
7
- klass.callable_actions.keys
8
- end
9
-
10
- def controller_name(klass)
11
- klass.controller_name
12
- end
13
-
14
- # Locking methods
15
- module Lock
16
- def self.included(base)
17
- base.class_eval do
18
- include Lockdown::Frameworks::Merb::Controller::Lock::InstanceMethods
19
- end
20
-
21
- base.before :set_current_user
22
- base.before :configure_lockdown
23
- base.before :check_request_authorization
24
- end
25
-
26
- module InstanceMethods
27
- def self.included(base)
28
- base.class_eval do
29
- include Lockdown::Controller::Core
30
- end
31
- end
32
-
33
- def sent_from_uri
34
- request.uri
35
- end
36
-
37
- def authorized?(path)
38
- return true if current_user_is_admin?
39
-
40
- path_allowed?(path)
41
- end
42
-
43
- # Can log Error => e if desired, I don't desire to now.
44
- # For now, just send home, but will probably make this configurable
45
- def access_denied(e)
46
- redirect Lockdown::System.fetch(:access_denied_path)
47
- end
48
-
49
- def path_from_hash(hsh)
50
- return hsh if hsh.is_a?(String)
51
- hsh = hsh.to_hash if hsh.is_a?(Mash)
52
- hsh['controller'].to_s + "/" + hsh['action'].to_s
53
- end
54
-
55
- def redirect_back_or_default(default)
56
- session[:prevpage] ? redirect(session[:prevpage]) : redirect(default)
57
- end
58
- end # InstanceMethods
59
- end # Lock
60
- end # Controller
61
- end # Merb
62
- end # Frameworks
63
- end # Lockdown
@@ -1,32 +0,0 @@
1
- module Lockdown
2
- module Frameworks
3
- module Merb
4
- module View
5
- def self.included(base)
6
- base.class_eval do
7
- alias_method :link_to_open, :link_to
8
- alias_method :link_to, :link_to_secured
9
- end
10
- end
11
-
12
- def link_to_secured(name, url = '', options = {})
13
- if authorized? url
14
- return link_to_open(name, url, options)
15
- end
16
- return ""
17
- end
18
-
19
- def link_to_or_show(name, url = '', options = {})
20
- lnk = link_to(name, url , options)
21
- lnk.length == 0 ? name : lnk
22
- end
23
-
24
- def links(*lis)
25
- rvalue = []
26
- lis.each{|link| rvalue << link if link.length > 0 }
27
- rvalue.join(" | ")
28
- end
29
- end # View
30
- end # Merb
31
- end # Frameworks
32
- end # Lockdown
@@ -1,84 +0,0 @@
1
- require File.join(File.dirname(__FILE__), "merb", "controller")
2
- require File.join(File.dirname(__FILE__), "merb", "view")
3
-
4
- module Lockdown
5
- module Frameworks
6
- module Merb
7
- class << self
8
- def use_me?
9
- Object.const_defined?("Merb") && ::Merb.const_defined?("AbstractController")
10
- end
11
-
12
- def included(mod)
13
- mod.extend Lockdown::Frameworks::Merb::Environment
14
- mixin
15
- end
16
-
17
- def mixin
18
- Lockdown.controller_parent.class_eval do
19
- include Lockdown::Frameworks::Merb::Controller::Lock
20
- end
21
- Lockdown.view_helper.class_eval do
22
- include Lockdown::Frameworks::Merb::View
23
- end
24
- Lockdown::System.class_eval do
25
- extend Lockdown::Frameworks::Merb::System
26
- end
27
- end
28
- end # class block
29
-
30
-
31
- module Environment
32
- def project_root
33
- ::Merb.root
34
- end
35
-
36
- def controller_parent
37
- ::Merb::Controller
38
- end
39
-
40
- def view_helper
41
- ::Merb::AssetsMixin
42
- end
43
-
44
- def controller_class_name(str)
45
- if str.include?("__")
46
- str.split("__").collect{|p| Lockdown.camelize(p)}.join("::")
47
- else
48
- Lockdown.camelize(str)
49
- end
50
- end
51
- end
52
-
53
- module System
54
- include Lockdown::Frameworks::Merb::Controller
55
-
56
- def skip_sync?
57
- Lockdown::System.fetch(:skip_db_sync_in).include?(Merb.environment)
58
- end
59
-
60
- def load_controller_classes
61
- @controller_classes = {}
62
-
63
- maybe_load_framework_controller_parent
64
-
65
- Dir.chdir("#{Lockdown.project_root}/app/controllers") do
66
- Dir["**/*.rb"].sort.each do |c|
67
- next if c == "application.rb"
68
- lockdown_load(c)
69
- end
70
- end
71
- end
72
-
73
- def maybe_load_framework_controller_parent
74
- load("application.rb") unless const_defined?("Application")
75
- end
76
-
77
- def lockdown_load(file)
78
- klass = Lockdown.class_name_from_file(file)
79
- @controller_classes[klass] = Lockdown.qualified_const_get(klass)
80
- end
81
- end # System
82
- end # Merb
83
- end # Frameworks
84
- end # Lockdown
@@ -1,70 +0,0 @@
1
- module Lockdown
2
- module Orms
3
- module DataMapper
4
- class << self
5
- def use_me?
6
- Object.const_defined?("DataMapper") && DataMapper.const_defined?("Base")
7
- end
8
-
9
- def included(mod)
10
- mod.extend Lockdown::Orms::Datamapper::Helper
11
- mixin
12
- end
13
-
14
- def mixin
15
- orm_parent.class_eval do
16
- include Lockdown::Orm::DataMapper::Stamps
17
- end
18
- end
19
- end # class block
20
-
21
- module Helper
22
- def orm_parent
23
- ::DataMapper::Base
24
- end
25
-
26
- #TODO: These may be called from DataMapper::Base or DataMapper, not sure
27
- #FIXME: If Datamapper is correct, need ::DataMapper
28
- def database_execute(query)
29
- DataMapper.database.execute(query)
30
- end
31
-
32
- def database_query(query)
33
- DataMapper.database.query(query)
34
- end
35
-
36
- def database_table_exists?(klass)
37
- DataMapper.database.table_exists?(klass)
38
- end
39
- end
40
-
41
- module Stamps
42
- def self.included(base)
43
- base.class_eval do
44
- alias_method :create_without_stamps, :create
45
- alias_method :create, :create_with_stamps
46
- alias_method :update_without_stamps, :update
47
- alias_method :update, :update_with_stamps
48
- end
49
- end
50
-
51
- def current_profile_id
52
- Thread.current[:profile_id]
53
- end
54
-
55
- def create_with_stamps
56
- profile_id = current_profile_id || Profile::SYSTEM
57
- self[:created_by] = profile_id if self.respond_to?(:created_by)
58
- self[:updated_by] = profile_id if self.respond_to?(:updated_by)
59
- create_without_stamps
60
- end
61
-
62
- def update_with_stamps
63
- profile_id = current_profile_id || Profile::SYSTEM
64
- self[:updated_by] = profile_id if self.respond_to?(:updated_by)
65
- update_without_stamps
66
- end
67
- end
68
- end
69
- end
70
- end
@@ -1,208 +0,0 @@
1
- module Lockdown
2
- module Rights
3
- attr_accessor :permissions #:nodoc:
4
- attr_accessor :user_groups #:nodoc:
5
-
6
- # :public_access allows access to all
7
- attr_reader :public_access #:nodoc:
8
- # :protected_access will restrict access to authenticated users.
9
- attr_reader :protected_access #:nodoc:
10
-
11
- # Future functionality:
12
- # :private_access will restrict access to model data to their creators.
13
- # attr_accessor :private_access
14
-
15
- # Sets permission with arrays of access_rights, e.g.:
16
- # ["controller_a/method_1", "controller_a/method_2", ...]
17
-
18
- def initialize_rights
19
- @permissions ||= {}
20
- @user_groups ||= {}
21
-
22
- @public_access ||= []
23
- @protected_access ||= []
24
- end
25
-
26
- def set_permission(name, *method_arrays)
27
- permissions[name] ||= []
28
- method_arrays.each{|ary| permissions[name] += ary}
29
- end
30
-
31
- # Permissions are stored as a hash with the value being the method_arrays
32
- def get_permissions
33
- permissions.keys
34
- end
35
-
36
- def permission_exists?(perm)
37
- get_permissions.include?(perm)
38
- end
39
-
40
- def access_rights_for_permission(perm)
41
- sym = Lockdown.get_symbol(perm)
42
-
43
- unless permission_exists?(sym)
44
- raise SecurityError, "Permission requested is not defined: #{sym}"
45
- end
46
- permissions[sym]
47
- end
48
-
49
- def set_user_group(name, *perms)
50
- user_groups[name] ||= []
51
- perms.each do |perm|
52
- unless permission_exists?(perm)
53
- raise SecurityError, "For UserGroup (#{name}), permission is invalid: #{perm}"
54
- end
55
- user_groups[name].push(perm)
56
- end
57
- end
58
-
59
- def get_user_groups
60
- user_groups.keys
61
- end
62
-
63
- def user_group_exists?(ug)
64
- get_user_groups.include?(ug)
65
- end
66
-
67
- # Determine if the user group is defined in init.rb
68
- def has_user_group?(ug)
69
- sym = Lockdown.get_symbol(ug)
70
-
71
- return true if sym == Lockdown.administrator_group_symbol
72
- user_group_exists?(sym)
73
- end
74
-
75
- def set_public_access(*perms)
76
- perms.each{|perm| @public_access += permissions[perm]}
77
- end
78
-
79
- def public_access?(perm)
80
- public_access.include?(perm)
81
- end
82
-
83
- def set_protected_access(*perms)
84
- perms.each{|perm| @protected_access += permissions[perm]}
85
- end
86
-
87
- def protected_access?(perm)
88
- protected_access.include?(perm)
89
- end
90
-
91
- def permission_assigned_automatically?(perm)
92
- public_access?(perm) || protected_access?(perm)
93
- end
94
-
95
- # Test user for administrator rights
96
- def administrator?(usr)
97
- user_has_user_group?(usr, Lockdown.administrator_group_symbol)
98
- end
99
-
100
- # Returns array of controller/action values administrators can access.
101
- def administrator_rights
102
- Lockdown::System.all_controllers_all_methods
103
- end
104
-
105
- def make_user_administrator(usr)
106
- unless Lockdown.database_table_exists?(UserGroup)
107
- create_administrator_user_group
108
- end
109
-
110
- usr.user_groups << UserGroup.find_or_create_by_name(Lockdown.administrator_group_string)
111
- end
112
-
113
- # Returns array of controller/action values all logged in users can access.
114
- def standard_authorized_user_rights
115
- Lockdown::System.public_access + Lockdown::System.protected_access
116
- end
117
-
118
- # Return array of controller/action values user can access.
119
- def access_rights_for_user(usr)
120
- return unless usr
121
- return :all if administrator?(usr)
122
-
123
- rights = standard_authorized_user_rights
124
-
125
- usr.user_groups.each do |grp|
126
- permissions_for_user_group(grp) do |perm|
127
- rights += access_rights_for_permission(perm)
128
- end
129
- end
130
- rights
131
- end
132
-
133
- # Use this for the management screen to restrict user group list to the
134
- # user. This will prevent a user from creating a user with more power than
135
- # him/her self.
136
- def user_groups_assignable_for_user(usr)
137
- return [] if usr.nil?
138
-
139
- if administrator?(usr)
140
- UserGroup.find_by_sql <<-SQL
141
- select user_groups.* from user_groups order by user_groups.name
142
- SQL
143
- else
144
- UserGroup.find_by_sql <<-SQL
145
- select user_groups.* from user_groups, user_groups_users
146
- where user_groups.id = user_groups_users.user_group_id
147
- and user_groups_users.user_id = #{usr.id}
148
- order by user_groups.name
149
- SQL
150
- end
151
- end
152
-
153
- # Similar to user_groups_assignable_for_user, this method should be
154
- # used to restrict users from creating a user group with more power than
155
- # they have been allowed.
156
- def permissions_assignable_for_user(usr)
157
- return [] if usr.nil?
158
- if administrator?(usr)
159
- get_permissions.collect{|k| Permission.find_by_name(Lockdown.get_string(k)) }.compact
160
- else
161
- user_groups_assignable_for_user(usr).collect{|g| g.permissions}.flatten.compact
162
- end
163
- end
164
-
165
- def permissions_for_user_group(ug)
166
- sym = Lockdown.get_symbol(ug)
167
- perm_array = []
168
-
169
- if has_user_group?(sym)
170
- permissions = user_groups[sym] || []
171
- else
172
- permissions = ug.permissions
173
- end
174
-
175
-
176
- permissions.each do |perm|
177
- perm_sym = Lockdown.get_symbol(perm)
178
-
179
- unless permission_exists?(perm_sym)
180
- raise SecurityError, "Permission associated to User Group is invalid: #{perm}"
181
- end
182
-
183
- if block_given?
184
- yield perm_sym
185
- else
186
- perm_array << perm_sym
187
- end
188
- end
189
-
190
- return perm_array unless block_given?
191
- end
192
-
193
-
194
- private
195
-
196
- def user_has_user_group?(usr, sym)
197
- usr.user_groups.each do |ug|
198
- return true if Lockdown.convert_reference_name(ug.name) == sym
199
- end
200
- false
201
- end
202
-
203
- def create_administrator_user_group
204
- UserGroup.create :name => Lockdown.administrator_group_string
205
- end
206
-
207
- end
208
- end