mirror of
https://github.com/fosrl/pangolin.git
synced 2025-08-30 14:39:29 +02:00
69 lines
1.7 KiB
TypeScript
69 lines
1.7 KiB
TypeScript
|
import { Request, Response, NextFunction } from "express";
|
||
|
import { db } from "@server/db";
|
||
|
import { userOrgs } from "@server/db/schema";
|
||
|
import { and, eq } from "drizzle-orm";
|
||
|
import createHttpError from "http-errors";
|
||
|
import HttpCode from "@server/types/HttpCode";
|
||
|
|
||
|
export async function verifyUserIsOrgOwner(
|
||
|
req: Request,
|
||
|
res: Response,
|
||
|
next: NextFunction
|
||
|
) {
|
||
|
const userId = req.user!.userId;
|
||
|
const orgId = req.params.orgId;
|
||
|
let userOrg = req.userOrg;
|
||
|
|
||
|
if (!userId) {
|
||
|
return next(
|
||
|
createHttpError(HttpCode.UNAUTHORIZED, "User not authenticated")
|
||
|
);
|
||
|
}
|
||
|
|
||
|
if (!orgId) {
|
||
|
return next(
|
||
|
createHttpError(
|
||
|
HttpCode.BAD_REQUEST,
|
||
|
"Organization ID not provided"
|
||
|
)
|
||
|
);
|
||
|
}
|
||
|
|
||
|
try {
|
||
|
if (!userOrg) {
|
||
|
const res = await db
|
||
|
.select()
|
||
|
.from(userOrgs)
|
||
|
.where(
|
||
|
and(eq(userOrgs.userId, userId), eq(userOrgs.orgId, orgId))
|
||
|
);
|
||
|
userOrg = res[0];
|
||
|
}
|
||
|
|
||
|
if (!userOrg) {
|
||
|
return next(
|
||
|
createHttpError(
|
||
|
HttpCode.FORBIDDEN,
|
||
|
"User does not have access to this organization"
|
||
|
)
|
||
|
);
|
||
|
}
|
||
|
|
||
|
if (!userOrg.isOwner) {
|
||
|
return next(
|
||
|
createHttpError(
|
||
|
HttpCode.FORBIDDEN,
|
||
|
"User is not an organization owner"
|
||
|
)
|
||
|
);
|
||
|
}
|
||
|
} catch (e) {
|
||
|
return next(
|
||
|
createHttpError(
|
||
|
HttpCode.INTERNAL_SERVER_ERROR,
|
||
|
"Error verifying organization access"
|
||
|
)
|
||
|
);
|
||
|
}
|
||
|
}
|