As I know CanCan and declarative_authorization, and I have implemented role-based credentials with both, I recommend CanCan. Only my two cents.
Example (unverified, unfortunately, I can not check here, and I do not have access to my code)
So, let's say we have this structure:
class User < ActiveRecord::Base belongs_to :role end class Role < ActiveRecord::Base has_many :users
Then CanCan might look like this:
class Ability include CanCan::Ability def initialize(user) @user = user @role = user.role
You can find all the information you need on the CanCan wiki on github. Personal recommendations:
Basically, you just need to extend the example above to include your roles in your relationship. To keep it simple, you can also create additional helper methods in ability.rb
.
The main middle caveat you can count on (at least me) is to make sure your user can do something with the model before determining what the user cannot. Otherwise, you will sit there disappointed and think, "but why? I never wrote that the user cannot." Yes. But you also never explicitly wrote that he can ...
pduersteler
source share