Skip to documentation content

Permission Matrix

A grid of roles and resources where each cell is allowed, denied, or inherited. Inherited cells say which role or parent resource decides them, and changes are counted until they are saved.

permission-matrix-demo

Overview

Permission Matrix edits who can do what. Roles run across the top and resources run down the side. Resources can be nested, so "Delete projects" sits under "Projects". Each cell holds one of three values:

  • Allowed, set on the cell.
  • Denied, set on the cell.
  • Inherited, meaning the cell has no decision of its own.

An inherited cell still has an answer, and the matrix shows it. The mark is outlined instead of filled, and the cell names where the decision comes from, such as "inherited from Viewer on Projects". People can see what a role can really do before they change anything, and they can tell a decision they set from one they would change by editing somewhere else.

The value is only the decisions that were set. Your application stores that object and resolves permissions on the server with the same rules. resolvePermission is exported for that.

Anatomy

PermissionMatrix
  • PermissionMatrixHolds the decisions, the saved decisions they are compared with, and the roles and resources
    • PermissionMatrixGridThe scrolling grid: role headers with bulk menus, resource names, and one cell per role and resource
    • PermissionMatrixFooterA legend, the count of unsaved changes, and Discard and Save

Installing permission-matrix brings Button and Menu along as registry dependencies.

Behavior

Inheritance

A cell without its own decision is resolved in this order:

  1. The role's decision on the nearest parent resource. An Admin allowed on "Projects" is allowed on "Create projects".
  2. The base role, asked the same way. A role with inheritsFrom takes the base role's answer, from the resource up to its root. Base roles can chain, and a loop is stopped.
  3. defaultPermission. This is deny unless you pass allow.

A role's own decisions always come before its base role's. An Editor allowed on "Projects" is allowed on "Delete projects", even when the Viewer it inherits from is denied there. Decide whether this order matches your server before you use the component. If it does not, resolve permissions yourself and keep the matrix for editing.

Editing

Clicking a cell, or pressing Space or Enter on it, moves it from inherited to allowed, then denied, then back to inherited. The keys A, D, and I set a value directly, and Backspace or Delete returns a cell to inherited.

Each role header and resource name opens a menu with "Allow all", "Deny all", and "Reset to inherited". A role's menu sets every resource for that role. A resource's menu sets that resource for every editable role.

Roles with readOnly show a lock. Their cells cannot be changed, and their headers have no menu. disabled makes the whole matrix read-only.

Unsaved changes

Changes are counted against savedValue, or against the value the matrix mounted with. A changed cell has a dot, and the footer counts the changes. Discard returns every cell to the saved value.

With onSave, the footer also shows Save. If onSave returns a promise, Save stays busy until the promise settles. On success, the saved value becomes the current one; on failure, the changes stay. onValueChange receives the list of changed cells with every edit, ready to send.

Size

The grid scrolls past maxHeight, which is 28rem by default, and on narrow screens. Role headers stay at the top and resource names stay at the start while it scrolls.

Accessibility

The matrix is an ARIA grid named by label. Role headers are column headers and resource names are row headers, so a screen reader reads a cell with its role and resource, as in "Admin, Delete projects, Denied, inherited from Viewer on Delete projects".

The grid is one tab stop. Arrow keys move between cells, role headers, and resource names. In right-to-left layouts, Left and Right swap.

KeyAction
← → ↑ ↓Move between cells and headers
Home / EndMove to the start or end of the row
Ctrl + Home / EndMove to the first or last cell
Page Up / Page DownMove ten rows
Space or Enter on a cellCycle inherited, allowed, denied
A / D / IAllow, deny, or inherit
Enter on a headerOpen the bulk menu

Read-only cells set aria-readonly. Allowed and denied cells differ in shape, a check or a dash, as well as color. Inherited cells are outlined, so the matrix does not depend on color. The footer count is a polite status region.

Installation

