Merge pull request #2999 from bluewave-labs/feat-allow-admin-change-passwords
Add superadmin password reset functionality for team members
This commit is contained in:
9 files changed
+238
-13
No files matched your search
@@ -10,10 +10,10 @@ import {
|
||||
editUserByIdParamValidation,
|
||||
editUserByIdBodyValidation,
|
||||
editSuperadminUserByIdBodyValidation,
|
||||
editUserPasswordByIdBodyValidation,
|
||||
} from "../../validation/joi.js";
|
||||
|
||||
const SERVICE_NAME = "authController";
|
||||
|
||||
/**
|
||||
* Authentication Controller
|
||||
*
|
||||
@@ -436,7 +436,7 @@ class AuthController extends BaseController {
|
||||
async (req, res) => {
|
||||
const roles = req?.user?.role;
|
||||
if (!roles.includes("superadmin")) {
|
||||
throw createError("Unauthorized", 403);
|
||||
throw this.errorService.createError("Unauthorized", 403);
|
||||
}
|
||||
|
||||
const userId = req.params.userId;
|
||||
@@ -456,6 +456,23 @@ class AuthController extends BaseController {
|
||||
SERVICE_NAME,
|
||||
"editUserById"
|
||||
);
|
||||
editUserPasswordById = this.asyncHandler(
|
||||
async (req, res) => {
|
||||
const roles = req?.user?.role;
|
||||
if (!roles.includes("superadmin")) {
|
||||
throw this.errorService.createError("Unauthorized", 403);
|
||||
}
|
||||
|
||||
const userId = req.params.userId;
|
||||
await editUserByIdParamValidation.validateAsync(req.params);
|
||||
await editUserPasswordByIdBodyValidation.validateAsync(req.body);
|
||||
const updatedPassword = req.body.password;
|
||||
await this.userService.setPasswordByUserId(userId, updatedPassword);
|
||||
return res.success({ msg: "Password reset successfully" });
|
||||
},
|
||||
SERVICE_NAME,
|
||||
"editUserPasswordById"
|
||||
);
|
||||
}
|
||||
|
||||
export default AuthController;
|
||||
@@ -25,6 +25,7 @@ class AuthRoutes {
|
||||
this.router.get("/users", verifyJWT, isAllowed(["admin", "superadmin"]), this.authController.getAllUsers);
|
||||
this.router.get("/users/:userId", verifyJWT, isAllowed(["superadmin"]), this.authController.getUserById);
|
||||
this.router.put("/users/:userId", verifyJWT, isAllowed(["superadmin"]), this.authController.editUserById);
|
||||
this.router.put("/users/:userId/password", verifyJWT, isAllowed(["superadmin"]), this.authController.editUserPasswordById);
|
||||
|
||||
this.router.put("/user", verifyJWT, upload.single("profileImage"), this.authController.editUser);
|
||||
this.router.delete("/user", verifyJWT, this.authController.deleteUser);
|
||||
|
||||
@@ -211,5 +211,9 @@ class UserService {
|
||||
editUserById = async (userId, user) => {
|
||||
await this.db.userModule.editUserById(userId, user);
|
||||
};
|
||||
setPasswordByUserId = async (userId, password) => {
|
||||
const updatedUser = await this.db.userModule.updateUser({ userId: userId, user: { password: password }, file: null });
|
||||
return updatedUser;
|
||||
};
|
||||
}
|
||||
export default UserService;
|
||||
@@ -670,6 +670,10 @@ const editSuperadminUserByIdBodyValidation = joi.object({
|
||||
.required(),
|
||||
});
|
||||
|
||||
const editUserPasswordByIdBodyValidation = joi.object({
|
||||
password: joi.string().min(8).required().pattern(passwordPattern),
|
||||
});
|
||||
|
||||
export {
|
||||
roleValidatior,
|
||||
loginValidation,
|
||||
@@ -739,4 +743,5 @@ export {
|
||||
editUserByIdParamValidation,
|
||||
editUserByIdBodyValidation,
|
||||
editSuperadminUserByIdBodyValidation,
|
||||
editUserPasswordByIdBodyValidation,
|
||||
};
|
||||
Reference in new issue
Block a user