Add per-user project-access restriction; generalize mediaauth into a unified auth proxy

shared/backend/auth: adds SchemaOptions.ProjectScoping and Store methods
(SetUserProjects/UserProjects/UserProjectsByUser/IsProjectAllowed,
SetKnownProjects/KnownProjects) backing AuthGuard's new per-user
project-access restriction.

shared/backend/authclient: Client can now identify its calling project to
AuthServer via WithProjectKey (sent as X-Project-Key on every request), so
AuthServer can enforce the restriction above per project.

shared/backend/mediaauth: generalized from a media-family-only package
into the one shared auth-proxy engine every AuthGuard-integrated project
should use. New accepts a projectKey; the login-required/setup-claim
storage it needs is now expressed as a small Store interface
(NewWithStore lets any project's own storage satisfy it, not just the
media family's shared `settings` table, which pgSettingsStore still backs
for existing callers). ManagedUpdateUser/ManagedDeleteUser now resolve
their {id} path param via chi.URLParam OR the stdlib 1.22+ ServeMux's
PathValue, so non-chi consumers work unchanged.

packages/web-ui: UserManagement gains an optional `projects` prop (a
per-account project-access picker), used only by AuthGuard's own admin UI.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
Gabriel20xxandClaude Sonnet 5 committed 2026-09-13 12:16:17 +02:00
1 parent 6ac2fcefeb
commit 663d498a03
7 files changed
+476 -51

No files matched your search

@@ -1,5 +1,6 @@
<script lang="ts">
import type {
KnownProject,
ManagedUser,
UserCreateInput,
UserCreateRequest,
@@ -19,7 +20,8 @@
onCreate,
onDelete,
onList,
onUpdate
onUpdate,
projects
}: {
currentRole?: UserRole;
currentUserId?: UserId;
@@ -27,6 +29,15 @@
onDelete?: UserDeleteRequest;
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
* component simply doesn't render.
*/
projects?: KnownProject[];
} = $props();
let users = $state<ManagedUser[]>([]);
@@ -102,10 +113,25 @@
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 ?? [])];
error = '';
notice = '';
}
function toggleEditProject(key: string, checked: boolean): void {
const current = editUser.projects ?? [];
editUser.projects = checked ? [...current, key] : current.filter((project) => project !== key);
}
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(', ');
}
function stopEditing(): void {
editingId = null;
editUser = {};
@@ -195,6 +221,21 @@
{#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">
<input
type="checkbox"
checked={(editUser.projects ?? []).includes(project.key)}
onchange={(event) => toggleEditProject(project.key, event.currentTarget.checked)}
/>
{project.label}
</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}
@@ -202,6 +243,9 @@
<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}
<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>
+1
View File
@@ -28,6 +28,7 @@ export type {
AuthResetRequest,
AuthSetupInput,
ContentSnippet,
KnownProject,
NavItem,
ProfileUpdateInput,
ManagedUser,
+33
View File
@@ -818,6 +818,39 @@
gap: 0.4rem;
}
.sc-user-projects-summary {
max-width: 12rem;
overflow: hidden;
font-size: 0.8rem;
text-overflow: ellipsis;
white-space: nowrap;
}
.sc-user-projects {
display: flex;
flex: 1 1 100%;
flex-wrap: wrap;
gap: 0.5rem 1rem;
margin: 0;
padding: 0.5rem 0.7rem;
border: 1px solid var(--sc-border);
border-radius: 0.5rem;
}
.sc-user-projects legend {
flex-basis: 100%;
margin-bottom: 0.3rem;
color: var(--sc-muted-fg);
font-size: 0.8rem;
}
.sc-user-project-option {
display: flex;
align-items: center;
gap: 0.35rem;
font-size: 0.85rem;
}
.sc-user-edit-row {
align-items: stretch;
flex-wrap: wrap;
+19
View File
@@ -58,6 +58,19 @@ export interface ManagedUser {
updatedAt?: string;
/** 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.
*/
projects?: string[];
}
/** One project an administrator may restrict a user's login access to - see UserManagement's `projects` prop. */
export interface KnownProject {
key: string;
label: string;
}
/**
@@ -79,6 +92,12 @@ export interface UserUpdateInput {
email?: string;
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.
*/
projects?: string[];
}
export type UserListRequest = () => ManagedUser[] | Promise<ManagedUser[]>;