Security: Roles & Permissions
Relevant source files
The following files were used as context for generating this wiki page:
- packages/twenty-server/src/engine/metadata-modules/role/role.service.ts
- packages/twenty-server/src/engine/metadata-modules/object-permission/object-permission.service.ts
- packages/twenty-front/src/pages/settings/roles/SettingsRoles.tsx
- packages/twenty-front/src/pages/settings/security/SettingsSecurity.tsx
- packages/twenty-server/test/integration/graphql/suites/settings-permissions/roles.integration-spec.ts
- packages/twenty-server/test/integration/graphql/suites/settings-permissions/granular-settings-permissions.integration-spec.ts
- packages/twenty-server/test/integration/graphql/suites/settings-permissions/security.integration-spec.ts
The Roles & Permissions system is a critical security component in Twenty, designed to provide granular control over user access to data and application settings. It operates on a role-based access control (RBAC) model where permissions are assigned to roles, and users inherit permissions from the role they are assigned. This allows administrators to define what different groups of users can see and do within the workspace.
The system is composed of three main concepts: Roles, which are collections of permissions; Object & Field Permissions, which control CRUD access to CRM data; and Permission Flags, which manage access to specific settings pages. This architecture enables a flexible and secure multi-user environment, ensuring that users only have access to the information and functionality necessary for their responsibilities.
System Architecture
The security model is built around a set of interconnected entities and services that work together to enforce access control rules.
Data Model
The core entities of the permission system are Role, ObjectPermission, FieldPermission, and PermissionFlag. These are linked to WorkspaceMember to grant users their respective access rights.
The diagram below illustrates the relationships between these core security entities.
Sources: packages/twenty-server/src/engine/metadata-modules/role/role.service.ts, packages/twenty-server/src/engine/metadata-modules/object-permission/object-permission.service.ts, packages/twenty-server/test/integration/graphql/suites/settings-permissions/granular-settings-permissions.integration-spec.ts
Backend Services
The backend logic for managing roles and permissions is primarily handled by two NestJS services: RoleService and ObjectPermissionService.
RoleService: Manages the lifecycle of roles, including creation, updates, and deletion. It also handles assigning roles to workspace members.createOneRole: Creates a new custom role.updateOneRole: Updates the properties of an existing role, such as its label.updateWorkspaceMemberRole: Changes the role assigned to a specific user.upsertPermissionFlags: Adds or removes granular settings permissions (flags) for a role.
ObjectPermissionService: Manages permissions related to CRM objects and fields.upsertObjectPermissions: Sets CRUD permissions for a role on one or more objects.upsertFieldPermissions: Sets read/write permissions for a role on specific fields within an object.
Sources: packages/twenty-server/src/engine/metadata-modules/role/role.service.ts, packages/twenty-server/src/engine/metadata-modules/object-permission/object-permission.service.ts
Role Management
Roles are managed through the "Roles" page in the workspace settings. Administrators can create custom roles, edit their permissions, and assign them to users.
Default Roles
Upon workspace creation, a set of default roles is available. Some of these are not editable to ensure system stability.
| Role Name | Description | Editable |
|---|---|---|
| Admin | Full access to all data and settings. | No |
| Member | Standard access to CRM data, with limited settings access. | No |
| Guest | Read-only access. | No |
| Object-restricted | A role for demonstrating restricted object access. | Yes |
| Workflow Manager | A role with permissions to manage workflows. | Yes |
GraphQL API for Roles
Role and permission management is exposed via the GraphQL API. The following mutations are key to this system.
| Mutation | Description |
|---|---|
createOneRole(createRoleInput: {label: String!}) | Creates a new, editable role. |
updateOneRole(updateRoleInput: {id: ID!, update: {label: String}}) | Updates the properties of an editable role. |
updateWorkspaceMemberRole(workspaceMemberId: ID!, roleId: ID!) | Assigns a role to a workspace member. An admin cannot change their own role. |
deleteOneRole(id: ID!) | Deletes a custom role. |
Sources: packages/twenty-server/test/integration/graphql/suites/settings-permissions/roles.integration-spec.ts:257-372, packages/twenty-server/test/integration/graphql/suites/settings-permissions/roles.integration-spec.ts:182-205
Frontend UI
The SettingsRoles.tsx component provides the user interface for managing roles. It fetches all roles and their assigned members, allowing admins to create, edit, and assign roles.
The following sequence diagram shows the flow for updating a role's permissions from the frontend.
Permission Granularity
The system allows for fine-grained control over both data access and settings access.
Object & Field Permissions
Access to CRM objects (e.g., Companies, Opportunities) and their individual fields can be configured per role. This is managed through the upsertObjectPermissions and upsertFieldPermissions mutations.
- Object Permissions: Control
read,update,soft delete, anddestroyactions on records of a specific object type. - Field Permissions: Control
readandupdateaccess to the value of a specific field.
An admin can grant a custom role the ability to update records of a "House" object but not delete them.
Sources: packages/twenty-server/src/engine/metadata-modules/object-permission/object-permission.service.ts:29-65, packages/twenty-server/test/integration/graphql/suites/settings-permissions/roles.integration-spec.ts:413-455
Settings Permissions (Permission Flags)
For roles where canUpdateAllSettings is false, access to specific areas of the workspace settings can be granted using PermissionFlagType. This allows non-admins to manage certain parts of the system without having full administrative privileges.
The upsertPermissionFlags mutation is used to assign these flags to a role.
| Permission Flag | Controlled Setting Area |
|---|---|
DATA_MODEL | Creating and editing custom objects and fields. |
WORKSPACE | General workspace settings like name and logo. |
WORKFLOWS | Creating and managing automation workflows. |
ROLES | Creating and editing roles and their permissions. |
WORKSPACE_MEMBERS | Inviting and managing workspace members. |
API_KEYS_AND_WEBHOOKS | Managing API keys and webhooks. |
SECURITY | Managing authentication providers and security policies. |
Sources: packages/twenty-server/test/integration/graphql/suites/settings-permissions/granular-settings-permissions.integration-spec.ts:60-70, packages/twenty-server/src/engine/metadata-modules/role/role.service.ts:190-213
Workspace Security Settings
Global security settings for the workspace are managed on the "Security" settings page. These settings are controlled by the SECURITY permission flag or by being an Admin.
Authentication Providers
Administrators can enable or disable different methods for users to log in. This is handled by the updateWorkspace mutation.
- Google Auth:
isGoogleAuthEnabled - Microsoft Auth:
isMicrosoftAuthEnabled - Password Auth:
isPasswordAuthEnabled
The SettingsSecurity.tsx component provides toggles for an admin to manage these settings. A user without the appropriate permissions will see a "Permission Denied" error if they attempt to change these settings.
Sources: packages/twenty-front/src/pages/settings/security/SettingsSecurity.tsx, packages/twenty-server/test/integration/graphql/suites/settings-permissions/security.integration-spec.ts:60-170
Public Invite Link
The ability for anyone with a link to join the workspace can be enabled or disabled. This is controlled by the isPublicInviteLinkEnabled boolean field in the updateWorkspace mutation.
Conclusion
Twenty's security model provides a robust and flexible framework for managing user access. By combining roles, object-level permissions, and granular settings flags, administrators can tailor the user experience to fit the security and operational needs of their organization. The system is designed to be extensible, with a clear separation between backend logic, API, and frontend implementation, allowing for future enhancements to the permission model.