@rljson/access-control

Decides with can() whether a subject may perform an action on an object, and why
Motivation
Applications built on Rljson keep their data on many nodes: a server, desktop
clients and browsers, often offline. Who may do what has to be decided the same
way on each of them, also without a connection to a server.
- Rights are data: roles, role assignments and releases are rows that
synchronize like every other table
- The decision is one pure function,
can(), that every node runs on its own
copy of the rights data
- Every result carries a reason, so a refusal can be explained to the user and
logged with the ref of the rights data it was made with
- The package has no runtime dependencies; organization, edit history, boards
and locks plug in through small interfaces
Installation
pnpm add @rljson/access-control
Usage
import { AccessData, can, type OrgResolver } from '@rljson/access-control';
// The rights data: a role and who holds it for which Scope
const data = new AccessData({
roles: [
{
id: 'editor',
rights: [{ right: 'document.read' }, { right: 'document.write' }],
},
],
roleAssignments: [
{
id: 'ra-1',
principalType: 'user',
principalId: 'ada',
roleId: 'editor',
scopeUnit: 'team-a',
},
],
shares: [
{
id: 'share-1',
objectType: 'document',
objectId: 'doc-1',
principalType: 'unit',
principalId: 'team-b',
access: 'read',
mode: 'link',
},
],
});
// The organization: two Units under one Root Unit
const org: OrgResolver = {
rootOf: () => 'company',
inScope: (scopeUnit, unitId) => scopeUnit === unitId,
groupsOf: () => [],
};
const ctx = { data, ref: 'rights-1', time: Date.now(), online: true, org };
const doc = { type: 'document', id: 'doc-1', ownerUnit: 'team-a' };
const ada = { userId: 'ada', unitId: 'team-a', roleId: 'editor' };
const bob = { userId: 'bob', unitId: 'team-b', roleId: 'editor' };
console.log(can(ada, 'document.write', doc, ctx).reason);
console.log(can(bob, 'document.write', doc, ctx).allowed);
It prints:
role editor holds document.write and its Scope team-a contains the Owner Unit team-a
false
bob holds the role editor but has no assignment, so the second call is
refused with the rule roleNotAssigned. With an assignment for team-b, bob would
read doc-1 through the release share-1, but not write it.
Limitations
- The organization is not part of this package: Root Units, Scopes, groups and
standing releases between Units come from an
OrgResolver
- Boards are not part of it either: State rules and State policies plug in as a
WorkflowGuard
- The lock check
leaseLock is a placeholder: a lease of another user blocks
changes online; offline it is not enforced
can() decides whether a subject may share an object, not whether the
target of the release is allowed
- Rights apply to object types; rights on parts of an object are not supported
- Rows are passed in plain; the package defines no Rljson table configs yet
Documentation
- Code guide
- Test guide
- Development guide
- The example world of the tests: test/example-world.ts
and the cases played through in
test/use-cases.spec.ts
How it works
can(subject, action, object, ctx) returns
{ allowed, step, rule, reason, ref }.
- Subject: the signed-in user, the Unit they work in and the role chosen
for the session. Rights of several roles do not add up
- Action:
<objectType>.<verb>, e.g. commission.read
- Object: type, ID, Owner Unit, optional State, lease and flags
- ctx: the rights data (
AccessData), its ref, the time, online or
offline, and the resolvers
The checks run in this order; the first refusal wins:
| Step |
Check |
action |
the action is well formed and fits the object type |
tenant |
subject and object lie under the same Root Unit; across Tenants only reading through a link release |
stateRules |
rules that bind everybody, e.g. a read-only State (WorkflowGuard.stateRules) |
lock |
a lease of another user (ctx.lock, default leaseLock) |
state |
what the State allows the subject, moves included (WorkflowGuard.state) |
allow |
the chosen role holds the right, and: a) its Scope contains the Owner Unit, b) a link release names the user, a group of the user or the Unit, c) the condition creator holds, or d) the condition sameGroup holds |
deny |
nothing allows it |
The right is always required: releases and conditions only widen the set of
objects it reaches. A read release opens read, a write release opens read,
write and move; no release opens share, unlock, delete or admin.
unlock needs a role without a condition whose Scope contains the Owner Unit.
AccessData indexes the rows once: pass them oldest first, the last row of
every ID counts, and withdrawn rows (revoked: true) are dropped.
Performance
test/can-performance.spec.ts measures 100,000
calls in one Tenant with 1,000 Units, 10,000 role assignments, 20 roles with 30
rights each and 100,000 releases. On a developer notebook (Node 22) a call
takes about 2 µs and indexing the rights data about 50 ms. The resolvers of the
test answer from maps; real resolvers add their own time.
Status
Before 1.0 — the API may change between minor versions.
Contributing
Follow the development guide and the
rljson guide.
License
MIT — see LICENSE.