Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think that has no problem to have the same model for all kind of users. You only need to define the user role, through a database column for example.</p> <p>But in many cases, you'll need more than a user role to deny or allow access, you'll need to verify if the current_user has requested the current Order, for example. So you can do this:</p> <p>Suppose you have this model:</p> <pre><code>class User &lt; ActiveRecord::Base devise :database_authenticatable, :confirmab... ... def admin? role == 'admin' end end </code></pre> <p>So you can verify User role through some method in your model like <code>admin?</code>.</p> <p>Now you can use Can Can for your authorization, as you can <a href="https://github.com/ryanb/cancan/wiki/defining-abilities" rel="nofollow">see here</a> how to define your abilities.</p> <p>Abilty class:</p> <pre><code>class Ability include CanCan::Ability def initialize(user) user ||= User.new # guest user (not logged in) if user.admin? can :manage, :all else can :read, :all end end end </code></pre> <p>Understands how CanCan works is an important thing: <a href="https://github.com/ryanb/cancan/#readme" rel="nofollow">look here</a>.</p> <p>Suppose you want to get params into your ability class that allows you to verify objects in your database before load the page, allowing or not the user access. In your application_controller file, put something like this:</p> <pre><code> ... def current_ability @current_ability ||= Ability.new(params, current_user) end ... </code></pre> <p>In your ability, change arguments of initialize method and uses <code>params</code> as you need:</p> <pre><code>class Ability include CanCan::Ability def initialize(params, user) user ||= User.new # guest user (not logged in) if user.admin? can :manage, :all else # Verify the record for atuhorization can :manage, Order if Order.find(params[:id]).requester_id == user.id end end end </code></pre>
    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. 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