Skip to content

Commit a0691e3

Browse files
gemanornikolasburk
andauthored
Permit.io Docs (#6962)
* Initial Version * Update content/200-orm/200-prisma-client/300-client-extensions/140-shared-extesions/100-permit-rbac.mdx * Update content/200-orm/200-prisma-client/300-client-extensions/140-shared-extesions/100-permit-rbac.mdx --------- Co-authored-by: Nikolas <[email protected]>
1 parent 2f9f1c0 commit a0691e3

File tree

1 file changed

+208
-3
lines changed
  • content/200-orm/200-prisma-client/300-client-extensions/140-shared-extesions

1 file changed

+208
-3
lines changed
Lines changed: 208 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,213 @@
11
---
2-
title: 'RBAC (Permit)'
2+
title: 'Fine-Grained Authorization (Permit)'
33
metaTitle: ''
4-
metaDescription: ''
4+
metaDescription: 'Learn how to implement RBAC, ABAC, and ReBAC authorization in your Prisma applications'
55
toc_max_heading_level: 4
66
---
77

8-
_add content about the Permit Client extension here_
8+
9+
10+
Database operations often require careful control over who can access or modify which data. While Prisma ORM excels at data modeling and database access, it doesn't include built-in authorization capabilities. This guide shows how to implement fine-grained authorization in your Prisma applications using the `@permitio/permit-prisma` extension.
11+
12+
Fine-grained authorization (FGA) provides detailed and precise control over what data users can access or modify at a granular level. Without proper authorization, your application might expose sensitive data or allow unauthorized modifications, creating security vulnerabilities.
13+
14+
## Access control models
15+
16+
This extension supports three access control models from Permit.io:
17+
18+
### Role-based Access Control (RBAC)
19+
20+
**What it is**: Users are assigned roles (Admin, Editor, Viewer) with predefined permissions to perform actions on resource types.
21+
22+
**Example**: An "Editor" role can update any document in the system.
23+
24+
**Best for**: Simple permission structures where access is determined by job function or user level.
25+
26+
### Attribute-Based Access Control (ABAC)
27+
28+
**What it is**: Access decisions based on attributes of users, resources, or environment.
29+
30+
**Examples**:
31+
32+
- Allow access if `user.department == document.department`
33+
- Allow updates if `document.status == "DRAFT"`
34+
35+
**How it works with the extension**: When `enableAttributeSync` is on, resource attributes are automatically synced to Permit.io for policy evaluation.
36+
37+
**Best for**: Dynamic rules that depend on context or data properties.
38+
39+
### Relationship-Based Access Control (ReBAC)
40+
41+
**What it is**: Permissions based on relationships between users and specific resource instances.
42+
43+
**Example**: A user is an "Owner" of document-123 but just a "Viewer" of document-456.
44+
45+
**How it works with the extension**:
46+
47+
- Resource instances are synced to Permit.io (with `enableResourceSync: true`)
48+
- Permission checks include the specific resource instance ID
49+
50+
**Best for**: Collaborative applications where users need different permissions on different instances of the same resource type.
51+
52+
### Choosing the right model
53+
54+
- **RBAC**: When you need simple, role-based access control
55+
- **ABAC**: When decisions depend on data properties or contextual information
56+
- **ReBAC**: When users need different permissions on different instances
57+
58+
## Usage
59+
60+
### Prerequisites
61+
62+
Before implementing fine-grained authorization with Prisma, make sure you have:
63+
64+
- A Prisma application with existing models and queries
65+
- Basic understanding of authorization concepts
66+
- Node.js and npm installed
67+
68+
### Installation
69+
70+
Install the extension alongside Prisma Client:
71+
72+
```terminal
73+
npm install @permitio/permit-prisma @prisma/client
74+
```
75+
76+
You'll also need to sign up for a [Permit account](https://app.permit.io) to define your authorization policies.
77+
78+
> **Note:**
79+
> Ensure that the Permit PDP container is running. It is recommended to run it using Docker for better performance, security, and availability. For instructions, refer to the Permit documentation: [Deploy Permit to Production](https://docs.permit.io/how-to/deploy/deploy-to-production/) and [PDP Overview](https://docs.permit.io/concepts/pdp/overview/).
80+
81+
## Basic setup
82+
83+
First, extend your Prisma Client with the Permit extension:
84+
85+
```typescript
86+
import { PrismaClient } from "@prisma/client";
87+
import { createPermitClientExtension } from "@permitio/permit-prisma";
88+
89+
const prisma = new PrismaClient().$extends(
90+
createPermitClientExtension({
91+
permitConfig: {
92+
token: process.env.PERMIT_API_KEY, // Your Permit API key
93+
pdp: "http://localhost:7766", // PDP address (local or cloud)
94+
},
95+
enableAutomaticChecks: true // Automatically enforce permissions
96+
})
97+
);
98+
```
99+
100+
## Implementing RBAC (Role-Based Access Control)
101+
102+
RBAC uses roles to determine access permissions. For example, "Admin" roles can perform all actions while "Viewer" roles can only read data.
103+
104+
1. **Define resources and actions in Permit.io dashboard**:
105+
- Create resources matching your Prisma models (e.g., "document")
106+
- Define actions (e.g., "create", "read", "update", "delete")
107+
- Create roles with permission sets (e.g., "admin", "editor", "viewer")
108+
2. **Set the active user in your code**:
109+
```typescript
110+
// Set the current user context before performing operations
111+
prisma.$permit.setUser("[email protected]");
112+
113+
// All subsequent operations will be checked against this user's permissions
114+
const documents = await prisma.document.findMany();
115+
```
116+
117+
## Implementing ABAC (Attribute-Based Access Control)
118+
119+
ABAC extends access control by considering user attributes, resource attributes, and context.
120+
121+
1. **Configure the extension for ABAC**:
122+
```typescript
123+
const prisma = new PrismaClient().$extends(
124+
createPermitClientExtension({
125+
permitConfig: { token: process.env.PERMIT_API_KEY, pdp: "http://localhost:7766" },
126+
enableAutomaticChecks: true,
127+
})
128+
);
129+
```
130+
2. **Set user with attributes:**
131+
```typescript
132+
prisma.$permit.setUser({
133+
134+
attributes: { department: "cardiology" }
135+
});
136+
137+
// Will succeed only if user department matches record department (per policy)
138+
const records = await prisma.medicalRecord.findMany({
139+
where: { department: "cardiology" }
140+
});
141+
```
142+
143+
## Implementing ReBAC (Relationship-Based Access Control)
144+
145+
ReBAC models permissions based on relationships between users and specific resource instances.
146+
147+
1. **Configure the extension for ReBAC**:
148+
```typescript
149+
const prisma = new PrismaClient().$extends(
150+
createPermitClientExtension({
151+
permitConfig: { token: process.env.PERMIT_API_KEY, pdp: "http://localhost:7766" },
152+
accessControlModel: "rebac",
153+
enableAutomaticChecks: true,
154+
enableResourceSync: true, // Sync resource instances with Permit.io
155+
enableDataFiltering: true // Filter queries by permissions
156+
})
157+
);
158+
```
159+
160+
2. ** Access instance-specific resources:**
161+
```typescript
162+
prisma.$permit.setUser("[email protected]");
163+
164+
// Will only succeed if the user has permission on this specific file
165+
const file = await prisma.file.findUnique({
166+
where: { id: "file-123" }
167+
});
168+
```
169+
170+
## Manual permission checks
171+
172+
For more control, you can perform explicit permission checks:
173+
174+
```typescript
175+
// Check if user can update a document
176+
const canUpdate = await prisma.$permit.check(
177+
"[email protected]", // user
178+
"update", // action
179+
"document" // resource
180+
);
181+
182+
if (canUpdate) {
183+
await prisma.document.update({
184+
where: { id: "doc-123" },
185+
data: { title: "Updated Title" }
186+
});
187+
}
188+
189+
// Or enforce permissions (throws if denied)
190+
await prisma.$permit.enforceCheck(
191+
192+
"delete",
193+
{ type: "document", key: "doc-123" }
194+
);
195+
```
196+
197+
## Common use cases
198+
199+
Here are some common scenarios where fine-grained authorization is valuable:
200+
201+
- **Multi-tenant applications**: Isolate data between different customers
202+
- **Healthcare applications**: Ensure patient data is only accessible to authorized staff
203+
- **Collaborative platforms**: Grant different permissions on shared resources
204+
- **Content management systems**: Control who can publish, edit, or view content
205+
206+
## Summary
207+
208+
By integrating the `@permitio/permit-prisma` extension with your Prisma ORM application, you can implement sophisticated authorization policies that protect your data and ensure users only access what they're permitted to see. The extension supports all major authorization models (RBAC, ABAC, ReBAC) and provides both automatic and manual permission enforcement.
209+
210+
## Next steps
211+
212+
- [Create a free Permit.io account](https://app.permit.io)
213+
- [View the full extension documentation](https://github.com/permitio/permit-prisma)

0 commit comments

Comments
 (0)