我已经设置好了ActiveAdmin和Pundit,并且可以正常工作了。我的用户模型有一个角色属性(rails 4.1枚举)。如何仅允许具有管理员角色的用户登录/admin?
发布于 2015-02-04 21:14:01
这是另一种不需要接触ActiveAdmin::BaseController的方法。
在AA初始值设定项中,添加
# config/initializers/active_admin.rb
config.on_unauthorized_access = :user_not_authorized并实现ApplicationController#user_not_authorized
# app/controllers/application_controller.rb
:
def user_not_authorized(exception)
sign_out # or user will end up in a redirection loop
flash[:error] = "Access has been denied because ..."
redirect_to new_user_session_path
end最后,更新AA页面(或仪表板等)的策略
# app/policies/active_admin/page_policy.rb
module ActiveAdmin
class PagePolicy < ::ActiveAdminPolicy
def show?
User.roles[user.role] >= User.roles['manager']
end
end
end完成后,只有经理及以上人员才能登录。
发布于 2015-02-04 20:29:54
我也面临着同样的问题,下面是我是如何解决的:
角色在User#role中定义。
class User < ActiveRecord::Base
enum role: [ :client, :reseller, :manager, :admin, :super ]
end以下是如何限制所有作为客户或经销商的用户访问AA的/admin。
首先,将before_filter添加到AA的BaseController中,如here所述。
# lib/active_admin/deny_unprivileged/usage.rb
module ActiveAdmin
##
# Only users showing a minimum user level (role)
# should be allowed to access the administration interface.
module DenyUnprivilegedUsage
extend ActiveSupport::Concern
included do
before_filter :deny_unprivileged_usage
end
private
##
# Deny access to /admin, unless user is, at least, a manager.
#
# This does the same as Devise::Controllers::Helpers#sign_out_and_redirect,
# but adds a flash message explaining why access has been denied.
#
# See Devise::Controllers::Helpers#sign_out_and_redirect
def deny_unprivileged_usage
if current_user and not User.roles[current_user.role] >= User.roles['manager']
resource_or_scope = current_user
scope = ::Devise::Mapping.find_scope!(resource_or_scope)
redirect_path = new_user_session_path
::Devise.sign_out_all_scopes ? sign_out : sign_out(scope)
flash[:error] = "!!!"
redirect_to redirect_path
end
end
end
end创建一个初始化器。
# config/initializers/active_admin_extensions.rb
require 'active_admin/deny_unprivileged_usage'
ActiveAdmin::BaseController.send(:include, ActiveAdmin::DenyUnprivilegedUsage)重新启动服务器。
现在,不是经理的用户将被拒绝访问/admin。用户实际上将成功登录,但是,在登录之后,上面定义的筛选器将立即注销用户,并重定向回登录页面。
deny_unprivileged_usage方法几乎是Devise::Controllers::Helpers#sign_out_and_redirect的副本-只是它添加了一条flash消息,向用户解释登录被拒绝的原因。如果没有此更改,用户将只能返回到登录页面,而不知道登录被拒绝的原因。
更新
转念一想,如果用户注册了,很难想象他们永远都不能登录。
因此,对不同的名称空间使用不同的授权适配器和策略来实现基于角色的访问限制可能会更简洁。如here所述。
然后,/reseller将能够登录,例如,更改其密码,而/manager将被呈现更多的功能。这样,策略和AA资源将被清楚地分开。
https://stackoverflow.com/questions/25231109
复制相似问题