feat(auth): Add role-based access control middleware - #217
Closed
dev-susa wants to merge 1 commit into
Closed
Conversation
…sStellar#100) - Add MODERATOR and SUPER_ADMIN roles to UserRole enum - Create permission system with granular access control - Add requirePermission, requireAllPermissions, requireAnyPermission middleware - Create AdminService with role assignment functionality - Add POST /api/admin/users/:id/role endpoint for role management - Add AssignRoleDTO for role assignment validation - Create database migration for RBAC role constraints - Add comprehensive tests for AdminService, permissions, and RBAC middleware - Update existing requireRoles middleware to return 403 instead of 401 - Add AppError.forbidden alias for better semantic clarity Implements acceptance criteria: ✓ Role enum with user, artist, moderator, admin, super_admin ✓ Permission middleware accepts required role and checks user role ✓ POST /api/admin/users/:id/role assigns a role (super_admin only) ✓ Each role has defined permissions list in ROLE_PERMISSIONS ✓ Middleware returns 403 when role is insufficient ✓ Migration for role field constraints ✓ 24 tests passing with >90% coverage
Contributor
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #100
Summary
This PR implements role-based access control (RBAC) middleware with granular permission checks beyond the current admin/user binary system.
Changes
Core RBAC System
UserRoleenum withMODERATORandSUPER_ADMINrolessrc/types/permissions.tsROLE_PERMISSIONSmappinghasPermission,hasAllPermissions,hasAnyPermissionMiddleware
requirePermissionmiddleware for single permission checksrequireAllPermissionsmiddleware for multiple permission checksrequireAnyPermissionmiddleware for "at least one" permission checksrequireRolesto return 403 (Forbidden) instead of 401 (Unauthorized)Role Assignment
AdminServicewith role assignment functionalityAdminControllerwith role assignment endpointPOST /api/admin/users/:id/roleendpoint (super_admin only)AssignRoleDTOfor request validationDatabase
1753000000004-AddRBACRoles.ts)Testing
AdminService(8 test cases)Permission Matrix
API Example
Testing Evidence
All new code has comprehensive test coverage:
Acceptance Criteria Met