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

@@ -1,6 +1,6 @@
<script lang="ts">
import type {
KnownProject,
KnownRole,
ManagedUser,
UserCreateInput,
UserCreateRequest,
@@ -21,7 +21,7 @@
onDelete,
onList,
onUpdate,
projects
roles
}: {
currentRole?: UserRole;
currentUserId?: UserId;
@@ -30,14 +30,13 @@
onList?: UserListRequest;
onUpdate?: UserUpdateRequest;
/**
* Every project an account can be restricted to logging into
* (AuthGuard's project-scope feature). Only AuthGuard's own admin UI
* passes this - every other project's UserManagement usage (proxying to
* AuthGuard's admin/users endpoints without knowing about registered
* projects) omits it, and the whole project-access section of this
* The custom roles an account can be placed in (AuthGuard's role-based
* project access: a role decides which projects its members may log into).
* Only AuthGuard's own admin UI passes this - every other project's
* UserManagement usage omits it, and the whole role section of this
* component simply doesn't render.
*/
projects?: KnownProject[];
roles?: KnownRole[];
} = $props();
let users = $state<ManagedUser[]>([]);
@@ -113,28 +112,26 @@
function startEditing(user: ManagedUser): void {
editingId = user.id;
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 = '';
notice = '';
}
function toggleEditProject(key: string, checked: boolean): void {
const current = editUser.projects ?? [];
editUser.projects = checked ? [...current, key] : current.filter((project) => project !== key);
function toggleEditRole(id: number, checked: boolean): void {
const current = editUser.roleIds ?? [];
editUser.roleIds = checked ? [...current, id] : current.filter((roleId) => roleId !== id);
}
function toggleNewProject(key: string, checked: boolean): void {
const current = newUser.projects ?? [];
newUser.projects = checked ? [...current, key] : current.filter((project) => project !== key);
function toggleNewRole(id: number, checked: boolean): void {
const current = newUser.roleIds ?? [];
newUser.roleIds = checked ? [...current, id] : current.filter((roleId) => roleId !== id);
}
function projectLabel(key: string): string {
return projects?.find((project) => project.key === key)?.label ?? key;
}
function projectSummary(user: ManagedUser): string {
if (!user.projects || user.projects.length === 0) return 'All projects';
return user.projects.map(projectLabel).join(', ');
// Names the roles an account is in, in the order the roles are listed. An account in none of
// them keeps every project.
function roleSummary(user: ManagedUser): string {
const names = (roles ?? []).filter((role) => (user.roleIds ?? []).includes(role.id)).map((role) => role.name);
return names.length > 0 ? names.join(', ') : 'All projects';
}
function stopEditing(): void {
@@ -203,17 +200,17 @@
{#if canAssignAdmin}<option value="admin">Admin</option>{/if}
</select>
</div>
{#if projects && projects.length > 0}
<fieldset class="sc-user-projects">
<legend>Projects (none checked = all projects)</legend>
{#each projects as project (project.key)}
<label class="sc-user-project-option">
{#if roles && roles.length > 0}
<fieldset class="sc-user-roles">
<legend>Roles (none checked = all projects)</legend>
{#each roles as role (role.id)}
<label class="sc-user-role-option">
<input
type="checkbox"
checked={(newUser.projects ?? []).includes(project.key)}
onchange={(event) => toggleNewProject(project.key, event.currentTarget.checked)}
checked={(newUser.roleIds ?? []).includes(role.id)}
onchange={(event) => toggleNewRole(role.id, event.currentTarget.checked)}
/>
{project.label}
{role.name}
</label>
{/each}
</fieldset>
@@ -241,17 +238,17 @@
{#if canAssignAdmin}<option value="admin">Admin</option>{/if}
{#if user.role === 'superadmin'}<option value="superadmin">Superadmin</option>{/if}
</select>
{#if projects && projects.length > 0}
<fieldset class="sc-user-projects">
<legend>Projects (none checked = all projects)</legend>
{#each projects as project (project.key)}
<label class="sc-user-project-option">
{#if roles && roles.length > 0}
<fieldset class="sc-user-roles">
<legend>Roles (none checked = all projects)</legend>
{#each roles as role (role.id)}
<label class="sc-user-role-option">
<input
type="checkbox"
checked={(editUser.projects ?? []).includes(project.key)}
onchange={(event) => toggleEditProject(project.key, event.currentTarget.checked)}
checked={(editUser.roleIds ?? []).includes(role.id)}
onchange={(event) => toggleEditRole(role.id, event.currentTarget.checked)}
/>
{project.label}
{role.name}
</label>
{/each}
</fieldset>
@@ -263,8 +260,8 @@
<div><strong>{user.username}</strong><span>{user.email}</span></div>
<span class="sc-user-role">{user.role}</span>
<span class="sc-muted sc-user-last-login" title="Last login">{formatLastLogin(user.lastLoginAt)}</span>
{#if projects && projects.length > 0}
<span class="sc-muted sc-user-projects-summary" title="Project access">{projectSummary(user)}</span>
{#if roles && roles.length > 0}
<span class="sc-muted sc-user-roles-summary" title="Roles">{roleSummary(user)}</span>
{/if}
<div class="sc-user-actions">
<Button disabled={saving} size="sm" type="button" variant="secondary" onclick={() => startEditing(user)}>Edit</Button>
+1
View File
@@ -41,6 +41,7 @@ export type {
AuthSetupInput,
ContentSnippet,
KnownProject,
KnownRole,
NavItem,
ProfileUpdateInput,
ManagedUser,
+4 -4
View File
@@ -809,7 +809,7 @@ html[data-wide-layout='true'] .sc-header {
gap: 0.4rem;
}
.sc-user-projects-summary {
.sc-user-roles-summary {
max-width: 12rem;
overflow: hidden;
font-size: 0.8rem;
@@ -817,7 +817,7 @@ html[data-wide-layout='true'] .sc-header {
white-space: nowrap;
}
.sc-user-projects {
.sc-user-roles {
display: flex;
flex: 1 1 100%;
flex-wrap: wrap;
@@ -828,14 +828,14 @@ html[data-wide-layout='true'] .sc-header {
border-radius: 0.5rem;
}
.sc-user-projects legend {
.sc-user-roles legend {
flex-basis: 100%;
margin-bottom: 0.3rem;
color: var(--sc-muted-fg);
font-size: 0.8rem;
}
.sc-user-project-option {
.sc-user-role-option {
display: flex;
align-items: center;
gap: 0.35rem;
+20 -14
View File
@@ -59,20 +59,26 @@ export interface ManagedUser {
/** Null/undefined means the account has never completed a login. */
lastLoginAt?: string | null;
/**
* Project keys this account is restricted to logging into (AuthGuard's
* project-scope feature); an empty array (never undefined, when the
* backend supports this feature at all) means unrestricted. Undefined on
* a backend that doesn't report it at all.
* Ids of the custom roles this account belongs to. Those roles decide which
* projects the account may log into (AuthGuard's role-based project access);
* an empty array means no custom role, which keeps every project open.
* 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 {
key: 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
* -- an empty string, not an absent key, is how this form represents "not
@@ -86,11 +92,11 @@ export interface UserCreateInput {
password: string;
role?: UserRole;
/**
* Project keys this new account is restricted to. An empty array means
* unrestricted; omitted when the host application does not expose project
* scoping.
* Ids of the custom roles this new account belongs to. An empty array means
* no custom role (every project); omitted when the host application does not
* expose roles.
*/
projects?: string[];
roleIds?: number[];
}
export interface UserUpdateInput {
@@ -99,11 +105,11 @@ export interface UserUpdateInput {
password?: string;
role?: UserRole;
/**
* Replaces the account's full project-access list when present (an empty
* array clears any restriction); omitted (undefined) leaves it untouched.
* Only ever set when UserManagement was given a `projects` prop.
* Replaces the account's full set of custom roles when present (an empty
* array removes it from every role); omitted (undefined) leaves them
* untouched. Only ever set when UserManagement was given a `roles` prop.
*/
projects?: string[];
roleIds?: number[];
}
export type UserListRequest = () => ManagedUser[] | Promise<ManagedUser[]>;