feat: add admin dialog and move user management out of settings/profile modals
- Add a dedicated admin entry (with icon) to the header profile dropdown, visible only to admins - Move all user-management settings from SettingsDialog/ProfileDialog into a new AdminDialog - Export AdminDialog from the package index Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
c94f749430
commit
a53bafff3f
6 files changed
+113
-104
No files matched your search
@@ -0,0 +1,86 @@
|
||||
<script lang="ts">
|
||||
import type {
|
||||
UserCreateRequest,
|
||||
UserDeleteRequest,
|
||||
UserId,
|
||||
UserListRequest,
|
||||
UserRole,
|
||||
UserUpdateRequest
|
||||
} from '../types';
|
||||
import Dialog from './Dialog.svelte';
|
||||
import UserManagement from './UserManagement.svelte';
|
||||
|
||||
let {
|
||||
currentRole = 'user',
|
||||
currentUserId,
|
||||
description = 'Manage accounts and access for this application.',
|
||||
loginRequired = true,
|
||||
onCreateUser,
|
||||
onDeleteUser,
|
||||
onLoginRequiredChange,
|
||||
onListUsers,
|
||||
onUpdateUser,
|
||||
open = $bindable(false),
|
||||
title = 'Admin'
|
||||
}: {
|
||||
currentRole?: UserRole;
|
||||
currentUserId?: UserId;
|
||||
description?: string;
|
||||
loginRequired?: boolean;
|
||||
onCreateUser?: UserCreateRequest;
|
||||
onDeleteUser?: UserDeleteRequest;
|
||||
onLoginRequiredChange?: (required: boolean) => void | Promise<void>;
|
||||
onListUsers?: UserListRequest;
|
||||
onUpdateUser?: UserUpdateRequest;
|
||||
open?: boolean;
|
||||
title?: string;
|
||||
} = $props();
|
||||
|
||||
let loginRequiredValue = $state<boolean>(true);
|
||||
let loginSettingError = $state('');
|
||||
|
||||
$effect(() => {
|
||||
loginRequiredValue = loginRequired;
|
||||
});
|
||||
|
||||
async function updateLoginRequired(required: boolean) {
|
||||
const previous = loginRequiredValue;
|
||||
loginRequiredValue = required;
|
||||
loginSettingError = '';
|
||||
try {
|
||||
await onLoginRequiredChange?.(required);
|
||||
} catch (error) {
|
||||
loginRequiredValue = previous;
|
||||
loginSettingError = error instanceof Error ? error.message : 'The setting could not be saved.';
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<Dialog bind:open {description} {title}>
|
||||
{#if onLoginRequiredChange}
|
||||
<div class="sc-field">
|
||||
<label class="flex items-center gap-3">
|
||||
<input
|
||||
class="size-4 accent-primary"
|
||||
type="checkbox"
|
||||
checked={loginRequiredValue}
|
||||
onchange={(event) => updateLoginRequired((event.currentTarget as HTMLInputElement).checked)}
|
||||
/>
|
||||
<span>
|
||||
<span class="block font-medium">Require login to access app</span>
|
||||
<span class="sc-muted">Admins and superadmins can change this setting.</span>
|
||||
</span>
|
||||
</label>
|
||||
{#if loginSettingError}<p class="sc-error" role="alert">{loginSettingError}</p>{/if}
|
||||
</div>
|
||||
{/if}
|
||||
|
||||
<UserManagement
|
||||
currentRole={currentRole}
|
||||
currentUserId={currentUserId}
|
||||
onCreate={onCreateUser}
|
||||
onDelete={onDeleteUser}
|
||||
onList={onListUsers}
|
||||
onUpdate={onUpdateUser}
|
||||
/>
|
||||
</Dialog>
|
||||
@@ -15,6 +15,7 @@
|
||||
headerActions,
|
||||
navItems = [],
|
||||
onAbout,
|
||||
onAdmin,
|
||||
onLogout,
|
||||
onNavigate,
|
||||
onProfile,
|
||||
@@ -34,6 +35,7 @@
|
||||
headerActions?: Snippet;
|
||||
navItems?: NavItem[];
|
||||
onAbout?: () => void | Promise<void>;
|
||||
onAdmin?: () => void | Promise<void>;
|
||||
onLogout?: () => void | Promise<void>;
|
||||
onNavigate?: (item: NavItem) => void;
|
||||
onProfile?: () => void;
|
||||
@@ -61,6 +63,7 @@
|
||||
actions={headerActions}
|
||||
{navItems}
|
||||
{onAbout}
|
||||
{onAdmin}
|
||||
{onLogout}
|
||||
{onNavigate}
|
||||
{onProfile}
|
||||
|
||||
@@ -100,6 +100,7 @@
|
||||
navItems = [],
|
||||
actions,
|
||||
onAbout,
|
||||
onAdmin,
|
||||
onLogout,
|
||||
onNavigate,
|
||||
onProfile,
|
||||
@@ -115,6 +116,7 @@
|
||||
navItems?: NavItem[];
|
||||
actions?: Snippet;
|
||||
onAbout?: () => void | Promise<void>;
|
||||
onAdmin?: () => void | Promise<void>;
|
||||
onLogout?: () => void | Promise<void>;
|
||||
onNavigate?: (item: NavItem) => void;
|
||||
onProfile?: () => void | Promise<void>;
|
||||
@@ -126,8 +128,12 @@
|
||||
let navOpen = $state(false);
|
||||
let profileOpen = $state(false);
|
||||
|
||||
// onAdmin is only ever passed by the caller when the signed-in user's role qualifies, so its
|
||||
// presence alone is enough to decide visibility here - same as onProfile/onLogout below.
|
||||
const hasProfileMenu = $derived(
|
||||
Boolean(username && (onProfile || (showSettings && onSettings) || onAbout || aboutHref || onLogout)),
|
||||
Boolean(
|
||||
username && (onProfile || onAdmin || (showSettings && onSettings) || onAbout || aboutHref || onLogout),
|
||||
),
|
||||
);
|
||||
|
||||
function closeMenus(): void {
|
||||
@@ -219,6 +225,21 @@
|
||||
</button>
|
||||
{/if}
|
||||
|
||||
{#if onAdmin}
|
||||
<button
|
||||
class="sc-profile-menu-item"
|
||||
role="menuitem"
|
||||
type="button"
|
||||
onclick={() => {
|
||||
profileOpen = false;
|
||||
void onAdmin?.();
|
||||
}}
|
||||
>
|
||||
<ShieldCheck aria-hidden="true" size={15} strokeWidth={2} />
|
||||
Admin
|
||||
</button>
|
||||
{/if}
|
||||
|
||||
{#if showSettings && onSettings}
|
||||
<button
|
||||
class="sc-profile-menu-item"
|
||||
|
||||
@@ -1,51 +1,27 @@
|
||||
<script lang="ts">
|
||||
import type {
|
||||
ProfileUpdateInput,
|
||||
TotpSetupRequest,
|
||||
TotpStatusRequest,
|
||||
TotpTokenAction,
|
||||
UserCreateRequest,
|
||||
UserDeleteRequest,
|
||||
UserId,
|
||||
UserListRequest,
|
||||
UserRole,
|
||||
UserUpdateRequest
|
||||
} from '../types';
|
||||
import type { ProfileUpdateInput, TotpSetupRequest, TotpStatusRequest, TotpTokenAction } from '../types';
|
||||
import Button from './Button.svelte';
|
||||
import Dialog from './Dialog.svelte';
|
||||
import TotpSettings from './TotpSettings.svelte';
|
||||
import UserManagement from './UserManagement.svelte';
|
||||
|
||||
let {
|
||||
currentEmail = '',
|
||||
currentRole = 'user',
|
||||
currentUserId,
|
||||
onCreateUser,
|
||||
onDeleteUser,
|
||||
onListUsers,
|
||||
currentUsername = '',
|
||||
onSave,
|
||||
onTotpDisable,
|
||||
onTotpEnable,
|
||||
onTotpSetup,
|
||||
onTotpStatus,
|
||||
onUpdateUser,
|
||||
totpEnabled = false,
|
||||
open = $bindable(false)
|
||||
}: {
|
||||
currentEmail?: string;
|
||||
currentRole?: UserRole;
|
||||
currentUserId?: UserId;
|
||||
onCreateUser?: UserCreateRequest;
|
||||
onDeleteUser?: UserDeleteRequest;
|
||||
onListUsers?: UserListRequest;
|
||||
currentUsername?: string;
|
||||
onSave?: (input: ProfileUpdateInput) => void | Promise<void>;
|
||||
onTotpDisable?: TotpTokenAction;
|
||||
onTotpEnable?: TotpTokenAction;
|
||||
onTotpSetup?: TotpSetupRequest;
|
||||
onTotpStatus?: TotpStatusRequest;
|
||||
onUpdateUser?: UserUpdateRequest;
|
||||
totpEnabled?: boolean;
|
||||
open?: boolean;
|
||||
} = $props();
|
||||
@@ -158,13 +134,4 @@
|
||||
enabled={totpEnabled}
|
||||
/>
|
||||
{/if}
|
||||
|
||||
<UserManagement
|
||||
currentRole={currentRole}
|
||||
currentUserId={currentUserId}
|
||||
onCreate={onCreateUser}
|
||||
onDelete={onDeleteUser}
|
||||
onList={onListUsers}
|
||||
onUpdate={onUpdateUser}
|
||||
/>
|
||||
</Dialog>
|
||||
@@ -1,93 +1,24 @@
|
||||
<script lang="ts">
|
||||
import type { Snippet } from 'svelte';
|
||||
import type {
|
||||
UserCreateRequest,
|
||||
UserDeleteRequest,
|
||||
UserId,
|
||||
UserListRequest,
|
||||
UserRole,
|
||||
UserUpdateRequest
|
||||
} from '../types';
|
||||
import Dialog from './Dialog.svelte';
|
||||
import UserManagement from './UserManagement.svelte';
|
||||
|
||||
let {
|
||||
children,
|
||||
description = '',
|
||||
currentRole = 'user',
|
||||
currentUserId,
|
||||
loginRequired = true,
|
||||
onCreateUser,
|
||||
onDeleteUser,
|
||||
onLoginRequiredChange,
|
||||
onListUsers,
|
||||
onUpdateUser,
|
||||
open = $bindable(false),
|
||||
title = 'Settings'
|
||||
}: {
|
||||
children?: Snippet;
|
||||
description?: string;
|
||||
currentRole?: UserRole;
|
||||
currentUserId?: UserId;
|
||||
loginRequired?: boolean;
|
||||
onCreateUser?: UserCreateRequest;
|
||||
onDeleteUser?: UserDeleteRequest;
|
||||
onLoginRequiredChange?: (required: boolean) => void | Promise<void>;
|
||||
onListUsers?: UserListRequest;
|
||||
onUpdateUser?: UserUpdateRequest;
|
||||
open?: boolean;
|
||||
title?: string;
|
||||
} = $props();
|
||||
|
||||
let loginRequiredValue = $state<boolean>(true);
|
||||
let loginSettingError = $state('');
|
||||
|
||||
$effect(() => {
|
||||
loginRequiredValue = loginRequired;
|
||||
});
|
||||
|
||||
async function updateLoginRequired(required: boolean) {
|
||||
const previous = loginRequiredValue;
|
||||
loginRequiredValue = required;
|
||||
loginSettingError = '';
|
||||
try {
|
||||
await onLoginRequiredChange?.(required);
|
||||
} catch (error) {
|
||||
loginRequiredValue = previous;
|
||||
loginSettingError = error instanceof Error ? error.message : 'The setting could not be saved.';
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<Dialog bind:open {description} {title}>
|
||||
{#if currentRole === 'admin' || currentRole === 'superadmin'}
|
||||
<div class="sc-field">
|
||||
<label class="flex items-center gap-3">
|
||||
<input
|
||||
class="size-4 accent-primary"
|
||||
type="checkbox"
|
||||
checked={loginRequiredValue}
|
||||
onchange={(event) => updateLoginRequired((event.currentTarget as HTMLInputElement).checked)}
|
||||
/>
|
||||
<span>
|
||||
<span class="block font-medium">Require login to access app</span>
|
||||
<span class="sc-muted">Admins and superadmins can change this setting.</span>
|
||||
</span>
|
||||
</label>
|
||||
{#if loginSettingError}<p class="sc-error" role="alert">{loginSettingError}</p>{/if}
|
||||
</div>
|
||||
{/if}
|
||||
{#if children}
|
||||
{@render children()}
|
||||
{:else}
|
||||
<p class="sc-muted">No settings have been configured for this application yet.</p>
|
||||
{/if}
|
||||
<UserManagement
|
||||
currentRole={currentRole}
|
||||
currentUserId={currentUserId}
|
||||
onCreate={onCreateUser}
|
||||
onDelete={onDeleteUser}
|
||||
onList={onListUsers}
|
||||
onUpdate={onUpdateUser}
|
||||
/>
|
||||
</Dialog>
|
||||
@@ -1,4 +1,5 @@
|
||||
export { default as AboutDialog } from './components/AboutDialog.svelte';
|
||||
export { default as AdminDialog } from './components/AdminDialog.svelte';
|
||||
export { default as AppShell } from './components/AppShell.svelte';
|
||||
export { default as AuthPage } from './components/AuthPage.svelte';
|
||||
export { default as Badge } from './components/Badge.svelte';
|
||||
|
||||
Reference in new issue
Block a user