lundi 4 février 2019

Rails CanCanCan resource abilities based on department

I have two user types Admin and Normal User

I have the following departments

  • Front Office (eg., id is 1)
  • Back Office (eg., id is 2)
  • Admin (eg., id is 3)

I have a model named Entry which takes user_id, department_id, customer_id

Admin user has full control CRUD over all departments and all entries

Normal User are created for respective department and has CRU control over entries of respective department

When I create an entry from Normal User (eg., id is 2) account I get the correct set of customer_id, department_id, user_id in the table. The ability of this user has only one entry can_create_entry(current_user.id, customer_id, department_id) eg., (1, 1, 2) for front office normal user account.

When I create an entry from Admin(eg., id is 1) account, I get the customer_id, department_id, user_id as (1,1,1) in the table wen i tried to create an entry for Back office department whose id is 2.

When I examined the Ability list for Admin user; I found there is a duplicate for entries ability viz.,

#<CanCan::Rule:0x0000000b61fd18 @match_all=false, @base_behavior=true, @actions=[:create], @subjects=[Entry(Table doesn't exist)], @conditions={:user_id=>1, :customer_id=>2, :department_id=>1}, @block=nil>

#<CanCan::Rule:0x0000000b61fd18 @match_all=false, @base_behavior=true, @actions=[:create], @subjects=[Entry(Table doesn't exist)], @conditions={:user_id=>1, :customer_id=>2, :department_id=>2}, @block=nil>

#<CanCan::Rule:0x0000000b61fd18 @match_all=false, @base_behavior=true, @actions=[:create], @subjects=[Entry(Table doesn't exist)], @conditions={:user_id=>1, :customer_id=>2, :department_id=>3}, @block=nil>

And for Normal User I have only one entry

#<CanCan::Rule:0x0000000b61fd18 @match_all=false, @base_behavior=true, @actions=[:create], @subjects=[Entry(Table doesn't exist)], @conditions={:user_id=>2, :customer_id=>2, :department_id=>1}, @block=nil>

Kindly help me fix the issue for admin role.

Aucun commentaire:

Enregistrer un commentaire