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
Enforcer requirement of record IDs for bulk operations
Damian Legawiec571e68551c90
diff --git a/spree/api/app/controllers/concerns/spree/api/v3/bulk_operations.rb b/spree/api/app/controllers/concerns/spree/api/v3/bulk_operations.rbindex 4232e2e3f9..5f684368dc 100644--- a/spree/api/app/controllers/concerns/spree/api/v3/bulk_operations.rb+++ b/spree/api/app/controllers/concerns/spree/api/v3/bulk_operations.rb@@ -9,6 +9,12 @@ module Spreemodule BulkOperationsextend ActiveSupport::Concern+ included do+ # Guards the concern's two actions; host controllers add their own+ # bulk actions to the filter with `before_action :require_ids!, only: [...]`.+ before_action :require_ids!, only: [:bulk_add_tags, :bulk_remove_tags]+ end+# POST /api/v3/admin/<resource>/bulk_add_tags# Body: { ids: [...], tags: ['summer', 'sale'] }def bulk_add_tags@@ -33,6 +39,21 @@ module Spreeprivate+ # 422s when the caller omits `ids` entirely. An explicit empty array+ # is allowed (= no-op, useful when a UI clears its selection between+ # render and submit); a missing key is treated as a client bug since+ # bulk endpoints would otherwise silently match no rows and the+ # caller couldn't tell the difference from a successful zero-match.+ def require_ids!+ return if params.key?(:ids)++ render_error(+ code: 'missing_ids',+ message: 'ids is required (send an empty array to no-op).',+ status: :unprocessable_content+ )+ end+# Hook for controllers to perform additional work after bulk tag# mutations — e.g. enqueueing search reindex jobs, re-matching# automatic taxons. Mirrors `Spree::Admin::BulkOperationsConcern`.diff --git a/spree/api/app/controllers/spree/api/v3/admin/customers_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/customers_controller.rbindex cc302676ce..91731439bc 100644--- a/spree/api/app/controllers/spree/api/v3/admin/customers_controller.rb+++ b/spree/api/app/controllers/spree/api/v3/admin/customers_controller.rb@@ -7,6 +7,8 @@ module Spreescoped_resource :customers+ before_action :require_ids!, only: [:bulk_add_to_groups, :bulk_remove_from_groups]+def create@resource = Spree.user_class.new(permitted_params)# Admin-created customers don't pick a password upfront — theydiff --git a/spree/api/app/controllers/spree/api/v3/admin/products_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/products_controller.rbindex 511d3edb17..606c1b35b4 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@@ -7,6 +7,13 @@ module Spreescoped_resource :products+ before_action :require_ids!, only: [+ :bulk_status_update,+ :bulk_add_to_categories,+ :bulk_remove_from_categories,+ :bulk_destroy+ ]+# POST /api/v3/admin/products/:id/clonedef clone@resource = find_resource@@ -57,7 +64,13 @@ module Spreedef bulk_destroyauthorize! :destroy, model_class- destroyed = bulk_collection.count(&:destroy)+ # Scope by `:destroy` rather than reusing `bulk_collection`+ # (which is `:update`-scoped). Otherwise an admin with update+ # rights but no destroy rights could soft-delete records.+ destroy_scope = model_class.for_store(current_store)+ .accessible_by(current_ability, :destroy)+ .where(id: decode_ids(params[:ids]))+ destroyed = destroy_scope.count(&:destroy)render json: { product_count: destroyed }enddiff --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.rbindex 4951583ddd..fd127052d2 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@@ -620,11 +620,11 @@ RSpec.describe Spree::Api::V3::Admin::ProductsController, type: :controller doexpect(json_response).to eq('product_count' => 0, 'status' => 'archived')end- it 'is a no-op when ids is omitted entirely' do+ it 'rejects an omitted ids param with 422' dopost :bulk_status_update, params: { status: 'archived' }, as: :json- expect(response).to have_http_status(:ok)- expect(json_response['product_count']).to eq(0)+ expect(response).to have_http_status(:unprocessable_content)+ expect(json_response.dig('error', 'code')).to eq('missing_ids')endit 'rejects an invalid status with 422' do@@ -773,27 +773,31 @@ RSpec.describe Spree::Api::V3::Admin::ProductsController, type: :controller doendit 'touches the products' do- product_old_updated_at = product.updated_at- second_product_old_updated_at = second_product.updated_at+ product_old_updated_at = product.reload.updated_at+ second_product_old_updated_at = second_product.reload.updated_at- post :bulk_add_to_categories, params: {- ids: [product.prefixed_id, second_product.prefixed_id],- category_ids: [category.prefixed_id]- }, as: :json+ Timecop.travel(1.second) do+ post :bulk_add_to_categories, params: {+ ids: [product.prefixed_id, second_product.prefixed_id],+ category_ids: [category.prefixed_id]+ }, as: :json+ end- expect(product.reload.updated_at).not_to eq(product_old_updated_at)- expect(second_product.reload.updated_at).not_to eq(second_product_old_updated_at)+ expect(product.reload.updated_at).to be > product_old_updated_at+ expect(second_product.reload.updated_at).to be > second_product_old_updated_atendit 'touches the category' docategory_old_updated_at = category.reload.updated_at- post :bulk_add_to_categories, params: {- ids: [product.prefixed_id],- category_ids: [category.prefixed_id]- }, as: :json+ Timecop.travel(1.second) do+ post :bulk_add_to_categories, params: {+ ids: [product.prefixed_id],+ category_ids: [category.prefixed_id]+ }, as: :json+ end- expect(category.reload.updated_at).not_to eq(category_old_updated_at)+ expect(category.reload.updated_at).to be > category_old_updated_atend# Legacy spec coverage: `bulk_auto_match_taxons` only enqueues jobs for@@ -884,29 +888,33 @@ RSpec.describe Spree::Api::V3::Admin::ProductsController, type: :controller doendit 'touches the products' do- product_old_updated_at = product.updated_at- second_product_old_updated_at = second_product.updated_at+ product_old_updated_at = product.reload.updated_at+ second_product_old_updated_at = second_product.reload.updated_at- post :bulk_remove_from_categories, params: {- ids: [product.prefixed_id, second_product.prefixed_id],- category_ids: [category.prefixed_id]- }, as: :json+ Timecop.travel(1.second) do+ post :bulk_remove_from_categories, params: {+ ids: [product.prefixed_id, second_product.prefixed_id],+ category_ids: [category.prefixed_id]+ }, as: :json+ end- expect(product.reload.updated_at).not_to eq(product_old_updated_at)- expect(second_product.reload.updated_at).not_to eq(second_product_old_updated_at)+ expect(product.reload.updated_at).to be > product_old_updated_at+ expect(second_product.reload.updated_at).to be > second_product_old_updated_atendit 'touches the categories' docategory_old_updated_at = category.reload.updated_atother_category_old_updated_at = other_category.reload.updated_at- post :bulk_remove_from_categories, params: {- ids: [product.prefixed_id],- category_ids: [category.prefixed_id, other_category.prefixed_id]- }, as: :json+ Timecop.travel(1.second) do+ post :bulk_remove_from_categories, params: {+ ids: [product.prefixed_id],+ category_ids: [category.prefixed_id, other_category.prefixed_id]+ }, as: :json+ end- expect(category.reload.updated_at).not_to eq(category_old_updated_at)- expect(other_category.reload.updated_at).not_to eq(other_category_old_updated_at)+ expect(category.reload.updated_at).to be > category_old_updated_at+ expect(other_category.reload.updated_at).to be > other_category_old_updated_atend# Legacy spec: after products are detached, surviving classifications@@ -1152,11 +1160,11 @@ RSpec.describe Spree::Api::V3::Admin::ProductsController, type: :controller doexpect(json_response).to eq('product_count' => 0)end- it 'is a no-op when ids is omitted entirely' do+ it 'rejects an omitted ids param with 422' dodelete :bulk_destroy, params: {}, as: :json- expect(response).to have_http_status(:ok)- expect(json_response['product_count']).to eq(0)+ expect(response).to have_http_status(:unprocessable_content)+ expect(json_response.dig('error', 'code')).to eq('missing_ids')endit 'returns 0 when the only IDs reference unreachable products' do