Spree自定义/扩展用户角色和权限

swa*_*pab 4 ruby-on-rails spree

我试图在 Spree 中指定一些自定义角色,例如角色“客户端”,并扩展访问该角色的管理部分的权限。

该用户将只能访问该用户创建的那些产品。概念是让具有“客户”角色的用户仅管理产品和其他某些模型。

首先,我添加了 CanCan 插件并在 role_ability.rb 中定义了 RoleAbility 类

就在这篇文章之后:Spree 自定义角色权限

class RoleAbility
  include CanCan::Ability

  def initialize(user)
    user ||= User.new
    if user.has_role? 'admin'
        can :manage, :all
    elsif user.has_role? 'client_admin'
      can :read, Product
      can :admin, Product
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

将其添加到初始化程序中:config/initializers/spree.rb

Ability.register_ability(RetailerAbility)
Run Code Online (Sandbox Code Playgroud)

还扩展了 admin_products_controller_decorator.rb :app/controllersadmin_products_controller_decorator.rb

Admin::ProductsController.class_eval do
    def authorize_admin
        authorize! :admin, Product
        authorize! params[:action].to_sym, Product
    end
end
Run Code Online (Sandbox Code Playgroud)

但我收到闪现消息“授权失败”

为了寻找一些运气,我参考了以下链接

自定义 Spree 角色的 github 要点:https://gist.github.com/1277326

这是我面临的类似问题:http://groups.google.com/group/spree-user/browse_thread/thread/1e819e10410d03c5/23b269e09c7ed47e

所有的努力都白费了……

有任何高度赞赏的指点吗?

提前致谢。

swa*_*pab 5

终于找到了。

经过仔细研究后发现,spree 1.1.1 通过向每个类添加“Spree”模块来更改类,这导致了像“spree/admin/overview”这样的 url,而之前是“admin/overview”。

所以这对我有用,我能够控制我的角色的权限。