From 7e41ebe018c01808f5fd98bf4eb5f8f5454abcae Mon Sep 17 00:00:00 2001 From: Surat Das Date: Thu, 7 Oct 2021 18:33:22 -0700 Subject: [PATCH 1/3] Prevent self deletion in users page --- src/components/UserList/ActionButtons.tsx | 22 ++++++++++++++-------- src/services/users.service.ts | 9 +++++++++ 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/components/UserList/ActionButtons.tsx b/src/components/UserList/ActionButtons.tsx index 40db0b7d..edd9e31b 100644 --- a/src/components/UserList/ActionButtons.tsx +++ b/src/components/UserList/ActionButtons.tsx @@ -25,14 +25,20 @@ export const ActionButtons: React.FunctionComponent = () => { onClick={() => { usersService .remove(ids) - .then(() => { - enqueueSnackbar(`Removed`, { - variant: "success", - }); - userDispatch({ - type: "getAll", - payload: userList.filter((user) => !ids.includes(user.id)), - }); + .then((value) => { + if (value.toString().trim().length === 0) { + enqueueSnackbar(`Removed`, { + variant: "success", + }); + userDispatch({ + type: "getAll", + payload: userList.filter((user) => !ids.includes(user.id)), + }); + } else { + enqueueSnackbar(`You cannot delete yourself.`, { + variant: "error", + }); + } }) .catch((err) => enqueueSnackbar(err, { diff --git a/src/services/users.service.ts b/src/services/users.service.ts index 18cc0bf8..9b7b34e9 100644 --- a/src/services/users.service.ts +++ b/src/services/users.service.ts @@ -106,6 +106,15 @@ async function assignRole(id: string | number, role: Role): Promise { } async function remove(ids: (string | number)[]): Promise { + + const userString = localStorage.getItem("user"); + if (userString) { + const user: User = JSON.parse(userString); + if (ids.includes(user.id)) { + return false; + } + } + const requestOptions = { method: "DELETE", headers: { "Content-Type": "application/json", ...authHeader() }, From ebbe0d3d4d8506efcd285b79369936a83a04eb79 Mon Sep 17 00:00:00 2001 From: Surat Das Date: Fri, 8 Oct 2021 11:51:23 -0700 Subject: [PATCH 2/3] Fixed review comments --- src/components/UserList/ActionButtons.tsx | 31 ++++++++++++----------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/src/components/UserList/ActionButtons.tsx b/src/components/UserList/ActionButtons.tsx index edd9e31b..1b771455 100644 --- a/src/components/UserList/ActionButtons.tsx +++ b/src/components/UserList/ActionButtons.tsx @@ -10,7 +10,7 @@ export const ActionButtons: React.FunctionComponent = () => { const props = useGridSlotComponentProps(); const { enqueueSnackbar } = useSnackbar(); const userDispatch = useUserDispatch(); - const { userList } = useUserState(); + const { userList, user } = useUserState(); const ids: GridRowId[] = React.useMemo( () => Object.values(props.state.selection), @@ -23,10 +23,15 @@ export const ActionButtons: React.FunctionComponent = () => { { - usersService - .remove(ids) - .then((value) => { - if (value.toString().trim().length === 0) { + const currentUserId = user?.id; + if (currentUserId && ids.includes(currentUserId)) { + enqueueSnackbar(`You cannot delete yourself.`, { + variant: "error", + }); + } else { + usersService + .remove(ids) + .then(() => { enqueueSnackbar(`Removed`, { variant: "success", }); @@ -34,17 +39,13 @@ export const ActionButtons: React.FunctionComponent = () => { type: "getAll", payload: userList.filter((user) => !ids.includes(user.id)), }); - } else { - enqueueSnackbar(`You cannot delete yourself.`, { - variant: "error", - }); - } - }) - .catch((err) => - enqueueSnackbar(err, { - variant: "error", }) - ); + .catch((err) => + enqueueSnackbar(err, { + variant: "error", + }) + ); + } }} > From e6f1ba7c5dbd356ae8fa3164ead4c257dcdc88e6 Mon Sep 17 00:00:00 2001 From: Surat Das Date: Fri, 8 Oct 2021 11:52:01 -0700 Subject: [PATCH 3/3] Fixed review comments --- src/services/users.service.ts | 9 --------- 1 file changed, 9 deletions(-) diff --git a/src/services/users.service.ts b/src/services/users.service.ts index 9b7b34e9..18cc0bf8 100644 --- a/src/services/users.service.ts +++ b/src/services/users.service.ts @@ -106,15 +106,6 @@ async function assignRole(id: string | number, role: Role): Promise { } async function remove(ids: (string | number)[]): Promise { - - const userString = localStorage.getItem("user"); - if (userString) { - const user: User = JSON.parse(userString); - if (ids.includes(user.id)) { - return false; - } - } - const requestOptions = { method: "DELETE", headers: { "Content-Type": "application/json", ...authHeader() },