summaryrefslogtreecommitdiff
path: root/app/Model/Acl.php
blob: 92ce0c5a026a817477b7875eab4edc557b56249d (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
<?php

namespace Kanboard\Model;

/**
 * Access List
 *
 * @package  model
 * @author   Frederic Guillot
 */
class Acl extends Base
{
    /**
     * Controllers and actions allowed from outside
     *
     * @access private
     * @var array
     */
    private $public_acl = array(
        'auth' => array('login', 'check', 'captcha'),
        'task' => array('readonly'),
        'board' => array('readonly'),
        'webhook' => '*',
        'ical' => '*',
        'feed' => '*',
        'oauth' => array('google', 'github', 'gitlab'),
    );

    /**
     * Controllers and actions for project members
     *
     * @access private
     * @var array
     */
    private $project_member_acl = array(
        'board' => '*',
        'comment' => '*',
        'file' => '*',
        'project' => array('show'),
        'listing' => '*',
        'activity' => '*',
        'subtask' => '*',
        'task' => '*',
        'taskduplication' => '*',
        'taskcreation' => '*',
        'taskmodification' => '*',
        'taskstatus' => '*',
        'tasklink' => '*',
        'timer' => '*',
        'customfilter' => '*',
        'calendar' => array('show', 'project'),
    );

    /**
     * Controllers and actions for project managers
     *
     * @access private
     * @var array
     */
    private $project_manager_acl = array(
        'action' => '*',
        'analytic' => '*',
        'category' => '*',
        'column' => '*',
        'export' => '*',
        'taskimport' => '*',
        'project' => array('edit', 'update', 'share', 'integration', 'users', 'alloweverybody', 'allow', 'setowner', 'revoke', 'duplicate', 'disable', 'enable'),
        'swimlane' => '*',
        'gantt' => array('project', 'savetaskdate', 'task', 'savetask'),
    );

    /**
     * Controllers and actions for project admins
     *
     * @access private
     * @var array
     */
    private $project_admin_acl = array(
        'project' => array('remove'),
        'projectuser' => '*',
        'gantt' => array('projects', 'saveprojectdate'),
    );

    /**
     * Controllers and actions for admins
     *
     * @access private
     * @var array
     */
    private $admin_acl = array(
        'user' => array('index', 'create', 'save', 'remove', 'authentication'),
        'userimport' => '*',
        'config' => '*',
        'link' => '*',
        'currency' => '*',
        'twofactor' => array('disable'),
    );

    /**
     * Extend ACL rules
     *
     * @access public
     * @param string $acl_name
     * @param aray   $rules
     */
    public function extend($acl_name, array $rules)
    {
        $this->$acl_name = array_merge($this->$acl_name, $rules);
    }

    /**
     * Return true if the specified controller/action match the given acl
     *
     * @access public
     * @param  array    $acl          Acl list
     * @param  string   $controller   Controller name
     * @param  string   $action       Action name
     * @return bool
     */
    public function matchAcl(array $acl, $controller, $action)
    {
        $controller = strtolower($controller);
        $action = strtolower($action);
        return isset($acl[$controller]) && $this->hasAction($action, $acl[$controller]);
    }

    /**
     * Return true if the specified action is inside the list of actions
     *
     * @access public
     * @param  string   $action       Action name
     * @param  mixed    $action       Actions list
     * @return bool
     */
    public function hasAction($action, $actions)
    {
        if (is_array($actions)) {
            return in_array($action, $actions);
        }

        return $actions === '*';
    }

    /**
     * Return true if the given action is public
     *
     * @access public
     * @param  string   $controller   Controller name
     * @param  string   $action       Action name
     * @return bool
     */
    public function isPublicAction($controller, $action)
    {
        return $this->matchAcl($this->public_acl, $controller, $action);
    }

    /**
     * Return true if the given action is for admins
     *
     * @access public
     * @param  string   $controller   Controller name
     * @param  string   $action       Action name
     * @return bool
     */
    public function isAdminAction($controller, $action)
    {
        return $this->matchAcl($this->admin_acl, $controller, $action);
    }

    /**
     * Return true if the given action is for project managers
     *
     * @access public
     * @param  string   $controller   Controller name
     * @param  string   $action       Action name
     * @return bool
     */
    public function isProjectManagerAction($controller, $action)
    {
        return $this->matchAcl($this->project_manager_acl, $controller, $action);
    }

    /**
     * Return true if the given action is for application managers
     *
     * @access public
     * @param  string   $controller   Controller name
     * @param  string   $action       Action name
     * @return bool
     */
    public function isProjectAdminAction($controller, $action)
    {
        return $this->matchAcl($this->project_admin_acl, $controller, $action);
    }

    /**
     * Return true if the given action is for project members
     *
     * @access public
     * @param  string   $controller   Controller name
     * @param  string   $action       Action name
     * @return bool
     */
    public function isProjectMemberAction($controller, $action)
    {
        return $this->matchAcl($this->project_member_acl, $controller, $action);
    }

    /**
     * Return true if the visitor is allowed to access to the given page
     * We suppose the user already authenticated
     *
     * @access public
     * @param  string   $controller   Controller name
     * @param  string   $action       Action name
     * @param  integer  $project_id   Project id
     * @return bool
     */
    public function isAllowed($controller, $action, $project_id = 0)
    {
        // If you are admin you have access to everything
        if ($this->userSession->isAdmin()) {
            return true;
        }

        // If you access to an admin action, your are not allowed
        if ($this->isAdminAction($controller, $action)) {
            return false;
        }

        // Check project admin permissions
        if ($this->isProjectAdminAction($controller, $action)) {
            return $this->handleProjectAdminPermissions($project_id);
        }

        // Check project manager permissions
        if ($this->isProjectManagerAction($controller, $action)) {
            return $this->handleProjectManagerPermissions($project_id);
        }

        // Check project member permissions
        if ($this->isProjectMemberAction($controller, $action)) {
            return $project_id > 0 && $this->projectPermission->isMember($project_id, $this->userSession->getId());
        }

        // Other applications actions are allowed
        return true;
    }

    /**
     * Handle permission for project manager
     *
     * @access public
     * @param integer $project_id
     * @return boolean
     */
    public function handleProjectManagerPermissions($project_id)
    {
        if ($project_id > 0) {
            if ($this->userSession->isProjectAdmin()) {
                return $this->projectPermission->isMember($project_id, $this->userSession->getId());
            }

            return $this->projectPermission->isManager($project_id, $this->userSession->getId());
        }

        return false;
    }

    /**
     * Handle permission for project admins
     *
     * @access public
     * @param integer $project_id
     * @return boolean
     */
    public function handleProjectAdminPermissions($project_id)
    {
        if (! $this->userSession->isProjectAdmin()) {
            return false;
        }

        if ($project_id > 0) {
            return $this->projectPermission->isMember($project_id, $this->userSession->getId());
        }

        return true;
    }
}