mirror of
https://github.com/fosrl/pangolin.git
synced 2025-08-03 09:34:48 +02:00
rename super user to admin and middleware refactoring
This commit is contained in:
parent
7b755a273c
commit
03051878ef
26 changed files with 790 additions and 529 deletions
|
@ -1,16 +1,16 @@
|
|||
import { Request, Response, NextFunction } from 'express';
|
||||
import { z } from 'zod';
|
||||
import { db } from '@server/db';
|
||||
import { eq } from 'drizzle-orm';
|
||||
import { orgs, userOrgs } from '@server/db/schema';
|
||||
import { Request, Response, NextFunction } from "express";
|
||||
import { z } from "zod";
|
||||
import { db } from "@server/db";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { orgs, userOrgs } from "@server/db/schema";
|
||||
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 { createSuperUserRole } from '@server/db/ensureActions';
|
||||
import HttpCode from "@server/types/HttpCode";
|
||||
import createHttpError from "http-errors";
|
||||
import { ActionsEnum, checkUserActionPermission } from "@server/auth/actions";
|
||||
import logger from "@server/logger";
|
||||
import { createAdminRole } from "@server/db/ensureActions";
|
||||
import config, { APP_PATH } from "@server/config";
|
||||
import { fromError } from 'zod-validation-error';
|
||||
import { fromError } from "zod-validation-error";
|
||||
|
||||
const createOrgSchema = z.object({
|
||||
orgId: z.string(),
|
||||
|
@ -20,7 +20,11 @@ const createOrgSchema = z.object({
|
|||
|
||||
const MAX_ORGS = 5;
|
||||
|
||||
export async function createOrg(req: Request, res: Response, next: NextFunction): Promise<any> {
|
||||
export async function createOrg(
|
||||
req: Request,
|
||||
res: Response,
|
||||
next: NextFunction
|
||||
): Promise<any> {
|
||||
try {
|
||||
const parsedBody = createOrgSchema.safeParse(req.body);
|
||||
if (!parsedBody.success) {
|
||||
|
@ -43,7 +47,7 @@ export async function createOrg(req: Request, res: Response, next: NextFunction)
|
|||
}
|
||||
|
||||
// TODO: we cant do this when they create an org because they are not in an org yet... maybe we need to make the org id optional on the userActions table
|
||||
// Check if the user has permission
|
||||
// Check if the user has permission
|
||||
// const hasPermission = await checkUserActionPermission(ActionsEnum.createOrg, req);
|
||||
// if (!hasPermission) {
|
||||
// return next(createHttpError(HttpCode.FORBIDDEN, 'User does not have permission to perform this action'));
|
||||
|
@ -52,11 +56,12 @@ export async function createOrg(req: Request, res: Response, next: NextFunction)
|
|||
const { orgId, name } = parsedBody.data;
|
||||
|
||||
// make sure the orgId is unique
|
||||
const orgExists = await db.select()
|
||||
const orgExists = await db
|
||||
.select()
|
||||
.from(orgs)
|
||||
.where(eq(orgs.orgId, orgId))
|
||||
.limit(1);
|
||||
|
||||
|
||||
if (orgExists.length > 0) {
|
||||
return next(
|
||||
createHttpError(
|
||||
|
@ -69,29 +74,34 @@ export async function createOrg(req: Request, res: Response, next: NextFunction)
|
|||
// create a url from config.app.base_url and get the hostname
|
||||
const domain = new URL(config.app.base_url).hostname;
|
||||
|
||||
const newOrg = await db.insert(orgs).values({
|
||||
orgId,
|
||||
name,
|
||||
domain
|
||||
}).returning();
|
||||
const newOrg = await db
|
||||
.insert(orgs)
|
||||
.values({
|
||||
orgId,
|
||||
name,
|
||||
domain,
|
||||
})
|
||||
.returning();
|
||||
|
||||
const roleId = await createSuperUserRole(newOrg[0].orgId);
|
||||
const roleId = await createAdminRole(newOrg[0].orgId);
|
||||
|
||||
if (!roleId) {
|
||||
return next(
|
||||
createHttpError(
|
||||
HttpCode.INTERNAL_SERVER_ERROR,
|
||||
`Error creating Super User role`
|
||||
`Error creating Admin role`
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
// put the user in the super user role
|
||||
await db.insert(userOrgs).values({
|
||||
userId: req.user!.userId,
|
||||
orgId: newOrg[0].orgId,
|
||||
roleId: roleId,
|
||||
}).execute();
|
||||
await db
|
||||
.insert(userOrgs)
|
||||
.values({
|
||||
userId: req.user!.userId,
|
||||
orgId: newOrg[0].orgId,
|
||||
roleId: roleId,
|
||||
})
|
||||
.execute();
|
||||
|
||||
return response(res, {
|
||||
data: newOrg[0],
|
||||
|
@ -102,6 +112,11 @@ export async function createOrg(req: Request, res: Response, next: NextFunction)
|
|||
});
|
||||
} catch (error) {
|
||||
logger.error(error);
|
||||
return next(createHttpError(HttpCode.INTERNAL_SERVER_ERROR, "An error occurred..."));
|
||||
return next(
|
||||
createHttpError(
|
||||
HttpCode.INTERNAL_SERVER_ERROR,
|
||||
"An error occurred..."
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue