|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +class Ability |
| 4 | + include CanCan::Ability |
| 5 | + |
| 6 | + def initialize(user) |
| 7 | + |
| 8 | + can :read, :all |
| 9 | + return unless user.present? |
| 10 | + can :manage, Post, author: user |
| 11 | + can :create, Comment |
| 12 | + can :manage, Comment, author: user |
| 13 | + return unless user.admin? |
| 14 | + can :manage, :all # finally we give all remaining permissions only to the admins |
| 15 | + # Define abilities for the user here. For example: |
| 16 | + # |
| 17 | + # return unless user.present? |
| 18 | + # can :read, :all |
| 19 | + # return unless user.admin? |
| 20 | + # can :manage, :all |
| 21 | + # |
| 22 | + # The first argument to `can` is the action you are giving the user |
| 23 | + # permission to do. |
| 24 | + # If you pass :manage it will apply to every action. Other common actions |
| 25 | + # here are :read, :create, :update and :destroy. |
| 26 | + # |
| 27 | + # The second argument is the resource the user can perform the action on. |
| 28 | + # If you pass :all it will apply to every resource. Otherwise pass a Ruby |
| 29 | + # class of the resource. |
| 30 | + # |
| 31 | + # The third argument is an optional hash of conditions to further filter the |
| 32 | + # objects. |
| 33 | + # For example, here the user can only update published articles. |
| 34 | + # |
| 35 | + # can :update, Article, published: true |
| 36 | + # |
| 37 | + # See the wiki for details: |
| 38 | + # https://github.com/CanCanCommunity/cancancan/blob/develop/docs/define_check_abilities.md |
| 39 | + end |
| 40 | +end |
0 commit comments