Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As I know CanCan and declarative_authorization, and I implemented role-based authorizations with both, I recommend CanCan. Just my two cents. </p> <p><strong>Example</strong> (untested, unfortunately I cannot test here and I have no access to my code)</p> <p>So let's say we have a structure like this:</p> <pre><code>class User &lt; ActiveRecord::Base belongs_to :role end class Role &lt; ActiveRecord::Base has_many :users # attributes: project_read, project_create, project_update end </code></pre> <p>Then, CanCan could look like this:</p> <pre><code>class Ability include CanCan::Ability def initialize(user) @user = user @role = user.role # user can see a project if he has project_read =&gt; true in his role can :read, Project if role.project_read? # same, but with create can :create, Project if role.project_create? # can do everything with projects if he is an admin can :manage, Project if user.admin? end end </code></pre> <p>You can find all information you need in the CanCan wiki on github. Personal recommendation to read: </p> <ul> <li><a href="https://github.com/ryanb/cancan/wiki/Defining-Abilities" rel="nofollow">https://github.com/ryanb/cancan/wiki/Defining-Abilities</a></li> <li><a href="https://github.com/ryanb/cancan/wiki/Defining-Abilities-with-Blocks" rel="nofollow">https://github.com/ryanb/cancan/wiki/Defining-Abilities-with-Blocks</a></li> <li><a href="https://github.com/ryanb/cancan/wiki/Authorizing-Controller-Actions" rel="nofollow">https://github.com/ryanb/cancan/wiki/Authorizing-Controller-Actions</a></li> </ul> <p>Basically you just need to extend the example above to include your roles through your relations. To keep it simple, you can also create additional helper methods in <code>ability.rb</code>.</p> <p>The main mean caveat you may fall for (at least I do): Make sure your user <em>can</em> do something with a model before you define what the user <em>can't</em>. Otherwise you'll sit there frustrated and think "but why? I never wrote the user can't.". Yeah. But you also never explicitly wrote that he can... </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload