The Situation
You're running a blog with different user types: readers, writers, editors, and admins. Each has different permissions for creating, editing, and publishing posts.
Permission Design
| Role | Permissions |
|---|---|
| Reader | posts.list.view |
| Writer | posts.list.view, posts.create, posts.edit.own |
| Editor | posts.list.view, posts.list.edit, posts.publish, posts.archive |
| Admin | * |
Writers can only edit their own posts; editors can edit any post.
Setup
import { createPermissions } from "permly";
import { memoryAdapter } from "permly/memory";
export const perms = createPermissions({
adapter: memoryAdapter(),
permissions: [
"posts.list.view",
"posts.create",
"posts.edit.own",
"posts.list.edit",
"posts.publish",
"posts.archive"
],
roles: ["admin", "editor", "writer", "reader"]
});
export async function setupBlog() {
const { createdRoles } = await perms.sync();
if (createdRoles.includes("admin")) {
await perms.role("admin").givePermission("*");
}
if (createdRoles.includes("editor")) {
await perms.role("editor").givePermission(
"posts.list.view", "posts.list.edit", "posts.publish", "posts.archive"
);
}
if (createdRoles.includes("writer")) {
await perms.role("writer").givePermission(
"posts.list.view", "posts.create", "posts.edit.own"
);
}
if (createdRoles.includes("reader")) {
await perms.role("reader").givePermission("posts.list.view");
}
}
Assigning Roles to Users
import { createPermissions } from "permly";
import { memoryAdapter } from "permly/memory";
const perms = createPermissions({
adapter: memoryAdapter(),
permissions: [
"posts.list.view", "posts.create", "posts.edit.own",
"posts.list.edit", "posts.publish", "posts.archive"
],
roles: ["admin", "editor", "writer", "reader"]
});
const { createdRoles } = await perms.sync();
if (createdRoles.includes("admin")) {
await perms.role("admin").givePermission("*");
}
if (createdRoles.includes("editor")) {
await perms.role("editor").givePermission(
"posts.list.view", "posts.list.edit", "posts.publish", "posts.archive"
);
}
if (createdRoles.includes("writer")) {
await perms.role("writer").givePermission(
"posts.list.view", "posts.create", "posts.edit.own"
);
}
if (createdRoles.includes("reader")) {
await perms.role("reader").givePermission("posts.list.view");
}
// Assign roles to different users
await perms.user(1).assignRole("admin");
await perms.user(2).assignRole("editor");
await perms.user(3).assignRole("writer");
await perms.user(4).assignRole("reader");
// Admin can do everything
await perms.user(1).can("posts.publish"); // true
// Editor can publish but not create from scratch
await perms.user(2).can("posts.publish"); // true
await perms.user(2).can("posts.create"); // false
// Writer can create but not publish
await perms.user(3).can("posts.create"); // true
await perms.user(3).can("posts.publish"); // false
// Reader can only view
await perms.user(4).can("posts.list.view"); // true
await perms.user(4).can("posts.create"); // false
What Each Role Can Do
import { createPermissions } from "permly";
import { memoryAdapter } from "permly/memory";
const perms = createPermissions({
adapter: memoryAdapter(),
permissions: [
"posts.list.view", "posts.create", "posts.edit.own",
"posts.list.edit", "posts.publish", "posts.archive"
],
roles: ["admin", "editor", "writer", "reader"]
});
const { createdRoles } = await perms.sync();
if (createdRoles.includes("admin")) {
await perms.role("admin").givePermission("*");
}
if (createdRoles.includes("editor")) {
await perms.role("editor").givePermission(
"posts.list.view", "posts.list.edit", "posts.publish", "posts.archive"
);
}
if (createdRoles.includes("writer")) {
await perms.role("writer").givePermission(
"posts.list.view", "posts.create", "posts.edit.own"
);
}
if (createdRoles.includes("reader")) {
await perms.role("reader").givePermission("posts.list.view");
}
// Admin: full control
await perms.user("alice").assignRole("admin");
const adminPerms = await perms.user("alice").getPermissions({ expand: true });
adminPerms.includes("posts.create"); // true
adminPerms.includes("posts.publish"); // true
// Editor: manage any post
await perms.user("bob").assignRole("editor");
const editorCanList = await perms.user("bob").can("posts.list.view"); // true
const editorCanEdit = await perms.user("bob").can("posts.list.edit"); // true
const editorCanPublish = await perms.user("bob").can("posts.publish"); // true
const editorCanCreate = await perms.user("bob").can("posts.create"); // false
// Writer: create and edit own posts only
await perms.user("charlie").assignRole("writer");
const writerCanCreate = await perms.user("charlie").can("posts.create"); // true
const writerCanPublish = await perms.user("charlie").can("posts.publish"); // false
// Reader: view only
await perms.user("dave").assignRole("reader");
const readerCanView = await perms.user("dave").can("posts.list.view"); // true
const readerCanCreate = await perms.user("dave").can("posts.create"); // false
Express Routes with Permission Checks
import express, { type Request, type Response } from "express";
import { createPermissions } from "permly";
import { memoryAdapter } from "permly/memory";
import { permlyExpress } from "permly/express";
interface AuthRequest extends Request {
user?: { id: number };
}
const perms = createPermissions({
adapter: memoryAdapter(),
permissions: [
"posts.list.view",
"posts.create",
"posts.edit.own",
"posts.list.edit",
"posts.publish",
"posts.archive"
],
roles: ["admin", "editor", "writer", "reader"],
strict: false
});
await perms.sync();
await perms.role("editor").givePermission("posts.list.view", "posts.list.edit", "posts.publish", "posts.archive");
await perms.role("writer").givePermission("posts.list.view", "posts.create", "posts.edit.own");
const app = express();
const guard = permlyExpress(perms, {
getUserId: (req) => Number(req.get("x-user-id"))
});
// Mock database
const posts = [
{ id: 1, authorId: 1, title: "First post", published: true }
];
// View all published posts (anyone can view)
app.get("/api/posts", (_req: Request, res: Response) => {
const published = posts.filter(p => p.published);
res.json(published);
});
// Create a new post (requires posts.create)
app.post("/api/posts", guard.permission("posts.create"), (req: AuthRequest, res: Response) => {
const post = {
id: posts.length + 1,
authorId: req.user?.id || 0,
title: req.body.title,
published: false
};
posts.push(post);
res.status(201).json(post);
});
// Edit own posts or any post if editor
app.put("/api/posts/:id", guard.own("posts.edit.own", (req: Request) => Promise.resolve(posts.find(p => p.id === Number(req.params.id))?.authorId || 0)), async (req: AuthRequest, res: Response) => {
const post = posts.find(p => p.id === Number(req.params.id));
if (!post) return res.status(404).json({ error: "Not found" });
const isEditor = await perms.user(req.user?.id || 0).can("posts.list.edit");
if (!isEditor && post.authorId !== (req.user?.id || 0)) {
return res.status(403).json({ error: "Can only edit your own posts" });
}
post.title = req.body.title;
res.json(post);
});
// Publish a post (requires posts.publish - editors and admins only)
app.post("/api/posts/:id/publish", guard.permission("posts.publish"), (req: Request, res: Response) => {
const post = posts.find(p => p.id === Number(req.params.id));
if (!post) return res.status(404).json({ error: "Not found" });
post.published = true;
res.json(post);
});
// Archive a post (requires posts.archive - editors and admins only)
app.delete("/api/posts/:id", guard.permission("posts.archive"), (req: Request, res: Response) => {
const post = posts.find(p => p.id === Number(req.params.id));
if (!post) return res.status(404).json({ error: "Not found" });
posts.splice(posts.indexOf(post), 1);
res.status(204).send();
});
Common Mistakes
Don't forget .own(): Writers can only edit their own posts. Use guard.own() to check both permission and ownership.
Forgetting to add posts.archive to permissions: If editors should delete posts, add this permission to the system.
See also: Users can edit only their own posts