Reputation: 1505
In RailsAdmin, how can I config it so that it only shows record associated to the current user? Right now, every 'admin' login will see everyone's records.
I want admin1 login to see only what's associated to admin1. Admin1 should not see Admin2's data.
I am not using Devise, Sorcery or Cancancan. All done manually at the moment.
Upvotes: 1
Views: 643
Reputation: 1505
I figured it out.
In rails_admin.rb, add:
config.current_user_method do
user = User.find_by( id:session[ :user_id ] )
end
config.authorize_with :cancan
In ability.rb:
class Ability
include CanCan::Ability
def initialize(user)
if user && user.admin?
can :access, :rails_admin
can :dashboard
can :manage, User, id: user.id
can :manage, Manager, user_id: user.id
can :manage, Staff, manager: { user_id: user.id }
end
end
end
Upvotes: 1
Reputation: 95
Sounds like you are looking for something like this:
https://github.com/influitive/apartment
If you want to do it manually... you'll want your controller to call a query that will filter to just the results that that user is supposed to have access to... So instead of something like Client.all , you'd use something like current_user.clients ....
Upvotes: 0