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.

Fix: API v3 uses the configured ability class (#14693)

Damian Legawiec44bce8f9f789
diff --git a/docs/developer/customization/permissions.mdx b/docs/developer/customization/permissions.mdx
index e86d827c19..93dbe79f72 100644
--- a/docs/developer/customization/permissions.mdx
+++ b/docs/developer/customization/permissions.mdx
@@ -70,4 +70,6 @@ The catalog is deliberately flat: it answers "may this role touch this kind of r
Every Admin API controller declares its resource (`scoped_resource :orders`). Each request checks the principal's keys — a staff member's role permissions on the current store, or a secret key's scopes — against `read_<resource>` or `write_<resource>` for the action. A missing key produces a 403 naming it — `details.required_permission` for staff, `details.required_scope` for a secret key. Behind that gate, keys compile to CanCanCan rules for record-level concerns, and `GET /api/v3/admin/me` returns both the rule dump the dashboard mirrors and `permission_keys`, the flat key list.
+Those rules come from `Spree::Ability`. When an application needs CanCanCan rules the catalog cannot express, replace the class with `Spree::Dependencies.ability_class = 'MyApp::Ability'` — subclass `Spree::Ability`, keep its `initialize(user, options = {})` signature (`options[:store]`, and `options[:resource]` for a seller panel), and add `can` / `cannot` rules after `super`. The Admin and Seller APIs build every staff ability from it; secret API keys use `Spree::ApiKeyAbility` and never consult it.
+
Storefront customers are not part of this system — customer authorization is ownership, enforced by the Store API's scoped lookups, with nothing to configure. The one swappable piece is `Spree::Storefront::AccessPolicy` (via `Spree::Dependencies.storefront_access_policy_class`): a generic `readable?` / `writable?` / `scope` protocol that defaults to "the caller owns the record", with carts and orders adding guest-token access. Replace it only when access must widen beyond the owner, such as company accounts sharing purchases or wishlists.
diff --git a/spree/api/app/controllers/spree/api/v3/base_controller.rb b/spree/api/app/controllers/spree/api/v3/base_controller.rb
index 5ff769ed23..ab0bf516c3 100644
--- a/spree/api/app/controllers/spree/api/v3/base_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/base_controller.rb
@@ -111,10 +111,11 @@ module Spree
try_spree_current_user
end
- # CanCanCan ability
+ # CanCanCan ability, built from the swappable
+ # `Spree::Dependencies.ability_class` (Spree::Ability by default).
# @return [Spree::Ability]
def current_ability
- @current_ability ||= Spree::Ability.new(current_user, ability_options)
+ @current_ability ||= Spree.ability_class.new(current_user, ability_options)
end
# Options passed to the CanCanCan ability
diff --git a/spree/api/app/controllers/spree/api/v3/seller/me_controller.rb b/spree/api/app/controllers/spree/api/v3/seller/me_controller.rb
index c2a320fda6..a30a5e0674 100644
--- a/spree/api/app/controllers/spree/api/v3/seller/me_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/seller/me_controller.rb
@@ -44,7 +44,9 @@ module Spree
# Empty until a seller is named: capability is per seller, so there is
# no meaningful answer spanning all of them. A bare ability (no
# resource) serializes to nothing rather than raising, so the panel
- # can call `/me` before choosing a seller.
+ # can call `/me` before choosing a seller. Deliberately the core
+ # class, not `Spree.ability_class`: a custom ability may add rules
+ # after `super`, and those must not leak into the no-seller answer.
def seller_ability
return Spree::Ability.new(nil) if current_seller.nil?
diff --git a/spree/api/spec/controllers/concerns/spree/api/v3/admin_authentication_spec.rb b/spree/api/spec/controllers/concerns/spree/api/v3/admin_authentication_spec.rb
index 4f588e547d..7c3d88a5dd 100644
--- a/spree/api/spec/controllers/concerns/spree/api/v3/admin_authentication_spec.rb
+++ b/spree/api/spec/controllers/concerns/spree/api/v3/admin_authentication_spec.rb
@@ -88,6 +88,35 @@ RSpec.describe Spree::Api::V3::Admin::TaxCategoriesController, type: :controller
end
end
+ # The upgrade guide's answer to record-level rules the catalog cannot express
+ # is replacing the ability class — so a JWT admin must be authorized by it.
+ describe 'custom ability class via Spree::Dependencies' do
+ let(:custom_ability_class) do
+ Class.new(Spree::Ability) do
+ def initialize(user, options = {})
+ super
+ cannot :manage, Spree::TaxCategory
+ end
+ end
+ end
+
+ before do
+ stub_const('MyApp::Ability', custom_ability_class)
+ Spree::Dependencies.ability_class = 'MyApp::Ability'
+ request.headers['Authorization'] = "Bearer #{admin_jwt_token}"
+ end
+
+ after { Spree::Dependencies.ability_class = 'Spree::Ability' }
+
+ it 'builds the JWT admin ability from the configured class and enforces its rules' do
+ get :show, params: { id: tax_category.prefixed_id }, as: :json
+
+ expect(controller.send(:current_ability)).to be_a(MyApp::Ability)
+ expect(controller.send(:current_ability).store).to eq(store)
+ expect(response).to have_http_status(:not_found)
+ end
+ end
+
describe 'touch throttling for secret keys' do
before { request.headers['X-Spree-Api-Key'] = secret_api_key.plaintext_token }