What it does
Ownership lets a user perform an action ONLY on resources they own. A user can edit their own posts but not others'. You define which permission grants ownership access, and permly checks it.
When to use it
Use ownership when:
- Users own their own resources — posts, comments, drafts
- Users can act on owned resources — edit your own post, delete your own comment
- Admins can act on anything — admins bypass ownership checks (they have broad permissions)
Ownership is common in social apps, blogs, issue trackers, and similar platforms.
How it works
- Define permissions in config:
["posts.edit", "posts.delete"]etc. - For ownership, add a
.ownvariant (optional):["posts.edit.own", "posts.delete.own"] - Check with context:
await perms.user(userId).canOwn("posts.edit", ownerId)
The check works two ways:
- If user has the base permission (e.g.,
posts.edit), they can act on any post (admin bypass) - If user only has the
.ownvariant, they can act only if they own it (compare user ID to owner ID)
Examples
import { perms, setupPermissions } from "./permly.js";
await setupPermissions();
// Alice (user 1) owns post 100 and has the "posts.edit.own" permission
const postOwnerId = 1;
const userId = 1;
await perms.user(userId).givePermission("posts.edit.own");
// Can Alice edit her own post?
const canEdit = await perms.user(userId).canOwn("posts.edit", postOwnerId);
console.log(canEdit); // true (has posts.edit.own and owns it)
// Bob (user 2) also has "posts.edit.own" but doesn't own the post
await perms.user(2).givePermission("posts.edit.own");
const bobCanEdit = await perms.user(2).canOwn("posts.edit", postOwnerId);
console.log(bobCanEdit); // false (has posts.edit.own but doesn't own it)
import { perms, setupPermissions } from "./permly.js";
await setupPermissions();
const postOwnerId = 1;
const userId = 1;
const canEdit: boolean = await perms.user(userId).canOwn("posts.edit", postOwnerId);
console.log(canEdit);
How canOwn works: the real rule
canOwn(permission, ownerId) checks two conditions:
- If user has the base permission (e.g.,
posts.edit), return true (regardless of owner) - Otherwise, check if user has the
.ownvariant AND owns the resource (ownerId matches userId)
This means admins don't get special bypass logic—they pass because they have broad permissions like *:
import { perms, setupPermissions } from "./permly.js";
await setupPermissions();
// Admin has "*" permission (via role)
await perms.user(99).assignRole("admin");
// Author only has posts.edit.own (can edit own posts only)
await perms.user(1).givePermission("posts.edit.own");
// Admin can edit any post because they have "*"
const adminCan = await perms.user(99).canOwn("posts.edit", 999); // true
console.log(adminCan);
// Author can edit only their own
const authorOwnCan = await perms.user(1).canOwn("posts.edit", 1); // true
const authorOtherCan = await perms.user(1).canOwn("posts.edit", 999); // false
console.log(authorOwnCan, authorOtherCan);
import { perms, setupPermissions } from "./permly.js";
await setupPermissions();
await perms.user(99).assignRole("admin");
await perms.user(1).givePermission("posts.edit.own");
const adminCan: boolean = await perms.user(99).canOwn("posts.edit", 999);
console.log(adminCan);
const authorOwnCan: boolean = await perms.user(1).canOwn("posts.edit", 1);
const authorOtherCan: boolean = await perms.user(1).canOwn("posts.edit", 999);
console.log(authorOwnCan, authorOtherCan);
Use with roles
Combine ownership with roles for cleaner code:
import { perms, setupPermissions } from "./permly.js";
await setupPermissions();
// Author role has canOwn for posts
await perms.user(1).assignRole("author");
// Check if user can edit (must either own it OR have global edit permission)
const isOwner = await perms.user(1).canOwn("posts.edit.own", 1);
const isEditor = await perms.user(1).can("posts.edit");
if (isOwner || isEditor) {
console.log("User can edit this post");
}
import { perms, setupPermissions } from "./permly.js";
await setupPermissions();
await perms.user(1).assignRole("author");
const isOwner: boolean = await perms.user(1).canOwn("posts.edit.own", 1);
const isEditor: boolean = await perms.user(1).can("posts.edit");
if (isOwner || isEditor) {
console.log("User can edit this post");
}
Common mistakes
Confusing ownership with permission — Ownership is a runtime check (is this your post?). Permissions are static grants (do you have edit permission?).
Forgetting to check ownership — If you check can("posts.edit") without also checking canOwn(), users can edit any post (if they have the permission).
Admin bypass doesn't work — If you only check ownership and don't check for global permissions, admins can't override. Check both.
Related pages
- Permissions — Core concepts
- Checking Permissions — Runtime checks
- Wildcards — Broad permission grants