apply validation
This commit is contained in:
1 parent
73f2f29c2a
commit
22826afd21
2 files changed
+53
-10
No files matched your search
@@ -5,6 +5,10 @@ import {
|
||||
recoveryValidation,
|
||||
recoveryTokenValidation,
|
||||
newPasswordValidation,
|
||||
getUserByIdParamValidation,
|
||||
editUserByIdParamValidation,
|
||||
editUserByIdBodyValidation,
|
||||
editSuperadminUserByIdBodyValidation,
|
||||
} from "../validation/joi.js";
|
||||
import jwt from "jsonwebtoken";
|
||||
import { getTokenFromHeaders } from "../utils/utils.js";
|
||||
@@ -396,6 +400,7 @@ class AuthController {
|
||||
|
||||
getUserById = asyncHandler(
|
||||
async (req, res, next) => {
|
||||
await getUserByIdParamValidation.validateAsync(req.params);
|
||||
const userId = req?.params?.userId;
|
||||
const roles = req?.user?.role;
|
||||
|
||||
@@ -417,14 +422,22 @@ class AuthController {
|
||||
|
||||
editUserById = asyncHandler(
|
||||
async (req, res, next) => {
|
||||
const userId = req.params.userId;
|
||||
const user = req.body;
|
||||
|
||||
const roles = req?.user?.role;
|
||||
if (!roles.includes("superadmin")) {
|
||||
throw createError("Unauthorized", 403);
|
||||
}
|
||||
|
||||
const userId = req.params.userId;
|
||||
const user = { ...req.body };
|
||||
|
||||
await editUserByIdParamValidation.validateAsync(req.params);
|
||||
// If this is superadmin self edit, allow "superadmin" role
|
||||
if (userId === req.user._id) {
|
||||
await editSuperadminUserByIdBodyValidation.validateAsync(req.body);
|
||||
} else {
|
||||
await editUserByIdBodyValidation.validateAsync(req.body);
|
||||
}
|
||||
|
||||
await this.db.editUserById(userId, user);
|
||||
return res.success({ msg: "ok" });
|
||||
},
|
||||
|
||||
Reference in new issue
Block a user