mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 07:29:16 +00:00
## Problem
In organization Team settings, admins could only assign one project at a
time to a team member with project-scoped access, both when sending an
invite and when managing an existing member's access. The platform API
(role_scoped_projects) already accepts an array of project refs, so this
was purely a UI constraint. Teams customers with larger orgs hit this
repeatedly, since granting access to several projects meant repeating
the picker one project at a time.
## Solution
Added a new shared component, elect, and used it in bothces that
previously used thonProjectSelector:
- Invite members: the project field changed from a scalar projectRef to
projectRefs: string[]; the invite payload now sends every selected
project. anage access: the one-at-a-is replaced by theulti-select, wired
into the list (add/remove updates thatlist; the per-row role dropdowns
and remove buttons are unchanged).
The multi-select reuses the existing debounced-search + infinite-scroll
project daer (same as the old single-ve the same), combined with the
generic ui-patterns/multi-select primitives for the checkable list and
removable trigger badges. It supports:
- Type-to-search against the paginated project list eselecting a project
eitherits badge on the trigger
- Projects the current user isn't permitted to remove (a role-removal
permission check) show as selected-but-locked, with an explanatory
tooltip
existing "All projects" toflows — the multi-select onlyrenders when that
toggle is off, and toggling it on/off behaves exactly as before.
Also fixed an unrelated pre-existing cosmetic bug noticed while testing:
the Managess sheet used bg-surface-2) instead of the sheetprimitive's
default opaque background, making it visibly see-through compared to
every other sheet in the app.
## Review instructions
1. Open Invite members, turn os if needed, toggle "Grant this role on
all projects" off, and confirm you can search and select several
projects, with each shown as a removable badge.
2. Send the invite and confirm applied to the new member'saccess.
3. Open Manage access on an existing member with project-scoped access —
confirm tcurrent projects are pre-se more projects via themulti-select,
remove one via its badge and another via the row's "X", and save.Confirm
the saved access ma
4. Toggle "Apply roles to all projects" on and off in Manage access and
confirm thexisting toggle behavior is
5. Confirm the Manage access sheet background is now fully opaque,
matching other sheets in the app.
6. pnpm --filter studio typecher studio test for the touchedfiles all
pass.
---------
Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
200 lines
6.6 KiB
TypeScript
200 lines
6.6 KiB
TypeScript
import { groupBy, isEqual } from 'lodash'
|
|
|
|
import {
|
|
OrganizationRole,
|
|
OrganizationRolesResponse,
|
|
} from '@/data/organization-members/organization-roles-query'
|
|
import { OrganizationMember } from '@/data/organizations/organization-members-query'
|
|
|
|
export interface ProjectRoleConfiguration {
|
|
ref?: string
|
|
name?: string
|
|
roleId: number
|
|
baseRoleId?: number
|
|
}
|
|
|
|
export const formatMemberRoleToProjectRoleConfiguration = (
|
|
member: OrganizationMember,
|
|
allRoles: OrganizationRolesResponse
|
|
) => {
|
|
const { org_scoped_roles, project_scoped_roles } = allRoles
|
|
|
|
const roleConfiguration = member.role_ids
|
|
.map((id) => {
|
|
const orgRole = org_scoped_roles.find((role) => role.id === id)
|
|
if (orgRole !== undefined) {
|
|
return { ref: undefined, name: undefined, roleId: orgRole.id }
|
|
}
|
|
|
|
const projectRole = project_scoped_roles.find((role) => role.id === id)
|
|
if (projectRole !== undefined) {
|
|
return projectRole.projects.map((x) => ({
|
|
ref: x.ref,
|
|
name: x.name,
|
|
roleId: projectRole.id,
|
|
baseRoleId: projectRole.base_role_id,
|
|
}))
|
|
}
|
|
|
|
return undefined
|
|
})
|
|
.filter(Boolean)
|
|
.flat()
|
|
.filter((p) => {
|
|
// [Joshen] Validate only for project scoped roles
|
|
// This filters out project scoped roles for projects that the user doesn't have access to
|
|
// (e.g if the project was deleted)
|
|
if ('baseRoleId' in p!) {
|
|
return p.ref !== undefined
|
|
} else {
|
|
return p
|
|
}
|
|
})
|
|
.sort((a, b) => (a?.name ?? '').localeCompare(b?.name ?? '')) as ProjectRoleConfiguration[]
|
|
|
|
return roleConfiguration
|
|
}
|
|
|
|
export const deriveChanges = (
|
|
original: ProjectRoleConfiguration[] = [],
|
|
final: ProjectRoleConfiguration[] = []
|
|
) => {
|
|
const removed: ProjectRoleConfiguration[] = []
|
|
const added: ProjectRoleConfiguration[] = []
|
|
const updated: {
|
|
ref?: string
|
|
name?: string
|
|
originalRole: number
|
|
originalBaseRole?: number
|
|
updatedRole: number
|
|
}[] = []
|
|
|
|
original.forEach((x) => {
|
|
const updatedRoleForProject = final.find((y) => x.ref === y.ref)
|
|
if (updatedRoleForProject === undefined) {
|
|
removed.push(x)
|
|
} else if (updatedRoleForProject.roleId !== x.roleId) {
|
|
updated.push({
|
|
ref: updatedRoleForProject.ref,
|
|
name: updatedRoleForProject.name,
|
|
originalRole: x.roleId,
|
|
originalBaseRole: x.baseRoleId,
|
|
updatedRole: updatedRoleForProject.roleId,
|
|
})
|
|
}
|
|
})
|
|
|
|
final.forEach((x) => {
|
|
const newRoleForProject = original.find((y) => x.ref === y.ref)
|
|
if (newRoleForProject === undefined) {
|
|
added.push(x)
|
|
}
|
|
})
|
|
|
|
return { removed, added, updated }
|
|
}
|
|
|
|
/** Order-insensitive: the same projects with the same roles count as unchanged */
|
|
export const hasRoleConfigurationChanges = (
|
|
original: ProjectRoleConfiguration[],
|
|
final: ProjectRoleConfiguration[]
|
|
) => {
|
|
const { removed, added, updated } = deriveChanges(original, final)
|
|
return removed.length > 0 || added.length > 0 || updated.length > 0
|
|
}
|
|
|
|
// [Joshen] Essentially spit out 3 arrays
|
|
// What to (1) Remove (2) Assign (3) Update
|
|
// And so that we can unit test this
|
|
export const deriveRoleChangeActions = (
|
|
existingRoles: OrganizationRole[],
|
|
changesToRoles: {
|
|
removed: ProjectRoleConfiguration[]
|
|
added: ProjectRoleConfiguration[]
|
|
updated: {
|
|
ref?: string
|
|
name?: string
|
|
originalRole: number
|
|
originalBaseRole?: number
|
|
updatedRole: number
|
|
}[]
|
|
}
|
|
) => {
|
|
const { removed, added, updated } = changesToRoles
|
|
const toRemove: number[] = []
|
|
const toAssign: { roleId: number; refs: string[] }[] = []
|
|
const toUpdate: { roleId: number; refs: string[] }[] = []
|
|
|
|
const groupByAddedRoles = groupBy(added, 'roleId')
|
|
const groupByRemovedRoles = groupBy(removed, 'roleId')
|
|
const groupByUpdatingFromRoles = groupBy(updated, 'originalRole')
|
|
const groupByUpdatingToRoles = groupBy(updated, 'updatedRole')
|
|
const existingProjectRolesByBaseIds = existingRoles
|
|
.filter((r) => (r?.projects ?? []).length > 0)
|
|
.map((r) => r.base_role_id)
|
|
|
|
existingRoles.forEach((role) => {
|
|
const projectRefsApplied = role.projects.map((x) => x.ref)
|
|
if (projectRefsApplied.length === 0) {
|
|
// [Joshen] In this case we're removing an org scope role, skip all the chekcs
|
|
return toRemove.push(role.id)
|
|
}
|
|
|
|
const toRemoveRole = projectRefsApplied.every((ref) => {
|
|
const isRemoved = removed.map((r) => r.ref).some((x) => x === ref)
|
|
const isUpdated = updated.map((r) => r.ref).some((x) => x === ref)
|
|
return isRemoved || isUpdated
|
|
})
|
|
const isRoleGettingAdded = added.some((r) => r.roleId === role.base_role_id)
|
|
const isRoleGettingUpdatedTo = updated.some((r) => r.updatedRole === role.base_role_id)
|
|
|
|
if (toRemoveRole && !isRoleGettingAdded && !isRoleGettingUpdatedTo) {
|
|
return toRemove.push(role.id)
|
|
}
|
|
|
|
const projectsToAddToRole = (groupByAddedRoles[role.base_role_id ?? '']?.map((r) => r.ref) ??
|
|
[]) as string[]
|
|
const projectsToRemoveFromRole = (groupByRemovedRoles[role.id]?.map((r) => r.ref) ??
|
|
[]) as string[]
|
|
const projectsUpdatingFromRole = (groupByUpdatingFromRoles[role.id]?.map((r) => r.ref) ??
|
|
[]) as string[]
|
|
const projectsUpdatingToRole = (groupByUpdatingToRoles[role.base_role_id ?? '']?.map(
|
|
(r) => r.ref
|
|
) ?? []) as string[]
|
|
const projectRefsAppliedUpdated = projectRefsApplied
|
|
.filter((x) => !projectsToRemoveFromRole.includes(x))
|
|
.filter((x) => !projectsUpdatingFromRole.includes(x))
|
|
.concat(projectsToAddToRole)
|
|
.concat(projectsUpdatingToRole)
|
|
|
|
if (!isEqual(projectRefsApplied, projectRefsAppliedUpdated)) {
|
|
toUpdate.push({ roleId: role.id, refs: projectRefsAppliedUpdated })
|
|
}
|
|
})
|
|
|
|
Object.keys(groupByAddedRoles).forEach((roleId) => {
|
|
if (!existingProjectRolesByBaseIds.includes(Number(roleId))) {
|
|
toAssign.push({
|
|
roleId: Number(roleId),
|
|
refs: groupByAddedRoles[roleId].map((x) => x.ref).filter((x) => x !== undefined),
|
|
})
|
|
}
|
|
})
|
|
|
|
Object.keys(groupByUpdatingToRoles).forEach((roleId) => {
|
|
if (!existingProjectRolesByBaseIds.includes(Number(roleId))) {
|
|
toAssign.push({
|
|
roleId: Number(roleId),
|
|
refs: groupByUpdatingToRoles[roleId].map((x) => x.ref).filter((x) => x !== undefined),
|
|
})
|
|
}
|
|
})
|
|
|
|
// [Joshen] Am sorting the results just so its more deterministic when writing tests
|
|
return {
|
|
toRemove: toRemove.sort((a, b) => a - b),
|
|
toAssign: toAssign.map((x) => ({ ...x, refs: x.refs.sort((a, b) => a.localeCompare(b)) })),
|
|
toUpdate: toUpdate.map((x) => ({ ...x, refs: x.refs.sort((a, b) => a.localeCompare(b)) })),
|
|
}
|
|
}
|