Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Fix: resolved promotion rule products and categories through the promotion's store
Damian Legawiecbd18df54a403
diff --git a/spree/core/app/models/spree/promotion/rules/category.rb b/spree/core/app/models/spree/promotion/rules/category.rbindex 6b567065ab..c88b86dabb 100644--- a/spree/core/app/models/spree/promotion/rules/category.rb+++ b/spree/core/app/models/spree/promotion/rules/category.rb@@ -21,18 +21,16 @@ module Spree'category'end- # Decode prefixed IDs before delegating to the generated setter (direct- # calls bypass PrefixedId's assign_attributes auto-resolver).- #- # Resolved through the promotion's own store, so an id belonging to- # another store raises rather than quietly linking — the promotion- # would otherwise discount against a catalog its store cannot see.+ # Resolved through the promotion's own store, prefixed and raw ids+ # alike, so an id belonging to another store raises rather than+ # quietly linking — the promotion would otherwise discount against a+ # catalog its store cannot see.def category_ids=(ids)- super(Array(ids).map do |id|- Spree::PrefixedId.prefixed_id?(id) ? promotion_categories_scope.find_by_param!(id).id : id- end)+ super(ids_within_store(ids, promotion && promotion_categories_scope))end+ validate :categories_belong_to_store+## Preferences#@@ -140,6 +138,13 @@ module Spreepromotion.store.categoriesend+ def categories_belong_to_store+ return if promotion.nil? || category_ids.empty?+ return if (category_ids - promotion_categories_scope.where(id: category_ids).ids).empty?++ errors.add(:categories, :invalid)+ end+# IDs of categories in rule including all their childrendef eligible_category_ids_including_children@eligible_category_ids_including_children ||= begindiff --git a/spree/core/app/models/spree/promotion/rules/product.rb b/spree/core/app/models/spree/promotion/rules/product.rbindex b54915dcff..0aef705fbe 100644--- a/spree/core/app/models/spree/promotion/rules/product.rb+++ b/spree/core/app/models/spree/promotion/rules/product.rb@@ -15,6 +15,15 @@ module Spreeself.additional_permitted_attributes = [product_ids: []]+ validate :products_belong_to_store++ # Resolved through the promotion's own store, so another store's+ # product raises rather than being linked and then served back with+ # its admin data.+ def product_ids=(ids)+ super(ids_within_store(ids, promotion&.store&.products))+ end+## Preferences#@@ -86,11 +95,18 @@ module SpreeActiveSupport::Deprecation.warn('Please use `product_ids=` instead.')- self.product_ids = s+ self.product_ids = s.to_s.split(',')endprivate+ def products_belong_to_store+ return if promotion.nil? || product_ids.empty?+ return if (product_ids - promotion.store.products.where(id: product_ids).ids).empty?++ errors.add(:products, :invalid)+ end+def add_productsreturn if product_ids_to_add.nil?diff --git a/spree/core/app/models/spree/promotion_rule.rb b/spree/core/app/models/spree/promotion_rule.rbindex 510a7e7f3f..7b3c5a01c2 100644--- a/spree/core/app/models/spree/promotion_rule.rb+++ b/spree/core/app/models/spree/promotion_rule.rb@@ -55,6 +55,20 @@ module Spreeprivate+ # Resolves ids, prefixed or raw, through the promotion's store, so another+ # store's record raises instead of being linked. A rule built before its+ # promotion is known passes the ids through; its validation checks them.+ #+ # @param ids [Array<String, Integer>]+ # @param scope [ActiveRecord::Relation, nil] the store's records+ # @return [Array<Integer>]+ def ids_within_store(ids, scope)+ ids = Array(ids).reject(&:blank?)+ return ids if scope.nil?++ ids.map { |id| Spree::PrefixedId.prefixed_id?(id) ? scope.find_by_param!(id).id : scope.find(id).id }+ end+def eligibility_error_message(key, options = {})Spree.t(key, Hash[scope: [:eligibility_errors, :messages]].merge(options))enddiff --git a/spree/core/spec/models/spree/promotion/rules/category_spec.rb b/spree/core/spec/models/spree/promotion/rules/category_spec.rbindex 6ebdad9834..5ddc79d5e1 100644--- a/spree/core/spec/models/spree/promotion/rules/category_spec.rb+++ b/spree/core/spec/models/spree/promotion/rules/category_spec.rb@@ -136,5 +136,19 @@ describe Spree::Promotion::Rules::Category, type: :model doexpect { rule.category_ids = [foreign_category.prefixed_id] }.to raise_error(ActiveRecord::RecordNotFound)end++ # Mass assignment decodes prefixed ids before the setter runs, so the raw+ # id must be checked as well.+ it 'rejects the foreign raw ID' do+ expect { rule.category_ids = [foreign_category.id] }.+ to raise_error(ActiveRecord::RecordNotFound)+ end++ it 'is invalid when built before its promotion is known' do+ built = described_class.new(category_ids: [foreign_category.id])+ built.promotion = promotion++ expect(built).not_to be_valid+ endendenddiff --git a/spree/core/spec/models/spree/promotion/rules/product_spec.rb b/spree/core/spec/models/spree/promotion/rules/product_spec.rbindex 32617b2642..ab3dc0cc5c 100644--- a/spree/core/spec/models/spree/promotion/rules/product_spec.rb+++ b/spree/core/spec/models/spree/promotion/rules/product_spec.rb@@ -191,4 +191,34 @@ describe Spree::Promotion::Rules::Product, type: :model doexpect(rule.reload.updated_at).to be > original_updated_atendend++ # Linked products are served back with their admin data, so a product from+ # another store must never be linked.+ context 'when given a product from another store' do+ let(:promotion) { create(:promotion) }+ let(:foreign_product) { create(:product, store: create(:store)) }++ it 'rejects the foreign ID' do+ persisted = described_class.create!(promotion: promotion)++ expect { persisted.product_ids = [foreign_product.id] }.to raise_error(ActiveRecord::RecordNotFound)+ expect { persisted.product_ids = [foreign_product.prefixed_id] }.to raise_error(ActiveRecord::RecordNotFound)+ end++ it 'is invalid when built before its promotion is known' do+ built = described_class.new(product_ids: [foreign_product.id])+ built.promotion = promotion++ expect(built).not_to be_valid+ end++ it "accepts the promotion store's own product" do+ own = create(:product, store: promotion.store)+ persisted = described_class.create!(promotion: promotion)++ persisted.product_ids = [own.prefixed_id]++ expect(persisted.reload.products).to eq([own])+ end+ endend