Permission matrix
Who may do what: people and teams by permissions, each cell allow, deny or inherit. No roles.
import { PermissionMatrix, permissionDiff, permissionSummary, setPermission, permissionAt, permissionKey } from "@forgeron/ui";| People and teams | InvoicesRead | InvoicesWrite | InvoicesDelete | PeopleRead | PeopleWrite |
|---|---|---|---|---|---|
| Ada Lovelaceada@example.test | |||||
| Sam Rochesam@example.test | |||||
| Finance4 members | |||||
| Support9 members |
Arrows move. Space or Enter cycles a cell, or sets a whole row or column from its header. A allows, D denies, I inherits.
import {
PermissionMatrix,
permissionKey,
type PermissionColumn,
type PermissionSubject,
type PermissionValue,
} from "@forgeron/ui";
import { useState } from "react";
const subjects: PermissionSubject[] = [
{ id: "ada", name: "Ada Lovelace", kind: "person", hint: "ada@example.test" },
{ id: "sam", name: "Sam Roche", kind: "person", hint: "sam@example.test" },
{ id: "finance", name: "Finance", kind: "team", hint: "4 members" },
{ id: "support", name: "Support", kind: "team", hint: "9 members" },
];
const columns: PermissionColumn[] = [
{ id: "invoices.read", group: "Invoices", label: "Read" },
{ id: "invoices.write", group: "Invoices", label: "Write" },
{ id: "invoices.delete", group: "Invoices", label: "Delete" },
{ id: "people.read", group: "People", label: "Read" },
{ id: "people.write", group: "People", label: "Write" },
];
const saved: PermissionValue = {
[permissionKey("ada", "invoices.read")]: "allow",
[permissionKey("ada", "invoices.write")]: "allow",
[permissionKey("finance", "invoices.read")]: "allow",
[permissionKey("finance", "invoices.write")]: "allow",
[permissionKey("support", "invoices.write")]: "deny",
};
export default function Default() {
const [value, setValue] = useState<PermissionValue>(saved);
return (
<div style={{ width: "100%" }}>
<PermissionMatrix
label="Permissions of the workspace"
subjects={subjects}
columns={columns}
value={value}
onChange={setValue}
saved={saved}
inherited={(_subject, column) => (column.endsWith(".read") ? "allow" : undefined)}
/>
</div>
);
}| People and teams | LogsRead | LogsExport |
|---|---|---|
| Audit | ||
| Léa Martin |
Read only. Arrows move between cells.
- Audit, allowed: Logs Read, Logs Export.
- Léa Martin, denied: Logs Export; 1 inherited.
import { PermissionMatrix, permissionKey } from "@forgeron/ui";
export default function ReadOnly() {
return (
<div style={{ width: "100%" }}>
<PermissionMatrix
label="Permissions of the audit team"
readOnly
summaryOpen
subjects={[
{ id: "audit", name: "Audit", kind: "team" },
{ id: "lea", name: "Léa Martin", kind: "person" },
]}
columns={[
{ id: "logs.read", group: "Logs", label: "Read" },
{ id: "logs.export", group: "Logs", label: "Export" },
]}
defaultValue={{
[permissionKey("audit", "logs.read")]: "allow",
[permissionKey("audit", "logs.export")]: "allow",
[permissionKey("lea", "logs.export")]: "deny",
}}
/>
</div>
);
}API → Props
| Prop | Type | Default | Description |
|---|---|---|---|
label | string | — | Names the grid for assistive technology. |
subjects | { id, name, kind?: 'person' | 'team', hint? }[] | — | The rows. |
columns | { id, label, group? }[] | — | The permissions: an action (label) on a resource (group). |
value / defaultValue / onChange | Record<string, 'allow' | 'deny'> | — | The cells that are not inherited, keyed by permissionKey(subjectId, columnId). An absent cell inherits. |
saved | PermissionValue | — | The saved grid: cells that differ are marked, counted, and listed in the summary. |
readOnly | boolean | false | Shows the grid, changes nothing; arrows still move. |
inherited | (subjectId, columnId) => 'allow' | 'deny' | undefined | — | What an inherited cell comes to: shown faded, dashed. |
maxHeight | number | 420 | Scrolls past it; the headers stay. |
summaryOpen | boolean | false | The text summary starts open. |
subjectLabel / stateNames | string; Partial<Record<…>> | — | Wording. |
className | string | — | Extra classes. |