chace password by superadmin

This commit is contained in:
karenvicent committed 2025-10-02 15:19:42 -04:00
1 parent 468a67f937
commit 4317d1a47e
6 files changed
+54 -47

No files matched your search

@@ -1,20 +1,20 @@
import { useState } from "react";
import { Button, Stack } from "@mui/material";
import { GenericDialog } from "../../../../Components/Dialog/genericDialog";
import TextInput from "../../../../Components/Inputs/TextInput";
import PasswordTooltip from "../../../Auth/components/PasswordTooltip";
import { GenericDialog } from "@/Components/v1/Dialog/genericDialog";
import TextInput from "@/Components/v1/Inputs/TextInput";
import PasswordTooltip from "@/Pages/v1/Auth/components/PasswordTooltip";
import { useTheme } from "@emotion/react";
import { useTranslation } from "react-i18next";
import { createToast } from "../../../../Utils/toastUtils";
import { PasswordEndAdornment } from "../../../../Components/Inputs/TextInput/Adornments";
import usePasswordFeedback from "../../../Auth/hooks/usePasswordFeedback";
import { PasswordEndAdornment } from "@/Components/v1/Inputs/TextInput/Adornments";
import usePasswordFeedback from "@/Pages/v1/Auth/hooks/usePasswordFeedback";
import PropTypes from "prop-types";
const ChangePasswordModal = ({ isSaving, isLoading, userId, changePassword, email }) => {
const ChangePasswordModal = ({ isSaving, isLoading, changePassword, email }) => {
const INITIAL_FORM_STATE = {
password: "",
confirm: "",
};
console.log("email", email);
const theme = useTheme();
const { t } = useTranslation();
const { feedback, handlePasswordFeedback } = usePasswordFeedback();
@@ -30,7 +30,6 @@ const ChangePasswordModal = ({ isSaving, isLoading, userId, changePassword, emai
const onChange = (e) => {
let { name, value } = e.target;
const updatedForm = { ...form, [name]: value };
//validateFields(name, value, updatedForm);
setForm(updatedForm);
handlePasswordFeedback(updatedForm, name, value, form, errors, setErrors);
@@ -130,11 +129,19 @@ const ChangePasswordModal = ({ isSaving, isLoading, userId, changePassword, emai
onClick={onsubmitChangePassword}
disabled={isLoadingSubmit || !isFormValid}
>
Save
{t("save")}
</Button>
</Stack>
</GenericDialog>
</>
);
};
ChangePasswordModal.propTypes = {
isSaving: PropTypes.bool.isRequired,
isLoading: PropTypes.bool.isRequired,
changePassword: PropTypes.func.isRequired,
email: PropTypes.string.isRequired,
};
export default ChangePasswordModal;
+14 -11
View File
@@ -7,8 +7,7 @@ import TextInput from "@/Components/v1/Inputs/TextInput/index.jsx";
import Search from "@/Components/v1/Inputs/Search/index.jsx";
import Button from "@mui/material/Button";
import RoleTable from "../components/RoleTable/index.jsx";
import ChangePasswordModal from "../components/ChangePasswordModal";
import ChangePasswordModal from "@/Pages/Account/components/ChangePasswordModal/index.jsx";
// Utils
import { useParams } from "react-router-dom";
import { useTheme } from "@emotion/react";
@@ -16,9 +15,12 @@ import { useTranslation } from "react-i18next";
import { useGetUser, useEditUser } from "../../../../Hooks/v1/userHooks.js";
import { EDITABLE_ROLES, ROLES } from "../../../../Utils/roleUtils.js";
import { useEditUserForm, useValidateEditUserForm } from "./hooks/editUser.js";
import { useSelector } from "react-redux";
const EditUser = () => {
const { user } = useSelector((state) => state.auth);
const { userId } = useParams();
const isSameUser = user?._id === userId;
const theme = useTheme();
const { t } = useTranslation();
const BREADCRUMBS = [
@@ -26,7 +28,7 @@ const EditUser = () => {
{ name: t("editUserPage.title"), path: "" },
];
const [user, isLoading, error] = useGetUser(userId);
const [userToEdit, isLoading, error] = useGetUser(userId);
const [editUser, isSaving, saveError, changePassword] = useEditUser(userId);
const [
form,
@@ -35,7 +37,7 @@ const EditUser = () => {
handleDeleteRole,
searchInput,
handleSearchInput,
] = useEditUserForm(user);
] = useEditUserForm(userToEdit);
const [errors, validateForm, validateField] = useValidateEditUserForm();
const onChange = (e) => {
@@ -119,13 +121,14 @@ const EditUser = () => {
>
{t("editUserPage.form.save")}
</Button>
<ChangePasswordModal
isSaving={isSaving}
isLoading={isLoading}
userId={userId}
changePassword={changePassword}
email={user?.email}
/>
{!isSameUser && (
<ChangePasswordModal
isSaving={isSaving}
isLoading={isLoading}
changePassword={changePassword}
email={userToEdit?.email}
/>
)}
</Stack>
</Stack>
</Stack>
-1
View File
@@ -1139,7 +1139,6 @@ class NetworkService {
async changePasswordByAdmin(config) {
const { userId, passwordForm } = config;
console.log("Password form data:", passwordForm);
return this.axiosInstance.put(`auth/users/${userId}/password`, passwordForm, {
headers: {
"Content-Type": "application/json",
+21 -24
View File
@@ -10,10 +10,12 @@ import {
editUserByIdParamValidation,
editUserByIdBodyValidation,
editSuperadminUserByIdBodyValidation,
editUserPasswordByIdBodyValidation,
} from "../../validation/joi.js";
import ErrorService from "../../service/v1/infrastructure/errorService.js";
const SERVICE_NAME = "authController";
const errorService = new ErrorService();
/**
* Authentication Controller
*
@@ -436,7 +438,7 @@ class AuthController extends BaseController {
async (req, res) => {
const roles = req?.user?.role;
if (!roles.includes("superadmin")) {
throw createError("Unauthorized", 403);
throw errorService.createError("Unauthorized", 403);
}
const userId = req.params.userId;
@@ -456,29 +458,24 @@ class AuthController extends BaseController {
SERVICE_NAME,
"editUserById"
);
editUserPasswordById = this.asyncHandler(
async (req, res) => {
console.log("editUserPasswordById called");
const roles = req?.user?.role;
if (!roles.includes("superadmin")) {
console.log("entro a error editUserPasswordById");
throw createError("Unauthorized", 403);
}
const userId = req.params.userId;
console.log("userId param:", userId);
const updates = { ...req.body };
console.log("newPassword body:", req.body);
await editUserByIdParamValidation.validateAsync(req.params);
console.log("params validated");
//await editUserPasswordByIdBodyValidation.validateAsync(req.body);
console.log("body validated");
await this.userService.setPasswordByUserId( userId, updates );
return res.success({ msg: "Password reset successfully" });
},
SERVICE_NAME, "editUserPasswordById"
)
editUserPasswordById = this.asyncHandler(
async (req, res) => {
const roles = req?.user?.role;
if (!roles.includes("superadmin")) {
throw errorService.createError("Unauthorized", 403);
}
const userId = req.params.userId;
const updates = { ...req.body };
console.log("body", req.body);
await editUserByIdParamValidation.validateAsync(req.params);
await editUserPasswordByIdBodyValidation.validateAsync(req.body);
await this.userService.setPasswordByUserId(userId, updates);
return res.success({ msg: "Password reset successfully" });
},
SERVICE_NAME,
"editUserPasswordById"
);
}
export default AuthController;
@@ -214,7 +214,6 @@ class UserService {
setPasswordByUserId = async (userId, updates) => {
const updatedUser = await this.db.userModule.updateUser({ userId: userId, user: updates, file: null });
return updatedUser;
}
};
}
export default UserService;
+2
View File
@@ -671,7 +671,9 @@ const editSuperadminUserByIdBodyValidation = joi.object({
});
const editUserPasswordByIdBodyValidation = joi.object({
email: joi.string().required(),
newPassword: joi.string().min(8).required().pattern(passwordPattern),
password: joi.string().min(8).required().pattern(passwordPattern),
});
export {