Skip to content

TaskView is a self-hosted project and task management platform focused on clarity, ownership, and control. TaskView is built for teams that want a transparent, self-hosted alternative to SaaS task managers.

fix: handle graph permissions in the API

Nikolai Giman996dd11af915
diff --git a/api/src/tv-modules/graph/GraphRepository.ts b/api/src/tv-modules/graph/GraphRepository.ts
index 89bd777..6d99678 100644
--- a/api/src/tv-modules/graph/GraphRepository.ts
+++ b/api/src/tv-modules/graph/GraphRepository.ts
@@ -18,6 +18,13 @@ export class GraphRepository {
return result ?? [];
}
+ public async fetchById(id: number): Promise<GraphReturnRelationsType | null> {
+ const result = await callWithCatch(() =>
+ this.db.dbDrizzle.select().from(GraphRelationsSchema).where(eq(GraphRelationsSchema.id, id))
+ );
+ return result?.[0] ?? null;
+ }
+
public async fetchAllEdges(goalId: number): Promise<GraphReturnRelationsType[]> {
const result = await callWithCatch(() =>
this.db.dbDrizzle.select().from(GraphRelationsSchema).where(eq(GraphRelationsSchema.goalId, goalId))
diff --git a/api/src/tv-modules/graph/GraphRoutes.ts b/api/src/tv-modules/graph/GraphRoutes.ts
index 1a7951c..c136517 100644
--- a/api/src/tv-modules/graph/GraphRoutes.ts
+++ b/api/src/tv-modules/graph/GraphRoutes.ts
@@ -2,6 +2,9 @@ import { Router } from 'express';
import type { Routable } from '../../types/routable.type';
import { IsLoggedIn } from '../auth/middlewares/is-logged-in';
import { GraphController } from './GraphControler';
+import { CanManageGraph } from './middlewares/CanManageGraph';
+import { CanViewGraph } from './middlewares/CanViewGraph';
+
export default class GraphRoutes implements Routable {
private readonly router: ReturnType<typeof Router>;
private readonly graphController: GraphController;
@@ -17,8 +20,8 @@ export default class GraphRoutes implements Routable {
}
initRoutes() {
- this.router.post('', [IsLoggedIn], this.graphController.addEdge);
- this.router.get('/:goalId', [IsLoggedIn], this.graphController.fetchAllEdges);
- this.router.delete('/:id', [IsLoggedIn], this.graphController.deleteEdge);
+ this.router.post('', [IsLoggedIn, CanManageGraph], this.graphController.addEdge);
+ this.router.get('/:goalId', [IsLoggedIn, CanViewGraph], this.graphController.fetchAllEdges);
+ this.router.delete('/:id', [IsLoggedIn, CanManageGraph], this.graphController.deleteEdge);
}
}
diff --git a/api/src/tv-modules/graph/middlewares/CanManageGraph.ts b/api/src/tv-modules/graph/middlewares/CanManageGraph.ts
new file mode 100644
index 0000000..207d684
--- /dev/null
+++ b/api/src/tv-modules/graph/middlewares/CanManageGraph.ts
@@ -0,0 +1,17 @@
+import type { NextFunction, Request, Response } from 'express';
+import { GoalPermissions } from '../../../types/auth.types';
+import { resolveGoalId } from './resolveGoalId';
+
+export const CanManageGraph = async (req: Request, res: Response, next: NextFunction) => {
+ const goalId = await resolveGoalId(req);
+ if (!goalId) {
+ return res.status(400).end();
+ }
+
+ const checker = await req.appUser.permissionsFetcher.getCheckerForGoal(goalId);
+ if (checker.hasPermissions(GoalPermissions.GRAPH_CAN_MANAGE)) {
+ return next();
+ }
+
+ return res.status(403).end();
+};
diff --git a/api/src/tv-modules/graph/middlewares/CanViewGraph.ts b/api/src/tv-modules/graph/middlewares/CanViewGraph.ts
new file mode 100644
index 0000000..98d233f
--- /dev/null
+++ b/api/src/tv-modules/graph/middlewares/CanViewGraph.ts
@@ -0,0 +1,17 @@
+import type { NextFunction, Request, Response } from 'express';
+import { GoalPermissions } from '../../../types/auth.types';
+import { resolveGoalId } from './resolveGoalId';
+
+export const CanViewGraph = async (req: Request, res: Response, next: NextFunction) => {
+ const goalId = await resolveGoalId(req);
+ if (!goalId) {
+ return res.status(400).end();
+ }
+
+ const checker = await req.appUser.permissionsFetcher.getCheckerForGoal(goalId);
+ if (checker.hasPermissions(GoalPermissions.GRAPH_CAN_VIEW)) {
+ return next();
+ }
+
+ return res.status(403).end();
+};
diff --git a/api/src/tv-modules/graph/middlewares/resolveGoalId.ts b/api/src/tv-modules/graph/middlewares/resolveGoalId.ts
new file mode 100644
index 0000000..cc26308
--- /dev/null
+++ b/api/src/tv-modules/graph/middlewares/resolveGoalId.ts
@@ -0,0 +1,37 @@
+import type { Request } from 'express';
+import { GraphRepository } from '../GraphRepository';
+import { TasksRepository } from '../../tasks/TasksRepository';
+
+/**
+ * Resolves goalId from graph request.
+ * - GET /:goalId → params.goalId
+ * - POST (addEdge) → resolve via fromTaskId (body.source)
+ * - DELETE /:id → resolve via edge id
+ */
+export async function resolveGoalId(req: Request): Promise<number | null> {
+ // Direct goalId in params (fetchAllEdges)
+ if (req.params.goalId) {
+ const id = Number(req.params.goalId);
+ return isNaN(id) ? null : id;
+ }
+
+ // addEdge: resolve goalId from task
+ if (req.body?.source) {
+ const taskId = Number(req.body.source);
+ if (isNaN(taskId)) return null;
+ const tasksRepo = new TasksRepository();
+ const task = await tasksRepo.fetchTaskByIdNew(taskId);
+ return task?.goalId ?? null;
+ }
+
+ // deleteEdge: resolve goalId from edge
+ if (req.params.id) {
+ const edgeId = Number(req.params.id);
+ if (isNaN(edgeId)) return null;
+ const graphRepo = new GraphRepository();
+ const edge = await graphRepo.fetchById(edgeId);
+ return edge?.goalId ?? null;
+ }
+
+ return null;
+}