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>
238 lines
12 KiB
TypeScript
238 lines
12 KiB
TypeScript
import { useEffect, useState } from "react";
|
|
import { api, type GroupRolePreview, type ProfilePictureSettings, type SignInSettings as SignInSettingsData } from "../../api/client";
|
|
import { ErrorNotice } from "../../components/Notice";
|
|
import { PageSkeleton } from "../../components/Skeleton";
|
|
import { readableError } from "../../utils/errors";
|
|
import { toast } from "../../utils/toast";
|
|
import ExportTableButton from "../../components/ExportTableButton";
|
|
import { t } from "../../i18n";
|
|
|
|
const DEFAULTS: SignInSettingsData = { roleFromGroups: false, groupsClaim: "groups", adminGroups: [], operatorGroups: [], otherwise: "viewer" };
|
|
const NO_PICTURES: ProfilePictureSettings = { gravatar: false, identityProvider: false };
|
|
|
|
/** One name per line or separated by commas — whichever is easier to paste in. */
|
|
const parseNames = (text: string): string[] => [...new Set(text.split(/[\n,]/).map((s) => s.trim()).filter(Boolean))];
|
|
|
|
/**
|
|
* Who is an administrator, an operator or a viewer, decided from the groups the identity provider (Authentik) says a person is in, each
|
|
* time they sign in — instead of by hand on the Users page. Off by default. "Check" shows what each person would become, from the
|
|
* groups they had at their last sign-in, before it is switched on.
|
|
*/
|
|
export default function SignInSettings() {
|
|
const [loaded, setLoaded] = useState<SignInSettingsData | null>(null);
|
|
const [error, setError] = useState<string | null>(null);
|
|
const [enabled, setEnabled] = useState(false);
|
|
const [claim, setClaim] = useState("groups");
|
|
const [adminText, setAdminText] = useState("");
|
|
const [operatorText, setOperatorText] = useState("");
|
|
const [otherwise, setOtherwise] = useState<"viewer" | "keep">("viewer");
|
|
const [preview, setPreview] = useState<GroupRolePreview | null>(null);
|
|
const [loadedPictures, setLoadedPictures] = useState<ProfilePictureSettings>(NO_PICTURES);
|
|
const [pictures, setPictures] = useState<ProfilePictureSettings>(NO_PICTURES);
|
|
const [busy, setBusy] = useState<"check" | "save" | null>(null);
|
|
|
|
useEffect(() => {
|
|
api.settings
|
|
.get()
|
|
.then((res) => {
|
|
const s = res.settings.signIn ?? DEFAULTS;
|
|
setLoaded(s);
|
|
setEnabled(s.roleFromGroups);
|
|
setClaim(s.groupsClaim);
|
|
setAdminText(s.adminGroups.join("\n"));
|
|
setOperatorText(s.operatorGroups.join("\n"));
|
|
setOtherwise(s.otherwise);
|
|
const p = res.settings.profilePictures ?? NO_PICTURES;
|
|
setLoadedPictures(p);
|
|
setPictures(p);
|
|
})
|
|
.catch((err) => setError(readableError(err)));
|
|
}, []);
|
|
|
|
if (!loaded) return error ? <ErrorNotice error={error} /> : <PageSkeleton />;
|
|
|
|
const draft = (): SignInSettingsData => ({ roleFromGroups: enabled, groupsClaim: claim.trim() || "groups", adminGroups: parseNames(adminText), operatorGroups: parseNames(operatorText), otherwise });
|
|
const dirty = JSON.stringify(draft()) !== JSON.stringify(loaded) || JSON.stringify(pictures) !== JSON.stringify(loadedPictures);
|
|
|
|
async function check() {
|
|
setBusy("check");
|
|
setError(null);
|
|
try {
|
|
const d = draft();
|
|
setPreview(await api.users.previewGroupRoles({ groupsClaim: d.groupsClaim, adminGroups: d.adminGroups, operatorGroups: d.operatorGroups, otherwise: d.otherwise }));
|
|
} catch (err) {
|
|
setError(readableError(err));
|
|
} finally {
|
|
setBusy(null);
|
|
}
|
|
}
|
|
|
|
async function save() {
|
|
setBusy("save");
|
|
setError(null);
|
|
try {
|
|
const d = draft();
|
|
await api.settings.update({ signIn: d, profilePictures: pictures });
|
|
setLoaded(d);
|
|
setLoadedPictures(pictures);
|
|
toast.success(d.roleFromGroups ? "Saved. Roles now follow groups at each sign-in." : "Saved. Roles are set by hand on the Users page.");
|
|
} catch (err) {
|
|
setError(readableError(err));
|
|
} finally {
|
|
setBusy(null);
|
|
}
|
|
}
|
|
|
|
const noAdmin = enabled && preview !== null && preview.adminsAfter === 0;
|
|
|
|
return (
|
|
<>
|
|
<div className="d-flex align-items-center justify-content-between mb-3">
|
|
<h3 className="mb-0">{t("Sign-in")}</h3>
|
|
<button className="btn btn-primary" onClick={save} disabled={busy !== null || !dirty || noAdmin}>
|
|
{busy === "save" ? t("Saving…") : t("Save")}
|
|
</button>
|
|
</div>
|
|
<ErrorNotice error={error} />
|
|
|
|
<div className="card mb-3">
|
|
<div className="card-header">
|
|
<h3 className="card-title">{t("Roles from groups")}</h3>
|
|
</div>
|
|
<div className="card-body">
|
|
<p className="text-secondary">
|
|
By default a new person is a viewer until an administrator changes their role on the Users page. Here you can let the groups the identity provider reports decide it instead:
|
|
at every sign-in a person is an administrator if they are in an admin group, an operator if they are in an operator group, and otherwise what you choose below.
|
|
In Authentik the <code>groups</code> claim comes with the <em>profile</em> scope this app already asks for.
|
|
</p>
|
|
<label className="form-check mb-3">
|
|
<input type="checkbox" className="form-check-input" checked={enabled} onChange={(e) => setEnabled(e.target.checked)} />
|
|
<span className="form-check-label">{t("Set roles from groups at each sign-in")}</span>
|
|
</label>
|
|
<div className="row g-3">
|
|
<div className="col-md-4">
|
|
<label className="form-label" htmlFor="si-claim">
|
|
Claim that holds the groups
|
|
</label>
|
|
<input id="si-claim" className="form-control" value={claim} onChange={(e) => setClaim(e.target.value)} />
|
|
</div>
|
|
<div className="col-md-4">
|
|
<label className="form-label" htmlFor="si-admin">
|
|
{t("Admin groups")}
|
|
</label>
|
|
<textarea id="si-admin" className="form-control" rows={3} placeholder={"homelab-admins"} value={adminText} onChange={(e) => setAdminText(e.target.value)} />
|
|
<div className="form-hint">{t("One per line, or separated by commas. Case doesn't matter.")}</div>
|
|
</div>
|
|
<div className="col-md-4">
|
|
<label className="form-label" htmlFor="si-operator">
|
|
{t("Operator groups")}
|
|
</label>
|
|
<textarea id="si-operator" className="form-control" rows={3} placeholder={"homelab-ops"} value={operatorText} onChange={(e) => setOperatorText(e.target.value)} />
|
|
</div>
|
|
<div className="col-md-6">
|
|
<label className="form-label" htmlFor="si-otherwise">
|
|
{t("Someone in none of those groups")}
|
|
</label>
|
|
<select id="si-otherwise" className="form-select" value={otherwise} onChange={(e) => setOtherwise(e.target.value as "viewer" | "keep")}>
|
|
<option value="viewer">becomes a viewer</option>
|
|
<option value="keep">keeps the role they already have</option>
|
|
</select>
|
|
</div>
|
|
</div>
|
|
<div className="alert alert-info mt-3 mb-0">
|
|
Safeguards: if a sign-in carries no groups at all (a wrong claim name, say) roles are left exactly as they are; the only administrator is never demoted by a group rule;
|
|
and the very first person to sign in is still the administrator. While this is on, roles can't be set by hand — a role set on the Users page would be undone at the next sign-in.
|
|
</div>
|
|
</div>
|
|
<div className="card-footer d-flex flex-wrap align-items-center gap-2">
|
|
<button className="btn btn-outline-secondary" onClick={check} disabled={busy !== null}>
|
|
{busy === "check" ? t("Checking…") : t("Check against the groups people had last time")}
|
|
</button>
|
|
<span className="text-secondary small">Changes nothing — it shows what each person would be.</span>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="card mb-3">
|
|
<div className="card-header">
|
|
<h3 className="card-title">{t("Profile pictures")}</h3>
|
|
</div>
|
|
<div className="card-body">
|
|
<p className="text-secondary">
|
|
Without a picture a person is shown as their initials. Either source below can be allowed; the picture from the identity provider is tried first. Both are off, because each means
|
|
<strong> this server</strong> asks an outside address for a picture. People's browsers never do: the server fetches, checks (small PNG, JPEG, WebP or GIF only) and keeps the pictures for a day,
|
|
and serves them from this app. A change shows within a day, or on the next sign-in for a new picture address.
|
|
</p>
|
|
<label className="form-check mb-2">
|
|
<input type="checkbox" className="form-check-input" checked={pictures.identityProvider} onChange={(e) => setPictures({ ...pictures, identityProvider: e.target.checked })} />
|
|
<span className="form-check-label">
|
|
The picture the identity provider gives
|
|
<span className="form-hint d-block">
|
|
The <code>picture</code> claim from Authentik, kept at each sign-in. Only addresses on Authentik's own host (or Gravatar, if that is allowed too) are used.
|
|
</span>
|
|
</span>
|
|
</label>
|
|
<label className="form-check mb-0">
|
|
<input type="checkbox" className="form-check-input" checked={pictures.gravatar} onChange={(e) => setPictures({ ...pictures, gravatar: e.target.checked })} />
|
|
<span className="form-check-label">
|
|
Gravatar
|
|
<span className="form-hint d-block">
|
|
gravatar.com is sent a SHA-256 hash of the person's email address (not the address itself), and sees this server's IP address. Anyone who already knows an address can check
|
|
it against a hash, so leave this off if the email addresses are private.
|
|
</span>
|
|
</span>
|
|
</label>
|
|
</div>
|
|
</div>
|
|
|
|
{preview && (
|
|
<div className="card">
|
|
<div className="card-header">
|
|
<h3 className="card-title">{t("What these rules would do")}</h3>
|
|
<div className="card-actions">
|
|
<ExportTableButton filename="role-preview" />
|
|
</div>
|
|
</div>
|
|
{preview.adminsAfter === 0 && (
|
|
<div className="alert alert-danger m-3 mb-0" role="alert">
|
|
With these rules nobody would be an administrator. Add an admin group that someone is in before switching this on.
|
|
</div>
|
|
)}
|
|
<div className="table-responsive">
|
|
<table className="table table-vcenter card-table">
|
|
<thead>
|
|
<tr>
|
|
<th>{t("Person")}</th>
|
|
<th>{t("Groups at last sign-in")}</th>
|
|
<th>{t("Now")}</th>
|
|
<th>{t("Would be")}</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
{preview.people.map((p) => (
|
|
<tr key={p.id}>
|
|
<td>{p.name}</td>
|
|
<td className="text-secondary">{p.groups === null ? "not known yet — they haven't signed in with groups" : p.groups.length === 0 ? "none" : p.groups.join(", ")}</td>
|
|
<td>{p.role}</td>
|
|
<td>
|
|
{p.wouldBe === null ? (
|
|
<span className="text-secondary">unchanged until they sign in</span>
|
|
) : p.wouldBe === p.role ? (
|
|
<span className="text-secondary">{p.wouldBe}</span>
|
|
) : (
|
|
<span className="fw-bold">{p.wouldBe}</span>
|
|
)}
|
|
</td>
|
|
</tr>
|
|
))}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<div className="card-footer text-secondary small">
|
|
Groups are recorded at every sign-in, whether or not this is on, so people need to have signed in once since this was installed to show up here.
|
|
</div>
|
|
</div>
|
|
)}
|
|
</>
|
|
);
|
|
}
|