Feat: Introduce user roles, implement role-based access control middleware - #27
Merged
Merged
Conversation
ONEONUORA
approved these changes
Jan 23, 2026
ONEONUORA
left a comment
Contributor
There was a problem hiding this comment.
Nice implementation @GideonBature
GideonBature
force-pushed
the
auth-role-guards
branch
from
January 24, 2026 21:46
1af6b6f to
23d08ed
Compare
…eware, and update authentication to include user roles
GideonBature
force-pushed
the
auth-role-guards
branch
from
January 24, 2026 21:48
23d08ed to
8791716
Compare
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.
Summary
This PR introduces a Role-Based Access Control (RBAC) system to the backend. It adds a
rolefield to users, updates the JWT claims to carry permissions, and implements middleware guards to protect specific routes based on user roles (Admin,Merchant,User).Key Changes
1. Database & Models
backend/migrations/20260123000000_add_user_roles.sqlto add arolecolumn to theuserstable (defaults to'user').Userstruct to include therolefield.Roleenum insrc/role.rswith variants:User,Merchant,Admin. Implementedhas_permissionlogic to handle role hierarchy (e.g., Admin has access to Merchant/User scopes).2. Authentication & JWT
generate_jwtandvalidate_jwtinsrc/auth.rsto include the user'srolein the token claims.IdentityServiceto handle role assignment during user creation and retrieval.3. Middleware & Security
src/middleware/role_guard.rsimplementing:require_role: strict role checking.require_any_role: checks against a list of allowed roles.admin_only&merchant_or_admin: helper guards.src/app.rsto applyrole_guard::require_role(Role::Admin)to administration routes (dashboard stats, system health, etc.).4. Testing
backend/tests/auth_guard_test.rscovering:Database Migrations
20260123000000_add_user_roles.sql)Userrole by default.Testing Plan
cargo testto execute the new auth guard and role tests./admin/*routes (should return 403 Forbidden).adminin the database./admin/*routes.Closes #8