About 1,600 more headings, buttons, table columns, status words, placeholders and short messages are translated. Tables of words built when the app loads are now read when they are drawn, so they follow the language, and a test fails if text is translated at load time. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
188 lines
8.1 KiB
TypeScript
188 lines
8.1 KiB
TypeScript
import { useState, type FormEvent } from "react";
|
|
import { Link } from "react-router-dom";
|
|
import { api, type AuthentikGroup, type AuthentikInfo, type AuthentikUserMade } from "../api/client";
|
|
import CopyButton from "./CopyButton";
|
|
import { ErrorNotice } from "./Notice";
|
|
import { confirmDialog } from "../utils/dialogs";
|
|
import { readableError } from "../utils/errors";
|
|
import { t } from "../i18n";
|
|
|
|
const EMPTY = { username: "", name: "", email: "" };
|
|
|
|
/**
|
|
* Making a user in Authentik, for an administrator: a name, an email if there is one, the groups, and out comes a one-time link for them to choose their own password with. The app never
|
|
* sees or sets a password, and the link is shown here once — it is not kept anywhere.
|
|
*/
|
|
export default function AuthentikNewUser({ a, onChanged }: { a: AuthentikInfo; onChanged: () => void }) {
|
|
const [open, setOpen] = useState(false);
|
|
const [fields, setFields] = useState(EMPTY);
|
|
const [groups, setGroups] = useState<AuthentikGroup[] | null>(null);
|
|
const [groupsError, setGroupsError] = useState<string | null>(null);
|
|
const [picked, setPicked] = useState<string[]>([]);
|
|
const [busy, setBusy] = useState(false);
|
|
const [error, setError] = useState<string | null>(null);
|
|
const [made, setMade] = useState<AuthentikUserMade | null>(null);
|
|
|
|
if (!a.canCreateUsers) {
|
|
return (
|
|
<div className="text-secondary small mt-3">
|
|
To make and manage users from here, give this Authentik a token that may add, change and delete them: <Link to="/integrations">{t("Integrations")}</Link> → edit → <em>Token for making and managing users</em>.
|
|
</div>
|
|
);
|
|
}
|
|
|
|
async function show() {
|
|
setOpen(true);
|
|
setMade(null);
|
|
setError(null);
|
|
setGroupsError(null);
|
|
try {
|
|
setGroups((await api.authentik.groups(a.id)).groups);
|
|
} catch (err) {
|
|
setGroups([]);
|
|
setGroupsError(readableError(err));
|
|
}
|
|
}
|
|
|
|
function close() {
|
|
setOpen(false);
|
|
setFields(EMPTY);
|
|
setPicked([]);
|
|
setMade(null);
|
|
setError(null);
|
|
}
|
|
|
|
async function submit(e: FormEvent) {
|
|
e.preventDefault();
|
|
if (busy) return;
|
|
setBusy(true);
|
|
setError(null);
|
|
try {
|
|
setMade(await api.authentik.createUser(a.id, { ...fields, groups: picked }));
|
|
setFields(EMPTY);
|
|
setPicked([]);
|
|
} catch (err) {
|
|
setError(readableError(err));
|
|
} finally {
|
|
setBusy(false);
|
|
}
|
|
}
|
|
|
|
async function stopAllowing() {
|
|
if (!(await confirmDialog({ title: t("Stop managing users"), message: `Remove the token for making and managing users from "${a.name}"? Reading it carries on. You can add one again under Integrations.`, confirmLabel: t("Remove"), danger: true }))) return;
|
|
try {
|
|
await api.authentik.removeWriteToken(a.id);
|
|
close();
|
|
onChanged();
|
|
} catch (err) {
|
|
setError(readableError(err));
|
|
}
|
|
}
|
|
|
|
const toggle = (id: string) => setPicked((p) => (p.includes(id) ? p.filter((x) => x !== id) : [...p, id]));
|
|
|
|
return (
|
|
<div className="mt-3">
|
|
<h4>{t("New user")}</h4>
|
|
{!open && (
|
|
<div className="d-flex flex-wrap align-items-center gap-2">
|
|
<button type="button" className="btn btn-sm btn-primary" onClick={() => void show()}>
|
|
{t("New user…")}
|
|
</button>
|
|
<button type="button" className="btn btn-sm btn-link text-secondary" onClick={() => void stopAllowing()}>
|
|
{t("Stop allowing this")}
|
|
</button>
|
|
</div>
|
|
)}
|
|
|
|
{open && made && (
|
|
<div className="card card-sm">
|
|
<div className="card-body">
|
|
<div className="mb-2">
|
|
<strong>{made.user.username}</strong> was made in {a.name}.
|
|
</div>
|
|
{made.link ? (
|
|
<>
|
|
<div className="input-group mb-2">
|
|
<input className="form-control" readOnly value={made.link} aria-label={t("Recovery link")} onFocus={(e) => e.currentTarget.select()} />
|
|
<CopyButton text={made.link} />
|
|
</div>
|
|
<div className="text-secondary small mb-3">
|
|
Send them this link. They open it and choose their own password; it works once. It is shown here only and kept nowhere, so copy it now. It points at the address this app reaches Authentik at, so the Authentik address in Integrations should be
|
|
the one people use.
|
|
</div>
|
|
</>
|
|
) : (
|
|
<div className="alert alert-warning mb-3">
|
|
The user exists, but no link could be made: {made.linkError ?? "no answer"}. Make one in Authentik: Directory → Users → {made.user.username} → Create recovery link.
|
|
</div>
|
|
)}
|
|
<button type="button" className="btn btn-sm btn-outline-secondary me-2" onClick={() => void show()}>
|
|
{t("Make another")}
|
|
</button>
|
|
<button type="button" className="btn btn-sm btn-link text-secondary" onClick={close}>
|
|
{t("Done")}
|
|
</button>
|
|
</div>
|
|
</div>
|
|
)}
|
|
|
|
{open && !made && (
|
|
<form onSubmit={submit} className="card card-sm">
|
|
<div className="card-body">
|
|
<ErrorNotice error={error} />
|
|
<div className="row g-2 mb-3">
|
|
<div className="col-md-4">
|
|
<label className="form-label" htmlFor={`ak-user-${a.id}`}>
|
|
{t("User name")}
|
|
</label>
|
|
<input id={`ak-user-${a.id}`} className="form-control" required maxLength={150} autoComplete="off" value={fields.username} onChange={(e) => setFields({ ...fields, username: e.target.value })} />
|
|
</div>
|
|
<div className="col-md-4">
|
|
<label className="form-label" htmlFor={`ak-name-${a.id}`}>
|
|
{t("Name")}
|
|
</label>
|
|
<input id={`ak-name-${a.id}`} className="form-control" required maxLength={200} autoComplete="off" value={fields.name} onChange={(e) => setFields({ ...fields, name: e.target.value })} />
|
|
</div>
|
|
<div className="col-md-4">
|
|
<label className="form-label" htmlFor={`ak-email-${a.id}`}>
|
|
Email <span className="text-secondary">{t("(optional)")}</span>
|
|
</label>
|
|
<input id={`ak-email-${a.id}`} type="email" className="form-control" maxLength={254} autoComplete="off" value={fields.email} onChange={(e) => setFields({ ...fields, email: e.target.value })} />
|
|
</div>
|
|
</div>
|
|
|
|
<div className="form-label">{t("Groups")}</div>
|
|
{groups === null && <div className="text-secondary small mb-3">{t("Reading the groups…")}</div>}
|
|
{groupsError && <div className="text-yellow small mb-2">The groups could not be read: {groupsError}. The user can still be made, without groups.</div>}
|
|
{groups && groups.length === 0 && !groupsError && <div className="text-secondary small mb-3">{t("There are no groups.")}</div>}
|
|
{groups && groups.length > 0 && (
|
|
<div className="mb-3">
|
|
{groups.map((g) => (
|
|
<label key={g.id} className="form-check">
|
|
<input type="checkbox" className="form-check-input" checked={picked.includes(g.id)} onChange={() => toggle(g.id)} />
|
|
<span className="form-check-label">
|
|
{g.name}
|
|
{g.admin && <span className="badge bg-yellow-lt text-yellow ms-2">administrators</span>}
|
|
</span>
|
|
</label>
|
|
))}
|
|
</div>
|
|
)}
|
|
|
|
<div className="d-flex flex-wrap align-items-center gap-2">
|
|
<button type="submit" className="btn btn-primary" disabled={busy || groups === null}>
|
|
{busy ? "Making…" : t("Make user and link")}
|
|
</button>
|
|
<button type="button" className="btn btn-link text-secondary" onClick={close} disabled={busy}>
|
|
{t("Cancel")}
|
|
</button>
|
|
<span className="text-secondary small">They choose their own password with the link; none is set here.</span>
|
|
</div>
|
|
</div>
|
|
</form>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|