Roles and scopes
Where a role lives, what its rules may target, and how inheritance works.
A role is a named set of rules that lives at one scope: a resource type such as organization,
workspace, or project. A role only ever takes effect through an
assignment of that role on a concrete resource of its scope type.
import { definePolicy } from "rolebook";
const policy = definePolicy({
resources: {
organization: { actions: ["read"] },
workspace: { parent: "organization", actions: ["read", "update"] },
project: { parent: "workspace", actions: ["read", "update", "delete"] },
},
roles: {
workspace_editor: {
scope: "workspace",
rules: [
{ effect: "allow", permission: "workspace:read" },
{ effect: "allow", permission: "project:read" },
{ effect: "allow", permission: "project:update" },
],
},
workspace_admin: {
scope: "workspace",
inherits: ["workspace_editor"],
rules: [
{ effect: "allow", permission: "workspace:update" },
{ effect: "allow", permission: "project:delete" },
],
},
},
}); A role definition accepts exactly scope, rules, and an optional inherits. scope must name a
declared resource type. A role may have up to 256 direct rules, and the whole policy up to 4,096.
What a role’s rules may target
A role’s rules may target its scope type or any type below it in the declared parent chain. They may not target an ancestor or an unrelated type, because such a rule could never apply: an assignment on a workspace never covers its organization.
- A
workspacerole can grantworkspace:updateandproject:delete. - A
workspacerole cannot grantorganization:read.definePolicy()throwsInvalid Rolebook policy: rule permission is outside the role scope.
Inheritance
inherits lists other roles at the same scope. A role inherits their complete rules, allows and
denies alike, transitively. In the example, workspace_admin can read, update, and delete projects and
update the workspace. Inheritance rules:
- Every inherited role must exist and must have the same
scope. inheritsmay not repeat a role, and inheritance may not form a cycle.- A role’s effective rule set (its own plus every inherited rule) is at most 4,096 rules, and the sum over all roles at most 16,384.
Decisions attribute a matched rule to the role that defines it. When workspace_admin is allowed
project:update, decision.matched.role is "workspace_editor", because that rule comes from the
inherited role. The assignment itself still names workspace_admin.
Assignment scope: self or descendants
An assignment places a role on one resource and says how far it reaches:
appliesTo | Covers |
|---|---|
"self" | Only the named scope resource. |
"descendants" | The named resource itself and every resource below it in the checked resource’s ancestry. |
Scope is inclusive: a descendants assignment on ws_456 applies to the workspace ws_456 and to
every project whose trusted ancestry contains ws_456. A self assignment on ws_456 never applies
to its projects.
The assignment’s scope resource type must equal the role’s scope. Assigning workspace_editor on a
project denies the whole binding with INVALID_ASSIGNMENT.
No bypass, no implication
There is no role that bypasses evaluation, and no action implies another. An organization owner who
should be able to read every project needs an organization-scoped role with an explicit
project:read rule and a descendants assignment. Deny rules still apply to such a role: see
deny precedence.
Designing roles
- Put a role at the narrowest scope that matches how it is granted. A role granted per project belongs
at
project; a role granted per workspace belongs atworkspaceand reaches projects throughdescendants. - Prefer composition with
inheritsover copying rules, so that a change to a base role reaches every role built on it. - Use deny rules for invariants that must hold whatever else a subject holds, such as “suspended users cannot write”, and keep them conditional on authoritative facts.
- For tenant-defined roles, see custom roles in the sharing package: tenant labels bounded by a static template role.