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
Seed a seller's role wherever it is created first
Damian Legawiecc6578cbfa931
diff --git a/packages/seller-dashboard/src/pages/accept-invitation.tsx b/packages/seller-dashboard/src/pages/accept-invitation.tsxindex 50dc14b67b..8186d32596 100644--- a/packages/seller-dashboard/src/pages/accept-invitation.tsx+++ b/packages/seller-dashboard/src/pages/accept-invitation.tsx@@ -1,6 +1,7 @@import { useAuth } from '@spree/dashboard-core'import { Button, Field, FieldError, FieldGroup, FieldLabel, Input } from '@spree/dashboard-ui'import { useQuery } from '@tanstack/react-query'+import { useNavigate } from '@tanstack/react-router'import { useState } from 'react'import { useForm } from 'react-hook-form'import { useTranslation } from 'react-i18next'@@ -30,6 +31,7 @@ export function AcceptInvitationPage({}) {const { t } = useTranslation()const { acceptInvitation } = useAuth()+ const navigate = useNavigate()const [error, setError] = useState<string | null>(null)const {@@ -57,8 +59,12 @@ export function AcceptInvitationPage({setError(null)try {await acceptInvitation(invitationId, token ?? '', values)- // The index route decides where to go — it already knows how to skip- // the picker for someone who runs exactly one seller.+ // The index route decides where to go from there — it already knows how+ // to skip the picker for someone who runs exactly one seller. Without+ // this the form simply sits there after a successful accept, and the+ // obvious next move is to submit again, against an invitation that has+ // now been consumed.+ navigate({ to: '/', replace: true })} catch {setError(t('accept_invitation.failed'))}diff --git a/spree/core/app/models/spree/role.rb b/spree/core/app/models/spree/role.rbindex 8d329c3bc1..5301bc3973 100644--- a/spree/core/app/models/spree/role.rb+++ b/spree/core/app/models/spree/role.rb@@ -74,6 +74,13 @@ module Spree# The resource's own super-role, created on first ask. Each owns one:# "admin" means everything in *this* store, so it cannot be shared.#+ # Permissions come from the audience the resource grants to, so a role is+ # born holding them whichever caller asks first — an invitation filling in+ # its default role, or the resource seeding its own team. A store's stays+ # empty by design: its super-role is short-circuited by+ # `Ability#activate_full_access` and needs no keys, while a seller's is an+ # ordinary role that would otherwise be refused by every endpoint.+ ## @param resource [Spree::Store, Object] defaults to the current store# @return [Spree::Role]def self.default_admin_role(resource = nil)@@ -81,9 +88,18 @@ module Spreefor_resource(resource).find_or_create_by(name: ADMIN_ROLE) do |role|role.mutable = false+ role.permissions = default_admin_permissions_for(resource)endend+ # @return [Array<String>] the keys a resource's own super-role is born with+ def self.default_admin_permissions_for(resource)+ audience = resource.try(:role_audience)+ return [] if audience.blank?++ Spree.permissions.grantable_keys(audience)+ end+# The protected super-role: everything in this store. Guarded by the owning# resource as well as the name — names are unique per resource, so a seller# may legitimately call a role "admin" and it must never be mistaken fordiff --git a/spree/core/app/models/spree/seller.rb b/spree/core/app/models/spree/seller.rbindex 97b2193839..f7527a4a1d 100644--- a/spree/core/app/models/spree/seller.rb+++ b/spree/core/app/models/spree/seller.rb@@ -170,22 +170,15 @@ module Spree# cascade through, so an invited seller stays deletable.before_destroy :dissolve_team, prepend: true- # A seller's own team is granted through its roles, not the store's.- # A store's "admin" role is the protected super-role and needs no- # permissions — `Ability#activate_full_access` short-circuits it. A- # seller's cannot take that path (`Role#admin?` requires `staff?`), so- # without seeding, whoever runs the seller would hold nothing and every- # endpoint on their own panel would refuse them.+ # Which audience of the permission catalog this seller's own super-role is+ # born holding. A seller's role cannot take the store super-role's+ # short-circuit (`Role#admin?` requires `staff?`), so without these keys+ # whoever runs the seller would hold nothing and every endpoint on their+ # own panel would refuse them.#- # Seeded at creation rather than patched afterwards: the role is born- # immutable, so a later write would have to defeat its own guard.- #- # @return [Spree::Role]- def default_user_role- roles.for_resource(self).find_or_create_by!(name: Spree::Role::ADMIN_ROLE) do |role|- role.mutable = false- role.permissions = Spree.permissions.grantable_keys(:seller)- end+ # @return [Symbol]+ def role_audience+ :sellerend# Whether the seller is currently away. The catalog stays visible; whatdiff --git a/spree/core/spec/models/spree/seller_spec.rb b/spree/core/spec/models/spree/seller_spec.rbindex fcc1911951..54b212951c 100644--- a/spree/core/spec/models/spree/seller_spec.rb+++ b/spree/core/spec/models/spree/seller_spec.rb@@ -335,6 +335,18 @@ describe Spree::Seller doexpect(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