fosrl.pangolin/server/routers/site/createSite.ts

166 lines
4.7 KiB
TypeScript
Raw Normal View History

import { Request, Response, NextFunction } from "express";
import { z } from "zod";
import { db } from "@server/db";
import {
roles,
userSites,
sites,
roleSites,
exitNodes,
} from "@server/db/schema";
2024-10-01 21:34:07 -04:00
import response from "@server/utils/response";
import HttpCode from "@server/types/HttpCode";
import createHttpError from "http-errors";
import { ActionsEnum, checkUserActionPermission } from "@server/auth/actions";
import logger from "@server/logger";
import { eq, and } from "drizzle-orm";
import { getUniqueSiteName } from "@server/db/names";
import { addPeer } from "../gerbil/peers";
import { fromError } from "zod-validation-error";
2024-10-01 21:34:07 -04:00
2024-10-06 18:05:20 -04:00
const createSiteParamsSchema = z.object({
orgId: z.string(),
2024-10-02 22:05:21 -04:00
});
2024-10-01 21:34:07 -04:00
2024-10-02 22:05:21 -04:00
// Define Zod schema for request body validation
const createSiteSchema = z.object({
2024-10-06 18:05:20 -04:00
name: z.string().min(1).max(255),
exitNodeId: z.number().int().positive(),
2024-10-06 18:05:20 -04:00
subdomain: z.string().min(1).max(255).optional(),
pubKey: z.string(),
subnet: z.string(),
2024-10-02 22:05:21 -04:00
});
2024-10-20 12:55:28 -04:00
export type CreateSiteResponse = {
2024-10-14 21:59:35 -04:00
name: string;
siteId: number;
orgId: string;
niceId: string;
// niceId: string;
// subdomain: string;
// subnet: string;
};
export async function createSite(
req: Request,
res: Response,
next: NextFunction
): Promise<any> {
2024-10-06 18:05:20 -04:00
try {
// Validate request body
const parsedBody = createSiteSchema.safeParse(req.body);
if (!parsedBody.success) {
return next(
createHttpError(
HttpCode.BAD_REQUEST,
fromError(parsedBody.error).toString()
2024-10-06 18:05:20 -04:00
)
);
}
2024-10-02 22:05:21 -04:00
const { name, subdomain, exitNodeId, pubKey, subnet } = parsedBody.data;
2024-10-02 22:05:21 -04:00
2024-10-06 18:05:20 -04:00
// Validate request params
const parsedParams = createSiteParamsSchema.safeParse(req.params);
if (!parsedParams.success) {
return next(
createHttpError(
HttpCode.BAD_REQUEST,
fromError(parsedParams.error).toString()
2024-10-06 18:05:20 -04:00
)
);
}
2024-10-02 22:05:21 -04:00
2024-10-06 18:05:20 -04:00
const { orgId } = parsedParams.data;
// Check if the user has permission to list sites
const hasPermission = await checkUserActionPermission(
ActionsEnum.createSite,
req
);
2024-10-06 18:05:20 -04:00
if (!hasPermission) {
return next(
createHttpError(
HttpCode.FORBIDDEN,
"User does not have permission perform this action"
)
);
2024-10-12 21:36:14 -04:00
}
if (!req.userOrgRoleId) {
return next(
createHttpError(HttpCode.FORBIDDEN, "User does not have a role")
);
2024-10-06 18:05:20 -04:00
}
2024-10-06 16:43:59 -04:00
const niceId = await getUniqueSiteName(orgId);
2024-10-14 21:59:35 -04:00
2024-10-06 18:05:20 -04:00
// Create new site in the database
const [newSite] = await db
.insert(sites)
.values({
orgId,
exitNodeId,
name,
niceId,
pubKey,
subnet,
})
.returning();
2024-11-03 17:28:12 -05:00
// find the Super User roleId and also add the resource to the Super User role
const superUserRole = await db
.select()
.from(roles)
2024-11-03 17:28:12 -05:00
.where(and(eq(roles.isSuperUserRole, true), eq(roles.orgId, orgId)))
.limit(1);
2024-10-13 17:13:47 -04:00
2024-11-03 17:28:12 -05:00
if (superUserRole.length === 0) {
2024-10-12 21:36:14 -04:00
return next(
2024-11-03 17:28:12 -05:00
createHttpError(HttpCode.NOT_FOUND, `Super User role not found`)
2024-10-12 21:36:14 -04:00
);
}
2024-10-13 17:13:47 -04:00
await db.insert(roleSites).values({
2024-11-03 17:28:12 -05:00
roleId: superUserRole[0].roleId,
2024-10-14 21:59:35 -04:00
siteId: newSite.siteId,
2024-10-12 21:36:14 -04:00
});
2024-10-02 22:05:21 -04:00
2024-11-03 17:28:12 -05:00
if (req.userOrgRoleId != superUserRole[0].roleId) {
2024-10-12 21:36:14 -04:00
// make sure the user can access the site
2024-10-13 17:13:47 -04:00
db.insert(userSites).values({
userId: req.user?.userId!,
2024-10-14 21:59:35 -04:00
siteId: newSite.siteId,
2024-10-12 21:36:14 -04:00
});
}
2024-10-13 17:13:47 -04:00
// Add the peer to the exit node
await addPeer(exitNodeId, {
publicKey: pubKey,
allowedIps: [],
});
2024-10-06 18:05:20 -04:00
return response(res, {
2024-10-14 21:59:35 -04:00
data: {
name: newSite.name,
niceId: newSite.niceId,
siteId: newSite.siteId,
orgId: newSite.orgId,
// subdomain: newSite.subdomain,
// subnet: newSite.subnet,
},
2024-10-06 18:05:20 -04:00
success: true,
error: false,
message: "Site created successfully",
status: HttpCode.CREATED,
});
} catch (error) {
logger.error(error);
return next(
createHttpError(
HttpCode.INTERNAL_SERVER_ERROR,
"An error occurred..."
)
);
2024-10-06 18:05:20 -04:00
}
}