Rails 3 ActiveAdmin. How to disable the dashboard for users with a role other than an administrator?

Here is how I can see the user role: current_admin_user.role

I am using CanCan

How can I

  • Hide toolbar button
  • Restrict access to your dashboard if the user finds out his URL

- I tried this, but it doesn’t work.

 dashboard_controller.rb if current_admin_user.role == 'customer' redirect_to shipments_path end 

I tried this in admin / dashboards.rb

 controller do def scoped_collection if current_admin_user.role == 'customer' redirect_to shipments_path end end end 

but raises an undefined method 'controller' for ActiveAdmin::Dashboards:Module (NoMethodError)

+4
source share
3 answers

I think you know this place: https://github.com/gregbell/active_admin/issues/501 , some good ideas about dashboards there.

Partialize something like this in the dashboards.rb file:

 ActiveAdmin::Dashboards.build do section 'Common', :priority => 1 do div do render 'common_dashboard' end end ... end 

Then from the partial that you have to create in app / views / admin / dashboard / _common_dashboard.html.erb, you can access the current_admin_user object:

 <ul> <li><%= current_admin_user.role %></li> </ul> 

Another way to access current_admin_user from dashboards.rb '' 'environment' '' is to use the arbre syntax and formulate sections of your panel, for example

 section "Common",:priority => 1 do div do if current_admin_user.role == "customer" li "You are a customer" end end '' end 
+3
source

Aslo, you can limit the sections of the control panel: if the conditions, so the toolbar will be available to all users, but only with allowed sections

 section("Recent Users", :if => proc{ can?(:manage, User) }) do ul do User.limit(10).order('created_at desc').collect do |user| li link_to(user.name, admin_user_path(user)) end end 
+1
source

you can only access the redirect_to controller action inside the toolbar section as follows:

 section do if current_admin_user.role == 'customer' controller.redirect_to shipments_path #any path you have in application else div do ... end end 
+1
source

Source: https://habr.com/ru/post/1392151/


All Articles