activeaclplus 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc ADDED
@@ -0,0 +1,241 @@
1
+ =Active Access Control Lists Plus (ActiveAclPlus)
2
+ The ActiveAclPlus plugin implements a flexible, fast and easy to use generic access control system.
3
+
4
+ ==License
5
+ ActiveAclPlus is released under the LGPL[http://www.opensource.org/licenses/lgpl-license.php] (Gnu Lesser General Public License) - see the included LICENSE file, too.
6
+
7
+ ==Features
8
+ * ease of use - uses polymorphic collections for associations.
9
+ * advanced design - uses SQL nested sets for inheritance, thus only needs a single DB query to decide on a permission request.
10
+ * scalable - there are no real benchmarks yet. But the system design is based on http://phpgacl.sourceforge.net, adding object orientation, polymorphism and two levels of caching. PhpGacl claims "A real-world working version with many added layers of complexity supports over 60,000 Accounts, 200 Groups and 300 ACO's." Tests on my dev notebook show 10 - 30 times performance improvements compared to active_rbac.
11
+ * caching - uses instance caching and optionally stores permission results in memcached using timeouts.
12
+ * flexible - grants simple (2D, like <code>current_user.has_permission?(User::LOGIN)</code>) and object level (3D, like <code>admin.has_permission?(Forum::ADMIN, :on => team_forum)</code>) permissions. Can assign and request permissions to and from every ActiveRecord model. No "hardcoded" permissions - all permissions can be assigned at runtime.
13
+ * grouping - permissions are inherited at target and requester side through groups. Every model implementing an SQL nested set tree may be used as a group.
14
+ * ControllerAction model loader: It maps controller actions to the DB so they can be used in permission assignment. The access object is available via calling <code>current_action</code> on the controller.
15
+ * exchangeable DB interface: ActiveRecord and direct MySQL adapter available
16
+ * supports namespaced models and single table inheritance (STI)
17
+ * 100 % C0 code coverage on unit tests
18
+
19
+ ==Limitations
20
+ * At present only one grouping type per model is supported. This could be changed on request but I don't see a use case for it yet.
21
+ * The DBMS has to support subselects. So PostgreSQL, Sqllite and MySQL 5 work, but MySQL 4 does not.
22
+
23
+
24
+ ==Prerequisites/Installation
25
+ ActiveAclPlus uses the has_many_polymorphs[http://www.agilewebdevelopment.com/plugins/restful_authentication] plugin. Make shure you've got a recent version, old versions have bugs affecting active_acl_plus.
26
+ !!Be sure you have the paches for 2.1, see: http://rubyforge.org/forum/forum.php?thread_id=26041&forum_id=16450
27
+
28
+ ./script/plugin install git://github.com/popel/active_acl_plus.git
29
+
30
+ generators XXX
31
+
32
+ ==Short summary
33
+ The ActiveAclPlus system consists of access objects, organized by access groups, that request privileges on each other. Allowing or denying access to a privilege is controlled by ACL (access control list entry) objects. Access objects and access groups can be instances of arbitrary ActiveRecord model classes enhanced by acts_as_access_object and acts_as_access_group. They are associated to ACL entries via polymorphic associations.
34
+
35
+ ===Access objects
36
+ These are basically requesters and targets in the permission system, as for example a User or a Forum model object. In this case a user could act as a requester ("do I have privilege Y?") or target ("does access object X have privilege Y on me?"). A Forum would most certainly be only used as a target, but all access objects can theoretically be used as both requesters and targets. Access objects use the acts_as_access_object macro inside their definition. This registers the model with the ACL system and enhances it with methods like has_privilege?.
37
+
38
+ Every model class must specify an association that is used as the "grouping type" to it. So User may declare has_and_belongs_to_many :user_groups and use acts_as_access_object :grouped_by => :user_groups. You can use a has_and_belongs_to_many or a belongs_to association for this. Common mapping attributes (:join_table, :foreign_key etc.) are supported.
39
+
40
+
41
+ ===Access groups
42
+ The access group model needs to implement a tree with nested set semantics having a "left" and "right" column, e.g. by using the built-in acts_as_nested_set or the much more recommended acts_as_betted_nested_set plugin. Groups are declared with acts_as_access_group.
43
+
44
+ Groups may be used to specify inheritance hierarchies for permissions. So you could have a 'registered users' group as a subgroup of the 'users' group and assign the privilege to log in to this group via an ACL entry. Every user belonging to this group will now be granted the privilege to log in. Then you could add a subgroup to registerd users, 'banned users', and deny the log in privilege for this group. Every user added to this group would now be unable to log in, regardless of beeing in 'registered users' or not, as 'banned users' would override the permission settings of 'registered users'.
45
+
46
+
47
+ ===Privileges
48
+ A privilege object is an object for the thing we wish to define a permission for. So User::LOGIN could be a privilege object for checking a users permission to log in, while Forum::ADMIN might define administration rights on a forum.
49
+
50
+ A privilege object itself is little more than it’s name and id and is usually bound to a constant inside the application, as it is not expected to change at runtime. Privileges are usually created by the developer in the source code and not in the admin frontend, as creating new privilege objects that have no meaning (by code that checks for them) would be pointless.
51
+
52
+
53
+ ===Access Control List (ACL) entries
54
+ ACL entries are the glue between all these objects, defining which requesters and requester groups have access to which privileges, optionally defining target objects and target groups as well. ACL entries are organized by ACL sections, for better overview in the admin screens.
55
+
56
+
57
+ ==Usage
58
+ ===In short
59
+ "No access defined" for a privilege evaluates to "deny". This may be overriden by an explicit "allow" or "deny". Privileges are inherited in requestor and target groups, this means you can override them in subgroups again. Privileges directly assigned to an object always supercede those assigned to groups.
60
+
61
+ ===Simple (2D) permissions
62
+ We want all registered users to be able to log in. We create the User model, the UserGroup model and the User::LOGIN privilege object as described above. Then we create a new ACL entry, set 'allow' to true, add the "registered users" group as requester group, User::LOGIN as privilege and we are done. Every user assigned to "registered users" or a subgroup of it will now be granted access by calling <code>my_user.has_privilege?(User::LOGIN)</code>.
63
+
64
+ ====Simple permissions example
65
+
66
+ class UserGroup < ActiveRecord::Base
67
+ acts_as_nested_set
68
+ acts_as_access_group
69
+ has_and_belongs_to_many :users
70
+ end
71
+
72
+ class User < ActiveRecord::Base
73
+ has_and_belongs_to_many :user_groups
74
+ acts_as_access_object :grouped_by => :user_groups
75
+ privilege_const_set('LOGIN')
76
+ end
77
+
78
+ # assume 'registered_users' exists and users 'john' and 'dr_evil' are members of it but 'anonymous' is not.
79
+ registered_users = UserGroup.find_by_name('registered_users')
80
+
81
+ acl = ActiveAcl::Acl.create :section => ActiveAcl::AclSection.create(:description => 'users')
82
+
83
+ acl.allow = true # true is default
84
+ acl.privileges << User::LOGIN
85
+ acl.note="login"
86
+ acl.save
87
+
88
+ acl.requester_groups << registered_users
89
+
90
+ john.has_privilege?(User::LOGIN) #=> true
91
+ dr_evil.has_privilege?(User::LOGIN) #=> true
92
+
93
+ anonymous.has_privilege?(User::LOGIN) #=> false
94
+
95
+ ===Overriding permissions
96
+ We want to ban specific users from our site. We create another ACL entry, assign the User::LOGIN privilege object, set 'allow' to false and then assign these users as requesters to the ACL entry. The direct permission assignment on the objects overrides the 'allow login' ACL entry from above.
97
+
98
+ ====Overriding permissions example
99
+
100
+ ban_users = ActiveAcl::Acl.create :section => ActiveAcl::AclSection.find_by_description('users')
101
+
102
+ ban_users.allow = false
103
+ ban_users.privileges << User::LOGIN
104
+ ban_users.requesters << dr_evil
105
+
106
+ ban_users.save
107
+
108
+ john.has_privilege?(User::LOGIN) #=> true
109
+ dr_evil.has_privilege?(User::LOGIN) #=> false
110
+
111
+ ===Object level (3D) permissions
112
+ We want to assign forum permissions. We have several privileges (Forum::ADMIN, Forum::READ, Forum::POST etc.), the afore mentioned User and UserGroup models as well as a Forum and a Category model for grouping the forums.
113
+
114
+ If we want to check if a certain user may read in a certain forum, it is not sufficient to check <code>test_user.has_privilege?(Forum::READ)</code> as the target object - in this case a forum - is needed to make a decision. The code to do the check is like <code>test_user.has_privilege?(Forum::READ, :on => teamforum)</code>.
115
+
116
+ To make this work you create a new ACL entry, add Forum::POST and Forum::READ as privileges, set 'allow' to true, add the registered users group as a requester group and the public forums category as a target group to the acl. Now every user belonging to the registered users group or a subgroup of it gains post and read privileges on all forums of the public forums category or a subcategory of it.
117
+
118
+ ====Object level permissions example
119
+
120
+ # Assuming setup as in the above examples
121
+
122
+ class Category < ActiveRecord::Base
123
+ acts_as_nested_set
124
+ acts_as_access_group
125
+ has_many :forums
126
+ end
127
+
128
+ class Forum < ActiveRecord::Base
129
+ belongs_to :category
130
+ acts_as_access_object :grouped_by => :category
131
+ privilege_const_set 'READ' => 'read postings in forum',
132
+ 'POST' => 'reply to threads in a forum'
133
+ end
134
+
135
+ # assume there is a forum 'speakers corner' assigned to the category 'public'.
136
+
137
+ acl = ActiveAcl::Acl.create :section => ActiveAcl::AclSection.create(:description => 'forum')
138
+
139
+ acl.allow = true
140
+ acl.requester_groups << registered_users
141
+ acl.target_groups << Category.find_by_name('public')
142
+
143
+ acl.privileges << Forum::READ
144
+ acl.privileges << Forum::POST
145
+
146
+ acl.save
147
+
148
+ speakers = Forum.find_by_name('speakers corner')
149
+
150
+ john.has_privilege?(Forum::READ, :on => speakers) #=> true
151
+ john.has_privilege?(Forum::POST, :on => speakers) #=> true
152
+ anonymous.has_privilege?(Forum::READ, :on => speakers) #=> false
153
+
154
+ ==CAUTION
155
+ Do not create ACL entries that are on different branches of the inheritance hierarchy and have allow/deny set differently on the same privilege objects. This way it's impossible to tell which permission should take precedence. At present this is by creation date, later entries superceding older ones, but this is most certainly not what you want.
156
+
157
+ Error checking for conflicting ACL entries is high up on the ToDo list.
158
+
159
+ ==Controller Actions
160
+ Defining permissions on controller actions (like "may user x execute AdminController.list ?") is quite a common case but we are facing a problem here: Controller actions have no corresponding DB models so permissions on them can't be easily defined.
161
+
162
+ ActiveAclPlus solves this by adding a ControllerAction and ControllerGroup model. For every public controller method (=action) there is one ControllerAction object in the DB.
163
+
164
+ On application startup, the plugin loader checks all controller files in app/controllers and loads or creates a ControllerAction object for every action it finds. These objects get cached in a hash in the ActiveAclPlus module. Every controller now has a method <code>current_action</code> that looks up and returns the access object for the current action, so it can be used for access checks like <code>current_user.has_privilege?(ActiveAcl::ControllerAction::EXECUTE, :on => current_action)</code>.
165
+
166
+ This works nicely for before_filters with authorization checks.
167
+
168
+ A word on the load mechanism: If the action has no corresponding DB entry (it's looked up on method creation by controller and action name) the loader searches for a controller group with the same name as the controller. If it is found, the action is created and assigned to this group. Else the controller group is created as a subgroup to the "unassigned controller actions" group (this name can be changed in the options) and the unassigned action is added to the controller group.
169
+
170
+ So you are free on how to organize your controllers. Maybe create an admin group and a public group and move controllers as a subgroup inside them?
171
+
172
+ ==Caching
173
+ The plugin provides two levels of caching. The instance cache is a hash inside the access object. The object first tries to serve a permission request from the instance cache. If it is not found and a simple permission is requested, the query fetches all simple permissions of the object and puts them in the instance cache. The reason for this is that there is no noticable speed penalty in fetching all 2D permissions at once compared to fetching only one, so this will save time and DB IO later on. Complex 3D queries are fetched independently and also saved to the instance cache. The instance cache lives inside the access object, so it has it's lifetime, too - which in rails usually is no more than a single request.
174
+
175
+ The second level cache tries to overcome this limitation by putting the instance cache of an access object in an external cache. It tries to get the instance cache from there if it is not set, and sets it if it was changed. The only real implementation for now is with the memcache daemon. The second level cache uses a timeout (which can be defined in the options) to expire the cached permissions.
176
+
177
+ Instance and second level cache can be expired explicitly by calling <code>clear_cached_permissions</code> on the access object. Calling <code>reload</code> on the object also purges the caches.
178
+
179
+ See ActiveAcl::Cache::MemcacheAdapter on how to set it up.
180
+
181
+
182
+ ==Preloader
183
+ The plugin includes a <code>load_files_from filenames</code> function. It can be used to preload source files (and therefore the classes in it) from an application path and should be used from environment.rb.
184
+
185
+ load_files_from("#{RAILS_ROOT}/app/controllers/**/[^.]*.rb")
186
+ load_files_from("#{RAILS_ROOT}/app/models/**/[^.]*.rb")
187
+
188
+ will load all models and controllers inside these folders and their subfolders. This way you can be shure they are registered with the ACL system at rails boot time - else they will be registered when they are called for the first time. This means that new controllers will not show up in the admin screens until they were accessed if not using the preloader.
189
+
190
+ ==Options
191
+ <code>ActiveAcl::OPTIONS</code> is an array that can be used to override various options for the system by setting the values in environment.rb. <code>ActiveAcl::DEFAULT_OPTIONS</code> is as follows:
192
+
193
+ DEFAULT_OPTIONS = {
194
+ :acl_sections_table => 'acl_sections',
195
+ :acls_privileges_table => 'acls_privileges',
196
+ :acls_table => 'acls',
197
+ :privileges_table => 'privileges',
198
+ :requester_links_table => 'requester_links',
199
+ :target_links_table => 'target_links',
200
+ :requester_group_links_table => 'requester_group_links',
201
+ :target_group_links_table => 'target_group_links',
202
+ :controller_actions_table => 'controller_actions',
203
+ :controller_groups_table => 'controller_groups',
204
+
205
+ :controllers_group_name => 'unassigned_controller_actions', # the name of the base group that newly created controller groups get assigned to
206
+ :controller_group_name_suffix => '_controller', # name suffix for generated controller groups
207
+
208
+ :cache_permission_timeout => 10, # timeout in seconds for the second level cache
209
+
210
+ :db => ActiveAcl::DB::ActiveRecordAdapter, # the DB Adapter to use
211
+ :cache => ActiveAcl::Cache::NoCacheAdapter, # the Cache Adapter to use
212
+ }
213
+
214
+ ==Tests
215
+ See: http://github.com/popel/active_acl_plus_test/tree/master for more info.
216
+
217
+ ==Credits
218
+ * Gregor Melhorn implemented this and maintained it up to Version 0.2.1. Thanks for releasing this!
219
+ * Evan for writing that great polymorph plugin and beeing so kind to add namespace and tablename support on my request.
220
+ * ReinH and markmeves for great support and suggestions at the rubyonrails channel on freenode.org.
221
+ * http://phpgacl.sourceforge.net as a great source of inspiration
222
+ * Obrie for writing plugin_migrations and loaded_plugins and also very nice support when I got stuck with using them.
223
+
224
+ ==ToDo/Ideas
225
+
226
+ in no particular order, just a reminder...
227
+
228
+ * direct PostgreSQL interface
229
+ * example on how to integrate with acts_as_authenticated
230
+ * example on controller actions
231
+ * make grouping optional
232
+ * add interface generators (started)
233
+ * error checking for conflicting ACL entries
234
+ * permissions should have more attributes (like :until,:left_boni,...)
235
+ * get all permissions for a requester within a section
236
+ * get all permissions of a requester (with one query)
237
+ * get all permissions of a requester on a target (with one query)
238
+ * get all requester with a given privilege on a target (one query)
239
+ * get all targets on which a requester has a certain privilege (one query)
240
+
241
+
data/Rakefile ADDED
@@ -0,0 +1,84 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+ require 'rake/gempackagetask'
5
+ require 'rake/contrib/sshpublisher'
6
+
7
+ # RCOV command, run as though from the commandline.
8
+ RCOV = "rcov"
9
+
10
+ RUBY_FORGE_PROJECT = "activeaclplus"
11
+ RUBY_FORGE_USER = "popel"
12
+
13
+ eval(File.read("./active_acl_plus.gemspec"))
14
+
15
+ Rake::GemPackageTask.new(PKG_GEM) do |p|
16
+ p.gem_spec = PKG_GEM
17
+ p.need_tar = true
18
+ p.need_zip = true
19
+ end
20
+
21
+ desc 'Default: run unit tests.'
22
+ task :default => :test
23
+
24
+ #desc "Publish the beta gem"
25
+ #task :pgem => [:package] do
26
+ # Rake::SshFilePublisher.new("pluginaweek@pluginaweek.org", "/home/pluginaweek/gems.pluginaweek.org/gems", "pkg", "#{PKG_FILE_NAME}.gem").upload
27
+ #end
28
+
29
+ desc "Publish the API documentation"
30
+ task :pdoc => [:rdoc] do
31
+ Rake::SshDirPublisher.new("popel@rubyforge.org", "/var/www/gforge-projects/activeaclplus/api", "rdoc").upload
32
+ #Rake::RubyForgePublisher.new(RUBY_FORGE_PROJECT, RUBY_FORGE_USER).upload
33
+ end
34
+
35
+ desc "Publish the API docs and gem"
36
+ task :publish => [:pdoc, :release]
37
+
38
+ desc "Publish the release files to RubyForge."
39
+ task :release => [:gem, :package] do
40
+ require 'rubyforge'
41
+ options={}
42
+ #options["cookie_jar"] = RubyForge::COOKIE_F
43
+ #options["password"] = ENV["RUBY_FORGE_PASSWORD"] if ENV["RUBY_FORGE_PASSWORD"]
44
+
45
+ ruby_forge = RubyForge.new
46
+ ruby_forge.configure
47
+ ruby_forge.login
48
+ %w( gem tgz zip ).each do |ext|
49
+ file = "pkg/#{PKG_FILE_NAME}.#{ext}"
50
+ puts "Releasing #{File.basename(file)}..."
51
+ ruby_forge.add_release(RUBY_FORGE_PROJECT, PKG_NAME, PKG_VERSION, file)
52
+ end
53
+ end
54
+
55
+ desc "generate a coverage report"
56
+ task :coverage do
57
+ sh "#{RCOV} --rails -T -Ilib -x db/**/* --output ../../../coverage/active_acl test/all_tests.rb"
58
+ end
59
+
60
+ desc "generate a coverage report saving current state"
61
+ task :coverage_save do
62
+ sh "#{RCOV} --rails -T -Ilib -x db/**/* --output ../../../coverage/active_acl --save ../../../coverage/active_acl/coverage.info test/all_tests.rb"
63
+ end
64
+
65
+ desc "generate a diff coverage report on previously saved state"
66
+ task :coverage_diff do
67
+ sh "#{RCOV} --rails -T -Ilib -x db/**/* --text-coverage-diff ../../../coverage/active_acl/coverage.info --output ../../../coverage/active_acl test/all_tests.rb"
68
+ end
69
+
70
+ desc 'Test the active_acl_plus plugin.'
71
+ Rake::TestTask.new(:test) do |t|
72
+ t.libs << 'lib'
73
+ t.pattern = 'test/unit/**/*_test.rb'
74
+ t.verbose = true
75
+ end
76
+
77
+ desc 'Generate documentation for the active_acl_plus plugin.'
78
+ Rake::RDocTask.new(:rdoc) do |rdoc|
79
+ rdoc.rdoc_dir = 'rdoc'
80
+ rdoc.title = 'ActiveAclPlus'
81
+ rdoc.options << '--line-numbers' << '--inline-source'
82
+ rdoc.rdoc_files.include('README')
83
+ rdoc.rdoc_files.include('lib/**/*.rb')
84
+ end
@@ -0,0 +1,111 @@
1
+ class BaseTableSetup < ActiveRecord::Migration
2
+ def self.up
3
+ create_table ActiveAcl::OPTIONS[:acls_table] do |t|
4
+ t.column :section_id, :int
5
+ t.column :allow, :boolean, :null => false, :default => true
6
+ t.column :enabled, :boolean, :null => false, :default => true
7
+ t.column :note, :string, :null => true
8
+ t.column :updated_at, :datetime, :null => false
9
+ end
10
+
11
+ add_index ActiveAcl::OPTIONS[:acls_table], :enabled
12
+ add_index ActiveAcl::OPTIONS[:acls_table], :section_id
13
+ add_index ActiveAcl::OPTIONS[:acls_table], :updated_at
14
+ add_index ActiveAcl::OPTIONS[:acls_table], :note, :unique
15
+
16
+ create_table ActiveAcl::OPTIONS[:acl_sections_table] do |t|
17
+ t.column :description, :string, :limit => 230, :null => false
18
+ end
19
+
20
+ add_index ActiveAcl::OPTIONS[:acl_sections_table], :description, :unique
21
+
22
+ create_table ActiveAcl::OPTIONS[:privileges_table] do |t|
23
+ t.column :section, :string, :limit => 230, :null => false
24
+ t.column :value, :string, :limit => 230, :null => false
25
+ t.column :description, :string, :limit => 230, :null => true
26
+ end
27
+
28
+ add_index ActiveAcl::OPTIONS[:privileges_table], [:section, :value], :unique
29
+
30
+ create_table ActiveAcl::OPTIONS[:acls_privileges_table], :id => false do |t|
31
+ t.column :acl_id, :int, :null => false
32
+ t.column :privilege_id, :int, :null => false
33
+ end
34
+
35
+ add_index ActiveAcl::OPTIONS[:acls_privileges_table], [:acl_id, :privilege_id], :unique
36
+
37
+ create_table ActiveAcl::OPTIONS[:requester_links_table] do |t|
38
+ t.column :acl_id, :int, :null => false
39
+ t.column :requester_id, :int, :null => false
40
+ t.column :requester_type, :string, :null => false
41
+ end
42
+
43
+ add_index ActiveAcl::OPTIONS[:requester_links_table], [:acl_id, :requester_id, :requester_type], :unique => true, :name => 'requester_links_join_index_1'
44
+ add_index ActiveAcl::OPTIONS[:requester_links_table], [:requester_type, :requester_id], :name => 'requester_links_join_index_2'
45
+ add_index ActiveAcl::OPTIONS[:requester_links_table], [:requester_id]
46
+
47
+ create_table ActiveAcl::OPTIONS[:requester_group_links_table] do |t|
48
+ t.column :acl_id, :int, :null => false
49
+ t.column :requester_group_id, :int, :null => false
50
+ t.column :requester_group_type, :string, :null => false
51
+ end
52
+
53
+ add_index ActiveAcl::OPTIONS[:requester_group_links_table], [:acl_id, :requester_group_id, :requester_group_type], :unique => true, :name => 'requester_group_links_join_index_1'
54
+ add_index ActiveAcl::OPTIONS[:requester_group_links_table], [:requester_group_type, :requester_group_id], :name => 'requester_group_links_join_index2'
55
+
56
+ create_table ActiveAcl::OPTIONS[:target_group_links_table] do |t|
57
+ t.column :acl_id, :int, :null => false
58
+ t.column :target_group_id, :int, :null => false
59
+ t.column :target_group_type, :string, :null => false
60
+ end
61
+
62
+ add_index ActiveAcl::OPTIONS[:target_group_links_table], [:acl_id, :target_group_id, :target_group_type], :unique => true, :name => 'target_group_links_join_index_1'
63
+ add_index ActiveAcl::OPTIONS[:target_group_links_table], [:target_group_type, :target_group_id], :name => 'target_group_links_join_index_2'
64
+
65
+ create_table ActiveAcl::OPTIONS[:target_links_table] do |t|
66
+ t.column :acl_id, :int, :null => false
67
+ t.column :target_id, :int, :null => false
68
+ t.column :target_type, :string, :null => false
69
+ end
70
+
71
+ add_index ActiveAcl::OPTIONS[:target_links_table], [:acl_id, :target_id, :target_type], :unique => true, :name => 'target_links_join_index_1'
72
+ add_index ActiveAcl::OPTIONS[:target_links_table], [:target_type, :target_id], :name => 'target_links_join_index_2'
73
+ add_index ActiveAcl::OPTIONS[:target_links_table], [:target_id]
74
+
75
+ create_table ActiveAcl::OPTIONS[:controller_actions_table] do |t|
76
+ t.column :controller, :string, :null => false
77
+ t.column :action, :string, :null => false
78
+ t.column :controller_group_id, :integer, :null => false
79
+ end
80
+
81
+ add_index ActiveAcl::OPTIONS[:controller_actions_table], [:controller, :action], :unique
82
+
83
+ create_table ActiveAcl::OPTIONS[:controller_groups_table] do |t|
84
+ t.column :description, :string, :null => false
85
+ t.column :lft, :integer
86
+ t.column :rgt, :integer
87
+ t.column :parent_id, :integer
88
+ end
89
+
90
+ add_index ActiveAcl::OPTIONS[:controller_groups_table], :description
91
+ add_index ActiveAcl::OPTIONS[:controller_groups_table], :lft
92
+ add_index ActiveAcl::OPTIONS[:controller_groups_table], :rgt
93
+ add_index ActiveAcl::OPTIONS[:controller_groups_table], :parent_id
94
+
95
+ # create root node
96
+ execute("INSERT INTO #{ActiveAcl::OPTIONS[:controller_groups_table]}(description, lft, rgt) VALUES ('controllers', 1, 2)")
97
+ end
98
+
99
+ def self.down
100
+ drop_table ActiveAcl::OPTIONS[:acls_table]
101
+ drop_table ActiveAcl::OPTIONS[:acl_sections_table]
102
+ drop_table ActiveAcl::OPTIONS[:privileges_table]
103
+ drop_table ActiveAcl::OPTIONS[:acls_privileges_table]
104
+ drop_table ActiveAcl::OPTIONS[:requester_links_table]
105
+ drop_table ActiveAcl::OPTIONS[:target_links_table]
106
+ drop_table ActiveAcl::OPTIONS[:requester_group_links_table]
107
+ drop_table ActiveAcl::OPTIONS[:target_group_links_table]
108
+ drop_table ActiveAcl::OPTIONS[:controller_actions_table]
109
+ drop_table ActiveAcl::OPTIONS[:controller_groups_table]
110
+ end
111
+ end
@@ -0,0 +1,29 @@
1
+ class ActiveAclGenerator < Rails::Generator::Base
2
+ attr_accessor :privileges_class_name, :privileges_file_name, :privileges_view_dir
3
+
4
+ def initialize(*runtime_args)
5
+ super(*runtime_args)
6
+ @privileges_class_name = (args[0] || 'PrivilegesController')
7
+ @privileges_file_name = @privileges_class_name.underscore
8
+ @privileges_view_dir = File.join('app', 'views', @privileges_file_name.gsub('_controller', ''))
9
+ end
10
+
11
+ def manifest
12
+ record do |m|
13
+ # Stylesheet, controllers and public directories.
14
+ m.directory File.join('public', 'stylesheets')
15
+ m.directory File.join('app', 'controllers')
16
+ m.directory File.join('app', 'views')
17
+ m.directory privileges_view_dir
18
+
19
+ m.template 'controllers/privileges_controller.rb', File.join('app', 'controllers', "#{privileges_file_name}.rb")
20
+ m.file 'views/privileges/_privilege_form.rhtml', File.join(privileges_view_dir, '_privilege_form.rhtml')
21
+ m.file 'views/privileges/edit.rhtml', File.join(privileges_view_dir, 'edit.rhtml')
22
+ m.file 'views/privileges/list.rhtml', File.join(privileges_view_dir, 'list.rhtml')
23
+ m.migration_template('../../../db/migrate/001_base_table_setup.rb',
24
+ 'db/migrate',
25
+ :assigns => {:migration_name => "BaseTableSetup"},
26
+ :migration_file_name => "base_table_setup")
27
+ end
28
+ end
29
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'active_acl'
data/install.rb ADDED
@@ -0,0 +1 @@
1
+ # Install hook code here
@@ -0,0 +1,37 @@
1
+ # This model is the "glue" :-). Every permission assignment uses an Acl
2
+ # model object, assigns objects, groups and privileges and setting
3
+ # 'allow' to "true" or "false" to grant or deny access.
4
+ module ActiveAcl
5
+ class Acl < ActiveRecord::Base
6
+ set_table_name ActiveAcl::OPTIONS[:acls_table]
7
+
8
+ belongs_to :section, :class_name => 'ActiveAcl::AclSection', :foreign_key => 'section_id'
9
+ has_and_belongs_to_many :privileges, :uniq => true, :join_table => ActiveAcl::OPTIONS[:acls_privileges_table], :class_name => 'ActiveAcl::Privilege'
10
+
11
+ has_many :target_links, :dependent => :delete_all, :class_name => 'ActiveAcl::TargetLink'
12
+ has_many :requester_links, :dependent => :delete_all,:class_name => 'ActiveAcl::RequesterLink'
13
+
14
+ has_many :requester_group_links, :dependent => :delete_all,:class_name => 'ActiveAcl::RequesterGroupLink'
15
+ has_many :target_group_links, :dependent => :delete_all,:class_name => 'ActiveAcl::TargetGroupLink'
16
+
17
+ validates_uniqueness_of :note
18
+ validates_presence_of :note
19
+
20
+ def self.reloadable? #:nodoc:
21
+ return false
22
+ end
23
+
24
+ # used as instance description in admin screen
25
+ def active_acl_description
26
+ if note
27
+ if section
28
+ '/' + section.description + '/' + note
29
+ else
30
+ return note
31
+ end
32
+ else
33
+ return nil
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,22 @@
1
+ # Groups Acl model objects into different sections to provide better
2
+ # overview in the admin screens. Has no meaning in permission resolution.
3
+ module ActiveAcl
4
+ class AclSection < ActiveRecord::Base
5
+ set_table_name ActiveAcl::OPTIONS[:acl_sections_table]
6
+
7
+ has_many :members, :class_name => 'ActiveAcl::Acl', :foreign_key => 'section_id'
8
+
9
+ validates_presence_of :description
10
+ validates_uniqueness_of :description
11
+
12
+ # Make shure there are no associated acls before destroying a section
13
+ def before_destroy
14
+ if members.empty?
15
+ true
16
+ else
17
+ errors.add_to_base("Can't delete a section with associated ACLs")
18
+ false
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,62 @@
1
+ require 'active_record'
2
+
3
+ module ActiveAcl #:nodoc:
4
+ module Acts #:nodoc:
5
+ module AccessGroup #:nodoc:
6
+
7
+ def self.included(base)
8
+ base.extend(ClassMethods)
9
+ end
10
+
11
+ module ClassMethods
12
+ # Extend self with access group capabilites. See README for details
13
+ # on usage. Accepts the following options as a hash:
14
+ # left_column:: name of the left column for nested set functionality, default :lft
15
+ # right_column:: name of the right column for nested set functionality, default :rgt
16
+ # Don't use 'left' and 'right' as column names - these are reserved words in most DBMS.
17
+ def acts_as_access_group(options = {})
18
+ configuration = {:left_column => :lft, :right_column => :rgt,
19
+ :controller => ActiveAcl::OPTIONS[:default_group_selector_controller],
20
+ :action => ActiveAcl::OPTIONS[:default_group_selector_action]}
21
+ configuration.update(options) if options.is_a?(Hash)
22
+ ActiveAcl::GROUP_CLASSES[self.name] = configuration
23
+
24
+ from_classes = ActiveAcl::GROUP_CLASSES.keys.collect do |x|
25
+ x.split('::').join('/').underscore.pluralize.to_sym
26
+ end
27
+
28
+ ActiveAcl::Acl.instance_eval do
29
+ has_many_polymorphs :requester_groups, {:from => from_classes,
30
+ :through => :"active_acl/requester_group_links",
31
+ :rename_individual_collections => true}
32
+
33
+ has_many_polymorphs :target_groups, {:from => from_classes,
34
+ :through => :"active_acl/target_group_links",
35
+ :rename_individual_collections => true}
36
+ end
37
+
38
+ include InstanceMethods
39
+ extend SingletonMethods
40
+
41
+ end
42
+ end
43
+
44
+ module SingletonMethods
45
+ # class description in engine interface
46
+ def active_acl_description
47
+ name
48
+ end
49
+ end
50
+
51
+ module InstanceMethods
52
+ # override this to customize the description in the interface
53
+ def active_acl_description
54
+ to_s
55
+ end
56
+ end
57
+
58
+ end
59
+ end
60
+ end
61
+
62
+ ActiveRecord::Base.send(:include, ActiveAcl::Acts::AccessGroup)