mirror of
https://github.com/fosrl/pangolin.git
synced 2025-08-04 18:14:53 +02:00
add resource whitelist auth method
This commit is contained in:
parent
998fab6d0a
commit
207a7b8a39
20 changed files with 970 additions and 739 deletions
64
server/routers/resource/getResourceWhitelist.ts
Normal file
64
server/routers/resource/getResourceWhitelist.ts
Normal file
|
@ -0,0 +1,64 @@
|
|||
import { Request, Response, NextFunction } from "express";
|
||||
import { z } from "zod";
|
||||
import { db } from "@server/db";
|
||||
import { resourceWhitelist, users } from "@server/db/schema"; // Assuming these are the correct tables
|
||||
import { eq } from "drizzle-orm";
|
||||
import response from "@server/utils/response";
|
||||
import HttpCode from "@server/types/HttpCode";
|
||||
import createHttpError from "http-errors";
|
||||
import logger from "@server/logger";
|
||||
import { fromError } from "zod-validation-error";
|
||||
|
||||
const getResourceWhitelistSchema = z.object({
|
||||
resourceId: z.string().transform(Number).pipe(z.number().int().positive())
|
||||
});
|
||||
|
||||
async function queryWhitelist(resourceId: number) {
|
||||
return await db
|
||||
.select({
|
||||
email: resourceWhitelist.email
|
||||
})
|
||||
.from(resourceWhitelist)
|
||||
.where(eq(resourceWhitelist.resourceId, resourceId));
|
||||
}
|
||||
|
||||
export type GetResourceWhitelistResponse = {
|
||||
whitelist: NonNullable<Awaited<ReturnType<typeof queryWhitelist>>>;
|
||||
};
|
||||
|
||||
export async function getResourceWhitelist(
|
||||
req: Request,
|
||||
res: Response,
|
||||
next: NextFunction
|
||||
): Promise<any> {
|
||||
try {
|
||||
const parsedParams = getResourceWhitelistSchema.safeParse(req.params);
|
||||
if (!parsedParams.success) {
|
||||
return next(
|
||||
createHttpError(
|
||||
HttpCode.BAD_REQUEST,
|
||||
fromError(parsedParams.error).toString()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
const { resourceId } = parsedParams.data;
|
||||
|
||||
const whitelist = await queryWhitelist(resourceId);
|
||||
|
||||
return response<GetResourceWhitelistResponse>(res, {
|
||||
data: {
|
||||
whitelist
|
||||
},
|
||||
success: true,
|
||||
error: false,
|
||||
message: "Resource whitelist retrieved successfully",
|
||||
status: HttpCode.OK
|
||||
});
|
||||
} catch (error) {
|
||||
logger.error(error);
|
||||
return next(
|
||||
createHttpError(HttpCode.INTERNAL_SERVER_ERROR, "An error occurred")
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue