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
Fix: Kept admin-only custom fields out of storefront search, filters and sorts
Damian Legawiec96f24f171644
diff --git a/spree/core/app/models/spree/product.rb b/spree/core/app/models/spree/product.rbindex fd2f3b444f..f0528d3c2a 100644--- a/spree/core/app/models/spree/product.rb+++ b/spree/core/app/models/spree/product.rb@@ -277,7 +277,9 @@ module Spree# the search. Definitions are store-owned, so the caller says which store# is searching; `Spree::Current.store` is the request's answer and a# background caller (an export, say) passes its own.- scope :search, ->(query, store = Spree::Current.store) {+ # @param audience [Symbol, nil] `:store` keeps a shopper's search off the+ # custom fields hidden from the storefront; `nil` is the back office+ scope :search, ->(query, store = Spree::Current.store, audience: nil) {next none if query.blank?product_ids = Spree::Variant.search_by_product_name_or_sku(query).pluck(:product_id)@@ -285,6 +287,7 @@ module Spreesearchable_definitions = store.custom_field_definitions.for_resource_type('Spree::Product').searchable+ searchable_definitions = searchable_definitions.storefront_visible if audience == :store# Only pay for the custom_field scan when some definition opts into search;# the leading-wildcard LIKE over spree_custom_fields can't use an index.diff --git a/spree/core/app/models/spree/search_provider/base.rb b/spree/core/app/models/spree/search_provider/base.rbindex d824ab3045..7cb335d0b6 100644--- a/spree/core/app/models/spree/search_provider/base.rb+++ b/spree/core/app/models/spree/search_provider/base.rb@@ -5,7 +5,7 @@ module Spree# attributes. `price` also sorts descending as `-price`.SORT_FIELDS = %w[price best_selling manual].freeze- attr_reader :store+ attr_reader :store, :audience# Whether this provider requires background indexing jobs.# Override in subclasses. Database provider returns false.@@ -13,13 +13,18 @@ module Spreefalseend- def initialize(store)+ # @param store [Spree::Store]+ # @param audience [Symbol, nil] who searches: `:store` (shoppers) limits+ # filters and sorts to what the storefront may see; `nil` is the back+ # office+ def initialize(store, audience: :store)@store = store+ @audience = audienceend# @return [CustomFieldSchema]def custom_field_schema- @custom_field_schema ||= CustomFieldSchema.new(store)+ @custom_field_schema ||= CustomFieldSchema.new(store, audience: audience)end# Deprecated 6.0 name, removed in 6.1.diff --git a/spree/core/app/models/spree/search_provider/database.rb b/spree/core/app/models/spree/search_provider/database.rbindex 2c825df65e..0c82ab6b82 100644--- a/spree/core/app/models/spree/search_provider/database.rb+++ b/spree/core/app/models/spree/search_provider/database.rb@@ -7,14 +7,6 @@ module Spree'best_selling' => :by_best_selling}.freeze- # @param store [Spree::Store]- # @param audience [Symbol, nil] who filters: `:store` (shoppers) limits- # filters to what the storefront may query; `nil` is the back office- def initialize(store, audience: :store)- super(store)- @audience = audience- end-def search_and_filter(scope:, query: nil, filters: {}, sort: nil, page: 1, limit: 25)filters = filters.is_a?(Hash) ? filters.dup : {}option_value_ids = filters.delete('with_option_value_ids') || filters.delete(:with_option_value_ids)@@ -73,14 +65,14 @@ module Spreeprivatedef apply_search_and_filters(scope, query: nil, filters: {})- scope = scope.search(query, store) if query.present?+ scope = scope.search(query, store, audience: audience) if query.present?filters = filters.to_unsafe_h if filters.respond_to?(:to_unsafe_h)scope, filters = scope.with_custom_field_filters(filters, schema: custom_field_schema)ransack_filters = sanitize_filters(filters)if ransack_filters.present?- search = scope.ransack(ransack_filters, auth_object: @audience)+ search = scope.ransack(ransack_filters, auth_object: audience)scope = search.result(distinct: true)enddiff --git a/spree/core/app/services/spree/search_provider/custom_field_schema.rb b/spree/core/app/services/spree/search_provider/custom_field_schema.rbindex 891d617788..1e6f744abf 100644--- a/spree/core/app/services/spree/search_provider/custom_field_schema.rb+++ b/spree/core/app/services/spree/search_provider/custom_field_schema.rb@@ -15,13 +15,16 @@ module SpreeORDERED_FILTER_PREDICATES = (TEXT_FILTER_PREDICATES | NUMBER_FILTER_PREDICATES).sort_by { |predicate| -predicate.length }.freeze- attr_reader :store+ attr_reader :store, :audience# @param store [Spree::Store] the store whose custom-field schema this# describes — definitions are store-owned, so two stores have their own# filter vocabularies.- def initialize(store)+ # @param audience [Symbol, nil] who filters: `:store` (shoppers) sees only+ # storefront-visible definitions; `nil` is the back office+ def initialize(store, audience: nil)@store = store+ @audience = audienceend# @return [Hash{String => Spree::CustomFieldDefinition}]@@ -134,8 +137,13 @@ module Spreeprivate+ # A definition hidden from the storefront stays out of its filter and+ # sort vocabulary too: the serializer withholds the value, and a sort or+ # a range filter on it would hand that value back one comparison at a+ # time.def product_definitionsscope = store.custom_field_definitions.for_resource_type('Spree::Product')+ scope = scope.storefront_visible if audience == :storescope.where(searchable: true).or(scope.where(sortable: true))enddiff --git a/spree/core/spec/models/spree/product_spec.rb b/spree/core/spec/models/spree/product_spec.rbindex 49a0b3b319..098e50142b 100644--- a/spree/core/spec/models/spree/product_spec.rb+++ b/spree/core/spec/models/spree/product_spec.rb@@ -2355,5 +2355,12 @@ describe Spree::Product, type: :model doexpect(described_class.search('hong-ku')).to include(matching)expect(described_class.search('hong-ku')).not_to include(other)end++ it 'skips admin-only custom fields for shoppers' do+ definition.update!(storefront_visible: false)++ expect(described_class.search('hong-ku', store, audience: :store)).not_to include(matching)+ expect(described_class.search('hong-ku', store)).to include(matching)+ endendenddiff --git a/spree/core/spec/models/spree/search_provider/database_spec.rb b/spree/core/spec/models/spree/search_provider/database_spec.rbindex ffc383d699..3b891e8a1f 100644--- a/spree/core/spec/models/spree/search_provider/database_spec.rb+++ b/spree/core/spec/models/spree/search_provider/database_spec.rb@@ -289,6 +289,34 @@ module Spreeendend+ # The storefront never shows an admin-only value, so a shopper must not+ # be able to read it back through search, a sort or a range filter.+ context 'with admin-only custom_fields' do+ let!(:definition) do+ create(:custom_field_definition, :number_field, :admin_only, :searchable, :sortable,+ namespace: 'internal', key: 'margin')+ end++ before do+ product_1.set_custom_field(definition, '40')+ product_2.set_custom_field(definition, '12')+ end++ it 'ignores them for shoppers' do+ expect(provider.search_and_filter(scope: scope, query: '40').products).to be_empty+ expect(provider.search_and_filter(scope: scope, filters: { 'cf_internal_margin_gteq' => '30' }).products).to match_array([product_1, product_2, product_3])+ expect(provider.search_and_filter(scope: scope, sort: '-cf_internal_margin').products.to_a).+ to eq(provider.search_and_filter(scope: scope).products.to_a)+ expect(provider.filters(scope: scope).sort_options.map { |option| option[:id] }).not_to include('cf_internal_margin')+ end++ it 'keeps them for the back office' do+ back_office = described_class.new(store, audience: nil)++ expect(back_office.search_and_filter(scope: scope, filters: { 'cf_internal_margin_gteq' => '30' }).products).to eq([product_1])+ end+ end+context 'with sortable custom_fields' dolet!(:definition) docreate(:custom_field_definition, :short_text_field, :sortable,diff --git a/spree/core/spec/services/spree/search_provider/custom_field_schema_spec.rb b/spree/core/spec/services/spree/search_provider/custom_field_schema_spec.rbindex 890657933c..de28e9eafa 100644--- a/spree/core/spec/services/spree/search_provider/custom_field_schema_spec.rb+++ b/spree/core/spec/services/spree/search_provider/custom_field_schema_spec.rb@@ -18,6 +18,14 @@ RSpec.describe Spree::SearchProvider::CustomFieldSchema doexpect(schema.searchable_attribute_keys).to include('cf_custom_weight')expect(schema.sortable_attribute_keys).to include('cf_custom_weight')end++ it 'keeps admin-only definitions out of the storefront vocabulary' do+ create(:custom_field_definition, :number_field, :admin_only, :sortable, :searchable,+ namespace: 'internal', key: 'margin')++ expect(described_class.new(store, audience: :store).entry_for('cf_internal_margin')).to be_nil+ expect(described_class.new(store).entry_for('cf_internal_margin')).to be_present+ endenddescribe '#entries' dodiff --git a/spree/providers/meilisearch/app/models/spree_meilisearch/search_provider.rb b/spree/providers/meilisearch/app/models/spree_meilisearch/search_provider.rbindex 1a663b3715..273c7104b6 100644--- a/spree/providers/meilisearch/app/models/spree_meilisearch/search_provider.rb+++ b/spree/providers/meilisearch/app/models/spree_meilisearch/search_provider.rb@@ -101,7 +101,7 @@ module SpreeMeilisearchdef reindex(scope = nil)full_reindex = scope.nil?scope ||= store.products- @custom_field_schema = Spree::SearchProvider::CustomFieldSchema.new(store)+ @custom_field_schema = Spree::SearchProvider::CustomFieldSchema.new(store, audience: audience)ensure_index_settings!# On a full reindex, clear first so stale docs (e.g. membership docs for