Skip to content

feat: production-grade RBAC with roles, permissions, ownership guards…#40

Open
jhayniffy wants to merge 3 commits into
MyFanss:mainfrom
jhayniffy:feature/rbac-roles-permissions-ownership
Open

feat: production-grade RBAC with roles, permissions, ownership guards…#40
jhayniffy wants to merge 3 commits into
MyFanss:mainfrom
jhayniffy:feature/rbac-roles-permissions-ownership

Conversation

@jhayniffy

Copy link
Copy Markdown
Contributor

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

jhayniffy and others added 3 commits June 20, 2026 00:12
… & 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'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Role-Based Access Control (RBAC) Foundation

1 participant