Choosing an authorization framework for rails
At my main customer’s we needed to choose an authorization framework. This is for a complex enterprise application, and requiring fine-grained authorization on:
- roles
- actions
- model: most users can only access their own objects.
I’d had a look around, and after some digging ended up looking at 3 plugins, Declarative Authorization, grant and cancan.
Grant fell off almost immediately. It centered all authorization in the model, and I felt it was a bit too lightweight for our application.
Then I looked at declarative authorization and cancan.
At first sight, declarative authorization looked like a winner: I’m a believer in open source natural selection, and with about 650 people watching the plugin on github, it looked like a lot of people had found it a good fit. It’s also been lovingly polished since september 2008, so the kinks have probably been ironed out.
I cloned both plugins, and looked at the code and documentation.
Cancan is partly based on declarative_authorization. What struck me at first sight, is how simple cancan looked. Much less code, much less meta-monkey-magic. And a very friendly DSL and documentation.
And get this: I ran reek on both plugins (it’s a hobby of mine). And cancan came out practically clean ! That’s like having an alien in the living room ! It *never* happens ! Run reek on your own code, just for laughs, and you’ll see what I mean.
So we ended up choosing cancan, although declarative_authorization might have more features out of the box, we feel we’ll be able to extend cancan with much more ease, if at all necessary. It feels better to have a clean, fathomable codebase, than a larger engine. I’m aware that cancan has the unfair advantage of having learned from its predecessors, and kudos to the maintainers of declarative_authorization for having inspired others.
Note: I’m aware there are quite a few other plugins out there. If you found another one and you’re very happy about it, please share.
Like this:
6 Responses
Subscribe to comments with RSS.
Devise rocks!
http://github.com/plataformatec/devise
Already rails3 compatible.
Hi Maxim, I believe you
But devise is an authentication framework, not authorization.
Once you have identified yourself, you still have to decide what the person can and cannot do.
Because of its architecture devise can be partially implement this function. Like this:
$ ./script/generate devise User
$ ./script/generate devise Admin
In e.g. app/controllers/comments_controller.rb
before_filter :authenticate_user!
In e.g. app/controllers/admin/base_controller.rb
before_filter :authenticate_admin!
BTW there is not a problem to use devise with declarative_authorization
HI,
I’d not recommend using an authorization framework. We need to refactor apps for customers comming from foreign developers on a regular basis. Often they have used an authorization framework plugin which then has not been further maintained. Since authorization goes a long way those refactorings are always very painful for customers and us
So I’d suggest to think twice if and which framework/plugin to use.
– Julian
Hi Julian,
The choice is between reinventing the wheel with own plugins and app components, or using what’s out there, and the danger that it’s going to become obsolete always exists …
your comment is applicable to pretty much any plugin
cancan has the advantage of being really light-weight, so it’s not really introducing a heavy dependency. But you are right, keeping an eye on whether it’s maintained is an important point.
You’re right. It’s applicable to many plugins but authorization is somewhat special. Not many aspects pervade an application like authorization logic. I don’t speak about controller or action level authorization but more fine grained controls including data level authorization. Beside of that: yes, cancan looks pretty nice.