Add Roles to Administration, with Stalwart's permissions in every language
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.
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
import { currentLanguage } from "@/lib/i18n";
|
||||
import { DEFAULT_UI_LANGUAGE } from "@/lib/languages";
|
||||
|
||||
/**
|
||||
* Stalwart's permission labels, in the reader's language.
|
||||
*
|
||||
* The server publishes one English label per permission -- "Accounts
|
||||
* Management: Create accounts" -- and nothing else. Each language has its own
|
||||
* file under `locales/permissions`, loaded only when the Roles screen asks, so
|
||||
* six hundred labels do not ride along with every page of mail.
|
||||
*
|
||||
* A file is keyed by permission name, not by the English label, so a change
|
||||
* to Stalwart's wording does not orphan its translation. A permission a later
|
||||
* Stalwart adds has no entry yet, and shows the server's English label until
|
||||
* one is written.
|
||||
*/
|
||||
|
||||
export interface PermissionCatalog {
|
||||
/** The English heading before the colon, e.g. "Accounts Management", to its translation. */
|
||||
categories: Record<string, string>;
|
||||
/** Permission name to the translated action, the part after the colon. */
|
||||
labels: Record<string, string>;
|
||||
}
|
||||
|
||||
export interface PermissionInfo {
|
||||
name: string;
|
||||
/** Stalwart's English label, as the server sent it. */
|
||||
label: string;
|
||||
}
|
||||
|
||||
export interface PermissionEntry {
|
||||
name: string;
|
||||
/** The heading this permission is listed under, in the reader's language. */
|
||||
category: string;
|
||||
/** The English heading, which is what groups are keyed by. */
|
||||
categoryKey: string;
|
||||
/** What it allows, in the reader's language. */
|
||||
action: string;
|
||||
}
|
||||
|
||||
/** The heading a label without one is listed under. */
|
||||
export const GENERAL_CATEGORY = "General";
|
||||
|
||||
/** Split Stalwart's "Heading: action" label at its first colon. */
|
||||
export function splitLabel(label: string): { categoryKey: string; action: string } {
|
||||
const at = label.indexOf(":");
|
||||
if (at < 0) return { categoryKey: GENERAL_CATEGORY, action: label };
|
||||
return { categoryKey: label.slice(0, at).trim(), action: label.slice(at + 1).trim() };
|
||||
}
|
||||
|
||||
const loaded = new Map<string, Promise<PermissionCatalog | null>>();
|
||||
|
||||
/** The catalogue for a language, or null for English and for a language without a file. */
|
||||
export function loadPermissionCatalog(tag: string = currentLanguage()): Promise<PermissionCatalog | null> {
|
||||
if (tag === DEFAULT_UI_LANGUAGE) return Promise.resolve(null);
|
||||
let pending = loaded.get(tag);
|
||||
if (!pending) {
|
||||
pending = import(`../locales/permissions/${tag}.ts`).then(
|
||||
(m: { permissionCatalog: PermissionCatalog }) => m.permissionCatalog,
|
||||
() => null,
|
||||
);
|
||||
loaded.set(tag, pending);
|
||||
}
|
||||
return pending;
|
||||
}
|
||||
|
||||
/** Each permission with its heading and action in the reader's language, falling back to Stalwart's English. */
|
||||
export function describePermissions(list: readonly PermissionInfo[], catalog: PermissionCatalog | null, generalLabel: string): PermissionEntry[] {
|
||||
return list.map(({ name, label }) => {
|
||||
const { categoryKey, action } = splitLabel(label);
|
||||
const category = categoryKey === GENERAL_CATEGORY ? generalLabel : (catalog?.categories[categoryKey] ?? categoryKey);
|
||||
return { name, categoryKey, category, action: catalog?.labels[name] ?? action };
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user