feat(rbac): refactor authorization to dynamic permission-based rbac and centralize constants
Overview
Refactored the authorization architecture from hardcoded role checking (requireRole) to fully dynamic permission-based authorization (requirePermission), where roles contain dynamic permissions and authorization is evaluated strictly against user permissions.
Key Changes
-
Dynamic RBAC Middleware: Replaced
requireRolewithrequirePermissionacross all API route modules (wallets,transactions,transfers,categories,budgets,saving-goals,reports,forecast,simulations,anomalies,subscriptions,query,recurring-transactions,notifications,reminders,ai-assistant,uploads,users,rbac). -
Centralized Constants (
src/common/constants/):-
PERMISSIONS: Centralized dictionary of 55 granular system permissions across 18 resources inpermission.constant.ts. -
SYSTEM_ROLES: Centralized constants (ADMIN,USER,MANAGER) for system bootstrapping and lifecycle protections insystem-role.constant.ts. - Removed obsolete
role.constant.tsandrole.middleware.ts.
-
-
System Invariants & Safety:
- Protected system roles (
ADMIN,USER,MANAGER) from physical deletion. - Protected the
ADMINrole from renaming. - Added protection to prevent soft-deleting the last active system administrator.
- Protected system roles (
- Audit Logging: Recorded detailed audit logs for all role and permission assignment mutations.
-
Dynamic User Filter: Updated
findAllUserSchemato accept dynamicroleNamestring filter.