Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: implement new role for admin #122

Merged
merged 5 commits into from
Sep 15, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/middleware/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,8 @@ export const adminProtect = asyncHandler((req) => {
if (req.headers['x-api-key'] === process.env.API_ACCESS_KEY) return;
if (req.user.role !== 'ADMIN') throw new createError(403, 'You are not permitted to access this resource');
});

export const externalPartyProtect = asyncHandler((req) => {
if (req.headers['x-api-key'] === process.env.API_ACCESS_KEY) return;
if (req.user.role !== 'SPECTATOR') throw new createError(403, 'You are not permitted to access this resource');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if this is used along with adminProtect neither of it will allow the call to go through, since each checks for a different role, can we refactor this into a function like roleProtect which takes in a list of accepted roles and checks accordingly ?

});
2 changes: 1 addition & 1 deletion src/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const UserSchema = new mongoose.Schema(
},
role: {
type: String,
enum: ['ADMIN', 'GROUP'],
enum: ['ADMIN', 'GROUP', 'SPECTATOR'],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we move this into an enum like object and use it here with Object.Values()

default: 'GROUP'
},
members: {
Expand Down
4 changes: 2 additions & 2 deletions src/routes/index.routes.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import express from 'express';
import { adminProtect, protect } from '@/middleware/auth';
import { adminProtect, externalPartyProtect, protect } from '@/middleware/auth';
import authRouter from './auth.routes';
import dashboardRouter from './dashboard.routes';
import leaderboardRouter from './leaderboard.routes';
Expand All @@ -15,7 +15,7 @@ router.use('/auth', authRouter);
router.use('/submissions', protect, submissionRouter);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Submission grade will have to be restricted as well for role SPECTATOR

router.use('/users', protect, userRouter);
router.use('/questions', protect, questionRouter);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question create update will have to be restricted as well for role SPECTATOR

router.use('/dashboard', protect, adminProtect, dashboardRouter);
router.use('/dashboard', protect, externalPartyProtect, adminProtect, dashboardRouter);
router.use('/leaderboard', leaderboardRouter);
router.use('/settings', protect, settingRouter);
router.use('/storage', protect, adminProtect, storageRouter);
Expand Down
4 changes: 2 additions & 2 deletions src/routes/user.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import express from 'express';
import { tracedAsyncHandler } from '@sliit-foss/functions';
import { Segments, celebrate } from 'celebrate';
import { changePassword, create, eliminateTeams, getAll, getById, update } from '@/controllers/user';
import { adminProtect } from '@/middleware/auth';
import { adminProtect, externalPartyProtect } from '@/middleware/auth';
import {
addUserSchema,
changePasswordSchema,
Expand All @@ -14,7 +14,7 @@ import {
const users = express.Router();

users.post('/', adminProtect, celebrate({ [Segments.BODY]: addUserSchema }), tracedAsyncHandler(create));
users.get('/', adminProtect, tracedAsyncHandler(getAll));
users.get('/', externalPartyProtect, adminProtect, tracedAsyncHandler(getAll));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Somewhere below there's a user update route as well I guess that needs restriction as well

users.get('/:id', celebrate({ [Segments.PARAMS]: userIdSchema }), adminProtect, tracedAsyncHandler(getById));
users.patch(
'/change_password',
Expand Down
3 changes: 1 addition & 2 deletions src/services/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ export const addNewUser = async (payload) => {
const newUser = await createUser({
...payload,
password: encryptedPassword,
is_verified: true,
role: 'ADMIN'
is_verified: true
});
try {
await sendAdminPassword(payload.email, generatedPassword);
Expand Down
3 changes: 2 additions & 1 deletion src/validations/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { genders, mealPreferences } from '@/models/user';

export const addUserSchema = {
name: Joi.string().required(),
email: Joi.string().email().required()
email: Joi.string().email().required(),
role: Joi.string().valid('ADMIN', 'SPECTATOR').required()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like in the model, can we move this into an enum like object and use it here with Object.Values()

};

export const userIdSchema = {
Expand Down
Loading