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: required every invitation to name its role

Damian Legawiece3754a171f43
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/invitations_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/invitations_controller_spec.rb
index 9ba949a719..f2790def29 100644
--- a/spree/api/spec/controllers/spree/api/v3/admin/invitations_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/admin/invitations_controller_spec.rb
@@ -53,6 +53,15 @@ RSpec.describe Spree::Api::V3::Admin::InvitationsController, type: :controller d
expect(Spree::Invitation.last.role).to eq(staff_role)
end
+ # Leaving the role out used to fall back to the admin role.
+ it 'refuses an invitation without a role' do
+ expect {
+ post :create, params: { email: '[email protected]' }, as: :json
+ }.not_to change(Spree::Invitation, :count)
+
+ expect(response).to have_http_status(:unprocessable_content)
+ end
+
it 'forbids inviting into a role whose permissions exceed its own' do
owner_role = create(:role, name: 'owner', permissions: Spree.permissions.grantable_keys(:store))
diff --git a/spree/core/app/models/spree/invitation.rb b/spree/core/app/models/spree/invitation.rb
index 61a3569505..37378cc2c7 100644
--- a/spree/core/app/models/spree/invitation.rb
+++ b/spree/core/app/models/spree/invitation.rb
@@ -51,7 +51,7 @@ module Spree
# Callbacks
#
after_initialize :set_defaults, if: :new_record?
- before_validation :set_role_and_resource, if: :new_record?
+ before_validation :set_resource, if: :new_record?
before_validation :set_invitee_from_email, on: :create
after_commit :publish_invitation_created_event, on: :create, unless: :skip_email
@@ -135,10 +135,11 @@ module Spree
# A role names what it governs, so the invitation follows it — one carrying
# another resource's role would grant access somewhere the inviter never
# named. Resolved at validation rather than on initialize, since a caller's
- # own `resource` is not assigned yet when the record is instantiated.
- def set_role_and_resource
+ # own `resource` is not assigned yet when the record is instantiated. The
+ # role is never defaulted: the inviter names it, and the controller checks
+ # they may grant it.
+ def set_resource
self.resource ||= role&.resource || Spree::Store.current
- self.role ||= Spree::Role.default_admin_role(resource)
end
def invitee_is_not_inviter
diff --git a/spree/core/app/workflows/spree/sellers/invite.rb b/spree/core/app/workflows/spree/sellers/invite.rb
index 2a10906abf..7c2c02665e 100644
--- a/spree/core/app/workflows/spree/sellers/invite.rb
+++ b/spree/core/app/workflows/spree/sellers/invite.rb
@@ -48,7 +48,7 @@ module Spree
# role naming somewhere else comes back as a 422 rather than access
# granted elsewhere.
def send_invitation
- @invitation = seller.invitations.new(email: email, role: role, inviter: inviter)
+ @invitation = seller.invitations.new(email: email, role: role || seller.default_user_role, inviter: inviter)
failure(@invitation, @invitation.errors) unless @invitation.save
end
diff --git a/spree/core/lib/spree/testing_support/factories/invitation_factory.rb b/spree/core/lib/spree/testing_support/factories/invitation_factory.rb
index b26017861f..2c5edbfc88 100644
--- a/spree/core/lib/spree/testing_support/factories/invitation_factory.rb
+++ b/spree/core/lib/spree/testing_support/factories/invitation_factory.rb
@@ -2,5 +2,6 @@ FactoryBot.define do
factory :invitation, class: Spree::Invitation do
email { FFaker::Internet.email }
association :inviter, factory: :admin_user
+ role { (resource || Spree::Store.current).default_user_role }
end
end
diff --git a/spree/core/spec/models/spree/invitation_spec.rb b/spree/core/spec/models/spree/invitation_spec.rb
index 7c3ee6f467..a05a5218a9 100644
--- a/spree/core/spec/models/spree/invitation_spec.rb
+++ b/spree/core/spec/models/spree/invitation_spec.rb
@@ -52,12 +52,21 @@ RSpec.describe Spree::Invitation, type: :model do
# Resolved at validation, not on initialize: the caller's own resource is
# not assigned yet when the record is instantiated.
- it 'defaults the resource and its admin role before validation' do
+ it 'defaults the resource before validation' do
invitation = build(:invitation)
invitation.valid?
expect(invitation.resource).to eq(Spree::Store.current)
- expect(invitation.role).to eq(Spree::Role.default_admin_role)
+ end
+
+ # Defaulting to the admin role let anyone allowed to invite staff hand out
+ # admin by leaving the role out.
+ it 'never defaults the role' do
+ invitation = build(:invitation, role: nil)
+
+ expect(invitation).not_to be_valid
+ expect(invitation.role).to be_nil
+ expect(invitation.errors[:role]).to be_present
end
it 'follows an explicitly assigned resource' do
diff --git a/spree/core/spec/models/spree/seller_spec.rb b/spree/core/spec/models/spree/seller_spec.rb
index c9599c0cd8..e912c17dda 100644
--- a/spree/core/spec/models/spree/seller_spec.rb
+++ b/spree/core/spec/models/spree/seller_spec.rb
@@ -313,7 +313,6 @@ describe Spree::Seller do
end
end
-
describe '#default_user_role' do
let(:seller) { create(:seller) }
@@ -336,18 +335,6 @@ describe Spree::Seller do
expect(seller.roles.count).to eq(1)
end
- # An invitation fills in its own default role, and the operator's invite
- # workflow sends none — so the invitation, not the seller, is what creates
- # the role first. Seeded there too, or whoever accepts holds nothing and
- # every endpoint on their own panel refuses them.
- it 'is seeded even when an invitation creates the role first' do
- invitation = seller.invitations.create!(email: '[email protected]', inviter: create(:admin_user))
-
- expect(invitation.role.permissions).
- to match_array(Spree.permissions.grantable_keys(:seller))
- expect(invitation.role).to eq(seller.default_user_role)
- end
-
# The operator's `sellers` key is not in the seller vocabulary, so holding
# this role never lets a seller administer sellers — their own included.
it 'does not let its holder manage the seller record' do