feat: assign custom roles in user management

This commit is contained in:
Gabriel20xx committed 2026-10-13 18:08:42 +00:00
1 parent 6388efd6d6
commit 0783b74da7
8 files changed
+72 -68

No files matched your search

-5
View File
@@ -1,5 +0,0 @@
---
"@gabriel20xx/web-ui": minor
---
`UserManagement` accepts an optional `projects` prop (`KnownProject[]`): when present, each account gets a per-project login-access picker (checkboxes in the edit row, a summary in the read-only row) and `onUpdate` receives a `projects: string[]` field. Only AuthGuard's own admin UI passes this prop today; every other consumer is unaffected.
+5
View File
@@ -0,0 +1,5 @@
---
"@gabriel20xx/web-ui": minor
---
`UserManagement` accepts an optional `roles` prop (`KnownRole[]`) in place of the `projects` prop it replaces: when present, each account gets a role picker (checkboxes in the add and edit rows, the role names in the read-only row), and `onCreate`/`onUpdate` receive a `roleIds: number[]` field. An account's projects now come from its roles, so per-account project checkboxes are gone. `ManagedUser` carries `roleIds` instead of `projects`. Only AuthGuard's own admin UI passes this prop today; every other consumer is unaffected.
+1 -1
View File
@@ -54,7 +54,7 @@ These are the complete Svelte component exports from `packages/web-ui/src/lib/in
| `StatusDialog` | Detailed service health/readiness status modal with refresh support. | | `StatusDialog` | Detailed service health/readiness status modal with refresh support. |
| `StatusWidget` | Header-friendly health/readiness indicator that polls `/healthz` and `/ready` and opens `StatusDialog`. | | `StatusWidget` | Header-friendly health/readiness indicator that polls `/healthz` and `/ready` and opens `StatusDialog`. |
| `TotpSettings` | TOTP status, setup, enable, and disable controls. | | `TotpSettings` | TOTP status, setup, enable, and disable controls. |
| `UserManagement` | Role-aware user administration with listing, creation, editing, deletion, project scopes, and pagination. | | `UserManagement` | Role-aware user administration with listing, creation, editing, deletion, role assignment, and pagination. |
This inventory describes the public component API only; the package also exports notification helpers, primary-color helpers, and TypeScript types. When a public component is added, removed, renamed, or materially changes responsibility, update this table in the same change as `packages/web-ui/src/lib/index.ts`. Keep the table aligned with that barrel file, and do not add internal or generated components to it. This inventory describes the public component API only; the package also exports notification helpers, primary-color helpers, and TypeScript types. When a public component is added, removed, renamed, or materially changes responsibility, update this table in the same change as `packages/web-ui/src/lib/index.ts`. Keep the table aligned with that barrel file, and do not add internal or generated components to it.
@@ -1,6 +1,6 @@
<script lang="ts"> <script lang="ts">
import type { import type {
KnownProject, KnownRole,
ManagedUser, ManagedUser,
UserCreateInput, UserCreateInput,
UserCreateRequest, UserCreateRequest,
@@ -21,7 +21,7 @@
onDelete, onDelete,
onList, onList,
onUpdate, onUpdate,
projects roles
}: { }: {
currentRole?: UserRole; currentRole?: UserRole;
currentUserId?: UserId; currentUserId?: UserId;
@@ -30,14 +30,13 @@
onList?: UserListRequest; onList?: UserListRequest;
onUpdate?: UserUpdateRequest; onUpdate?: UserUpdateRequest;
/** /**
* Every project an account can be restricted to logging into * The custom roles an account can be placed in (AuthGuard's role-based
* (AuthGuard's project-scope feature). Only AuthGuard's own admin UI * project access: a role decides which projects its members may log into).
* passes this - every other project's UserManagement usage (proxying to * Only AuthGuard's own admin UI passes this - every other project's
* AuthGuard's admin/users endpoints without knowing about registered * UserManagement usage omits it, and the whole role section of this
* projects) omits it, and the whole project-access section of this
* component simply doesn't render. * component simply doesn't render.
*/ */
projects?: KnownProject[]; roles?: KnownRole[];
} = $props(); } = $props();
let users = $state<ManagedUser[]>([]); let users = $state<ManagedUser[]>([]);
@@ -113,28 +112,26 @@
function startEditing(user: ManagedUser): void { function startEditing(user: ManagedUser): void {
editingId = user.id; editingId = user.id;
editUser = { username: user.username, email: user.email, role: roleForForm(user.role) }; editUser = { username: user.username, email: user.email, role: roleForForm(user.role) };
if (projects && projects.length > 0) editUser.projects = [...(user.projects ?? [])]; if (roles && roles.length > 0) editUser.roleIds = [...(user.roleIds ?? [])];
error = ''; error = '';
notice = ''; notice = '';
} }
function toggleEditProject(key: string, checked: boolean): void { function toggleEditRole(id: number, checked: boolean): void {
const current = editUser.projects ?? []; const current = editUser.roleIds ?? [];
editUser.projects = checked ? [...current, key] : current.filter((project) => project !== key); editUser.roleIds = checked ? [...current, id] : current.filter((roleId) => roleId !== id);
} }
function toggleNewProject(key: string, checked: boolean): void { function toggleNewRole(id: number, checked: boolean): void {
const current = newUser.projects ?? []; const current = newUser.roleIds ?? [];
newUser.projects = checked ? [...current, key] : current.filter((project) => project !== key); newUser.roleIds = checked ? [...current, id] : current.filter((roleId) => roleId !== id);
} }
function projectLabel(key: string): string { // Names the roles an account is in, in the order the roles are listed. An account in none of
return projects?.find((project) => project.key === key)?.label ?? key; // them keeps every project.
} function roleSummary(user: ManagedUser): string {
const names = (roles ?? []).filter((role) => (user.roleIds ?? []).includes(role.id)).map((role) => role.name);
function projectSummary(user: ManagedUser): string { return names.length > 0 ? names.join(', ') : 'All projects';
if (!user.projects || user.projects.length === 0) return 'All projects';
return user.projects.map(projectLabel).join(', ');
} }
function stopEditing(): void { function stopEditing(): void {
@@ -203,17 +200,17 @@
{#if canAssignAdmin}<option value="admin">Admin</option>{/if} {#if canAssignAdmin}<option value="admin">Admin</option>{/if}
</select> </select>
</div> </div>
{#if projects && projects.length > 0} {#if roles && roles.length > 0}
<fieldset class="sc-user-projects"> <fieldset class="sc-user-roles">
<legend>Projects (none checked = all projects)</legend> <legend>Roles (none checked = all projects)</legend>
{#each projects as project (project.key)} {#each roles as role (role.id)}
<label class="sc-user-project-option"> <label class="sc-user-role-option">
<input <input
type="checkbox" type="checkbox"
checked={(newUser.projects ?? []).includes(project.key)} checked={(newUser.roleIds ?? []).includes(role.id)}
onchange={(event) => toggleNewProject(project.key, event.currentTarget.checked)} onchange={(event) => toggleNewRole(role.id, event.currentTarget.checked)}
/> />
{project.label} {role.name}
</label> </label>
{/each} {/each}
</fieldset> </fieldset>
@@ -241,17 +238,17 @@
{#if canAssignAdmin}<option value="admin">Admin</option>{/if} {#if canAssignAdmin}<option value="admin">Admin</option>{/if}
{#if user.role === 'superadmin'}<option value="superadmin">Superadmin</option>{/if} {#if user.role === 'superadmin'}<option value="superadmin">Superadmin</option>{/if}
</select> </select>
{#if projects && projects.length > 0} {#if roles && roles.length > 0}
<fieldset class="sc-user-projects"> <fieldset class="sc-user-roles">
<legend>Projects (none checked = all projects)</legend> <legend>Roles (none checked = all projects)</legend>
{#each projects as project (project.key)} {#each roles as role (role.id)}
<label class="sc-user-project-option"> <label class="sc-user-role-option">
<input <input
type="checkbox" type="checkbox"
checked={(editUser.projects ?? []).includes(project.key)} checked={(editUser.roleIds ?? []).includes(role.id)}
onchange={(event) => toggleEditProject(project.key, event.currentTarget.checked)} onchange={(event) => toggleEditRole(role.id, event.currentTarget.checked)}
/> />
{project.label} {role.name}
</label> </label>
{/each} {/each}
</fieldset> </fieldset>
@@ -263,8 +260,8 @@
<div><strong>{user.username}</strong><span>{user.email}</span></div> <div><strong>{user.username}</strong><span>{user.email}</span></div>
<span class="sc-user-role">{user.role}</span> <span class="sc-user-role">{user.role}</span>
<span class="sc-muted sc-user-last-login" title="Last login">{formatLastLogin(user.lastLoginAt)}</span> <span class="sc-muted sc-user-last-login" title="Last login">{formatLastLogin(user.lastLoginAt)}</span>
{#if projects && projects.length > 0} {#if roles && roles.length > 0}
<span class="sc-muted sc-user-projects-summary" title="Project access">{projectSummary(user)}</span> <span class="sc-muted sc-user-roles-summary" title="Roles">{roleSummary(user)}</span>
{/if} {/if}
<div class="sc-user-actions"> <div class="sc-user-actions">
<Button disabled={saving} size="sm" type="button" variant="secondary" onclick={() => startEditing(user)}>Edit</Button> <Button disabled={saving} size="sm" type="button" variant="secondary" onclick={() => startEditing(user)}>Edit</Button>
+1
View File
@@ -41,6 +41,7 @@ export type {
AuthSetupInput, AuthSetupInput,
ContentSnippet, ContentSnippet,
KnownProject, KnownProject,
KnownRole,
NavItem, NavItem,
ProfileUpdateInput, ProfileUpdateInput,
ManagedUser, ManagedUser,
+4 -4
View File
@@ -809,7 +809,7 @@ html[data-wide-layout='true'] .sc-header {
gap: 0.4rem; gap: 0.4rem;
} }
.sc-user-projects-summary { .sc-user-roles-summary {
max-width: 12rem; max-width: 12rem;
overflow: hidden; overflow: hidden;
font-size: 0.8rem; font-size: 0.8rem;
@@ -817,7 +817,7 @@ html[data-wide-layout='true'] .sc-header {
white-space: nowrap; white-space: nowrap;
} }
.sc-user-projects { .sc-user-roles {
display: flex; display: flex;
flex: 1 1 100%; flex: 1 1 100%;
flex-wrap: wrap; flex-wrap: wrap;
@@ -828,14 +828,14 @@ html[data-wide-layout='true'] .sc-header {
border-radius: 0.5rem; border-radius: 0.5rem;
} }
.sc-user-projects legend { .sc-user-roles legend {
flex-basis: 100%; flex-basis: 100%;
margin-bottom: 0.3rem; margin-bottom: 0.3rem;
color: var(--sc-muted-fg); color: var(--sc-muted-fg);
font-size: 0.8rem; font-size: 0.8rem;
} }
.sc-user-project-option { .sc-user-role-option {
display: flex; display: flex;
align-items: center; align-items: center;
gap: 0.35rem; gap: 0.35rem;
+20 -14
View File
@@ -59,20 +59,26 @@ export interface ManagedUser {
/** Null/undefined means the account has never completed a login. */ /** Null/undefined means the account has never completed a login. */
lastLoginAt?: string | null; lastLoginAt?: string | null;
/** /**
* Project keys this account is restricted to logging into (AuthGuard's * Ids of the custom roles this account belongs to. Those roles decide which
* project-scope feature); an empty array (never undefined, when the * projects the account may log into (AuthGuard's role-based project access);
* backend supports this feature at all) means unrestricted. Undefined on * an empty array means no custom role, which keeps every project open.
* a backend that doesn't report it at all. * Undefined on a backend that doesn't report roles at all.
*/ */
projects?: string[]; roleIds?: number[];
} }
/** One project an administrator may restrict a user's login access to - see UserManagement's `projects` prop. */ /** One registered project an administrator can pick from. */
export interface KnownProject { export interface KnownProject {
key: string; key: string;
label: string; label: string;
} }
/** One custom role an administrator may assign to a user - see UserManagement's `roles` prop. */
export interface KnownRole {
id: number;
name: string;
}
/** /**
* username/email are plain (never undefined) so <input> bindings stay simple * username/email are plain (never undefined) so <input> bindings stay simple
* -- an empty string, not an absent key, is how this form represents "not * -- an empty string, not an absent key, is how this form represents "not
@@ -86,11 +92,11 @@ export interface UserCreateInput {
password: string; password: string;
role?: UserRole; role?: UserRole;
/** /**
* Project keys this new account is restricted to. An empty array means * Ids of the custom roles this new account belongs to. An empty array means
* unrestricted; omitted when the host application does not expose project * no custom role (every project); omitted when the host application does not
* scoping. * expose roles.
*/ */
projects?: string[]; roleIds?: number[];
} }
export interface UserUpdateInput { export interface UserUpdateInput {
@@ -99,11 +105,11 @@ export interface UserUpdateInput {
password?: string; password?: string;
role?: UserRole; role?: UserRole;
/** /**
* Replaces the account's full project-access list when present (an empty * Replaces the account's full set of custom roles when present (an empty
* array clears any restriction); omitted (undefined) leaves it untouched. * array removes it from every role); omitted (undefined) leaves them
* Only ever set when UserManagement was given a `projects` prop. * untouched. Only ever set when UserManagement was given a `roles` prop.
*/ */
projects?: string[]; roleIds?: number[];
} }
export type UserListRequest = () => ManagedUser[] | Promise<ManagedUser[]>; export type UserListRequest = () => ManagedUser[] | Promise<ManagedUser[]>;
+4 -4
View File
@@ -811,7 +811,7 @@ html[data-wide-layout='true'] .sc-header {
gap: 0.4rem; gap: 0.4rem;
} }
.sc-user-projects-summary { .sc-user-roles-summary {
max-width: 12rem; max-width: 12rem;
overflow: hidden; overflow: hidden;
font-size: 0.8rem; font-size: 0.8rem;
@@ -819,7 +819,7 @@ html[data-wide-layout='true'] .sc-header {
white-space: nowrap; white-space: nowrap;
} }
.sc-user-projects { .sc-user-roles {
display: flex; display: flex;
flex: 1 1 100%; flex: 1 1 100%;
flex-wrap: wrap; flex-wrap: wrap;
@@ -830,14 +830,14 @@ html[data-wide-layout='true'] .sc-header {
border-radius: 0.5rem; border-radius: 0.5rem;
} }
.sc-user-projects legend { .sc-user-roles legend {
flex-basis: 100%; flex-basis: 100%;
margin-bottom: 0.3rem; margin-bottom: 0.3rem;
color: var(--sc-muted-fg); color: var(--sc-muted-fg);
font-size: 0.8rem; font-size: 0.8rem;
} }
.sc-user-project-option { .sc-user-role-option {
display: flex; display: flex;
align-items: center; align-items: center;
gap: 0.35rem; gap: 0.35rem;