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
Store-scope product tag autocomplete via tenant (F8)
Damian Legawiec35af5545382c
diff --git a/spree/api/app/controllers/spree/api/v3/admin/tags_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/tags_controller.rbindex 0a2954c34b..d18ce9616d 100644--- a/spree/api/app/controllers/spree/api/v3/admin/tags_controller.rb+++ b/spree/api/app/controllers/spree/api/v3/admin/tags_controller.rb@@ -60,15 +60,24 @@ module Spreefalseend- # Tagging filter. `Spree::Order` carries a `tenant` (store_id) column- # via `acts_as_taggable_tenant`, so its tag vocabulary is bounded to- # the current store; other taggables fall back to the type filter.+ # Tagging filter. Store-owned taggables (`Spree::Product`,+ # `Spree::Order`) tenant their taggings by `store_id` via+ # `acts_as_taggable_tenant`, so their vocabulary is bounded to the+ # current store with a single indexed `tenant` filter. Global+ # taggables like the customer/user type carry no store, so their tags+ # are not store-scoped here.def taggings_conditionsconditions = { taggable_type: taggable_type, context: 'tags' }- conditions[:tenant] = current_store.id.to_s if taggable_type == 'Spree::Order'+ conditions[:tenant] = current_store.id.to_s if store_tenanted_taggable?conditionsend+ # True for taggables whose taggings carry a store `tenant`+ # (`acts_as_taggable_tenant :store_id`), i.e. store-owned records.+ def store_tenanted_taggable?+ %w[Spree::Product Spree::Order].include?(taggable_type)+ end+# Per-type scope check for API-key principals: a key listing product# tags needs `read_products`, order tags `read_orders`, etc. JWT# admins are gated by store membership + CanCanCan, not scopes.diff --git a/spree/api/spec/controllers/spree/api/v3/admin/tags_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/tags_controller_spec.rbindex 93ebbafec8..450d8425e7 100644--- a/spree/api/spec/controllers/spree/api/v3/admin/tags_controller_spec.rb+++ b/spree/api/spec/controllers/spree/api/v3/admin/tags_controller_spec.rb@@ -43,6 +43,22 @@ RSpec.describe Spree::Api::V3::Admin::TagsController, type: :controller doendend+ context 'product tag vocabulary is store-scoped' do+ let(:headers) { bearer_headers }++ let(:other_store) { create(:store) }+ let!(:own_product) { create(:product, store: store, tag_list: ['bestseller']) }+ let!(:foreign_product) { create(:product, store: other_store, tag_list: ['clearance']) }++ it 'excludes another store\'s product tags' do+ get :index, params: { taggable_type: 'Spree::Product' }, as: :json++ names = json_response['data'].map { |t| t['name'] }+ expect(names).to include('bestseller')+ expect(names).not_to include('clearance')+ end+ end+describe 'API-key scope enforcement' dolet(:headers) { { 'x-spree-api-key' => api_key.plaintext_token } }let!(:tagged_user) { create(:user, tag_list: ['vip']) }diff --git a/spree/core/app/models/spree/product.rb b/spree/core/app/models/spree/product.rbindex 390dea8aeb..96be729ec1 100644--- a/spree/core/app/models/spree/product.rb+++ b/spree/core/app/models/spree/product.rb@@ -24,6 +24,7 @@ module Spreeacts_as_paranoidacts_as_taggable_on :tags, :labels+ acts_as_taggable_tenant :store_idnormalizes :name, with: ->(value) { value&.to_s&.squish&.presence }include Spree::ProductScopesdiff --git a/spree/core/lib/tasks/product_tag_tenants.rake b/spree/core/lib/tasks/product_tag_tenants.rakenew file mode 100644index 0000000000..1202e52770--- /dev/null+++ b/spree/core/lib/tasks/product_tag_tenants.rake@@ -0,0 +1,26 @@+namespace :spree do+ namespace :upgrade do+ desc <<~DESC+ Backfills the +tenant+ column on existing +Spree::Product+ taggings from+ each product's +store_id+, so product tag vocabulary is bounded to the+ owning store (matching +Spree::Order+, which already tenants its taggings).++ Run once after upgrading to the release that adds+ +acts_as_taggable_tenant :store_id+ to +Spree::Product+. New product+ taggings get their tenant automatically; only rows created before the+ upgrade need this. Idempotent — re-running only touches rows still+ missing a tenant.+ DESC+ task backfill_product_tag_tenants: :environment do+ taggings = ActsAsTaggableOn::Tagging.arel_table.name+ products = Spree::Product.table_name++ updated = ActsAsTaggableOn::Tagging.+ where(taggable_type: 'Spree::Product', tenant: nil).+ joins("INNER JOIN #{products} ON #{products}.id = #{taggings}.taggable_id").+ update_all("tenant = #{products}.store_id")++ puts " Backfilled tenant on #{updated} product tagging(s)."+ end+ end+end