---
name: epic-permissions
description: Guide on RBAC system and permissions for Epic Stack
categories:
- permissions
- rbac
- access-control
---
# Epic Stack: Permissions
## When to use this skill
Use this skill when you need to:
- Implement role-based access control (RBAC)
- Validate permissions on server-side or client-side
- Create new permissions or roles
- Restrict access to routes or actions
- Implement granular permissions (`own` vs `any`)
## Patterns and conventions
### Permissions Philosophy
Following Epic Web principles:
**Explicit is better than implicit** - Always explicitly check permissions.
Don't assume a user has access based on implicit rules or hidden logic. Every
permission check should be visible and clear in the code.
**Example - Explicit permission checks:**
```typescript
// ✅ Good - Explicit permission check
export async function action({ request }: Route.ActionArgs) {
const userId = await requireUserId(request)
// Explicitly check permission - clear and visible
await requireUserWithPermission(request, 'delete:note:own')
// Permission check is explicit and obvious
await prisma.note.delete({ where: { id: noteId } })
}
// ❌ Avoid - Implicit permission check
export async function action({ request }: Route.ActionArgs) {
const userId = await requireUserId(request)
const note = await prisma.note.findUnique({ where: { id: noteId } })
// Implicit check - not clear what permission is being checked
if (note.ownerId !== userId) {
throw new Response('Forbidden', { status: 403 })
}
// What permission does this represent? Not explicit
}
```
**Example - Explicit permission strings:**
```typescript
// ✅ Good - Explicit permission string
const permission: PermissionString = 'delete:note:own'
// Clear: action (delete), entity (note), access (own)
await requireUserWithPermission(request, permission)
// ❌ Avoid - Implicit or unclear permissions
const canDelete = checkUserCanDelete(user, note)
// What permission is this checking? Not explicit
```
### RBAC Model
Epic Stack uses an RBAC (Role-Based Access Control) model where:
- **Users** have **Roles**
- **Roles** have **Permissions**
- A user's permissions are the union of all permissions from their roles
### Permission Structure
Permissions follow the format: `action:entity:access`
**Components:**
- `action`: The allowed action (`create`, `read`, `update`, `delete`)
- `entity`: The entity being acted upon (`user`, `note`, etc.)
- `access`: The access level (`own`, `any`, `own,any`)
**Examples:**
- `create:note:own` - Can create own notes
- `read:note:any` - Can read any note
- `delete:user:any` - Can delete any user (admin)
- `update:note:own` - Can update only own notes
### Prisma Schema
**Models:**
```prisma
model Permission {
id String @id @default(cuid())
action String // e.g. create, read, update, delete
entity String // e.g. note, user, etc.
access String // e.g. own or any
description String @default("")
roles Role[]
@@unique([action, entity, access])
}
model Role {
id String @id @default(cuid())
name String @unique
description String @default("")
users User[]
permissions Permission[]
}
model User {
id String @id @default(cuid())
// ...
roles Role[]
}
```
### Validate Permissions Server-Side
**Require specific permission:**
```typescript
import { requireUserWithPermission } from '#app/utils/permissions.server.ts'
export async function action({ request }: Route.ActionArgs) {
const userId = await requireUserWithPermission(
request,
'delete:note:own', // Throws 403 error if doesn't have permission
)
// User has the permission, continue...
}
```
**Require specific role:**
```typescript
import { requireUserWithRole } from '#app/utils/permissions.server.ts'
export async function loader({ request }: Route.LoaderArgs) {
const userId = await requireUserWithRole(request, 'admin')
// User has admin role, continue...
}
```
**Conditional permissions (own vs any) - explicit:**
```typescript
export async function action({ request }: Route.ActionArgs) {
const userId = await requireUserId(request)
// Explicitly determine ownership
const note = await prisma.note.findUnique({
where: { id: noteId },
select: { ownerId: true },
})
const isOwner = note.ownerId === userId
// Explicitly check the appropriate permission based on ownership
await requireUserWithPermission(
request,
isOwner ? 'delete:note:own' : 'delete:note:any', // Explicit permission string
)
// Permission check is explicit and clear
// Proceed with deletion...
}
```
### Validate Permissions Client-Side
**Check if user has permission:**
```typescript
import { userHasPermission, useOptionalUser } from '#app/utils/user.ts'
export default function NoteRoute({ loaderData }: Route.ComponentProps) {
const user = useOptionalUser()
const isOwner = user?.id === loaderData.note.ownerId
const canDelete = userHasPermission(
user,
isOwner ? 'delete:note:own' : 'delete:note:any',
)
return (
{canDelete && (
)}
)
}
```
**Check if user has role:**
```typescript
import { userHasRole } from '#app/utils/user.ts'
export default function AdminRoute() {
const user = useOptionalUser()
const isAdmin = userHasRole(user, 'admin')
if (!isAdmin) {
return