2026-08-05 14:57:01 +02:00
|
|
|
<script lang="ts">
|
|
|
|
|
import type {
|
|
|
|
|
ManagedUser,
|
|
|
|
|
UserCreateInput,
|
|
|
|
|
UserCreateRequest,
|
|
|
|
|
UserDeleteRequest,
|
|
|
|
|
UserId,
|
|
|
|
|
UserListRequest,
|
|
|
|
|
UserRole,
|
|
|
|
|
UserUpdateInput,
|
|
|
|
|
UserUpdateRequest
|
|
|
|
|
} from '../types';
|
|
|
|
|
import Button from './Button.svelte';
|
|
|
|
|
|
|
|
|
|
let {
|
|
|
|
|
currentRole = 'user',
|
|
|
|
|
currentUserId,
|
|
|
|
|
onCreate,
|
|
|
|
|
onDelete,
|
|
|
|
|
onList,
|
|
|
|
|
onUpdate
|
|
|
|
|
}: {
|
|
|
|
|
currentRole?: UserRole;
|
|
|
|
|
currentUserId?: UserId;
|
|
|
|
|
onCreate?: UserCreateRequest;
|
|
|
|
|
onDelete?: UserDeleteRequest;
|
|
|
|
|
onList?: UserListRequest;
|
|
|
|
|
onUpdate?: UserUpdateRequest;
|
|
|
|
|
} = $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>({});
|
|
|
|
|
|
|
|
|
|
const canManage = $derived(currentRole === 'admin' || currentRole === 'superadmin');
|
|
|
|
|
const canAssignAdmin = $derived(currentRole === 'superadmin');
|
|
|
|
|
|
|
|
|
|
function roleForForm(role: UserRole | undefined): UserRole {
|
|
|
|
|
return canAssignAdmin && role ? role : 'user';
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-04 15:02:16 +02:00
|
|
|
function formatLastLogin(value: string | null | undefined): string {
|
|
|
|
|
if (!value) return 'Never';
|
|
|
|
|
const parsed = new Date(value);
|
|
|
|
|
return Number.isNaN(parsed.getTime()) ? 'Never' : parsed.toLocaleString();
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-05 14:57:01 +02:00
|
|
|
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;
|
|
|
|
|
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) };
|
|
|
|
|
error = '';
|
|
|
|
|
notice = '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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" required type="text" bind:value={newUser.username} />
|
|
|
|
|
<input aria-label="New email" class="sc-input" placeholder="Email" required type="email" bind:value={newUser.email} />
|
|
|
|
|
<input aria-label="New password" class="sc-input" minlength="8" placeholder="Password" required 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>
|
|
|
|
|
<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 users 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>
|
|
|
|
|
<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>
|
2026-09-04 15:02:16 +02:00
|
|
|
<span class="sc-muted sc-user-last-login" title="Last login">{formatLastLogin(user.lastLoginAt)}</span>
|
2026-08-05 14:57:01 +02:00
|
|
|
<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>
|
|
|
|
|
{/if}
|
|
|
|
|
</section>
|
|
|
|
|
{/if}
|