我正在为我的应用程序使用rails administrate,但是我想通过administrate仪表板限制对正在管理的资源的访问。
我还在我的rails应用程序的其他部分使用cancancan来管理访问和权限。
是否有人设法在administrate中使用cancancan,以便administrate仪表板可以使用在cancancan中定义的功能,显示资源并应用相同的权限?
谢谢
发布于 2018-12-21 06:19:31
你可以在这里找到一些关于需要做什么的信息:https://administrate-prototype.herokuapp.com/authorization
这里提到的内容可以很好地过滤记录集合,但在尝试授权单个资源时会中断。解决方案是覆盖find_resource方法。以下是最终的工作代码:
# app/controllers/admin/application_controller.rb
rescue_from CanCan::AccessDenied do |exception|
flash[:notice] = "Access Denied"
redirect_to admin_root_path
end
# Override find_resource, because it initially calls scoped_resource.find(param)
# which breaks since we are overriding that method as well.
def find_resource(param)
resource_class.default_scoped.find(param)
end
# Limit the scope of the given resource
def scoped_resource
super.accessible_by(current_ability)
end
# Raise an exception if the user is not permitted to access this resource
def authorize_resource(resource)
raise CanCan::AccessDenied unless show_action?(params[:action], resource)
end
# Hide links to actions if the user is not allowed to do them
def show_action?(action, resource)
# translate :show action to :read for cancan
if ["show", :show].include?(action)
action = :read
end
can? action, resource
end这将使您开始使用CanCan进行基本的资源授权。如果你需要限制对嵌套资源的访问,可能需要对字段视图进行进一步的定制,但从那时起,这应该是非常标准的。希望这能有所帮助。:)
https://stackoverflow.com/questions/53238556
复制相似问题