@@ -62,346 +62,346 @@ |
||
62 | 62 | * @package OC\Group |
63 | 63 | */ |
64 | 64 | class Manager extends PublicEmitter implements IGroupManager { |
65 | - /** @var GroupInterface[] */ |
|
66 | - private $backends = []; |
|
67 | - |
|
68 | - /** @var \OC\User\Manager */ |
|
69 | - private $userManager; |
|
70 | - /** @var EventDispatcherInterface */ |
|
71 | - private $dispatcher; |
|
72 | - /** @var ILogger */ |
|
73 | - private $logger; |
|
74 | - |
|
75 | - /** @var \OC\Group\Group[] */ |
|
76 | - private $cachedGroups = []; |
|
77 | - |
|
78 | - /** @var \OC\Group\Group[] */ |
|
79 | - private $cachedUserGroups = []; |
|
80 | - |
|
81 | - /** @var \OC\SubAdmin */ |
|
82 | - private $subAdmin = null; |
|
83 | - |
|
84 | - /** |
|
85 | - * @param \OC\User\Manager $userManager |
|
86 | - * @param EventDispatcherInterface $dispatcher |
|
87 | - * @param ILogger $logger |
|
88 | - */ |
|
89 | - public function __construct(\OC\User\Manager $userManager, |
|
90 | - EventDispatcherInterface $dispatcher, |
|
91 | - ILogger $logger) { |
|
92 | - $this->userManager = $userManager; |
|
93 | - $this->dispatcher = $dispatcher; |
|
94 | - $this->logger = $logger; |
|
95 | - |
|
96 | - $cachedGroups = & $this->cachedGroups; |
|
97 | - $cachedUserGroups = & $this->cachedUserGroups; |
|
98 | - $this->listen('\OC\Group', 'postDelete', function ($group) use (&$cachedGroups, &$cachedUserGroups) { |
|
99 | - /** |
|
100 | - * @var \OC\Group\Group $group |
|
101 | - */ |
|
102 | - unset($cachedGroups[$group->getGID()]); |
|
103 | - $cachedUserGroups = []; |
|
104 | - }); |
|
105 | - $this->listen('\OC\Group', 'postAddUser', function ($group) use (&$cachedUserGroups) { |
|
106 | - /** |
|
107 | - * @var \OC\Group\Group $group |
|
108 | - */ |
|
109 | - $cachedUserGroups = []; |
|
110 | - }); |
|
111 | - $this->listen('\OC\Group', 'postRemoveUser', function ($group) use (&$cachedUserGroups) { |
|
112 | - /** |
|
113 | - * @var \OC\Group\Group $group |
|
114 | - */ |
|
115 | - $cachedUserGroups = []; |
|
116 | - }); |
|
117 | - } |
|
118 | - |
|
119 | - /** |
|
120 | - * Checks whether a given backend is used |
|
121 | - * |
|
122 | - * @param string $backendClass Full classname including complete namespace |
|
123 | - * @return bool |
|
124 | - */ |
|
125 | - public function isBackendUsed($backendClass) { |
|
126 | - $backendClass = strtolower(ltrim($backendClass, '\\')); |
|
127 | - |
|
128 | - foreach ($this->backends as $backend) { |
|
129 | - if (strtolower(get_class($backend)) === $backendClass) { |
|
130 | - return true; |
|
131 | - } |
|
132 | - } |
|
133 | - |
|
134 | - return false; |
|
135 | - } |
|
136 | - |
|
137 | - /** |
|
138 | - * @param \OCP\GroupInterface $backend |
|
139 | - */ |
|
140 | - public function addBackend($backend) { |
|
141 | - $this->backends[] = $backend; |
|
142 | - $this->clearCaches(); |
|
143 | - } |
|
144 | - |
|
145 | - public function clearBackends() { |
|
146 | - $this->backends = []; |
|
147 | - $this->clearCaches(); |
|
148 | - } |
|
149 | - |
|
150 | - /** |
|
151 | - * Get the active backends |
|
152 | - * @return \OCP\GroupInterface[] |
|
153 | - */ |
|
154 | - public function getBackends() { |
|
155 | - return $this->backends; |
|
156 | - } |
|
157 | - |
|
158 | - |
|
159 | - protected function clearCaches() { |
|
160 | - $this->cachedGroups = []; |
|
161 | - $this->cachedUserGroups = []; |
|
162 | - } |
|
163 | - |
|
164 | - /** |
|
165 | - * @param string $gid |
|
166 | - * @return \OC\Group\Group |
|
167 | - */ |
|
168 | - public function get($gid) { |
|
169 | - if (isset($this->cachedGroups[$gid])) { |
|
170 | - return $this->cachedGroups[$gid]; |
|
171 | - } |
|
172 | - return $this->getGroupObject($gid); |
|
173 | - } |
|
174 | - |
|
175 | - /** |
|
176 | - * @param string $gid |
|
177 | - * @param string $displayName |
|
178 | - * @return \OCP\IGroup |
|
179 | - */ |
|
180 | - protected function getGroupObject($gid, $displayName = null) { |
|
181 | - $backends = []; |
|
182 | - foreach ($this->backends as $backend) { |
|
183 | - if ($backend->implementsActions(\OC\Group\Backend::GROUP_DETAILS)) { |
|
184 | - $groupData = $backend->getGroupDetails($gid); |
|
185 | - if (is_array($groupData) && !empty($groupData)) { |
|
186 | - // take the display name from the first backend that has a non-null one |
|
187 | - if (is_null($displayName) && isset($groupData['displayName'])) { |
|
188 | - $displayName = $groupData['displayName']; |
|
189 | - } |
|
190 | - $backends[] = $backend; |
|
191 | - } |
|
192 | - } else if ($backend->groupExists($gid)) { |
|
193 | - $backends[] = $backend; |
|
194 | - } |
|
195 | - } |
|
196 | - if (count($backends) === 0) { |
|
197 | - return null; |
|
198 | - } |
|
199 | - $this->cachedGroups[$gid] = new Group($gid, $backends, $this->dispatcher, $this->userManager, $this, $displayName); |
|
200 | - return $this->cachedGroups[$gid]; |
|
201 | - } |
|
202 | - |
|
203 | - /** |
|
204 | - * @param string $gid |
|
205 | - * @return bool |
|
206 | - */ |
|
207 | - public function groupExists($gid) { |
|
208 | - return $this->get($gid) instanceof IGroup; |
|
209 | - } |
|
210 | - |
|
211 | - /** |
|
212 | - * @param string $gid |
|
213 | - * @return \OC\Group\Group |
|
214 | - */ |
|
215 | - public function createGroup($gid) { |
|
216 | - if ($gid === '' || $gid === null) { |
|
217 | - return false; |
|
218 | - } else if ($group = $this->get($gid)) { |
|
219 | - return $group; |
|
220 | - } else { |
|
221 | - $this->emit('\OC\Group', 'preCreate', array($gid)); |
|
222 | - foreach ($this->backends as $backend) { |
|
223 | - if ($backend->implementsActions(\OC\Group\Backend::CREATE_GROUP)) { |
|
224 | - $backend->createGroup($gid); |
|
225 | - $group = $this->getGroupObject($gid); |
|
226 | - $this->emit('\OC\Group', 'postCreate', array($group)); |
|
227 | - return $group; |
|
228 | - } |
|
229 | - } |
|
230 | - return null; |
|
231 | - } |
|
232 | - } |
|
233 | - |
|
234 | - /** |
|
235 | - * @param string $search |
|
236 | - * @param int $limit |
|
237 | - * @param int $offset |
|
238 | - * @return \OC\Group\Group[] |
|
239 | - */ |
|
240 | - public function search($search, $limit = null, $offset = null) { |
|
241 | - $groups = []; |
|
242 | - foreach ($this->backends as $backend) { |
|
243 | - $groupIds = $backend->getGroups($search, $limit, $offset); |
|
244 | - foreach ($groupIds as $groupId) { |
|
245 | - $aGroup = $this->get($groupId); |
|
246 | - if ($aGroup instanceof IGroup) { |
|
247 | - $groups[$groupId] = $aGroup; |
|
248 | - } else { |
|
249 | - $this->logger->debug('Group "' . $groupId . '" was returned by search but not found through direct access', ['app' => 'core']); |
|
250 | - } |
|
251 | - } |
|
252 | - if (!is_null($limit) and $limit <= 0) { |
|
253 | - return array_values($groups); |
|
254 | - } |
|
255 | - } |
|
256 | - return array_values($groups); |
|
257 | - } |
|
258 | - |
|
259 | - /** |
|
260 | - * @param IUser|null $user |
|
261 | - * @return \OC\Group\Group[] |
|
262 | - */ |
|
263 | - public function getUserGroups(IUser $user= null) { |
|
264 | - if (!$user instanceof IUser) { |
|
265 | - return []; |
|
266 | - } |
|
267 | - return $this->getUserIdGroups($user->getUID()); |
|
268 | - } |
|
269 | - |
|
270 | - /** |
|
271 | - * @param string $uid the user id |
|
272 | - * @return \OC\Group\Group[] |
|
273 | - */ |
|
274 | - public function getUserIdGroups($uid) { |
|
275 | - if (isset($this->cachedUserGroups[$uid])) { |
|
276 | - return $this->cachedUserGroups[$uid]; |
|
277 | - } |
|
278 | - $groups = []; |
|
279 | - foreach ($this->backends as $backend) { |
|
280 | - $groupIds = $backend->getUserGroups($uid); |
|
281 | - if (is_array($groupIds)) { |
|
282 | - foreach ($groupIds as $groupId) { |
|
283 | - $aGroup = $this->get($groupId); |
|
284 | - if ($aGroup instanceof IGroup) { |
|
285 | - $groups[$groupId] = $aGroup; |
|
286 | - } else { |
|
287 | - $this->logger->debug('User "' . $uid . '" belongs to deleted group: "' . $groupId . '"', ['app' => 'core']); |
|
288 | - } |
|
289 | - } |
|
290 | - } |
|
291 | - } |
|
292 | - $this->cachedUserGroups[$uid] = $groups; |
|
293 | - return $this->cachedUserGroups[$uid]; |
|
294 | - } |
|
295 | - |
|
296 | - /** |
|
297 | - * Checks if a userId is in the admin group |
|
298 | - * @param string $userId |
|
299 | - * @return bool if admin |
|
300 | - */ |
|
301 | - public function isAdmin($userId) { |
|
302 | - foreach ($this->backends as $backend) { |
|
303 | - if ($backend->implementsActions(\OC\Group\Backend::IS_ADMIN) && $backend->isAdmin($userId)) { |
|
304 | - return true; |
|
305 | - } |
|
306 | - } |
|
307 | - return $this->isInGroup($userId, 'admin'); |
|
308 | - } |
|
309 | - |
|
310 | - /** |
|
311 | - * Checks if a userId is in a group |
|
312 | - * @param string $userId |
|
313 | - * @param string $group |
|
314 | - * @return bool if in group |
|
315 | - */ |
|
316 | - public function isInGroup($userId, $group) { |
|
317 | - return array_key_exists($group, $this->getUserIdGroups($userId)); |
|
318 | - } |
|
319 | - |
|
320 | - /** |
|
321 | - * get a list of group ids for a user |
|
322 | - * @param IUser $user |
|
323 | - * @return array with group ids |
|
324 | - */ |
|
325 | - public function getUserGroupIds(IUser $user) { |
|
326 | - return array_map(function($value) { |
|
327 | - return (string) $value; |
|
328 | - }, array_keys($this->getUserGroups($user))); |
|
329 | - } |
|
330 | - |
|
331 | - /** |
|
332 | - * get an array of groupid and displayName for a user |
|
333 | - * @param IUser $user |
|
334 | - * @return array ['displayName' => displayname] |
|
335 | - */ |
|
336 | - public function getUserGroupNames(IUser $user) { |
|
337 | - return array_map(function($group) { |
|
338 | - return array('displayName' => $group->getDisplayName()); |
|
339 | - }, $this->getUserGroups($user)); |
|
340 | - } |
|
341 | - |
|
342 | - /** |
|
343 | - * get a list of all display names in a group |
|
344 | - * @param string $gid |
|
345 | - * @param string $search |
|
346 | - * @param int $limit |
|
347 | - * @param int $offset |
|
348 | - * @return array an array of display names (value) and user ids (key) |
|
349 | - */ |
|
350 | - public function displayNamesInGroup($gid, $search = '', $limit = -1, $offset = 0) { |
|
351 | - $group = $this->get($gid); |
|
352 | - if(is_null($group)) { |
|
353 | - return []; |
|
354 | - } |
|
355 | - |
|
356 | - $search = trim($search); |
|
357 | - $groupUsers = []; |
|
358 | - |
|
359 | - if(!empty($search)) { |
|
360 | - // only user backends have the capability to do a complex search for users |
|
361 | - $searchOffset = 0; |
|
362 | - $searchLimit = $limit * 100; |
|
363 | - if($limit === -1) { |
|
364 | - $searchLimit = 500; |
|
365 | - } |
|
366 | - |
|
367 | - do { |
|
368 | - $filteredUsers = $this->userManager->searchDisplayName($search, $searchLimit, $searchOffset); |
|
369 | - foreach($filteredUsers as $filteredUser) { |
|
370 | - if($group->inGroup($filteredUser)) { |
|
371 | - $groupUsers[]= $filteredUser; |
|
372 | - } |
|
373 | - } |
|
374 | - $searchOffset += $searchLimit; |
|
375 | - } while(count($groupUsers) < $searchLimit+$offset && count($filteredUsers) >= $searchLimit); |
|
376 | - |
|
377 | - if($limit === -1) { |
|
378 | - $groupUsers = array_slice($groupUsers, $offset); |
|
379 | - } else { |
|
380 | - $groupUsers = array_slice($groupUsers, $offset, $limit); |
|
381 | - } |
|
382 | - } else { |
|
383 | - $groupUsers = $group->searchUsers('', $limit, $offset); |
|
384 | - } |
|
385 | - |
|
386 | - $matchingUsers = []; |
|
387 | - foreach($groupUsers as $groupUser) { |
|
388 | - $matchingUsers[$groupUser->getUID()] = $groupUser->getDisplayName(); |
|
389 | - } |
|
390 | - return $matchingUsers; |
|
391 | - } |
|
392 | - |
|
393 | - /** |
|
394 | - * @return \OC\SubAdmin |
|
395 | - */ |
|
396 | - public function getSubAdmin() { |
|
397 | - if (!$this->subAdmin) { |
|
398 | - $this->subAdmin = new \OC\SubAdmin( |
|
399 | - $this->userManager, |
|
400 | - $this, |
|
401 | - \OC::$server->getDatabaseConnection() |
|
402 | - ); |
|
403 | - } |
|
404 | - |
|
405 | - return $this->subAdmin; |
|
406 | - } |
|
65 | + /** @var GroupInterface[] */ |
|
66 | + private $backends = []; |
|
67 | + |
|
68 | + /** @var \OC\User\Manager */ |
|
69 | + private $userManager; |
|
70 | + /** @var EventDispatcherInterface */ |
|
71 | + private $dispatcher; |
|
72 | + /** @var ILogger */ |
|
73 | + private $logger; |
|
74 | + |
|
75 | + /** @var \OC\Group\Group[] */ |
|
76 | + private $cachedGroups = []; |
|
77 | + |
|
78 | + /** @var \OC\Group\Group[] */ |
|
79 | + private $cachedUserGroups = []; |
|
80 | + |
|
81 | + /** @var \OC\SubAdmin */ |
|
82 | + private $subAdmin = null; |
|
83 | + |
|
84 | + /** |
|
85 | + * @param \OC\User\Manager $userManager |
|
86 | + * @param EventDispatcherInterface $dispatcher |
|
87 | + * @param ILogger $logger |
|
88 | + */ |
|
89 | + public function __construct(\OC\User\Manager $userManager, |
|
90 | + EventDispatcherInterface $dispatcher, |
|
91 | + ILogger $logger) { |
|
92 | + $this->userManager = $userManager; |
|
93 | + $this->dispatcher = $dispatcher; |
|
94 | + $this->logger = $logger; |
|
95 | + |
|
96 | + $cachedGroups = & $this->cachedGroups; |
|
97 | + $cachedUserGroups = & $this->cachedUserGroups; |
|
98 | + $this->listen('\OC\Group', 'postDelete', function ($group) use (&$cachedGroups, &$cachedUserGroups) { |
|
99 | + /** |
|
100 | + * @var \OC\Group\Group $group |
|
101 | + */ |
|
102 | + unset($cachedGroups[$group->getGID()]); |
|
103 | + $cachedUserGroups = []; |
|
104 | + }); |
|
105 | + $this->listen('\OC\Group', 'postAddUser', function ($group) use (&$cachedUserGroups) { |
|
106 | + /** |
|
107 | + * @var \OC\Group\Group $group |
|
108 | + */ |
|
109 | + $cachedUserGroups = []; |
|
110 | + }); |
|
111 | + $this->listen('\OC\Group', 'postRemoveUser', function ($group) use (&$cachedUserGroups) { |
|
112 | + /** |
|
113 | + * @var \OC\Group\Group $group |
|
114 | + */ |
|
115 | + $cachedUserGroups = []; |
|
116 | + }); |
|
117 | + } |
|
118 | + |
|
119 | + /** |
|
120 | + * Checks whether a given backend is used |
|
121 | + * |
|
122 | + * @param string $backendClass Full classname including complete namespace |
|
123 | + * @return bool |
|
124 | + */ |
|
125 | + public function isBackendUsed($backendClass) { |
|
126 | + $backendClass = strtolower(ltrim($backendClass, '\\')); |
|
127 | + |
|
128 | + foreach ($this->backends as $backend) { |
|
129 | + if (strtolower(get_class($backend)) === $backendClass) { |
|
130 | + return true; |
|
131 | + } |
|
132 | + } |
|
133 | + |
|
134 | + return false; |
|
135 | + } |
|
136 | + |
|
137 | + /** |
|
138 | + * @param \OCP\GroupInterface $backend |
|
139 | + */ |
|
140 | + public function addBackend($backend) { |
|
141 | + $this->backends[] = $backend; |
|
142 | + $this->clearCaches(); |
|
143 | + } |
|
144 | + |
|
145 | + public function clearBackends() { |
|
146 | + $this->backends = []; |
|
147 | + $this->clearCaches(); |
|
148 | + } |
|
149 | + |
|
150 | + /** |
|
151 | + * Get the active backends |
|
152 | + * @return \OCP\GroupInterface[] |
|
153 | + */ |
|
154 | + public function getBackends() { |
|
155 | + return $this->backends; |
|
156 | + } |
|
157 | + |
|
158 | + |
|
159 | + protected function clearCaches() { |
|
160 | + $this->cachedGroups = []; |
|
161 | + $this->cachedUserGroups = []; |
|
162 | + } |
|
163 | + |
|
164 | + /** |
|
165 | + * @param string $gid |
|
166 | + * @return \OC\Group\Group |
|
167 | + */ |
|
168 | + public function get($gid) { |
|
169 | + if (isset($this->cachedGroups[$gid])) { |
|
170 | + return $this->cachedGroups[$gid]; |
|
171 | + } |
|
172 | + return $this->getGroupObject($gid); |
|
173 | + } |
|
174 | + |
|
175 | + /** |
|
176 | + * @param string $gid |
|
177 | + * @param string $displayName |
|
178 | + * @return \OCP\IGroup |
|
179 | + */ |
|
180 | + protected function getGroupObject($gid, $displayName = null) { |
|
181 | + $backends = []; |
|
182 | + foreach ($this->backends as $backend) { |
|
183 | + if ($backend->implementsActions(\OC\Group\Backend::GROUP_DETAILS)) { |
|
184 | + $groupData = $backend->getGroupDetails($gid); |
|
185 | + if (is_array($groupData) && !empty($groupData)) { |
|
186 | + // take the display name from the first backend that has a non-null one |
|
187 | + if (is_null($displayName) && isset($groupData['displayName'])) { |
|
188 | + $displayName = $groupData['displayName']; |
|
189 | + } |
|
190 | + $backends[] = $backend; |
|
191 | + } |
|
192 | + } else if ($backend->groupExists($gid)) { |
|
193 | + $backends[] = $backend; |
|
194 | + } |
|
195 | + } |
|
196 | + if (count($backends) === 0) { |
|
197 | + return null; |
|
198 | + } |
|
199 | + $this->cachedGroups[$gid] = new Group($gid, $backends, $this->dispatcher, $this->userManager, $this, $displayName); |
|
200 | + return $this->cachedGroups[$gid]; |
|
201 | + } |
|
202 | + |
|
203 | + /** |
|
204 | + * @param string $gid |
|
205 | + * @return bool |
|
206 | + */ |
|
207 | + public function groupExists($gid) { |
|
208 | + return $this->get($gid) instanceof IGroup; |
|
209 | + } |
|
210 | + |
|
211 | + /** |
|
212 | + * @param string $gid |
|
213 | + * @return \OC\Group\Group |
|
214 | + */ |
|
215 | + public function createGroup($gid) { |
|
216 | + if ($gid === '' || $gid === null) { |
|
217 | + return false; |
|
218 | + } else if ($group = $this->get($gid)) { |
|
219 | + return $group; |
|
220 | + } else { |
|
221 | + $this->emit('\OC\Group', 'preCreate', array($gid)); |
|
222 | + foreach ($this->backends as $backend) { |
|
223 | + if ($backend->implementsActions(\OC\Group\Backend::CREATE_GROUP)) { |
|
224 | + $backend->createGroup($gid); |
|
225 | + $group = $this->getGroupObject($gid); |
|
226 | + $this->emit('\OC\Group', 'postCreate', array($group)); |
|
227 | + return $group; |
|
228 | + } |
|
229 | + } |
|
230 | + return null; |
|
231 | + } |
|
232 | + } |
|
233 | + |
|
234 | + /** |
|
235 | + * @param string $search |
|
236 | + * @param int $limit |
|
237 | + * @param int $offset |
|
238 | + * @return \OC\Group\Group[] |
|
239 | + */ |
|
240 | + public function search($search, $limit = null, $offset = null) { |
|
241 | + $groups = []; |
|
242 | + foreach ($this->backends as $backend) { |
|
243 | + $groupIds = $backend->getGroups($search, $limit, $offset); |
|
244 | + foreach ($groupIds as $groupId) { |
|
245 | + $aGroup = $this->get($groupId); |
|
246 | + if ($aGroup instanceof IGroup) { |
|
247 | + $groups[$groupId] = $aGroup; |
|
248 | + } else { |
|
249 | + $this->logger->debug('Group "' . $groupId . '" was returned by search but not found through direct access', ['app' => 'core']); |
|
250 | + } |
|
251 | + } |
|
252 | + if (!is_null($limit) and $limit <= 0) { |
|
253 | + return array_values($groups); |
|
254 | + } |
|
255 | + } |
|
256 | + return array_values($groups); |
|
257 | + } |
|
258 | + |
|
259 | + /** |
|
260 | + * @param IUser|null $user |
|
261 | + * @return \OC\Group\Group[] |
|
262 | + */ |
|
263 | + public function getUserGroups(IUser $user= null) { |
|
264 | + if (!$user instanceof IUser) { |
|
265 | + return []; |
|
266 | + } |
|
267 | + return $this->getUserIdGroups($user->getUID()); |
|
268 | + } |
|
269 | + |
|
270 | + /** |
|
271 | + * @param string $uid the user id |
|
272 | + * @return \OC\Group\Group[] |
|
273 | + */ |
|
274 | + public function getUserIdGroups($uid) { |
|
275 | + if (isset($this->cachedUserGroups[$uid])) { |
|
276 | + return $this->cachedUserGroups[$uid]; |
|
277 | + } |
|
278 | + $groups = []; |
|
279 | + foreach ($this->backends as $backend) { |
|
280 | + $groupIds = $backend->getUserGroups($uid); |
|
281 | + if (is_array($groupIds)) { |
|
282 | + foreach ($groupIds as $groupId) { |
|
283 | + $aGroup = $this->get($groupId); |
|
284 | + if ($aGroup instanceof IGroup) { |
|
285 | + $groups[$groupId] = $aGroup; |
|
286 | + } else { |
|
287 | + $this->logger->debug('User "' . $uid . '" belongs to deleted group: "' . $groupId . '"', ['app' => 'core']); |
|
288 | + } |
|
289 | + } |
|
290 | + } |
|
291 | + } |
|
292 | + $this->cachedUserGroups[$uid] = $groups; |
|
293 | + return $this->cachedUserGroups[$uid]; |
|
294 | + } |
|
295 | + |
|
296 | + /** |
|
297 | + * Checks if a userId is in the admin group |
|
298 | + * @param string $userId |
|
299 | + * @return bool if admin |
|
300 | + */ |
|
301 | + public function isAdmin($userId) { |
|
302 | + foreach ($this->backends as $backend) { |
|
303 | + if ($backend->implementsActions(\OC\Group\Backend::IS_ADMIN) && $backend->isAdmin($userId)) { |
|
304 | + return true; |
|
305 | + } |
|
306 | + } |
|
307 | + return $this->isInGroup($userId, 'admin'); |
|
308 | + } |
|
309 | + |
|
310 | + /** |
|
311 | + * Checks if a userId is in a group |
|
312 | + * @param string $userId |
|
313 | + * @param string $group |
|
314 | + * @return bool if in group |
|
315 | + */ |
|
316 | + public function isInGroup($userId, $group) { |
|
317 | + return array_key_exists($group, $this->getUserIdGroups($userId)); |
|
318 | + } |
|
319 | + |
|
320 | + /** |
|
321 | + * get a list of group ids for a user |
|
322 | + * @param IUser $user |
|
323 | + * @return array with group ids |
|
324 | + */ |
|
325 | + public function getUserGroupIds(IUser $user) { |
|
326 | + return array_map(function($value) { |
|
327 | + return (string) $value; |
|
328 | + }, array_keys($this->getUserGroups($user))); |
|
329 | + } |
|
330 | + |
|
331 | + /** |
|
332 | + * get an array of groupid and displayName for a user |
|
333 | + * @param IUser $user |
|
334 | + * @return array ['displayName' => displayname] |
|
335 | + */ |
|
336 | + public function getUserGroupNames(IUser $user) { |
|
337 | + return array_map(function($group) { |
|
338 | + return array('displayName' => $group->getDisplayName()); |
|
339 | + }, $this->getUserGroups($user)); |
|
340 | + } |
|
341 | + |
|
342 | + /** |
|
343 | + * get a list of all display names in a group |
|
344 | + * @param string $gid |
|
345 | + * @param string $search |
|
346 | + * @param int $limit |
|
347 | + * @param int $offset |
|
348 | + * @return array an array of display names (value) and user ids (key) |
|
349 | + */ |
|
350 | + public function displayNamesInGroup($gid, $search = '', $limit = -1, $offset = 0) { |
|
351 | + $group = $this->get($gid); |
|
352 | + if(is_null($group)) { |
|
353 | + return []; |
|
354 | + } |
|
355 | + |
|
356 | + $search = trim($search); |
|
357 | + $groupUsers = []; |
|
358 | + |
|
359 | + if(!empty($search)) { |
|
360 | + // only user backends have the capability to do a complex search for users |
|
361 | + $searchOffset = 0; |
|
362 | + $searchLimit = $limit * 100; |
|
363 | + if($limit === -1) { |
|
364 | + $searchLimit = 500; |
|
365 | + } |
|
366 | + |
|
367 | + do { |
|
368 | + $filteredUsers = $this->userManager->searchDisplayName($search, $searchLimit, $searchOffset); |
|
369 | + foreach($filteredUsers as $filteredUser) { |
|
370 | + if($group->inGroup($filteredUser)) { |
|
371 | + $groupUsers[]= $filteredUser; |
|
372 | + } |
|
373 | + } |
|
374 | + $searchOffset += $searchLimit; |
|
375 | + } while(count($groupUsers) < $searchLimit+$offset && count($filteredUsers) >= $searchLimit); |
|
376 | + |
|
377 | + if($limit === -1) { |
|
378 | + $groupUsers = array_slice($groupUsers, $offset); |
|
379 | + } else { |
|
380 | + $groupUsers = array_slice($groupUsers, $offset, $limit); |
|
381 | + } |
|
382 | + } else { |
|
383 | + $groupUsers = $group->searchUsers('', $limit, $offset); |
|
384 | + } |
|
385 | + |
|
386 | + $matchingUsers = []; |
|
387 | + foreach($groupUsers as $groupUser) { |
|
388 | + $matchingUsers[$groupUser->getUID()] = $groupUser->getDisplayName(); |
|
389 | + } |
|
390 | + return $matchingUsers; |
|
391 | + } |
|
392 | + |
|
393 | + /** |
|
394 | + * @return \OC\SubAdmin |
|
395 | + */ |
|
396 | + public function getSubAdmin() { |
|
397 | + if (!$this->subAdmin) { |
|
398 | + $this->subAdmin = new \OC\SubAdmin( |
|
399 | + $this->userManager, |
|
400 | + $this, |
|
401 | + \OC::$server->getDatabaseConnection() |
|
402 | + ); |
|
403 | + } |
|
404 | + |
|
405 | + return $this->subAdmin; |
|
406 | + } |
|
407 | 407 | } |
@@ -41,338 +41,338 @@ |
||
41 | 41 | use Symfony\Component\EventDispatcher\GenericEvent; |
42 | 42 | |
43 | 43 | class Group implements IGroup { |
44 | - /** @var null|string */ |
|
45 | - protected $displayName; |
|
44 | + /** @var null|string */ |
|
45 | + protected $displayName; |
|
46 | 46 | |
47 | - /** @var string */ |
|
48 | - private $gid; |
|
47 | + /** @var string */ |
|
48 | + private $gid; |
|
49 | 49 | |
50 | - /** @var \OC\User\User[] */ |
|
51 | - private $users = array(); |
|
50 | + /** @var \OC\User\User[] */ |
|
51 | + private $users = array(); |
|
52 | 52 | |
53 | - /** @var bool */ |
|
54 | - private $usersLoaded; |
|
53 | + /** @var bool */ |
|
54 | + private $usersLoaded; |
|
55 | 55 | |
56 | - /** @var Backend[] */ |
|
57 | - private $backends; |
|
58 | - /** @var EventDispatcherInterface */ |
|
59 | - private $dispatcher; |
|
60 | - /** @var \OC\User\Manager|IUserManager */ |
|
61 | - private $userManager; |
|
62 | - /** @var PublicEmitter */ |
|
63 | - private $emitter; |
|
56 | + /** @var Backend[] */ |
|
57 | + private $backends; |
|
58 | + /** @var EventDispatcherInterface */ |
|
59 | + private $dispatcher; |
|
60 | + /** @var \OC\User\Manager|IUserManager */ |
|
61 | + private $userManager; |
|
62 | + /** @var PublicEmitter */ |
|
63 | + private $emitter; |
|
64 | 64 | |
65 | 65 | |
66 | - /** |
|
67 | - * @param string $gid |
|
68 | - * @param Backend[] $backends |
|
69 | - * @param EventDispatcherInterface $dispatcher |
|
70 | - * @param IUserManager $userManager |
|
71 | - * @param PublicEmitter $emitter |
|
72 | - * @param string $displayName |
|
73 | - */ |
|
74 | - public function __construct(string $gid, array $backends, EventDispatcherInterface $dispatcher, IUserManager $userManager, PublicEmitter $emitter = null, ?string $displayName = null) { |
|
75 | - $this->gid = $gid; |
|
76 | - $this->backends = $backends; |
|
77 | - $this->dispatcher = $dispatcher; |
|
78 | - $this->userManager = $userManager; |
|
79 | - $this->emitter = $emitter; |
|
80 | - $this->displayName = $displayName; |
|
81 | - } |
|
66 | + /** |
|
67 | + * @param string $gid |
|
68 | + * @param Backend[] $backends |
|
69 | + * @param EventDispatcherInterface $dispatcher |
|
70 | + * @param IUserManager $userManager |
|
71 | + * @param PublicEmitter $emitter |
|
72 | + * @param string $displayName |
|
73 | + */ |
|
74 | + public function __construct(string $gid, array $backends, EventDispatcherInterface $dispatcher, IUserManager $userManager, PublicEmitter $emitter = null, ?string $displayName = null) { |
|
75 | + $this->gid = $gid; |
|
76 | + $this->backends = $backends; |
|
77 | + $this->dispatcher = $dispatcher; |
|
78 | + $this->userManager = $userManager; |
|
79 | + $this->emitter = $emitter; |
|
80 | + $this->displayName = $displayName; |
|
81 | + } |
|
82 | 82 | |
83 | - public function getGID() { |
|
84 | - return $this->gid; |
|
85 | - } |
|
83 | + public function getGID() { |
|
84 | + return $this->gid; |
|
85 | + } |
|
86 | 86 | |
87 | - public function getDisplayName() { |
|
88 | - if (is_null($this->displayName)) { |
|
89 | - return $this->gid; |
|
90 | - } |
|
91 | - return $this->displayName; |
|
92 | - } |
|
87 | + public function getDisplayName() { |
|
88 | + if (is_null($this->displayName)) { |
|
89 | + return $this->gid; |
|
90 | + } |
|
91 | + return $this->displayName; |
|
92 | + } |
|
93 | 93 | |
94 | - /** |
|
95 | - * get all users in the group |
|
96 | - * |
|
97 | - * @return \OC\User\User[] |
|
98 | - */ |
|
99 | - public function getUsers() { |
|
100 | - if ($this->usersLoaded) { |
|
101 | - return $this->users; |
|
102 | - } |
|
94 | + /** |
|
95 | + * get all users in the group |
|
96 | + * |
|
97 | + * @return \OC\User\User[] |
|
98 | + */ |
|
99 | + public function getUsers() { |
|
100 | + if ($this->usersLoaded) { |
|
101 | + return $this->users; |
|
102 | + } |
|
103 | 103 | |
104 | - $userIds = array(); |
|
105 | - foreach ($this->backends as $backend) { |
|
106 | - $diff = array_diff( |
|
107 | - $backend->usersInGroup($this->gid), |
|
108 | - $userIds |
|
109 | - ); |
|
110 | - if ($diff) { |
|
111 | - $userIds = array_merge($userIds, $diff); |
|
112 | - } |
|
113 | - } |
|
104 | + $userIds = array(); |
|
105 | + foreach ($this->backends as $backend) { |
|
106 | + $diff = array_diff( |
|
107 | + $backend->usersInGroup($this->gid), |
|
108 | + $userIds |
|
109 | + ); |
|
110 | + if ($diff) { |
|
111 | + $userIds = array_merge($userIds, $diff); |
|
112 | + } |
|
113 | + } |
|
114 | 114 | |
115 | - $this->users = $this->getVerifiedUsers($userIds); |
|
116 | - $this->usersLoaded = true; |
|
117 | - return $this->users; |
|
118 | - } |
|
115 | + $this->users = $this->getVerifiedUsers($userIds); |
|
116 | + $this->usersLoaded = true; |
|
117 | + return $this->users; |
|
118 | + } |
|
119 | 119 | |
120 | - /** |
|
121 | - * check if a user is in the group |
|
122 | - * |
|
123 | - * @param IUser $user |
|
124 | - * @return bool |
|
125 | - */ |
|
126 | - public function inGroup(IUser $user) { |
|
127 | - if (isset($this->users[$user->getUID()])) { |
|
128 | - return true; |
|
129 | - } |
|
130 | - foreach ($this->backends as $backend) { |
|
131 | - if ($backend->inGroup($user->getUID(), $this->gid)) { |
|
132 | - $this->users[$user->getUID()] = $user; |
|
133 | - return true; |
|
134 | - } |
|
135 | - } |
|
136 | - return false; |
|
137 | - } |
|
120 | + /** |
|
121 | + * check if a user is in the group |
|
122 | + * |
|
123 | + * @param IUser $user |
|
124 | + * @return bool |
|
125 | + */ |
|
126 | + public function inGroup(IUser $user) { |
|
127 | + if (isset($this->users[$user->getUID()])) { |
|
128 | + return true; |
|
129 | + } |
|
130 | + foreach ($this->backends as $backend) { |
|
131 | + if ($backend->inGroup($user->getUID(), $this->gid)) { |
|
132 | + $this->users[$user->getUID()] = $user; |
|
133 | + return true; |
|
134 | + } |
|
135 | + } |
|
136 | + return false; |
|
137 | + } |
|
138 | 138 | |
139 | - /** |
|
140 | - * add a user to the group |
|
141 | - * |
|
142 | - * @param IUser $user |
|
143 | - */ |
|
144 | - public function addUser(IUser $user) { |
|
145 | - if ($this->inGroup($user)) { |
|
146 | - return; |
|
147 | - } |
|
139 | + /** |
|
140 | + * add a user to the group |
|
141 | + * |
|
142 | + * @param IUser $user |
|
143 | + */ |
|
144 | + public function addUser(IUser $user) { |
|
145 | + if ($this->inGroup($user)) { |
|
146 | + return; |
|
147 | + } |
|
148 | 148 | |
149 | - $this->dispatcher->dispatch(IGroup::class . '::preAddUser', new GenericEvent($this, [ |
|
150 | - 'user' => $user, |
|
151 | - ])); |
|
149 | + $this->dispatcher->dispatch(IGroup::class . '::preAddUser', new GenericEvent($this, [ |
|
150 | + 'user' => $user, |
|
151 | + ])); |
|
152 | 152 | |
153 | - if ($this->emitter) { |
|
154 | - $this->emitter->emit('\OC\Group', 'preAddUser', array($this, $user)); |
|
155 | - } |
|
156 | - foreach ($this->backends as $backend) { |
|
157 | - if ($backend->implementsActions(\OC\Group\Backend::ADD_TO_GROUP)) { |
|
158 | - $backend->addToGroup($user->getUID(), $this->gid); |
|
159 | - if ($this->users) { |
|
160 | - $this->users[$user->getUID()] = $user; |
|
161 | - } |
|
153 | + if ($this->emitter) { |
|
154 | + $this->emitter->emit('\OC\Group', 'preAddUser', array($this, $user)); |
|
155 | + } |
|
156 | + foreach ($this->backends as $backend) { |
|
157 | + if ($backend->implementsActions(\OC\Group\Backend::ADD_TO_GROUP)) { |
|
158 | + $backend->addToGroup($user->getUID(), $this->gid); |
|
159 | + if ($this->users) { |
|
160 | + $this->users[$user->getUID()] = $user; |
|
161 | + } |
|
162 | 162 | |
163 | - $this->dispatcher->dispatch(IGroup::class . '::postAddUser', new GenericEvent($this, [ |
|
164 | - 'user' => $user, |
|
165 | - ])); |
|
163 | + $this->dispatcher->dispatch(IGroup::class . '::postAddUser', new GenericEvent($this, [ |
|
164 | + 'user' => $user, |
|
165 | + ])); |
|
166 | 166 | |
167 | - if ($this->emitter) { |
|
168 | - $this->emitter->emit('\OC\Group', 'postAddUser', array($this, $user)); |
|
169 | - } |
|
170 | - return; |
|
171 | - } |
|
172 | - } |
|
173 | - } |
|
167 | + if ($this->emitter) { |
|
168 | + $this->emitter->emit('\OC\Group', 'postAddUser', array($this, $user)); |
|
169 | + } |
|
170 | + return; |
|
171 | + } |
|
172 | + } |
|
173 | + } |
|
174 | 174 | |
175 | - /** |
|
176 | - * remove a user from the group |
|
177 | - * |
|
178 | - * @param \OC\User\User $user |
|
179 | - */ |
|
180 | - public function removeUser($user) { |
|
181 | - $result = false; |
|
182 | - $this->dispatcher->dispatch(IGroup::class . '::preRemoveUser', new GenericEvent($this, [ |
|
183 | - 'user' => $user, |
|
184 | - ])); |
|
185 | - if ($this->emitter) { |
|
186 | - $this->emitter->emit('\OC\Group', 'preRemoveUser', array($this, $user)); |
|
187 | - } |
|
188 | - foreach ($this->backends as $backend) { |
|
189 | - if ($backend->implementsActions(\OC\Group\Backend::REMOVE_FROM_GOUP) and $backend->inGroup($user->getUID(), $this->gid)) { |
|
190 | - $backend->removeFromGroup($user->getUID(), $this->gid); |
|
191 | - $result = true; |
|
192 | - } |
|
193 | - } |
|
194 | - if ($result) { |
|
195 | - $this->dispatcher->dispatch(IGroup::class . '::postRemoveUser', new GenericEvent($this, [ |
|
196 | - 'user' => $user, |
|
197 | - ])); |
|
198 | - if ($this->emitter) { |
|
199 | - $this->emitter->emit('\OC\Group', 'postRemoveUser', array($this, $user)); |
|
200 | - } |
|
201 | - if ($this->users) { |
|
202 | - foreach ($this->users as $index => $groupUser) { |
|
203 | - if ($groupUser->getUID() === $user->getUID()) { |
|
204 | - unset($this->users[$index]); |
|
205 | - return; |
|
206 | - } |
|
207 | - } |
|
208 | - } |
|
209 | - } |
|
210 | - } |
|
175 | + /** |
|
176 | + * remove a user from the group |
|
177 | + * |
|
178 | + * @param \OC\User\User $user |
|
179 | + */ |
|
180 | + public function removeUser($user) { |
|
181 | + $result = false; |
|
182 | + $this->dispatcher->dispatch(IGroup::class . '::preRemoveUser', new GenericEvent($this, [ |
|
183 | + 'user' => $user, |
|
184 | + ])); |
|
185 | + if ($this->emitter) { |
|
186 | + $this->emitter->emit('\OC\Group', 'preRemoveUser', array($this, $user)); |
|
187 | + } |
|
188 | + foreach ($this->backends as $backend) { |
|
189 | + if ($backend->implementsActions(\OC\Group\Backend::REMOVE_FROM_GOUP) and $backend->inGroup($user->getUID(), $this->gid)) { |
|
190 | + $backend->removeFromGroup($user->getUID(), $this->gid); |
|
191 | + $result = true; |
|
192 | + } |
|
193 | + } |
|
194 | + if ($result) { |
|
195 | + $this->dispatcher->dispatch(IGroup::class . '::postRemoveUser', new GenericEvent($this, [ |
|
196 | + 'user' => $user, |
|
197 | + ])); |
|
198 | + if ($this->emitter) { |
|
199 | + $this->emitter->emit('\OC\Group', 'postRemoveUser', array($this, $user)); |
|
200 | + } |
|
201 | + if ($this->users) { |
|
202 | + foreach ($this->users as $index => $groupUser) { |
|
203 | + if ($groupUser->getUID() === $user->getUID()) { |
|
204 | + unset($this->users[$index]); |
|
205 | + return; |
|
206 | + } |
|
207 | + } |
|
208 | + } |
|
209 | + } |
|
210 | + } |
|
211 | 211 | |
212 | - /** |
|
213 | - * search for users in the group by userid |
|
214 | - * |
|
215 | - * @param string $search |
|
216 | - * @param int $limit |
|
217 | - * @param int $offset |
|
218 | - * @return \OC\User\User[] |
|
219 | - */ |
|
220 | - public function searchUsers($search, $limit = null, $offset = null) { |
|
221 | - $users = array(); |
|
222 | - foreach ($this->backends as $backend) { |
|
223 | - $userIds = $backend->usersInGroup($this->gid, $search, $limit, $offset); |
|
224 | - $users += $this->getVerifiedUsers($userIds); |
|
225 | - if (!is_null($limit) and $limit <= 0) { |
|
226 | - return $users; |
|
227 | - } |
|
228 | - } |
|
229 | - return $users; |
|
230 | - } |
|
212 | + /** |
|
213 | + * search for users in the group by userid |
|
214 | + * |
|
215 | + * @param string $search |
|
216 | + * @param int $limit |
|
217 | + * @param int $offset |
|
218 | + * @return \OC\User\User[] |
|
219 | + */ |
|
220 | + public function searchUsers($search, $limit = null, $offset = null) { |
|
221 | + $users = array(); |
|
222 | + foreach ($this->backends as $backend) { |
|
223 | + $userIds = $backend->usersInGroup($this->gid, $search, $limit, $offset); |
|
224 | + $users += $this->getVerifiedUsers($userIds); |
|
225 | + if (!is_null($limit) and $limit <= 0) { |
|
226 | + return $users; |
|
227 | + } |
|
228 | + } |
|
229 | + return $users; |
|
230 | + } |
|
231 | 231 | |
232 | - /** |
|
233 | - * returns the number of users matching the search string |
|
234 | - * |
|
235 | - * @param string $search |
|
236 | - * @return int|bool |
|
237 | - */ |
|
238 | - public function count($search = '') { |
|
239 | - $users = false; |
|
240 | - foreach ($this->backends as $backend) { |
|
241 | - if($backend->implementsActions(\OC\Group\Backend::COUNT_USERS)) { |
|
242 | - if($users === false) { |
|
243 | - //we could directly add to a bool variable, but this would |
|
244 | - //be ugly |
|
245 | - $users = 0; |
|
246 | - } |
|
247 | - $users += $backend->countUsersInGroup($this->gid, $search); |
|
248 | - } |
|
249 | - } |
|
250 | - return $users; |
|
251 | - } |
|
232 | + /** |
|
233 | + * returns the number of users matching the search string |
|
234 | + * |
|
235 | + * @param string $search |
|
236 | + * @return int|bool |
|
237 | + */ |
|
238 | + public function count($search = '') { |
|
239 | + $users = false; |
|
240 | + foreach ($this->backends as $backend) { |
|
241 | + if($backend->implementsActions(\OC\Group\Backend::COUNT_USERS)) { |
|
242 | + if($users === false) { |
|
243 | + //we could directly add to a bool variable, but this would |
|
244 | + //be ugly |
|
245 | + $users = 0; |
|
246 | + } |
|
247 | + $users += $backend->countUsersInGroup($this->gid, $search); |
|
248 | + } |
|
249 | + } |
|
250 | + return $users; |
|
251 | + } |
|
252 | 252 | |
253 | - /** |
|
254 | - * returns the number of disabled users |
|
255 | - * |
|
256 | - * @return int|bool |
|
257 | - */ |
|
258 | - public function countDisabled() { |
|
259 | - $users = false; |
|
260 | - foreach ($this->backends as $backend) { |
|
261 | - if($backend instanceOf ICountDisabledInGroup) { |
|
262 | - if($users === false) { |
|
263 | - //we could directly add to a bool variable, but this would |
|
264 | - //be ugly |
|
265 | - $users = 0; |
|
266 | - } |
|
267 | - $users += $backend->countDisabledInGroup($this->gid); |
|
268 | - } |
|
269 | - } |
|
270 | - return $users; |
|
271 | - } |
|
253 | + /** |
|
254 | + * returns the number of disabled users |
|
255 | + * |
|
256 | + * @return int|bool |
|
257 | + */ |
|
258 | + public function countDisabled() { |
|
259 | + $users = false; |
|
260 | + foreach ($this->backends as $backend) { |
|
261 | + if($backend instanceOf ICountDisabledInGroup) { |
|
262 | + if($users === false) { |
|
263 | + //we could directly add to a bool variable, but this would |
|
264 | + //be ugly |
|
265 | + $users = 0; |
|
266 | + } |
|
267 | + $users += $backend->countDisabledInGroup($this->gid); |
|
268 | + } |
|
269 | + } |
|
270 | + return $users; |
|
271 | + } |
|
272 | 272 | |
273 | - /** |
|
274 | - * search for users in the group by displayname |
|
275 | - * |
|
276 | - * @param string $search |
|
277 | - * @param int $limit |
|
278 | - * @param int $offset |
|
279 | - * @return \OC\User\User[] |
|
280 | - */ |
|
281 | - public function searchDisplayName($search, $limit = null, $offset = null) { |
|
282 | - $users = array(); |
|
283 | - foreach ($this->backends as $backend) { |
|
284 | - $userIds = $backend->usersInGroup($this->gid, $search, $limit, $offset); |
|
285 | - $users = $this->getVerifiedUsers($userIds); |
|
286 | - if (!is_null($limit) and $limit <= 0) { |
|
287 | - return array_values($users); |
|
288 | - } |
|
289 | - } |
|
290 | - return array_values($users); |
|
291 | - } |
|
273 | + /** |
|
274 | + * search for users in the group by displayname |
|
275 | + * |
|
276 | + * @param string $search |
|
277 | + * @param int $limit |
|
278 | + * @param int $offset |
|
279 | + * @return \OC\User\User[] |
|
280 | + */ |
|
281 | + public function searchDisplayName($search, $limit = null, $offset = null) { |
|
282 | + $users = array(); |
|
283 | + foreach ($this->backends as $backend) { |
|
284 | + $userIds = $backend->usersInGroup($this->gid, $search, $limit, $offset); |
|
285 | + $users = $this->getVerifiedUsers($userIds); |
|
286 | + if (!is_null($limit) and $limit <= 0) { |
|
287 | + return array_values($users); |
|
288 | + } |
|
289 | + } |
|
290 | + return array_values($users); |
|
291 | + } |
|
292 | 292 | |
293 | - /** |
|
294 | - * delete the group |
|
295 | - * |
|
296 | - * @return bool |
|
297 | - */ |
|
298 | - public function delete() { |
|
299 | - // Prevent users from deleting group admin |
|
300 | - if ($this->getGID() === 'admin') { |
|
301 | - return false; |
|
302 | - } |
|
293 | + /** |
|
294 | + * delete the group |
|
295 | + * |
|
296 | + * @return bool |
|
297 | + */ |
|
298 | + public function delete() { |
|
299 | + // Prevent users from deleting group admin |
|
300 | + if ($this->getGID() === 'admin') { |
|
301 | + return false; |
|
302 | + } |
|
303 | 303 | |
304 | - $result = false; |
|
305 | - $this->dispatcher->dispatch(IGroup::class . '::preDelete', new GenericEvent($this)); |
|
306 | - if ($this->emitter) { |
|
307 | - $this->emitter->emit('\OC\Group', 'preDelete', array($this)); |
|
308 | - } |
|
309 | - foreach ($this->backends as $backend) { |
|
310 | - if ($backend->implementsActions(\OC\Group\Backend::DELETE_GROUP)) { |
|
311 | - $result = true; |
|
312 | - $backend->deleteGroup($this->gid); |
|
313 | - } |
|
314 | - } |
|
315 | - if ($result) { |
|
316 | - $this->dispatcher->dispatch(IGroup::class . '::postDelete', new GenericEvent($this)); |
|
317 | - if ($this->emitter) { |
|
318 | - $this->emitter->emit('\OC\Group', 'postDelete', array($this)); |
|
319 | - } |
|
320 | - } |
|
321 | - return $result; |
|
322 | - } |
|
304 | + $result = false; |
|
305 | + $this->dispatcher->dispatch(IGroup::class . '::preDelete', new GenericEvent($this)); |
|
306 | + if ($this->emitter) { |
|
307 | + $this->emitter->emit('\OC\Group', 'preDelete', array($this)); |
|
308 | + } |
|
309 | + foreach ($this->backends as $backend) { |
|
310 | + if ($backend->implementsActions(\OC\Group\Backend::DELETE_GROUP)) { |
|
311 | + $result = true; |
|
312 | + $backend->deleteGroup($this->gid); |
|
313 | + } |
|
314 | + } |
|
315 | + if ($result) { |
|
316 | + $this->dispatcher->dispatch(IGroup::class . '::postDelete', new GenericEvent($this)); |
|
317 | + if ($this->emitter) { |
|
318 | + $this->emitter->emit('\OC\Group', 'postDelete', array($this)); |
|
319 | + } |
|
320 | + } |
|
321 | + return $result; |
|
322 | + } |
|
323 | 323 | |
324 | - /** |
|
325 | - * returns all the Users from an array that really exists |
|
326 | - * @param string[] $userIds an array containing user IDs |
|
327 | - * @return \OC\User\User[] an Array with the userId as Key and \OC\User\User as value |
|
328 | - */ |
|
329 | - private function getVerifiedUsers($userIds) { |
|
330 | - if (!is_array($userIds)) { |
|
331 | - return array(); |
|
332 | - } |
|
333 | - $users = array(); |
|
334 | - foreach ($userIds as $userId) { |
|
335 | - $user = $this->userManager->get($userId); |
|
336 | - if (!is_null($user)) { |
|
337 | - $users[$userId] = $user; |
|
338 | - } |
|
339 | - } |
|
340 | - return $users; |
|
341 | - } |
|
324 | + /** |
|
325 | + * returns all the Users from an array that really exists |
|
326 | + * @param string[] $userIds an array containing user IDs |
|
327 | + * @return \OC\User\User[] an Array with the userId as Key and \OC\User\User as value |
|
328 | + */ |
|
329 | + private function getVerifiedUsers($userIds) { |
|
330 | + if (!is_array($userIds)) { |
|
331 | + return array(); |
|
332 | + } |
|
333 | + $users = array(); |
|
334 | + foreach ($userIds as $userId) { |
|
335 | + $user = $this->userManager->get($userId); |
|
336 | + if (!is_null($user)) { |
|
337 | + $users[$userId] = $user; |
|
338 | + } |
|
339 | + } |
|
340 | + return $users; |
|
341 | + } |
|
342 | 342 | |
343 | - /** |
|
344 | - * @return bool |
|
345 | - * @since 14.0.0 |
|
346 | - */ |
|
347 | - public function canRemoveUser() { |
|
348 | - foreach ($this->backends as $backend) { |
|
349 | - if ($backend->implementsActions(GroupInterface::REMOVE_FROM_GOUP)) { |
|
350 | - return true; |
|
351 | - } |
|
352 | - } |
|
353 | - return false; |
|
354 | - } |
|
343 | + /** |
|
344 | + * @return bool |
|
345 | + * @since 14.0.0 |
|
346 | + */ |
|
347 | + public function canRemoveUser() { |
|
348 | + foreach ($this->backends as $backend) { |
|
349 | + if ($backend->implementsActions(GroupInterface::REMOVE_FROM_GOUP)) { |
|
350 | + return true; |
|
351 | + } |
|
352 | + } |
|
353 | + return false; |
|
354 | + } |
|
355 | 355 | |
356 | - /** |
|
357 | - * @return bool |
|
358 | - * @since 14.0.0 |
|
359 | - */ |
|
360 | - public function canAddUser() { |
|
361 | - foreach ($this->backends as $backend) { |
|
362 | - if ($backend->implementsActions(GroupInterface::ADD_TO_GROUP)) { |
|
363 | - return true; |
|
364 | - } |
|
365 | - } |
|
366 | - return false; |
|
367 | - } |
|
356 | + /** |
|
357 | + * @return bool |
|
358 | + * @since 14.0.0 |
|
359 | + */ |
|
360 | + public function canAddUser() { |
|
361 | + foreach ($this->backends as $backend) { |
|
362 | + if ($backend->implementsActions(GroupInterface::ADD_TO_GROUP)) { |
|
363 | + return true; |
|
364 | + } |
|
365 | + } |
|
366 | + return false; |
|
367 | + } |
|
368 | 368 | |
369 | - /** |
|
370 | - * @return bool |
|
371 | - * @since 16.0.0 |
|
372 | - */ |
|
373 | - public function hideFromCollaboration(): bool { |
|
374 | - return array_reduce($this->backends, function(bool $hide, GroupInterface $backend) { |
|
375 | - return $hide | ($backend instanceof IHideFromCollaborationBackend && $backend->hideGroup($this->gid)); |
|
376 | - }, false); |
|
377 | - } |
|
369 | + /** |
|
370 | + * @return bool |
|
371 | + * @since 16.0.0 |
|
372 | + */ |
|
373 | + public function hideFromCollaboration(): bool { |
|
374 | + return array_reduce($this->backends, function(bool $hide, GroupInterface $backend) { |
|
375 | + return $hide | ($backend instanceof IHideFromCollaborationBackend && $backend->hideGroup($this->gid)); |
|
376 | + }, false); |
|
377 | + } |
|
378 | 378 | } |
@@ -68,348 +68,348 @@ |
||
68 | 68 | |
69 | 69 | class DIContainer extends SimpleContainer implements IAppContainer { |
70 | 70 | |
71 | - /** |
|
72 | - * @var array |
|
73 | - */ |
|
74 | - private $middleWares = []; |
|
75 | - |
|
76 | - /** @var ServerContainer */ |
|
77 | - private $server; |
|
78 | - |
|
79 | - /** |
|
80 | - * Put your class dependencies in here |
|
81 | - * @param string $appName the name of the app |
|
82 | - * @param array $urlParams |
|
83 | - * @param ServerContainer|null $server |
|
84 | - */ |
|
85 | - public function __construct($appName, $urlParams = array(), ServerContainer $server = null){ |
|
86 | - parent::__construct(); |
|
87 | - $this['AppName'] = $appName; |
|
88 | - $this['urlParams'] = $urlParams; |
|
89 | - |
|
90 | - $this->registerAlias('Request', IRequest::class); |
|
91 | - |
|
92 | - /** @var \OC\ServerContainer $server */ |
|
93 | - if ($server === null) { |
|
94 | - $server = \OC::$server; |
|
95 | - } |
|
96 | - $this->server = $server; |
|
97 | - $this->server->registerAppContainer($appName, $this); |
|
98 | - |
|
99 | - // aliases |
|
100 | - $this->registerAlias('appName', 'AppName'); |
|
101 | - $this->registerAlias('webRoot', 'WebRoot'); |
|
102 | - $this->registerAlias('userId', 'UserId'); |
|
103 | - |
|
104 | - /** |
|
105 | - * Core services |
|
106 | - */ |
|
107 | - $this->registerService(IOutput::class, function(){ |
|
108 | - return new Output($this->getServer()->getWebRoot()); |
|
109 | - }); |
|
110 | - |
|
111 | - $this->registerService(Folder::class, function() { |
|
112 | - return $this->getServer()->getUserFolder(); |
|
113 | - }); |
|
114 | - |
|
115 | - $this->registerService(IAppData::class, function (SimpleContainer $c) { |
|
116 | - return $this->getServer()->getAppDataDir($c->query('AppName')); |
|
117 | - }); |
|
118 | - |
|
119 | - $this->registerService(IL10N::class, function($c) { |
|
120 | - return $this->getServer()->getL10N($c->query('AppName')); |
|
121 | - }); |
|
122 | - |
|
123 | - // Log wrapper |
|
124 | - $this->registerService(ILogger::class, function ($c) { |
|
125 | - return new OC\AppFramework\Logger($this->server->query(ILogger::class), $c->query('AppName')); |
|
126 | - }); |
|
127 | - |
|
128 | - $this->registerService(IServerContainer::class, function () { |
|
129 | - return $this->getServer(); |
|
130 | - }); |
|
131 | - $this->registerAlias('ServerContainer', IServerContainer::class); |
|
132 | - |
|
133 | - $this->registerService(\OCP\WorkflowEngine\IManager::class, function ($c) { |
|
134 | - return $c->query(Manager::class); |
|
135 | - }); |
|
136 | - |
|
137 | - $this->registerService(\OCP\AppFramework\IAppContainer::class, function ($c) { |
|
138 | - return $c; |
|
139 | - }); |
|
140 | - |
|
141 | - // commonly used attributes |
|
142 | - $this->registerService('UserId', function ($c) { |
|
143 | - return $c->query(IUserSession::class)->getSession()->get('user_id'); |
|
144 | - }); |
|
145 | - |
|
146 | - $this->registerService('WebRoot', function ($c) { |
|
147 | - return $c->query('ServerContainer')->getWebRoot(); |
|
148 | - }); |
|
149 | - |
|
150 | - $this->registerService('OC_Defaults', function ($c) { |
|
151 | - return $c->getServer()->getThemingDefaults(); |
|
152 | - }); |
|
153 | - |
|
154 | - $this->registerService(IConfig::class, function ($c) { |
|
155 | - return $c->query(OC\GlobalScale\Config::class); |
|
156 | - }); |
|
157 | - |
|
158 | - $this->registerService('Protocol', function($c){ |
|
159 | - /** @var \OC\Server $server */ |
|
160 | - $server = $c->query('ServerContainer'); |
|
161 | - $protocol = $server->getRequest()->getHttpProtocol(); |
|
162 | - return new Http($_SERVER, $protocol); |
|
163 | - }); |
|
164 | - |
|
165 | - $this->registerService('Dispatcher', function($c) { |
|
166 | - return new Dispatcher( |
|
167 | - $c['Protocol'], |
|
168 | - $c['MiddlewareDispatcher'], |
|
169 | - $c->query(IControllerMethodReflector::class), |
|
170 | - $c['Request'] |
|
171 | - ); |
|
172 | - }); |
|
173 | - |
|
174 | - /** |
|
175 | - * App Framework default arguments |
|
176 | - */ |
|
177 | - $this->registerParameter('corsMethods', 'PUT, POST, GET, DELETE, PATCH'); |
|
178 | - $this->registerParameter('corsAllowedHeaders', 'Authorization, Content-Type, Accept'); |
|
179 | - $this->registerParameter('corsMaxAge', 1728000); |
|
180 | - |
|
181 | - /** |
|
182 | - * Middleware |
|
183 | - */ |
|
184 | - $this->registerService('MiddlewareDispatcher', function(SimpleContainer $c) { |
|
185 | - $server = $this->getServer(); |
|
186 | - |
|
187 | - $dispatcher = new MiddlewareDispatcher(); |
|
188 | - $dispatcher->registerMiddleware( |
|
189 | - $c->query(OC\AppFramework\Middleware\Security\ReloadExecutionMiddleware::class) |
|
190 | - ); |
|
191 | - |
|
192 | - $dispatcher->registerMiddleware( |
|
193 | - new OC\AppFramework\Middleware\Security\SameSiteCookieMiddleware( |
|
194 | - $c->query(IRequest::class), |
|
195 | - $c->query(IControllerMethodReflector::class) |
|
196 | - ) |
|
197 | - ); |
|
198 | - $dispatcher->registerMiddleware( |
|
199 | - new CORSMiddleware( |
|
200 | - $c->query(IRequest::class), |
|
201 | - $c->query(IControllerMethodReflector::class), |
|
202 | - $c->query(IUserSession::class), |
|
203 | - $c->query(OC\Security\Bruteforce\Throttler::class) |
|
204 | - ) |
|
205 | - ); |
|
206 | - $dispatcher->registerMiddleware( |
|
207 | - new OCSMiddleware( |
|
208 | - $c->query(IRequest::class) |
|
209 | - ) |
|
210 | - ); |
|
211 | - |
|
212 | - $securityMiddleware = new SecurityMiddleware( |
|
213 | - $c->query(IRequest::class), |
|
214 | - $c->query(IControllerMethodReflector::class), |
|
215 | - $c->query(INavigationManager::class), |
|
216 | - $c->query(IURLGenerator::class), |
|
217 | - $server->getLogger(), |
|
218 | - $c['AppName'], |
|
219 | - $server->getUserSession()->isLoggedIn(), |
|
220 | - $server->getGroupManager()->isAdmin($this->getUserId()), |
|
221 | - $server->getContentSecurityPolicyManager(), |
|
222 | - $server->getCsrfTokenManager(), |
|
223 | - $server->getContentSecurityPolicyNonceManager(), |
|
224 | - $server->getAppManager(), |
|
225 | - $server->getL10N('lib') |
|
226 | - ); |
|
227 | - $dispatcher->registerMiddleware($securityMiddleware); |
|
228 | - $dispatcher->registerMiddleware( |
|
229 | - new OC\AppFramework\Middleware\Security\PasswordConfirmationMiddleware( |
|
230 | - $c->query(IControllerMethodReflector::class), |
|
231 | - $c->query(ISession::class), |
|
232 | - $c->query(IUserSession::class), |
|
233 | - $c->query(ITimeFactory::class) |
|
234 | - ) |
|
235 | - ); |
|
236 | - $dispatcher->registerMiddleware( |
|
237 | - new TwoFactorMiddleware( |
|
238 | - $c->query(OC\Authentication\TwoFactorAuth\Manager::class), |
|
239 | - $c->query(IUserSession::class), |
|
240 | - $c->query(ISession::class), |
|
241 | - $c->query(IURLGenerator::class), |
|
242 | - $c->query(IControllerMethodReflector::class), |
|
243 | - $c->query(IRequest::class) |
|
244 | - ) |
|
245 | - ); |
|
246 | - $dispatcher->registerMiddleware( |
|
247 | - new OC\AppFramework\Middleware\Security\BruteForceMiddleware( |
|
248 | - $c->query(IControllerMethodReflector::class), |
|
249 | - $c->query(OC\Security\Bruteforce\Throttler::class), |
|
250 | - $c->query(IRequest::class) |
|
251 | - ) |
|
252 | - ); |
|
253 | - $dispatcher->registerMiddleware( |
|
254 | - new RateLimitingMiddleware( |
|
255 | - $c->query(IRequest::class), |
|
256 | - $c->query(IUserSession::class), |
|
257 | - $c->query(IControllerMethodReflector::class), |
|
258 | - $c->query(OC\Security\RateLimiting\Limiter::class) |
|
259 | - ) |
|
260 | - ); |
|
261 | - $dispatcher->registerMiddleware( |
|
262 | - new OC\AppFramework\Middleware\PublicShare\PublicShareMiddleware( |
|
263 | - $c->query(IRequest::class), |
|
264 | - $c->query(ISession::class), |
|
265 | - $c->query(\OCP\IConfig::class) |
|
266 | - ) |
|
267 | - ); |
|
268 | - $dispatcher->registerMiddleware( |
|
269 | - $c->query(\OC\AppFramework\Middleware\AdditionalScriptsMiddleware::class) |
|
270 | - ); |
|
271 | - |
|
272 | - foreach($this->middleWares as $middleWare) { |
|
273 | - $dispatcher->registerMiddleware($c[$middleWare]); |
|
274 | - } |
|
275 | - |
|
276 | - $dispatcher->registerMiddleware( |
|
277 | - new SessionMiddleware( |
|
278 | - $c->query(IRequest::class), |
|
279 | - $c->query(IControllerMethodReflector::class), |
|
280 | - $c->query(ISession::class) |
|
281 | - ) |
|
282 | - ); |
|
283 | - return $dispatcher; |
|
284 | - }); |
|
285 | - |
|
286 | - $this->registerAlias(\OCP\Collaboration\Resources\IManager::class, OC\Collaboration\Resources\Manager::class); |
|
287 | - } |
|
288 | - |
|
289 | - /** |
|
290 | - * @return \OCP\IServerContainer |
|
291 | - */ |
|
292 | - public function getServer() |
|
293 | - { |
|
294 | - return $this->server; |
|
295 | - } |
|
296 | - |
|
297 | - /** |
|
298 | - * @param string $middleWare |
|
299 | - * @return boolean|null |
|
300 | - */ |
|
301 | - public function registerMiddleWare($middleWare) { |
|
302 | - $this->middleWares[] = $middleWare; |
|
303 | - } |
|
304 | - |
|
305 | - /** |
|
306 | - * used to return the appname of the set application |
|
307 | - * @return string the name of your application |
|
308 | - */ |
|
309 | - public function getAppName() { |
|
310 | - return $this->query('AppName'); |
|
311 | - } |
|
312 | - |
|
313 | - /** |
|
314 | - * @deprecated use IUserSession->isLoggedIn() |
|
315 | - * @return boolean |
|
316 | - */ |
|
317 | - public function isLoggedIn() { |
|
318 | - return \OC::$server->getUserSession()->isLoggedIn(); |
|
319 | - } |
|
320 | - |
|
321 | - /** |
|
322 | - * @deprecated use IGroupManager->isAdmin($userId) |
|
323 | - * @return boolean |
|
324 | - */ |
|
325 | - public function isAdminUser() { |
|
326 | - $uid = $this->getUserId(); |
|
327 | - return \OC_User::isAdminUser($uid); |
|
328 | - } |
|
329 | - |
|
330 | - private function getUserId() { |
|
331 | - return $this->getServer()->getSession()->get('user_id'); |
|
332 | - } |
|
333 | - |
|
334 | - /** |
|
335 | - * @deprecated use the ILogger instead |
|
336 | - * @param string $message |
|
337 | - * @param string $level |
|
338 | - * @return mixed |
|
339 | - */ |
|
340 | - public function log($message, $level) { |
|
341 | - switch($level){ |
|
342 | - case 'debug': |
|
343 | - $level = ILogger::DEBUG; |
|
344 | - break; |
|
345 | - case 'info': |
|
346 | - $level = ILogger::INFO; |
|
347 | - break; |
|
348 | - case 'warn': |
|
349 | - $level = ILogger::WARN; |
|
350 | - break; |
|
351 | - case 'fatal': |
|
352 | - $level = ILogger::FATAL; |
|
353 | - break; |
|
354 | - default: |
|
355 | - $level = ILogger::ERROR; |
|
356 | - break; |
|
357 | - } |
|
358 | - \OCP\Util::writeLog($this->getAppName(), $message, $level); |
|
359 | - } |
|
360 | - |
|
361 | - /** |
|
362 | - * Register a capability |
|
363 | - * |
|
364 | - * @param string $serviceName e.g. 'OCA\Files\Capabilities' |
|
365 | - */ |
|
366 | - public function registerCapability($serviceName) { |
|
367 | - $this->query('OC\CapabilitiesManager')->registerCapability(function() use ($serviceName) { |
|
368 | - return $this->query($serviceName); |
|
369 | - }); |
|
370 | - } |
|
371 | - |
|
372 | - /** |
|
373 | - * @param string $name |
|
374 | - * @return mixed |
|
375 | - * @throws QueryException if the query could not be resolved |
|
376 | - */ |
|
377 | - public function query($name) { |
|
378 | - try { |
|
379 | - return $this->queryNoFallback($name); |
|
380 | - } catch (QueryException $firstException) { |
|
381 | - try { |
|
382 | - return $this->getServer()->query($name); |
|
383 | - } catch (QueryException $secondException) { |
|
384 | - if ($firstException->getCode() === 1) { |
|
385 | - throw $secondException; |
|
386 | - } |
|
387 | - throw $firstException; |
|
388 | - } |
|
389 | - } |
|
390 | - } |
|
391 | - |
|
392 | - /** |
|
393 | - * @param string $name |
|
394 | - * @return mixed |
|
395 | - * @throws QueryException if the query could not be resolved |
|
396 | - */ |
|
397 | - public function queryNoFallback($name) { |
|
398 | - $name = $this->sanitizeName($name); |
|
399 | - |
|
400 | - if ($this->offsetExists($name)) { |
|
401 | - return parent::query($name); |
|
402 | - } else { |
|
403 | - if ($this['AppName'] === 'settings' && strpos($name, 'OC\\Settings\\') === 0) { |
|
404 | - return parent::query($name); |
|
405 | - } else if ($this['AppName'] === 'core' && strpos($name, 'OC\\Core\\') === 0) { |
|
406 | - return parent::query($name); |
|
407 | - } else if (strpos($name, \OC\AppFramework\App::buildAppNamespace($this['AppName']) . '\\') === 0) { |
|
408 | - return parent::query($name); |
|
409 | - } |
|
410 | - } |
|
411 | - |
|
412 | - throw new QueryException('Could not resolve ' . $name . '!' . |
|
413 | - ' Class can not be instantiated', 1); |
|
414 | - } |
|
71 | + /** |
|
72 | + * @var array |
|
73 | + */ |
|
74 | + private $middleWares = []; |
|
75 | + |
|
76 | + /** @var ServerContainer */ |
|
77 | + private $server; |
|
78 | + |
|
79 | + /** |
|
80 | + * Put your class dependencies in here |
|
81 | + * @param string $appName the name of the app |
|
82 | + * @param array $urlParams |
|
83 | + * @param ServerContainer|null $server |
|
84 | + */ |
|
85 | + public function __construct($appName, $urlParams = array(), ServerContainer $server = null){ |
|
86 | + parent::__construct(); |
|
87 | + $this['AppName'] = $appName; |
|
88 | + $this['urlParams'] = $urlParams; |
|
89 | + |
|
90 | + $this->registerAlias('Request', IRequest::class); |
|
91 | + |
|
92 | + /** @var \OC\ServerContainer $server */ |
|
93 | + if ($server === null) { |
|
94 | + $server = \OC::$server; |
|
95 | + } |
|
96 | + $this->server = $server; |
|
97 | + $this->server->registerAppContainer($appName, $this); |
|
98 | + |
|
99 | + // aliases |
|
100 | + $this->registerAlias('appName', 'AppName'); |
|
101 | + $this->registerAlias('webRoot', 'WebRoot'); |
|
102 | + $this->registerAlias('userId', 'UserId'); |
|
103 | + |
|
104 | + /** |
|
105 | + * Core services |
|
106 | + */ |
|
107 | + $this->registerService(IOutput::class, function(){ |
|
108 | + return new Output($this->getServer()->getWebRoot()); |
|
109 | + }); |
|
110 | + |
|
111 | + $this->registerService(Folder::class, function() { |
|
112 | + return $this->getServer()->getUserFolder(); |
|
113 | + }); |
|
114 | + |
|
115 | + $this->registerService(IAppData::class, function (SimpleContainer $c) { |
|
116 | + return $this->getServer()->getAppDataDir($c->query('AppName')); |
|
117 | + }); |
|
118 | + |
|
119 | + $this->registerService(IL10N::class, function($c) { |
|
120 | + return $this->getServer()->getL10N($c->query('AppName')); |
|
121 | + }); |
|
122 | + |
|
123 | + // Log wrapper |
|
124 | + $this->registerService(ILogger::class, function ($c) { |
|
125 | + return new OC\AppFramework\Logger($this->server->query(ILogger::class), $c->query('AppName')); |
|
126 | + }); |
|
127 | + |
|
128 | + $this->registerService(IServerContainer::class, function () { |
|
129 | + return $this->getServer(); |
|
130 | + }); |
|
131 | + $this->registerAlias('ServerContainer', IServerContainer::class); |
|
132 | + |
|
133 | + $this->registerService(\OCP\WorkflowEngine\IManager::class, function ($c) { |
|
134 | + return $c->query(Manager::class); |
|
135 | + }); |
|
136 | + |
|
137 | + $this->registerService(\OCP\AppFramework\IAppContainer::class, function ($c) { |
|
138 | + return $c; |
|
139 | + }); |
|
140 | + |
|
141 | + // commonly used attributes |
|
142 | + $this->registerService('UserId', function ($c) { |
|
143 | + return $c->query(IUserSession::class)->getSession()->get('user_id'); |
|
144 | + }); |
|
145 | + |
|
146 | + $this->registerService('WebRoot', function ($c) { |
|
147 | + return $c->query('ServerContainer')->getWebRoot(); |
|
148 | + }); |
|
149 | + |
|
150 | + $this->registerService('OC_Defaults', function ($c) { |
|
151 | + return $c->getServer()->getThemingDefaults(); |
|
152 | + }); |
|
153 | + |
|
154 | + $this->registerService(IConfig::class, function ($c) { |
|
155 | + return $c->query(OC\GlobalScale\Config::class); |
|
156 | + }); |
|
157 | + |
|
158 | + $this->registerService('Protocol', function($c){ |
|
159 | + /** @var \OC\Server $server */ |
|
160 | + $server = $c->query('ServerContainer'); |
|
161 | + $protocol = $server->getRequest()->getHttpProtocol(); |
|
162 | + return new Http($_SERVER, $protocol); |
|
163 | + }); |
|
164 | + |
|
165 | + $this->registerService('Dispatcher', function($c) { |
|
166 | + return new Dispatcher( |
|
167 | + $c['Protocol'], |
|
168 | + $c['MiddlewareDispatcher'], |
|
169 | + $c->query(IControllerMethodReflector::class), |
|
170 | + $c['Request'] |
|
171 | + ); |
|
172 | + }); |
|
173 | + |
|
174 | + /** |
|
175 | + * App Framework default arguments |
|
176 | + */ |
|
177 | + $this->registerParameter('corsMethods', 'PUT, POST, GET, DELETE, PATCH'); |
|
178 | + $this->registerParameter('corsAllowedHeaders', 'Authorization, Content-Type, Accept'); |
|
179 | + $this->registerParameter('corsMaxAge', 1728000); |
|
180 | + |
|
181 | + /** |
|
182 | + * Middleware |
|
183 | + */ |
|
184 | + $this->registerService('MiddlewareDispatcher', function(SimpleContainer $c) { |
|
185 | + $server = $this->getServer(); |
|
186 | + |
|
187 | + $dispatcher = new MiddlewareDispatcher(); |
|
188 | + $dispatcher->registerMiddleware( |
|
189 | + $c->query(OC\AppFramework\Middleware\Security\ReloadExecutionMiddleware::class) |
|
190 | + ); |
|
191 | + |
|
192 | + $dispatcher->registerMiddleware( |
|
193 | + new OC\AppFramework\Middleware\Security\SameSiteCookieMiddleware( |
|
194 | + $c->query(IRequest::class), |
|
195 | + $c->query(IControllerMethodReflector::class) |
|
196 | + ) |
|
197 | + ); |
|
198 | + $dispatcher->registerMiddleware( |
|
199 | + new CORSMiddleware( |
|
200 | + $c->query(IRequest::class), |
|
201 | + $c->query(IControllerMethodReflector::class), |
|
202 | + $c->query(IUserSession::class), |
|
203 | + $c->query(OC\Security\Bruteforce\Throttler::class) |
|
204 | + ) |
|
205 | + ); |
|
206 | + $dispatcher->registerMiddleware( |
|
207 | + new OCSMiddleware( |
|
208 | + $c->query(IRequest::class) |
|
209 | + ) |
|
210 | + ); |
|
211 | + |
|
212 | + $securityMiddleware = new SecurityMiddleware( |
|
213 | + $c->query(IRequest::class), |
|
214 | + $c->query(IControllerMethodReflector::class), |
|
215 | + $c->query(INavigationManager::class), |
|
216 | + $c->query(IURLGenerator::class), |
|
217 | + $server->getLogger(), |
|
218 | + $c['AppName'], |
|
219 | + $server->getUserSession()->isLoggedIn(), |
|
220 | + $server->getGroupManager()->isAdmin($this->getUserId()), |
|
221 | + $server->getContentSecurityPolicyManager(), |
|
222 | + $server->getCsrfTokenManager(), |
|
223 | + $server->getContentSecurityPolicyNonceManager(), |
|
224 | + $server->getAppManager(), |
|
225 | + $server->getL10N('lib') |
|
226 | + ); |
|
227 | + $dispatcher->registerMiddleware($securityMiddleware); |
|
228 | + $dispatcher->registerMiddleware( |
|
229 | + new OC\AppFramework\Middleware\Security\PasswordConfirmationMiddleware( |
|
230 | + $c->query(IControllerMethodReflector::class), |
|
231 | + $c->query(ISession::class), |
|
232 | + $c->query(IUserSession::class), |
|
233 | + $c->query(ITimeFactory::class) |
|
234 | + ) |
|
235 | + ); |
|
236 | + $dispatcher->registerMiddleware( |
|
237 | + new TwoFactorMiddleware( |
|
238 | + $c->query(OC\Authentication\TwoFactorAuth\Manager::class), |
|
239 | + $c->query(IUserSession::class), |
|
240 | + $c->query(ISession::class), |
|
241 | + $c->query(IURLGenerator::class), |
|
242 | + $c->query(IControllerMethodReflector::class), |
|
243 | + $c->query(IRequest::class) |
|
244 | + ) |
|
245 | + ); |
|
246 | + $dispatcher->registerMiddleware( |
|
247 | + new OC\AppFramework\Middleware\Security\BruteForceMiddleware( |
|
248 | + $c->query(IControllerMethodReflector::class), |
|
249 | + $c->query(OC\Security\Bruteforce\Throttler::class), |
|
250 | + $c->query(IRequest::class) |
|
251 | + ) |
|
252 | + ); |
|
253 | + $dispatcher->registerMiddleware( |
|
254 | + new RateLimitingMiddleware( |
|
255 | + $c->query(IRequest::class), |
|
256 | + $c->query(IUserSession::class), |
|
257 | + $c->query(IControllerMethodReflector::class), |
|
258 | + $c->query(OC\Security\RateLimiting\Limiter::class) |
|
259 | + ) |
|
260 | + ); |
|
261 | + $dispatcher->registerMiddleware( |
|
262 | + new OC\AppFramework\Middleware\PublicShare\PublicShareMiddleware( |
|
263 | + $c->query(IRequest::class), |
|
264 | + $c->query(ISession::class), |
|
265 | + $c->query(\OCP\IConfig::class) |
|
266 | + ) |
|
267 | + ); |
|
268 | + $dispatcher->registerMiddleware( |
|
269 | + $c->query(\OC\AppFramework\Middleware\AdditionalScriptsMiddleware::class) |
|
270 | + ); |
|
271 | + |
|
272 | + foreach($this->middleWares as $middleWare) { |
|
273 | + $dispatcher->registerMiddleware($c[$middleWare]); |
|
274 | + } |
|
275 | + |
|
276 | + $dispatcher->registerMiddleware( |
|
277 | + new SessionMiddleware( |
|
278 | + $c->query(IRequest::class), |
|
279 | + $c->query(IControllerMethodReflector::class), |
|
280 | + $c->query(ISession::class) |
|
281 | + ) |
|
282 | + ); |
|
283 | + return $dispatcher; |
|
284 | + }); |
|
285 | + |
|
286 | + $this->registerAlias(\OCP\Collaboration\Resources\IManager::class, OC\Collaboration\Resources\Manager::class); |
|
287 | + } |
|
288 | + |
|
289 | + /** |
|
290 | + * @return \OCP\IServerContainer |
|
291 | + */ |
|
292 | + public function getServer() |
|
293 | + { |
|
294 | + return $this->server; |
|
295 | + } |
|
296 | + |
|
297 | + /** |
|
298 | + * @param string $middleWare |
|
299 | + * @return boolean|null |
|
300 | + */ |
|
301 | + public function registerMiddleWare($middleWare) { |
|
302 | + $this->middleWares[] = $middleWare; |
|
303 | + } |
|
304 | + |
|
305 | + /** |
|
306 | + * used to return the appname of the set application |
|
307 | + * @return string the name of your application |
|
308 | + */ |
|
309 | + public function getAppName() { |
|
310 | + return $this->query('AppName'); |
|
311 | + } |
|
312 | + |
|
313 | + /** |
|
314 | + * @deprecated use IUserSession->isLoggedIn() |
|
315 | + * @return boolean |
|
316 | + */ |
|
317 | + public function isLoggedIn() { |
|
318 | + return \OC::$server->getUserSession()->isLoggedIn(); |
|
319 | + } |
|
320 | + |
|
321 | + /** |
|
322 | + * @deprecated use IGroupManager->isAdmin($userId) |
|
323 | + * @return boolean |
|
324 | + */ |
|
325 | + public function isAdminUser() { |
|
326 | + $uid = $this->getUserId(); |
|
327 | + return \OC_User::isAdminUser($uid); |
|
328 | + } |
|
329 | + |
|
330 | + private function getUserId() { |
|
331 | + return $this->getServer()->getSession()->get('user_id'); |
|
332 | + } |
|
333 | + |
|
334 | + /** |
|
335 | + * @deprecated use the ILogger instead |
|
336 | + * @param string $message |
|
337 | + * @param string $level |
|
338 | + * @return mixed |
|
339 | + */ |
|
340 | + public function log($message, $level) { |
|
341 | + switch($level){ |
|
342 | + case 'debug': |
|
343 | + $level = ILogger::DEBUG; |
|
344 | + break; |
|
345 | + case 'info': |
|
346 | + $level = ILogger::INFO; |
|
347 | + break; |
|
348 | + case 'warn': |
|
349 | + $level = ILogger::WARN; |
|
350 | + break; |
|
351 | + case 'fatal': |
|
352 | + $level = ILogger::FATAL; |
|
353 | + break; |
|
354 | + default: |
|
355 | + $level = ILogger::ERROR; |
|
356 | + break; |
|
357 | + } |
|
358 | + \OCP\Util::writeLog($this->getAppName(), $message, $level); |
|
359 | + } |
|
360 | + |
|
361 | + /** |
|
362 | + * Register a capability |
|
363 | + * |
|
364 | + * @param string $serviceName e.g. 'OCA\Files\Capabilities' |
|
365 | + */ |
|
366 | + public function registerCapability($serviceName) { |
|
367 | + $this->query('OC\CapabilitiesManager')->registerCapability(function() use ($serviceName) { |
|
368 | + return $this->query($serviceName); |
|
369 | + }); |
|
370 | + } |
|
371 | + |
|
372 | + /** |
|
373 | + * @param string $name |
|
374 | + * @return mixed |
|
375 | + * @throws QueryException if the query could not be resolved |
|
376 | + */ |
|
377 | + public function query($name) { |
|
378 | + try { |
|
379 | + return $this->queryNoFallback($name); |
|
380 | + } catch (QueryException $firstException) { |
|
381 | + try { |
|
382 | + return $this->getServer()->query($name); |
|
383 | + } catch (QueryException $secondException) { |
|
384 | + if ($firstException->getCode() === 1) { |
|
385 | + throw $secondException; |
|
386 | + } |
|
387 | + throw $firstException; |
|
388 | + } |
|
389 | + } |
|
390 | + } |
|
391 | + |
|
392 | + /** |
|
393 | + * @param string $name |
|
394 | + * @return mixed |
|
395 | + * @throws QueryException if the query could not be resolved |
|
396 | + */ |
|
397 | + public function queryNoFallback($name) { |
|
398 | + $name = $this->sanitizeName($name); |
|
399 | + |
|
400 | + if ($this->offsetExists($name)) { |
|
401 | + return parent::query($name); |
|
402 | + } else { |
|
403 | + if ($this['AppName'] === 'settings' && strpos($name, 'OC\\Settings\\') === 0) { |
|
404 | + return parent::query($name); |
|
405 | + } else if ($this['AppName'] === 'core' && strpos($name, 'OC\\Core\\') === 0) { |
|
406 | + return parent::query($name); |
|
407 | + } else if (strpos($name, \OC\AppFramework\App::buildAppNamespace($this['AppName']) . '\\') === 0) { |
|
408 | + return parent::query($name); |
|
409 | + } |
|
410 | + } |
|
411 | + |
|
412 | + throw new QueryException('Could not resolve ' . $name . '!' . |
|
413 | + ' Class can not be instantiated', 1); |
|
414 | + } |
|
415 | 415 | } |