umami/src/pages/api/users/[userId]/index.ts

108 lines
2.4 KiB
TypeScript
Raw Normal View History

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