fix: make isAdmin require req.prisma too
This commit is contained in:
1 parent
cc534a2914
commit
60688e2e91
8 files changed
+10
-12
No files matched your search
@@ -33,7 +33,7 @@ import { schemaQueryUserId } from "@lib/validations/shared/queryUserId";
|
||||
*/
|
||||
export async function deleteHandler(req: NextApiRequest) {
|
||||
const query = schemaQueryUserId.parse(req.query);
|
||||
const isAdmin = await isAdminGuard(req.userId);
|
||||
const isAdmin = await isAdminGuard(req.userId, req.prisma);
|
||||
// Here we only check for ownership of the user if the user is not admin, otherwise we let ADMIN's edit any user
|
||||
if (!isAdmin && query.userId !== req.userId)
|
||||
throw new HttpError({ statusCode: 401, message: "Unauthorized" });
|
||||
|
||||
@@ -34,7 +34,7 @@ import { schemaUserReadPublic } from "@lib/validations/user";
|
||||
*/
|
||||
export async function getHandler(req: NextApiRequest) {
|
||||
const query = schemaQueryUserId.parse(req.query);
|
||||
const isAdmin = await isAdminGuard(req.userId);
|
||||
const isAdmin = await isAdminGuard(req.userId, req.prisma);
|
||||
// Here we only check for ownership of the user if the user is not admin, otherwise we let ADMIN's edit any user
|
||||
if (!isAdmin && query.userId !== req.userId)
|
||||
throw new HttpError({ statusCode: 401, message: "Unauthorized" });
|
||||
|
||||
@@ -55,7 +55,7 @@ import { schemaUserEditBodyParams, schemaUserReadPublic } from "@lib/validations
|
||||
*/
|
||||
export async function patchHandler(req: NextApiRequest) {
|
||||
const query = schemaQueryUserId.parse(req.query);
|
||||
const isAdmin = await isAdminGuard(req.userId);
|
||||
const isAdmin = await isAdminGuard(req.userId, req.prisma);
|
||||
// Here we only check for ownership of the user if the user is not admin, otherwise we let ADMIN's edit any user
|
||||
if (!isAdmin && query.userId !== req.userId)
|
||||
throw new HttpError({ statusCode: 401, message: "Unauthorized" });
|
||||
|
||||
@@ -24,8 +24,8 @@ import { Prisma } from ".prisma/client";
|
||||
* 404:
|
||||
* description: No users were found
|
||||
*/
|
||||
async function getHandler({ userId }: NextApiRequest) {
|
||||
const isAdmin = await isAdminGuard(userId);
|
||||
async function getHandler({ userId, prisma }: NextApiRequest) {
|
||||
const isAdmin = await isAdminGuard(userId, prisma);
|
||||
const where: Prisma.UserWhereInput = {};
|
||||
// If user is not ADMIN, return only his data.
|
||||
if (!isAdmin) where.id = userId;
|
||||
|
||||
@@ -8,7 +8,7 @@ import { isAdminGuard } from "@lib/utils/isAdmin";
|
||||
import { schemaUserCreateBodyParams } from "@lib/validations/user";
|
||||
|
||||
async function postHandler(req: NextApiRequest) {
|
||||
const isAdmin = await isAdminGuard(req.userId);
|
||||
const isAdmin = await isAdminGuard(req.userId, req.prisma);
|
||||
// If user is not ADMIN, return unauthorized.
|
||||
if (!isAdmin) throw new HttpError({ statusCode: 401, message: "You are not authorized" });
|
||||
const data = schemaUserCreateBodyParams.parse(req.body);
|
||||
|
||||
Reference in new issue
Block a user