feat: production-grade RBAC with roles, permissions, ownership guards…#40
Open
jhayniffy wants to merge 3 commits into
Open
feat: production-grade RBAC with roles, permissions, ownership guards…#40jhayniffy wants to merge 3 commits into
jhayniffy wants to merge 3 commits into
Conversation
… & admin role controls - Add UserRole enum (fan/creator/admin) with hierarchy and Permission enum stub - Extend JWT payload to include role; JwtStrategy attaches userId, email, role - Add @currentuser() decorator for typed user injection - Implement RolesGuard (exact match + @roles), PermissionsGuard, PoliciesGuard - Add UserOwnerPolicy and AdminOverridePolicy for owner-or-admin access - Add PATCH /admin/users/:id/role (admin-only); last-admin demotion protection - Strip role from CreateUserDto/UpdateUserDto; reject client self-promotion - Log all role changes with actor, target, old role, new role via AppLogger - Return 403 INSUFFICIENT_ROLE for role failures (not 401) - Add @ApiBearerAuth + @ApiForbiddenResponse on all protected routes - TypeORM migration to backfill existing users role column to 'fan' - Fix token.service.spec.ts: pass role as 3rd arg to issueTokenPair - Fix users.controller.spec.ts: pass currentUser to ownership-checked methods - Fix admin-users.controller.ts: add @request() decorator to req param - Add 12 RBAC unit test cases in test/rbac.e2e-spec.ts - Add docs/rbac.md covering role definitions, decorators, ownership pattern
- updateUser/updateProfile: compare currentUser.userId (not .id) against route :id param - coerce :id param to Number to avoid string vs number mismatch - align JwtPayload interface in PermissionService to use userId field - update permission.service.spec.ts and users.controller.spec.ts mock payloads accordingly Fixes auth e2e: 'password change invalidates all active refresh sessions'
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.
Introduce a foundational Role-Based Access Control (RBAC) layer on top of existing JWT authentication.
Changes
Add role-based authorization for fans, creators, and admins.
Implement route protection based on assigned roles.
Support fine-grained permission checks for protected actions.
Enforce resource ownership rules (resource owner OR admin access).
Add secure role assignment workflows.
Prevent privilege escalation through authorization safeguards.
Notes
This establishes the core authorization framework for MyFans while maintaining compatibility with the current JWT authentication flow. The implementation is designed to be extensible, allowing future modules such as creators, subscriptions, and payouts to integrate with the authorization system without requiring major auth refactoring.
closes #10