mirror of
https://github.com/fosrl/pangolin.git
synced 2025-08-03 09:34:48 +02:00
refactor and reorganize
This commit is contained in:
parent
9732098799
commit
3b4a993704
216 changed files with 519 additions and 2128 deletions
|
@ -2,7 +2,7 @@ import { ErrorRequestHandler, NextFunction, Response } from "express";
|
|||
import ErrorResponse from "@server/types/ErrorResponse";
|
||||
import HttpCode from "@server/types/HttpCode";
|
||||
import logger from "@server/logger";
|
||||
import config from "@server/config";
|
||||
import config from "@server/lib/config";
|
||||
|
||||
export const errorHandlerMiddleware: ErrorRequestHandler = (
|
||||
error,
|
||||
|
|
|
@ -1,45 +0,0 @@
|
|||
import db from "@server/db";
|
||||
import { and, eq } from "drizzle-orm";
|
||||
import { roleResources, userResources } from "@server/db/schema";
|
||||
|
||||
export async function canUserAccessResource({
|
||||
userId,
|
||||
resourceId,
|
||||
roleId
|
||||
}: {
|
||||
userId: string;
|
||||
resourceId: number;
|
||||
roleId: number;
|
||||
}): Promise<boolean> {
|
||||
const roleResourceAccess = await db
|
||||
.select()
|
||||
.from(roleResources)
|
||||
.where(
|
||||
and(
|
||||
eq(roleResources.resourceId, resourceId),
|
||||
eq(roleResources.roleId, roleId)
|
||||
)
|
||||
)
|
||||
.limit(1);
|
||||
|
||||
if (roleResourceAccess.length > 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const userResourceAccess = await db
|
||||
.select()
|
||||
.from(userResources)
|
||||
.where(
|
||||
and(
|
||||
eq(userResources.userId, userId),
|
||||
eq(userResources.resourceId, resourceId)
|
||||
)
|
||||
)
|
||||
.limit(1);
|
||||
|
||||
if (userResourceAccess.length > 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
|
@ -4,7 +4,7 @@ import { resourceAccessToken, resources, userOrgs } from "@server/db/schema";
|
|||
import { and, eq } from "drizzle-orm";
|
||||
import createHttpError from "http-errors";
|
||||
import HttpCode from "@server/types/HttpCode";
|
||||
import { canUserAccessResource } from "./helpers/canUserAccessResource";
|
||||
import { canUserAccessResource } from "@server/lib/canUserAccessResource";
|
||||
|
||||
export async function verifyAccessTokenAccess(
|
||||
req: Request,
|
||||
|
|
|
@ -1,16 +1,17 @@
|
|||
import { NextFunction, Response } from "express";
|
||||
import ErrorResponse from "@server/types/ErrorResponse";
|
||||
import { unauthorized, verifySession } from "@server/auth";
|
||||
import { db } from "@server/db";
|
||||
import { users } from "@server/db/schema";
|
||||
import { eq } from "drizzle-orm";
|
||||
import createHttpError from "http-errors";
|
||||
import HttpCode from "@server/types/HttpCode";
|
||||
import { verifySession } from "@server/auth/sessions/verifySession";
|
||||
import { unauthorized } from "@server/auth/unauthorizedResponse";
|
||||
|
||||
export const verifySessionMiddleware = async (
|
||||
req: any,
|
||||
res: Response<ErrorResponse>,
|
||||
next: NextFunction,
|
||||
next: NextFunction
|
||||
) => {
|
||||
const { session, user } = await verifySession(req);
|
||||
if (!session || !user) {
|
||||
|
@ -24,7 +25,7 @@ export const verifySessionMiddleware = async (
|
|||
|
||||
if (!existingUser || !existingUser[0]) {
|
||||
return next(
|
||||
createHttpError(HttpCode.BAD_REQUEST, "User does not exist"),
|
||||
createHttpError(HttpCode.BAD_REQUEST, "User does not exist")
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
@ -4,7 +4,7 @@ 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";
|
||||
import { canUserAccessResource } from "./helpers/canUserAccessResource";
|
||||
import { canUserAccessResource } from "../lib/canUserAccessResource";
|
||||
|
||||
export async function verifyTargetAccess(
|
||||
req: Request,
|
||||
|
|
|
@ -1,12 +1,13 @@
|
|||
import { NextFunction, Response } from "express";
|
||||
import ErrorResponse from "@server/types/ErrorResponse";
|
||||
import { unauthorized, verifySession } from "@server/auth";
|
||||
import { db } from "@server/db";
|
||||
import { users } from "@server/db/schema";
|
||||
import { eq } from "drizzle-orm";
|
||||
import createHttpError from "http-errors";
|
||||
import HttpCode from "@server/types/HttpCode";
|
||||
import config from "@server/config";
|
||||
import config from "@server/lib/config";
|
||||
import { verifySession } from "@server/auth/sessions/verifySession";
|
||||
import { unauthorized } from "@server/auth/unauthorizedResponse";
|
||||
|
||||
export const verifySessionUserMiddleware = async (
|
||||
req: any,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue