0

I have a Rails-Application, that serves different sites. E.g.

  • www.example1.com
  • www.example2.com

These sites are stored in the Site-model. Also I have set up a User-Role system, using Devise, Rolify, and cancancan

Now one user can have different Roles on different site. E.g he can be an Administrator on www.example1.com, but only a simple user on www.example2.com

I am loading the users permissions in the ability-model. Now my question is: Where is this "initialize"-function called? I need to give this function an additional parameter site_id, so that only the appropriate rights of the site are loaded, not the one of the other side. How can I do this?

models/ability.rb

class Ability
  include CanCan::Ability
  # From where is this function called, and how can I adjust this call? 
  def initialize(user, site_id) 
    return false unless user.present?
    user_role = user.users_roles.find_by(site_id: site_id).try(:role)
    user_role.permissions.each do |p|
      if p.permission_subject_id.nil?
        can p.permission_action.to_sym, p.permission_subject_class.constantize
      else
        can p.permission_action.to_sym, p.permission_subject_class.constantize, id: p.subject_id
      end
    end unless user_role.nil?
  end
end
Michael B
  • 1,660
  • 3
  • 28
  • 59

1 Answers1

1

That's initialized using the current_ability method. So you need to overwrite that helper in the application_controller like so

class ApplicationController < ActionController::Base
  #...

  private

  def current_ability
    @current_ability ||= Ability.new(current_user, request. original_url)
  end
end
coderhs
  • 4,357
  • 1
  • 16
  • 25