{"id":8024,"date":"2015-11-21T13:43:04","date_gmt":"2015-11-21T13:43:04","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2015\/11\/21\/pundit-with-rails-plus-user-admin-and-roles-models-open-source-projects-elabs-pundit\/"},"modified":"2022-08-30T15:03:03","modified_gmt":"2022-08-30T15:03:03","slug":"pundit-with-rails-plus-user-admin-and-roles-models-open-source-projects-elabs-pundit","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2015\/11\/21\/pundit-with-rails-plus-user-admin-and-roles-models-open-source-projects-elabs-pundit\/","title":{"rendered":"Pundit with Rails plus User, Admin and Roles Models-open source projects elabs\/pundit"},"content":{"rendered":"<p><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/b9ec534333161d95ff15a80f1c2bcddb?s=128&amp;d=identicon&amp;r=PG\" \/> <strong>monkbroc<\/strong><\/p>\n<p>In the publicly accesible part of my application, the User model is used. The default Pundit configuration works since <code>pundit_user<\/code> returns <code>current_user<\/code>.<\/p>\n<p>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).<\/p>\n<p>In admin controllers <code>pundit_user<\/code> returns <code>current_administrator<\/code>.<\/p>\n<p>In Rails Admin, the current user is set to the current administrator:<\/p>\n<pre><code>config.authenticate_with do\n  warden.authenticate! :scope =&gt; :administrator\nend\nconfig.current_user_method(&amp;:current_administrator)\n<\/code><\/pre>\n<p>While I don&#8217;t use authorization in Rails Admin(all admins have all powers), you should be able to use the <code>rails_admin_pundit<\/code> gem since it does<\/p>\n<p><code>@controller.class.send(:alias_method, :pundit_user, :_current_user)<\/code><\/p>\n<p>which is defined in Rails Admin as<\/p>\n<pre><code>def _current_user\n  instance_eval(&amp;RailsAdmin::Config.current_user_method)\nend\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>monkbroc 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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-8024","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/8024","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=8024"}],"version-history":[{"count":1,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/8024\/revisions"}],"predecessor-version":[{"id":8686,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/8024\/revisions\/8686"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=8024"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=8024"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=8024"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}