如何将对活动管理员的访问权限限制为管理员用户 [英] How to limit access to active admin to admin users

查看:69
本文介绍了如何将对活动管理员的访问权限限制为管理员用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望只有其属性 is_admin 设置为true的用户才能访问我的活动管理员后端

I want that only my users who have their attribute is_admin set to true to be able to access my active admin backend

我应该怎么做?

普通用户只能登录该网站,而不能登录到活动管理员。

"Normal" users should only be able to login to the site, not to active admin.

推荐答案

config / initializers / active_admin.rb 中config:

config.authentication_method = :authenticate_admin_user!

因此,如果您创建一个名为authenticate_admin_user的方法!在ApplicationController中,ActiveAdmin将检查用户是否可以进入管理页面。像这样:

so if you create a method named authenticate_admin_user! in the ApplicationController, then ActiveAdmin will check if the user can go to the admin pages or not. Like this:

# restrict access to admin module for non-admin users
def authenticate_admin_user!
  raise SecurityError unless current_user.try(:admin?)
end

并且从ApplicationController中的异常中解救出来(或者您实际上可以在 authenticate_admin_user!方法内部重定向)

and rescue from that exception in ApplicationController (or you can actually redirect inside the authenticate_admin_user! method)

rescue_from SecurityError do |exception|
  redirect_to root_url
end

还有另一件事,如果您不如果有admin_users,则最好在 config / initializers / active_admin.rb 中更改此行:

And one more small thing, if you don't have admin_users, then it would be nice to change this line in config/initializers/active_admin.rb:

config.current_user_method = :current_user

通过设计,您可能想要为管理员/非管理员用户提供不同的默认路径,因此您可以在控制器中定义 after_sign_in_path_for 方法

And with devise you might want to make the default path different for admin/non-admin users, so you can define after_sign_in_path_for method in the controller

# path for redirection after user sign_in, depending on user role
def after_sign_in_path_for(user)
  user.admin? ? admin_dashboard_path : root_path 
end

这篇关于如何将对活动管理员的访问权限限制为管理员用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