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.

Allow admin users to delete all user roles (#13182)

Jawaria Warisc61d54ff6ec3
diff --git a/core/app/models/concerns/spree/user_methods.rb b/core/app/models/concerns/spree/user_methods.rb
index ef35ab3e1d..8080772c7f 100644
--- a/core/app/models/concerns/spree/user_methods.rb
+++ b/core/app/models/concerns/spree/user_methods.rb
@@ -137,7 +137,7 @@ module Spree
# Returns true if the user can be deleted
# @return [Boolean]
def can_be_deleted?
- if has_spree_role?(Spree::Role::ADMIN_ROLE)
+ if role_users.where(resource: Spree::Store.current).exists?
Spree::Store.current.users.where.not(id: id).exists?
else
orders.complete.none?
diff --git a/core/spec/models/spree/concerns/user_methods_spec.rb b/core/spec/models/spree/concerns/user_methods_spec.rb
index ac5ecd684e..4964ab6d4d 100644
--- a/core/spec/models/spree/concerns/user_methods_spec.rb
+++ b/core/spec/models/spree/concerns/user_methods_spec.rb
@@ -150,4 +150,39 @@ describe Spree::UserMethods do
expect(Spree.user_class.multi_search('greg smith')).to eq([])
end
end
+
+ describe '#can_be_deleted?' do
+ subject { test_user.can_be_deleted? }
+
+ context 'when user has a role on current store' do
+ let!(:role) { create(:role, name: 'test') }
+
+ it 'returns true if another user also has a role on the store' do
+ test_user.add_role(role.name, current_store)
+ other_user = create(:user)
+ other_user.add_role(role.name, current_store)
+
+ expect(subject).to be(true)
+ end
+
+ it 'returns false if user is the last with a role on the store' do
+ current_store.role_users.destroy_all
+ test_user.add_role(role.name, current_store)
+
+ expect(subject).to be(false)
+ end
+ end
+
+ context 'when user has no role on current store' do
+ it 'returns true if user has no completed orders' do
+ expect(subject).to be(true)
+ end
+
+ it 'returns false if user has completed orders' do
+ create(:order, user: test_user, completed_at: Time.current)
+
+ expect(subject).to be(false)
+ end
+ end
+ end
end