Pundit-TS offers a robust solution for organizing authorization logic with type safety. Inspired by the pundit gem, it simplifies role-based and attribute-based access control, ensuring that authorization rules are centralized and easy to manage. Ideal for TypeScript developers seeking clarity in their authorization practices.
Pundit-TS is a robust authorization library designed to help developers organize authorization logic in a fully type-safe manner. Drawing inspiration from the Pundit gem, it offers a seamless way to implement authorization policies across various models and actions.
where clauses and joins based on user authorization, minimizing unnecessary data retrieval.Pundit-TS caters to different use cases with examples:
PostPolicy#filter to construct arguments for prisma.post.findMany calls.Here are some implementation examples demonstrating how to manage access control:
class Policy {
authorize(ctx, object, action) {
const isAuthenticated = ctx.actor !== null;
const role = ctx.actor?.role;
const isAdmin = role === "admin";
const isEditor = role === "editor";
switch (action) {
case "create":
return isAdmin || isEditor;
case "delete":
return isAdmin;
case "view":
return true; // Accessible to all, including anonymous users
default:
return false; // Disallow other actions
}
}
}
class Policy {
authorize(ctx, object, action) {
if (ctx.actor === null) {
throw new UnauthorizedError();
}
const role = ctx.actor.role;
const isAdmin = role === "admin";
const isEditor = role === "editor";
switch (action) {
case "delete":
return isAdmin;
case "update:content":
return isAdmin || isEditor;
case "view:content":
return true;
default:
return false;
}
}
}
Ideal for multi-tenant applications:
class DocumentPolicy {
authorize(ctx, object, action) {
if (ctx.actor === null) {
return false; // Anonymous users cannot perform any action
}
const isOrganizationOwner = ctx.actor.id === object.organization.owner_id;
if (isOrganizationOwner) {
return true; // Organization owner can perform any action
}
const member = object.organization.members.findById(ctx.actor.id);
if (!member) {
return false;
}
switch (action) {
case "create":
return member.permissions.canCreateDocument;
case "update":
return member.permissions.canUpdateDocument;
default:
return false;
}
}
}
To get started, create your models and actions, declare your policies, and encapsulate your authorization logic behind your PunditPolicy implementations. Here is an example:
// Create your Pundit instance:
import { Pundit } from 'pundit-ts';
import { PostPolicy, UserPolicy } from './policies';
const pundit = new Pundit<PolicyContext>()
.register(new UserPolicy())
.register(new PostPolicy());
Once integrated, authorization checks can be performed seamlessly:
const ctx = new PolicyContext();
const post = new Post();
await pundit.authorize(ctx, post, "create");
For more information, visit the Pundit-TS GitHub repository. This library offers developers a comprehensive, type-safe means to manage complex authorization logic.
No comments yet.
Sign in to be the first to comment.