feat: assign custom roles in user management
This commit is contained in:
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>
|
||||
|
||||
Reference in new issue
Block a user