【问题标题】:Howto add a new role with a custom permission set for Solidus / Spree?如何为 Solidus / Spree 添加具有自定义权限集的新角色?
【发布时间】:2016-01-24 19:07:05
【问题描述】:

我想添加一个新的“供应商”角色。 供应商可以管理自己的产品和订单,但看不到属于其他供应商的任何订单或产品。

我计划在 Stock Location 周围实施这一点。 例如:一位具有供应商角色的用户属于东京库存位置。具有供应商角色的其他用户属于其他库存位置。

我需要采取哪些步骤才能使其正常工作?

【问题讨论】:

  • 尝试回答需要回答的问题总是好的。请提供更好的标题,因为它并不表示问题。 “如何使用自定义权限集创建自定义角色。”会更好,但使用“如何使用自定义权限集创建自定义角色”。根本没有必要使用您的第一段,我建议将其删除。另外,你的格式不对。提出问题,然后在答案中提供解决方案。不要把它全部放在问题中。 Stack Overflow 会让你选择你的答案。
  • 我同意。感谢您的输入。 :-)

标签: ruby-on-rails ruby metaprogramming spree


【解决方案1】:

我相信在 Solidus 中您会创建一个 PermissionSet,并在您的 Spree/Solidus 初始化程序中为该新角色分配权限集。通过这种方式,您可以避免所有 if/then/else 并轻松存储具有重叠权限集的多个角色。

【讨论】:

    【解决方案2】:

    1。创建供应商角色

    $ rails console
    Spree::Role.create(name: 'vendor')
    

    2。定义 CanCan 权限:

    app/models/spree/multi_vendor_ability.rb

    module Spree
      class MultiVendorAbility
        include CanCan::Ability
    
        def initialize(user)
          user ||= Spree::User.new # guest user (not logged in)
          if user.admin?
            can :manage, :all
            puts "IS admin"
          else
            can :read, :all
            puts "NOT admin"
          end
    
          if user.stock_locations.present?
            can  :manage, Spree::StockItem, :stock_location_id => user.stock_locations.first.id
          end
        end
    
      end
    end
    

    3。向 Spree::User 添加新方法

    config/initializers/spree_user.rb

    Spree::User.class_eval do
    
        def vendor?
          self.role_users.any? { |ru| ru.role.name == 'vendor' }
        end
    
    end
    

    4。 RSpec

    spec/models/multi_vendor_spec.rb

    require 'spec_helper'
    
    describe Spree::MultiVendorAbility do
      let(:ability) { Spree::MultiVendorAbility.new(user) }
      let(:user)    { create(:user) }
      let(:product) { create :product }
      let(:stock_location) { create(:stock_location_with_items) }
      let(:stock_item) { stock_location.stock_items.order(:id).first }
    
      subject { ability }
    
      context "test" do
        before do
          Rails.logger.debug "The StockItem is #{stock_item.inspect}"
          user.stock_locations = [stock_location]
          user.save
        end
    
        context "when the user is associated with the stock location" do
           it { is_expected.to be_able_to(:manage,  Spree::StockItem) }
        end
    
        context "when the user is NOT associated with the stock location" do
            before do
              stock_item.stock_location_id = nil
              stock_item.save
              user.stock_locations = []
              user.save
              puts "The StockItem is #{stock_item.inspect}"
              user.stock_locations = []
            end
            it { is_expected.to_not be_able_to(:manage, Spree::StockItem) }
        end
    
      end
    
    end
    

    【讨论】:

    • 我在solidus_auth_devise 或solidus 中没有看到app/models/spree/multi_vendor_ability.rb。
    • 不,这是您添加到您的 Rails 应用程序的文件。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-01
    • 2020-01-08
    • 1970-01-01
    • 2022-07-27
    相关资源
    最近更新 更多