1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Jitamin. |
5
|
|
|
* |
6
|
|
|
* Copyright (C) Jitamin Team |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Jitamin\Helper; |
13
|
|
|
|
14
|
|
|
use Jitamin\Foundation\Base; |
15
|
|
|
use Jitamin\Foundation\Security\Role; |
16
|
|
|
use Jitamin\Model\ColumnRestrictionModel; |
17
|
|
|
use Jitamin\Model\ProjectRoleRestrictionModel; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class ProjectRoleHelper. |
21
|
|
|
*/ |
22
|
|
|
class ProjectRoleHelper extends Base |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Get project role for the current user. |
26
|
|
|
* |
27
|
|
|
* @param int $project_id |
28
|
|
|
* |
29
|
|
|
* @return string |
30
|
|
|
*/ |
31
|
|
|
public function getProjectUserRole($project_id) |
32
|
|
|
{ |
33
|
|
|
return $this->memoryCache->proxy($this->projectUserRoleModel, 'getUserRole', $project_id, $this->userSession->getId()); |
|
|
|
|
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Return true if the task can be moved by the logged user. |
38
|
|
|
* |
39
|
|
|
* @param array $task |
40
|
|
|
* |
41
|
|
|
* @return bool |
42
|
|
|
*/ |
43
|
|
|
public function isDraggable(array &$task) |
44
|
|
|
{ |
45
|
|
|
if ($task['is_active'] == 1 && $this->helper->user->hasProjectAccess('Project/Board/BoardAjaxController', 'store', $task['project_id'])) { |
|
|
|
|
46
|
|
|
return $this->isSortableColumn($task['project_id'], $task['column_id']); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return false; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Return true is the column is sortable. |
54
|
|
|
* |
55
|
|
|
* @param int $project_id |
56
|
|
|
* @param int $column_id |
57
|
|
|
* |
58
|
|
|
* @return bool |
59
|
|
|
*/ |
60
|
|
|
public function isSortableColumn($project_id, $column_id) |
61
|
|
|
{ |
62
|
|
|
$role = $this->getProjectUserRole($project_id); |
63
|
|
|
|
64
|
|
|
if ($this->role->isCustomProjectRole($role)) { |
|
|
|
|
65
|
|
|
$sortableColumns = $this->columnMoveRestrictionCacheDecorator->getSortableColumns($project_id, $role); |
|
|
|
|
66
|
|
|
|
67
|
|
|
foreach ($sortableColumns as $column) { |
68
|
|
|
if ($column['src_column_id'] == $column_id || $column['dst_column_id'] == $column_id) { |
69
|
|
|
return true; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return empty($sortableColumns) && $this->isAllowedToMoveTask($project_id, $role); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return true; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Check if the user can move a task. |
81
|
|
|
* |
82
|
|
|
* @param int $project_id |
83
|
|
|
* @param int $src_column_id |
84
|
|
|
* @param int $dst_column_id |
85
|
|
|
* |
86
|
|
|
* @return bool|int |
87
|
|
|
*/ |
88
|
|
|
public function canMoveTask($project_id, $src_column_id, $dst_column_id) |
89
|
|
|
{ |
90
|
|
|
$role = $this->getProjectUserRole($project_id); |
91
|
|
|
|
92
|
|
|
if ($this->role->isCustomProjectRole($role)) { |
|
|
|
|
93
|
|
|
if ($src_column_id == $dst_column_id) { |
94
|
|
|
return true; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$sortableColumns = $this->columnMoveRestrictionCacheDecorator->getSortableColumns($project_id, $role); |
|
|
|
|
98
|
|
|
|
99
|
|
|
foreach ($sortableColumns as $column) { |
100
|
|
|
if ($column['src_column_id'] == $src_column_id && $column['dst_column_id'] == $dst_column_id) { |
101
|
|
|
return true; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
if ($column['dst_column_id'] == $src_column_id && $column['src_column_id'] == $dst_column_id) { |
105
|
|
|
return true; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return empty($sortableColumns) && $this->isAllowedToMoveTask($project_id, $role); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return true; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Return true if the user can create a task for the given column. |
117
|
|
|
* |
118
|
|
|
* @param int $project_id |
119
|
|
|
* @param int $column_id |
120
|
|
|
* |
121
|
|
|
* @return bool |
122
|
|
|
*/ |
123
|
|
View Code Duplication |
public function canCreateTaskInColumn($project_id, $column_id) |
|
|
|
|
124
|
|
|
{ |
125
|
|
|
$role = $this->getProjectUserRole($project_id); |
126
|
|
|
|
127
|
|
|
if ($this->role->isCustomProjectRole($role)) { |
|
|
|
|
128
|
|
|
if (!$this->isAllowedToCreateTask($project_id, $column_id, $role)) { |
129
|
|
|
return false; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return $this->helper->user->hasProjectAccess('Task/TaskController', 'create', $project_id); |
|
|
|
|
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Return true if the user can create a task for the given column. |
138
|
|
|
* |
139
|
|
|
* @param int $project_id |
140
|
|
|
* @param int $column_id |
141
|
|
|
* |
142
|
|
|
* @return bool |
143
|
|
|
*/ |
144
|
|
View Code Duplication |
public function canChangeTaskStatusInColumn($project_id, $column_id) |
|
|
|
|
145
|
|
|
{ |
146
|
|
|
$role = $this->getProjectUserRole($project_id); |
147
|
|
|
|
148
|
|
|
if ($this->role->isCustomProjectRole($role)) { |
|
|
|
|
149
|
|
|
if (!$this->isAllowedToChangeTaskStatus($project_id, $column_id, $role)) { |
150
|
|
|
return false; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return $this->helper->user->hasProjectAccess('Task/TaskStatusController', 'close', $project_id); |
|
|
|
|
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Return true if the user can remove a task. |
159
|
|
|
* |
160
|
|
|
* Regular users can't remove tasks from other people |
161
|
|
|
* |
162
|
|
|
* @public |
163
|
|
|
* |
164
|
|
|
* @param array $task |
165
|
|
|
* |
166
|
|
|
* @return bool |
167
|
|
|
*/ |
168
|
|
|
public function canRemoveTask(array $task) |
169
|
|
|
{ |
170
|
|
|
if (isset($task['creator_id']) && $task['creator_id'] == $this->userSession->getId()) { |
|
|
|
|
171
|
|
|
return true; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
if ($this->userSession->isAdmin() || $this->getProjectUserRole($task['project_id']) === Role::PROJECT_MANAGER) { |
|
|
|
|
175
|
|
|
return true; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
return false; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Check project access. |
183
|
|
|
* |
184
|
|
|
* @param string $controller |
185
|
|
|
* @param string $action |
186
|
|
|
* @param int $project_id |
187
|
|
|
* |
188
|
|
|
* @return bool |
189
|
|
|
*/ |
190
|
|
|
public function checkProjectAccess($controller, $action, $project_id) |
191
|
|
|
{ |
192
|
|
|
if (!$this->userSession->isLogged()) { |
|
|
|
|
193
|
|
|
return false; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
if ($this->userSession->isAdmin()) { |
|
|
|
|
197
|
|
|
return true; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
if (!$this->helper->user->hasAccess($controller, $action)) { |
|
|
|
|
201
|
|
|
return false; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
$role = $this->getProjectUserRole($project_id); |
205
|
|
|
|
206
|
|
|
if ($this->role->isCustomProjectRole($role)) { |
|
|
|
|
207
|
|
|
$result = $this->projectAuthorization->isAllowed($controller, $action, Role::PROJECT_MEMBER); |
|
|
|
|
208
|
|
|
} else { |
209
|
|
|
$result = $this->projectAuthorization->isAllowed($controller, $action, $role); |
|
|
|
|
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
return $result; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Check authorization for a custom project role to change the task status. |
217
|
|
|
* |
218
|
|
|
* @param int $project_id |
219
|
|
|
* @param int $column_id |
220
|
|
|
* @param string $role |
221
|
|
|
* |
222
|
|
|
* @return bool |
223
|
|
|
*/ |
224
|
|
View Code Duplication |
protected function isAllowedToChangeTaskStatus($project_id, $column_id, $role) |
|
|
|
|
225
|
|
|
{ |
226
|
|
|
$columnRestrictions = $this->columnRestrictionCacheDecorator->getAllByRole($project_id, $role); |
|
|
|
|
227
|
|
|
|
228
|
|
|
foreach ($columnRestrictions as $restriction) { |
229
|
|
|
if ($restriction['column_id'] == $column_id) { |
230
|
|
|
if ($restriction['rule'] == ColumnRestrictionModel::RULE_ALLOW_TASK_OPEN_CLOSE) { |
231
|
|
|
return true; |
232
|
|
|
} elseif ($restriction['rule'] == ColumnRestrictionModel::RULE_BLOCK_TASK_OPEN_CLOSE) { |
233
|
|
|
return false; |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
$projectRestrictions = $this->projectRoleRestrictionCacheDecorator->getAllByRole($project_id, $role); |
|
|
|
|
239
|
|
|
|
240
|
|
|
foreach ($projectRestrictions as $restriction) { |
241
|
|
|
if ($restriction['rule'] == ProjectRoleRestrictionModel::RULE_TASK_OPEN_CLOSE) { |
242
|
|
|
return false; |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
return true; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Check authorization for a custom project role to create a task. |
251
|
|
|
* |
252
|
|
|
* @param int $project_id |
253
|
|
|
* @param int $column_id |
254
|
|
|
* @param string $role |
255
|
|
|
* |
256
|
|
|
* @return bool |
257
|
|
|
*/ |
258
|
|
View Code Duplication |
protected function isAllowedToCreateTask($project_id, $column_id, $role) |
|
|
|
|
259
|
|
|
{ |
260
|
|
|
$columnRestrictions = $this->columnRestrictionCacheDecorator->getAllByRole($project_id, $role); |
|
|
|
|
261
|
|
|
|
262
|
|
|
foreach ($columnRestrictions as $restriction) { |
263
|
|
|
if ($restriction['column_id'] == $column_id) { |
264
|
|
|
if ($restriction['rule'] == ColumnRestrictionModel::RULE_ALLOW_TASK_CREATION) { |
265
|
|
|
return true; |
266
|
|
|
} elseif ($restriction['rule'] == ColumnRestrictionModel::RULE_BLOCK_TASK_CREATION) { |
267
|
|
|
return false; |
268
|
|
|
} |
269
|
|
|
} |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
$projectRestrictions = $this->projectRoleRestrictionCacheDecorator->getAllByRole($project_id, $role); |
|
|
|
|
273
|
|
|
|
274
|
|
|
foreach ($projectRestrictions as $restriction) { |
275
|
|
|
if ($restriction['rule'] == ProjectRoleRestrictionModel::RULE_TASK_CREATION) { |
276
|
|
|
return false; |
277
|
|
|
} |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
return true; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* Check if the role can move task in the given project. |
285
|
|
|
* |
286
|
|
|
* @param int $project_id |
287
|
|
|
* @param string $role |
288
|
|
|
* |
289
|
|
|
* @return bool |
290
|
|
|
*/ |
291
|
|
|
protected function isAllowedToMoveTask($project_id, $role) |
292
|
|
|
{ |
293
|
|
|
$projectRestrictions = $this->projectRoleRestrictionCacheDecorator->getAllByRole($project_id, $role); |
|
|
|
|
294
|
|
|
|
295
|
|
|
foreach ($projectRestrictions as $restriction) { |
296
|
|
|
if ($restriction['rule'] == ProjectRoleRestrictionModel::RULE_TASK_MOVE) { |
297
|
|
|
return false; |
298
|
|
|
} |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
return true; |
302
|
|
|
} |
303
|
|
|
} |
304
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.