Main's new wording (PEP-123 «Продакт» words) kept and wrapped in t()/m();
wording(mode, locale) gives English words for {task}/{runner} placeholders;
glossary check (pnpm lint) passes; English for all new keys (en/merge-*.ts).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
81 lines
3.9 KiB
TypeScript
81 lines
3.9 KiB
TypeScript
// Who may log into Peppa at all (web and remote MCP): an allowlist of Gitea logins, plus
|
||
// people invited to or members of a project, plus local accounts without Gitea (PEP-68).
|
||
import { m as tr } from "@peppa/shared/i18n";
|
||
import { projectInvites, projectMembers, users, type Db } from "@peppa/db";
|
||
import { eq, sql } from "drizzle-orm";
|
||
import { getSetting, setSetting } from "./settings.ts";
|
||
import type { User } from "./users.ts";
|
||
|
||
const ALLOWED = "platform.allowed_logins";
|
||
const DEFAULT_ALLOWED = ["asel", "7601978", "dana"];
|
||
|
||
export async function allowedLogins(db: Db): Promise<string[]> {
|
||
return (await getSetting<string[]>(db, ALLOWED)) ?? DEFAULT_ALLOWED;
|
||
}
|
||
|
||
export async function setAllowedLogins(db: Db, logins: string[]): Promise<void> {
|
||
await setSetting(db, ALLOWED, [...new Set(logins.map((l) => l.trim().toLowerCase()).filter(Boolean))].sort());
|
||
}
|
||
|
||
/** A Gitea login may log in if it is allowlisted or has a pending project invitation. */
|
||
export async function isLoginAllowed(db: Db, login: string): Promise<boolean> {
|
||
const l = login.toLowerCase();
|
||
if ((await allowedLogins(db)).includes(l)) return true;
|
||
const [invite] = await db.select().from(projectInvites).where(eq(projectInvites.login, l)).limit(1);
|
||
return !!invite;
|
||
}
|
||
|
||
/**
|
||
* An existing user keeps access while not disabled and allowlisted, a project member, or a
|
||
* local account (PEP-68: created by a superadmin or the CLI, can't come from Gitea OAuth).
|
||
*/
|
||
export async function isUserAllowed(db: Db, user: User): Promise<boolean> {
|
||
if (user.disabledAt) return false;
|
||
if (user.giteaId < 0) return true;
|
||
if (await isLoginAllowed(db, user.login)) return true;
|
||
const [m] = await db.select().from(projectMembers).where(eq(projectMembers.userId, user.id)).limit(1);
|
||
return !!m;
|
||
}
|
||
|
||
/**
|
||
* Why a Gitea OAuth login must be refused before the user row is touched, or null (PEP-68):
|
||
* a local user's login is never silently merged with a Gitea account; a disabled user stays out.
|
||
*/
|
||
export async function giteaLoginRefusal(db: Db, login: string): Promise<string | null> {
|
||
const [u] = await db.select().from(users).where(sql`lower(${users.login}) = ${login.toLowerCase()}`);
|
||
if (!u) return null;
|
||
if (u.giteaId < 0) return tr("Логин «{login}» занят пользователем Peppa FACTORY — войдите по логину и паролю", { login });
|
||
if (u.disabledAt) return tr("Нет доступа к Peppa FACTORY. Попросите администратора открыть его.");
|
||
return null;
|
||
}
|
||
|
||
/**
|
||
* PEP-71: anyone with eGov can register, so a project without a subscription pool runs agents on
|
||
* this machine's own login unless its owner registered themselves through eGov (a Peppa account
|
||
* without Gitea and without a password) and is neither a platform admin nor allowlisted — such a
|
||
* project needs a pool, set by a superadmin, first. Projects without an owner are the platform's.
|
||
*/
|
||
export async function mayUseMachineLogin(db: Db, ownerId: number | null): Promise<boolean> {
|
||
if (ownerId == null) return true;
|
||
const [owner] = await db.select().from(users).where(eq(users.id, ownerId));
|
||
if (!owner || owner.disabledAt) return false;
|
||
if (!(owner.giteaId < 0 && !owner.passwordHash)) return true;
|
||
const login = owner.login.toLowerCase();
|
||
const admins = (await getSetting<string[]>(db, "platform.admins")) ?? (await allowedLogins(db));
|
||
return admins.includes(login) || (await allowedLogins(db)).includes(login);
|
||
}
|
||
|
||
const SESSIONS_AFTER = "auth.sessions_after";
|
||
|
||
/** PEP-95: every web session issued before now stops working — everyone signs in again. */
|
||
export async function logoutAllUsers(db: Db, now = new Date()): Promise<Date> {
|
||
await setSetting(db, SESSIONS_AFTER, now.getTime());
|
||
return now;
|
||
}
|
||
|
||
/** Web sessions issued before this moment are invalid (null: no cut-off). */
|
||
export async function sessionsValidAfter(db: Db): Promise<number | null> {
|
||
const v = await getSetting<number>(db, SESSIONS_AFTER);
|
||
return typeof v === "number" ? v : null;
|
||
}
|