Add Role-Based Access Control (RBAC) for Admin and Regular Users Team 153#86
Merged
krishnapaljadeja merged 3 commits intogdg-charusat:mainfrom Mar 1, 2026
Merged
Conversation
Contributor
Contributor
|
@Nency02 can you please resolve these conflicts and rebase it . thankyou |
…r users - Add Role enum (USER, ADMIN) to Prisma schema with default USER - Add role field to User model (backward compatible) - Add requireRole() middleware factory and requireAdmin shorthand - Update authenticate middleware to include role in req.user - Create admin.controller.js with: getAllUsers, updateUserRole, deleteUser, getAllChallenges, deleteChallenge, getPlatformStats - Create admin.routes.js with all routes protected by requireAdmin - Register /api/admin routes in app.js - Expose role field in auth service register/login/getProfile responses - Add admin seed user (admin / Admin@1234) - Add Prisma migration SQL for Role enum and role column Closes gdg-charusat#81
- Restore PULL_REQUEST_TEMPLATE.md to blank template (revert PR-specific content that should not modify the template) - Remove duplicate prisma version entry in package-lock.json (keep ^5.22.0, remove ^5.8.0)
…enum - Keep Role enum (USER, ADMIN) from feat/rbac-admin-roles - Keep ChallengeInvite model from main - Restore challengeInvites relation on User model - Restore invites relation on Challenge model
59fdc1c to
1bf409c
Compare
Contributor
🎉 PR Merged — Points Awarded!Congratulations @Nency02! Your contribution has been merged.
The central leaderboard has been updated. Keep contributing!
|
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.
Team Number : Team 153
Description
Implements Role-Based Access Control (RBAC) for the backend. A
Roleenum (USER,ADMIN) is added to the Prisma schema andUsermodel. New middleware (requireRole,requireAdmin) enforces role checks on protected routes. A dedicated/api/adminroute group exposes admin-only operations (user management, challenge management, platform stats). All existing users default to theUSERrole — fully backward compatible.Related Issue
Closes #81
Type of Change
Changes Made
prisma/schema.prisma— AddedRoleenum (USER,ADMIN) androle Role @default(USER)field to theUsermodelprisma/migrations/20260228000000_add_role_rbac/migration.sql— Migration SQL: createsRoleenum and addsrolecolumn withDEFAULT 'USER'src/middlewares/auth.middleware.js— AddedrequireRole(...roles)middleware factory andrequireAdminshorthand;authenticatenow selects and attachesroletoreq.usersrc/controllers/admin.controller.js(new) — Admin-only handlers:getAllUsers,updateUserRole,deleteUser,getAllChallenges,deleteChallenge,getPlatformStatssrc/routes/admin.routes.js(new) — All routes under/api/adminprotected byauthenticate + requireAdminsrc/app.js— Registered admin routes at/api/adminsrc/services/auth.service.js—register,login,getProfileresponses now includeroleprisma/seed.js— Added a dedicatedADMINuser (admin/Admin@1234) created at seed timeScreenshots (if applicable)
N/A — backend API changes only.
API Endpoints Added
/api/admin/stats/api/admin/users/api/admin/users/:id/role/api/admin/users/:id/api/admin/challenges/api/admin/challenges/:idError Responses
401 Unauthorized— no token or invalid token403 Forbidden— authenticated but insufficient role ("Access denied: insufficient permissions")Testing
npm run build)Manual test scenarios:
USERtoken hitting/api/admin/*→ receives403ADMINtoken hitting/api/admin/*→ receives correct data401rolefield → default toUSER(backward compatible)Checklist
Additional Notes
enum(not hard-coded strings) for scalabilityrequireRoleis a generic factory — new roles can be added to the enum and used instantly without changing the middlewareadmin/Admin@1234— change before productionnpx prisma migrate deployafter configuringDATABASE_URLin.env