Getting Started

Core Concepts

Permissions, roles, users, and how they work together

What it does

This page explains the core model: permissions, roles, users, and how checks work.

Permissions

A permission is something a user may do. Name them like posts.edit (letters, numbers, - and _, separated by dots):

  • posts.create
  • posts.edit
  • posts.delete
  • comments.moderate

Permissions are listed in your config and created in the database when you call sync().

Roles

A role is a named group of permissions. Examples: admin, editor, viewer.

Roles are listed in config and created in the database when you call sync(). Then you grant permissions to them:

import { perms, setupPermissions } from "./permly.js";

await setupPermissions(); // creates roles and permissions in the database

const editorPerms = await perms.role("editor").getPermissions();
console.log(editorPerms); // → ["posts.*"]
import { perms } from "./permly.js";

await perms.sync();

await perms.role("editor").givePermission("posts.create", "posts.edit");

const editorPerms: string[] = await perms.role("editor").getPermissions();

Users and roles

A user is someone using your app. Users can have multiple roles (e.g., both editor and moderator).

Assign or remove roles:

import { perms, setupPermissions } from "./permly.js";

await setupPermissions();

await perms.user(42).assignRole("editor", "moderator");

await perms.user(42).getRoles(); // → ["editor", "moderator"]

await perms.user(42).removeRole("moderator");

// Or replace all roles at once:
await perms.user(42).syncRoles(["viewer"]);
await perms.user(42).assignRole("editor", "moderator");

const roles: string[] = await perms.user(42).getRoles();

await perms.user(42).removeRole("moderator");

await perms.user(42).syncRoles(["viewer"]);

User IDs can be numbers or strings (integers, UUIDs, MongoDB ObjectIds).

Direct permissions

A user can also have a permission directly, without a role:

import { perms, setupPermissions } from "./permly.js";

await setupPermissions();

// User 7 is a viewer, but also gets posts.delete just for them:
await perms.user(7).assignRole("viewer");
await perms.user(7).givePermission("posts.delete");

// Now user 7 can delete posts (from direct permission)
// and view things (from viewer role)
await perms.user(7).assignRole("viewer");
await perms.user(7).givePermission("posts.delete");

Checks: can()

Check if a user can do something:

const allowed = await perms.user(1).can("posts.create");

if (allowed) {
  // user 1 may create a post
}
const allowed: boolean = await perms.user(1).can("posts.create");

True if the user has the permission (from a role or directly).
False if the user doesn't have it.

Checks: hasRole()

Check if a user has a specific role:

await perms.user(1).hasRole("admin"); // → true or false
const isAdmin: boolean = await perms.user(1).hasRole("admin");

How checks work inside

When you call can("posts.create"):

  1. permly caches the user's roles and permissions for 60 seconds
  2. On check, it looks up: does any role have this permission? Or does the user have it directly?
  3. Wildcards match too: if a role has posts.*, then posts.create is allowed
  4. Returns true or false

Every change (assign role, give permission) clears the user's cache immediately, so the next check sees the new state.

Permissions vs. roles

  • Use roles for common patterns: "editors can do X", "viewers can do Y"
  • Use direct permissions for rare exceptions: "this one user gets one extra permission"

For "edit only your own", use a .own permission: posts.edit.own. See Ownership.

Common questions

Can I create roles and permissions at runtime?

Yes:

await perms.createRole("moderator");
await perms.createPermission("comments.moderate");

Do roles and permissions have to be in config?

No, config is just the default. You can create them at runtime too. But config-driven setup is simpler for most apps.

What happens if I check a permission that doesn't exist?

By default, permly throws an error (strict mode). This catches typos early. You can set strict: false to return false instead.

Next steps