Skip to content

Commit 70e7e9a

Browse files
committed
filter locked team members. prevent locking self
1 parent 788e4cc commit 70e7e9a

File tree

5 files changed

+16
-5
lines changed

5 files changed

+16
-5
lines changed

src/api/routers/managementRouter.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import express, { Response } from 'express';
22
import { z } from 'zod';
33

44
import { isSuperUserCheck } from '../middleware/userRoleMiddleware';
5-
import { getAllUsersList, updateUserLock } from '../services/managementService';
5+
import { getAllUsersList, getUserById, updateUserLock } from '../services/managementService';
66
import { ParticipantRequest } from '../services/participantsService';
77

88
const handleGetAllUsers = async (req: ParticipantRequest, res: Response) => {
@@ -13,6 +13,11 @@ const handleGetAllUsers = async (req: ParticipantRequest, res: Response) => {
1313
const handleChangeUserLock = async (req: ParticipantRequest, res: Response) => {
1414
const { userId } = z.object({ userId: z.coerce.number() }).parse(req.params);
1515
const { isLocked } = z.object({ isLocked: z.boolean() }).parse(req.body);
16+
const user = await getUserById(userId);
17+
if (req.auth?.payload?.email === user?.email) {
18+
res.status(403).send([{ message: 'You cannot lock yourself.' }]);
19+
return;
20+
}
1621
await updateUserLock(req, userId, isLocked);
1722
return res.status(200).end();
1823
};

src/api/services/managementService.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ export const getAllUsersList = async () => {
1313
return userList;
1414
};
1515

16+
export const getUserById = async (userId: number) => {
17+
const user = await User.query().where('id', userId).first();
18+
return user;
19+
};
20+
1621
export const updateUserLock = async (
1722
req: UserParticipantRequest,
1823
userId: number,

src/api/services/usersService.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ export const findUserByEmail = async (email: string) => {
7474
const user = await User.query()
7575
.findOne('email', email)
7676
.where('deleted', 0)
77+
.where('locked', 0)
7778
.modify('withParticipants');
7879

7980
if (user?.participants) {
@@ -107,6 +108,7 @@ export const getAllUsersFromParticipantWithRoles = async (participant: Participa
107108
const usersWithParticipants = await User.query()
108109
.whereIn('id', participantUserIds)
109110
.where('deleted', 0)
111+
.where('locked', 0)
110112
.withGraphFetched('userToParticipantRoles');
111113

112114
return mapUsersWithParticipantRoles(usersWithParticipants, participant.id);
@@ -117,7 +119,7 @@ export const getAllUsersFromParticipant = async (participant: Participant) => {
117119
await UserToParticipantRole.query().where('participantId', participant.id)
118120
).map((userToParticipantRole) => userToParticipantRole.userId);
119121

120-
return User.query().whereIn('id', participantUserIds).where('deleted', 0);
122+
return User.query().whereIn('id', participantUserIds).where('deleted', 0).where('locked', 0);
121123
};
122124

123125
export const sendInviteEmailToExistingUser = (

src/web/components/UserManagement/UserManagementItem.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export function UserManagementItem({ user, onChangeUserLock }: UserManagementIte
2424
<td>
2525
<div className='theme-switch action-cell' title='Disable User Access'>
2626
<Switch.Root
27-
name='dark-mode'
27+
name='user-locked'
2828
checked={user.locked}
2929
onCheckedChange={onLockedToggle}
3030
className='theme-toggle clickable-item'

src/web/services/userAccount.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,7 @@ export async function GetAllUsers() {
123123

124124
export async function ChangeUserLock(userId: number, isLocked: boolean) {
125125
try {
126-
const res = await axios.patch(`/manage/${userId}/changeLock`, { userId, isLocked });
127-
return res;
126+
return await axios.patch(`/manage/${userId}/changeLock`, { userId, isLocked });
128127
} catch (e: unknown) {
129128
throw backendError(e, 'Unable to update user lock status.');
130129
}

0 commit comments

Comments
 (0)