Skip to content

Commit

Permalink
Merge pull request #80 from abinth11/bug-fixes-and-updates
Browse files Browse the repository at this point in the history
role check middleware updated
  • Loading branch information
abinth11 authored Sep 9, 2023
2 parents 23eec53 + 270efef commit bb1bddb
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 69 deletions.
Empty file.
59 changes: 11 additions & 48 deletions server/src/frameworks/webserver/middlewares/roleCheckMiddleware.ts
Original file line number Diff line number Diff line change
@@ -1,54 +1,17 @@
// Admin Role Middleware
import { CustomRequest } from '../../../types/customRequest';
import { NextFunction,Response } from 'express';
import { CustomRequest } from '../../../types/customRequest';
import AppError from '../../../utils/appError';
import HttpStatusCodes from '../../../constants/HttpStatusCodes';
export const adminRoleCheckMiddleware = (
req: CustomRequest,
res: Response,
next: NextFunction
) => {
const role = req.user?.role;
if (role === 'admin') {
// User has the admin role, allow access
next();
} else {
// User does not have the admin role, deny access
throw new AppError('Unauthorized role', HttpStatusCodes.UNAUTHORIZED);
}
};

// Instructor Role Middleware
export const instructorRoleCheckMiddleware = (
req: CustomRequest,
res: Response,
next: NextFunction
) => {
const role = req.user?.role


if (role === 'instructor') {
// User has the instructor role, allow access
next();
} else {
// User does not have the instructor role, deny access
throw new AppError('Unauthorized role, you are not a instructor', HttpStatusCodes.UNAUTHORIZED);
}
const roleCheckMiddleware = (roleToCheck: string) => {
return (req: CustomRequest, res: Response, next: NextFunction) => {
const role = req.user?.role;
if (role === roleToCheck) {
next();
} else {
throw new AppError('Unauthorized role', HttpStatusCodes.UNAUTHORIZED);
}
};
};

export const studentRoleCheckMiddleware = (
req: CustomRequest,
res: Response,
next: NextFunction
) => {
const role = req.user?.role;


if (role === 'student') {
// User has the instructor role, allow access
next();
} else {
// User does not have the instructor role, deny access
throw new AppError('Unauthorized role', HttpStatusCodes.UNAUTHORIZED);
}
};
export default roleCheckMiddleware;
17 changes: 7 additions & 10 deletions server/src/frameworks/webserver/routes/course.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ import express from 'express';
import courseController from '../../../adapters/controllers/courseController';
import { courseRepositoryMongodb } from '../../../frameworks/database/mongodb/repositories/courseReposMongoDb';
import { courseDbRepository } from '../../../app/repositories/courseDbRepository';
import {
instructorRoleCheckMiddleware,
studentRoleCheckMiddleware
} from '../middlewares/roleCheckMiddleware';
import roleCheckMiddleware from '../middlewares/roleCheckMiddleware';
import { cloudServiceInterface } from '../../../app/services/cloudServiceInterface';
import { s3Service } from '../../../frameworks/services/s3CloudService';
import upload from '../middlewares/multer';
Expand Down Expand Up @@ -46,15 +43,15 @@ const courseRouter = (redisClient: RedisClient) => {
router.post(
'/instructors/add-course',
jwtAuthMiddleware,
instructorRoleCheckMiddleware,
roleCheckMiddleware('instructor'),
upload.array('files'),
controller.addCourse
);

router.put(
'/instructors/edit-course/:courseId',
jwtAuthMiddleware,
instructorRoleCheckMiddleware,
roleCheckMiddleware('instructor'),
upload.array('files'),
controller.editCourse
);
Expand All @@ -70,22 +67,22 @@ const courseRouter = (redisClient: RedisClient) => {
router.get(
'/get-course-by-instructor',
jwtAuthMiddleware,
instructorRoleCheckMiddleware,
roleCheckMiddleware('instructor'),
controller.getCoursesByInstructor
);

router.post(
'/instructors/add-lesson/:courseId',
jwtAuthMiddleware,
instructorRoleCheckMiddleware,
roleCheckMiddleware('instructor'),
upload.array('media'),
controller.addLesson
);

router.put(
'/instructors/edit-lesson/:lessonId',
jwtAuthMiddleware,
instructorRoleCheckMiddleware,
roleCheckMiddleware('instructor'),
upload.array('media'),
controller.editLesson
);
Expand Down Expand Up @@ -142,7 +139,7 @@ const courseRouter = (redisClient: RedisClient) => {
router.get(
'/get-recommended-courses',
jwtAuthMiddleware,
studentRoleCheckMiddleware,
roleCheckMiddleware('student'),
controller.getRecommendedCourseByStudentInterest
);

Expand Down
4 changes: 2 additions & 2 deletions server/src/frameworks/webserver/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import courseRouter from './course';
import instructorRouter from './instructor';
import { RedisClient } from '../../../app';
import jwtAuthMiddleware from '../middlewares/userAuth';
import { adminRoleCheckMiddleware } from '../middlewares/roleCheckMiddleware';
import roleCheckMiddleware from '../middlewares/roleCheckMiddleware';
import videoStreamRouter from './videoStream';
import refreshRouter from './refresh';
import paymentRouter from './payment';
Expand All @@ -18,7 +18,7 @@ const routes = (app: Application, redisClient: RedisClient) => {
app.use(
'/api/admin',
jwtAuthMiddleware,
adminRoleCheckMiddleware,
roleCheckMiddleware('admin'),
adminRouter()
);
app.use('/api/category', categoryRouter());
Expand Down
8 changes: 4 additions & 4 deletions server/src/frameworks/webserver/routes/instructor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { authService } from '../../../frameworks/services/authService';
import { authServiceInterface } from '../../../app/services/authServicesInterface';
import { cloudServiceInterface } from '../../../app/services/cloudServiceInterface';
import { s3Service } from '../../../frameworks/services/s3CloudService';
import { instructorRoleCheckMiddleware } from '../middlewares/roleCheckMiddleware';
import roleCheckMiddleware from '../middlewares/roleCheckMiddleware';
import jwtAuthMiddleware from '../middlewares/userAuth';
import upload from '../middlewares/multer';
import { courseDbRepository } from '../../../app/repositories/courseDbRepository';
Expand Down Expand Up @@ -57,22 +57,22 @@ const instructorRouter = () => {
router.get(
'/get-instructor-details',
jwtAuthMiddleware,
instructorRoleCheckMiddleware,
roleCheckMiddleware('instructor'),
controller.getInstructorDetails
);

router.put(
'/update-profile',
jwtAuthMiddleware,
upload.single('image'),
instructorRoleCheckMiddleware,
roleCheckMiddleware('instructor'),
controller.updateProfile
);

router.patch(
'/change-password',
jwtAuthMiddleware,
instructorRoleCheckMiddleware,
roleCheckMiddleware('instructor'),
controller.changePassword
);

Expand Down
9 changes: 4 additions & 5 deletions server/src/frameworks/webserver/routes/student.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ import { cacheRepositoryInterface } from '../../../app/repositories/cachedRepoIn
import jwtAuthMiddleware from '../middlewares/userAuth';
import { contactDbInterface } from '../../../app/repositories/contactDbRepository';
import { contactRepositoryMongodb } from '../../../frameworks/database/mongodb/repositories/contactsRepoMongoDb';
import { adminRoleCheckMiddleware } from '../middlewares/roleCheckMiddleware';
import { adminRepoMongoDb } from '@src/frameworks/database/mongodb/repositories/adminRepoMongoDb';
import roleCheckMiddleware from '../middlewares/roleCheckMiddleware';

const studentRouter = (redisClient: RedisClient) => {
const router = express.Router();
Expand Down Expand Up @@ -57,21 +56,21 @@ const studentRouter = (redisClient: RedisClient) => {
router.patch(
'/block-student/:studentId',
jwtAuthMiddleware,
adminRoleCheckMiddleware,
roleCheckMiddleware('admin'),
controller.blockStudent
);

router.patch(
'/unblock-student/:studentId',
jwtAuthMiddleware,
adminRoleCheckMiddleware,
roleCheckMiddleware('admin'),
controller.unblockStudent
);

router.get(
'/get-all-blocked-students',
jwtAuthMiddleware,
adminRoleCheckMiddleware,
roleCheckMiddleware('admin'),
controller.getAllBlockedStudents
);

Expand Down

0 comments on commit bb1bddb

Please sign in to comment.