summaryrefslogtreecommitdiff
path: root/app/Api/BaseApi.php
blob: d8c46fd52baa0e06b3303112a0cb411150097456 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php

namespace Kanboard\Api;

use JsonRPC\Exception\AccessDeniedException;
use Kanboard\Core\Base;

/**
 * Base class
 *
 * @package  Kanboard\Api
 * @author   Frederic Guillot
 */
abstract class BaseApi extends Base
{
    private $user_allowed_procedures = array(
        'getMe',
        'getMyDashboard',
        'getMyActivityStream',
        'createMyPrivateProject',
        'getMyProjectsList',
        'getMyProjects',
        'getMyOverdueTasks',
    );

    private $both_allowed_procedures = array(
        'getTimezone',
        'getVersion',
        'getDefaultTaskColor',
        'getDefaultTaskColors',
        'getColorList',
        'getProjectById',
        'getTask',
        'getTaskByReference',
        'getAllTasks',
        'openTask',
        'closeTask',
        'moveTaskPosition',
        'createTask',
        'updateTask',
        'getBoard',
        'getProjectActivity',
        'getOverdueTasksByProject',
        'searchTasks',
    );

    public function checkProcedurePermission($is_user, $procedure)
    {
        $is_both_procedure = in_array($procedure, $this->both_allowed_procedures);
        $is_user_procedure = in_array($procedure, $this->user_allowed_procedures);

        if ($is_user && ! $is_both_procedure && ! $is_user_procedure) {
            throw new AccessDeniedException('Permission denied');
        } elseif (! $is_user && ! $is_both_procedure && $is_user_procedure) {
            throw new AccessDeniedException('Permission denied');
        }

        $this->logger->debug('API call: '.$procedure);
    }

    public function checkProjectPermission($project_id)
    {
        if ($this->userSession->isLogged() && ! $this->projectPermission->isUserAllowed($project_id, $this->userSession->getId())) {
            throw new AccessDeniedException('Permission denied');
        }
    }

    public function checkTaskPermission($task_id)
    {
        if ($this->userSession->isLogged()) {
            $this->checkProjectPermission($this->taskFinder->getProjectId($task_id));
        }
    }

    protected function formatTask($task)
    {
        if (! empty($task)) {
            $task['url'] = $this->helper->url->to('TaskViewController', 'show', array('task_id' => $task['id'], 'project_id' => $task['project_id']), '', true);
            $task['color'] = $this->color->getColorProperties($task['color_id']);
        }

        return $task;
    }

    protected function formatTasks($tasks)
    {
        if (! empty($tasks)) {
            foreach ($tasks as &$task) {
                $task = $this->formatTask($task);
            }
        }

        return $tasks;
    }

    protected function formatProject($project)
    {
        if (! empty($project)) {
            $project['url'] = array(
                'board' => $this->helper->url->to('board', 'show', array('project_id' => $project['id']), '', true),
                'calendar' => $this->helper->url->to('calendar', 'show', array('project_id' => $project['id']), '', true),
                'list' => $this->helper->url->to('listing', 'show', array('project_id' => $project['id']), '', true),
            );
        }

        return $project;
    }

    protected function formatProjects($projects)
    {
        if (! empty($projects)) {
            foreach ($projects as &$project) {
                $project = $this->formatProject($project);
            }
        }

        return $projects;
    }
}