2023-04-13 21:08:53 +02:00
|
|
|
import { NextApiRequestQueryBody, Roles, User } from 'lib/types';
|
2022-12-28 00:18:58 +01:00
|
|
|
import { canDeleteUser, canUpdateUser, canViewUser } from 'lib/auth';
|
2020-08-09 11:03:37 +02:00
|
|
|
import { useAuth } from 'lib/middleware';
|
2022-11-15 22:21:14 +01:00
|
|
|
import { NextApiResponse } from 'next';
|
2022-11-20 09:48:13 +01:00
|
|
|
import { badRequest, hashPassword, methodNotAllowed, ok, unauthorized } from 'next-basics';
|
2023-03-03 07:48:30 +01:00
|
|
|
import { deleteUser, getUser, updateUser } from 'queries';
|
2022-11-15 22:21:14 +01:00
|
|
|
|
2022-11-18 07:46:05 +01:00
|
|
|
export interface UserRequestQuery {
|
2022-11-15 22:21:14 +01:00
|
|
|
id: string;
|
|
|
|
}
|
|
|
|
|
2022-11-18 07:46:05 +01:00
|
|
|
export interface UserRequestBody {
|
2022-11-15 22:21:14 +01:00
|
|
|
username: string;
|
|
|
|
password: string;
|
2023-04-13 21:08:53 +02:00
|
|
|
role: Roles;
|
2022-11-15 22:21:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export default async (
|
2022-11-18 07:46:05 +01:00
|
|
|
req: NextApiRequestQueryBody<UserRequestQuery, UserRequestBody>,
|
2022-11-15 22:21:14 +01:00
|
|
|
res: NextApiResponse<User>,
|
|
|
|
) => {
|
2020-08-09 11:03:37 +02:00
|
|
|
await useAuth(req, res);
|
|
|
|
|
2022-11-09 21:03:24 +01:00
|
|
|
const {
|
2022-12-07 03:36:41 +01:00
|
|
|
user: { id: userId, isAdmin },
|
2022-11-09 21:03:24 +01:00
|
|
|
} = req.auth;
|
2020-08-09 11:03:37 +02:00
|
|
|
const { id } = req.query;
|
|
|
|
|
2020-09-11 22:49:43 +02:00
|
|
|
if (req.method === 'GET') {
|
2022-12-28 00:18:58 +01:00
|
|
|
if (!(await canViewUser(req.auth, id))) {
|
2022-09-30 00:15:11 +02:00
|
|
|
return unauthorized(res);
|
|
|
|
}
|
|
|
|
|
2022-11-01 07:42:37 +01:00
|
|
|
const user = await getUser({ id });
|
2020-08-09 11:03:37 +02:00
|
|
|
|
2022-11-01 07:42:37 +01:00
|
|
|
return ok(res, user);
|
2020-08-09 11:03:37 +02:00
|
|
|
}
|
|
|
|
|
2022-09-30 00:15:11 +02:00
|
|
|
if (req.method === 'POST') {
|
2022-12-28 00:18:58 +01:00
|
|
|
if (!(await canUpdateUser(req.auth, id))) {
|
2022-09-30 00:15:11 +02:00
|
|
|
return unauthorized(res);
|
|
|
|
}
|
|
|
|
|
2023-04-13 21:08:53 +02:00
|
|
|
const { username, password, role } = req.body;
|
2022-12-02 05:53:37 +01:00
|
|
|
|
2022-11-01 07:42:37 +01:00
|
|
|
const user = await getUser({ id });
|
2022-09-30 00:15:11 +02:00
|
|
|
|
2022-11-15 22:21:14 +01:00
|
|
|
const data: any = {};
|
2022-09-30 00:15:11 +02:00
|
|
|
|
2023-04-13 21:08:53 +02:00
|
|
|
if (password) {
|
2022-09-30 00:15:11 +02:00
|
|
|
data.password = hashPassword(password);
|
|
|
|
}
|
|
|
|
|
2023-05-06 06:16:13 +02:00
|
|
|
// Only admin can change these fields
|
2023-04-13 21:08:53 +02:00
|
|
|
if (role && isAdmin) {
|
|
|
|
data.role = role;
|
|
|
|
}
|
|
|
|
|
2022-12-07 03:36:41 +01:00
|
|
|
if (username && isAdmin) {
|
2022-09-30 00:15:11 +02:00
|
|
|
data.username = username;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check when username changes
|
2022-11-01 07:42:37 +01:00
|
|
|
if (data.username && user.username !== data.username) {
|
|
|
|
const userByUsername = await getUser({ username });
|
2022-09-30 00:15:11 +02:00
|
|
|
|
2022-11-01 07:42:37 +01:00
|
|
|
if (userByUsername) {
|
|
|
|
return badRequest(res, 'User already exists');
|
2022-09-30 00:15:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-01 07:42:37 +01:00
|
|
|
const updated = await updateUser(data, { id });
|
2022-09-30 00:15:11 +02:00
|
|
|
|
|
|
|
return ok(res, updated);
|
|
|
|
}
|
|
|
|
|
2020-08-09 11:03:37 +02:00
|
|
|
if (req.method === 'DELETE') {
|
2022-12-28 00:18:58 +01:00
|
|
|
if (!(await canDeleteUser(req.auth))) {
|
2022-12-02 05:53:37 +01:00
|
|
|
return unauthorized(res);
|
2022-11-01 02:50:05 +01:00
|
|
|
}
|
|
|
|
|
2022-12-02 05:53:37 +01:00
|
|
|
if (id === userId) {
|
2023-01-25 16:42:46 +01:00
|
|
|
return badRequest(res, 'You cannot delete yourself.');
|
2022-09-30 00:15:11 +02:00
|
|
|
}
|
|
|
|
|
2022-11-01 07:42:37 +01:00
|
|
|
await deleteUser(id);
|
2020-08-09 11:03:37 +02:00
|
|
|
|
2020-09-11 22:49:43 +02:00
|
|
|
return ok(res);
|
2020-08-09 11:03:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return methodNotAllowed(res);
|
|
|
|
};
|