Role-Based Access Control (RBAC)
Relevant source files
The following files were used as context for generating this wiki page:
The Role-Based Access Control (RBAC) system in n8n provides a structured approach to managing user permissions. It is designed to control what actions users can perform within the application by assigning them specific roles. Each role is a collection of fine-grained permissions, known as scopes. This system allows for flexible and secure management of user access across different parts of the application, such as global settings and specific projects.
The core of the RBAC system involves Roles and Scopes. Roles are defined as system-level constructs and are synchronized with the database upon application startup by the AuthRolesService. This ensures that the permission sets are always up-to-date with the application's code definitions.
Core Concepts
The RBAC implementation is built on two primary concepts: Roles and Scopes.
- Role: A role is a named set of permissions that can be assigned to a user. n8n defines several system roles (e.g., Owner, Admin, Member) with predefined permission sets. Roles can be of different types, such as
globalorproject, to delineate their area of influence. - Scope: A scope represents a single, specific permission within the system (e.g.,
user:create,workflow:read). Roles are composed of multiple scopes, and it is the combination of these scopes that determines a user's capabilities.
Sources: packages/db/src/entities/role.ts:18-24, packages/@n8n/db/src/services/auth.roles.service.ts:11-12
Data Model
The RBAC system's data is persisted in the database using two main entities: Role and Scope. They share a many-to-many relationship, where a single role can have multiple scopes, and a single scope can be part of multiple roles.
The diagram below illustrates the relationship between the Role and Scope entities.
Role Entity
The Role entity stores information about each role available in the system. System-defined roles are marked with the systemRole flag.
| Field | Type | Description |
|---|---|---|
id | string | Unique identifier for the role. |
slug | string | A unique, machine-readable name for the role (e.g., global:owner). |
displayName | string | A human-readable name for the role (e.g., "Owner"). |
description | string | null | A brief description of the role's purpose. |
roleType | 'global' | 'project' | The type of the role, indicating its context. |
systemRole | boolean | Flag indicating if the role is a built-in system role. |
scopes | Scope[] | A collection of Scope entities associated with this role. |
createdAt | Date | Timestamp of when the role was created. |
updatedAt | Date | Timestamp of the last update. |
Sources: packages/db/src/entities/role.ts:11-54
A code snippet of the Role entity definition:
// packages/db/src/entities/role.ts:11-54
@Entity()
export class Role extends BaseEntity {
// ...
@Column({
unique: true,
})
slug: string;
@Column()
displayName: string;
@Column({
type: 'text',
nullable: true,
})
description: string | null;
@Column({
type: 'enum',
enum: ['global', 'project'],
})
roleType: 'global' | 'project';
@Column({
default: false,
})
systemRole: boolean;
// ...
@ManyToMany(() => Scope, (scope) => scope.roles, {
cascade: true,
eager: true,
})
@JoinTable()
scopes: Scope[];
// ...
}
System Roles and Scopes
n8n comes with a set of predefined system roles. These roles are defined in the source code and synchronized with the database.
Global Roles
Global roles apply to the entire n8n instance. The primary global roles are defined as constants and validated using a Zod schema.
| Role Slug | Description |
|---|---|
global:owner | The instance owner with full permissions. |
global:admin | An administrator with extensive, but not full, permissions. |
global:member | A standard user with limited permissions. |
default | A default role for new users before full setup. |
Sources: packages/@n8n/api-types/src/schemas/user.schema.ts:6-10, packages/@n8n/permissions/src/roles/role-maps.ee.ts:10-15
The roles are defined in a constant and a Zod schema for validation in API types:
// packages/@n8n/api-types/src/schemas/user.schema.ts:6-13
export const ROLE = {
Owner: 'global:owner',
Member: 'global:member',
Admin: 'global:admin',
Default: 'default', // default user with no email when setting up instance
} as const;
// ...
export const roleSchema = z.enum(roleValuesForSchema);
Role to Scope Mapping
The permissions for each system role are determined by a predefined list of scopes. These mappings are stored in GLOBAL_ROLES.
For example, the global:owner role has a comprehensive set of scopes, granting it full control over the instance. The global:member role has a more restricted set of scopes. The full list of scopes for each role is extensive and defined in the source.
Sources: packages/@n8n/permissions/src/roles/role-maps.ee.ts:10-101
Initialization and Synchronization
The AuthRolesService is responsible for ensuring that the roles and scopes defined in the source code are correctly represented in the database. This process runs when the application starts.
The diagram below shows the initialization flow.
AuthRolesService
This service class orchestrates the synchronization of roles and scopes.
The init method calls syncScopes and syncRoles in sequence.
syncScopes(): This method ensures all scopes defined inALL_SCOPESexist in the database with the correctdisplayNameanddescription. It will create new scopes or update existing ones as needed.syncRoles(): This method performs a similar function for roles. It iterates through the roles defined inALL_ROLES, checks if they exist in the database as system roles, and creates or updates them. A critical part of this step is assigning the correct set of scopes to each role based on the definitions in the source code.
Sources: packages/@n8n/db/src/services/auth.roles.service.ts:15-117, packages/@n8n/permissions/src/index.ts:2-4
Summary
The RBAC system in n8n is a robust mechanism for managing user permissions through roles and scopes. Its design, centered around database-persisted entities that are synchronized with code-based definitions, ensures consistency and maintainability. The AuthRolesService plays a key role in initializing the system, while the clear separation of roles and scopes allows for both broad and granular control over user access throughout the application.