首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >rails使用cancancan进行管理

rails使用cancancan进行管理
EN

Stack Overflow用户
提问于 2018-11-10 19:39:29
回答 1查看 433关注 0票数 3

我正在为我的应用程序使用rails administrate,但是我想通过administrate仪表板限制对正在管理的资源的访问。

我还在我的rails应用程序的其他部分使用cancancan来管理访问和权限。

是否有人设法在administrate中使用cancancan,以便administrate仪表板可以使用在cancancan中定义的功能,显示资源并应用相同的权限?

谢谢

EN

回答 1

Stack Overflow用户

发布于 2018-12-21 06:19:31

你可以在这里找到一些关于需要做什么的信息:https://administrate-prototype.herokuapp.com/authorization

这里提到的内容可以很好地过滤记录集合,但在尝试授权单个资源时会中断。解决方案是覆盖find_resource方法。以下是最终的工作代码:

代码语言:javascript
复制
# 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进行基本的资源授权。如果你需要限制对嵌套资源的访问,可能需要对字段视图进行进一步的定制,但从那时起,这应该是非常标准的。希望这能有所帮助。:)

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53238556

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档