Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,11 @@ export default function AdminManageAccessModal({
);
return;
}
const orgIds = data.roles?.map((r) => r.orgId) ?? [];
if (new Set(orgIds).size !== orgIds.length) {
toast.error("A user can only have one role per organization");
return;
}
// When updating an existing user (has id), email is not required
// The API will handle PII restrictions and won't update email if not provided
// For new users, email is required by the schema
Expand Down Expand Up @@ -710,6 +715,13 @@ export default function AdminManageAccessModal({
);
}

const assignedOrgIds = new Set(
((field.value as RoleEntry[]) ?? [])
.filter((_, i) => i !== index)
.map((r) => r.orgId)
.filter((id): id is number => id != null),
);

return (
<div
key={index}
Expand Down Expand Up @@ -744,10 +756,14 @@ export default function AdminManageAccessModal({
<VirtualizedCombobox
value={roleEntry.orgId.toString()}
options={
orgs?.orgs.map((org) => ({
value: org.id.toString(),
label: `${org.name} (${org.orgType})`,
})) ?? []
orgs?.orgs
.filter(
(org) => !assignedOrgIds.has(org.id),
)
.map((org) => ({
value: org.id.toString(),
label: `${org.name} (${org.orgType})`,
})) ?? []
}
searchPlaceholder="Select an organization"
disabled={isLoadingOrgs}
Expand Down
7 changes: 2 additions & 5 deletions packages/api/src/router/user-grant-access.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -474,14 +474,11 @@ describe("User Router - Grant Access", () => {
}),
).resolves.toMatchObject({ id: user.id });

// Adding a role requires org admin
// Upgrading the role requires org admin
await expect(
editorClient.user.crupdate({
id: user.id,
roles: [
{ orgId: nationOrg.id, roleName: "editor" },
{ orgId: nationOrg.id, roleName: "admin" },
],
roles: [{ orgId: nationOrg.id, roleName: "admin" }],
}),
).rejects.toThrow(ERRORS.MUST_BE_ADMIN_TO_GRANT_ROLES);

Expand Down
9 changes: 8 additions & 1 deletion packages/validators/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,14 @@ export const CrupdateUserSchema = UserInsertSchema.extend({
orgId: z.number(),
roleName: z.enum(["user", "editor", "admin"]),
})
.array(),
.array()
.refine(
(roles) => {
const orgIds = roles.map((r) => r.orgId);
return new Set(orgIds).size === orgIds.length;
},
{ message: "A user can only have one role per organization" },
),
f3Name: z.string().optional(),
email: z
.string()
Expand Down
Loading