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.

Ability to pass additional parameters to ability class such as curren… (#12645)

Damian Legawiec38dd91ccb3ad
diff --git a/api/app/controllers/spree/api/v2/base_controller.rb b/api/app/controllers/spree/api/v2/base_controller.rb
index a910eb6525..6b59062114 100644
--- a/api/app/controllers/spree/api/v2/base_controller.rb
+++ b/api/app/controllers/spree/api/v2/base_controller.rb
@@ -96,7 +96,12 @@ module Spree
# Needs to be overridden so that we use Spree's Ability rather than anyone else's.
def current_ability
- @current_ability ||= Spree::Dependencies.ability_class.constantize.new(spree_current_user)
+ @current_ability ||= Spree::Dependencies.ability_class.constantize.new(spree_current_user, ability_options)
+ end
+
+ # this method can be extended in extensions or developer applications
+ def ability_options
+ { store: current_store }
end
def request_includes
diff --git a/core/app/models/spree/ability.rb b/core/app/models/spree/ability.rb
index 4f26ede840..3f4cf035de 100644
--- a/core/app/models/spree/ability.rb
+++ b/core/app/models/spree/ability.rb
@@ -23,15 +23,15 @@ module Spree
abilities.delete(ability)
end
- def initialize(user)
+ def initialize(user, options = {})
alias_cancan_delete_action
user ||= Spree.user_class.new
if user.persisted? && user.try(:spree_admin?)
- apply_admin_permissions(user)
+ apply_admin_permissions(user, options)
else
- apply_user_permissions(user)
+ apply_user_permissions(user, options)
end
# Include any abilities registered by extensions, etc.
@@ -56,7 +56,7 @@ module Spree
alias_action :create, :update, :destroy, to: :modify
end
- def apply_admin_permissions(user)
+ def apply_admin_permissions(user, options)
can :manage, :all
cannot :cancel, Spree::Order
can :cancel, Spree::Order, &:allow_cancel?
@@ -64,7 +64,7 @@ module Spree
cannot [:edit, :update], Spree::ReimbursementType, mutable: false
end
- def apply_user_permissions(user)
+ def apply_user_permissions(user, options)
can :read, ::Spree::Country
can :read, ::Spree::OptionType
can :read, ::Spree::OptionValue
diff --git a/core/app/models/spree/export.rb b/core/app/models/spree/export.rb
index 7340286f15..541d127db1 100644
--- a/core/app/models/spree/export.rb
+++ b/core/app/models/spree/export.rb
@@ -124,7 +124,7 @@ module Spree
end
def current_ability
- @current_ability ||= Spree::Dependencies.ability_class.constantize.new(user)
+ @current_ability ||= Spree::Dependencies.ability_class.constantize.new(user, { store: store })
end
# eg. Spree::Exports::Products => products-store-my-store-code-20241030133348.csv
diff --git a/core/lib/spree/core/controller_helpers/auth.rb b/core/lib/spree/core/controller_helpers/auth.rb
index 1050303c61..2ac156f17e 100644
--- a/core/lib/spree/core/controller_helpers/auth.rb
+++ b/core/lib/spree/core/controller_helpers/auth.rb
@@ -17,7 +17,7 @@ module Spree
# Needs to be overridden so that we use Spree's Ability rather than anyone else's.
def current_ability
- @current_ability ||= Spree::Dependencies.ability_class.constantize.new(try_spree_current_user)
+ @current_ability ||= Spree::Dependencies.ability_class.constantize.new(try_spree_current_user, { store: current_store })
end
def redirect_back_or_default(default)
diff --git a/core/spec/lib/spree/core/controller_helpers/auth_spec.rb b/core/spec/lib/spree/core/controller_helpers/auth_spec.rb
index 909574de29..e7427289df 100644
--- a/core/spec/lib/spree/core/controller_helpers/auth_spec.rb
+++ b/core/spec/lib/spree/core/controller_helpers/auth_spec.rb
@@ -12,6 +12,12 @@ describe Spree::Core::ControllerHelpers::Auth, type: :controller do
controller(FakesController) {}
include Spree::TestingSupport::UrlHelpers
+ let(:store) { @default_store }
+
+ before do
+ allow(controller).to receive(:current_store).and_return(store)
+ end
+
describe '#current_ability' do
it 'returns Spree::Ability instance' do
expect(controller.current_ability.class).to eq Spree::Ability