A role is a named set of permissions given to accounts, groups and tenants. It gets its own section under a new Access heading: every role listed with the permissions it grants once its bases are followed, and a panel to create, edit and delete one. A role builds on others and has everything they grant; a denial anywhere in the tree wins, which is how Stalwart resolves it (permissions.rs unions enabled and disabled across the tree, then subtracts). The picker is Stalwart's own list of permissions, under its headings, searchable and filterable to what is granted or set here. Each permission is not set, allowed or denied, and one that is inherited says which role it comes from. Only permissions the viewer holds can be allowed, because Stalwart refuses the rest, and a role carrying anything the viewer lacks opens read-only with no delete, because Stalwart checks a grant but not a delete. Saving sends a pointer for each permission and base role that changed. The roles Stalwart hands out by default, read from x:Authentication, say so before they are changed and cannot be deleted here; a role still in use is kept by the server, and the refusal names what uses it. The permission list is Stalwart's schema. A new route, GET /api/admin/permissions, fetches /api/schema as the signed-in account and returns only names and labels, behind the same two gates as the registry methods and held in memory for an hour. Its labels are English only, so every one of the 661 has a translation in each of the eight other languages, in its own file keyed by permission name and loaded only when Roles opens. A permission a later Stalwart adds shows its English label. A test holds every language to the 0.16.22 snapshot: nothing missing, nothing stale. The mock answers x:Role/set with the grant check, loops and in-use refusals, reads the defaults from x:Authentication, and serves the schema gzipped as the real one is. Fifty-two new strings and two plurals in all nine catalogues, and 661 permission labels with 59 headings in each of the eight translations.
64 lines
2.6 KiB
TypeScript
64 lines
2.6 KiB
TypeScript
import { gunzipSync } from "node:zlib";
|
|
import { config } from "./config.js";
|
|
|
|
/**
|
|
* Stalwart's list of permissions, for the Roles screen's picker.
|
|
*
|
|
* Stalwart publishes its whole registry schema at `GET /api/schema` to any
|
|
* signed-in account -- objects, forms, layouts and `enums.Permission`, a label
|
|
* for each permission. Its own administration interface is built from it. The
|
|
* browser cannot fetch it (no credentials there, and another origin), so this
|
|
* fetches it as the signed-in account and hands back the one part the client
|
|
* needs: a list of names and English labels, a few dozen kilobytes rather than
|
|
* the whole document.
|
|
*
|
|
* Held in memory for an hour per server, because it changes only when Stalwart
|
|
* is upgraded. Nothing is written anywhere.
|
|
*/
|
|
|
|
export interface PermissionInfo {
|
|
name: string;
|
|
label: string;
|
|
}
|
|
|
|
const CACHE_MS = 60 * 60 * 1000;
|
|
const cache = new Map<string, { at: number; list: PermissionInfo[] }>();
|
|
|
|
/** The permission list out of a schema document, or an empty list if it is not where 0.16 keeps it. */
|
|
export function extractPermissions(schema: unknown): PermissionInfo[] {
|
|
const list = (schema as { enums?: { Permission?: unknown } } | null)?.enums?.Permission;
|
|
if (!Array.isArray(list)) return [];
|
|
const out: PermissionInfo[] = [];
|
|
const seen = new Set<string>();
|
|
for (const item of list) {
|
|
const { name, label } = (item ?? {}) as { name?: unknown; label?: unknown };
|
|
if (typeof name !== "string" || !name || seen.has(name)) continue;
|
|
seen.add(name);
|
|
out.push({ name, label: typeof label === "string" && label ? label : name });
|
|
}
|
|
return out;
|
|
}
|
|
|
|
/**
|
|
* The schema's bytes as JSON. The file is shipped gzipped; whether the server
|
|
* says so in Content-Encoding (so fetch has already inflated it) or serves the
|
|
* .gz as it is, the magic number settles which this is.
|
|
*/
|
|
export function parseSchemaBody(bytes: Uint8Array): unknown {
|
|
const raw = bytes[0] === 0x1f && bytes[1] === 0x8b ? gunzipSync(bytes) : Buffer.from(bytes);
|
|
return JSON.parse(raw.toString("utf8"));
|
|
}
|
|
|
|
export async function fetchPermissions(authorization: string, baseUrl: string): Promise<PermissionInfo[] | null> {
|
|
const hit = cache.get(baseUrl);
|
|
if (hit && Date.now() - hit.at < CACHE_MS) return hit.list;
|
|
const res = await fetch(`${baseUrl}/api/schema`, {
|
|
headers: { authorization, accept: "application/json" },
|
|
signal: AbortSignal.timeout(config.upstreamTimeout),
|
|
});
|
|
if (!res.ok) return null;
|
|
const list = extractPermissions(parseSchemaBody(new Uint8Array(await res.arrayBuffer())));
|
|
if (list.length) cache.set(baseUrl, { at: Date.now(), list });
|
|
return list;
|
|
}
|