Simple Access Control: First Release
Posted by Mathew Abonyi Fri, 28 Jul 2006 17:29:00 GMT
In keeping with my promise from the previous post on acl_system2 and the new access control system I created, I’ve released it as a plugin:
script/plugin install http://mabs29.googlecode.com/svn/trunk/plugins/simple_access_controlIt’s usage is very intuitive:
class ApplicationController
access_rule 'admin', :only => :create
access_rule 'moderator || admin', :only => :newTreat it just like a before filter. Your rules are composed of titles, which require you to have a Role model with a has_and_belongs_to_many relationship with your user model (you may name your user model anything you like, but it is vital that you have an accessor called current_user). You also get a few freebies, like restrict_to(rule, &block), has_permission?(rule, user = current_user), and two backward compatible commands for acl_system2 users: access_control and permit?.
If you need more pointers, all of this is explained in the README.

I like it very much but I still have a problem using it: this rule works: access_rule ‘admin’, :only => :adminzone But now I want that the admin can acces the userzone, too. But the role doesn’t work: access_rule ‘user || admin’, :only => :userzone I hope you can help me ;-)
Markus, thanks for pointing that out. I must’ve forgotten to test my regexp. If you update your version of Simple Access Control, everything should work fine.
Thanks Mathew for fixing it. Now it works just perfect :-)
hi! great plugin fix! i’m planning to extend it to allow access control inheritance for parent roles. but i can’t seem to get a hold of it. opensvn.csie.org has been down for quite a while now.
Thanks for the heads up. I’ll be moving the plugins to code.google.com soon enough.
Unfortunatly I have another problem since moving to the fixed version. Now the protected action permission_denied doesn’t work anymore. Where can the problem be? I hope you can help me again because I’m new to rails and especially to debugging it on my own.
That is beginning to sound like it is out of my hands, unfortunately. Have you tried asking on #rubyonrails @ irc.freenode.net? If you are on Mac OS X, pick up Colloquy and check it out (I’m on there as mabs29).
To offer some quick advice, the protected permission_denied method is only called if your controller responds to it. If you think it is a public/protected/private problem, fiddle with the declarations in both your controller and vendor/plugins/simple_access_control/lib/simple_access_control.rb.
Good luck!
Hi, Mathew
great work on the plugin.
I have installed and updated teh database as required, and have linked a user to a role of admin.
however when I put the rule [ access_rule ‘admin’, :only => :index ] in my account contoller, the page jsut returns a blank page.
Is this a bug or have I missed a stage, should I have used script/generate to create any extra views / controllers?
thanks Scott
If you aren’t the admin, it will have returned false to the before_filter, halting the execution of your controller. You need to define a permission_denied protected method in your ApplicationController if you don’t want a blank.
In the coming revision, I will have a default for permission_denied and granted:Hope that helps.
hi,
thanks for the help, but I have set it up so it should allow me to access the page.
mu users table has a user with id => 1 and login => scott
roles table has id => 1 title => admin
roles_users role_id => 1 user_id => 1
my account class has the following
access_rule ‘admin’, :only => :index
Do you have ‘has_and_belongs_to_many :roles’ in your User model?
Also, are you migrating from acl_system2 or approaching this plugin afresh?
Note: I didn’t want to step on Ezra’s toes, since he provided a very nice plugin that I used for a while, but it sounds like I should make the install script create a working setup and repeat any necessary documentation from his plugin in my README. If there are steps you followed that I didn’t list or aren’t in Ezra’s plugin, please let me know so I can include them in the next revision.
Hi,
No, I didn’t have the relationship set up in the user model. All working now.
I haven’t used the acl_system before so came straight too simple_access_control.
I believe adding the above to your documentation could be useful for other users.
Thanks for your help, Scott
Something’s weird here. I’ve defined permission_denied on my ApplicationController to redirect to the home page, but when I try to browse a page under access control, I only see a blank page. It does work when access is permitted, but not when denied. Any help on this? Thanks.
I stil have the same problem as Evan. If somebody has fixed it, please comment how you’ve done it.
Only got around to it now. The reason it never surfaced in my application was, quite simply, I always required a person to be logged in first. I made an update which calls permission_denied if you are not logged in—the reason for this is that current_user is required for checking permissions.
If you have a guest account, I suggest all newcomers are automatically given the user ‘anonymous’ or ‘guest’. You’ll have to give all real users the ‘user’ role and then you can filter out anonymous/user/admin appropriately.
Hope that all solves your problems, Markus and Evan.
[...] simple_access_control – A nice user authorization extension for the acts_as_authenticated plugin. Simple, but efficient and elegant, just the way I like things. (That’s why I love Rails.) [...]
Just a note, if you wish to have true join models:
User: has_many :rights has_many :roles, :through => :rights
This works just as well. And rights is just a join table with a user_id and role_id. Can name rights whatever you wish as well.
Good one, Dustin. I forgot to note that adding rights is pretty straightforward for SimpleAccessControl, though I personally haven’t needed to add that functionality yet.
can’t get it running.
BTW, in the README stated: “SimpleAccessControl is a streamlined”, but it doesn’t look to be equipped with two most important views/controllers for this. Indeed, there is no page to manipulate roles, no page to manipulate users.
this plugin is not for newbies, but the bigger guys seem to able to write ACL stuff like this on their own, so what’s the point? ;)
Valery: It’s so “bigger guys” don’t have to repeat each other (DRYAO). Besides, assigning roles to users is something that’s usually very tied to the nature of your application, so default controllers wouldn’t make much sense.
This plugin is the most straight forward and effeicient (low weight) access control every made, I love it.
When I try to use this plugin, I get a:
uninitialized constant User::Role
error. There’s no Role model of course, so where is that supposed to come from?
This plugin needs a little setup how-to…not enough documentation.
I have been using your simple_access_control plugin successfully until I checked my code out on a different machine (same ruby version and rails is frozen to 1.2.3) now I get errors. I wonder if you could point me in the right direction as to what is wrong?
If you have time I would be very thankful.
CODE: <% restrict_to ‘admin || manager || report_viewer’ do %> <% end %>
ERROR: ./script/../config/../vendor/plugins/simple_access_control/lib/simple_access_control.rb:123:in `check’: You have a nil object when you didn’t expect it! The error occurred while evaluating nil.downcase
STACK: vendor/plugins/simple_access_control/lib/simple_access_control.rb:107:in `process’ vendor/rails/activerecord/lib/active_record/associations/association_proxy.rb:123:in `map’ vendor/rails/activerecord/lib/active_record/associations/association_proxy.rb:123:in `send’ vendor/rails/activerecord/lib/active_record/associations/association_proxy.rb:123:in `method_missing’ vendor/rails/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb:91:in `method_missing’ vendor/plugins/simple_access_control/lib/simple_access_control.rb:123:in `check’ (eval):1:in `process’ (eval):2:in `send’ (eval):2:in `restrict_to’ #{RAILS_ROOT}/app/views/account/index.rhtml:19:in `_run_rhtml_47app47views47account47index46rhtml’ -e:4:in `load’ -e:4
——
CODE: access_rule ‘data_entry || manager || admin’
ERROR: vendor/plugins/simple_access_control/lib/simple_access_control.rb:123:in `check’: You have a nil object when you didn’t expect it! The error occurred while evaluating nil.downcase
STACK: vendor/plugins/simple_access_control/lib/simple_access_control.rb:107:in `process’ vendor/rails/activerecord/lib/active_record/associations/association_proxy.rb:123:in `map’ vendor/rails/activerecord/lib/active_record/associations/association_proxy.rb:123:in `send’ vendor/rails/activerecord/lib/active_record/associations/association_proxy.rb:123:in `method_missing’ vendor/rails/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb:91:in `method_missing’ vendor/plugins/simple_access_control/lib/simple_access_control.rb:123:in `check’ (eval):1:in `process’ -e:4:in `load’ -e:4
Okay finally worked this out, I had a role which has a empty string for the title. Makes sense now looking at the error!!