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
Remove deprecated Ability extension hooks and legacy permissions (#14077)
Damian Legawiece021f4186dea
diff --git a/spree/core/app/models/spree/ability.rb b/spree/core/app/models/spree/ability.rbindex f436e9889e..c2bcd41741 100644--- a/spree/core/app/models/spree/ability.rb+++ b/spree/core/app/models/spree/ability.rb@@ -1,8 +1,6 @@-# Implementation class for Cancan gem. Instead of overriding this class, consider adding new permissions-# using the special +register_ability+ method which allows extensions to add their own abilities.-#-# The preferred way to add permissions is now through permission sets. See Spree::PermissionSets::Base-# for more details on creating custom permission sets.+# Implementation class for Cancan gem. Permissions are configured through+# permission sets — see Spree::PermissionSets::Base for details on creating+# custom ones.## @example Configuring role permissions# Spree.permissions.assign(:customer_service, [@@ -17,37 +15,12 @@ module Spreeclass Abilityinclude CanCan::Ability- class_attribute :abilities- self.abilities = Set.new-# @return [Object] the current userattr_reader :user# @return [Spree::Store, nil] the current storeattr_reader :store- # Allows us to go beyond the standard cancan initialize method which makes it difficult for engines to- # modify the default +Ability+ of an application. The +ability+ argument must be a class that includes- # the +CanCan::Ability+ module. The registered ability should behave properly as a stand-alone class- # and therefore should be easy to test in isolation.- # @deprecated Use Spree::PermissionSets instead. Will be removed in Spree 5.5.- def self.register_ability(ability)- Spree::Deprecation.warn(- 'Spree::Ability.register_ability is deprecated and will be removed in Spree 5.5. ' \- 'Please use Spree::PermissionSets instead. See Spree::PermissionSets::Base for details.'- )- abilities.add(ability)- end-- # @deprecated Use Spree::PermissionSets instead. Will be removed in Spree 5.5.- def self.remove_ability(ability)- Spree::Deprecation.warn(- 'Spree::Ability.remove_ability is deprecated and will be removed in Spree 5.5. ' \- 'Please use Spree::PermissionSets instead. See Spree::PermissionSets::Base for details.'- )- abilities.delete(ability)- end-def initialize(user, options = {})alias_cancan_delete_action@@ -55,24 +28,10 @@ module Spree@store = options[:store] || Spree::Current.storeapply_permissions_from_sets-- # Include any abilities registered by extensions, etc.- # this is legacy behaviour and should be removed in Spree 5.0- Ability.abilities.merge(abilities_to_register).each do |clazz|- Spree::Deprecation.warn("Ability merging is deprecated and will be removed in Spree 5.5. Please use Permission Sets")-- merge clazz.new(@user)- endendprotected- # you can override this method to register your abilities- # this method has to return array of classes- def abilities_to_register- []- end-def alias_cancan_delete_actionalias_action :delete, to: :destroyalias_action :create, :update, :destroy, to: :modify@@ -82,13 +41,7 @@ module Spreedef apply_permissions_from_setsrole_names = determine_role_namespermission_sets = Spree.permissions.permission_sets_for_roles(role_names)-- # If no permission sets are configured for the user's roles, use legacy behavior- if permission_sets.empty?- apply_legacy_permissions- else- activate_permission_sets(permission_sets)- end+ activate_permission_sets(permission_sets)end# Determines the role names for the current user.@@ -121,71 +74,5 @@ module Spreepermission_set.activate!endend-- # Legacy permission application for backward compatibility.- # This is used when no permission sets are configured for the user's roles.- def apply_legacy_permissions- if @user.persisted? && @user.is_a?(Spree.admin_user_class) && @user.try(:spree_admin?, @store)- apply_admin_permissions(@user, { store: @store })- else- apply_user_permissions(@user, { store: @store })- end-- protect_admin_role- end-- def apply_admin_permissions(_user, _options)- Spree::Deprecation.warn("Ability#apply_admin_permissions is deprecated and will be removed in Spree 5.5. Please use Permission Sets")- can :manage, :all- cannot :cancel, Spree::Order- can :cancel, Spree::Order, &:allow_cancel?- cannot :destroy, Spree::Order- can :destroy, Spree::Order, &:can_be_deleted?- cannot [:edit, :update], Spree::RefundReason, mutable: false- cannot [:edit, :update], Spree::ReimbursementType, mutable: false- end-- def apply_user_permissions(user, _options)- Spree::Deprecation.warn("Ability#apply_user_permissions is deprecated and will be removed in Spree 5.5. Please use Permission Sets")-- can :read, ::Spree::Country- can :read, ::Spree::OptionType- can :read, ::Spree::OptionValue- can :create, ::Spree::Order- can :show, ::Spree::Order do |order, token|- order.user == user || order.token && token == order.token- end- can :update, ::Spree::Order do |order, token|- !order.completed? && (order.user == user || order.token && token == order.token)- end- # Address management - only for persisted users with matching user_id- can :manage, ::Spree::Address, user_id: user.id if user.persisted?- can [:read, :destroy], ::Spree::CreditCard, user_id: user.id- can :read, ::Spree::Product- can :create, ::Spree.user_class- can [:show, :update, :destroy], ::Spree.user_class, id: user.id- can :read, ::Spree::State- can :read, ::Spree::Store- can :read, ::Spree::Taxon- can :read, ::Spree::Taxonomy- can :read, ::Spree::Variant- can :read, ::Spree::Zone- can :manage, ::Spree::Wishlist, user_id: user.id- can :show, ::Spree::Wishlist do |wishlist|- wishlist.user == user || wishlist.is_private == false- end- can [:create, :update, :destroy], ::Spree::WishedItem do |wished_item|- wished_item.wishlist.user == user- end- can :accept, Spree::Invitation, invitee_id: [user.id, nil], invitee_type: user.class.name, status: 'pending'- can :show, ::Spree::DigitalLink do |digital_link, token|- digital_link.token == token- end- can :read, ::Spree::Policy- end-- def protect_admin_role- cannot [:update, :destroy], ::Spree::Role, name: ['admin']- endendenddiff --git a/spree/core/lib/spree/testing_support/bar_ability.rb b/spree/core/lib/spree/testing_support/bar_ability.rbdeleted file mode 100644index ad23f1e436..0000000000--- a/spree/core/lib/spree/testing_support/bar_ability.rb+++ /dev/null@@ -1,14 +0,0 @@-# Fake ability for testing administration-class BarAbility- include CanCan::Ability-- def initialize(user)- user ||= Spree::User.new- if user.has_spree_role? 'bar'- # allow dispatch to :admin, :index, and :show on Spree::Order- can [:admin, :index, :show], Spree::Order- # allow dispatch to :index, :show, :create and :update shipments on the admin- can [:admin, :manage], Spree::Shipment- end- end-enddiff --git a/spree/core/spec/models/spree/ability_spec.rb b/spree/core/spec/models/spree/ability_spec.rbindex 6b6828af07..41c29df1f3 100644--- a/spree/core/spec/models/spree/ability_spec.rb+++ b/spree/core/spec/models/spree/ability_spec.rb@@ -1,23 +1,6 @@require 'spec_helper'require 'cancan/matchers'require 'spree/testing_support/ability_helpers'-require 'spree/testing_support/bar_ability'--# Fake ability for testing registration of additional abilities-class FooAbility- include CanCan::Ability-- ORDER_NUMBER = 'N210868'-- def initialize(_user)- # allow anyone to perform index on Order- can :index, Spree::Order- # allow anyone to update an Order with specific order number- can :update, Spree::Order do |order|- order.number = ORDER_NUMBER- end- end-enddescribe Spree::Ability, type: :model dolet(:store) { @default_store }@@ -31,34 +14,6 @@ describe Spree::Ability, type: :model doSpree.permissions.assign(:admin, [Spree::PermissionSets::SuperUser])end- after do- Spree::Ability.abilities = Set.new- end-- context 'register_ability' do- it 'adds the ability to the list of abilties' do- Spree::Ability.register_ability(FooAbility)- expect(Spree::Ability.new(user).abilities).not_to be_empty- end-- it 'applies the registered abilities permissions' do- Spree::Ability.register_ability(FooAbility)- expect(Spree::Ability.new(user).can?(:update, create(:order, number: FooAbility::ORDER_NUMBER))).to be true- end- end-- context '#abilities_to_register' do- it 'adds the ability to the list of abilities' do- allow_any_instance_of(Spree::Ability).to receive(:abilities_to_register).and_return([FooAbility])- expect(Spree::Ability.new(user).abilities).to include FooAbility- end-- it 'applies the registered abilities permissions' do- allow_any_instance_of(Spree::Ability).to receive(:abilities_to_register).and_return([FooAbility])- expect(Spree::Ability.new(user).can?(:update, create(:order, number: FooAbility::ORDER_NUMBER))).to be true- end- end-context 'for general resource' dolet(:resource) { Object.new }@@ -123,41 +78,6 @@ describe Spree::Ability, type: :model doendend- context 'with fakedispatch user' do- it 'is able to admin on the order and shipment pages' do- allow(user).to receive(:has_spree_role?).with('admin').and_return(false)- allow(user).to receive(:has_spree_role?).with('bar').and_return(true)-- Spree::Ability.register_ability(BarAbility)-- expect(ability).not_to be_able_to :admin, resource-- expect(ability).to be_able_to :admin, resource_order- expect(ability).to be_able_to :index, resource_order- expect(ability).not_to be_able_to :update, resource_order- # ability.should_not be_able_to :create, resource_order # Fails-- expect(ability).to be_able_to :admin, resource_shipment- expect(ability).to be_able_to :index, resource_shipment- expect(ability).to be_able_to :create, resource_shipment-- expect(ability).not_to be_able_to :admin, resource_product- expect(ability).not_to be_able_to :update, resource_product- # ability.should_not be_able_to :show, resource_product # Fails-- expect(ability).not_to be_able_to :admin, resource_user- expect(ability).not_to be_able_to :update, resource_user- expect(ability).to be_able_to :update, user- # ability.should_not be_able_to :create, resource_user # Fails- # It can create new users if is has access to the :admin, User!!- expect(ability).to be_able_to :create, user-- # TODO: change the Ability class so only users and customers get the extra permissions?-- Spree::Ability.remove_ability(BarAbility)- end- end-context 'with customer' doit 'is not able to admin' doexpect(ability).not_to be_able_to :admin, resourcediff --git a/spree/core/spec/models/spree/permission_sets/ability_integration_spec.rb b/spree/core/spec/models/spree/permission_sets/ability_integration_spec.rbindex 8526fa7668..978f87bf39 100644--- a/spree/core/spec/models/spree/permission_sets/ability_integration_spec.rb+++ b/spree/core/spec/models/spree/permission_sets/ability_integration_spec.rb@@ -80,12 +80,11 @@ RSpec.describe 'Permission Sets Integration with Ability', type: :model do# Don't configure any permission sets for this roleend- it 'falls back to legacy behavior' do+ it 'grants no permissions when no sets are configured for the role' doability = Spree::Ability.new(user)- # Legacy behavior for regular users- expect(ability.can?(:read, Spree::Product)).to be true- expect(ability.can?(:create, Spree::Order)).to be true+ expect(ability.can?(:read, Spree::Product)).to be false+ expect(ability.can?(:create, Spree::Order)).to be falseexpect(ability.can?(:manage, Spree::Product)).to be falseendend