Modern Docker Management, Designed for Everyone
Go composecontainer-managementcontainersdockerdocker-composedocker-managementgoself-hostedsveltekittypescriptweb-ui
fix: duplicate role assignments cause a 500 in the user editor (#4332)
DominikZublasingfdc9c0e5c426
diff --git a/backend/internal/role/service.go b/backend/internal/role/service.goindex 12b5fa64e..652d9f2fa 100644--- a/backend/internal/role/service.go+++ b/backend/internal/role/service.go@@ -377,6 +377,15 @@ func (s *RoleService) ListUserAssignments(ctx context.Context, userID string) ([// is enforced before commit. Shared by SetUserAssignments and// ReplaceOidcAssignments.func (s *RoleService) replaceUserAssignmentsForSourceInternal(ctx context.Context, userID, source string, desired []UserRoleAssignment) error {+ seen := make(map[string]struct{}, len(desired))+ desired = slices.DeleteFunc(desired, func(a UserRoleAssignment) bool {+ key := assignmentKeyInternal(a)+ if _, dup := seen[key]; dup {+ return true+ }+ seen[key] = struct{}{}+ return false+ })for i := range desired {desired[i].UserID = userIDdesired[i].Source = source@@ -424,6 +433,14 @@ func (s *RoleService) replaceUserAssignmentsForSourceInternal(ctx context.Contexreturn nil}+func assignmentKeyInternal(a UserRoleAssignment) string {+ envID := ""+ if a.EnvironmentID != nil {+ envID = *a.EnvironmentID+ }+ return a.RoleID + "\x00" + envID+}+// SetUserAssignments replaces the user's source='manual' assignments with the// given desired set. Source='oidc' rows are preserved (use// ReplaceOidcAssignments for those). Enforces the global-admin guard.diff --git a/backend/resources/migrations/postgres/091_scope_role_assignment_uniqueness_by_source.sql b/backend/resources/migrations/postgres/091_scope_role_assignment_uniqueness_by_source.sqlnew file mode 100644index 000000000..116954a3f--- /dev/null+++ b/backend/resources/migrations/postgres/091_scope_role_assignment_uniqueness_by_source.sql@@ -0,0 +1,19 @@+-- +goose Up+DROP INDEX IF EXISTS idx_ura_uniq;+CREATE UNIQUE INDEX idx_ura_uniq+ ON user_role_assignments(user_id, role_id, COALESCE(environment_id, ''), source);++-- +goose Down+-- The old index spans sources, so drop OIDC rows that duplicate a manual grant first.+DELETE FROM user_role_assignments+WHERE source = 'oidc'+ AND EXISTS (+ SELECT 1 FROM user_role_assignments m+ WHERE m.source <> 'oidc'+ AND m.user_id = user_role_assignments.user_id+ AND m.role_id = user_role_assignments.role_id+ AND COALESCE(m.environment_id, '') = COALESCE(user_role_assignments.environment_id, '')+ );+DROP INDEX IF EXISTS idx_ura_uniq;+CREATE UNIQUE INDEX idx_ura_uniq+ ON user_role_assignments(user_id, role_id, COALESCE(environment_id, ''));diff --git a/backend/resources/migrations/sqlite/091_scope_role_assignment_uniqueness_by_source.sql b/backend/resources/migrations/sqlite/091_scope_role_assignment_uniqueness_by_source.sqlnew file mode 100644index 000000000..116954a3f--- /dev/null+++ b/backend/resources/migrations/sqlite/091_scope_role_assignment_uniqueness_by_source.sql@@ -0,0 +1,19 @@+-- +goose Up+DROP INDEX IF EXISTS idx_ura_uniq;+CREATE UNIQUE INDEX idx_ura_uniq+ ON user_role_assignments(user_id, role_id, COALESCE(environment_id, ''), source);++-- +goose Down+-- The old index spans sources, so drop OIDC rows that duplicate a manual grant first.+DELETE FROM user_role_assignments+WHERE source = 'oidc'+ AND EXISTS (+ SELECT 1 FROM user_role_assignments m+ WHERE m.source <> 'oidc'+ AND m.user_id = user_role_assignments.user_id+ AND m.role_id = user_role_assignments.role_id+ AND COALESCE(m.environment_id, '') = COALESCE(user_role_assignments.environment_id, '')+ );+DROP INDEX IF EXISTS idx_ura_uniq;+CREATE UNIQUE INDEX idx_ura_uniq+ ON user_role_assignments(user_id, role_id, COALESCE(environment_id, ''));diff --git a/frontend/src/lib/components/forms/role-assignments-editor.svelte b/frontend/src/lib/components/forms/role-assignments-editor.svelteindex ac86201fa..51d8afa9e 100644--- a/frontend/src/lib/components/forms/role-assignments-editor.svelte+++ b/frontend/src/lib/components/forms/role-assignments-editor.svelte@@ -67,14 +67,18 @@}function updateAssignment(index: number, patch: Partial<Assignment>) {- if (disabled) return;- assignments = assignments.map((a, i) => (i === index ? { ...a, ...patch } : a));+ const row = assignments[index];+ if (disabled || !row) return;+ // Mutate in place so the row keeps its identity, which the {#each} key relies on.+ Object.assign(row, patch);}function addAssignment() {if (disabled) return;const defaultRoleId = roles[0]?.id ?? '';- assignments = [...assignments, { roleId: defaultRoleId, environmentId: undefined }];+ const freeEnv = envOptions.find((option) => !isEnvTaken(option.id, defaultRoleId, -1));+ const envValue = freeEnv?.id ?? GLOBAL_ENVIRONMENT_OPTION_ID;+ assignments = [...assignments, { roleId: defaultRoleId, environmentId: selectValueToEnvId(envValue) }];}function removeAssignment(index: number) {@@ -135,7 +139,7 @@</p>{/if}- {#each assignments as assignment, index (`${assignment.roleId}-${assignment.environmentId ?? 'global'}`)}+ {#each assignments as assignment, index (assignment)}{@const envValue = envIdToSelectValue(assignment.environmentId)}<div class="grid grid-cols-1 gap-2 rounded-md border bg-card/50 p-3 sm:grid-cols-halves-action sm:items-center"><Select.Rootdiff --git a/frontend/src/lib/utils/options.ts b/frontend/src/lib/utils/options.tsindex 2c7cf1297..680de8ba0 100644--- a/frontend/src/lib/utils/options.ts+++ b/frontend/src/lib/utils/options.ts@@ -10,7 +10,9 @@ export function buildGlobalEnvironmentOptions<T extends { id: string; name: stri): { id: string; name: string }[] {return [{ id: GLOBAL_ENVIRONMENT_OPTION_ID, name: globalLabel },- ...environments.map((env) => ({ id: env.id, name: env.name }))+ ...environments+ .map((env) => ({ id: env.id, name: env.name }))+ .sort((a, b) => a.name.localeCompare(b.name, undefined, { numeric: true, sensitivity: 'base' }))];}diff --git a/frontend/src/routes/(app)/account/+page.svelte b/frontend/src/routes/(app)/account/+page.svelteindex c5bbb5a6d..f4baaf5b5 100644--- a/frontend/src/routes/(app)/account/+page.svelte+++ b/frontend/src/routes/(app)/account/+page.svelte@@ -349,7 +349,7 @@</h3>{#if currentUser.roleAssignments && currentUser.roleAssignments.length > 0}<ul class="flex flex-wrap gap-2">- {#each currentUser.roleAssignments as ra (`${ra.roleId}-${ra.environmentId ?? 'global'}`)}+ {#each currentUser.roleAssignments as ra (`${ra.source}-${ra.roleId}-${ra.environmentId ?? 'global'}`)}<li class="rounded-lg border border-border/60 bg-muted/20 px-3 py-1.5"><span class="text-sm font-medium">{prettyRoleName(ra.roleId)}</span><span class="ml-2 text-xs text-muted-foreground">diff --git a/frontend/src/routes/(app)/settings/users/+page.svelte b/frontend/src/routes/(app)/settings/users/+page.svelteindex 2e5249ef1..593bc7626 100644--- a/frontend/src/routes/(app)/settings/users/+page.svelte+++ b/frontend/src/routes/(app)/settings/users/+page.svelte@@ -48,9 +48,22 @@const editingAssignments = $derived(userToEdit?.roleAssignments?.filter((a) => a.source !== 'oidc')- .map((a) => ({ roleId: a.roleId, environmentId: a.environmentId })) ?? []+ .map((a) => ({ roleId: a.roleId, environmentId: a.environmentId }))+ .sort(compareAssignments) ?? []);+ const nameCollator = new Intl.Collator(undefined, { numeric: true, sensitivity: 'base' });++ // Global assignments sort first because their environment name is empty.+ function compareAssignments(a: { roleId: string; environmentId?: string }, b: { roleId: string; environmentId?: string }) {+ const envName = (id?: string) => (id ? (data.environments.find((e) => e.id === id)?.name ?? id) : '');+ const roleName = (id: string) => data.roles.find((r) => r.id === id)?.name ?? id;+ return (+ nameCollator.compare(envName(a.environmentId), envName(b.environmentId)) ||+ nameCollator.compare(roleName(a.roleId), roleName(b.roleId))+ );+ }+let isLoading = $state({creating: false,editing: false,diff --git a/frontend/src/routes/(app)/settings/users/components/user-table.svelte b/frontend/src/routes/(app)/settings/users/components/user-table.svelteindex 52d492c74..0ef27aa00 100644--- a/frontend/src/routes/(app)/settings/users/components/user-table.svelte+++ b/frontend/src/routes/(app)/settings/users/components/user-table.svelte@@ -138,14 +138,17 @@}// Group by roleId so a user assigned the same role on multiple envs gets// one badge with a tooltip listing the env count.- const byRole = new Map<string, number>();+ // A Set per role, since manual and OIDC can both grant the same environment.+ const byRole = new Map<string, Set<string>>();for (const a of assignments) {- byRole.set(a.roleId, (byRole.get(a.roleId) ?? 0) + 1);+ const envs = byRole.get(a.roleId) ?? new Set<string>();+ envs.add(a.environmentId ?? 'global');+ byRole.set(a.roleId, envs);}- return Array.from(byRole.entries()).map(([roleId, count]) => ({+ return Array.from(byRole.entries()).map(([roleId, envs]) => ({text: roleName(roleId),variant: roleVariant(roleId),- tooltip: count > 1 ? m.users_role_summary_env_count({ count }) : undefined+ tooltip: envs.size > 1 ? m.users_role_summary_env_count({ count: envs.size }) : undefined}));}