cancan - Rails - CanCanCan - common abilities -


i using rails 4, devise, role model , cancancan.

is possible define ability in ability.rb common number of roles?

for example, every logged in user can crud own profile page? , roles have specific abilities on top of common ability?

how work? need create role in role model common abilities , allow each user have multiple roles, common abilities role specific abilities?

for example, in ability.rb, have:

class ability   include cancan::ability    def initialize(user)        alias_action :create, :read, :update, :destroy, :to => :crud       # define abilities passed in user here. example:     #     user ||= user.new # guest user (not logged in)        #users not signed in can create registration or login         # can read publicly available projects, programs , proposals       can :read, project, {:active => true, :closed => false, :sweep => { :disclosure => { :allusers => true } } }        # {:active => true, :closed => false  &&  :project.sweep.disclosure.allusers => true}       # if user role student        if user_signed_in?         can :crud, profile, :user_id => user.id #[for themselves]         elsif user.try(:profile).present? && user.profile.has_role?(:student) 

so, want students able read same things guests can read. there way students can new users , users signed in can (as student specific abilities)?

you can make kind of composition in roles through function calls this

class ability   include cancan::ability    def initialize(user)     # define abilities passed in user here. example:     #     user ||= user.new # guest user (not logged in)        #users not signed in can create registration or login        # can read publicly available projects, programs , proposals        # {:active => true, :closed => false  &&  :project.sweep.disclosure.allusers => true}       # if user role student        if user_signed_in?         if user.try(:profile).present? && user.profile.has_role?(:student)           student         else           authenticated         end       else         anonymous       end   end    def anonymous       can :read, project, {:active => true, :closed => false, :sweep => { :disclosure => { :allusers => true } } }   end    def authenticated     anonymous     can :crud, profile, :user_id => user.id #[for themselves]   end    def student     authenticated     #other student abilities   end   #other roles follow same principal   def teacher     authenticated   end end 

the authenticated function contain common abilities role , each role needs call (it's kind of inheritance student can authenticated user can plus abilities)


Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -