|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Arthur Schiwon <[email protected]> |
|
4
|
|
|
* @author Bart Visscher <[email protected]> |
|
5
|
|
|
* @author Joas Schilling <[email protected]> |
|
6
|
|
|
* @author Lukas Reschke <[email protected]> |
|
7
|
|
|
* @author Morris Jobke <[email protected]> |
|
8
|
|
|
* @author Robin Appelman <[email protected]> |
|
9
|
|
|
* @author Robin McCorkell <[email protected]> |
|
10
|
|
|
* |
|
11
|
|
|
* @copyright Copyright (c) 2015, ownCloud, Inc. |
|
12
|
|
|
* @license AGPL-3.0 |
|
13
|
|
|
* |
|
14
|
|
|
* This code is free software: you can redistribute it and/or modify |
|
15
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
|
16
|
|
|
* as published by the Free Software Foundation. |
|
17
|
|
|
* |
|
18
|
|
|
* This program is distributed in the hope that it will be useful, |
|
19
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
20
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
21
|
|
|
* GNU Affero General Public License for more details. |
|
22
|
|
|
* |
|
23
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
|
24
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
|
25
|
|
|
* |
|
26
|
|
|
*/ |
|
27
|
|
|
|
|
28
|
|
|
namespace OC\Group; |
|
29
|
|
|
|
|
30
|
|
|
use OCP\IGroup; |
|
31
|
|
|
|
|
32
|
|
|
class Group implements IGroup { |
|
33
|
|
|
/** |
|
34
|
|
|
* @var string $id |
|
35
|
|
|
*/ |
|
36
|
|
|
private $gid; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var \OC\User\User[] $users |
|
40
|
|
|
*/ |
|
41
|
|
|
private $users = array(); |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var bool $usersLoaded |
|
45
|
|
|
*/ |
|
46
|
|
|
private $usersLoaded; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var \OC_Group_Backend[]|\OC_Group_Database[] $backend |
|
50
|
|
|
*/ |
|
51
|
|
|
private $backends; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @var \OC\Hooks\PublicEmitter $emitter |
|
55
|
|
|
*/ |
|
56
|
|
|
private $emitter; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @var \OC\User\Manager $userManager |
|
60
|
|
|
*/ |
|
61
|
|
|
private $userManager; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param string $gid |
|
65
|
|
|
* @param \OC_Group_Backend[] $backends |
|
66
|
|
|
* @param \OC\User\Manager $userManager |
|
67
|
|
|
* @param \OC\Hooks\PublicEmitter $emitter |
|
68
|
|
|
*/ |
|
69
|
470 |
|
public function __construct($gid, $backends, $userManager, $emitter = null) { |
|
70
|
470 |
|
$this->gid = $gid; |
|
71
|
470 |
|
$this->backends = $backends; |
|
72
|
470 |
|
$this->userManager = $userManager; |
|
73
|
470 |
|
$this->emitter = $emitter; |
|
74
|
470 |
|
} |
|
75
|
|
|
|
|
76
|
477 |
|
public function getGID() { |
|
77
|
477 |
|
return $this->gid; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* get all users in the group |
|
82
|
|
|
* |
|
83
|
|
|
* @return \OC\User\User[] |
|
84
|
|
|
*/ |
|
85
|
4 |
|
public function getUsers() { |
|
86
|
4 |
|
if ($this->usersLoaded) { |
|
87
|
|
|
return $this->users; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
4 |
|
$userIds = array(); |
|
91
|
4 |
|
foreach ($this->backends as $backend) { |
|
92
|
4 |
|
$diff = array_diff( |
|
93
|
4 |
|
$backend->usersInGroup($this->gid), |
|
94
|
|
|
$userIds |
|
95
|
4 |
|
); |
|
96
|
4 |
|
if ($diff) { |
|
|
|
|
|
|
97
|
4 |
|
$userIds = array_merge($userIds, $diff); |
|
98
|
4 |
|
} |
|
99
|
4 |
|
} |
|
100
|
|
|
|
|
101
|
4 |
|
$this->users = $this->getVerifiedUsers($userIds); |
|
102
|
4 |
|
$this->usersLoaded = true; |
|
103
|
4 |
|
return $this->users; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* check if a user is in the group |
|
108
|
|
|
* |
|
109
|
|
|
* @param \OC\User\User $user |
|
110
|
|
|
* @return bool |
|
111
|
|
|
*/ |
|
112
|
435 |
|
public function inGroup($user) { |
|
113
|
435 |
|
if (isset($this->users[$user->getUID()])) { |
|
114
|
4 |
|
return true; |
|
115
|
|
|
} |
|
116
|
435 |
|
foreach ($this->backends as $backend) { |
|
117
|
435 |
|
if ($backend->inGroup($user->getUID(), $this->gid)) { |
|
118
|
20 |
|
$this->users[$user->getUID()] = $user; |
|
119
|
20 |
|
return true; |
|
120
|
|
|
} |
|
121
|
432 |
|
} |
|
122
|
431 |
|
return false; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* add a user to the group |
|
127
|
|
|
* |
|
128
|
|
|
* @param \OC\User\User $user |
|
129
|
|
|
*/ |
|
130
|
420 |
|
public function addUser($user) { |
|
131
|
420 |
|
if ($this->inGroup($user)) { |
|
132
|
3 |
|
return; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
419 |
|
if ($this->emitter) { |
|
136
|
418 |
|
$this->emitter->emit('\OC\Group', 'preAddUser', array($this, $user)); |
|
137
|
418 |
|
} |
|
138
|
419 |
|
foreach ($this->backends as $backend) { |
|
139
|
419 |
|
if ($backend->implementsActions(\OC_Group_Backend::ADD_TO_GROUP)) { |
|
140
|
419 |
|
$backend->addToGroup($user->getUID(), $this->gid); |
|
141
|
419 |
|
if ($this->users) { |
|
|
|
|
|
|
142
|
|
|
$this->users[$user->getUID()] = $user; |
|
143
|
|
|
} |
|
144
|
419 |
|
if ($this->emitter) { |
|
145
|
418 |
|
$this->emitter->emit('\OC\Group', 'postAddUser', array($this, $user)); |
|
146
|
418 |
|
} |
|
147
|
419 |
|
return; |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* remove a user from the group |
|
154
|
|
|
* |
|
155
|
|
|
* @param \OC\User\User $user |
|
156
|
|
|
*/ |
|
157
|
410 |
|
public function removeUser($user) { |
|
158
|
410 |
|
$result = false; |
|
159
|
410 |
|
if ($this->emitter) { |
|
160
|
407 |
|
$this->emitter->emit('\OC\Group', 'preRemoveUser', array($this, $user)); |
|
161
|
407 |
|
} |
|
162
|
410 |
|
foreach ($this->backends as $backend) { |
|
163
|
410 |
|
if ($backend->implementsActions(\OC_Group_Backend::REMOVE_FROM_GOUP) and $backend->inGroup($user->getUID(), $this->gid)) { |
|
164
|
409 |
|
$backend->removeFromGroup($user->getUID(), $this->gid); |
|
165
|
409 |
|
$result = true; |
|
166
|
409 |
|
} |
|
167
|
410 |
|
} |
|
168
|
410 |
|
if ($result) { |
|
169
|
409 |
|
if ($this->emitter) { |
|
170
|
407 |
|
$this->emitter->emit('\OC\Group', 'postRemoveUser', array($this, $user)); |
|
171
|
407 |
|
} |
|
172
|
409 |
|
if ($this->users) { |
|
|
|
|
|
|
173
|
7 |
|
foreach ($this->users as $index => $groupUser) { |
|
174
|
7 |
|
if ($groupUser->getUID() === $user->getUID()) { |
|
175
|
7 |
|
unset($this->users[$index]); |
|
176
|
7 |
|
return; |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
403 |
|
} |
|
181
|
404 |
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* search for users in the group by userid |
|
185
|
|
|
* |
|
186
|
|
|
* @param string $search |
|
187
|
|
|
* @param int $limit |
|
188
|
|
|
* @param int $offset |
|
189
|
|
|
* @return \OC\User\User[] |
|
190
|
|
|
*/ |
|
191
|
36 |
View Code Duplication |
public function searchUsers($search, $limit = null, $offset = null) { |
|
192
|
36 |
|
$users = array(); |
|
193
|
36 |
|
foreach ($this->backends as $backend) { |
|
194
|
36 |
|
$userIds = $backend->usersInGroup($this->gid, $search, $limit, $offset); |
|
195
|
36 |
|
$users += $this->getVerifiedUsers($userIds); |
|
196
|
36 |
|
if (!is_null($limit) and $limit <= 0) { |
|
197
|
30 |
|
return array_values($users); |
|
198
|
|
|
} |
|
199
|
6 |
|
} |
|
200
|
6 |
|
return array_values($users); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* returns the number of users matching the search string |
|
205
|
|
|
* |
|
206
|
|
|
* @param string $search |
|
207
|
|
|
* @return int|bool |
|
208
|
|
|
*/ |
|
209
|
3 |
|
public function count($search = '') { |
|
210
|
3 |
|
$users = false; |
|
211
|
3 |
|
foreach ($this->backends as $backend) { |
|
212
|
3 |
|
if($backend->implementsActions(\OC_Group_Backend::COUNT_USERS)) { |
|
213
|
2 |
|
if($users === false) { |
|
214
|
|
|
//we could directly add to a bool variable, but this would |
|
215
|
|
|
//be ugly |
|
216
|
2 |
|
$users = 0; |
|
217
|
2 |
|
} |
|
218
|
2 |
|
$users += $backend->countUsersInGroup($this->gid, $search); |
|
|
|
|
|
|
219
|
2 |
|
} |
|
220
|
3 |
|
} |
|
221
|
3 |
|
return $users; |
|
|
|
|
|
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* search for users in the group by displayname |
|
226
|
|
|
* |
|
227
|
|
|
* @param string $search |
|
228
|
|
|
* @param int $limit |
|
229
|
|
|
* @param int $offset |
|
230
|
|
|
* @return \OC\User\User[] |
|
231
|
|
|
*/ |
|
232
|
|
View Code Duplication |
public function searchDisplayName($search, $limit = null, $offset = null) { |
|
233
|
|
|
$users = array(); |
|
234
|
|
|
foreach ($this->backends as $backend) { |
|
235
|
|
|
$userIds = $backend->usersInGroup($this->gid, $search, $limit, $offset); |
|
236
|
|
|
$users = $this->getVerifiedUsers($userIds); |
|
237
|
|
|
if (!is_null($limit) and $limit <= 0) { |
|
238
|
|
|
return array_values($users); |
|
239
|
|
|
} |
|
240
|
|
|
} |
|
241
|
|
|
return array_values($users); |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* delete the group |
|
246
|
|
|
* |
|
247
|
|
|
* @return bool |
|
248
|
|
|
*/ |
|
249
|
458 |
|
public function delete() { |
|
250
|
|
|
// Prevent users from deleting group admin |
|
251
|
458 |
|
if ($this->getGID() === 'admin') { |
|
252
|
87 |
|
return false; |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
397 |
|
$result = false; |
|
256
|
397 |
|
if ($this->emitter) { |
|
257
|
396 |
|
$this->emitter->emit('\OC\Group', 'preDelete', array($this)); |
|
258
|
396 |
|
} |
|
259
|
397 |
|
foreach ($this->backends as $backend) { |
|
260
|
397 |
|
if ($backend->implementsActions(\OC_Group_Backend::DELETE_GROUP)) { |
|
261
|
397 |
|
$result = true; |
|
262
|
397 |
|
$backend->deleteGroup($this->gid); |
|
263
|
397 |
|
} |
|
264
|
397 |
|
} |
|
265
|
397 |
|
if ($result and $this->emitter) { |
|
266
|
396 |
|
$this->emitter->emit('\OC\Group', 'postDelete', array($this)); |
|
267
|
396 |
|
} |
|
268
|
397 |
|
return $result; |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
|
|
/** |
|
272
|
|
|
* returns all the Users from an array that really exists |
|
273
|
|
|
* @param string[] $userIds an array containing user IDs |
|
274
|
|
|
* @return \OC\User\User[] an Array with the userId as Key and \OC\User\User as value |
|
275
|
|
|
*/ |
|
276
|
40 |
|
private function getVerifiedUsers($userIds) { |
|
277
|
40 |
|
if (!is_array($userIds)) { |
|
278
|
|
|
return array(); |
|
279
|
|
|
} |
|
280
|
40 |
|
$users = array(); |
|
281
|
40 |
|
foreach ($userIds as $userId) { |
|
282
|
40 |
|
$user = $this->userManager->get($userId); |
|
283
|
40 |
|
if (!is_null($user)) { |
|
284
|
40 |
|
$users[$userId] = $user; |
|
285
|
40 |
|
} |
|
286
|
40 |
|
} |
|
287
|
40 |
|
return $users; |
|
288
|
|
|
} |
|
289
|
|
|
} |
|
290
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.