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.

Keep a seller's catalog inside their own store

Damian Legawiec70570a8d3e34
diff --git a/spree/core/app/models/spree/product.rb b/spree/core/app/models/spree/product.rb
index 51cc005c91..7e2bedd12a 100644
--- a/spree/core/app/models/spree/product.rb
+++ b/spree/core/app/models/spree/product.rb
@@ -143,6 +143,7 @@ module Spree
# Guards every write path (including raw prefixed-id assignment) against
# linking another store's profile.
validate :delivery_profile_must_belong_to_store, if: :delivery_profile_id_changed?
+ validate :vendor_must_belong_to_store, if: :vendor_id_changed?
# Every product has at least one variant. `default_variant` is the "face" of
# the product (price display, default add-to-cart, property delegation).
@@ -936,6 +937,16 @@ module Spree
errors.add(:delivery_profile, :invalid)
end
+ # A seller from another store would put one store's catalog inside another
+ # store's vendor — and every vendor-scoped read is rooted in exactly that
+ # association.
+ def vendor_must_belong_to_store
+ return if vendor.nil? || store.nil?
+ return if vendor.store_id == store_id
+
+ errors.add(:vendor, :invalid)
+ end
+
def run_touch_callbacks
run_callbacks(:touch)
end
diff --git a/spree/core/lib/spree/core/engine.rb b/spree/core/lib/spree/core/engine.rb
index 983f7180f4..637eea7bbf 100644
--- a/spree/core/lib/spree/core/engine.rb
+++ b/spree/core/lib/spree/core/engine.rb
@@ -454,6 +454,7 @@ module Spree
Spree::TaxRate,
Spree::Category,
Spree::Variant,
+ Spree::Vendor,
Spree.customer_class
]
diff --git a/spree/core/lib/spree/core/permission_configuration.rb b/spree/core/lib/spree/core/permission_configuration.rb
index 9f7f1d9a73..8783be4d7a 100644
--- a/spree/core/lib/spree/core/permission_configuration.rb
+++ b/spree/core/lib/spree/core/permission_configuration.rb
@@ -337,9 +337,13 @@ module Spree
[Spree.admin_user_class, Spree::Invitation, Spree::Role, Spree::RoleUser]
})
- # A seller's own record: profile, branding, addresses, onboarding. Only
- # ever granted on the vendor surface — the store's staff manage vendors
- # through the marketplace endpoints, which the `vendors` resource covers.
+ # A seller's own record: profile, branding, addresses, onboarding.
+ #
+ # Grantable to staff as well, like every resource — the operator runs the
+ # marketplace and manages its sellers. What keeps a seller to their own
+ # record is not this key but the branch they hold it on: the vendor
+ # endpoints scope-fetch through `current_vendor`, so the key answers what
+ # they may do and the scope answers to which vendor.
register_resource(:vendor_profile, group: :access, subjects: -> { [Spree::Vendor] },
audiences: %i[vendor])
diff --git a/spree/core/spec/models/spree/vendor_spec.rb b/spree/core/spec/models/spree/vendor_spec.rb
index 6dd686f3cd..b6a7838beb 100644
--- a/spree/core/spec/models/spree/vendor_spec.rb
+++ b/spree/core/spec/models/spree/vendor_spec.rb
@@ -122,6 +122,18 @@ describe Spree::Vendor do
end
describe 'products' do
+ # Every vendor-scoped read is rooted in this association, so a seller from
+ # another store would put one store's catalog inside another store's vendor.
+ it 'refuses a seller from another store' do
+ foreign = create(:vendor, :approved, store: create(:store))
+
+ expect(build(:product, store: store, vendor: foreign)).not_to be_valid
+ end
+
+ it 'accepts a seller from the same store' do
+ expect(build(:product, store: store, vendor: create(:vendor, :approved, store: store))).to be_valid
+ end
+
it 'keeps them when the vendor is destroyed' do
vendor = create(:vendor, :approved)
product = create(:product, store: store, vendor: vendor)
@@ -149,6 +161,14 @@ describe Spree::Vendor do
end
end
+ describe 'custom fields' do
+ # Including the concern is not enough — a resource the registry does not
+ # list has no way to define fields for it.
+ it 'is registered as a custom-field resource' do
+ expect(Spree.custom_fields.enabled_resources).to include(described_class)
+ end
+ end
+
describe 'settlement configuration' do
it 'defaults to the vendor remitting its own tax' do
expect(create(:vendor).tax_remittance).to eq('vendor')