Rails 3 ActiveAdmin。如何为具有非管理员角色的用户禁用仪表板?

发布于 2024-12-27 22:08:02 字数 607 浏览 1 评论 0原文

这是我查看用户角色的方式: current_admin_user.role

我正在使用 CanCan

如何

  1. 隐藏仪表板按钮
  2. 限制对仪表板的访问,以防用户发现其 url

- 我已经尝试过这个但不起作用。

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

我在 admin/dashboards.rb 中尝试了此操作

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

,但产生错误 undefined method 'controller' for ActiveAdmin::Dashboards:Module (NoMethodError)

This is how I can see user's role: current_admin_user.role

I'm using CanCan

How can I

  1. Hide the Dashboard button
  2. Restrict access to the Dashboard in case the user finds out it's url

-
I have tried this but 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 produces error undefined method 'controller' for ActiveAdmin::Dashboards:Module (NoMethodError)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

树深时见影 2025-01-03 22:08:02

我想你知道这个地方: https://github.com/gregbell/active_admin/issues/501< /a>,那里有一些关于仪表板的好主意。

在dashboards.rb文件中渲染一个部分,如下所示:

ActiveAdmin::Dashboards.build do

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

然后从应该在app/views/admin/dashboard/_common_dashboard.html.erb创建的部分中,您可以访问current_admin_user对象:

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

另一种访问权限的方法dashes.rb '''environment''' 中的 current_admin_user 是使用 arbre 语法并像这样制定仪表板部分

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

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

Render a partial within your dashboards.rb file with something like this:

ActiveAdmin::Dashboards.build do

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

Then from the partial, which you should create at 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 get access to the current_admin_user from the dashboards.rb '''environment''' is to use the arbre syntax and formulate your dashboard sections like so

section "Common",:priority => 1 do 
  div do     
    if current_admin_user.role == "customer"
      li "You are a customer"
    end
  end
  '' 
end
眼眸印温柔 2025-01-03 22:08:02

您还可以使用 :if 条件限制仪表板部分,
因此仪表板将可供所有用户使用,但仅限于允许的部分

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

Aslo you can restrict dashboard sections with :if conditions,
so dashboard will be available for 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
氛圍 2025-01-03 22:08:02

您可以通过以下方式访问仪表板部分内的控制器操作 redirect_to

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

you can access controller action redirect_to just inside dashboard section in this way:

section do
  if current_admin_user.role == 'customer'
    controller.redirect_to shipments_path #any path you have in application
  else
    div do
    ...
    end
  end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文