Role based access control
A flexible and secure permission management system for workspaces.
Our RBAC system provides flexible and secure permission management for workspaces, allowing fine-grained control over who can access and modify resources.
Initial Setup
When setting up the project for the first time, you need to seed the database to initialize the RBAC system.
To seed the database with default roles and permissions:
This will create:
- All default permissions in the database
- Three default roles (Owner, Admin, Member)
- Role-permission relationships
Permissions System
Our platform uses a granular permission system to control access to resources. Permissions follow a resource:action format.
Available Permissions
Predefined permissions constants:
Member Permissions
view:members
- View workspace memberscreate:members
- Add new membersupdate:members
- Update member detailsdelete:members
- Delete membersmanage:members
- Full member managementinvite:members
- Send invitationsremove:members
- Remove members
Workspace Permissions
manage:roles
- Manage role assignmentsmanage:workspace
- Manage workspace settingsdelete:workspace
- Delete workspacetransfer:ownership
- Transfer ownership
Item Permissions
view:items
- View workspace itemscreate:items
- Create new itemsupdate:items
- Update existing itemsdelete:items
- Delete items
Default Roles
The system comes with predefined roles that have specific permissions. These roles provide a starting point for access control in your workspaces.
Default Roles Configuration
Predefined roles with their permissions:
Owner
All Permissions
Workspace owner with full administrative access
Permissions: All permissions (*)
Admin
Limited Permissions
Workspace administrator with member management capabilities
Permissions: view:members, create:members, update:members, delete:members
Member
Basic Access
Regular workspace member with basic access
Permissions: view:members
The RBAC system is automatically initialized during database seeding. Permissions and roles are created with predefined settings but can be modified through the admin interface.
Database Schema
The RBAC system uses a relational database structure to manage roles and permissions:
Technical Implementation
The RBAC system uses a many-to-many relationship between roles and permissions, implemented using PostgreSQL through Drizzle ORM. This allows for flexible permission management and easy role assignments.
Permission Checking
Check if a user has a specific permission:
Implementation Details
The permission checking functions are implemented as follows:
React Components
Authorization Component
Create a component to conditionally render UI based on permissions:
Usage example:
Client-Side Permission Hook
For client components that need to check permissions:
Usage example:
Best Practices
Security
- Always check permissions on both client and server
- Use middleware for API routes to validate permissions
- Avoid hardcoding permission checks in UI components
- Implement least privilege principle for roles
Performance
- Cache permission results when appropriate
- Use batch permission checking for multiple permissions
- Optimize database queries for permission checks
- Consider denormalization for frequently checked permissions
Maintenance
- Use constants for permission names
- Document new permissions when adding them
- Create migration scripts for permission changes
- Test permission changes thoroughly
Additional Resources
Member Management
See the Members Management documentation for more information on member roles.
Multi-Workspace
Learn about Multi-Workspace management in the application.