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\Model; |
13
|
|
|
|
14
|
|
|
use Jitamin\Foundation\Database\Model; |
15
|
|
|
use Jitamin\Foundation\Security\Role; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Project User Role. |
19
|
|
|
*/ |
20
|
|
|
class ProjectUserRoleModel extends Model |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* SQL table name. |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
const TABLE = 'project_has_users'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Get the list of active project for the given user. |
31
|
|
|
* |
32
|
|
|
* @param int $user_id |
33
|
|
|
* |
34
|
|
|
* @return array |
35
|
|
|
*/ |
36
|
|
|
public function getActiveProjectsByUser($user_id) |
37
|
|
|
{ |
38
|
|
|
return $this->getProjectsByUser($user_id, [ProjectModel::ACTIVE]); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Get the list of project visible for the given user. |
43
|
|
|
* |
44
|
|
|
* @param int $user_id |
45
|
|
|
* @param array $status |
46
|
|
|
* |
47
|
|
|
* @return array |
48
|
|
|
*/ |
49
|
|
|
public function getProjectsByUser($user_id, $status = [ProjectModel::ACTIVE, ProjectModel::INACTIVE]) |
50
|
|
|
{ |
51
|
|
|
$userProjects = $this->db |
|
|
|
|
52
|
|
|
->hashtable(ProjectModel::TABLE) |
53
|
|
|
->beginOr() |
54
|
|
|
->eq(self::TABLE.'.user_id', $user_id) |
55
|
|
|
->eq(ProjectModel::TABLE.'.is_everybody_allowed', 1) |
56
|
|
|
->closeOr() |
57
|
|
|
->in(ProjectModel::TABLE.'.is_active', $status) |
58
|
|
|
->join(self::TABLE, 'project_id', 'id') |
59
|
|
|
->getAll(ProjectModel::TABLE.'.id', ProjectModel::TABLE.'.name'); |
60
|
|
|
|
61
|
|
|
$groupProjects = $this->projectGroupRoleModel->getProjectsByUser($user_id, $status); |
|
|
|
|
62
|
|
|
$projects = $userProjects + $groupProjects; |
63
|
|
|
|
64
|
|
|
asort($projects); |
65
|
|
|
|
66
|
|
|
return $projects; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* For a given project get the role of the specified user. |
71
|
|
|
* |
72
|
|
|
* @param int $project_id |
73
|
|
|
* @param int $user_id |
74
|
|
|
* |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
|
|
public function getUserRole($project_id, $user_id) |
78
|
|
|
{ |
79
|
|
|
$projectInfo = $this->db->table(ProjectModel::TABLE) |
|
|
|
|
80
|
|
|
->eq('id', $project_id) |
81
|
|
|
->columns('owner_id', 'is_everybody_allowed') |
82
|
|
|
->findOne(); |
83
|
|
|
|
84
|
|
|
if ($projectInfo['is_everybody_allowed'] == 1) { |
85
|
|
|
return $projectInfo['owner_id'] == $user_id ? Role::PROJECT_MANAGER : Role::PROJECT_MEMBER; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$role = $this->db->table(self::TABLE)->eq('user_id', $user_id)->eq('project_id', $project_id)->findOneColumn('role'); |
|
|
|
|
89
|
|
|
|
90
|
|
|
if (empty($role)) { |
91
|
|
|
$role = $this->projectGroupRoleModel->getUserRole($project_id, $user_id); |
|
|
|
|
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $role; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Get all users associated directly to the project. |
99
|
|
|
* |
100
|
|
|
* @param int $project_id |
101
|
|
|
* |
102
|
|
|
* @return array |
103
|
|
|
*/ |
104
|
|
View Code Duplication |
public function getUsers($project_id) |
|
|
|
|
105
|
|
|
{ |
106
|
|
|
return $this->db->table(self::TABLE) |
|
|
|
|
107
|
|
|
->columns(UserModel::TABLE.'.id', UserModel::TABLE.'.username', UserModel::TABLE.'.name', self::TABLE.'.role') |
108
|
|
|
->join(UserModel::TABLE, 'id', 'user_id') |
109
|
|
|
->eq('project_id', $project_id) |
110
|
|
|
->asc(UserModel::TABLE.'.username') |
111
|
|
|
->asc(UserModel::TABLE.'.name') |
112
|
|
|
->findAll(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Get all users (fetch users from groups). |
117
|
|
|
* |
118
|
|
|
* @param int $project_id |
119
|
|
|
* |
120
|
|
|
* @return array |
121
|
|
|
*/ |
122
|
|
|
public function getAllUsers($project_id) |
123
|
|
|
{ |
124
|
|
|
$userMembers = $this->getUsers($project_id); |
125
|
|
|
$groupMembers = $this->projectGroupRoleModel->getUsers($project_id); |
|
|
|
|
126
|
|
|
$members = array_merge($userMembers, $groupMembers); |
127
|
|
|
|
128
|
|
|
return $this->userModel->prepareList($members); |
|
|
|
|
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Get users grouped by role. |
133
|
|
|
* |
134
|
|
|
* @param int $project_id Project id |
135
|
|
|
* |
136
|
|
|
* @return array |
137
|
|
|
*/ |
138
|
|
|
public function getAllUsersGroupedByRole($project_id) |
139
|
|
|
{ |
140
|
|
|
$users = []; |
141
|
|
|
|
142
|
|
|
$userMembers = $this->getUsers($project_id); |
143
|
|
|
$groupMembers = $this->projectGroupRoleModel->getUsers($project_id); |
|
|
|
|
144
|
|
|
$members = array_merge($userMembers, $groupMembers); |
145
|
|
|
|
146
|
|
|
foreach ($members as $user) { |
147
|
|
|
if (!isset($users[$user['role']])) { |
148
|
|
|
$users[$user['role']] = []; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
$users[$user['role']][$user['id']] = $user['name'] ?: $user['username']; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return $users; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Get list of users that can be assigned to a task (only Manager and Member). |
159
|
|
|
* |
160
|
|
|
* @param int $project_id |
161
|
|
|
* |
162
|
|
|
* @return array |
163
|
|
|
*/ |
164
|
|
|
public function getAssignableUsers($project_id) |
165
|
|
|
{ |
166
|
|
|
if ($this->projectPermissionModel->isEverybodyAllowed($project_id)) { |
|
|
|
|
167
|
|
|
return $this->userModel->getActiveUsersList(); |
|
|
|
|
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
$userMembers = $this->db->table(self::TABLE) |
|
|
|
|
171
|
|
|
->columns(UserModel::TABLE.'.id', UserModel::TABLE.'.username', UserModel::TABLE.'.name') |
172
|
|
|
->join(UserModel::TABLE, 'id', 'user_id') |
173
|
|
|
->eq(UserModel::TABLE.'.is_active', 1) |
174
|
|
|
->eq(self::TABLE.'.project_id', $project_id) |
175
|
|
|
->neq(self::TABLE.'.role', Role::PROJECT_VIEWER) |
176
|
|
|
->findAll(); |
177
|
|
|
|
178
|
|
|
$groupMembers = $this->projectGroupRoleModel->getAssignableUsers($project_id); |
|
|
|
|
179
|
|
|
$members = array_merge($userMembers, $groupMembers); |
180
|
|
|
|
181
|
|
|
return $this->userModel->prepareList($members); |
|
|
|
|
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Get list of users that can be assigned to a task (only Manager and Member). |
186
|
|
|
* |
187
|
|
|
* @param int $project_id Project id |
188
|
|
|
* @param bool $unassigned Prepend the 'Unassigned' value |
189
|
|
|
* @param bool $everybody Prepend the 'Everbody' value |
190
|
|
|
* @param bool $singleUser If there is only one user return only this user |
191
|
|
|
* |
192
|
|
|
* @return array |
193
|
|
|
*/ |
194
|
|
|
public function getAssignableUsersList($project_id, $unassigned = true, $everybody = false, $singleUser = false) |
195
|
|
|
{ |
196
|
|
|
$users = $this->getAssignableUsers($project_id); |
197
|
|
|
|
198
|
|
|
if ($singleUser && count($users) === 1) { |
199
|
|
|
return $users; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
if ($unassigned) { |
203
|
|
|
$users = [t('Unassigned')] + $users; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
if ($everybody) { |
207
|
|
|
$users = [UserModel::EVERYBODY_ID => t('Everybody')] + $users; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
return $users; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Add a user to the project. |
215
|
|
|
* |
216
|
|
|
* @param int $project_id |
217
|
|
|
* @param int $user_id |
218
|
|
|
* @param string $role |
219
|
|
|
* |
220
|
|
|
* @return bool |
221
|
|
|
*/ |
222
|
|
View Code Duplication |
public function addUser($project_id, $user_id, $role) |
|
|
|
|
223
|
|
|
{ |
224
|
|
|
return $this->db->table(self::TABLE)->insert([ |
|
|
|
|
225
|
|
|
'user_id' => $user_id, |
226
|
|
|
'project_id' => $project_id, |
227
|
|
|
'role' => $role, |
228
|
|
|
]); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
|
232
|
|
View Code Duplication |
public function getRemoveUser() |
|
|
|
|
233
|
|
|
{ |
234
|
|
|
$user = $this->userModel->getById($this->request->getIntegerParam('user_id')); |
|
|
|
|
235
|
|
|
|
236
|
|
|
if (empty($user)) { |
237
|
|
|
throw new PageNotFoundException(); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
$role = $this->getUserRole($this->request->getIntegerParam('project_id', 0), $this->userSession->getId()); |
|
|
|
|
241
|
|
|
|
242
|
|
|
if (!$this->userSession->isAdmin() && $role != Role::PROJECT_MANAGER) { |
|
|
|
|
243
|
|
|
throw new AccessForbiddenException(); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
return $user; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Remove a user from the project. |
252
|
|
|
* |
253
|
|
|
* @param int $project_id |
254
|
|
|
* @param int $user_id |
255
|
|
|
* |
256
|
|
|
* @return bool |
257
|
|
|
*/ |
258
|
|
|
public function removeUser($project_id, $user_id) |
259
|
|
|
{ |
260
|
|
|
return $this->db->table(self::TABLE)->eq('user_id', $user_id)->eq('project_id', $project_id)->remove(); |
|
|
|
|
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* Change a user role for the project. |
265
|
|
|
* |
266
|
|
|
* @param int $project_id |
267
|
|
|
* @param int $user_id |
268
|
|
|
* @param string $role |
269
|
|
|
* |
270
|
|
|
* @return bool |
271
|
|
|
*/ |
272
|
|
|
public function changeUserRole($project_id, $user_id, $role) |
273
|
|
|
{ |
274
|
|
|
return $this->db->table(self::TABLE) |
|
|
|
|
275
|
|
|
->eq('user_id', $user_id) |
276
|
|
|
->eq('project_id', $project_id) |
277
|
|
|
->update([ |
278
|
|
|
'role' => $role, |
279
|
|
|
]); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Copy user access from a project to another one. |
284
|
|
|
* |
285
|
|
|
* @param int $project_src_id |
286
|
|
|
* @param int $project_dst_id |
287
|
|
|
* |
288
|
|
|
* @return bool |
289
|
|
|
*/ |
290
|
|
View Code Duplication |
public function duplicate($project_src_id, $project_dst_id) |
|
|
|
|
291
|
|
|
{ |
292
|
|
|
$rows = $this->db->table(self::TABLE)->eq('project_id', $project_src_id)->findAll(); |
|
|
|
|
293
|
|
|
|
294
|
|
|
foreach ($rows as $row) { |
295
|
|
|
$result = $this->db->table(self::TABLE)->save([ |
|
|
|
|
296
|
|
|
'project_id' => $project_dst_id, |
297
|
|
|
'user_id' => $row['user_id'], |
298
|
|
|
'role' => $row['role'], |
299
|
|
|
]); |
300
|
|
|
|
301
|
|
|
if (!$result) { |
302
|
|
|
return false; |
303
|
|
|
} |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
return true; |
307
|
|
|
} |
308
|
|
|
} |
309
|
|
|
|
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.