cancancan icon indicating copy to clipboard operation
cancancan copied to clipboard

Use Cancancan with Multiple Ability files for different models

Open Frank004 opened this issue 10 years ago • 11 comments

Hi its possible to have more that one ability file or extra arg in the initialize. I need to use the cancancan on a Admin model , User Model and a Company Model example:

class PolicyAbility
  include CanCan::Ability
  def initialize(company) #current_company 
    if company.policy.each do |p|
     ... 
    end
  end
end

class Ability
  include CanCan::Ability
  def initialize(user) #current_user 
   if user.role == "manager"
      can [:manage], User
   else
     can [:index,:show] User
   end
  end
end
class AdminAbility
  include CanCan::Ability
  def initialize(admin) #current_admin
      can [:manage], Company
  end
end

Frank004 avatar Oct 04 '16 18:10 Frank004

This the solution that I use for my problem. Love to know if there's a better solución for this.

class ApplicationController < ActionController::Base
   def current_company
         ...
   end 
 private
    def current_ability
      @current_ability ||= Ability.new(current_user, current_company)
    end
end
class Ability
  include CanCan::Ability
  def initialize(user, company)
    user stuff
    and company stuff
  end
end

Frank004 avatar Oct 04 '16 23:10 Frank004

class ApplicationController < ActionController::Base
   def current_company
         ...
   end 
 private
    def current_ability
      @current_ability ||=
        if current_user
          Ability.new(current_user, current_company)
        else current_super_admin
          SuperAdminAbility.new
        end
    end
end

Frank004 avatar Oct 12 '16 15:10 Frank004

Does this still work for you @Frank004 ?

kaizenx avatar Jan 04 '18 11:01 kaizenx

@kaizenx yes I'm still using it on rails 5.0

Frank004 avatar Jan 05 '18 02:01 Frank004

Hi can yo show the final code in Ability, controlles and models.

lalovargas avatar Jan 12 '18 00:01 lalovargas

@Frank004 would be awesome if you can share some more code

mustela avatar Jan 23 '18 15:01 mustela

From cancan :

  • https://github.com/ryanb/cancan/issues/311#issuecomment-904149
  • https://github.com/ryanb/cancan/wiki/Changing-Defaults
# routes.rb
devise_for :users # current_user
devise_for :admins # current_admin
# in application_controller.rb
def current_ability
  @current_ability ||= current_admin ? AdminAbility.new(current_admin) : UserAbility.new(current_user)
end

dinatih avatar Jan 25 '18 00:01 dinatih

@mustela and @lalovargas

ApplicationController

  def current_ability
    if current_user
      @current_ability ||=  Ability.new(current_user, current_company)
    elsif current_admin
      @current_ability ||=  AdminAbility.new(current_admin)
    end
  end

Ability for user and Ability for Admin

nothing more

Frank004 avatar Jan 31 '18 16:01 Frank004

Thanks @Frank004, I did a few more things, and the performance improvement was incredible.

We used to have a single ability file per role, with a lot of rules. So it was actually killing each request. What we did was to use the controller name to actually load only the abilities you need for that particular endpoint.

So we ended up with something like

-- abilities
---- user_ability < base_ability
---- resource_ability < base_ability
---- another_resource_ability < base_ability

Then we have a factory that we placed in the application_controller that loads the correct class, something like:

def current_ability
  @current_ability ||= Abilities::BaseAbility.build(current_user, controller_name)
end

Hope it helps :)

mustela avatar Jan 31 '18 17:01 mustela

@mustela WOW that's better than what I'm using the request go faster like this.

Frank004 avatar Jan 31 '18 17:01 Frank004

https://github.com/CanCanCommunity/cancancan/wiki/Defining-Abilities:-Best-Practices#split-your-abilityrb-file

gazeldx avatar Sep 04 '19 08:09 gazeldx