• ShipSaaSDocumentation
  • Introduction
  • Getting Started
    • Installation
    • Environment Setup
    • Database Setup
    • Development Guide
  • Database
    • Repository Pattern
    • Drizzle Queries
    • Migration Workflow
    • Drizzle Studio
    • Best Practices
  • File Management
    • File Upload Component
    • Server Actions
    • Storage Configuration
  • Authentication Methods
    • Credentials Authentication
    • Magic Link Authentication
    • Two-Factor Authentication (2FA)
    • Social Authentication
    • API Keys & Bearer Tokens
    • Organizations (Multi-tenant)
  • Authorization & Permissions
    • Roles & Hierarchy
    • Route Protection
    • API Protection
    • CASL Permissions
  • Email & Notifications
    • Resend Setup
    • React Email Templates
    • Notification System
    • Email Workflows
    • Customization & Extension
  • Stripe & Payments
    • Stripe Setup
    • Better Auth Integration
    • Checkout Methods
    • Subscription Management
    • Admin Dashboard
    • Webhook Events
  • Architecture & Development
    • Presentation Layer
    • Service Layer
    • Persistence Layer
    • Helpers Architecture
    • Project Structure
    • Development Workflow
  • Testing
    • General Testing
    • Unit Testing
    • UI Component Testing
    • Repository Testing
    • Service Testing
    • End-to-End Testing
  • Deployment
    • Vercel Deployment
    • GitHub Actions CI/CD
    • Database Backup & Restore
    • Database Migrations
    • Troubleshooting Deployment
  • AI Agents Integration
  • Blog Management
    • Blog Setup
    • MDX Blog
    • Database Blog
  • Async Tasks
    • Setup
    • Email Workflows
    • Payment Processing
    • Maintenance Jobs
  1. Documentation
  2. Authorization & Permissions
  3. Roles & Hierarchy
Buydocschangelogsdemocommunity

Roles & Hierarchy

PreviousAuthorization & PermissionsNextRoute Protection
On this page
Role HierarchyRole DescriptionsRole ManagementAssigning RolesRole Hierarchy ValidationCommon Role ScenariosBest Practices

The boilerplate includes a hierarchical role system where higher roles inherit permissions from lower roles, providing flexible and scalable access control.

Perfect for: Team management, progressive access levels, and enterprise role structures.

Role Hierarchy

The boilerplate includes a hierarchical role system where higher roles inherit permissions from lower roles:

SUPER_ADMIN  ← Highest level (all permissions)
    ↑
  ADMIN      ← Admin panel access
    ↑
REDACTOR     ← Content management
    ↑
MODERATOR    ← Community management
    ↑
  USER       ← Basic authenticated user
    ↑
 GUEST       ← Unauthenticated user

Role inheritance: A MODERATOR automatically has all USER permissions, an ADMIN has all MODERATOR + USER permissions, etc.

Role Descriptions

Unauthenticated users

Permissions:

  • View public content
  • Access landing pages
  • Use login/register forms
  • Read documentation

Cannot: Access any protected features

Basic authenticated users

Permissions:

  • Access dashboard
  • Update own profile
  • Create own content
  • View own data

Cannot: Access admin features or modify other users' data

Community management

Permissions:

  • All USER permissions +
  • Moderate content
  • Manage comments
  • Handle user reports
  • Basic content management

Cannot: Access admin panel or user management

Content management

Permissions:

  • All MODERATOR permissions +
  • Create/edit articles
  • Manage content categories
  • Publish content
  • Content workflow management

Cannot: Access admin panel or user management

Administrative access

Permissions:

  • All REDACTOR permissions +
  • User management
  • System settings
  • Analytics access
  • Billing management

Cannot: Delete organization or access super admin features

Complete system access

Permissions:

  • All ADMIN permissions +
  • System configuration
  • Delete organizations
  • Manage other admins
  • Access all features

Can do everything in the system

Role Management

Assigning Roles

typescript
// Admin function to assign roles
export async function assignRole(userId: string, newRole: Role) {
  // Verify current user has permission to assign roles
  const currentUser = await getCurrentUser()
  const ability = await definePermissions(currentUser)

  if (!ability.can('manage', 'User')) {
    throw new Error('Insufficient permissions to assign roles')
  }

  // Update user role
  await updateUserRole(userId, newRole)

  // Log role change for audit
  await logRoleChange({
    userId,
    oldRole: user.role,
    newRole,
    assignedBy: currentUser.id
  })
}

Role Hierarchy Validation

typescript
// Ensure role hierarchy is respected
export function canAssignRole(assignerRole: Role, targetRole: Role): boolean {
  const roleHierarchy = {
    'GUEST': 0,
    'USER': 1,
    'MODERATOR': 2,
    'REDACTOR': 3,
    'ADMIN': 4,
    'SUPER_ADMIN': 5
  }

  const assignerLevel = roleHierarchy[assignerRole]
  const targetLevel = roleHierarchy[targetRole]

  // Can only assign roles lower than or equal to your own
  return assignerLevel >= targetLevel
}

Common Role Scenarios

Typical use cases for each role:

GUEST (Public Users):

  • Marketing website visitors
  • Potential customers browsing features
  • Users reading documentation

USER (Customers):

  • Paying subscribers
  • Individual users of SaaS features
  • Personal account management

MODERATOR (Community Managers):

  • Forum moderators
  • Content reviewers
  • Customer support agents

REDACTOR (Content Creators):

  • Blog writers
  • Content managers
  • Marketing team members

ADMIN (System Administrators):

  • IT administrators
  • Business managers
  • Billing administrators

SUPER_ADMIN (System Owners):

  • Company founders
  • Technical leads
  • System architects

Best Practices

1

Start with minimal roles

Begin with USER and ADMIN, add others as needed.

2

Document role permissions

Keep clear documentation of what each role can do.

3

Regular role audits

Review and update user roles periodically.

4

Principle of least privilege

Give users the minimum role needed for their tasks.

Role system ready! Your application now has a clear, hierarchical role structure that scales with your business needs.