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
290
291
292
|
<?php
/**
* @author Arthur Schiwon <blizzz@owncloud.com>
* @author Bart Visscher <bartv@thisnet.nl>
* @author Bernhard Posselt <dev@bernhard-posselt.com>
* @author Joas Schilling <nickvergessen@gmx.de>
* @author macjohnny <estebanmarin@gmx.ch>
* @author Morris Jobke <hey@morrisjobke.de>
* @author Robin Appelman <icewind@owncloud.com>
* @author Robin McCorkell <rmccorkell@karoshi.org.uk>
* @author voxsim <Simon Vocella>
*
* @copyright Copyright (c) 2015, ownCloud, Inc.
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OC\Group;
use OC\Hooks\PublicEmitter;
use OCP\IGroupManager;
/**
* Class Manager
*
* Hooks available in scope \OC\Group:
* - preAddUser(\OC\Group\Group $group, \OC\User\User $user)
* - postAddUser(\OC\Group\Group $group, \OC\User\User $user)
* - preRemoveUser(\OC\Group\Group $group, \OC\User\User $user)
* - postRemoveUser(\OC\Group\Group $group, \OC\User\User $user)
* - preDelete(\OC\Group\Group $group)
* - postDelete(\OC\Group\Group $group)
* - preCreate(string $groupId)
* - postCreate(\OC\Group\Group $group)
*
* @package OC\Group
*/
class Manager extends PublicEmitter implements IGroupManager {
/**
* @var \OC_Group_Backend[]|\OC_Group_Database[] $backends
*/
private $backends = array();
/**
* @var \OC\User\Manager $userManager
*/
private $userManager;
/**
* @var \OC\Group\Group[]
*/
private $cachedGroups = array();
/**
* @var \OC\Group\Group[]
*/
private $cachedUserGroups = array();
/**
* @param \OC\User\Manager $userManager
*/
public function __construct($userManager) {
$this->userManager = $userManager;
$cachedGroups = & $this->cachedGroups;
$cachedUserGroups = & $this->cachedUserGroups;
$this->listen('\OC\Group', 'postDelete', function ($group) use (&$cachedGroups, &$cachedUserGroups) {
/**
* @var \OC\Group\Group $group
*/
unset($cachedGroups[$group->getGID()]);
$cachedUserGroups = array();
});
$this->listen('\OC\Group', 'postAddUser', function ($group) use (&$cachedUserGroups) {
/**
* @var \OC\Group\Group $group
*/
$cachedUserGroups = array();
});
$this->listen('\OC\Group', 'postRemoveUser', function ($group) use (&$cachedUserGroups) {
/**
* @var \OC\Group\Group $group
*/
$cachedUserGroups = array();
});
}
/**
* @param \OC_Group_Backend $backend
*/
public function addBackend($backend) {
$this->backends[] = $backend;
}
public function clearBackends() {
$this->backends = array();
$this->cachedGroups = array();
}
/**
* @param string $gid
* @return \OC\Group\Group
*/
public function get($gid) {
if (isset($this->cachedGroups[$gid])) {
return $this->cachedGroups[$gid];
}
return $this->getGroupObject($gid);
}
protected function getGroupObject($gid) {
$backends = array();
foreach ($this->backends as $backend) {
if ($backend->groupExists($gid)) {
$backends[] = $backend;
}
}
if (count($backends) === 0) {
return null;
}
$this->cachedGroups[$gid] = new Group($gid, $backends, $this->userManager, $this);
return $this->cachedGroups[$gid];
}
/**
* @param string $gid
* @return bool
*/
public function groupExists($gid) {
return !is_null($this->get($gid));
}
/**
* @param string $gid
* @return \OC\Group\Group
*/
public function createGroup($gid) {
if ($gid === '' || is_null($gid)) {
return false;
} else if ($group = $this->get($gid)) {
return $group;
} else {
$this->emit('\OC\Group', 'preCreate', array($gid));
foreach ($this->backends as $backend) {
if ($backend->implementsActions(\OC_Group_Backend::CREATE_GROUP)) {
$backend->createGroup($gid);
$group = $this->getGroupObject($gid);
$this->emit('\OC\Group', 'postCreate', array($group));
return $group;
}
}
return null;
}
}
/**
* @param string $search
* @param int $limit
* @param int $offset
* @return \OC\Group\Group[]
*/
public function search($search, $limit = null, $offset = null) {
$groups = array();
foreach ($this->backends as $backend) {
$groupIds = $backend->getGroups($search, $limit, $offset);
foreach ($groupIds as $groupId) {
$groups[$groupId] = $this->get($groupId);
}
if (!is_null($limit) and $limit <= 0) {
return array_values($groups);
}
}
return array_values($groups);
}
/**
* @param \OC\User\User $user
* @return \OC\Group\Group[]
*/
public function getUserGroups($user) {
return $this->getUserIdGroups($user->getUID());
}
/**
* @param string $uid the user id
* @return \OC\Group\Group[]
*/
public function getUserIdGroups($uid) {
if (isset($this->cachedUserGroups[$uid])) {
return $this->cachedUserGroups[$uid];
}
$groups = array();
foreach ($this->backends as $backend) {
$groupIds = $backend->getUserGroups($uid);
foreach ($groupIds as $groupId) {
$groups[$groupId] = $this->get($groupId);
}
}
$this->cachedUserGroups[$uid] = $groups;
return $this->cachedUserGroups[$uid];
}
/**
* Checks if a userId is in the admin group
* @param string $userId
* @return bool if admin
*/
public function isAdmin($userId) {
return $this->isInGroup($userId, 'admin');
}
/**
* Checks if a userId is in a group
* @param string $userId
* @param group $group
* @return bool if in group
*/
public function isInGroup($userId, $group) {
return array_key_exists($group, $this->getUserIdGroups($userId));
}
/**
* get a list of group ids for a user
* @param \OC\User\User $user
* @return array with group ids
*/
public function getUserGroupIds($user) {
return array_keys($this->getUserGroups($user));
}
/**
* get a list of all display names in a group
* @param string $gid
* @param string $search
* @param int $limit
* @param int $offset
* @return array an array of display names (value) and user ids (key)
*/
public function displayNamesInGroup($gid, $search = '', $limit = -1, $offset = 0) {
$group = $this->get($gid);
if(is_null($group)) {
return array();
}
$search = trim($search);
$groupUsers = array();
if(!empty($search)) {
// only user backends have the capability to do a complex search for users
$searchOffset = 0;
$searchLimit = $limit * 100;
if($limit === -1) {
$searchLimit = 500;
}
do {
$filteredUsers = $this->userManager->search($search, $searchLimit, $searchOffset);
foreach($filteredUsers as $filteredUser) {
if($group->inGroup($filteredUser)) {
$groupUsers[]= $filteredUser;
}
}
$searchOffset += $searchLimit;
} while(count($groupUsers) < $searchLimit+$offset && count($filteredUsers) >= $searchLimit);
if($limit === -1) {
$groupUsers = array_slice($groupUsers, $offset);
} else {
$groupUsers = array_slice($groupUsers, $offset, $limit);
}
} else {
$groupUsers = $group->searchUsers('', $limit, $offset);
}
$matchingUsers = array();
foreach($groupUsers as $groupUser) {
$matchingUsers[$groupUser->getUID()] = $groupUser->getDisplayName();
}
return $matchingUsers;
}
}
|