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
Resolve polymorphic endpoint scopes through the catalog
Damian Legawiec0e47ec307ff8
diff --git a/spree/api/app/controllers/concerns/spree/api/v3/scoped_authorization.rb b/spree/api/app/controllers/concerns/spree/api/v3/scoped_authorization.rbindex 8571959870..6e95b863c5 100644--- a/spree/api/app/controllers/concerns/spree/api/v3/scoped_authorization.rb+++ b/spree/api/app/controllers/concerns/spree/api/v3/scoped_authorization.rb@@ -121,6 +121,10 @@ module Spreereturn unless ability.respond_to?(:permission_keys)required = "#{action_kind}_#{resource}"+ # Scope names outside the catalog (the exports/imports `:all`+ # fallback, host-registered types) defer to CanCanCan's `authorize!`+ # — the caller stays gated per subject, just not by key.+ return unless Spree.permissions.key?(required)return if ability.permission_keys.include?(required)Rails.logger.info dodiff --git a/spree/api/app/controllers/spree/api/v3/admin/translations_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/translations_controller.rbindex 4252ce8feb..bd36777687 100644--- a/spree/api/app/controllers/spree/api/v3/admin/translations_controller.rb+++ b/spree/api/app/controllers/spree/api/v3/admin/translations_controller.rb@@ -50,9 +50,15 @@ module Spreeend# Per-parent scope check: reading a product's translations needs- # `read_products`, a category's `read_categories`, etc.+ # `read_products`, a category's `read_categories`, etc. The parent+ # class resolves to its owning catalog resource (an option type's+ # translations ride the `products` permission), falling back to the+ # pluralized route segment for classes outside the catalog.def scoped_resource_name- parent_lookup&.segment&.pluralize&.to_sym+ return unless parent_lookup++ Spree.permissions.resource_for_subject(parent_lookup.klass)&.name ||+ parent_lookup.segment.pluralize.to_symend# Translatable by class hierarchy, not strict equality, so a subclassdiff --git a/spree/api/spec/controllers/spree/api/v3/admin/orders_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/orders_controller_spec.rbindex 6a4ab63ac9..cab518bb3f 100644--- a/spree/api/spec/controllers/spree/api/v3/admin/orders_controller_spec.rb+++ b/spree/api/spec/controllers/spree/api/v3/admin/orders_controller_spec.rb@@ -564,9 +564,13 @@ RSpec.describe Spree::Api::V3::Admin::OrdersController, type: :controller docontext 'with completed order' dolet!(:order) { create(:completed_order_with_totals, store: store) }- it 'returns 403 (CanCan denies destroy on completed orders)' do+ # Record-state eligibility lives on the model (Axis A/B split), so the+ # caller is authorized but the resource's state blocks the operation:+ # 422, matching every other `can_be_deleted?` guard.+ it 'returns 422 (completed orders cannot be deleted)' dosubject- expect(response).to have_http_status(:forbidden)+ expect(response).to have_http_status(:unprocessable_content)+ expect(Spree::Order.find_by(id: order.id)).to be_presentendendenddiff --git a/spree/api/spec/controllers/spree/api/v3/admin/translations_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/translations_controller_spec.rbindex 25a9a6539d..a064dc9eaa 100644--- a/spree/api/spec/controllers/spree/api/v3/admin/translations_controller_spec.rb+++ b/spree/api/spec/controllers/spree/api/v3/admin/translations_controller_spec.rb@@ -37,6 +37,48 @@ RSpec.describe Spree::Api::V3::Admin::TranslationsController, type: :controllerendend+ # The per-parent scope resolves through the catalog: an option type's+ # translations ride the `products` permission, not a literal+ # `option_types` scope that no key or role could ever hold.+ describe 'key gate over polymorphic parents' do+ let!(:option_type) { create(:option_type) }++ context 'as a staffer holding read_products' do+ let(:staffer) do+ create(:admin_user, :without_admin_role).tap do |user|+ user.role_users.create!(role: create(:role, name: 'catalog_viewer', permissions: %w[read_products]), resource: store)+ end+ end+ let(:headers) do+ { 'Authorization' => "Bearer #{Spree::Api::V3::TestingSupport.generate_jwt(staffer, audience: Spree::Api::V3::JwtAuthentication::JWT_AUDIENCE_ADMIN)}" }+ end++ it 'reads an option type translation matrix' do+ get :index, params: { option_type_id: option_type.prefixed_id }, as: :json++ expect(response).to have_http_status(:ok)+ end+ end++ context 'as a staffer without a products permission' do+ let(:staffer) do+ create(:admin_user, :without_admin_role).tap do |user|+ user.role_users.create!(role: create(:role, name: 'orders_only', permissions: %w[read_orders]), resource: store)+ end+ end+ let(:headers) do+ { 'Authorization' => "Bearer #{Spree::Api::V3::TestingSupport.generate_jwt(staffer, audience: Spree::Api::V3::JwtAuthentication::JWT_AUDIENCE_ADMIN)}" }+ end++ it 'is denied with the products permission named' do+ get :index, params: { option_type_id: option_type.prefixed_id }, as: :json++ expect(response).to have_http_status(:forbidden)+ expect(json_response.dig('error', 'details', 'required_permission')).to eq('read_products')+ end+ end+ end+context 'when the parent has translatable children (option type → values)' dolet!(:option_type) { create(:option_type, name: 'size', presentation: 'Size') }let!(:option_value) { create(:option_value, name: 'small', presentation: 'Small', option_type: option_type) }diff --git a/spree/core/lib/spree/core/permission_configuration.rb b/spree/core/lib/spree/core/permission_configuration.rbindex 8a1d5081ff..061e5a7bfc 100644--- a/spree/core/lib/spree/core/permission_configuration.rb+++ b/spree/core/lib/spree/core/permission_configuration.rb@@ -99,6 +99,21 @@ module Spree@resources[name.to_s.to_sym]end+ # The catalog resource whose subjects cover the given model class —+ # matching by ancestry so subclasses (e.g. a Gateway under PaymentMethod)+ # resolve to their base subject's resource. Used to derive the required+ # permission for polymorphic endpoints (translations, custom fields).+ #+ # @param klass [Class]+ # @return [Resource, nil]+ def resource_for_subject(klass)+ return nil unless klass.is_a?(Class)++ resources.find do |candidate|+ candidate.subjects.any? { |subject| subject.is_a?(Class) && klass <= subject }+ end+ end+# One grantable key with its kind and owning resource — the unit the# `/admin/permissions` discovery endpoint serializes.class Entrydiff --git a/spree/core/spec/lib/spree/core/permission_configuration_spec.rb b/spree/core/spec/lib/spree/core/permission_configuration_spec.rbindex 41736e6482..b8ea1ae941 100644--- a/spree/core/spec/lib/spree/core/permission_configuration_spec.rb+++ b/spree/core/spec/lib/spree/core/permission_configuration_spec.rb@@ -131,6 +131,22 @@ RSpec.describe Spree::PermissionConfiguration doendend+ describe '#resource_for_subject' do+ it 'resolves a model class to its owning resource' do+ expect(configuration.resource_for_subject(Spree::Order).name).to eq(:orders)+ expect(configuration.resource_for_subject(Spree::OptionType).name).to eq(:products)+ end++ it 'matches by ancestry so subclasses resolve to the base subject' do+ expect(configuration.resource_for_subject(Spree::Gateway).name).to eq(:settings)+ end++ it 'returns nil for classes no resource covers' do+ expect(configuration.resource_for_subject(Spree::Country)).to be_nil+ expect(configuration.resource_for_subject('not a class')).to be_nil+ end+ end+describe '#reset!' doit 'restores the default catalog' doconfiguration.register_resource(:reviews, group: :catalog, subjects: [Spree::Product])