peppa/apps/web/app/(app)/users/page.tsx
Асель 414e4500e4 PEP-68: local users without Gitea — password login, superadmin management
- users.password_hash/password_set_at/disabled_at (migration 0002_local_users)
- core/local-users.ts: scrypt, passwordLogin with attempt limits, create/reset/disable
- isSuperadmin(db, user) = platform.admins; isUserAllowed honours disabled_at
- web: /auth/password + login form, /users for superadmins, Settings → password
- Gitea login with a local user's login is refused (giteaLoginRefusal)
- local users: projects in peppa-bot namespace, members without Gitea collaboration
- CLI: peppa user add|passwd|disable|enable; tests in testkit

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 03:20:21 +05:00

93 lines
5.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { notFound } from "next/navigation";
import { isLocalUser, isSuperadmin, listUsers, MIN_PASSWORD, platformAdmins } from "@peppa/core";
import { Hidden } from "@/components/cards";
import { ago, Flash, Notice, PageHeader, Section } from "@/components/common";
import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
import { Card } from "@/components/ui/card";
import { Input, Label } from "@/components/ui/input";
import { Workspace } from "@/components/Workspace";
import { requireUser } from "@/lib/auth";
import { db } from "@/lib/db";
import { createLocalUserAction, resetUserPasswordAction, setUserDisabledAction } from "../actions";
const back = { back: "/users" };
/** Superadmin only (PEP-68): local users without Gitea — create, reset password, disable. */
export default async function Users({ searchParams }: { searchParams: Promise<{ error?: string; created?: string; reset?: string }> }) {
const me = await requireUser();
if (!(await isSuperadmin(db, me))) notFound();
const { error, created, reset } = await searchParams;
const all = await listUsers(db);
const admins = new Set(await platformAdmins(db));
return (
<Workspace>
<div className="max-w-3xl">
<Flash error={error} />
{created && <Notice>Пользователь «{created}» создан — передайте ему логин и временный пароль, сменить его можно в «Настройках».</Notice>}
{reset && <Notice>Пароль «{reset}» сброшен — передайте новый временный пароль.</Notice>}
<PageHeader title="Пользователи">
Локальные пользователи входят по логину и паролю, без аккаунта Gitea. Отключённый пользователь сразу теряет сессии веба и MCP.
</PageHeader>
<Section title="Новый локальный пользователь">
<Card>
<form action={createLocalUserAction} className="flex flex-col gap-3">
<Hidden fields={back} />
<div className="flex flex-wrap gap-3">
<Label className="min-w-40 flex-1">
Логин
<Input name="login" required pattern="[a-zA-Z0-9][a-zA-Z0-9._\-]{1,38}" autoComplete="off" autoCapitalize="none" spellCheck={false} className="font-mono" />
</Label>
<Label className="min-w-40 flex-1">
Имя
<Input name="name" autoComplete="off" />
</Label>
<Label className="min-w-40 flex-1">
Временный пароль
<Input name="password" type="password" required minLength={MIN_PASSWORD} autoComplete="new-password" />
</Label>
</div>
<Button variant="default" className="self-start">Создать</Button>
</form>
</Card>
</Section>
<Section title="Все пользователи" count={all.length}>
{all.map((u) => {
const local = isLocalUser(u);
return (
<Card key={u.id} className="gap-3" data-user={u.login}>
<div className="flex flex-wrap items-center gap-2">
<span className="font-mono font-medium">{u.login}</span>
{u.name && <span className="text-sm text-muted-foreground">{u.name}</span>}
<Badge variant={local ? "info" : "muted"}>{local ? "локальный" : "Gitea"}</Badge>
{admins.has(u.login.toLowerCase()) && <Badge variant="primary">суперадмин</Badge>}
{u.disabledAt && <Badge variant="danger">отключён</Badge>}
{local && !u.passwordHash && <Badge variant="warn">без пароля</Badge>}
<span className="ml-auto text-xs text-muted-foreground">{u.lastLoginAt ? `вход ${ago(u.lastLoginAt)}` : "ещё не входил"}</span>
</div>
<div className="flex flex-wrap items-center gap-2">
{local && (
<form action={resetUserPasswordAction} className="flex flex-wrap gap-2">
<Hidden fields={{ userId: String(u.id), ...back }} />
<Input name="password" type="password" required minLength={MIN_PASSWORD} placeholder="Новый временный пароль" aria-label={`Новый пароль для ${u.login}`} autoComplete="new-password" className="w-56" />
<Button size="sm">Сбросить пароль</Button>
</form>
)}
{u.id !== me.id && (
<form action={setUserDisabledAction} className="ml-auto">
<Hidden fields={{ userId: String(u.id), disabled: u.disabledAt ? "0" : "1", ...back }} />
<Button size="sm" variant={u.disabledAt ? "default" : "destructive"}>{u.disabledAt ? "Включить" : "Отключить"}</Button>
</form>
)}
</div>
</Card>
);
})}
</Section>
</div>
</Workspace>
);
}