fix: make isAdmin require req.prisma too

This commit is contained in:
Agusti Fernandez Pardo
2022-06-18 03:03:13 +02:00
parent cc534a2914
commit 60688e2e91
8 changed files with 10 additions and 12 deletions
+1 -1
View File
@@ -34,7 +34,7 @@ export const verifyApiKey: NextMiddleware = async (req, res, next) => {
/* We save the user id in the request for later use */
req.userId = apiKey.userId;
/* We save the isAdmin boolean here for later use */
req.isAdmin = await isAdminGuard(req.userId);
req.isAdmin = await isAdminGuard(req.userId, prisma);
await next();
};
+2 -4
View File
@@ -1,8 +1,6 @@
import { UserPermissionRole } from "@prisma/client";
import { PrismaClient, UserPermissionRole } from "@prisma/client";
import prisma from "@calcom/prisma";
export const isAdminGuard = async (userId: number) => {
export const isAdminGuard = async (userId: number, prisma: PrismaClient) => {
const user = await prisma.user.findUnique({ where: { id: userId } });
return user?.role === UserPermissionRole.ADMIN;
};
+1 -1
View File
@@ -7,7 +7,7 @@ export type GetSubscriberOptions = {
eventTypeId: number;
triggerEvent: WebhookTriggerEvents;
};
/** @note will this not work with custom prisma? since we're importing prisma directly and not passing it from request here **/
const getWebhooks = async (options: GetSubscriberOptions) => {
const { userId, eventTypeId } = options;
const allWebhooks = await prisma.webhook.findMany({
+1 -1
View File
@@ -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" });
+1 -1
View File
@@ -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" });
+1 -1
View File
@@ -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" });
+2 -2
View File
@@ -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;
+1 -1
View File
@@ -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);