fosrl.pangolin/server/routers/auth/verifySiteAccess.ts

80 lines
2.9 KiB
TypeScript
Raw Normal View History

2024-10-03 22:31:20 -04:00
import { Request, Response, NextFunction } from 'express';
import { db } from '@server/db';
import { sites, userOrgs, userSites, roleSites, roles } from '@server/db/schema';
import { and, eq, or } from 'drizzle-orm';
2024-10-03 22:31:20 -04:00
import createHttpError from 'http-errors';
import HttpCode from '@server/types/HttpCode';
export async function verifySiteAccess(req: Request, res: Response, next: NextFunction) {
2024-10-13 17:13:47 -04:00
const userId = req.user!.userId; // Assuming you have user information in the request
2024-10-12 22:31:24 -04:00
const siteId = parseInt(req.params.siteId || req.body.siteId || req.query.siteId);
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-03 22:31:20 -04:00
2024-10-06 18:05:20 -04:00
if (isNaN(siteId)) {
return next(createHttpError(HttpCode.BAD_REQUEST, 'Invalid site ID'));
}
2024-10-03 22:31:20 -04:00
2024-10-06 18:05:20 -04:00
try {
// Get the site
const site = await db.select().from(sites).where(eq(sites.siteId, siteId)).limit(1);
2024-10-03 22:31:20 -04:00
2024-10-06 18:05:20 -04:00
if (site.length === 0) {
return next(createHttpError(HttpCode.NOT_FOUND, `Site with ID ${siteId} not found`));
}
2024-10-06 18:05:20 -04:00
if (!site[0].orgId) {
return next(createHttpError(HttpCode.INTERNAL_SERVER_ERROR, `Site with ID ${siteId} does not have an organization ID`));
}
2024-10-06 18:05:20 -04:00
// Get user's role ID in the organization
const userOrgRole = await db.select()
.from(userOrgs)
.where(and(eq(userOrgs.userId, userId), eq(userOrgs.orgId, site[0].orgId)))
.limit(1);
2024-10-06 18:05:20 -04:00
if (userOrgRole.length === 0) {
return next(createHttpError(HttpCode.FORBIDDEN, 'User does not have access to this organization'));
}
2024-10-06 18:05:20 -04:00
const userOrgRoleId = userOrgRole[0].roleId;
req.userOrgRoleId = userOrgRoleId;
req.userOrgId = site[0].orgId;
2024-10-03 22:31:20 -04:00
2024-10-06 18:05:20 -04:00
// Check role-based site access first
const roleSiteAccess = await db.select()
.from(roleSites)
.where(
and(
eq(roleSites.siteId, siteId),
eq(roleSites.roleId, userOrgRoleId)
)
)
.limit(1);
2024-10-06 18:05:20 -04:00
if (roleSiteAccess.length > 0) {
// User's role has access to the site
return next();
}
2024-10-06 18:05:20 -04:00
// If role doesn't have access, check user-specific site access
const userSiteAccess = await db.select()
.from(userSites)
.where(and(eq(userSites.userId, userId), eq(userSites.siteId, siteId)))
.limit(1);
2024-10-03 22:31:20 -04:00
2024-10-06 18:05:20 -04:00
if (userSiteAccess.length > 0) {
// User has direct access to the site
return next();
}
2024-10-06 18:05:20 -04:00
// If we reach here, the user doesn't have access to the site
return next(createHttpError(HttpCode.FORBIDDEN, 'User does not have access to this site'));
2024-10-06 18:05:20 -04:00
} catch (error) {
return next(createHttpError(HttpCode.INTERNAL_SERVER_ERROR, 'Error verifying site access'));
}
2024-10-13 17:13:47 -04:00
}