mirror of
https://github.com/fosrl/pangolin.git
synced 2025-08-03 09:34:48 +02:00
more validation and redirects
This commit is contained in:
parent
0ff183796c
commit
57ba84eb02
18 changed files with 620 additions and 443 deletions
|
@ -9,7 +9,6 @@ export enum ActionsEnum {
|
|||
createOrg = "createOrg",
|
||||
deleteOrg = "deleteOrg",
|
||||
getOrg = "getOrg",
|
||||
listOrgs = "listOrgs",
|
||||
updateOrg = "updateOrg",
|
||||
createSite = "createSite",
|
||||
deleteSite = "deleteSite",
|
||||
|
|
|
@ -1,26 +1,33 @@
|
|||
import { Request, Response, NextFunction } from 'express';
|
||||
import { db } from '@server/db';
|
||||
import { userOrgs, orgs } from '@server/db/schema';
|
||||
import { eq } from 'drizzle-orm';
|
||||
import createHttpError from 'http-errors';
|
||||
import HttpCode from '@server/types/HttpCode';
|
||||
import { Request, Response, NextFunction } from "express";
|
||||
import { db } from "@server/db";
|
||||
import { userOrgs, orgs } from "@server/db/schema";
|
||||
import { eq } from "drizzle-orm";
|
||||
import createHttpError from "http-errors";
|
||||
import HttpCode from "@server/types/HttpCode";
|
||||
|
||||
export async function getUserOrgs(req: Request, res: Response, next: NextFunction) {
|
||||
export async function getUserOrgs(
|
||||
req: Request,
|
||||
res: Response,
|
||||
next: NextFunction,
|
||||
) {
|
||||
const userId = req.user?.userId; // Assuming you have user information in the request
|
||||
|
||||
if (!userId) {
|
||||
return next(createHttpError(HttpCode.UNAUTHORIZED, 'User not authenticated'));
|
||||
return next(
|
||||
createHttpError(HttpCode.UNAUTHORIZED, "User not authenticated"),
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
const userOrganizations = await db.select({
|
||||
orgId: userOrgs.orgId,
|
||||
roleId: userOrgs.roleId,
|
||||
})
|
||||
const userOrganizations = await db
|
||||
.select({
|
||||
orgId: userOrgs.orgId,
|
||||
roleId: userOrgs.roleId,
|
||||
})
|
||||
.from(userOrgs)
|
||||
.where(eq(userOrgs.userId, userId));
|
||||
|
||||
req.userOrgIds = userOrganizations.map(org => org.orgId);
|
||||
req.userOrgIds = userOrganizations.map((org) => org.orgId);
|
||||
// req.userOrgRoleIds = userOrganizations.reduce((acc, org) => {
|
||||
// acc[org.orgId] = org.role;
|
||||
// return acc;
|
||||
|
@ -28,6 +35,11 @@ export async function getUserOrgs(req: Request, res: Response, next: NextFunctio
|
|||
|
||||
next();
|
||||
} catch (error) {
|
||||
next(createHttpError(HttpCode.INTERNAL_SERVER_ERROR, 'Error retrieving user organizations'));
|
||||
next(
|
||||
createHttpError(
|
||||
HttpCode.INTERNAL_SERVER_ERROR,
|
||||
"Error retrieving user organizations",
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,17 +13,19 @@ const listOrgsSchema = z.object({
|
|||
limit: z
|
||||
.string()
|
||||
.optional()
|
||||
.default("1000")
|
||||
.transform(Number)
|
||||
.pipe(z.number().int().positive().default(10)),
|
||||
.pipe(z.number().int().positive()),
|
||||
offset: z
|
||||
.string()
|
||||
.optional()
|
||||
.default("0")
|
||||
.transform(Number)
|
||||
.pipe(z.number().int().nonnegative().default(0)),
|
||||
.pipe(z.number().int().nonnegative()),
|
||||
});
|
||||
|
||||
export type ListOrgsResponse = {
|
||||
organizations: Org[];
|
||||
orgs: Org[];
|
||||
pagination: { total: number; limit: number; offset: number };
|
||||
};
|
||||
|
||||
|
@ -45,27 +47,13 @@ export async function listOrgs(
|
|||
|
||||
const { limit, offset } = parsedQuery.data;
|
||||
|
||||
// Check if the user has permission to list sites
|
||||
const hasPermission = await checkUserActionPermission(
|
||||
ActionsEnum.listOrgs,
|
||||
req,
|
||||
);
|
||||
if (!hasPermission) {
|
||||
return next(
|
||||
createHttpError(
|
||||
HttpCode.FORBIDDEN,
|
||||
"User does not have permission to perform this action",
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// Use the userOrgs passed from the middleware
|
||||
const userOrgIds = req.userOrgIds;
|
||||
|
||||
if (!userOrgIds || userOrgIds.length === 0) {
|
||||
return response<ListOrgsResponse>(res, {
|
||||
data: {
|
||||
organizations: [],
|
||||
orgs: [],
|
||||
pagination: {
|
||||
total: 0,
|
||||
limit,
|
||||
|
@ -94,7 +82,7 @@ export async function listOrgs(
|
|||
|
||||
return response<ListOrgsResponse>(res, {
|
||||
data: {
|
||||
organizations,
|
||||
orgs: organizations,
|
||||
pagination: {
|
||||
total: totalCount,
|
||||
limit,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue