fosrl.pangolin/server/middlewares/verifyTargetAccess.ts

130 lines
3.4 KiB
TypeScript
Raw Normal View History

import { Request, Response, NextFunction } from "express";
import { db } from "@server/db";
import { resources, targets, userOrgs } from "@server/db/schema";
import { and, eq } from "drizzle-orm";
import createHttpError from "http-errors";
import HttpCode from "@server/types/HttpCode";
2025-01-11 19:47:07 -05:00
import { canUserAccessResource } from "../auth/canUserAccessResource";
2024-10-03 22:31:20 -04:00
export async function verifyTargetAccess(
req: Request,
res: Response,
next: NextFunction
) {
const userId = req.user!.userId;
2024-10-06 18:05:20 -04:00
const targetId = parseInt(req.params.targetId);
2024-10-03 22:31:20 -04:00
2024-10-06 18:05:20 -04:00
if (!userId) {
return next(
createHttpError(HttpCode.UNAUTHORIZED, "User not authenticated")
);
2024-10-06 18:05:20 -04:00
}
2024-10-03 22:31:20 -04:00
2024-10-06 18:05:20 -04:00
if (isNaN(targetId)) {
return next(
createHttpError(HttpCode.BAD_REQUEST, "Invalid organization ID")
);
2024-10-06 18:05:20 -04:00
}
2024-10-03 22:31:20 -04:00
const target = await db
.select()
2024-10-06 18:05:20 -04:00
.from(targets)
.where(eq(targets.targetId, targetId))
.limit(1);
2024-10-03 22:31:20 -04:00
2024-10-06 18:05:20 -04:00
if (target.length === 0) {
return next(
createHttpError(
HttpCode.NOT_FOUND,
2024-11-09 23:59:19 -05:00
`Target with ID ${targetId} not found`
2024-10-06 18:05:20 -04:00
)
);
}
2024-10-03 22:31:20 -04:00
2024-10-06 18:05:20 -04:00
const resourceId = target[0].resourceId;
2024-10-03 22:31:20 -04:00
2024-10-19 20:47:05 -04:00
if (!resourceId) {
2024-10-06 18:05:20 -04:00
return next(
createHttpError(
HttpCode.INTERNAL_SERVER_ERROR,
2024-11-09 23:59:19 -05:00
`Target with ID ${targetId} does not have a resource ID`
2024-10-06 18:05:20 -04:00
)
);
}
2024-10-03 22:31:20 -04:00
try {
const resource = await db
.select()
.from(resources)
.where(eq(resources.resourceId, resourceId!))
.limit(1);
2024-10-03 22:31:20 -04:00
if (resource.length === 0) {
return next(
createHttpError(
HttpCode.NOT_FOUND,
`Resource with ID ${resourceId} not found`
)
);
}
2024-10-03 22:31:20 -04:00
if (!resource[0].orgId) {
return next(
createHttpError(
HttpCode.INTERNAL_SERVER_ERROR,
`resource with ID ${resourceId} does not have an organization ID`
)
);
}
2024-11-09 23:59:19 -05:00
if (!req.userOrg) {
const res = await db
.select()
.from(userOrgs)
.where(
and(
eq(userOrgs.userId, userId),
eq(userOrgs.orgId, resource[0].orgId)
)
);
2024-11-09 23:59:19 -05:00
req.userOrg = res[0];
}
2024-11-09 23:59:19 -05:00
if (!req.userOrg) {
next(
createHttpError(
HttpCode.FORBIDDEN,
"User does not have access to this organization"
)
);
} else {
2024-11-09 23:59:19 -05:00
req.userOrgRoleId = req.userOrg.roleId;
req.userOrgId = resource[0].orgId!;
}
const resourceAllowed = await canUserAccessResource({
userId,
resourceId,
roleId: req.userOrgRoleId!
});
if (!resourceAllowed) {
return next(
createHttpError(
HttpCode.FORBIDDEN,
"User does not have access to this resource"
)
);
}
next();
} catch (e) {
2024-10-06 18:05:20 -04:00
return next(
createHttpError(
HttpCode.INTERNAL_SERVER_ERROR,
"Error verifying organization access"
2024-10-06 18:05:20 -04:00
)
);
}
2024-10-13 17:13:47 -04:00
}