Pundit with Rails plus User, Admin and Roles Models

倾然丶 夕夏残阳落幕 提交于 2019-12-06 07:21:55

In the publicly accesible part of my application, the User model is used. The default Pundit configuration works since pundit_user returns current_user.

In the admin part of my application, the Administrator model is used. One reason for this is that an admin can stay logged in while impersonating a user (to see what they see in their profile and help troubleshoot).

In admin controllers pundit_user returns current_administrator.

In Rails Admin, the current user is set to the current administrator:

config.authenticate_with do
  warden.authenticate! :scope => :administrator
end
config.current_user_method(&:current_administrator)

While I don't use authorization in Rails Admin(all admins have all powers), you should be able to use the rails_admin_pundit gem since it does

@controller.class.send(:alias_method, :pundit_user, :_current_user)

which is defined in Rails Admin as

def _current_user
  instance_eval(&RailsAdmin::Config.current_user_method)
end
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!