npx honestui@latest add permission-matrix

Usage

import {
  PermissionMatrix,
  type PermissionGrants,
} from "@/components/ui/permission-matrix/permission-matrix"

const roles = [
  { id: "owner", name: "Owner", readOnly: true },
  { id: "editor", name: "Editor", inheritsFrom: "viewer" },
  { id: "viewer", name: "Viewer" },
]

const resources = [
  { id: "projects", name: "Projects", children: [{ id: "projects.delete", name: "Delete projects" }] },
  { id: "billing", name: "Billing" },
]

const [grants, setGrants] = React.useState<PermissionGrants>(saved)

<PermissionMatrix
  label="Workspace permissions"
  roles={roles}
  resources={resources}
  value={grants}
  onValueChange={setGrants}
  onSave={(next, changes) => api.updatePermissions(changes)}
/>

The same rules on the server

import { getParentIndex, resolvePermission } from "@/components/ui/permission-matrix/permission-matrix-utils"

const parents = getParentIndex(resources)
const { value } = resolvePermission(grants, "editor", "projects.delete", { roles, parents })

Allowing by default

<PermissionMatrix defaultPermission="allow" labels={{ byDefault: "not set, so allowed" }} />

Your own layout

<PermissionMatrix roles={roles} resources={resources}>
  <PermissionMatrixGrid maxHeight="40rem" />
  <PermissionMatrixFooter />
</PermissionMatrix>

Don't do this

Storing resolved permissions

// Bad. Every inherited cell becomes a decision, so changing a parent or a base role no longer reaches it.
save(allCells.map((cell) => resolvePermission(grants, cell.role, cell.resource, options)))

Store value as it is. It holds only the decisions people set.

Hiding inherited cells

An empty cell looks like "no access" even when the role is allowed through its base role. Keep the effective mark visible, as the matrix does.

Using color alone in custom marks

When you restyle the cells, keep a different shape for allowed and denied, and a different border for inherited.

Examples

Flat scopes

API tokens with no nesting and no base roles, so an unset scope is not granted. The text below the matrix is the change list onValueChange provides.

permission-matrix-scopes

API reference

PermissionMatrix

Accepts native div props beyond those listed.

PropTypeDefaultDescription
rolesPermissionRole[]RequiredColumns, in order.
resourcesPermissionResource[]RequiredRows, in order. children nest.
valuePermissionGrantsNoneThe decisions that are set, by role id and resource id.
defaultValuePermissionGrants{}The initial decisions when uncontrolled.
onValueChange(value, changes) => voidNoneRuns on every edit with the cells that differ from the saved value.
savedValuePermissionGrantsThe value at mount, then each saved valueWhat changes are counted against.
onSave(value, changes) => void | Promise<void>NoneShows Save while there are changes.
defaultPermission"allow" | "deny""deny"The decision when nothing up the chain is set.
labelstring"Permissions"The grid's accessible name.
disabledbooleanfalseMakes every cell read-only.
labelsPartial<PermissionMatrixLabels>EnglishEvery word the matrix writes.

PermissionMatrixGrid

Accepts native div props.

PropTypeDefaultDescription
maxHeightstring"28rem"The height the grid scrolls past.

Cells have data-value (allow, deny, or inherit), data-effective (allow or deny), and data-dirty when changed.

PermissionMatrixFooter

Accepts native div props.

Types and helpers

ExportShape
PermissionRole{ id, name, description?, inheritsFrom?, readOnly? }
PermissionResource{ id, name, description?, children? }
PermissionGrants{ [roleId]: { [resourceId]: "allow" | "deny" } }
PermissionChange{ roleId, resourceId, from, to }, where each value is allow, deny, or inherit
resolvePermission(grants, roleId, resourceId, { roles, parents, defaultValue? }) => { value, source }
setCell / setCellsReturn new grants with cells set. inherit removes a decision.
diffGrants(before, after) => PermissionChange[]