Role Management System
This guide explains how to manage roles and permissions in the DeployStack backend for developers.Overview
DeployStack uses a centralized role-based access control (RBAC) system with automatic synchronization between code definitions and database storage.Role Definitions
All roles and permissions are defined in a single file:services/backend/src/permissions/index.ts
This is the single source of truth for all role definitions. To see current roles and their permissions, check this file.
Role Types
The system supports two types of roles:- Global Roles: System-wide roles (
global_admin
,global_user
) - Team Roles: Team-specific roles (
team_admin
,team_user
)
Database Storage
Roles are stored in theroles
table with the following structure:
id
- Role identifier (e.g., ‘global_admin’)name
- Display name (e.g., ‘Global Administrator’)description
- Role descriptionpermissions
- JSON array of permission stringsis_system_role
- Boolean flag for core system rolescreated_at
/updated_at
- Timestamps
authUser.role_id
column.
Automatic Synchronization
The system automatically syncs role permissions from code to database on server startup. The RoleSyncService compares the definitions inpermissions/index.ts
with the database and updates any differences. This ensures the database always matches your code definitions without manual intervention.
Adding New Roles
1. Define the Role
Add your new role toservices/backend/src/permissions/index.ts
:
2. Create Database Migration (Optional)
If you want the role available immediately in existing databases, create a migration:3. Restart Server
The RoleSyncService will automatically populate the role’s permissions from your code definition on the next server startup.Permission System
Available Permissions
All permissions are auto-generated from role definitions. TheAVAILABLE_PERMISSIONS
array in permissions/index.ts
contains all unique permissions across all roles, sorted alphabetically.
Permission Checking
Use the middleware functions for route protection:Programmatic Checks
API Endpoints
Role Management
GET /api/roles
- List all rolesPOST /api/roles
- Create new rolePUT /api/roles/:id
- Update roleDELETE /api/roles/:id
- Delete role
User Role Assignment
PUT /api/users/:id/role
- Assign role to userGET /api/users/me
- Get current user with role info
Best Practices
- Single Source: Always define roles in
permissions/index.ts
- Descriptive Names: Use clear permission names with dot notation (
users.edit
,content.moderate
) - Minimal Permissions: Give roles only the permissions they need
- System Roles: Mark core roles as
is_system_role: true
to prevent deletion - Testing: Test permission changes in development before deploying
Security Notes
- System roles cannot be modified or deleted via API
- Users cannot change their own roles
- The last global administrator cannot be deleted
- All permission checks happen server-side