Skip to content

armgit / spree

Public Imported from GitHub
0

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.

Fix: Read delivery methods, markets, channels, stock locations, product types, tax categories and option types through the store

Damian Legawiec23fbea56847f
diff --git a/spree/api/app/controllers/spree/api/v3/admin/orders/fulfillments_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/orders/fulfillments_controller.rb
index 1e537edd29..7b2c71c0e2 100644
--- a/spree/api/app/controllers/spree/api/v3/admin/orders/fulfillments_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/admin/orders/fulfillments_controller.rb
@@ -186,7 +186,7 @@ module Spree
def delivery_method_for_create
return if create_params[:delivery_method_id].blank?
- Spree::DeliveryMethod.accessible_by(current_ability, :show).find_by_prefix_id!(create_params[:delivery_method_id])
+ current_store.delivery_methods.accessible_by(current_ability, :show).find_by_prefix_id!(create_params[:delivery_method_id])
end
def source_fulfillment_for_create
diff --git a/spree/api/app/controllers/spree/api/v3/admin/product_types_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/product_types_controller.rb
index 9072009c6d..25aec6fa56 100644
--- a/spree/api/app/controllers/spree/api/v3/admin/product_types_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/admin/product_types_controller.rb
@@ -76,9 +76,21 @@ module Spree
def permitted_params
attrs = params.permit(*permitted_attributes)
attrs = scope_delivery_profile(attrs)
+ attrs = scope_option_type_ids(attrs)
scope_category_ids(attrs)
end
+ # `option_type_ids=` resolves prefixed ids with no store scoping, so a
+ # type could otherwise seed another store's option types onto every
+ # product it is applied to.
+ def scope_option_type_ids(attrs)
+ return attrs if attrs[:option_type_ids].blank?
+
+ option_type_ids = decode_ids(attrs[:option_type_ids], Spree::OptionType)
+ attrs[:option_type_ids] = Spree::OptionType.for_store(current_store).where(id: option_type_ids).pluck(:id)
+ attrs
+ end
+
# The template profile must belong to this store — a cross-store id
# 404s instead of silently linking.
def scope_delivery_profile(attrs)
@@ -93,7 +105,6 @@ module Spree
# `category_ids=` resolves prefixed ids with no store scoping, so a
# type in this store could otherwise be pointed at another store's
# categories. Mirrors ProductsController#apply_categories.
- # (OptionType is global — nothing to scope there.)
def scope_category_ids(attrs)
return attrs if attrs[:category_ids].blank?
diff --git a/spree/api/app/controllers/spree/api/v3/admin/products_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/products_controller.rb
index eda440bc82..55ee2de35c 100644
--- a/spree/api/app/controllers/spree/api/v3/admin/products_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/admin/products_controller.rb
@@ -323,7 +323,24 @@ module Spree
end
def permitted_params
- params.permit(*permitted_attributes).tap { |attrs| attrs.delete(:status) if review_status?(attrs[:status]) }
+ params.permit(*permitted_attributes).tap do |attrs|
+ attrs.delete(:status) if review_status?(attrs[:status])
+ scope_store_references(attrs)
+ end
+ end
+
+ # Read through the store here because the model only checks that the
+ # record exists: another store's type or tax category would otherwise
+ # be accepted, and the type would seed its option types onto the
+ # product.
+ STORE_REFERENCES = { product_type_id: :product_types, tax_category_id: :tax_categories }.freeze
+
+ def scope_store_references(attrs)
+ STORE_REFERENCES.each do |key, association|
+ next if attrs[key].blank?
+
+ attrs[key] = current_store.public_send(association).find_by_param!(attrs[key]).id
+ end
end
# A review status is an outcome, not a value to assign: `proposed`
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/orders/fulfillments_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/orders/fulfillments_controller_spec.rb
index 55fd48f36b..1041bbbf08 100644
--- a/spree/api/spec/controllers/spree/api/v3/admin/orders/fulfillments_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/admin/orders/fulfillments_controller_spec.rb
@@ -110,6 +110,18 @@ RSpec.describe Spree::Api::V3::Admin::Orders::FulfillmentsController, type: :con
expect(fulfillment.delivery_method).to eq(delivery_method)
end
+ it 'refuses a delivery method from another store' do
+ delivery_method = create(:delivery_method, store: create(:store))
+
+ post :create, params: {
+ order_id: order.prefixed_id,
+ stock_location_id: shipment.stock_location.prefixed_id,
+ delivery_method_id: delivery_method.prefixed_id
+ }, as: :json
+
+ expect(response).to have_http_status(:not_found)
+ end
+
it "registers an already-shipped fulfillment with status: 'shipped' and an explicit cost" do
post :create, params: {
order_id: order.prefixed_id,
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/product_types_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/product_types_controller_spec.rb
index 1d3031afdd..c6677824e2 100644
--- a/spree/api/spec/controllers/spree/api/v3/admin/product_types_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/admin/product_types_controller_spec.rb
@@ -64,6 +64,18 @@ RSpec.describe Spree::Api::V3::Admin::ProductTypesController, type: :controller
expect(product.reload.option_types).to be_empty
end
+ it 'ignores option types from another store' do
+ foreign_option_type = create(:option_type, store: create(:store))
+
+ patch :update, params: {
+ id: product_type.prefixed_id,
+ option_type_ids: [foreign_option_type.prefixed_id]
+ }, as: :json
+
+ expect(response).to have_http_status(:ok)
+ expect(product_type.reload.option_types).to be_empty
+ end
+
it 'ignores categories from another store' do
foreign_category = create(:category, store: create(:store))
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/products_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/products_controller_spec.rb
index 8db990f416..0c0fc34bf7 100644
--- a/spree/api/spec/controllers/spree/api/v3/admin/products_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/admin/products_controller_spec.rb
@@ -896,6 +896,15 @@ RSpec.describe Spree::Api::V3::Admin::ProductsController, type: :controller do
expect(product.reload.product_type).to eq(product_type)
end
+ it 'refuses a product type from another store' do
+ product_type = create(:product_type, store: create(:store))
+
+ patch :update, params: { id: product.prefixed_id, product_type_id: product_type.prefixed_id }, as: :json
+
+ expect(response).to have_http_status(:not_found)
+ expect(product.reload.product_type).to be_nil
+ end
+
it 'clears the product type with null' do
product.update!(product_type: create(:product_type, store: store))
diff --git a/spree/core/app/services/spree/orders/update.rb b/spree/core/app/services/spree/orders/update.rb
index 824e98caa5..3134e9231f 100644
--- a/spree/core/app/services/spree/orders/update.rb
+++ b/spree/core/app/services/spree/orders/update.rb
@@ -14,6 +14,7 @@ module Spree
items_param = @params.delete(:items)
address_params = extract_address_params
+ scope_store_references
# requires_new: the rescue below sits outside the block, so joining a
# caller's open transaction (the API's order lock) would let it commit
@@ -63,6 +64,24 @@ module Spree
end.compact_blank
end
+ # Named by id in the payload and read through the order's own store, as
+ # Orders::Create does: the model only checks that the record exists, so
+ # another store's market, channel or warehouse would otherwise be
+ # accepted.
+ STORE_REFERENCES = {
+ market_id: :markets,
+ channel_id: :channels,
+ preferred_stock_location_id: :stock_locations
+ }.freeze
+
+ def scope_store_references
+ STORE_REFERENCES.each do |key, association|
+ next if @params[key].blank?
+
+ @params[key] = @order.store.public_send(association).find_by_param!(@params[key]).id
+ end
+ end
+
def assign_addresses(address_params)
address_params.each do |association, attributes|
@order.public_send(:"#{association}_attributes=", attributes)
diff --git a/spree/core/spec/services/spree/orders/update_spec.rb b/spree/core/spec/services/spree/orders/update_spec.rb
index 891d5fc7b7..83f53b6052 100644
--- a/spree/core/spec/services/spree/orders/update_spec.rb
+++ b/spree/core/spec/services/spree/orders/update_spec.rb
@@ -28,6 +28,14 @@ module Spree
describe '#call' do
subject { described_class.call(order: order, params: params) }
+ context 'with a channel from another store' do
+ let(:params) { { channel_id: create(:channel, store: create(:store)).prefixed_id } }
+
+ it 'refuses it' do
+ expect { subject }.to raise_error(ActiveRecord::RecordNotFound)
+ end
+ end
+
context 'with empty params' do
let(:params) { {} }