Skip to content

Commit 36e007d

Browse files
committed
fix(organizations): offer Leave only from an authoritative role
1 parent 4334e92 commit 36e007d

2 files changed

Lines changed: 16 additions & 8 deletions

File tree

‎apps/sim/app/workspace/[workspaceId]/settings/components/team-management/team-management.tsx‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import {
3131
useMemberRemovalImpact,
3232
useOrganization,
3333
useOrganizationRoster,
34+
useOrganizationSummary,
3435
useRemoveMember,
3536
useTransferOwnership,
3637
} from '@/hooks/queries/organization'
@@ -93,13 +94,19 @@ export function TeamManagement({
9394
refetch: refetchRoster,
9495
} = useOrganizationRoster(organizationId)
9596

96-
// Prefer the roster row: Better Auth's full-organization read caps its member list and can omit
97-
// the caller. An unknown role hides Leave rather than offering an owner a request that always fails.
98-
const currentUserRole =
99-
roster?.members.find((member) => member.userId === session?.user?.id)?.role ??
100-
organization?.members.find((member) => member.user.email === session?.user?.email)?.role
101-
const adminOrOwner = isOrgAdminRole(currentUserRole ?? 'member')
102-
const canLeave = currentUserRole !== undefined && currentUserRole !== 'owner'
97+
// Only a row naming the caller (the roster) or the server's own role read is authoritative:
98+
// Better Auth's full-organization read caps its member list and can omit or lag the caller.
99+
const rosterRole = roster?.members.find((member) => member.userId === session?.user?.id)?.role
100+
const { data: organizationSummary } = useOrganizationSummary(organizationId, {
101+
enabled: rosterError !== null,
102+
})
103+
const confirmedRole = rosterRole ?? organizationSummary?.userRole
104+
const adminOrOwner = isOrgAdminRole(
105+
confirmedRole ??
106+
organization?.members.find((member) => member.user.email === session?.user?.email)?.role ??
107+
'member'
108+
)
109+
const canLeave = confirmedRole !== undefined && confirmedRole !== 'owner'
103110

104111
const {
105112
data: organizationBillingData,

‎apps/sim/hooks/queries/organization.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@ export const ORGANIZATION_REMOVAL_IMPACT_STALE_TIME = 0
4949
export { organizationKeys }
5050

5151
/** Revalidates organization membership and chat policy through the shared authorized read. */
52-
export function useOrganizationSummary(organizationId: string) {
52+
export function useOrganizationSummary(organizationId: string, options?: { enabled?: boolean }) {
5353
return useQuery({
54+
enabled: options?.enabled ?? true,
5455
queryKey: organizationKeys.summary(organizationId),
5556
queryFn: ({ signal }) =>
5657
requestJson(getOrganizationContract, {

0 commit comments

Comments
 (0)