Fix critical and high audit findings
Fixed findings from the audit reports (SECURITY, QUALITY, PERFORMANCE): - P-001 · Dashboard re-downloads the entire dataset every 5 seconds Re-synced the vendored shared/ snapshot from shared commit 199a5ef. Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
7dd0d34383
commit
a84a36349b
31 files changed
+653
-6055
No files matched your search
@@ -1,3 +1,6 @@
|
||||
<!--
|
||||
Internal to MediaPlayer. Not exported from the package barrel; do not re-export.
|
||||
-->
|
||||
<script lang="ts">
|
||||
import { Maximize2, Minimize2, Pause, Play, Settings, Volume2, VolumeX } from '@lucide/svelte'
|
||||
import { onMount } from 'svelte'
|
||||
|
||||
@@ -1,286 +0,0 @@
|
||||
<script lang="ts">
|
||||
import type {
|
||||
KnownRole,
|
||||
ManagedUser,
|
||||
UserCreateInput,
|
||||
UserCreateRequest,
|
||||
UserDeleteRequest,
|
||||
UserId,
|
||||
UserListRequest,
|
||||
UserRole,
|
||||
UserUpdateInput,
|
||||
UserUpdateRequest
|
||||
} from '../types';
|
||||
import Button from './Button.svelte';
|
||||
import Pagination from './Pagination.svelte';
|
||||
|
||||
let {
|
||||
currentRole = 'user',
|
||||
currentUserId,
|
||||
onCreate,
|
||||
onDelete,
|
||||
onList,
|
||||
onUpdate,
|
||||
roles
|
||||
}: {
|
||||
currentRole?: UserRole;
|
||||
currentUserId?: UserId;
|
||||
onCreate?: UserCreateRequest;
|
||||
onDelete?: UserDeleteRequest;
|
||||
onList?: UserListRequest;
|
||||
onUpdate?: UserUpdateRequest;
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
roles?: KnownRole[];
|
||||
} = $props();
|
||||
|
||||
let users = $state<ManagedUser[]>([]);
|
||||
let loading = $state(false);
|
||||
let saving = $state(false);
|
||||
let error = $state('');
|
||||
let notice = $state('');
|
||||
let loaded = $state(false);
|
||||
let newUser = $state<UserCreateInput>({ username: '', email: '', password: '', role: 'user' });
|
||||
let editingId = $state<UserId | null>(null);
|
||||
let editUser = $state<UserUpdateInput>({});
|
||||
let page = $state(1);
|
||||
let pageSize = $state(24);
|
||||
|
||||
const canManage = $derived(currentRole === 'admin' || currentRole === 'superadmin');
|
||||
const canAssignAdmin = $derived(currentRole === 'superadmin');
|
||||
const pageCount = $derived(Math.max(1, Math.ceil(users.length / pageSize)));
|
||||
const currentPage = $derived(Math.min(page, pageCount));
|
||||
const pagedUsers = $derived(
|
||||
users.slice((currentPage - 1) * pageSize, (currentPage - 1) * pageSize + pageSize)
|
||||
);
|
||||
|
||||
function roleForForm(role: UserRole | undefined): UserRole {
|
||||
return canAssignAdmin && role ? role : 'user';
|
||||
}
|
||||
|
||||
function formatLastLogin(value: string | null | undefined): string {
|
||||
if (!value) return 'Never';
|
||||
const parsed = new Date(value);
|
||||
return Number.isNaN(parsed.getTime()) ? 'Never' : parsed.toLocaleString();
|
||||
}
|
||||
|
||||
async function loadUsers(): Promise<void> {
|
||||
if (!onList || !canManage) return;
|
||||
loading = true;
|
||||
error = '';
|
||||
try {
|
||||
users = await onList();
|
||||
loaded = true;
|
||||
} catch (caught) {
|
||||
error = caught instanceof Error ? caught.message : 'Unable to load users.';
|
||||
} finally {
|
||||
loading = false;
|
||||
}
|
||||
}
|
||||
|
||||
$effect(() => {
|
||||
if (canManage && onList && !loaded) void loadUsers();
|
||||
});
|
||||
|
||||
async function createUser(event: SubmitEvent): Promise<void> {
|
||||
event.preventDefault();
|
||||
if (!onCreate) return;
|
||||
if (!newUser.username.trim() && !newUser.email.trim()) {
|
||||
error = 'A username or email is required.';
|
||||
return;
|
||||
}
|
||||
saving = true;
|
||||
error = '';
|
||||
notice = '';
|
||||
try {
|
||||
await onCreate({ ...newUser, role: roleForForm(newUser.role) });
|
||||
newUser = { username: '', email: '', password: '', role: 'user' };
|
||||
notice = 'User created.';
|
||||
await loadUsers();
|
||||
} catch (caught) {
|
||||
error = caught instanceof Error ? caught.message : 'Unable to create user.';
|
||||
} finally {
|
||||
saving = false;
|
||||
}
|
||||
}
|
||||
|
||||
function startEditing(user: ManagedUser): void {
|
||||
editingId = user.id;
|
||||
editUser = { username: user.username, email: user.email, role: roleForForm(user.role) };
|
||||
if (roles && roles.length > 0) editUser.roleIds = [...(user.roleIds ?? [])];
|
||||
error = '';
|
||||
notice = '';
|
||||
}
|
||||
|
||||
function toggleEditRole(id: number, checked: boolean): void {
|
||||
const current = editUser.roleIds ?? [];
|
||||
editUser.roleIds = checked ? [...current, id] : current.filter((roleId) => roleId !== id);
|
||||
}
|
||||
|
||||
function toggleNewRole(id: number, checked: boolean): void {
|
||||
const current = newUser.roleIds ?? [];
|
||||
newUser.roleIds = checked ? [...current, id] : current.filter((roleId) => roleId !== id);
|
||||
}
|
||||
|
||||
// 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 {
|
||||
editingId = null;
|
||||
editUser = {};
|
||||
}
|
||||
|
||||
async function updateUser(event: SubmitEvent): Promise<void> {
|
||||
event.preventDefault();
|
||||
if (!onUpdate || editingId === null) return;
|
||||
saving = true;
|
||||
error = '';
|
||||
notice = '';
|
||||
try {
|
||||
const input = { ...editUser, role: roleForForm(editUser.role) };
|
||||
if (!input.password) delete input.password;
|
||||
await onUpdate(editingId, input);
|
||||
stopEditing();
|
||||
notice = 'User updated.';
|
||||
await loadUsers();
|
||||
} catch (caught) {
|
||||
error = caught instanceof Error ? caught.message : 'Unable to update user.';
|
||||
} finally {
|
||||
saving = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function deleteUser(user: ManagedUser): Promise<void> {
|
||||
if (!onDelete || String(user.id) === String(currentUserId)) return;
|
||||
if (!window.confirm(`Delete user ${user.username}?`)) return;
|
||||
saving = true;
|
||||
error = '';
|
||||
notice = '';
|
||||
try {
|
||||
await onDelete(user.id);
|
||||
notice = 'User deleted.';
|
||||
await loadUsers();
|
||||
} catch (caught) {
|
||||
error = caught instanceof Error ? caught.message : 'Unable to delete user.';
|
||||
} finally {
|
||||
saving = false;
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
{#if canManage && onList && onCreate && onUpdate && onDelete}
|
||||
<section class="sc-user-management" aria-labelledby="sc-user-management-title">
|
||||
<div class="sc-user-management-header">
|
||||
<div>
|
||||
<h3 id="sc-user-management-title">User management</h3>
|
||||
<p class="sc-muted">Add, edit, or remove accounts for this project.</p>
|
||||
</div>
|
||||
<Button disabled={loading || saving} size="sm" type="button" variant="secondary" onclick={() => void loadUsers()}>
|
||||
Refresh
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
<form class="sc-user-form" onsubmit={createUser}>
|
||||
<strong>Add user</strong>
|
||||
<div class="sc-user-grid">
|
||||
<input aria-label="New username" class="sc-input" placeholder="Username (optional if email is set)" type="text" bind:value={newUser.username} />
|
||||
<input aria-label="New email" class="sc-input" placeholder="Email (optional if username is set)" type="email" bind:value={newUser.email} />
|
||||
<input aria-label="New password" class="sc-input" minlength="8" placeholder="Password (optional)" type="password" bind:value={newUser.password} />
|
||||
<select aria-label="New role" class="sc-input" disabled={!canAssignAdmin} bind:value={newUser.role}>
|
||||
<option value="user">User</option>
|
||||
{#if canAssignAdmin}<option value="admin">Admin</option>{/if}
|
||||
</select>
|
||||
</div>
|
||||
{#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.roleIds ?? []).includes(role.id)}
|
||||
onchange={(event) => toggleNewRole(role.id, event.currentTarget.checked)}
|
||||
/>
|
||||
{role.name}
|
||||
</label>
|
||||
{/each}
|
||||
</fieldset>
|
||||
{/if}
|
||||
<div class="sc-form-actions"><Button disabled={saving} type="submit">Add user</Button></div>
|
||||
</form>
|
||||
|
||||
{#if error}<p aria-live="polite" class="sc-error">{error}</p>{/if}
|
||||
{#if notice}<p aria-live="polite" class="sc-success">{notice}</p>{/if}
|
||||
|
||||
{#if loading && users.length === 0}
|
||||
<p class="sc-muted">Loading users...</p>
|
||||
{:else if users.length === 0}
|
||||
<p class="sc-muted">No users found.</p>
|
||||
{:else}
|
||||
<div class="sc-user-list">
|
||||
{#each pagedUsers as user (user.id)}
|
||||
{#if String(editingId) === String(user.id)}
|
||||
<form class="sc-user-row sc-user-edit-row" onsubmit={updateUser}>
|
||||
<input aria-label="Username" class="sc-input" required type="text" bind:value={editUser.username} />
|
||||
<input aria-label="Email" class="sc-input" required type="email" bind:value={editUser.email} />
|
||||
<input aria-label="New password" class="sc-input" minlength="8" placeholder="Keep password" type="password" bind:value={editUser.password} />
|
||||
<select aria-label="Role" class="sc-input" disabled={!canAssignAdmin || user.role === 'superadmin'} bind:value={editUser.role}>
|
||||
<option value="user">User</option>
|
||||
{#if canAssignAdmin}<option value="admin">Admin</option>{/if}
|
||||
{#if user.role === 'superadmin'}<option value="superadmin">Superadmin</option>{/if}
|
||||
</select>
|
||||
{#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.roleIds ?? []).includes(role.id)}
|
||||
onchange={(event) => toggleEditRole(role.id, event.currentTarget.checked)}
|
||||
/>
|
||||
{role.name}
|
||||
</label>
|
||||
{/each}
|
||||
</fieldset>
|
||||
{/if}
|
||||
<div class="sc-user-actions"><Button disabled={saving} size="sm" type="submit">Save</Button><Button disabled={saving} size="sm" type="button" variant="secondary" onclick={stopEditing}>Cancel</Button></div>
|
||||
</form>
|
||||
{:else}
|
||||
<div class="sc-user-row">
|
||||
<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 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>
|
||||
<Button disabled={saving || String(user.id) === String(currentUserId) || user.role === 'superadmin'} size="sm" type="button" variant="danger" onclick={() => void deleteUser(user)}>Remove</Button>
|
||||
</div>
|
||||
</div>
|
||||
{/if}
|
||||
{/each}
|
||||
</div>
|
||||
<Pagination
|
||||
page={currentPage}
|
||||
{pageSize}
|
||||
pageLength={pagedUsers.length}
|
||||
totalLength={users.length}
|
||||
onPaging={(change) => {
|
||||
page = change.page;
|
||||
pageSize = change.pageSize;
|
||||
}}
|
||||
/>
|
||||
{/if}
|
||||
</section>
|
||||
{/if}
|
||||
Reference in new issue
Block a user