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