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.

Restrict collection product curation to manual collections

Łukasz Adamczyk00f2c85b11df
diff --git a/spree/api/app/controllers/spree/api/v3/admin/collections/products_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/collections/products_controller.rb
index 1f72aa4c51..47811b0572 100644
--- a/spree/api/app/controllers/spree/api/v3/admin/collections/products_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/admin/collections/products_controller.rb
@@ -68,8 +68,13 @@ module Spree
end
# Loads the parent collection (runs before the base set_resource).
+ # Scoped to +manual+ so automatic (rule-based) collections — whose
+ # membership is materialized by RegenerateProducts and would drop any
+ # manual add on the next regeneration — can't have their membership
+ # listed or mutated through this endpoint (mirrors the categories API).
def set_parent
- @parent_collection = Spree::Collection.accessible_by(current_ability, :update).
+ @parent_collection = Spree::Collection.manual.
+ accessible_by(current_ability, :update).
for_store(current_store).
find_by_prefix_id!(params[:collection_id])
authorize_parent!(@parent_collection)
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 f0309f2590..a947f4d240 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
@@ -242,8 +242,11 @@ module Spree
def apply_collections(service)
authorize! :update, model_class
+ # Only manual collections accept curation — automatic membership is
+ # rebuilt from rules, so a manual add/remove would be lost on the next
+ # regeneration (mirrors apply_categories, whose association is manual).
collection_ids = decode_ids(params[:collection_ids])
- collections = current_store.collections.
+ collections = current_store.collections.manual.
accessible_by(current_ability, :update).where(id: collection_ids)
service.call(collections: collections, products: bulk_collection)
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/collections/products_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/collections/products_controller_spec.rb
index 6446db2c63..35c2d9a187 100644
--- a/spree/api/spec/controllers/spree/api/v3/admin/collections/products_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/admin/collections/products_controller_spec.rb
@@ -44,6 +44,13 @@ RSpec.describe Spree::Api::V3::Admin::Collections::ProductsController, type: :co
expect(response).to have_http_status(:ok)
expect(json_response['data']).to eq([])
end
+
+ it '404s for an automatic collection (curation is manual-only)' do
+ automatic = create(:automatic_collection, store: store)
+ get :index, params: { collection_id: automatic.prefixed_id }, as: :json
+
+ expect(response).to have_http_status(:not_found)
+ end
end
describe 'POST #create' do
@@ -62,6 +69,13 @@ RSpec.describe Spree::Api::V3::Admin::Collections::ProductsController, type: :co
expect(response).to have_http_status(:not_found)
end
+
+ it '404s for an automatic collection (membership is rule-managed, not curated)' do
+ automatic = create(:automatic_collection, store: store)
+ post :create, params: { collection_id: automatic.prefixed_id, product_id: product.prefixed_id }, as: :json
+
+ expect(response).to have_http_status(:not_found)
+ end
end
describe 'DELETE #destroy' do
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 38031fdb87..db6f57e09b 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
@@ -1324,6 +1324,20 @@ RSpec.describe Spree::Api::V3::Admin::ProductsController, type: :controller do
expect(product.reload.collections).not_to include(foreign_collection)
end
+ it 'silently ignores automatic collections (curation is manual-only)' do
+ automatic = create(:automatic_collection, store: store)
+
+ post :bulk_add_to_collections, params: {
+ ids: [product.prefixed_id],
+ collection_ids: [collection.prefixed_id, automatic.prefixed_id]
+ }, as: :json
+
+ expect(response).to have_http_status(:ok)
+ expect(json_response['collection_count']).to eq(1)
+ expect(product.reload.collections).to include(collection)
+ expect(product.reload.collections).not_to include(automatic)
+ end
+
it 'silently drops products from other stores' do
other_store_product = create(:product, store: create(:store))