@@ -25,12 +25,10 @@ import {
2525 Switch ,
2626} from '@/components/emcn'
2727import { ArrowLeft } from '@/components/emcn/icons'
28- import { useSession } from '@/lib/auth/auth-client'
2928import { getEnv , isTruthy } from '@/lib/core/config/env'
3029import { cn } from '@/lib/core/utils/cn'
3130import type { PermissionGroupConfig } from '@/lib/permission-groups/types'
3231import { getUserColor } from '@/lib/workspaces/colors'
33- import { getUserRole } from '@/lib/workspaces/organization'
3432import { getAllBlocks } from '@/blocks'
3533import {
3634 type PermissionGroup ,
@@ -44,7 +42,7 @@ import {
4442 useUserPermissionConfig ,
4543} from '@/ee/access-control/hooks/permission-groups'
4644import { useBlacklistedProviders } from '@/hooks/queries/allowed-providers'
47- import { useOrganizationRoster , useOrganizations } from '@/hooks/queries/organization'
45+ import { useOrganizationRoster } from '@/hooks/queries/organization'
4846import { useProviderModels } from '@/hooks/queries/providers'
4947import {
5048 DYNAMIC_MODEL_PROVIDERS ,
@@ -407,27 +405,31 @@ export function AccessControl() {
407405 const params = useParams ( )
408406 const workspaceId = typeof params ?. workspaceId === 'string' ? params . workspaceId : undefined
409407
410- const { data : session } = useSession ( )
411- const { data : organizationsData , isPending : orgLoading } = useOrganizations ( )
412- const activeOrganization = organizationsData ?. activeOrganization
413- const organizationId = activeOrganization ?. id
408+ // Access control is governed by the workspace's OWNING organization, which may
409+ // differ from the caller's active org (e.g. external members). Resolve the org
410+ // id and the caller's admin status server-side from the workspace so gating is
411+ // never keyed off the session's active org.
412+ const { data : userPermissionConfig , isPending : entitlementLoading } =
413+ useUserPermissionConfig ( workspaceId )
414+ const organizationId = userPermissionConfig ?. organizationId ?? undefined
415+ const currentUserIsOrgAdmin = userPermissionConfig ?. isOrgAdmin ?? false
414416
417+ // Group + roster reads require org admin/owner on the host org; only fetch them
418+ // for admins to avoid surfacing expected 403s for non-admins/external members.
415419 const { data : permissionGroups = [ ] , isPending : groupsLoading } = usePermissionGroups (
416420 organizationId ,
417- ! ! organizationId
421+ ! ! organizationId && currentUserIsOrgAdmin
418422 )
419- const { data : roster } = useOrganizationRoster ( organizationId )
420- const { data : userPermissionConfig , isPending : entitlementLoading } =
421- useUserPermissionConfig ( workspaceId )
422-
423- const userRole = getUserRole ( activeOrganization , session ?. user ?. email ?? undefined )
424- const currentUserIsOrgAdmin = userRole === 'owner' || userRole === 'admin'
423+ const { data : roster } = useOrganizationRoster ( currentUserIsOrgAdmin ? organizationId : undefined )
425424
426425 const accessControlEnabledLocally = isTruthy ( getEnv ( 'NEXT_PUBLIC_ACCESS_CONTROL_ENABLED' ) )
427426 const isEntitled = accessControlEnabledLocally || ! ! userPermissionConfig ?. entitled
428427 const canManage = isEntitled && currentUserIsOrgAdmin && ! ! organizationId
429428
430- const isLoading = ! workspaceId || orgLoading || groupsLoading || entitlementLoading
429+ const isLoading =
430+ ! workspaceId ||
431+ entitlementLoading ||
432+ ( ! ! organizationId && currentUserIsOrgAdmin && groupsLoading )
431433
432434 const createPermissionGroup = useCreatePermissionGroup ( )
433435 const updatePermissionGroup = useUpdatePermissionGroup ( )
0 commit comments