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.

1.1 KB
Raw Blame
import type { NextFunction, Request, Response } from 'express';
import { GoalPermissionsFetcher } from '../../../core/GoalPermissionsFetcher';
import { $logger } from '../../../modules/logget';
import { GoalPermissions } from '../../../types/auth.types';
import { logError } from '../../../utils/api';

export const CanFetchUsersCollaboration = async (req: Request, res: Response, next: NextFunction) => {
    const goalId = req.params.goalId;
    if (!goalId) {
        return res.status(400).end();
    }

    const permissions = await req.appUser.permissionsFetcher
        .getPermissionsForType(Number(goalId), GoalPermissionsFetcher.PERMISSION_TYPE_FOR_GOAL)
        .catch(logError);

    if (!permissions) {
        $logger.error('Can not get permissions for CanFetchUsersCollaboration middleware');
        return res.status(500).end();
    }

    if (
        permissions.hasPermissions(GoalPermissions.TASKS_CAN_ASSIGN_USERS) ||
        permissions.hasPermissions(GoalPermissions.GOAL_CAN_MANAGE_USERS) ||
        permissions.hasPermissions(GoalPermissions.TASKS_CAN_WATCH_ASSIGNED_USERS)
    ) {
        return next();
    }

    return res.status(403).end();
};