1 parent
2260520ae5
commit
2ab58700b2
3 files changed
+61
-39
No files matched your search
@@ -1,12 +1,20 @@
|
|||||||
import { z } from 'zod';
|
import { z } from "zod";
|
||||||
import { hashPassword } from '@/lib/password';
|
import { hashPassword } from "@/lib/password";
|
||||||
import { parseRequest } from '@/lib/request';
|
import { parseRequest } from "@/lib/request";
|
||||||
import { badRequest, json, ok, unauthorized } from '@/lib/response';
|
import { badRequest, json, ok, unauthorized } from "@/lib/response";
|
||||||
import { userRoleParam } from '@/lib/schema';
|
import { userRoleParam } from "@/lib/schema";
|
||||||
import { canDeleteUser, canUpdateUser, canViewUser } from '@/permissions';
|
import { canDeleteUser, canUpdateUser, canViewUser } from "@/permissions";
|
||||||
import { deleteUser, getUser, getUserByUsername, updateUser } from '@/queries/prisma';
|
import {
|
||||||
|
deleteUser,
|
||||||
|
getUser,
|
||||||
|
getUserByUsername,
|
||||||
|
updateUser,
|
||||||
|
} from "@/queries/prisma";
|
||||||
|
|
||||||
export async function GET(request: Request, { params }: { params: Promise<{ userId: string }> }) {
|
export async function GET(
|
||||||
|
request: Request,
|
||||||
|
{ params }: { params: Promise<{ userId: string }> },
|
||||||
|
) {
|
||||||
const { auth, error } = await parseRequest(request);
|
const { auth, error } = await parseRequest(request);
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
@@ -24,7 +32,10 @@ export async function GET(request: Request, { params }: { params: Promise<{ user
|
|||||||
return json(user);
|
return json(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function POST(request: Request, { params }: { params: Promise<{ userId: string }> }) {
|
export async function POST(
|
||||||
|
request: Request,
|
||||||
|
{ params }: { params: Promise<{ userId: string }> },
|
||||||
|
) {
|
||||||
const schema = z.object({
|
const schema = z.object({
|
||||||
username: z.string().max(255).optional(),
|
username: z.string().max(255).optional(),
|
||||||
password: z.string().max(255).optional(),
|
password: z.string().max(255).optional(),
|
||||||
@@ -59,15 +70,15 @@ export async function POST(request: Request, { params }: { params: Promise<{ use
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (username && auth.user.isAdmin) {
|
if (username && auth.user.isAdmin) {
|
||||||
data.username = username;
|
data.username = username.toLowerCase();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check when username changes
|
// Check when username changes
|
||||||
if (data.username && user.username !== data.username) {
|
if (data.username && user.username !== data.username) {
|
||||||
const user = await getUserByUsername(username);
|
const existingUser = await getUserByUsername(username);
|
||||||
|
|
||||||
if (user) {
|
if (existingUser && existingUser.id !== userId) {
|
||||||
return badRequest({ message: 'User already exists' });
|
return badRequest({ message: "User already exists" });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -93,7 +104,7 @@ export async function DELETE(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (userId === auth.user.id) {
|
if (userId === auth.user.id) {
|
||||||
return badRequest({ message: 'You cannot delete yourself.' });
|
return badRequest({ message: "You cannot delete yourself." });
|
||||||
}
|
}
|
||||||
|
|
||||||
await deleteUser(userId);
|
await deleteUser(userId);
|
||||||
|
|||||||
+13
-11
@@ -1,11 +1,11 @@
|
|||||||
import { z } from 'zod';
|
import { z } from "zod";
|
||||||
import { ROLES } from '@/lib/constants';
|
import { ROLES } from "@/lib/constants";
|
||||||
import { uuid } from '@/lib/crypto';
|
import { uuid } from "@/lib/crypto";
|
||||||
import { hashPassword } from '@/lib/password';
|
import { hashPassword } from "@/lib/password";
|
||||||
import { parseRequest } from '@/lib/request';
|
import { parseRequest } from "@/lib/request";
|
||||||
import { badRequest, json, unauthorized } from '@/lib/response';
|
import { badRequest, json, unauthorized } from "@/lib/response";
|
||||||
import { canCreateUser } from '@/permissions';
|
import { canCreateUser } from "@/permissions";
|
||||||
import { createUser, getUserByUsername } from '@/queries/prisma';
|
import { createUser, getUserByUsername } from "@/queries/prisma";
|
||||||
|
|
||||||
export async function POST(request: Request) {
|
export async function POST(request: Request) {
|
||||||
const schema = z.object({
|
const schema = z.object({
|
||||||
@@ -27,15 +27,17 @@ export async function POST(request: Request) {
|
|||||||
|
|
||||||
const { id, username, password, role } = body;
|
const { id, username, password, role } = body;
|
||||||
|
|
||||||
const existingUser = await getUserByUsername(username, { showDeleted: true });
|
const existingUser = await getUserByUsername(username, {
|
||||||
|
showDeleted: true,
|
||||||
|
});
|
||||||
|
|
||||||
if (existingUser) {
|
if (existingUser) {
|
||||||
return badRequest({ message: 'User already exists' });
|
return badRequest({ message: "User already exists" });
|
||||||
}
|
}
|
||||||
|
|
||||||
const user = await createUser({
|
const user = await createUser({
|
||||||
id: id || uuid(),
|
id: id || uuid(),
|
||||||
username,
|
username: username.toLowerCase(),
|
||||||
password: hashPassword(password),
|
password: hashPassword(password),
|
||||||
role: role ?? ROLES.user,
|
role: role ?? ROLES.user,
|
||||||
});
|
});
|
||||||
|
|||||||
+23
-14
@@ -1,8 +1,8 @@
|
|||||||
import { Prisma } from '@/generated/prisma/client';
|
import { Prisma } from "@/generated/prisma/client";
|
||||||
import { ROLES } from '@/lib/constants';
|
import { ROLES } from "@/lib/constants";
|
||||||
import { getRandomChars } from '@/lib/generate';
|
import { getRandomChars } from "@/lib/generate";
|
||||||
import prisma from '@/lib/prisma';
|
import prisma from "@/lib/prisma";
|
||||||
import type { QueryFilters, Role } from '@/lib/types';
|
import type { QueryFilters, Role } from "@/lib/types";
|
||||||
|
|
||||||
import UserFindManyArgs = Prisma.UserFindManyArgs;
|
import UserFindManyArgs = Prisma.UserFindManyArgs;
|
||||||
|
|
||||||
@@ -11,7 +11,10 @@ export interface GetUserOptions {
|
|||||||
showDeleted?: boolean;
|
showDeleted?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function findUser(criteria: Prisma.UserFindUniqueArgs, options: GetUserOptions = {}) {
|
async function findUser(
|
||||||
|
criteria: Prisma.UserFindUniqueArgs,
|
||||||
|
options: GetUserOptions = {},
|
||||||
|
) {
|
||||||
const { includePassword = false, showDeleted = false } = options;
|
const { includePassword = false, showDeleted = false } = options;
|
||||||
|
|
||||||
return prisma.client.user.findUnique({
|
return prisma.client.user.findUnique({
|
||||||
@@ -41,27 +44,33 @@ export async function getUser(userId: string, options: GetUserOptions = {}) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getUserByUsername(username: string, options: GetUserOptions = {}) {
|
export async function getUserByUsername(
|
||||||
return findUser({ where: { username } }, options);
|
username: string,
|
||||||
|
options: GetUserOptions = {},
|
||||||
|
) {
|
||||||
|
return findUser({ where: { username: username.toLowerCase() } }, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getUsers(criteria: UserFindManyArgs, filters: QueryFilters = {}) {
|
export async function getUsers(
|
||||||
|
criteria: UserFindManyArgs,
|
||||||
|
filters: QueryFilters = {},
|
||||||
|
) {
|
||||||
const { search } = filters;
|
const { search } = filters;
|
||||||
|
|
||||||
const where: Prisma.UserWhereInput = {
|
const where: Prisma.UserWhereInput = {
|
||||||
...criteria.where,
|
...criteria.where,
|
||||||
...prisma.getSearchParameters(search, [{ username: 'contains' }]),
|
...prisma.getSearchParameters(search, [{ username: "contains" }]),
|
||||||
deletedAt: null,
|
deletedAt: null,
|
||||||
};
|
};
|
||||||
|
|
||||||
return prisma.pagedQuery(
|
return prisma.pagedQuery(
|
||||||
'user',
|
"user",
|
||||||
{
|
{
|
||||||
...criteria,
|
...criteria,
|
||||||
where,
|
where,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
orderBy: 'createdAt',
|
orderBy: "createdAt",
|
||||||
sortDescending: true,
|
sortDescending: true,
|
||||||
...filters,
|
...filters,
|
||||||
},
|
},
|
||||||
@@ -110,7 +119,7 @@ export async function deleteUser(userId: string) {
|
|||||||
let websiteIds = [];
|
let websiteIds = [];
|
||||||
|
|
||||||
if (websites.length > 0) {
|
if (websites.length > 0) {
|
||||||
websiteIds = websites.map(a => a.id);
|
websiteIds = websites.map((a) => a.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
const teams = await client.team.findMany({
|
const teams = await client.team.findMany({
|
||||||
@@ -124,7 +133,7 @@ export async function deleteUser(userId: string) {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const teamIds = teams.map(a => a.id);
|
const teamIds = teams.map((a) => a.id);
|
||||||
|
|
||||||
if (cloudMode) {
|
if (cloudMode) {
|
||||||
return transaction([
|
return transaction([
|
||||||
|
|||||||
Reference in new issue
Block a user