What it does
Instead of assigning roles to users, you can grant permissions directly. A user gets a permission, period—no role needed.
When to use it
Use direct permissions when:
- Users need ad-hoc access — a guest editor, a one-off moderator
- You don't have stable groups — permissions are scattered across users
- Mixing roles and direct permissions — some users get roles, others get individual permissions
Most apps combine roles and direct permissions. Roles handle the standard groups; direct permissions handle exceptions.
How it works
- Grant a permission to a user:
await perms.user(42).givePermission("posts.delete") - Check it:
await perms.user(42).can("posts.delete")— returns true - Revoke it:
await perms.user(42).revokePermission("posts.delete")
Direct permissions stack with roles. If a user has a role that grants posts.edit and you also directly grant posts.edit, they still have it—granting twice is harmless.
Examples
import { perms, setupPermissions } from "./permly.js";
await setupPermissions();
// Grant a permission directly to a user
await perms.user(42).givePermission("posts.delete");
// User can now delete posts
const allowed = await perms.user(42).can("posts.delete");
console.log(allowed); // true
import { perms, setupPermissions } from "./permly.js";
await setupPermissions();
await perms.user(42).givePermission("posts.delete");
const allowed: boolean = await perms.user(42).can("posts.delete");
console.log(allowed);
Revoke permissions
Remove a direct permission:
import { perms, setupPermissions } from "./permly.js";
await setupPermissions();
// Grant it
await perms.user(42).givePermission("posts.delete");
// Later, revoke it
await perms.user(42).revokePermission("posts.delete");
// User can no longer delete
const allowed = await perms.user(42).can("posts.delete");
console.log(allowed); // false
import { perms, setupPermissions } from "./permly.js";
await setupPermissions();
await perms.user(42).givePermission("posts.delete");
await perms.user(42).revokePermission("posts.delete");
const allowed: boolean = await perms.user(42).can("posts.delete");
console.log(allowed);
Mix roles and direct permissions
A user can have both roles and direct permissions. They can do anything their roles allow OR their direct permissions allow:
import { perms, setupPermissions } from "./permly.js";
await setupPermissions();
// User is an editor (via role)
await perms.user(42).assignRole("editor");
// But also grant direct permission to delete (beyond editor perms)
await perms.user(42).givePermission("posts.delete");
// Can they delete? Yes (from direct permission)
const canDelete = await perms.user(42).can("posts.delete");
console.log(canDelete); // true
import { perms, setupPermissions } from "./permly.js";
await setupPermissions();
await perms.user(42).assignRole("editor");
await perms.user(42).givePermission("posts.delete");
const canDelete: boolean = await perms.user(42).can("posts.delete");
console.log(canDelete);
Common mistakes
Granting the same permission twice — Harmless. The second grant does nothing.
Forgetting to revoke — If you revoke a permission but the user still has a role with that permission, they can still do it. Revoke only removes direct permissions.
Over-using direct permissions — If half your users have ad-hoc permissions, you should have created a role instead.
Related pages
- Roles — Group permissions together
- Permissions — Core concepts
- Checking Permissions — How to verify access