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
Fix: stopped staff without the admin role from removing admins
Damian Legawiececeef2d2316a
diff --git a/spree/api/app/controllers/concerns/spree/api/v3/admin/role_grant_guard.rb b/spree/api/app/controllers/concerns/spree/api/v3/admin/role_grant_guard.rbindex b9c5210980..cc081430dc 100644--- a/spree/api/app/controllers/concerns/spree/api/v3/admin/role_grant_guard.rb+++ b/spree/api/app/controllers/concerns/spree/api/v3/admin/role_grant_guard.rb@@ -23,13 +23,13 @@ module Spree# flow leaves it off (its gate is `manage Invitation`).# @return [Boolean] true if the request was rejected (caller should return)def reject_unauthorized_role_grant!(role_ids, require_role_management: false)- return false if role_ids.blank?-# The management gate runs whenever a role mutation is attempted —- # before resolving ids — so passing unknown ids can't slip the- # reconciliation (which would still remove the user's current roles)- # past a caller without role-management authority.+ # before resolving ids, and even for an empty list — so passing+ # unknown ids, or none at all, can't slip the reconciliation (which+ # would still remove the user's current roles) past a caller+ # without role-management authority.return true if require_role_management && reject_without_role_management!+ return false if role_ids.blank?roles = current_store.roles.where(id: role_ids.map(&:to_s)).to_areturn false if roles.empty?@@ -40,6 +40,30 @@ module Spreefalseend+ # Removing a role is bounded like granting it: a caller may only take+ # away authority they could hand out, so a staff member who can+ # manage staff cannot strip the admin role from the store's owners.+ # The store also keeps at least one admin, whoever asks.+ #+ # @param user [Object] whose roles are being removed+ # @param roles [Array<Spree::Role>] the store roles being removed+ # @return [Boolean] true if the request was rejected (caller should return)+ def reject_unauthorized_role_removal!(user, roles)+ return false if roles.empty?++ if roles.any?(&:admin?)+ return deny_role_grant!('You cannot remove the admin role.') unless caller_holds_admin_role?+ return deny_role_grant!('The store must keep at least one admin.') if last_admin?(user)+ end++ return false if caller_holds_admin_role?++ beyond_own = roles.reject { |role| excess_permission_keys(role.permissions).empty? }+ return false if beyond_own.empty?++ deny_role_grant!("You cannot remove roles beyond your own privileges: #{beyond_own.map(&:name).join(', ')}")+ end+# Rejects granting permission keys beyond the caller's own — used by# roles#create/#update on the requested `permissions` array.#@@ -125,6 +149,11 @@ module Spreeendend+ def last_admin?(user)+ admin_roles = current_store.roles.select(&:admin?)+ Spree::RoleUser.where(role: admin_roles).where.not(user: user).none?+ end+def caller_holds_admin_role?return current_api_key.has_scope?('write_all') if scope_limited_principal?diff --git a/spree/api/app/controllers/spree/api/v3/admin/admin_users_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/admin_users_controller.rbindex 3d29cb9142..2a858683c0 100644--- a/spree/api/app/controllers/spree/api/v3/admin/admin_users_controller.rb+++ b/spree/api/app/controllers/spree/api/v3/admin/admin_users_controller.rb@@ -24,6 +24,8 @@ module Spree# the account globally. The user keeps access to any other stores.def destroyauthorize!(:destroy, @resource)+ return if reject_unauthorized_role_removal!(@resource, @resource.spree_roles.for_resource(current_store).to_a)+@resource.role_users.where(role: current_store.roles).destroy_allhead :no_contentend@@ -42,6 +44,7 @@ module Spree# RoleManagement permission set, not by profile-edit rights.role_ids = role_ids_param if params.key?(:role_ids)return if role_ids && reject_unauthorized_role_grant!(role_ids, require_role_management: true)+ return if role_ids && reject_unauthorized_role_removal!(@resource, removed_roles(role_ids))if @resource.update(identity_params)apply_role_ids(role_ids) if role_ids@@ -89,6 +92,12 @@ module Spreeids.map { |id| Spree::PrefixedId.prefixed_id?(id) ? Spree::PrefixedId.decode_prefixed_id(id) : id }.compactend+ # The store roles the user holds that `desired_role_ids` leaves out.+ def removed_roles(desired_role_ids)+ target = desired_role_ids.map(&:to_s)+ @resource.spree_roles.for_resource(current_store).reject { |role| target.include?(role.id.to_s) }+ end+# Reconcile the user's roles on this store to match `desired_role_ids`.# Adds missing assignments and removes extras — no-op for unchanged.def apply_role_ids(desired_role_ids)diff --git a/spree/api/spec/controllers/spree/api/v3/admin/admin_users_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/admin_users_controller_spec.rbindex 263dee0d5a..f0cab81ed5 100644--- a/spree/api/spec/controllers/spree/api/v3/admin/admin_users_controller_spec.rb+++ b/spree/api/spec/controllers/spree/api/v3/admin/admin_users_controller_spec.rb@@ -99,6 +99,33 @@ RSpec.describe Spree::Api::V3::Admin::AdminUsersController, type: :controller doexpect(target.reload.spree_admin?(store)).to be(false)end+ # Removal is bounded like granting: a staff manager cannot strip the+ # owners' admin role and lock them out.+ it 'forbids removing a store owner' do+ owner = create(:admin_user)++ delete :destroy, params: { id: owner.prefixed_id }, as: :json++ expect(response).to have_http_status(:forbidden)+ expect(owner.reload.spree_admin?(store)).to be(true)+ end++ it 'forbids clearing an owner\'s roles' do+ owner = create(:admin_user)++ patch :update, params: { id: owner.prefixed_id, role_ids: [] }, as: :json++ expect(response).to have_http_status(:forbidden)+ expect(owner.reload.spree_admin?(store)).to be(true)+ end++ it 'still removes a member it could have added' do+ delete :destroy, params: { id: target.prefixed_id }, as: :json++ expect(response).to have_http_status(:no_content)+ expect(target.role_users.where(role: store.roles)).not_to exist+ end+it 'forbids assigning a role whose permissions exceed its own' doowner_role = create(:role, name: 'owner', permissions: Spree.permissions.grantable_keys(:store))@@ -118,6 +145,24 @@ RSpec.describe Spree::Api::V3::Admin::AdminUsersController, type: :controller doexpect(response).to have_http_status(:ok)expect(target.reload.spree_admin?(store)).to be(true)end++ it 'refuses removing the last admin' do+ Spree::RoleUser.where(role: admin_role).where.not(user: admin_user).destroy_all++ delete :destroy, params: { id: admin_user.prefixed_id }, as: :json++ expect(response).to have_http_status(:forbidden)+ expect(admin_user.reload.spree_admin?(store)).to be(true)+ end++ it 'removes an admin while another remains' do+ other_admin = create(:admin_user)++ delete :destroy, params: { id: other_admin.prefixed_id }, as: :json++ expect(response).to have_http_status(:no_content)+ expect(other_admin.reload.spree_admin?(store)).to be(false)+ endendendenddiff --git a/spree/api/spec/integration/spree/api/v3/admin/admin_users_spec.rb b/spree/api/spec/integration/spree/api/v3/admin/admin_users_spec.rbindex 24c89bcff7..1b57a87ab9 100644--- a/spree/api/spec/integration/spree/api/v3/admin/admin_users_spec.rb+++ b/spree/api/spec/integration/spree/api/v3/admin/admin_users_spec.rb@@ -108,7 +108,8 @@ RSpec.describe 'Admin Staff API', type: :request, swagger_doc: 'api-reference/addelete 'Remove a staff member from this store' dotags 'Staff'security [api_key: [], bearer_auth: []]- description "Removes the user's role assignments on the current store. The account is preserved — the user keeps access to any other stores."+ description "Removes the user's role assignments on the current store. The account is preserved — the user keeps access to any other stores. " \+ 'Removing a role needs the same rights as granting it, and the store always keeps at least one admin.'admin_scope :write, :staffadmin_sdk_example 'admin-users/delete'@@ -120,6 +121,9 @@ RSpec.describe 'Admin Staff API', type: :request, swagger_doc: 'api-reference/adresponse '204', 'staff removed from store' dolet(:'x-spree-api-key') { secret_api_key.plaintext_token }+ # The store must keep an admin, so another one stays behind.+ before { create(:admin_user).role_users.find_or_create_by!(role: admin_role) }+run_test! do |response|# Account still exists; only the per-store RoleUser is gone.expect(Spree.admin_user_class.exists?(staff_member.id)).to be true