@@ -52,606 +52,606 @@ |
||
52 | 52 | use OCP\Util; |
53 | 53 | |
54 | 54 | class User_LDAP extends BackendUtility implements \OCP\IUserBackend, \OCP\UserInterface, IUserLDAP { |
55 | - /** @var \OCP\IConfig */ |
|
56 | - protected $ocConfig; |
|
57 | - |
|
58 | - /** @var INotificationManager */ |
|
59 | - protected $notificationManager; |
|
60 | - |
|
61 | - /** @var string */ |
|
62 | - protected $currentUserInDeletionProcess; |
|
63 | - |
|
64 | - /** @var UserPluginManager */ |
|
65 | - protected $userPluginManager; |
|
66 | - |
|
67 | - /** |
|
68 | - * @param Access $access |
|
69 | - * @param \OCP\IConfig $ocConfig |
|
70 | - * @param \OCP\Notification\IManager $notificationManager |
|
71 | - * @param IUserSession $userSession |
|
72 | - */ |
|
73 | - public function __construct(Access $access, IConfig $ocConfig, INotificationManager $notificationManager, IUserSession $userSession, UserPluginManager $userPluginManager) { |
|
74 | - parent::__construct($access); |
|
75 | - $this->ocConfig = $ocConfig; |
|
76 | - $this->notificationManager = $notificationManager; |
|
77 | - $this->userPluginManager = $userPluginManager; |
|
78 | - $this->registerHooks($userSession); |
|
79 | - } |
|
80 | - |
|
81 | - protected function registerHooks(IUserSession $userSession) { |
|
82 | - $userSession->listen('\OC\User', 'preDelete', [$this, 'preDeleteUser']); |
|
83 | - $userSession->listen('\OC\User', 'postDelete', [$this, 'postDeleteUser']); |
|
84 | - } |
|
85 | - |
|
86 | - public function preDeleteUser(IUser $user) { |
|
87 | - $this->currentUserInDeletionProcess = $user->getUID(); |
|
88 | - } |
|
89 | - |
|
90 | - public function postDeleteUser() { |
|
91 | - $this->currentUserInDeletionProcess = null; |
|
92 | - } |
|
93 | - |
|
94 | - /** |
|
95 | - * checks whether the user is allowed to change his avatar in Nextcloud |
|
96 | - * |
|
97 | - * @param string $uid the Nextcloud user name |
|
98 | - * @return boolean either the user can or cannot |
|
99 | - * @throws \Exception |
|
100 | - */ |
|
101 | - public function canChangeAvatar($uid) { |
|
102 | - if ($this->userPluginManager->implementsActions(Backend::PROVIDE_AVATAR)) { |
|
103 | - return $this->userPluginManager->canChangeAvatar($uid); |
|
104 | - } |
|
105 | - |
|
106 | - if(!$this->implementsActions(Backend::PROVIDE_AVATAR)) { |
|
107 | - return true; |
|
108 | - } |
|
109 | - |
|
110 | - $user = $this->access->userManager->get($uid); |
|
111 | - if(!$user instanceof User) { |
|
112 | - return false; |
|
113 | - } |
|
114 | - $imageData = $user->getAvatarImage(); |
|
115 | - if($imageData === false) { |
|
116 | - return true; |
|
117 | - } |
|
118 | - return !$user->updateAvatar(true); |
|
119 | - } |
|
120 | - |
|
121 | - /** |
|
122 | - * Return the username for the given login name, if available |
|
123 | - * |
|
124 | - * @param string $loginName |
|
125 | - * @return string|false |
|
126 | - * @throws \Exception |
|
127 | - */ |
|
128 | - public function loginName2UserName($loginName) { |
|
129 | - $cacheKey = 'loginName2UserName-' . $loginName; |
|
130 | - $username = $this->access->connection->getFromCache($cacheKey); |
|
131 | - |
|
132 | - if ($username !== null) { |
|
133 | - return $username; |
|
134 | - } |
|
135 | - |
|
136 | - try { |
|
137 | - $ldapRecord = $this->getLDAPUserByLoginName($loginName); |
|
138 | - $user = $this->access->userManager->get($ldapRecord['dn'][0]); |
|
139 | - if ($user === null || $user instanceof OfflineUser) { |
|
140 | - // this path is not really possible, however get() is documented |
|
141 | - // to return User, OfflineUser or null so we are very defensive here. |
|
142 | - $this->access->connection->writeToCache($cacheKey, false); |
|
143 | - return false; |
|
144 | - } |
|
145 | - $username = $user->getUsername(); |
|
146 | - $this->access->connection->writeToCache($cacheKey, $username); |
|
147 | - return $username; |
|
148 | - } catch (NotOnLDAP $e) { |
|
149 | - $this->access->connection->writeToCache($cacheKey, false); |
|
150 | - return false; |
|
151 | - } |
|
152 | - } |
|
55 | + /** @var \OCP\IConfig */ |
|
56 | + protected $ocConfig; |
|
57 | + |
|
58 | + /** @var INotificationManager */ |
|
59 | + protected $notificationManager; |
|
60 | + |
|
61 | + /** @var string */ |
|
62 | + protected $currentUserInDeletionProcess; |
|
63 | + |
|
64 | + /** @var UserPluginManager */ |
|
65 | + protected $userPluginManager; |
|
66 | + |
|
67 | + /** |
|
68 | + * @param Access $access |
|
69 | + * @param \OCP\IConfig $ocConfig |
|
70 | + * @param \OCP\Notification\IManager $notificationManager |
|
71 | + * @param IUserSession $userSession |
|
72 | + */ |
|
73 | + public function __construct(Access $access, IConfig $ocConfig, INotificationManager $notificationManager, IUserSession $userSession, UserPluginManager $userPluginManager) { |
|
74 | + parent::__construct($access); |
|
75 | + $this->ocConfig = $ocConfig; |
|
76 | + $this->notificationManager = $notificationManager; |
|
77 | + $this->userPluginManager = $userPluginManager; |
|
78 | + $this->registerHooks($userSession); |
|
79 | + } |
|
80 | + |
|
81 | + protected function registerHooks(IUserSession $userSession) { |
|
82 | + $userSession->listen('\OC\User', 'preDelete', [$this, 'preDeleteUser']); |
|
83 | + $userSession->listen('\OC\User', 'postDelete', [$this, 'postDeleteUser']); |
|
84 | + } |
|
85 | + |
|
86 | + public function preDeleteUser(IUser $user) { |
|
87 | + $this->currentUserInDeletionProcess = $user->getUID(); |
|
88 | + } |
|
89 | + |
|
90 | + public function postDeleteUser() { |
|
91 | + $this->currentUserInDeletionProcess = null; |
|
92 | + } |
|
93 | + |
|
94 | + /** |
|
95 | + * checks whether the user is allowed to change his avatar in Nextcloud |
|
96 | + * |
|
97 | + * @param string $uid the Nextcloud user name |
|
98 | + * @return boolean either the user can or cannot |
|
99 | + * @throws \Exception |
|
100 | + */ |
|
101 | + public function canChangeAvatar($uid) { |
|
102 | + if ($this->userPluginManager->implementsActions(Backend::PROVIDE_AVATAR)) { |
|
103 | + return $this->userPluginManager->canChangeAvatar($uid); |
|
104 | + } |
|
105 | + |
|
106 | + if(!$this->implementsActions(Backend::PROVIDE_AVATAR)) { |
|
107 | + return true; |
|
108 | + } |
|
109 | + |
|
110 | + $user = $this->access->userManager->get($uid); |
|
111 | + if(!$user instanceof User) { |
|
112 | + return false; |
|
113 | + } |
|
114 | + $imageData = $user->getAvatarImage(); |
|
115 | + if($imageData === false) { |
|
116 | + return true; |
|
117 | + } |
|
118 | + return !$user->updateAvatar(true); |
|
119 | + } |
|
120 | + |
|
121 | + /** |
|
122 | + * Return the username for the given login name, if available |
|
123 | + * |
|
124 | + * @param string $loginName |
|
125 | + * @return string|false |
|
126 | + * @throws \Exception |
|
127 | + */ |
|
128 | + public function loginName2UserName($loginName) { |
|
129 | + $cacheKey = 'loginName2UserName-' . $loginName; |
|
130 | + $username = $this->access->connection->getFromCache($cacheKey); |
|
131 | + |
|
132 | + if ($username !== null) { |
|
133 | + return $username; |
|
134 | + } |
|
135 | + |
|
136 | + try { |
|
137 | + $ldapRecord = $this->getLDAPUserByLoginName($loginName); |
|
138 | + $user = $this->access->userManager->get($ldapRecord['dn'][0]); |
|
139 | + if ($user === null || $user instanceof OfflineUser) { |
|
140 | + // this path is not really possible, however get() is documented |
|
141 | + // to return User, OfflineUser or null so we are very defensive here. |
|
142 | + $this->access->connection->writeToCache($cacheKey, false); |
|
143 | + return false; |
|
144 | + } |
|
145 | + $username = $user->getUsername(); |
|
146 | + $this->access->connection->writeToCache($cacheKey, $username); |
|
147 | + return $username; |
|
148 | + } catch (NotOnLDAP $e) { |
|
149 | + $this->access->connection->writeToCache($cacheKey, false); |
|
150 | + return false; |
|
151 | + } |
|
152 | + } |
|
153 | 153 | |
154 | - /** |
|
155 | - * returns the username for the given LDAP DN, if available |
|
156 | - * |
|
157 | - * @param string $dn |
|
158 | - * @return string|false with the username |
|
159 | - */ |
|
160 | - public function dn2UserName($dn) { |
|
161 | - return $this->access->dn2username($dn); |
|
162 | - } |
|
163 | - |
|
164 | - /** |
|
165 | - * returns an LDAP record based on a given login name |
|
166 | - * |
|
167 | - * @param string $loginName |
|
168 | - * @return array |
|
169 | - * @throws NotOnLDAP |
|
170 | - */ |
|
171 | - public function getLDAPUserByLoginName($loginName) { |
|
172 | - //find out dn of the user name |
|
173 | - $attrs = $this->access->userManager->getAttributes(); |
|
174 | - $users = $this->access->fetchUsersByLoginName($loginName, $attrs); |
|
175 | - if(count($users) < 1) { |
|
176 | - throw new NotOnLDAP('No user available for the given login name on ' . |
|
177 | - $this->access->connection->ldapHost . ':' . $this->access->connection->ldapPort); |
|
178 | - } |
|
179 | - return $users[0]; |
|
180 | - } |
|
181 | - |
|
182 | - /** |
|
183 | - * Check if the password is correct without logging in the user |
|
184 | - * |
|
185 | - * @param string $uid The username |
|
186 | - * @param string $password The password |
|
187 | - * @return false|string |
|
188 | - */ |
|
189 | - public function checkPassword($uid, $password) { |
|
190 | - try { |
|
191 | - $ldapRecord = $this->getLDAPUserByLoginName($uid); |
|
192 | - } catch(NotOnLDAP $e) { |
|
193 | - \OC::$server->getLogger()->logException($e, ['app' => 'user_ldap', 'level' => ILogger::DEBUG]); |
|
194 | - return false; |
|
195 | - } |
|
196 | - $dn = $ldapRecord['dn'][0]; |
|
197 | - $user = $this->access->userManager->get($dn); |
|
198 | - |
|
199 | - if(!$user instanceof User) { |
|
200 | - Util::writeLog('user_ldap', |
|
201 | - 'LDAP Login: Could not get user object for DN ' . $dn . |
|
202 | - '. Maybe the LDAP entry has no set display name attribute?', |
|
203 | - ILogger::WARN); |
|
204 | - return false; |
|
205 | - } |
|
206 | - if($user->getUsername() !== false) { |
|
207 | - //are the credentials OK? |
|
208 | - if(!$this->access->areCredentialsValid($dn, $password)) { |
|
209 | - return false; |
|
210 | - } |
|
211 | - |
|
212 | - $this->access->cacheUserExists($user->getUsername()); |
|
213 | - $user->processAttributes($ldapRecord); |
|
214 | - $user->markLogin(); |
|
215 | - |
|
216 | - return $user->getUsername(); |
|
217 | - } |
|
218 | - |
|
219 | - return false; |
|
220 | - } |
|
221 | - |
|
222 | - /** |
|
223 | - * Set password |
|
224 | - * @param string $uid The username |
|
225 | - * @param string $password The new password |
|
226 | - * @return bool |
|
227 | - */ |
|
228 | - public function setPassword($uid, $password) { |
|
229 | - if ($this->userPluginManager->implementsActions(Backend::SET_PASSWORD)) { |
|
230 | - return $this->userPluginManager->setPassword($uid, $password); |
|
231 | - } |
|
232 | - |
|
233 | - $user = $this->access->userManager->get($uid); |
|
234 | - |
|
235 | - if(!$user instanceof User) { |
|
236 | - throw new \Exception('LDAP setPassword: Could not get user object for uid ' . $uid . |
|
237 | - '. Maybe the LDAP entry has no set display name attribute?'); |
|
238 | - } |
|
239 | - if($user->getUsername() !== false && $this->access->setPassword($user->getDN(), $password)) { |
|
240 | - $ldapDefaultPPolicyDN = $this->access->connection->ldapDefaultPPolicyDN; |
|
241 | - $turnOnPasswordChange = $this->access->connection->turnOnPasswordChange; |
|
242 | - if (!empty($ldapDefaultPPolicyDN) && ((int)$turnOnPasswordChange === 1)) { |
|
243 | - //remove last password expiry warning if any |
|
244 | - $notification = $this->notificationManager->createNotification(); |
|
245 | - $notification->setApp('user_ldap') |
|
246 | - ->setUser($uid) |
|
247 | - ->setObject('pwd_exp_warn', $uid) |
|
248 | - ; |
|
249 | - $this->notificationManager->markProcessed($notification); |
|
250 | - } |
|
251 | - return true; |
|
252 | - } |
|
253 | - |
|
254 | - return false; |
|
255 | - } |
|
256 | - |
|
257 | - /** |
|
258 | - * Get a list of all users |
|
259 | - * |
|
260 | - * @param string $search |
|
261 | - * @param integer $limit |
|
262 | - * @param integer $offset |
|
263 | - * @return string[] an array of all uids |
|
264 | - */ |
|
265 | - public function getUsers($search = '', $limit = 10, $offset = 0) { |
|
266 | - $search = $this->access->escapeFilterPart($search, true); |
|
267 | - $cachekey = 'getUsers-'.$search.'-'.$limit.'-'.$offset; |
|
268 | - |
|
269 | - //check if users are cached, if so return |
|
270 | - $ldap_users = $this->access->connection->getFromCache($cachekey); |
|
271 | - if(!is_null($ldap_users)) { |
|
272 | - return $ldap_users; |
|
273 | - } |
|
274 | - |
|
275 | - // if we'd pass -1 to LDAP search, we'd end up in a Protocol |
|
276 | - // error. With a limit of 0, we get 0 results. So we pass null. |
|
277 | - if($limit <= 0) { |
|
278 | - $limit = null; |
|
279 | - } |
|
280 | - $filter = $this->access->combineFilterWithAnd(array( |
|
281 | - $this->access->connection->ldapUserFilter, |
|
282 | - $this->access->connection->ldapUserDisplayName . '=*', |
|
283 | - $this->access->getFilterPartForUserSearch($search) |
|
284 | - )); |
|
285 | - |
|
286 | - Util::writeLog('user_ldap', |
|
287 | - 'getUsers: Options: search '.$search.' limit '.$limit.' offset '.$offset.' Filter: '.$filter, |
|
288 | - ILogger::DEBUG); |
|
289 | - //do the search and translate results to Nextcloud names |
|
290 | - $ldap_users = $this->access->fetchListOfUsers( |
|
291 | - $filter, |
|
292 | - $this->access->userManager->getAttributes(true), |
|
293 | - $limit, $offset); |
|
294 | - $ldap_users = $this->access->nextcloudUserNames($ldap_users); |
|
295 | - Util::writeLog('user_ldap', 'getUsers: '.count($ldap_users). ' Users found', ILogger::DEBUG); |
|
296 | - |
|
297 | - $this->access->connection->writeToCache($cachekey, $ldap_users); |
|
298 | - return $ldap_users; |
|
299 | - } |
|
300 | - |
|
301 | - /** |
|
302 | - * checks whether a user is still available on LDAP |
|
303 | - * |
|
304 | - * @param string|\OCA\User_LDAP\User\User $user either the Nextcloud user |
|
305 | - * name or an instance of that user |
|
306 | - * @return bool |
|
307 | - * @throws \Exception |
|
308 | - * @throws \OC\ServerNotAvailableException |
|
309 | - */ |
|
310 | - public function userExistsOnLDAP($user) { |
|
311 | - if(is_string($user)) { |
|
312 | - $user = $this->access->userManager->get($user); |
|
313 | - } |
|
314 | - if(is_null($user)) { |
|
315 | - return false; |
|
316 | - } |
|
317 | - |
|
318 | - $dn = $user->getDN(); |
|
319 | - //check if user really still exists by reading its entry |
|
320 | - if(!is_array($this->access->readAttribute($dn, '', $this->access->connection->ldapUserFilter))) { |
|
321 | - try { |
|
322 | - $uuid = $this->access->getUserMapper()->getUUIDByDN($dn); |
|
323 | - if (!$uuid) { |
|
324 | - return false; |
|
325 | - } |
|
326 | - $newDn = $this->access->getUserDnByUuid($uuid); |
|
327 | - //check if renamed user is still valid by reapplying the ldap filter |
|
328 | - if ($newDn === $dn || !is_array($this->access->readAttribute($newDn, '', $this->access->connection->ldapUserFilter))) { |
|
329 | - return false; |
|
330 | - } |
|
331 | - $this->access->getUserMapper()->setDNbyUUID($newDn, $uuid); |
|
332 | - return true; |
|
333 | - } catch (ServerNotAvailableException $e) { |
|
334 | - throw $e; |
|
335 | - } catch (\Exception $e) { |
|
336 | - return false; |
|
337 | - } |
|
338 | - } |
|
339 | - |
|
340 | - if($user instanceof OfflineUser) { |
|
341 | - $user->unmark(); |
|
342 | - } |
|
343 | - |
|
344 | - return true; |
|
345 | - } |
|
346 | - |
|
347 | - /** |
|
348 | - * check if a user exists |
|
349 | - * @param string $uid the username |
|
350 | - * @return boolean |
|
351 | - * @throws \Exception when connection could not be established |
|
352 | - */ |
|
353 | - public function userExists($uid) { |
|
354 | - $userExists = $this->access->connection->getFromCache('userExists'.$uid); |
|
355 | - if(!is_null($userExists)) { |
|
356 | - return (bool)$userExists; |
|
357 | - } |
|
358 | - //getting dn, if false the user does not exist. If dn, he may be mapped only, requires more checking. |
|
359 | - $user = $this->access->userManager->get($uid); |
|
360 | - |
|
361 | - if(is_null($user)) { |
|
362 | - Util::writeLog('user_ldap', 'No DN found for '.$uid.' on '. |
|
363 | - $this->access->connection->ldapHost, ILogger::DEBUG); |
|
364 | - $this->access->connection->writeToCache('userExists'.$uid, false); |
|
365 | - return false; |
|
366 | - } else if($user instanceof OfflineUser) { |
|
367 | - //express check for users marked as deleted. Returning true is |
|
368 | - //necessary for cleanup |
|
369 | - return true; |
|
370 | - } |
|
371 | - |
|
372 | - $result = $this->userExistsOnLDAP($user); |
|
373 | - $this->access->connection->writeToCache('userExists'.$uid, $result); |
|
374 | - return $result; |
|
375 | - } |
|
376 | - |
|
377 | - /** |
|
378 | - * returns whether a user was deleted in LDAP |
|
379 | - * |
|
380 | - * @param string $uid The username of the user to delete |
|
381 | - * @return bool |
|
382 | - */ |
|
383 | - public function deleteUser($uid) { |
|
384 | - if ($this->userPluginManager->canDeleteUser()) { |
|
385 | - $status = $this->userPluginManager->deleteUser($uid); |
|
386 | - if($status === false) { |
|
387 | - return false; |
|
388 | - } |
|
389 | - } |
|
390 | - |
|
391 | - $marked = $this->ocConfig->getUserValue($uid, 'user_ldap', 'isDeleted', 0); |
|
392 | - if((int)$marked === 0) { |
|
393 | - \OC::$server->getLogger()->notice( |
|
394 | - 'User '.$uid . ' is not marked as deleted, not cleaning up.', |
|
395 | - ['app' => 'user_ldap']); |
|
396 | - return false; |
|
397 | - } |
|
398 | - \OC::$server->getLogger()->info('Cleaning up after user ' . $uid, |
|
399 | - ['app' => 'user_ldap']); |
|
400 | - |
|
401 | - $this->access->getUserMapper()->unmap($uid); // we don't emit unassign signals here, since it is implicit to delete signals fired from core |
|
402 | - $this->access->userManager->invalidate($uid); |
|
403 | - return true; |
|
404 | - } |
|
405 | - |
|
406 | - /** |
|
407 | - * get the user's home directory |
|
408 | - * |
|
409 | - * @param string $uid the username |
|
410 | - * @return bool|string |
|
411 | - * @throws NoUserException |
|
412 | - * @throws \Exception |
|
413 | - */ |
|
414 | - public function getHome($uid) { |
|
415 | - // user Exists check required as it is not done in user proxy! |
|
416 | - if(!$this->userExists($uid)) { |
|
417 | - return false; |
|
418 | - } |
|
419 | - |
|
420 | - if ($this->userPluginManager->implementsActions(Backend::GET_HOME)) { |
|
421 | - return $this->userPluginManager->getHome($uid); |
|
422 | - } |
|
423 | - |
|
424 | - $cacheKey = 'getHome'.$uid; |
|
425 | - $path = $this->access->connection->getFromCache($cacheKey); |
|
426 | - if(!is_null($path)) { |
|
427 | - return $path; |
|
428 | - } |
|
429 | - |
|
430 | - // early return path if it is a deleted user |
|
431 | - $user = $this->access->userManager->get($uid); |
|
432 | - if($user instanceof OfflineUser) { |
|
433 | - if($this->currentUserInDeletionProcess !== null |
|
434 | - && $this->currentUserInDeletionProcess === $user->getOCName() |
|
435 | - ) { |
|
436 | - return $user->getHomePath(); |
|
437 | - } else { |
|
438 | - throw new NoUserException($uid . ' is not a valid user anymore'); |
|
439 | - } |
|
440 | - } else if ($user === null) { |
|
441 | - throw new NoUserException($uid . ' is not a valid user anymore'); |
|
442 | - } |
|
443 | - |
|
444 | - $path = $user->getHomePath(); |
|
445 | - $this->access->cacheUserHome($uid, $path); |
|
446 | - |
|
447 | - return $path; |
|
448 | - } |
|
449 | - |
|
450 | - /** |
|
451 | - * get display name of the user |
|
452 | - * @param string $uid user ID of the user |
|
453 | - * @return string|false display name |
|
454 | - */ |
|
455 | - public function getDisplayName($uid) { |
|
456 | - if ($this->userPluginManager->implementsActions(Backend::GET_DISPLAYNAME)) { |
|
457 | - return $this->userPluginManager->getDisplayName($uid); |
|
458 | - } |
|
459 | - |
|
460 | - if(!$this->userExists($uid)) { |
|
461 | - return false; |
|
462 | - } |
|
463 | - |
|
464 | - $cacheKey = 'getDisplayName'.$uid; |
|
465 | - if(!is_null($displayName = $this->access->connection->getFromCache($cacheKey))) { |
|
466 | - return $displayName; |
|
467 | - } |
|
468 | - |
|
469 | - //Check whether the display name is configured to have a 2nd feature |
|
470 | - $additionalAttribute = $this->access->connection->ldapUserDisplayName2; |
|
471 | - $displayName2 = ''; |
|
472 | - if ($additionalAttribute !== '') { |
|
473 | - $displayName2 = $this->access->readAttribute( |
|
474 | - $this->access->username2dn($uid), |
|
475 | - $additionalAttribute); |
|
476 | - } |
|
477 | - |
|
478 | - $displayName = $this->access->readAttribute( |
|
479 | - $this->access->username2dn($uid), |
|
480 | - $this->access->connection->ldapUserDisplayName); |
|
481 | - |
|
482 | - if($displayName && (count($displayName) > 0)) { |
|
483 | - $displayName = $displayName[0]; |
|
484 | - |
|
485 | - if (is_array($displayName2)){ |
|
486 | - $displayName2 = count($displayName2) > 0 ? $displayName2[0] : ''; |
|
487 | - } |
|
488 | - |
|
489 | - $user = $this->access->userManager->get($uid); |
|
490 | - if ($user instanceof User) { |
|
491 | - $displayName = $user->composeAndStoreDisplayName($displayName, $displayName2); |
|
492 | - $this->access->connection->writeToCache($cacheKey, $displayName); |
|
493 | - } |
|
494 | - if ($user instanceof OfflineUser) { |
|
495 | - /** @var OfflineUser $user*/ |
|
496 | - $displayName = $user->getDisplayName(); |
|
497 | - } |
|
498 | - return $displayName; |
|
499 | - } |
|
500 | - |
|
501 | - return null; |
|
502 | - } |
|
503 | - |
|
504 | - /** |
|
505 | - * set display name of the user |
|
506 | - * @param string $uid user ID of the user |
|
507 | - * @param string $displayName new display name of the user |
|
508 | - * @return string|false display name |
|
509 | - */ |
|
510 | - public function setDisplayName($uid, $displayName) { |
|
511 | - if ($this->userPluginManager->implementsActions(Backend::SET_DISPLAYNAME)) { |
|
512 | - $this->userPluginManager->setDisplayName($uid, $displayName); |
|
513 | - $this->access->cacheUserDisplayName($uid, $displayName); |
|
514 | - return $displayName; |
|
515 | - } |
|
516 | - return false; |
|
517 | - } |
|
518 | - |
|
519 | - /** |
|
520 | - * Get a list of all display names |
|
521 | - * |
|
522 | - * @param string $search |
|
523 | - * @param string|null $limit |
|
524 | - * @param string|null $offset |
|
525 | - * @return array an array of all displayNames (value) and the corresponding uids (key) |
|
526 | - */ |
|
527 | - public function getDisplayNames($search = '', $limit = null, $offset = null) { |
|
528 | - $cacheKey = 'getDisplayNames-'.$search.'-'.$limit.'-'.$offset; |
|
529 | - if(!is_null($displayNames = $this->access->connection->getFromCache($cacheKey))) { |
|
530 | - return $displayNames; |
|
531 | - } |
|
532 | - |
|
533 | - $displayNames = array(); |
|
534 | - $users = $this->getUsers($search, $limit, $offset); |
|
535 | - foreach ($users as $user) { |
|
536 | - $displayNames[$user] = $this->getDisplayName($user); |
|
537 | - } |
|
538 | - $this->access->connection->writeToCache($cacheKey, $displayNames); |
|
539 | - return $displayNames; |
|
540 | - } |
|
541 | - |
|
542 | - /** |
|
543 | - * Check if backend implements actions |
|
544 | - * @param int $actions bitwise-or'ed actions |
|
545 | - * @return boolean |
|
546 | - * |
|
547 | - * Returns the supported actions as int to be |
|
548 | - * compared with \OC\User\Backend::CREATE_USER etc. |
|
549 | - */ |
|
550 | - public function implementsActions($actions) { |
|
551 | - return (bool)((Backend::CHECK_PASSWORD |
|
552 | - | Backend::GET_HOME |
|
553 | - | Backend::GET_DISPLAYNAME |
|
554 | - | (($this->access->connection->ldapUserAvatarRule !== 'none') ? Backend::PROVIDE_AVATAR : 0) |
|
555 | - | Backend::COUNT_USERS |
|
556 | - | (((int)$this->access->connection->turnOnPasswordChange === 1)? Backend::SET_PASSWORD :0) |
|
557 | - | $this->userPluginManager->getImplementedActions()) |
|
558 | - & $actions); |
|
559 | - } |
|
560 | - |
|
561 | - /** |
|
562 | - * @return bool |
|
563 | - */ |
|
564 | - public function hasUserListings() { |
|
565 | - return true; |
|
566 | - } |
|
567 | - |
|
568 | - /** |
|
569 | - * counts the users in LDAP |
|
570 | - * |
|
571 | - * @return int|bool |
|
572 | - */ |
|
573 | - public function countUsers() { |
|
574 | - if ($this->userPluginManager->implementsActions(Backend::COUNT_USERS)) { |
|
575 | - return $this->userPluginManager->countUsers(); |
|
576 | - } |
|
577 | - |
|
578 | - $filter = $this->access->getFilterForUserCount(); |
|
579 | - $cacheKey = 'countUsers-'.$filter; |
|
580 | - if(!is_null($entries = $this->access->connection->getFromCache($cacheKey))) { |
|
581 | - return $entries; |
|
582 | - } |
|
583 | - $entries = $this->access->countUsers($filter); |
|
584 | - $this->access->connection->writeToCache($cacheKey, $entries); |
|
585 | - return $entries; |
|
586 | - } |
|
587 | - |
|
588 | - /** |
|
589 | - * Backend name to be shown in user management |
|
590 | - * @return string the name of the backend to be shown |
|
591 | - */ |
|
592 | - public function getBackendName(){ |
|
593 | - return 'LDAP'; |
|
594 | - } |
|
154 | + /** |
|
155 | + * returns the username for the given LDAP DN, if available |
|
156 | + * |
|
157 | + * @param string $dn |
|
158 | + * @return string|false with the username |
|
159 | + */ |
|
160 | + public function dn2UserName($dn) { |
|
161 | + return $this->access->dn2username($dn); |
|
162 | + } |
|
163 | + |
|
164 | + /** |
|
165 | + * returns an LDAP record based on a given login name |
|
166 | + * |
|
167 | + * @param string $loginName |
|
168 | + * @return array |
|
169 | + * @throws NotOnLDAP |
|
170 | + */ |
|
171 | + public function getLDAPUserByLoginName($loginName) { |
|
172 | + //find out dn of the user name |
|
173 | + $attrs = $this->access->userManager->getAttributes(); |
|
174 | + $users = $this->access->fetchUsersByLoginName($loginName, $attrs); |
|
175 | + if(count($users) < 1) { |
|
176 | + throw new NotOnLDAP('No user available for the given login name on ' . |
|
177 | + $this->access->connection->ldapHost . ':' . $this->access->connection->ldapPort); |
|
178 | + } |
|
179 | + return $users[0]; |
|
180 | + } |
|
181 | + |
|
182 | + /** |
|
183 | + * Check if the password is correct without logging in the user |
|
184 | + * |
|
185 | + * @param string $uid The username |
|
186 | + * @param string $password The password |
|
187 | + * @return false|string |
|
188 | + */ |
|
189 | + public function checkPassword($uid, $password) { |
|
190 | + try { |
|
191 | + $ldapRecord = $this->getLDAPUserByLoginName($uid); |
|
192 | + } catch(NotOnLDAP $e) { |
|
193 | + \OC::$server->getLogger()->logException($e, ['app' => 'user_ldap', 'level' => ILogger::DEBUG]); |
|
194 | + return false; |
|
195 | + } |
|
196 | + $dn = $ldapRecord['dn'][0]; |
|
197 | + $user = $this->access->userManager->get($dn); |
|
198 | + |
|
199 | + if(!$user instanceof User) { |
|
200 | + Util::writeLog('user_ldap', |
|
201 | + 'LDAP Login: Could not get user object for DN ' . $dn . |
|
202 | + '. Maybe the LDAP entry has no set display name attribute?', |
|
203 | + ILogger::WARN); |
|
204 | + return false; |
|
205 | + } |
|
206 | + if($user->getUsername() !== false) { |
|
207 | + //are the credentials OK? |
|
208 | + if(!$this->access->areCredentialsValid($dn, $password)) { |
|
209 | + return false; |
|
210 | + } |
|
211 | + |
|
212 | + $this->access->cacheUserExists($user->getUsername()); |
|
213 | + $user->processAttributes($ldapRecord); |
|
214 | + $user->markLogin(); |
|
215 | + |
|
216 | + return $user->getUsername(); |
|
217 | + } |
|
218 | + |
|
219 | + return false; |
|
220 | + } |
|
221 | + |
|
222 | + /** |
|
223 | + * Set password |
|
224 | + * @param string $uid The username |
|
225 | + * @param string $password The new password |
|
226 | + * @return bool |
|
227 | + */ |
|
228 | + public function setPassword($uid, $password) { |
|
229 | + if ($this->userPluginManager->implementsActions(Backend::SET_PASSWORD)) { |
|
230 | + return $this->userPluginManager->setPassword($uid, $password); |
|
231 | + } |
|
232 | + |
|
233 | + $user = $this->access->userManager->get($uid); |
|
234 | + |
|
235 | + if(!$user instanceof User) { |
|
236 | + throw new \Exception('LDAP setPassword: Could not get user object for uid ' . $uid . |
|
237 | + '. Maybe the LDAP entry has no set display name attribute?'); |
|
238 | + } |
|
239 | + if($user->getUsername() !== false && $this->access->setPassword($user->getDN(), $password)) { |
|
240 | + $ldapDefaultPPolicyDN = $this->access->connection->ldapDefaultPPolicyDN; |
|
241 | + $turnOnPasswordChange = $this->access->connection->turnOnPasswordChange; |
|
242 | + if (!empty($ldapDefaultPPolicyDN) && ((int)$turnOnPasswordChange === 1)) { |
|
243 | + //remove last password expiry warning if any |
|
244 | + $notification = $this->notificationManager->createNotification(); |
|
245 | + $notification->setApp('user_ldap') |
|
246 | + ->setUser($uid) |
|
247 | + ->setObject('pwd_exp_warn', $uid) |
|
248 | + ; |
|
249 | + $this->notificationManager->markProcessed($notification); |
|
250 | + } |
|
251 | + return true; |
|
252 | + } |
|
253 | + |
|
254 | + return false; |
|
255 | + } |
|
256 | + |
|
257 | + /** |
|
258 | + * Get a list of all users |
|
259 | + * |
|
260 | + * @param string $search |
|
261 | + * @param integer $limit |
|
262 | + * @param integer $offset |
|
263 | + * @return string[] an array of all uids |
|
264 | + */ |
|
265 | + public function getUsers($search = '', $limit = 10, $offset = 0) { |
|
266 | + $search = $this->access->escapeFilterPart($search, true); |
|
267 | + $cachekey = 'getUsers-'.$search.'-'.$limit.'-'.$offset; |
|
268 | + |
|
269 | + //check if users are cached, if so return |
|
270 | + $ldap_users = $this->access->connection->getFromCache($cachekey); |
|
271 | + if(!is_null($ldap_users)) { |
|
272 | + return $ldap_users; |
|
273 | + } |
|
274 | + |
|
275 | + // if we'd pass -1 to LDAP search, we'd end up in a Protocol |
|
276 | + // error. With a limit of 0, we get 0 results. So we pass null. |
|
277 | + if($limit <= 0) { |
|
278 | + $limit = null; |
|
279 | + } |
|
280 | + $filter = $this->access->combineFilterWithAnd(array( |
|
281 | + $this->access->connection->ldapUserFilter, |
|
282 | + $this->access->connection->ldapUserDisplayName . '=*', |
|
283 | + $this->access->getFilterPartForUserSearch($search) |
|
284 | + )); |
|
285 | + |
|
286 | + Util::writeLog('user_ldap', |
|
287 | + 'getUsers: Options: search '.$search.' limit '.$limit.' offset '.$offset.' Filter: '.$filter, |
|
288 | + ILogger::DEBUG); |
|
289 | + //do the search and translate results to Nextcloud names |
|
290 | + $ldap_users = $this->access->fetchListOfUsers( |
|
291 | + $filter, |
|
292 | + $this->access->userManager->getAttributes(true), |
|
293 | + $limit, $offset); |
|
294 | + $ldap_users = $this->access->nextcloudUserNames($ldap_users); |
|
295 | + Util::writeLog('user_ldap', 'getUsers: '.count($ldap_users). ' Users found', ILogger::DEBUG); |
|
296 | + |
|
297 | + $this->access->connection->writeToCache($cachekey, $ldap_users); |
|
298 | + return $ldap_users; |
|
299 | + } |
|
300 | + |
|
301 | + /** |
|
302 | + * checks whether a user is still available on LDAP |
|
303 | + * |
|
304 | + * @param string|\OCA\User_LDAP\User\User $user either the Nextcloud user |
|
305 | + * name or an instance of that user |
|
306 | + * @return bool |
|
307 | + * @throws \Exception |
|
308 | + * @throws \OC\ServerNotAvailableException |
|
309 | + */ |
|
310 | + public function userExistsOnLDAP($user) { |
|
311 | + if(is_string($user)) { |
|
312 | + $user = $this->access->userManager->get($user); |
|
313 | + } |
|
314 | + if(is_null($user)) { |
|
315 | + return false; |
|
316 | + } |
|
317 | + |
|
318 | + $dn = $user->getDN(); |
|
319 | + //check if user really still exists by reading its entry |
|
320 | + if(!is_array($this->access->readAttribute($dn, '', $this->access->connection->ldapUserFilter))) { |
|
321 | + try { |
|
322 | + $uuid = $this->access->getUserMapper()->getUUIDByDN($dn); |
|
323 | + if (!$uuid) { |
|
324 | + return false; |
|
325 | + } |
|
326 | + $newDn = $this->access->getUserDnByUuid($uuid); |
|
327 | + //check if renamed user is still valid by reapplying the ldap filter |
|
328 | + if ($newDn === $dn || !is_array($this->access->readAttribute($newDn, '', $this->access->connection->ldapUserFilter))) { |
|
329 | + return false; |
|
330 | + } |
|
331 | + $this->access->getUserMapper()->setDNbyUUID($newDn, $uuid); |
|
332 | + return true; |
|
333 | + } catch (ServerNotAvailableException $e) { |
|
334 | + throw $e; |
|
335 | + } catch (\Exception $e) { |
|
336 | + return false; |
|
337 | + } |
|
338 | + } |
|
339 | + |
|
340 | + if($user instanceof OfflineUser) { |
|
341 | + $user->unmark(); |
|
342 | + } |
|
343 | + |
|
344 | + return true; |
|
345 | + } |
|
346 | + |
|
347 | + /** |
|
348 | + * check if a user exists |
|
349 | + * @param string $uid the username |
|
350 | + * @return boolean |
|
351 | + * @throws \Exception when connection could not be established |
|
352 | + */ |
|
353 | + public function userExists($uid) { |
|
354 | + $userExists = $this->access->connection->getFromCache('userExists'.$uid); |
|
355 | + if(!is_null($userExists)) { |
|
356 | + return (bool)$userExists; |
|
357 | + } |
|
358 | + //getting dn, if false the user does not exist. If dn, he may be mapped only, requires more checking. |
|
359 | + $user = $this->access->userManager->get($uid); |
|
360 | + |
|
361 | + if(is_null($user)) { |
|
362 | + Util::writeLog('user_ldap', 'No DN found for '.$uid.' on '. |
|
363 | + $this->access->connection->ldapHost, ILogger::DEBUG); |
|
364 | + $this->access->connection->writeToCache('userExists'.$uid, false); |
|
365 | + return false; |
|
366 | + } else if($user instanceof OfflineUser) { |
|
367 | + //express check for users marked as deleted. Returning true is |
|
368 | + //necessary for cleanup |
|
369 | + return true; |
|
370 | + } |
|
371 | + |
|
372 | + $result = $this->userExistsOnLDAP($user); |
|
373 | + $this->access->connection->writeToCache('userExists'.$uid, $result); |
|
374 | + return $result; |
|
375 | + } |
|
376 | + |
|
377 | + /** |
|
378 | + * returns whether a user was deleted in LDAP |
|
379 | + * |
|
380 | + * @param string $uid The username of the user to delete |
|
381 | + * @return bool |
|
382 | + */ |
|
383 | + public function deleteUser($uid) { |
|
384 | + if ($this->userPluginManager->canDeleteUser()) { |
|
385 | + $status = $this->userPluginManager->deleteUser($uid); |
|
386 | + if($status === false) { |
|
387 | + return false; |
|
388 | + } |
|
389 | + } |
|
390 | + |
|
391 | + $marked = $this->ocConfig->getUserValue($uid, 'user_ldap', 'isDeleted', 0); |
|
392 | + if((int)$marked === 0) { |
|
393 | + \OC::$server->getLogger()->notice( |
|
394 | + 'User '.$uid . ' is not marked as deleted, not cleaning up.', |
|
395 | + ['app' => 'user_ldap']); |
|
396 | + return false; |
|
397 | + } |
|
398 | + \OC::$server->getLogger()->info('Cleaning up after user ' . $uid, |
|
399 | + ['app' => 'user_ldap']); |
|
400 | + |
|
401 | + $this->access->getUserMapper()->unmap($uid); // we don't emit unassign signals here, since it is implicit to delete signals fired from core |
|
402 | + $this->access->userManager->invalidate($uid); |
|
403 | + return true; |
|
404 | + } |
|
405 | + |
|
406 | + /** |
|
407 | + * get the user's home directory |
|
408 | + * |
|
409 | + * @param string $uid the username |
|
410 | + * @return bool|string |
|
411 | + * @throws NoUserException |
|
412 | + * @throws \Exception |
|
413 | + */ |
|
414 | + public function getHome($uid) { |
|
415 | + // user Exists check required as it is not done in user proxy! |
|
416 | + if(!$this->userExists($uid)) { |
|
417 | + return false; |
|
418 | + } |
|
419 | + |
|
420 | + if ($this->userPluginManager->implementsActions(Backend::GET_HOME)) { |
|
421 | + return $this->userPluginManager->getHome($uid); |
|
422 | + } |
|
423 | + |
|
424 | + $cacheKey = 'getHome'.$uid; |
|
425 | + $path = $this->access->connection->getFromCache($cacheKey); |
|
426 | + if(!is_null($path)) { |
|
427 | + return $path; |
|
428 | + } |
|
429 | + |
|
430 | + // early return path if it is a deleted user |
|
431 | + $user = $this->access->userManager->get($uid); |
|
432 | + if($user instanceof OfflineUser) { |
|
433 | + if($this->currentUserInDeletionProcess !== null |
|
434 | + && $this->currentUserInDeletionProcess === $user->getOCName() |
|
435 | + ) { |
|
436 | + return $user->getHomePath(); |
|
437 | + } else { |
|
438 | + throw new NoUserException($uid . ' is not a valid user anymore'); |
|
439 | + } |
|
440 | + } else if ($user === null) { |
|
441 | + throw new NoUserException($uid . ' is not a valid user anymore'); |
|
442 | + } |
|
443 | + |
|
444 | + $path = $user->getHomePath(); |
|
445 | + $this->access->cacheUserHome($uid, $path); |
|
446 | + |
|
447 | + return $path; |
|
448 | + } |
|
449 | + |
|
450 | + /** |
|
451 | + * get display name of the user |
|
452 | + * @param string $uid user ID of the user |
|
453 | + * @return string|false display name |
|
454 | + */ |
|
455 | + public function getDisplayName($uid) { |
|
456 | + if ($this->userPluginManager->implementsActions(Backend::GET_DISPLAYNAME)) { |
|
457 | + return $this->userPluginManager->getDisplayName($uid); |
|
458 | + } |
|
459 | + |
|
460 | + if(!$this->userExists($uid)) { |
|
461 | + return false; |
|
462 | + } |
|
463 | + |
|
464 | + $cacheKey = 'getDisplayName'.$uid; |
|
465 | + if(!is_null($displayName = $this->access->connection->getFromCache($cacheKey))) { |
|
466 | + return $displayName; |
|
467 | + } |
|
468 | + |
|
469 | + //Check whether the display name is configured to have a 2nd feature |
|
470 | + $additionalAttribute = $this->access->connection->ldapUserDisplayName2; |
|
471 | + $displayName2 = ''; |
|
472 | + if ($additionalAttribute !== '') { |
|
473 | + $displayName2 = $this->access->readAttribute( |
|
474 | + $this->access->username2dn($uid), |
|
475 | + $additionalAttribute); |
|
476 | + } |
|
477 | + |
|
478 | + $displayName = $this->access->readAttribute( |
|
479 | + $this->access->username2dn($uid), |
|
480 | + $this->access->connection->ldapUserDisplayName); |
|
481 | + |
|
482 | + if($displayName && (count($displayName) > 0)) { |
|
483 | + $displayName = $displayName[0]; |
|
484 | + |
|
485 | + if (is_array($displayName2)){ |
|
486 | + $displayName2 = count($displayName2) > 0 ? $displayName2[0] : ''; |
|
487 | + } |
|
488 | + |
|
489 | + $user = $this->access->userManager->get($uid); |
|
490 | + if ($user instanceof User) { |
|
491 | + $displayName = $user->composeAndStoreDisplayName($displayName, $displayName2); |
|
492 | + $this->access->connection->writeToCache($cacheKey, $displayName); |
|
493 | + } |
|
494 | + if ($user instanceof OfflineUser) { |
|
495 | + /** @var OfflineUser $user*/ |
|
496 | + $displayName = $user->getDisplayName(); |
|
497 | + } |
|
498 | + return $displayName; |
|
499 | + } |
|
500 | + |
|
501 | + return null; |
|
502 | + } |
|
503 | + |
|
504 | + /** |
|
505 | + * set display name of the user |
|
506 | + * @param string $uid user ID of the user |
|
507 | + * @param string $displayName new display name of the user |
|
508 | + * @return string|false display name |
|
509 | + */ |
|
510 | + public function setDisplayName($uid, $displayName) { |
|
511 | + if ($this->userPluginManager->implementsActions(Backend::SET_DISPLAYNAME)) { |
|
512 | + $this->userPluginManager->setDisplayName($uid, $displayName); |
|
513 | + $this->access->cacheUserDisplayName($uid, $displayName); |
|
514 | + return $displayName; |
|
515 | + } |
|
516 | + return false; |
|
517 | + } |
|
518 | + |
|
519 | + /** |
|
520 | + * Get a list of all display names |
|
521 | + * |
|
522 | + * @param string $search |
|
523 | + * @param string|null $limit |
|
524 | + * @param string|null $offset |
|
525 | + * @return array an array of all displayNames (value) and the corresponding uids (key) |
|
526 | + */ |
|
527 | + public function getDisplayNames($search = '', $limit = null, $offset = null) { |
|
528 | + $cacheKey = 'getDisplayNames-'.$search.'-'.$limit.'-'.$offset; |
|
529 | + if(!is_null($displayNames = $this->access->connection->getFromCache($cacheKey))) { |
|
530 | + return $displayNames; |
|
531 | + } |
|
532 | + |
|
533 | + $displayNames = array(); |
|
534 | + $users = $this->getUsers($search, $limit, $offset); |
|
535 | + foreach ($users as $user) { |
|
536 | + $displayNames[$user] = $this->getDisplayName($user); |
|
537 | + } |
|
538 | + $this->access->connection->writeToCache($cacheKey, $displayNames); |
|
539 | + return $displayNames; |
|
540 | + } |
|
541 | + |
|
542 | + /** |
|
543 | + * Check if backend implements actions |
|
544 | + * @param int $actions bitwise-or'ed actions |
|
545 | + * @return boolean |
|
546 | + * |
|
547 | + * Returns the supported actions as int to be |
|
548 | + * compared with \OC\User\Backend::CREATE_USER etc. |
|
549 | + */ |
|
550 | + public function implementsActions($actions) { |
|
551 | + return (bool)((Backend::CHECK_PASSWORD |
|
552 | + | Backend::GET_HOME |
|
553 | + | Backend::GET_DISPLAYNAME |
|
554 | + | (($this->access->connection->ldapUserAvatarRule !== 'none') ? Backend::PROVIDE_AVATAR : 0) |
|
555 | + | Backend::COUNT_USERS |
|
556 | + | (((int)$this->access->connection->turnOnPasswordChange === 1)? Backend::SET_PASSWORD :0) |
|
557 | + | $this->userPluginManager->getImplementedActions()) |
|
558 | + & $actions); |
|
559 | + } |
|
560 | + |
|
561 | + /** |
|
562 | + * @return bool |
|
563 | + */ |
|
564 | + public function hasUserListings() { |
|
565 | + return true; |
|
566 | + } |
|
567 | + |
|
568 | + /** |
|
569 | + * counts the users in LDAP |
|
570 | + * |
|
571 | + * @return int|bool |
|
572 | + */ |
|
573 | + public function countUsers() { |
|
574 | + if ($this->userPluginManager->implementsActions(Backend::COUNT_USERS)) { |
|
575 | + return $this->userPluginManager->countUsers(); |
|
576 | + } |
|
577 | + |
|
578 | + $filter = $this->access->getFilterForUserCount(); |
|
579 | + $cacheKey = 'countUsers-'.$filter; |
|
580 | + if(!is_null($entries = $this->access->connection->getFromCache($cacheKey))) { |
|
581 | + return $entries; |
|
582 | + } |
|
583 | + $entries = $this->access->countUsers($filter); |
|
584 | + $this->access->connection->writeToCache($cacheKey, $entries); |
|
585 | + return $entries; |
|
586 | + } |
|
587 | + |
|
588 | + /** |
|
589 | + * Backend name to be shown in user management |
|
590 | + * @return string the name of the backend to be shown |
|
591 | + */ |
|
592 | + public function getBackendName(){ |
|
593 | + return 'LDAP'; |
|
594 | + } |
|
595 | 595 | |
596 | - /** |
|
597 | - * Return access for LDAP interaction. |
|
598 | - * @param string $uid |
|
599 | - * @return Access instance of Access for LDAP interaction |
|
600 | - */ |
|
601 | - public function getLDAPAccess($uid) { |
|
602 | - return $this->access; |
|
603 | - } |
|
596 | + /** |
|
597 | + * Return access for LDAP interaction. |
|
598 | + * @param string $uid |
|
599 | + * @return Access instance of Access for LDAP interaction |
|
600 | + */ |
|
601 | + public function getLDAPAccess($uid) { |
|
602 | + return $this->access; |
|
603 | + } |
|
604 | 604 | |
605 | - /** |
|
606 | - * Return LDAP connection resource from a cloned connection. |
|
607 | - * The cloned connection needs to be closed manually. |
|
608 | - * of the current access. |
|
609 | - * @param string $uid |
|
610 | - * @return resource of the LDAP connection |
|
611 | - */ |
|
612 | - public function getNewLDAPConnection($uid) { |
|
613 | - $connection = clone $this->access->getConnection(); |
|
614 | - return $connection->getConnectionResource(); |
|
615 | - } |
|
616 | - |
|
617 | - /** |
|
618 | - * create new user |
|
619 | - * @param string $username username of the new user |
|
620 | - * @param string $password password of the new user |
|
621 | - * @throws \UnexpectedValueException |
|
622 | - * @return bool |
|
623 | - */ |
|
624 | - public function createUser($username, $password) { |
|
625 | - if ($this->userPluginManager->implementsActions(Backend::CREATE_USER)) { |
|
626 | - if ($dn = $this->userPluginManager->createUser($username, $password)) { |
|
627 | - if (is_string($dn)) { |
|
628 | - // the NC user creation work flow requires a know user id up front |
|
629 | - $uuid = $this->access->getUUID($dn, true); |
|
630 | - if(is_string($uuid)) { |
|
631 | - $this->access->mapAndAnnounceIfApplicable( |
|
632 | - $this->access->getUserMapper(), |
|
633 | - $dn, |
|
634 | - $username, |
|
635 | - $uuid, |
|
636 | - true |
|
637 | - ); |
|
638 | - $this->access->cacheUserExists($username); |
|
639 | - } else { |
|
640 | - \OC::$server->getLogger()->warning( |
|
641 | - 'Failed to map created LDAP user with userid {userid}, because UUID could not be determined', |
|
642 | - [ |
|
643 | - 'app' => 'user_ldap', |
|
644 | - 'userid' => $username, |
|
645 | - ] |
|
646 | - ); |
|
647 | - } |
|
648 | - } else { |
|
649 | - throw new \UnexpectedValueException("LDAP Plugin: Method createUser changed to return the user DN instead of boolean."); |
|
650 | - } |
|
651 | - } |
|
652 | - return (bool) $dn; |
|
653 | - } |
|
654 | - return false; |
|
655 | - } |
|
605 | + /** |
|
606 | + * Return LDAP connection resource from a cloned connection. |
|
607 | + * The cloned connection needs to be closed manually. |
|
608 | + * of the current access. |
|
609 | + * @param string $uid |
|
610 | + * @return resource of the LDAP connection |
|
611 | + */ |
|
612 | + public function getNewLDAPConnection($uid) { |
|
613 | + $connection = clone $this->access->getConnection(); |
|
614 | + return $connection->getConnectionResource(); |
|
615 | + } |
|
616 | + |
|
617 | + /** |
|
618 | + * create new user |
|
619 | + * @param string $username username of the new user |
|
620 | + * @param string $password password of the new user |
|
621 | + * @throws \UnexpectedValueException |
|
622 | + * @return bool |
|
623 | + */ |
|
624 | + public function createUser($username, $password) { |
|
625 | + if ($this->userPluginManager->implementsActions(Backend::CREATE_USER)) { |
|
626 | + if ($dn = $this->userPluginManager->createUser($username, $password)) { |
|
627 | + if (is_string($dn)) { |
|
628 | + // the NC user creation work flow requires a know user id up front |
|
629 | + $uuid = $this->access->getUUID($dn, true); |
|
630 | + if(is_string($uuid)) { |
|
631 | + $this->access->mapAndAnnounceIfApplicable( |
|
632 | + $this->access->getUserMapper(), |
|
633 | + $dn, |
|
634 | + $username, |
|
635 | + $uuid, |
|
636 | + true |
|
637 | + ); |
|
638 | + $this->access->cacheUserExists($username); |
|
639 | + } else { |
|
640 | + \OC::$server->getLogger()->warning( |
|
641 | + 'Failed to map created LDAP user with userid {userid}, because UUID could not be determined', |
|
642 | + [ |
|
643 | + 'app' => 'user_ldap', |
|
644 | + 'userid' => $username, |
|
645 | + ] |
|
646 | + ); |
|
647 | + } |
|
648 | + } else { |
|
649 | + throw new \UnexpectedValueException("LDAP Plugin: Method createUser changed to return the user DN instead of boolean."); |
|
650 | + } |
|
651 | + } |
|
652 | + return (bool) $dn; |
|
653 | + } |
|
654 | + return false; |
|
655 | + } |
|
656 | 656 | |
657 | 657 | } |
@@ -103,16 +103,16 @@ discard block |
||
103 | 103 | return $this->userPluginManager->canChangeAvatar($uid); |
104 | 104 | } |
105 | 105 | |
106 | - if(!$this->implementsActions(Backend::PROVIDE_AVATAR)) { |
|
106 | + if (!$this->implementsActions(Backend::PROVIDE_AVATAR)) { |
|
107 | 107 | return true; |
108 | 108 | } |
109 | 109 | |
110 | 110 | $user = $this->access->userManager->get($uid); |
111 | - if(!$user instanceof User) { |
|
111 | + if (!$user instanceof User) { |
|
112 | 112 | return false; |
113 | 113 | } |
114 | 114 | $imageData = $user->getAvatarImage(); |
115 | - if($imageData === false) { |
|
115 | + if ($imageData === false) { |
|
116 | 116 | return true; |
117 | 117 | } |
118 | 118 | return !$user->updateAvatar(true); |
@@ -126,7 +126,7 @@ discard block |
||
126 | 126 | * @throws \Exception |
127 | 127 | */ |
128 | 128 | public function loginName2UserName($loginName) { |
129 | - $cacheKey = 'loginName2UserName-' . $loginName; |
|
129 | + $cacheKey = 'loginName2UserName-'.$loginName; |
|
130 | 130 | $username = $this->access->connection->getFromCache($cacheKey); |
131 | 131 | |
132 | 132 | if ($username !== null) { |
@@ -172,9 +172,9 @@ discard block |
||
172 | 172 | //find out dn of the user name |
173 | 173 | $attrs = $this->access->userManager->getAttributes(); |
174 | 174 | $users = $this->access->fetchUsersByLoginName($loginName, $attrs); |
175 | - if(count($users) < 1) { |
|
176 | - throw new NotOnLDAP('No user available for the given login name on ' . |
|
177 | - $this->access->connection->ldapHost . ':' . $this->access->connection->ldapPort); |
|
175 | + if (count($users) < 1) { |
|
176 | + throw new NotOnLDAP('No user available for the given login name on '. |
|
177 | + $this->access->connection->ldapHost.':'.$this->access->connection->ldapPort); |
|
178 | 178 | } |
179 | 179 | return $users[0]; |
180 | 180 | } |
@@ -189,23 +189,23 @@ discard block |
||
189 | 189 | public function checkPassword($uid, $password) { |
190 | 190 | try { |
191 | 191 | $ldapRecord = $this->getLDAPUserByLoginName($uid); |
192 | - } catch(NotOnLDAP $e) { |
|
192 | + } catch (NotOnLDAP $e) { |
|
193 | 193 | \OC::$server->getLogger()->logException($e, ['app' => 'user_ldap', 'level' => ILogger::DEBUG]); |
194 | 194 | return false; |
195 | 195 | } |
196 | 196 | $dn = $ldapRecord['dn'][0]; |
197 | 197 | $user = $this->access->userManager->get($dn); |
198 | 198 | |
199 | - if(!$user instanceof User) { |
|
199 | + if (!$user instanceof User) { |
|
200 | 200 | Util::writeLog('user_ldap', |
201 | - 'LDAP Login: Could not get user object for DN ' . $dn . |
|
201 | + 'LDAP Login: Could not get user object for DN '.$dn. |
|
202 | 202 | '. Maybe the LDAP entry has no set display name attribute?', |
203 | 203 | ILogger::WARN); |
204 | 204 | return false; |
205 | 205 | } |
206 | - if($user->getUsername() !== false) { |
|
206 | + if ($user->getUsername() !== false) { |
|
207 | 207 | //are the credentials OK? |
208 | - if(!$this->access->areCredentialsValid($dn, $password)) { |
|
208 | + if (!$this->access->areCredentialsValid($dn, $password)) { |
|
209 | 209 | return false; |
210 | 210 | } |
211 | 211 | |
@@ -232,14 +232,14 @@ discard block |
||
232 | 232 | |
233 | 233 | $user = $this->access->userManager->get($uid); |
234 | 234 | |
235 | - if(!$user instanceof User) { |
|
236 | - throw new \Exception('LDAP setPassword: Could not get user object for uid ' . $uid . |
|
235 | + if (!$user instanceof User) { |
|
236 | + throw new \Exception('LDAP setPassword: Could not get user object for uid '.$uid. |
|
237 | 237 | '. Maybe the LDAP entry has no set display name attribute?'); |
238 | 238 | } |
239 | - if($user->getUsername() !== false && $this->access->setPassword($user->getDN(), $password)) { |
|
239 | + if ($user->getUsername() !== false && $this->access->setPassword($user->getDN(), $password)) { |
|
240 | 240 | $ldapDefaultPPolicyDN = $this->access->connection->ldapDefaultPPolicyDN; |
241 | 241 | $turnOnPasswordChange = $this->access->connection->turnOnPasswordChange; |
242 | - if (!empty($ldapDefaultPPolicyDN) && ((int)$turnOnPasswordChange === 1)) { |
|
242 | + if (!empty($ldapDefaultPPolicyDN) && ((int) $turnOnPasswordChange === 1)) { |
|
243 | 243 | //remove last password expiry warning if any |
244 | 244 | $notification = $this->notificationManager->createNotification(); |
245 | 245 | $notification->setApp('user_ldap') |
@@ -268,18 +268,18 @@ discard block |
||
268 | 268 | |
269 | 269 | //check if users are cached, if so return |
270 | 270 | $ldap_users = $this->access->connection->getFromCache($cachekey); |
271 | - if(!is_null($ldap_users)) { |
|
271 | + if (!is_null($ldap_users)) { |
|
272 | 272 | return $ldap_users; |
273 | 273 | } |
274 | 274 | |
275 | 275 | // if we'd pass -1 to LDAP search, we'd end up in a Protocol |
276 | 276 | // error. With a limit of 0, we get 0 results. So we pass null. |
277 | - if($limit <= 0) { |
|
277 | + if ($limit <= 0) { |
|
278 | 278 | $limit = null; |
279 | 279 | } |
280 | 280 | $filter = $this->access->combineFilterWithAnd(array( |
281 | 281 | $this->access->connection->ldapUserFilter, |
282 | - $this->access->connection->ldapUserDisplayName . '=*', |
|
282 | + $this->access->connection->ldapUserDisplayName.'=*', |
|
283 | 283 | $this->access->getFilterPartForUserSearch($search) |
284 | 284 | )); |
285 | 285 | |
@@ -292,7 +292,7 @@ discard block |
||
292 | 292 | $this->access->userManager->getAttributes(true), |
293 | 293 | $limit, $offset); |
294 | 294 | $ldap_users = $this->access->nextcloudUserNames($ldap_users); |
295 | - Util::writeLog('user_ldap', 'getUsers: '.count($ldap_users). ' Users found', ILogger::DEBUG); |
|
295 | + Util::writeLog('user_ldap', 'getUsers: '.count($ldap_users).' Users found', ILogger::DEBUG); |
|
296 | 296 | |
297 | 297 | $this->access->connection->writeToCache($cachekey, $ldap_users); |
298 | 298 | return $ldap_users; |
@@ -308,16 +308,16 @@ discard block |
||
308 | 308 | * @throws \OC\ServerNotAvailableException |
309 | 309 | */ |
310 | 310 | public function userExistsOnLDAP($user) { |
311 | - if(is_string($user)) { |
|
311 | + if (is_string($user)) { |
|
312 | 312 | $user = $this->access->userManager->get($user); |
313 | 313 | } |
314 | - if(is_null($user)) { |
|
314 | + if (is_null($user)) { |
|
315 | 315 | return false; |
316 | 316 | } |
317 | 317 | |
318 | 318 | $dn = $user->getDN(); |
319 | 319 | //check if user really still exists by reading its entry |
320 | - if(!is_array($this->access->readAttribute($dn, '', $this->access->connection->ldapUserFilter))) { |
|
320 | + if (!is_array($this->access->readAttribute($dn, '', $this->access->connection->ldapUserFilter))) { |
|
321 | 321 | try { |
322 | 322 | $uuid = $this->access->getUserMapper()->getUUIDByDN($dn); |
323 | 323 | if (!$uuid) { |
@@ -337,7 +337,7 @@ discard block |
||
337 | 337 | } |
338 | 338 | } |
339 | 339 | |
340 | - if($user instanceof OfflineUser) { |
|
340 | + if ($user instanceof OfflineUser) { |
|
341 | 341 | $user->unmark(); |
342 | 342 | } |
343 | 343 | |
@@ -352,18 +352,18 @@ discard block |
||
352 | 352 | */ |
353 | 353 | public function userExists($uid) { |
354 | 354 | $userExists = $this->access->connection->getFromCache('userExists'.$uid); |
355 | - if(!is_null($userExists)) { |
|
356 | - return (bool)$userExists; |
|
355 | + if (!is_null($userExists)) { |
|
356 | + return (bool) $userExists; |
|
357 | 357 | } |
358 | 358 | //getting dn, if false the user does not exist. If dn, he may be mapped only, requires more checking. |
359 | 359 | $user = $this->access->userManager->get($uid); |
360 | 360 | |
361 | - if(is_null($user)) { |
|
361 | + if (is_null($user)) { |
|
362 | 362 | Util::writeLog('user_ldap', 'No DN found for '.$uid.' on '. |
363 | 363 | $this->access->connection->ldapHost, ILogger::DEBUG); |
364 | 364 | $this->access->connection->writeToCache('userExists'.$uid, false); |
365 | 365 | return false; |
366 | - } else if($user instanceof OfflineUser) { |
|
366 | + } else if ($user instanceof OfflineUser) { |
|
367 | 367 | //express check for users marked as deleted. Returning true is |
368 | 368 | //necessary for cleanup |
369 | 369 | return true; |
@@ -383,19 +383,19 @@ discard block |
||
383 | 383 | public function deleteUser($uid) { |
384 | 384 | if ($this->userPluginManager->canDeleteUser()) { |
385 | 385 | $status = $this->userPluginManager->deleteUser($uid); |
386 | - if($status === false) { |
|
386 | + if ($status === false) { |
|
387 | 387 | return false; |
388 | 388 | } |
389 | 389 | } |
390 | 390 | |
391 | 391 | $marked = $this->ocConfig->getUserValue($uid, 'user_ldap', 'isDeleted', 0); |
392 | - if((int)$marked === 0) { |
|
392 | + if ((int) $marked === 0) { |
|
393 | 393 | \OC::$server->getLogger()->notice( |
394 | - 'User '.$uid . ' is not marked as deleted, not cleaning up.', |
|
394 | + 'User '.$uid.' is not marked as deleted, not cleaning up.', |
|
395 | 395 | ['app' => 'user_ldap']); |
396 | 396 | return false; |
397 | 397 | } |
398 | - \OC::$server->getLogger()->info('Cleaning up after user ' . $uid, |
|
398 | + \OC::$server->getLogger()->info('Cleaning up after user '.$uid, |
|
399 | 399 | ['app' => 'user_ldap']); |
400 | 400 | |
401 | 401 | $this->access->getUserMapper()->unmap($uid); // we don't emit unassign signals here, since it is implicit to delete signals fired from core |
@@ -413,7 +413,7 @@ discard block |
||
413 | 413 | */ |
414 | 414 | public function getHome($uid) { |
415 | 415 | // user Exists check required as it is not done in user proxy! |
416 | - if(!$this->userExists($uid)) { |
|
416 | + if (!$this->userExists($uid)) { |
|
417 | 417 | return false; |
418 | 418 | } |
419 | 419 | |
@@ -423,22 +423,22 @@ discard block |
||
423 | 423 | |
424 | 424 | $cacheKey = 'getHome'.$uid; |
425 | 425 | $path = $this->access->connection->getFromCache($cacheKey); |
426 | - if(!is_null($path)) { |
|
426 | + if (!is_null($path)) { |
|
427 | 427 | return $path; |
428 | 428 | } |
429 | 429 | |
430 | 430 | // early return path if it is a deleted user |
431 | 431 | $user = $this->access->userManager->get($uid); |
432 | - if($user instanceof OfflineUser) { |
|
433 | - if($this->currentUserInDeletionProcess !== null |
|
432 | + if ($user instanceof OfflineUser) { |
|
433 | + if ($this->currentUserInDeletionProcess !== null |
|
434 | 434 | && $this->currentUserInDeletionProcess === $user->getOCName() |
435 | 435 | ) { |
436 | 436 | return $user->getHomePath(); |
437 | 437 | } else { |
438 | - throw new NoUserException($uid . ' is not a valid user anymore'); |
|
438 | + throw new NoUserException($uid.' is not a valid user anymore'); |
|
439 | 439 | } |
440 | 440 | } else if ($user === null) { |
441 | - throw new NoUserException($uid . ' is not a valid user anymore'); |
|
441 | + throw new NoUserException($uid.' is not a valid user anymore'); |
|
442 | 442 | } |
443 | 443 | |
444 | 444 | $path = $user->getHomePath(); |
@@ -457,12 +457,12 @@ discard block |
||
457 | 457 | return $this->userPluginManager->getDisplayName($uid); |
458 | 458 | } |
459 | 459 | |
460 | - if(!$this->userExists($uid)) { |
|
460 | + if (!$this->userExists($uid)) { |
|
461 | 461 | return false; |
462 | 462 | } |
463 | 463 | |
464 | 464 | $cacheKey = 'getDisplayName'.$uid; |
465 | - if(!is_null($displayName = $this->access->connection->getFromCache($cacheKey))) { |
|
465 | + if (!is_null($displayName = $this->access->connection->getFromCache($cacheKey))) { |
|
466 | 466 | return $displayName; |
467 | 467 | } |
468 | 468 | |
@@ -479,10 +479,10 @@ discard block |
||
479 | 479 | $this->access->username2dn($uid), |
480 | 480 | $this->access->connection->ldapUserDisplayName); |
481 | 481 | |
482 | - if($displayName && (count($displayName) > 0)) { |
|
482 | + if ($displayName && (count($displayName) > 0)) { |
|
483 | 483 | $displayName = $displayName[0]; |
484 | 484 | |
485 | - if (is_array($displayName2)){ |
|
485 | + if (is_array($displayName2)) { |
|
486 | 486 | $displayName2 = count($displayName2) > 0 ? $displayName2[0] : ''; |
487 | 487 | } |
488 | 488 | |
@@ -526,7 +526,7 @@ discard block |
||
526 | 526 | */ |
527 | 527 | public function getDisplayNames($search = '', $limit = null, $offset = null) { |
528 | 528 | $cacheKey = 'getDisplayNames-'.$search.'-'.$limit.'-'.$offset; |
529 | - if(!is_null($displayNames = $this->access->connection->getFromCache($cacheKey))) { |
|
529 | + if (!is_null($displayNames = $this->access->connection->getFromCache($cacheKey))) { |
|
530 | 530 | return $displayNames; |
531 | 531 | } |
532 | 532 | |
@@ -548,12 +548,12 @@ discard block |
||
548 | 548 | * compared with \OC\User\Backend::CREATE_USER etc. |
549 | 549 | */ |
550 | 550 | public function implementsActions($actions) { |
551 | - return (bool)((Backend::CHECK_PASSWORD |
|
551 | + return (bool) ((Backend::CHECK_PASSWORD |
|
552 | 552 | | Backend::GET_HOME |
553 | 553 | | Backend::GET_DISPLAYNAME |
554 | 554 | | (($this->access->connection->ldapUserAvatarRule !== 'none') ? Backend::PROVIDE_AVATAR : 0) |
555 | 555 | | Backend::COUNT_USERS |
556 | - | (((int)$this->access->connection->turnOnPasswordChange === 1)? Backend::SET_PASSWORD :0) |
|
556 | + | (((int) $this->access->connection->turnOnPasswordChange === 1) ? Backend::SET_PASSWORD : 0) |
|
557 | 557 | | $this->userPluginManager->getImplementedActions()) |
558 | 558 | & $actions); |
559 | 559 | } |
@@ -577,7 +577,7 @@ discard block |
||
577 | 577 | |
578 | 578 | $filter = $this->access->getFilterForUserCount(); |
579 | 579 | $cacheKey = 'countUsers-'.$filter; |
580 | - if(!is_null($entries = $this->access->connection->getFromCache($cacheKey))) { |
|
580 | + if (!is_null($entries = $this->access->connection->getFromCache($cacheKey))) { |
|
581 | 581 | return $entries; |
582 | 582 | } |
583 | 583 | $entries = $this->access->countUsers($filter); |
@@ -589,7 +589,7 @@ discard block |
||
589 | 589 | * Backend name to be shown in user management |
590 | 590 | * @return string the name of the backend to be shown |
591 | 591 | */ |
592 | - public function getBackendName(){ |
|
592 | + public function getBackendName() { |
|
593 | 593 | return 'LDAP'; |
594 | 594 | } |
595 | 595 | |
@@ -627,7 +627,7 @@ discard block |
||
627 | 627 | if (is_string($dn)) { |
628 | 628 | // the NC user creation work flow requires a know user id up front |
629 | 629 | $uuid = $this->access->getUUID($dn, true); |
630 | - if(is_string($uuid)) { |
|
630 | + if (is_string($uuid)) { |
|
631 | 631 | $this->access->mapAndAnnounceIfApplicable( |
632 | 632 | $this->access->getUserMapper(), |
633 | 633 | $dn, |
@@ -47,1212 +47,1212 @@ |
||
47 | 47 | use OCP\ILogger; |
48 | 48 | |
49 | 49 | class Group_LDAP extends BackendUtility implements \OCP\GroupInterface, IGroupLDAP, IGetDisplayNameBackend { |
50 | - protected $enabled = false; |
|
51 | - |
|
52 | - /** |
|
53 | - * @var string[] $cachedGroupMembers array of users with gid as key |
|
54 | - */ |
|
55 | - protected $cachedGroupMembers; |
|
56 | - |
|
57 | - /** |
|
58 | - * @var string[] $cachedGroupsByMember array of groups with uid as key |
|
59 | - */ |
|
60 | - protected $cachedGroupsByMember; |
|
61 | - |
|
62 | - /** |
|
63 | - * @var string[] $cachedNestedGroups array of groups with gid (DN) as key |
|
64 | - */ |
|
65 | - protected $cachedNestedGroups; |
|
66 | - |
|
67 | - /** @var GroupPluginManager */ |
|
68 | - protected $groupPluginManager; |
|
69 | - |
|
70 | - public function __construct(Access $access, GroupPluginManager $groupPluginManager) { |
|
71 | - parent::__construct($access); |
|
72 | - $filter = $this->access->connection->ldapGroupFilter; |
|
73 | - $gassoc = $this->access->connection->ldapGroupMemberAssocAttr; |
|
74 | - if(!empty($filter) && !empty($gassoc)) { |
|
75 | - $this->enabled = true; |
|
76 | - } |
|
77 | - |
|
78 | - $this->cachedGroupMembers = new CappedMemoryCache(); |
|
79 | - $this->cachedGroupsByMember = new CappedMemoryCache(); |
|
80 | - $this->cachedNestedGroups = new CappedMemoryCache(); |
|
81 | - $this->groupPluginManager = $groupPluginManager; |
|
82 | - } |
|
83 | - |
|
84 | - /** |
|
85 | - * is user in group? |
|
86 | - * @param string $uid uid of the user |
|
87 | - * @param string $gid gid of the group |
|
88 | - * @return bool |
|
89 | - * |
|
90 | - * Checks whether the user is member of a group or not. |
|
91 | - */ |
|
92 | - public function inGroup($uid, $gid) { |
|
93 | - if(!$this->enabled) { |
|
94 | - return false; |
|
95 | - } |
|
96 | - $cacheKey = 'inGroup'.$uid.':'.$gid; |
|
97 | - $inGroup = $this->access->connection->getFromCache($cacheKey); |
|
98 | - if(!is_null($inGroup)) { |
|
99 | - return (bool)$inGroup; |
|
100 | - } |
|
101 | - |
|
102 | - $userDN = $this->access->username2dn($uid); |
|
103 | - |
|
104 | - if(isset($this->cachedGroupMembers[$gid])) { |
|
105 | - $isInGroup = in_array($userDN, $this->cachedGroupMembers[$gid]); |
|
106 | - return $isInGroup; |
|
107 | - } |
|
108 | - |
|
109 | - $cacheKeyMembers = 'inGroup-members:'.$gid; |
|
110 | - $members = $this->access->connection->getFromCache($cacheKeyMembers); |
|
111 | - if(!is_null($members)) { |
|
112 | - $this->cachedGroupMembers[$gid] = $members; |
|
113 | - $isInGroup = in_array($userDN, $members, true); |
|
114 | - $this->access->connection->writeToCache($cacheKey, $isInGroup); |
|
115 | - return $isInGroup; |
|
116 | - } |
|
117 | - |
|
118 | - $groupDN = $this->access->groupname2dn($gid); |
|
119 | - // just in case |
|
120 | - if(!$groupDN || !$userDN) { |
|
121 | - $this->access->connection->writeToCache($cacheKey, false); |
|
122 | - return false; |
|
123 | - } |
|
124 | - |
|
125 | - //check primary group first |
|
126 | - if($gid === $this->getUserPrimaryGroup($userDN)) { |
|
127 | - $this->access->connection->writeToCache($cacheKey, true); |
|
128 | - return true; |
|
129 | - } |
|
130 | - |
|
131 | - //usually, LDAP attributes are said to be case insensitive. But there are exceptions of course. |
|
132 | - $members = $this->_groupMembers($groupDN); |
|
133 | - if(!is_array($members) || count($members) === 0) { |
|
134 | - $this->access->connection->writeToCache($cacheKey, false); |
|
135 | - return false; |
|
136 | - } |
|
137 | - |
|
138 | - //extra work if we don't get back user DNs |
|
139 | - if(strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'memberuid') { |
|
140 | - $dns = array(); |
|
141 | - $filterParts = array(); |
|
142 | - $bytes = 0; |
|
143 | - foreach($members as $mid) { |
|
144 | - $filter = str_replace('%uid', $mid, $this->access->connection->ldapLoginFilter); |
|
145 | - $filterParts[] = $filter; |
|
146 | - $bytes += strlen($filter); |
|
147 | - if($bytes >= 9000000) { |
|
148 | - // AD has a default input buffer of 10 MB, we do not want |
|
149 | - // to take even the chance to exceed it |
|
150 | - $filter = $this->access->combineFilterWithOr($filterParts); |
|
151 | - $bytes = 0; |
|
152 | - $filterParts = array(); |
|
153 | - $users = $this->access->fetchListOfUsers($filter, 'dn', count($filterParts)); |
|
154 | - $dns = array_merge($dns, $users); |
|
155 | - } |
|
156 | - } |
|
157 | - if(count($filterParts) > 0) { |
|
158 | - $filter = $this->access->combineFilterWithOr($filterParts); |
|
159 | - $users = $this->access->fetchListOfUsers($filter, 'dn', count($filterParts)); |
|
160 | - $dns = array_merge($dns, $users); |
|
161 | - } |
|
162 | - $members = $dns; |
|
163 | - } |
|
164 | - |
|
165 | - $isInGroup = in_array($userDN, $members); |
|
166 | - $this->access->connection->writeToCache($cacheKey, $isInGroup); |
|
167 | - $this->access->connection->writeToCache($cacheKeyMembers, $members); |
|
168 | - $this->cachedGroupMembers[$gid] = $members; |
|
169 | - |
|
170 | - return $isInGroup; |
|
171 | - } |
|
172 | - |
|
173 | - /** |
|
174 | - * @param string $dnGroup |
|
175 | - * @return array |
|
176 | - * |
|
177 | - * For a group that has user membership defined by an LDAP search url attribute returns the users |
|
178 | - * that match the search url otherwise returns an empty array. |
|
179 | - */ |
|
180 | - public function getDynamicGroupMembers($dnGroup) { |
|
181 | - $dynamicGroupMemberURL = strtolower($this->access->connection->ldapDynamicGroupMemberURL); |
|
182 | - |
|
183 | - if (empty($dynamicGroupMemberURL)) { |
|
184 | - return array(); |
|
185 | - } |
|
186 | - |
|
187 | - $dynamicMembers = array(); |
|
188 | - $memberURLs = $this->access->readAttribute( |
|
189 | - $dnGroup, |
|
190 | - $dynamicGroupMemberURL, |
|
191 | - $this->access->connection->ldapGroupFilter |
|
192 | - ); |
|
193 | - if ($memberURLs !== false) { |
|
194 | - // this group has the 'memberURL' attribute so this is a dynamic group |
|
195 | - // example 1: ldap:///cn=users,cn=accounts,dc=dcsubbase,dc=dcbase??one?(o=HeadOffice) |
|
196 | - // example 2: ldap:///cn=users,cn=accounts,dc=dcsubbase,dc=dcbase??one?(&(o=HeadOffice)(uidNumber>=500)) |
|
197 | - $pos = strpos($memberURLs[0], '('); |
|
198 | - if ($pos !== false) { |
|
199 | - $memberUrlFilter = substr($memberURLs[0], $pos); |
|
200 | - $foundMembers = $this->access->searchUsers($memberUrlFilter,'dn'); |
|
201 | - $dynamicMembers = array(); |
|
202 | - foreach($foundMembers as $value) { |
|
203 | - $dynamicMembers[$value['dn'][0]] = 1; |
|
204 | - } |
|
205 | - } else { |
|
206 | - \OCP\Util::writeLog('user_ldap', 'No search filter found on member url '. |
|
207 | - 'of group ' . $dnGroup, ILogger::DEBUG); |
|
208 | - } |
|
209 | - } |
|
210 | - return $dynamicMembers; |
|
211 | - } |
|
212 | - |
|
213 | - /** |
|
214 | - * @param string $dnGroup |
|
215 | - * @param array|null &$seen |
|
216 | - * @return array|mixed|null |
|
217 | - * @throws \OC\ServerNotAvailableException |
|
218 | - */ |
|
219 | - private function _groupMembers($dnGroup, &$seen = null) { |
|
220 | - if ($seen === null) { |
|
221 | - $seen = []; |
|
222 | - } |
|
223 | - $allMembers = []; |
|
224 | - if (array_key_exists($dnGroup, $seen)) { |
|
225 | - // avoid loops |
|
226 | - return []; |
|
227 | - } |
|
228 | - // used extensively in cron job, caching makes sense for nested groups |
|
229 | - $cacheKey = '_groupMembers'.$dnGroup; |
|
230 | - $groupMembers = $this->access->connection->getFromCache($cacheKey); |
|
231 | - if($groupMembers !== null) { |
|
232 | - return $groupMembers; |
|
233 | - } |
|
234 | - $seen[$dnGroup] = 1; |
|
235 | - $members = $this->access->readAttribute($dnGroup, $this->access->connection->ldapGroupMemberAssocAttr); |
|
236 | - if (is_array($members)) { |
|
237 | - $fetcher = function($memberDN, &$seen) { |
|
238 | - return $this->_groupMembers($memberDN, $seen); |
|
239 | - }; |
|
240 | - $allMembers = $this->walkNestedGroups($dnGroup, $fetcher, $members); |
|
241 | - } |
|
242 | - |
|
243 | - $allMembers += $this->getDynamicGroupMembers($dnGroup); |
|
244 | - |
|
245 | - $this->access->connection->writeToCache($cacheKey, $allMembers); |
|
246 | - return $allMembers; |
|
247 | - } |
|
248 | - |
|
249 | - /** |
|
250 | - * @param string $DN |
|
251 | - * @param array|null &$seen |
|
252 | - * @return array |
|
253 | - * @throws \OC\ServerNotAvailableException |
|
254 | - */ |
|
255 | - private function _getGroupDNsFromMemberOf($DN) { |
|
256 | - $groups = $this->access->readAttribute($DN, 'memberOf'); |
|
257 | - if (!is_array($groups)) { |
|
258 | - return []; |
|
259 | - } |
|
260 | - |
|
261 | - $fetcher = function($groupDN) { |
|
262 | - if (isset($this->cachedNestedGroups[$groupDN])) { |
|
263 | - $nestedGroups = $this->cachedNestedGroups[$groupDN]; |
|
264 | - } else { |
|
265 | - $nestedGroups = $this->access->readAttribute($groupDN, 'memberOf'); |
|
266 | - if (!is_array($nestedGroups)) { |
|
267 | - $nestedGroups = []; |
|
268 | - } |
|
269 | - $this->cachedNestedGroups[$groupDN] = $nestedGroups; |
|
270 | - } |
|
271 | - return $nestedGroups; |
|
272 | - }; |
|
273 | - |
|
274 | - $groups = $this->walkNestedGroups($DN, $fetcher, $groups); |
|
275 | - return $this->access->groupsMatchFilter($groups); |
|
276 | - } |
|
277 | - |
|
278 | - /** |
|
279 | - * @param string $dn |
|
280 | - * @param \Closure $fetcher args: string $dn, array $seen, returns: string[] of dns |
|
281 | - * @param array $list |
|
282 | - * @return array |
|
283 | - */ |
|
284 | - private function walkNestedGroups(string $dn, \Closure $fetcher, array $list): array { |
|
285 | - $nesting = (int) $this->access->connection->ldapNestedGroups; |
|
286 | - // depending on the input, we either have a list of DNs or a list of LDAP records |
|
287 | - // also, the output expects either DNs or records. Testing the first element should suffice. |
|
288 | - $recordMode = is_array($list) && isset($list[0]) && is_array($list[0]) && isset($list[0]['dn'][0]); |
|
289 | - |
|
290 | - if ($nesting !== 1) { |
|
291 | - if($recordMode) { |
|
292 | - // the keys are numeric, but should hold the DN |
|
293 | - return array_reduce($list, function ($transformed, $record) use ($dn) { |
|
294 | - if($record['dn'][0] != $dn) { |
|
295 | - $transformed[$record['dn'][0]] = $record; |
|
296 | - } |
|
297 | - return $transformed; |
|
298 | - }, []); |
|
299 | - } |
|
300 | - return $list; |
|
301 | - } |
|
302 | - |
|
303 | - $seen = []; |
|
304 | - while ($record = array_pop($list)) { |
|
305 | - $recordDN = $recordMode ? $record['dn'][0] : $record; |
|
306 | - if ($recordDN === $dn || array_key_exists($recordDN, $seen)) { |
|
307 | - // Prevent loops |
|
308 | - continue; |
|
309 | - } |
|
310 | - $fetched = $fetcher($record, $seen); |
|
311 | - $list = array_merge($list, $fetched); |
|
312 | - $seen[$recordDN] = $record; |
|
313 | - } |
|
314 | - |
|
315 | - return $recordMode ? $seen : array_keys($seen); |
|
316 | - } |
|
317 | - |
|
318 | - /** |
|
319 | - * translates a gidNumber into an ownCloud internal name |
|
320 | - * @param string $gid as given by gidNumber on POSIX LDAP |
|
321 | - * @param string $dn a DN that belongs to the same domain as the group |
|
322 | - * @return string|bool |
|
323 | - */ |
|
324 | - public function gidNumber2Name($gid, $dn) { |
|
325 | - $cacheKey = 'gidNumberToName' . $gid; |
|
326 | - $groupName = $this->access->connection->getFromCache($cacheKey); |
|
327 | - if(!is_null($groupName) && isset($groupName)) { |
|
328 | - return $groupName; |
|
329 | - } |
|
330 | - |
|
331 | - //we need to get the DN from LDAP |
|
332 | - $filter = $this->access->combineFilterWithAnd([ |
|
333 | - $this->access->connection->ldapGroupFilter, |
|
334 | - 'objectClass=posixGroup', |
|
335 | - $this->access->connection->ldapGidNumber . '=' . $gid |
|
336 | - ]); |
|
337 | - $result = $this->access->searchGroups($filter, array('dn'), 1); |
|
338 | - if(empty($result)) { |
|
339 | - return false; |
|
340 | - } |
|
341 | - $dn = $result[0]['dn'][0]; |
|
342 | - |
|
343 | - //and now the group name |
|
344 | - //NOTE once we have separate ownCloud group IDs and group names we can |
|
345 | - //directly read the display name attribute instead of the DN |
|
346 | - $name = $this->access->dn2groupname($dn); |
|
347 | - |
|
348 | - $this->access->connection->writeToCache($cacheKey, $name); |
|
349 | - |
|
350 | - return $name; |
|
351 | - } |
|
352 | - |
|
353 | - /** |
|
354 | - * returns the entry's gidNumber |
|
355 | - * @param string $dn |
|
356 | - * @param string $attribute |
|
357 | - * @return string|bool |
|
358 | - */ |
|
359 | - private function getEntryGidNumber($dn, $attribute) { |
|
360 | - $value = $this->access->readAttribute($dn, $attribute); |
|
361 | - if(is_array($value) && !empty($value)) { |
|
362 | - return $value[0]; |
|
363 | - } |
|
364 | - return false; |
|
365 | - } |
|
366 | - |
|
367 | - /** |
|
368 | - * returns the group's primary ID |
|
369 | - * @param string $dn |
|
370 | - * @return string|bool |
|
371 | - */ |
|
372 | - public function getGroupGidNumber($dn) { |
|
373 | - return $this->getEntryGidNumber($dn, 'gidNumber'); |
|
374 | - } |
|
375 | - |
|
376 | - /** |
|
377 | - * returns the user's gidNumber |
|
378 | - * @param string $dn |
|
379 | - * @return string|bool |
|
380 | - */ |
|
381 | - public function getUserGidNumber($dn) { |
|
382 | - $gidNumber = false; |
|
383 | - if($this->access->connection->hasGidNumber) { |
|
384 | - $gidNumber = $this->getEntryGidNumber($dn, $this->access->connection->ldapGidNumber); |
|
385 | - if($gidNumber === false) { |
|
386 | - $this->access->connection->hasGidNumber = false; |
|
387 | - } |
|
388 | - } |
|
389 | - return $gidNumber; |
|
390 | - } |
|
391 | - |
|
392 | - /** |
|
393 | - * returns a filter for a "users has specific gid" search or count operation |
|
394 | - * |
|
395 | - * @param string $groupDN |
|
396 | - * @param string $search |
|
397 | - * @return string |
|
398 | - * @throws \Exception |
|
399 | - */ |
|
400 | - private function prepareFilterForUsersHasGidNumber($groupDN, $search = '') { |
|
401 | - $groupID = $this->getGroupGidNumber($groupDN); |
|
402 | - if($groupID === false) { |
|
403 | - throw new \Exception('Not a valid group'); |
|
404 | - } |
|
405 | - |
|
406 | - $filterParts = []; |
|
407 | - $filterParts[] = $this->access->getFilterForUserCount(); |
|
408 | - if ($search !== '') { |
|
409 | - $filterParts[] = $this->access->getFilterPartForUserSearch($search); |
|
410 | - } |
|
411 | - $filterParts[] = $this->access->connection->ldapGidNumber .'=' . $groupID; |
|
412 | - |
|
413 | - return $this->access->combineFilterWithAnd($filterParts); |
|
414 | - } |
|
415 | - |
|
416 | - /** |
|
417 | - * returns a list of users that have the given group as gid number |
|
418 | - * |
|
419 | - * @param string $groupDN |
|
420 | - * @param string $search |
|
421 | - * @param int $limit |
|
422 | - * @param int $offset |
|
423 | - * @return string[] |
|
424 | - */ |
|
425 | - public function getUsersInGidNumber($groupDN, $search = '', $limit = -1, $offset = 0) { |
|
426 | - try { |
|
427 | - $filter = $this->prepareFilterForUsersHasGidNumber($groupDN, $search); |
|
428 | - $users = $this->access->fetchListOfUsers( |
|
429 | - $filter, |
|
430 | - [$this->access->connection->ldapUserDisplayName, 'dn'], |
|
431 | - $limit, |
|
432 | - $offset |
|
433 | - ); |
|
434 | - return $this->access->nextcloudUserNames($users); |
|
435 | - } catch (\Exception $e) { |
|
436 | - return []; |
|
437 | - } |
|
438 | - } |
|
439 | - |
|
440 | - /** |
|
441 | - * returns the number of users that have the given group as gid number |
|
442 | - * |
|
443 | - * @param string $groupDN |
|
444 | - * @param string $search |
|
445 | - * @param int $limit |
|
446 | - * @param int $offset |
|
447 | - * @return int |
|
448 | - */ |
|
449 | - public function countUsersInGidNumber($groupDN, $search = '', $limit = -1, $offset = 0) { |
|
450 | - try { |
|
451 | - $filter = $this->prepareFilterForUsersHasGidNumber($groupDN, $search); |
|
452 | - $users = $this->access->countUsers($filter, ['dn'], $limit, $offset); |
|
453 | - return (int)$users; |
|
454 | - } catch (\Exception $e) { |
|
455 | - return 0; |
|
456 | - } |
|
457 | - } |
|
458 | - |
|
459 | - /** |
|
460 | - * gets the gidNumber of a user |
|
461 | - * @param string $dn |
|
462 | - * @return string |
|
463 | - */ |
|
464 | - public function getUserGroupByGid($dn) { |
|
465 | - $groupID = $this->getUserGidNumber($dn); |
|
466 | - if($groupID !== false) { |
|
467 | - $groupName = $this->gidNumber2Name($groupID, $dn); |
|
468 | - if($groupName !== false) { |
|
469 | - return $groupName; |
|
470 | - } |
|
471 | - } |
|
472 | - |
|
473 | - return false; |
|
474 | - } |
|
475 | - |
|
476 | - /** |
|
477 | - * translates a primary group ID into an Nextcloud internal name |
|
478 | - * @param string $gid as given by primaryGroupID on AD |
|
479 | - * @param string $dn a DN that belongs to the same domain as the group |
|
480 | - * @return string|bool |
|
481 | - */ |
|
482 | - public function primaryGroupID2Name($gid, $dn) { |
|
483 | - $cacheKey = 'primaryGroupIDtoName'; |
|
484 | - $groupNames = $this->access->connection->getFromCache($cacheKey); |
|
485 | - if(!is_null($groupNames) && isset($groupNames[$gid])) { |
|
486 | - return $groupNames[$gid]; |
|
487 | - } |
|
488 | - |
|
489 | - $domainObjectSid = $this->access->getSID($dn); |
|
490 | - if($domainObjectSid === false) { |
|
491 | - return false; |
|
492 | - } |
|
493 | - |
|
494 | - //we need to get the DN from LDAP |
|
495 | - $filter = $this->access->combineFilterWithAnd(array( |
|
496 | - $this->access->connection->ldapGroupFilter, |
|
497 | - 'objectsid=' . $domainObjectSid . '-' . $gid |
|
498 | - )); |
|
499 | - $result = $this->access->searchGroups($filter, array('dn'), 1); |
|
500 | - if(empty($result)) { |
|
501 | - return false; |
|
502 | - } |
|
503 | - $dn = $result[0]['dn'][0]; |
|
504 | - |
|
505 | - //and now the group name |
|
506 | - //NOTE once we have separate Nextcloud group IDs and group names we can |
|
507 | - //directly read the display name attribute instead of the DN |
|
508 | - $name = $this->access->dn2groupname($dn); |
|
509 | - |
|
510 | - $this->access->connection->writeToCache($cacheKey, $name); |
|
511 | - |
|
512 | - return $name; |
|
513 | - } |
|
514 | - |
|
515 | - /** |
|
516 | - * returns the entry's primary group ID |
|
517 | - * @param string $dn |
|
518 | - * @param string $attribute |
|
519 | - * @return string|bool |
|
520 | - */ |
|
521 | - private function getEntryGroupID($dn, $attribute) { |
|
522 | - $value = $this->access->readAttribute($dn, $attribute); |
|
523 | - if(is_array($value) && !empty($value)) { |
|
524 | - return $value[0]; |
|
525 | - } |
|
526 | - return false; |
|
527 | - } |
|
528 | - |
|
529 | - /** |
|
530 | - * returns the group's primary ID |
|
531 | - * @param string $dn |
|
532 | - * @return string|bool |
|
533 | - */ |
|
534 | - public function getGroupPrimaryGroupID($dn) { |
|
535 | - return $this->getEntryGroupID($dn, 'primaryGroupToken'); |
|
536 | - } |
|
537 | - |
|
538 | - /** |
|
539 | - * returns the user's primary group ID |
|
540 | - * @param string $dn |
|
541 | - * @return string|bool |
|
542 | - */ |
|
543 | - public function getUserPrimaryGroupIDs($dn) { |
|
544 | - $primaryGroupID = false; |
|
545 | - if($this->access->connection->hasPrimaryGroups) { |
|
546 | - $primaryGroupID = $this->getEntryGroupID($dn, 'primaryGroupID'); |
|
547 | - if($primaryGroupID === false) { |
|
548 | - $this->access->connection->hasPrimaryGroups = false; |
|
549 | - } |
|
550 | - } |
|
551 | - return $primaryGroupID; |
|
552 | - } |
|
553 | - |
|
554 | - /** |
|
555 | - * returns a filter for a "users in primary group" search or count operation |
|
556 | - * |
|
557 | - * @param string $groupDN |
|
558 | - * @param string $search |
|
559 | - * @return string |
|
560 | - * @throws \Exception |
|
561 | - */ |
|
562 | - private function prepareFilterForUsersInPrimaryGroup($groupDN, $search = '') { |
|
563 | - $groupID = $this->getGroupPrimaryGroupID($groupDN); |
|
564 | - if($groupID === false) { |
|
565 | - throw new \Exception('Not a valid group'); |
|
566 | - } |
|
567 | - |
|
568 | - $filterParts = []; |
|
569 | - $filterParts[] = $this->access->getFilterForUserCount(); |
|
570 | - if ($search !== '') { |
|
571 | - $filterParts[] = $this->access->getFilterPartForUserSearch($search); |
|
572 | - } |
|
573 | - $filterParts[] = 'primaryGroupID=' . $groupID; |
|
574 | - |
|
575 | - return $this->access->combineFilterWithAnd($filterParts); |
|
576 | - } |
|
577 | - |
|
578 | - /** |
|
579 | - * returns a list of users that have the given group as primary group |
|
580 | - * |
|
581 | - * @param string $groupDN |
|
582 | - * @param string $search |
|
583 | - * @param int $limit |
|
584 | - * @param int $offset |
|
585 | - * @return string[] |
|
586 | - */ |
|
587 | - public function getUsersInPrimaryGroup($groupDN, $search = '', $limit = -1, $offset = 0) { |
|
588 | - try { |
|
589 | - $filter = $this->prepareFilterForUsersInPrimaryGroup($groupDN, $search); |
|
590 | - $users = $this->access->fetchListOfUsers( |
|
591 | - $filter, |
|
592 | - array($this->access->connection->ldapUserDisplayName, 'dn'), |
|
593 | - $limit, |
|
594 | - $offset |
|
595 | - ); |
|
596 | - return $this->access->nextcloudUserNames($users); |
|
597 | - } catch (\Exception $e) { |
|
598 | - return array(); |
|
599 | - } |
|
600 | - } |
|
601 | - |
|
602 | - /** |
|
603 | - * returns the number of users that have the given group as primary group |
|
604 | - * |
|
605 | - * @param string $groupDN |
|
606 | - * @param string $search |
|
607 | - * @param int $limit |
|
608 | - * @param int $offset |
|
609 | - * @return int |
|
610 | - */ |
|
611 | - public function countUsersInPrimaryGroup($groupDN, $search = '', $limit = -1, $offset = 0) { |
|
612 | - try { |
|
613 | - $filter = $this->prepareFilterForUsersInPrimaryGroup($groupDN, $search); |
|
614 | - $users = $this->access->countUsers($filter, array('dn'), $limit, $offset); |
|
615 | - return (int)$users; |
|
616 | - } catch (\Exception $e) { |
|
617 | - return 0; |
|
618 | - } |
|
619 | - } |
|
620 | - |
|
621 | - /** |
|
622 | - * gets the primary group of a user |
|
623 | - * @param string $dn |
|
624 | - * @return string |
|
625 | - */ |
|
626 | - public function getUserPrimaryGroup($dn) { |
|
627 | - $groupID = $this->getUserPrimaryGroupIDs($dn); |
|
628 | - if($groupID !== false) { |
|
629 | - $groupName = $this->primaryGroupID2Name($groupID, $dn); |
|
630 | - if($groupName !== false) { |
|
631 | - return $groupName; |
|
632 | - } |
|
633 | - } |
|
634 | - |
|
635 | - return false; |
|
636 | - } |
|
637 | - |
|
638 | - /** |
|
639 | - * Get all groups a user belongs to |
|
640 | - * @param string $uid Name of the user |
|
641 | - * @return array with group names |
|
642 | - * |
|
643 | - * This function fetches all groups a user belongs to. It does not check |
|
644 | - * if the user exists at all. |
|
645 | - * |
|
646 | - * This function includes groups based on dynamic group membership. |
|
647 | - */ |
|
648 | - public function getUserGroups($uid) { |
|
649 | - if(!$this->enabled) { |
|
650 | - return array(); |
|
651 | - } |
|
652 | - $cacheKey = 'getUserGroups'.$uid; |
|
653 | - $userGroups = $this->access->connection->getFromCache($cacheKey); |
|
654 | - if(!is_null($userGroups)) { |
|
655 | - return $userGroups; |
|
656 | - } |
|
657 | - $userDN = $this->access->username2dn($uid); |
|
658 | - if(!$userDN) { |
|
659 | - $this->access->connection->writeToCache($cacheKey, array()); |
|
660 | - return array(); |
|
661 | - } |
|
662 | - |
|
663 | - $groups = []; |
|
664 | - $primaryGroup = $this->getUserPrimaryGroup($userDN); |
|
665 | - $gidGroupName = $this->getUserGroupByGid($userDN); |
|
666 | - |
|
667 | - $dynamicGroupMemberURL = strtolower($this->access->connection->ldapDynamicGroupMemberURL); |
|
668 | - |
|
669 | - if (!empty($dynamicGroupMemberURL)) { |
|
670 | - // look through dynamic groups to add them to the result array if needed |
|
671 | - $groupsToMatch = $this->access->fetchListOfGroups( |
|
672 | - $this->access->connection->ldapGroupFilter,array('dn',$dynamicGroupMemberURL)); |
|
673 | - foreach($groupsToMatch as $dynamicGroup) { |
|
674 | - if (!array_key_exists($dynamicGroupMemberURL, $dynamicGroup)) { |
|
675 | - continue; |
|
676 | - } |
|
677 | - $pos = strpos($dynamicGroup[$dynamicGroupMemberURL][0], '('); |
|
678 | - if ($pos !== false) { |
|
679 | - $memberUrlFilter = substr($dynamicGroup[$dynamicGroupMemberURL][0],$pos); |
|
680 | - // apply filter via ldap search to see if this user is in this |
|
681 | - // dynamic group |
|
682 | - $userMatch = $this->access->readAttribute( |
|
683 | - $userDN, |
|
684 | - $this->access->connection->ldapUserDisplayName, |
|
685 | - $memberUrlFilter |
|
686 | - ); |
|
687 | - if ($userMatch !== false) { |
|
688 | - // match found so this user is in this group |
|
689 | - $groupName = $this->access->dn2groupname($dynamicGroup['dn'][0]); |
|
690 | - if(is_string($groupName)) { |
|
691 | - // be sure to never return false if the dn could not be |
|
692 | - // resolved to a name, for whatever reason. |
|
693 | - $groups[] = $groupName; |
|
694 | - } |
|
695 | - } |
|
696 | - } else { |
|
697 | - \OCP\Util::writeLog('user_ldap', 'No search filter found on member url '. |
|
698 | - 'of group ' . print_r($dynamicGroup, true), ILogger::DEBUG); |
|
699 | - } |
|
700 | - } |
|
701 | - } |
|
702 | - |
|
703 | - // if possible, read out membership via memberOf. It's far faster than |
|
704 | - // performing a search, which still is a fallback later. |
|
705 | - // memberof doesn't support memberuid, so skip it here. |
|
706 | - if((int)$this->access->connection->hasMemberOfFilterSupport === 1 |
|
707 | - && (int)$this->access->connection->useMemberOfToDetectMembership === 1 |
|
708 | - && strtolower($this->access->connection->ldapGroupMemberAssocAttr) !== 'memberuid' |
|
709 | - ) { |
|
710 | - $groupDNs = $this->_getGroupDNsFromMemberOf($userDN); |
|
711 | - if (is_array($groupDNs)) { |
|
712 | - foreach ($groupDNs as $dn) { |
|
713 | - $groupName = $this->access->dn2groupname($dn); |
|
714 | - if(is_string($groupName)) { |
|
715 | - // be sure to never return false if the dn could not be |
|
716 | - // resolved to a name, for whatever reason. |
|
717 | - $groups[] = $groupName; |
|
718 | - } |
|
719 | - } |
|
720 | - } |
|
721 | - |
|
722 | - if($primaryGroup !== false) { |
|
723 | - $groups[] = $primaryGroup; |
|
724 | - } |
|
725 | - if($gidGroupName !== false) { |
|
726 | - $groups[] = $gidGroupName; |
|
727 | - } |
|
728 | - $this->access->connection->writeToCache($cacheKey, $groups); |
|
729 | - return $groups; |
|
730 | - } |
|
731 | - |
|
732 | - //uniqueMember takes DN, memberuid the uid, so we need to distinguish |
|
733 | - if((strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'uniquemember') |
|
734 | - || (strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'member') |
|
735 | - ) { |
|
736 | - $uid = $userDN; |
|
737 | - } else if(strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'memberuid') { |
|
738 | - $result = $this->access->readAttribute($userDN, 'uid'); |
|
739 | - if ($result === false) { |
|
740 | - \OCP\Util::writeLog('user_ldap', 'No uid attribute found for DN ' . $userDN . ' on '. |
|
741 | - $this->access->connection->ldapHost, ILogger::DEBUG); |
|
742 | - } |
|
743 | - $uid = $result[0]; |
|
744 | - } else { |
|
745 | - // just in case |
|
746 | - $uid = $userDN; |
|
747 | - } |
|
748 | - |
|
749 | - if(isset($this->cachedGroupsByMember[$uid])) { |
|
750 | - $groups = array_merge($groups, $this->cachedGroupsByMember[$uid]); |
|
751 | - } else { |
|
752 | - $groupsByMember = array_values($this->getGroupsByMember($uid)); |
|
753 | - $groupsByMember = $this->access->nextcloudGroupNames($groupsByMember); |
|
754 | - $this->cachedGroupsByMember[$uid] = $groupsByMember; |
|
755 | - $groups = array_merge($groups, $groupsByMember); |
|
756 | - } |
|
757 | - |
|
758 | - if($primaryGroup !== false) { |
|
759 | - $groups[] = $primaryGroup; |
|
760 | - } |
|
761 | - if($gidGroupName !== false) { |
|
762 | - $groups[] = $gidGroupName; |
|
763 | - } |
|
764 | - |
|
765 | - $groups = array_unique($groups, SORT_LOCALE_STRING); |
|
766 | - $this->access->connection->writeToCache($cacheKey, $groups); |
|
767 | - |
|
768 | - return $groups; |
|
769 | - } |
|
770 | - |
|
771 | - /** |
|
772 | - * @param string $dn |
|
773 | - * @param array|null &$seen |
|
774 | - * @return array |
|
775 | - */ |
|
776 | - private function getGroupsByMember($dn, &$seen = null) { |
|
777 | - if ($seen === null) { |
|
778 | - $seen = []; |
|
779 | - } |
|
780 | - if (array_key_exists($dn, $seen)) { |
|
781 | - // avoid loops |
|
782 | - return []; |
|
783 | - } |
|
784 | - $allGroups = []; |
|
785 | - $seen[$dn] = true; |
|
786 | - $filter = $this->access->connection->ldapGroupMemberAssocAttr.'='.$dn; |
|
787 | - $groups = $this->access->fetchListOfGroups($filter, |
|
788 | - [$this->access->connection->ldapGroupDisplayName, 'dn']); |
|
789 | - if (is_array($groups)) { |
|
790 | - $fetcher = function ($dn, &$seen) { |
|
791 | - if(is_array($dn) && isset($dn['dn'][0])) { |
|
792 | - $dn = $dn['dn'][0]; |
|
793 | - } |
|
794 | - return $this->getGroupsByMember($dn, $seen); |
|
795 | - }; |
|
796 | - $allGroups = $this->walkNestedGroups($dn, $fetcher, $groups); |
|
797 | - } |
|
798 | - $visibleGroups = $this->access->groupsMatchFilter(array_keys($allGroups)); |
|
799 | - return array_intersect_key($allGroups, array_flip($visibleGroups)); |
|
800 | - } |
|
801 | - |
|
802 | - /** |
|
803 | - * get a list of all users in a group |
|
804 | - * |
|
805 | - * @param string $gid |
|
806 | - * @param string $search |
|
807 | - * @param int $limit |
|
808 | - * @param int $offset |
|
809 | - * @return array with user ids |
|
810 | - */ |
|
811 | - public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) { |
|
812 | - if(!$this->enabled) { |
|
813 | - return array(); |
|
814 | - } |
|
815 | - if(!$this->groupExists($gid)) { |
|
816 | - return array(); |
|
817 | - } |
|
818 | - $search = $this->access->escapeFilterPart($search, true); |
|
819 | - $cacheKey = 'usersInGroup-'.$gid.'-'.$search.'-'.$limit.'-'.$offset; |
|
820 | - // check for cache of the exact query |
|
821 | - $groupUsers = $this->access->connection->getFromCache($cacheKey); |
|
822 | - if(!is_null($groupUsers)) { |
|
823 | - return $groupUsers; |
|
824 | - } |
|
825 | - |
|
826 | - // check for cache of the query without limit and offset |
|
827 | - $groupUsers = $this->access->connection->getFromCache('usersInGroup-'.$gid.'-'.$search); |
|
828 | - if(!is_null($groupUsers)) { |
|
829 | - $groupUsers = array_slice($groupUsers, $offset, $limit); |
|
830 | - $this->access->connection->writeToCache($cacheKey, $groupUsers); |
|
831 | - return $groupUsers; |
|
832 | - } |
|
833 | - |
|
834 | - if($limit === -1) { |
|
835 | - $limit = null; |
|
836 | - } |
|
837 | - $groupDN = $this->access->groupname2dn($gid); |
|
838 | - if(!$groupDN) { |
|
839 | - // group couldn't be found, return empty resultset |
|
840 | - $this->access->connection->writeToCache($cacheKey, array()); |
|
841 | - return array(); |
|
842 | - } |
|
843 | - |
|
844 | - $primaryUsers = $this->getUsersInPrimaryGroup($groupDN, $search, $limit, $offset); |
|
845 | - $posixGroupUsers = $this->getUsersInGidNumber($groupDN, $search, $limit, $offset); |
|
846 | - $members = $this->_groupMembers($groupDN); |
|
847 | - if(!$members && empty($posixGroupUsers) && empty($primaryUsers)) { |
|
848 | - //in case users could not be retrieved, return empty result set |
|
849 | - $this->access->connection->writeToCache($cacheKey, []); |
|
850 | - return []; |
|
851 | - } |
|
852 | - |
|
853 | - $groupUsers = array(); |
|
854 | - $isMemberUid = (strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'memberuid'); |
|
855 | - $attrs = $this->access->userManager->getAttributes(true); |
|
856 | - foreach($members as $member) { |
|
857 | - if($isMemberUid) { |
|
858 | - //we got uids, need to get their DNs to 'translate' them to user names |
|
859 | - $filter = $this->access->combineFilterWithAnd(array( |
|
860 | - str_replace('%uid', trim($member), $this->access->connection->ldapLoginFilter), |
|
861 | - $this->access->getFilterPartForUserSearch($search) |
|
862 | - )); |
|
863 | - $ldap_users = $this->access->fetchListOfUsers($filter, $attrs, 1); |
|
864 | - if(count($ldap_users) < 1) { |
|
865 | - continue; |
|
866 | - } |
|
867 | - $groupUsers[] = $this->access->dn2username($ldap_users[0]['dn'][0]); |
|
868 | - } else { |
|
869 | - //we got DNs, check if we need to filter by search or we can give back all of them |
|
870 | - if ($search !== '') { |
|
871 | - if(!$this->access->readAttribute($member, |
|
872 | - $this->access->connection->ldapUserDisplayName, |
|
873 | - $this->access->getFilterPartForUserSearch($search))) { |
|
874 | - continue; |
|
875 | - } |
|
876 | - } |
|
877 | - // dn2username will also check if the users belong to the allowed base |
|
878 | - if($ocname = $this->access->dn2username($member)) { |
|
879 | - $groupUsers[] = $ocname; |
|
880 | - } |
|
881 | - } |
|
882 | - } |
|
883 | - |
|
884 | - $groupUsers = array_unique(array_merge($groupUsers, $primaryUsers, $posixGroupUsers)); |
|
885 | - natsort($groupUsers); |
|
886 | - $this->access->connection->writeToCache('usersInGroup-'.$gid.'-'.$search, $groupUsers); |
|
887 | - $groupUsers = array_slice($groupUsers, $offset, $limit); |
|
888 | - |
|
889 | - $this->access->connection->writeToCache($cacheKey, $groupUsers); |
|
890 | - |
|
891 | - return $groupUsers; |
|
892 | - } |
|
893 | - |
|
894 | - /** |
|
895 | - * returns the number of users in a group, who match the search term |
|
896 | - * @param string $gid the internal group name |
|
897 | - * @param string $search optional, a search string |
|
898 | - * @return int|bool |
|
899 | - */ |
|
900 | - public function countUsersInGroup($gid, $search = '') { |
|
901 | - if ($this->groupPluginManager->implementsActions(GroupInterface::COUNT_USERS)) { |
|
902 | - return $this->groupPluginManager->countUsersInGroup($gid, $search); |
|
903 | - } |
|
904 | - |
|
905 | - $cacheKey = 'countUsersInGroup-'.$gid.'-'.$search; |
|
906 | - if(!$this->enabled || !$this->groupExists($gid)) { |
|
907 | - return false; |
|
908 | - } |
|
909 | - $groupUsers = $this->access->connection->getFromCache($cacheKey); |
|
910 | - if(!is_null($groupUsers)) { |
|
911 | - return $groupUsers; |
|
912 | - } |
|
913 | - |
|
914 | - $groupDN = $this->access->groupname2dn($gid); |
|
915 | - if(!$groupDN) { |
|
916 | - // group couldn't be found, return empty result set |
|
917 | - $this->access->connection->writeToCache($cacheKey, false); |
|
918 | - return false; |
|
919 | - } |
|
920 | - |
|
921 | - $members = $this->_groupMembers($groupDN); |
|
922 | - $primaryUserCount = $this->countUsersInPrimaryGroup($groupDN, ''); |
|
923 | - if(!$members && $primaryUserCount === 0) { |
|
924 | - //in case users could not be retrieved, return empty result set |
|
925 | - $this->access->connection->writeToCache($cacheKey, false); |
|
926 | - return false; |
|
927 | - } |
|
928 | - |
|
929 | - if ($search === '') { |
|
930 | - $groupUsers = count($members) + $primaryUserCount; |
|
931 | - $this->access->connection->writeToCache($cacheKey, $groupUsers); |
|
932 | - return $groupUsers; |
|
933 | - } |
|
934 | - $search = $this->access->escapeFilterPart($search, true); |
|
935 | - $isMemberUid = |
|
936 | - (strtolower($this->access->connection->ldapGroupMemberAssocAttr) |
|
937 | - === 'memberuid'); |
|
938 | - |
|
939 | - //we need to apply the search filter |
|
940 | - //alternatives that need to be checked: |
|
941 | - //a) get all users by search filter and array_intersect them |
|
942 | - //b) a, but only when less than 1k 10k ?k users like it is |
|
943 | - //c) put all DNs|uids in a LDAP filter, combine with the search string |
|
944 | - // and let it count. |
|
945 | - //For now this is not important, because the only use of this method |
|
946 | - //does not supply a search string |
|
947 | - $groupUsers = array(); |
|
948 | - foreach($members as $member) { |
|
949 | - if($isMemberUid) { |
|
950 | - //we got uids, need to get their DNs to 'translate' them to user names |
|
951 | - $filter = $this->access->combineFilterWithAnd(array( |
|
952 | - str_replace('%uid', $member, $this->access->connection->ldapLoginFilter), |
|
953 | - $this->access->getFilterPartForUserSearch($search) |
|
954 | - )); |
|
955 | - $ldap_users = $this->access->fetchListOfUsers($filter, 'dn', 1); |
|
956 | - if(count($ldap_users) < 1) { |
|
957 | - continue; |
|
958 | - } |
|
959 | - $groupUsers[] = $this->access->dn2username($ldap_users[0]); |
|
960 | - } else { |
|
961 | - //we need to apply the search filter now |
|
962 | - if(!$this->access->readAttribute($member, |
|
963 | - $this->access->connection->ldapUserDisplayName, |
|
964 | - $this->access->getFilterPartForUserSearch($search))) { |
|
965 | - continue; |
|
966 | - } |
|
967 | - // dn2username will also check if the users belong to the allowed base |
|
968 | - if($ocname = $this->access->dn2username($member)) { |
|
969 | - $groupUsers[] = $ocname; |
|
970 | - } |
|
971 | - } |
|
972 | - } |
|
973 | - |
|
974 | - //and get users that have the group as primary |
|
975 | - $primaryUsers = $this->countUsersInPrimaryGroup($groupDN, $search); |
|
976 | - |
|
977 | - return count($groupUsers) + $primaryUsers; |
|
978 | - } |
|
979 | - |
|
980 | - /** |
|
981 | - * get a list of all groups |
|
982 | - * |
|
983 | - * @param string $search |
|
984 | - * @param $limit |
|
985 | - * @param int $offset |
|
986 | - * @return array with group names |
|
987 | - * |
|
988 | - * Returns a list with all groups (used by getGroups) |
|
989 | - */ |
|
990 | - protected function getGroupsChunk($search = '', $limit = -1, $offset = 0) { |
|
991 | - if(!$this->enabled) { |
|
992 | - return array(); |
|
993 | - } |
|
994 | - $cacheKey = 'getGroups-'.$search.'-'.$limit.'-'.$offset; |
|
995 | - |
|
996 | - //Check cache before driving unnecessary searches |
|
997 | - \OCP\Util::writeLog('user_ldap', 'getGroups '.$cacheKey, ILogger::DEBUG); |
|
998 | - $ldap_groups = $this->access->connection->getFromCache($cacheKey); |
|
999 | - if(!is_null($ldap_groups)) { |
|
1000 | - return $ldap_groups; |
|
1001 | - } |
|
1002 | - |
|
1003 | - // if we'd pass -1 to LDAP search, we'd end up in a Protocol |
|
1004 | - // error. With a limit of 0, we get 0 results. So we pass null. |
|
1005 | - if($limit <= 0) { |
|
1006 | - $limit = null; |
|
1007 | - } |
|
1008 | - $filter = $this->access->combineFilterWithAnd(array( |
|
1009 | - $this->access->connection->ldapGroupFilter, |
|
1010 | - $this->access->getFilterPartForGroupSearch($search) |
|
1011 | - )); |
|
1012 | - \OCP\Util::writeLog('user_ldap', 'getGroups Filter '.$filter, ILogger::DEBUG); |
|
1013 | - $ldap_groups = $this->access->fetchListOfGroups($filter, |
|
1014 | - array($this->access->connection->ldapGroupDisplayName, 'dn'), |
|
1015 | - $limit, |
|
1016 | - $offset); |
|
1017 | - $ldap_groups = $this->access->nextcloudGroupNames($ldap_groups); |
|
1018 | - |
|
1019 | - $this->access->connection->writeToCache($cacheKey, $ldap_groups); |
|
1020 | - return $ldap_groups; |
|
1021 | - } |
|
1022 | - |
|
1023 | - /** |
|
1024 | - * get a list of all groups using a paged search |
|
1025 | - * |
|
1026 | - * @param string $search |
|
1027 | - * @param int $limit |
|
1028 | - * @param int $offset |
|
1029 | - * @return array with group names |
|
1030 | - * |
|
1031 | - * Returns a list with all groups |
|
1032 | - * Uses a paged search if available to override a |
|
1033 | - * server side search limit. |
|
1034 | - * (active directory has a limit of 1000 by default) |
|
1035 | - */ |
|
1036 | - public function getGroups($search = '', $limit = -1, $offset = 0) { |
|
1037 | - if(!$this->enabled) { |
|
1038 | - return array(); |
|
1039 | - } |
|
1040 | - $search = $this->access->escapeFilterPart($search, true); |
|
1041 | - $pagingSize = (int)$this->access->connection->ldapPagingSize; |
|
1042 | - if ($pagingSize <= 0) { |
|
1043 | - return $this->getGroupsChunk($search, $limit, $offset); |
|
1044 | - } |
|
1045 | - $maxGroups = 100000; // limit max results (just for safety reasons) |
|
1046 | - if ($limit > -1) { |
|
1047 | - $overallLimit = min($limit + $offset, $maxGroups); |
|
1048 | - } else { |
|
1049 | - $overallLimit = $maxGroups; |
|
1050 | - } |
|
1051 | - $chunkOffset = $offset; |
|
1052 | - $allGroups = array(); |
|
1053 | - while ($chunkOffset < $overallLimit) { |
|
1054 | - $chunkLimit = min($pagingSize, $overallLimit - $chunkOffset); |
|
1055 | - $ldapGroups = $this->getGroupsChunk($search, $chunkLimit, $chunkOffset); |
|
1056 | - $nread = count($ldapGroups); |
|
1057 | - \OCP\Util::writeLog('user_ldap', 'getGroups('.$search.'): read '.$nread.' at offset '.$chunkOffset.' (limit: '.$chunkLimit.')', ILogger::DEBUG); |
|
1058 | - if ($nread) { |
|
1059 | - $allGroups = array_merge($allGroups, $ldapGroups); |
|
1060 | - $chunkOffset += $nread; |
|
1061 | - } |
|
1062 | - if ($nread < $chunkLimit) { |
|
1063 | - break; |
|
1064 | - } |
|
1065 | - } |
|
1066 | - return $allGroups; |
|
1067 | - } |
|
1068 | - |
|
1069 | - /** |
|
1070 | - * @param string $group |
|
1071 | - * @return bool |
|
1072 | - */ |
|
1073 | - public function groupMatchesFilter($group) { |
|
1074 | - return (strripos($group, $this->groupSearch) !== false); |
|
1075 | - } |
|
1076 | - |
|
1077 | - /** |
|
1078 | - * check if a group exists |
|
1079 | - * @param string $gid |
|
1080 | - * @return bool |
|
1081 | - */ |
|
1082 | - public function groupExists($gid) { |
|
1083 | - $groupExists = $this->access->connection->getFromCache('groupExists'.$gid); |
|
1084 | - if(!is_null($groupExists)) { |
|
1085 | - return (bool)$groupExists; |
|
1086 | - } |
|
1087 | - |
|
1088 | - //getting dn, if false the group does not exist. If dn, it may be mapped |
|
1089 | - //only, requires more checking. |
|
1090 | - $dn = $this->access->groupname2dn($gid); |
|
1091 | - if(!$dn) { |
|
1092 | - $this->access->connection->writeToCache('groupExists'.$gid, false); |
|
1093 | - return false; |
|
1094 | - } |
|
1095 | - |
|
1096 | - //if group really still exists, we will be able to read its objectclass |
|
1097 | - if(!is_array($this->access->readAttribute($dn, ''))) { |
|
1098 | - $this->access->connection->writeToCache('groupExists'.$gid, false); |
|
1099 | - return false; |
|
1100 | - } |
|
1101 | - |
|
1102 | - $this->access->connection->writeToCache('groupExists'.$gid, true); |
|
1103 | - return true; |
|
1104 | - } |
|
1105 | - |
|
1106 | - /** |
|
1107 | - * Check if backend implements actions |
|
1108 | - * @param int $actions bitwise-or'ed actions |
|
1109 | - * @return boolean |
|
1110 | - * |
|
1111 | - * Returns the supported actions as int to be |
|
1112 | - * compared with GroupInterface::CREATE_GROUP etc. |
|
1113 | - */ |
|
1114 | - public function implementsActions($actions) { |
|
1115 | - return (bool)((GroupInterface::COUNT_USERS | |
|
1116 | - $this->groupPluginManager->getImplementedActions()) & $actions); |
|
1117 | - } |
|
1118 | - |
|
1119 | - /** |
|
1120 | - * Return access for LDAP interaction. |
|
1121 | - * @return Access instance of Access for LDAP interaction |
|
1122 | - */ |
|
1123 | - public function getLDAPAccess($gid) { |
|
1124 | - return $this->access; |
|
1125 | - } |
|
1126 | - |
|
1127 | - /** |
|
1128 | - * create a group |
|
1129 | - * @param string $gid |
|
1130 | - * @return bool |
|
1131 | - * @throws \Exception |
|
1132 | - */ |
|
1133 | - public function createGroup($gid) { |
|
1134 | - if ($this->groupPluginManager->implementsActions(GroupInterface::CREATE_GROUP)) { |
|
1135 | - if ($dn = $this->groupPluginManager->createGroup($gid)) { |
|
1136 | - //updates group mapping |
|
1137 | - $uuid = $this->access->getUUID($dn, false); |
|
1138 | - if(is_string($uuid)) { |
|
1139 | - $this->access->mapAndAnnounceIfApplicable( |
|
1140 | - $this->access->getGroupMapper(), |
|
1141 | - $dn, |
|
1142 | - $gid, |
|
1143 | - $uuid, |
|
1144 | - false |
|
1145 | - ); |
|
1146 | - $this->access->connection->writeToCache("groupExists" . $gid, true); |
|
1147 | - } |
|
1148 | - } |
|
1149 | - return $dn != null; |
|
1150 | - } |
|
1151 | - throw new \Exception('Could not create group in LDAP backend.'); |
|
1152 | - } |
|
1153 | - |
|
1154 | - /** |
|
1155 | - * delete a group |
|
1156 | - * @param string $gid gid of the group to delete |
|
1157 | - * @return bool |
|
1158 | - * @throws \Exception |
|
1159 | - */ |
|
1160 | - public function deleteGroup($gid) { |
|
1161 | - if ($this->groupPluginManager->implementsActions(GroupInterface::DELETE_GROUP)) { |
|
1162 | - if ($ret = $this->groupPluginManager->deleteGroup($gid)) { |
|
1163 | - #delete group in nextcloud internal db |
|
1164 | - $this->access->getGroupMapper()->unmap($gid); |
|
1165 | - $this->access->connection->writeToCache("groupExists".$gid, false); |
|
1166 | - } |
|
1167 | - return $ret; |
|
1168 | - } |
|
1169 | - throw new \Exception('Could not delete group in LDAP backend.'); |
|
1170 | - } |
|
1171 | - |
|
1172 | - /** |
|
1173 | - * Add a user to a group |
|
1174 | - * @param string $uid Name of the user to add to group |
|
1175 | - * @param string $gid Name of the group in which add the user |
|
1176 | - * @return bool |
|
1177 | - * @throws \Exception |
|
1178 | - */ |
|
1179 | - public function addToGroup($uid, $gid) { |
|
1180 | - if ($this->groupPluginManager->implementsActions(GroupInterface::ADD_TO_GROUP)) { |
|
1181 | - if ($ret = $this->groupPluginManager->addToGroup($uid, $gid)) { |
|
1182 | - $this->access->connection->clearCache(); |
|
1183 | - unset($this->cachedGroupMembers[$gid]); |
|
1184 | - } |
|
1185 | - return $ret; |
|
1186 | - } |
|
1187 | - throw new \Exception('Could not add user to group in LDAP backend.'); |
|
1188 | - } |
|
1189 | - |
|
1190 | - /** |
|
1191 | - * Removes a user from a group |
|
1192 | - * @param string $uid Name of the user to remove from group |
|
1193 | - * @param string $gid Name of the group from which remove the user |
|
1194 | - * @return bool |
|
1195 | - * @throws \Exception |
|
1196 | - */ |
|
1197 | - public function removeFromGroup($uid, $gid) { |
|
1198 | - if ($this->groupPluginManager->implementsActions(GroupInterface::REMOVE_FROM_GROUP)) { |
|
1199 | - if ($ret = $this->groupPluginManager->removeFromGroup($uid, $gid)) { |
|
1200 | - $this->access->connection->clearCache(); |
|
1201 | - unset($this->cachedGroupMembers[$gid]); |
|
1202 | - } |
|
1203 | - return $ret; |
|
1204 | - } |
|
1205 | - throw new \Exception('Could not remove user from group in LDAP backend.'); |
|
1206 | - } |
|
1207 | - |
|
1208 | - /** |
|
1209 | - * Gets group details |
|
1210 | - * @param string $gid Name of the group |
|
1211 | - * @return array | false |
|
1212 | - * @throws \Exception |
|
1213 | - */ |
|
1214 | - public function getGroupDetails($gid) { |
|
1215 | - if ($this->groupPluginManager->implementsActions(GroupInterface::GROUP_DETAILS)) { |
|
1216 | - return $this->groupPluginManager->getGroupDetails($gid); |
|
1217 | - } |
|
1218 | - throw new \Exception('Could not get group details in LDAP backend.'); |
|
1219 | - } |
|
1220 | - |
|
1221 | - /** |
|
1222 | - * Return LDAP connection resource from a cloned connection. |
|
1223 | - * The cloned connection needs to be closed manually. |
|
1224 | - * of the current access. |
|
1225 | - * @param string $gid |
|
1226 | - * @return resource of the LDAP connection |
|
1227 | - */ |
|
1228 | - public function getNewLDAPConnection($gid) { |
|
1229 | - $connection = clone $this->access->getConnection(); |
|
1230 | - return $connection->getConnectionResource(); |
|
1231 | - } |
|
1232 | - |
|
1233 | - /** |
|
1234 | - * @throws \OC\ServerNotAvailableException |
|
1235 | - */ |
|
1236 | - public function getDisplayName(string $gid): string { |
|
1237 | - if ($this->groupPluginManager instanceof IGetDisplayNameBackend) { |
|
1238 | - return $this->groupPluginManager->getDisplayName($gid); |
|
1239 | - } |
|
1240 | - |
|
1241 | - $cacheKey = 'group_getDisplayName' . $gid; |
|
1242 | - if (!is_null($displayName = $this->access->connection->getFromCache($cacheKey))) { |
|
1243 | - return $displayName; |
|
1244 | - } |
|
1245 | - |
|
1246 | - $displayName = $this->access->readAttribute( |
|
1247 | - $this->access->groupname2dn($gid), |
|
1248 | - $this->access->connection->ldapGroupDisplayName); |
|
1249 | - |
|
1250 | - if ($displayName && (count($displayName) > 0)) { |
|
1251 | - $displayName = $displayName[0]; |
|
1252 | - $this->access->connection->writeToCache($cacheKey, $displayName); |
|
1253 | - return $displayName; |
|
1254 | - } |
|
1255 | - |
|
1256 | - return ''; |
|
1257 | - } |
|
50 | + protected $enabled = false; |
|
51 | + |
|
52 | + /** |
|
53 | + * @var string[] $cachedGroupMembers array of users with gid as key |
|
54 | + */ |
|
55 | + protected $cachedGroupMembers; |
|
56 | + |
|
57 | + /** |
|
58 | + * @var string[] $cachedGroupsByMember array of groups with uid as key |
|
59 | + */ |
|
60 | + protected $cachedGroupsByMember; |
|
61 | + |
|
62 | + /** |
|
63 | + * @var string[] $cachedNestedGroups array of groups with gid (DN) as key |
|
64 | + */ |
|
65 | + protected $cachedNestedGroups; |
|
66 | + |
|
67 | + /** @var GroupPluginManager */ |
|
68 | + protected $groupPluginManager; |
|
69 | + |
|
70 | + public function __construct(Access $access, GroupPluginManager $groupPluginManager) { |
|
71 | + parent::__construct($access); |
|
72 | + $filter = $this->access->connection->ldapGroupFilter; |
|
73 | + $gassoc = $this->access->connection->ldapGroupMemberAssocAttr; |
|
74 | + if(!empty($filter) && !empty($gassoc)) { |
|
75 | + $this->enabled = true; |
|
76 | + } |
|
77 | + |
|
78 | + $this->cachedGroupMembers = new CappedMemoryCache(); |
|
79 | + $this->cachedGroupsByMember = new CappedMemoryCache(); |
|
80 | + $this->cachedNestedGroups = new CappedMemoryCache(); |
|
81 | + $this->groupPluginManager = $groupPluginManager; |
|
82 | + } |
|
83 | + |
|
84 | + /** |
|
85 | + * is user in group? |
|
86 | + * @param string $uid uid of the user |
|
87 | + * @param string $gid gid of the group |
|
88 | + * @return bool |
|
89 | + * |
|
90 | + * Checks whether the user is member of a group or not. |
|
91 | + */ |
|
92 | + public function inGroup($uid, $gid) { |
|
93 | + if(!$this->enabled) { |
|
94 | + return false; |
|
95 | + } |
|
96 | + $cacheKey = 'inGroup'.$uid.':'.$gid; |
|
97 | + $inGroup = $this->access->connection->getFromCache($cacheKey); |
|
98 | + if(!is_null($inGroup)) { |
|
99 | + return (bool)$inGroup; |
|
100 | + } |
|
101 | + |
|
102 | + $userDN = $this->access->username2dn($uid); |
|
103 | + |
|
104 | + if(isset($this->cachedGroupMembers[$gid])) { |
|
105 | + $isInGroup = in_array($userDN, $this->cachedGroupMembers[$gid]); |
|
106 | + return $isInGroup; |
|
107 | + } |
|
108 | + |
|
109 | + $cacheKeyMembers = 'inGroup-members:'.$gid; |
|
110 | + $members = $this->access->connection->getFromCache($cacheKeyMembers); |
|
111 | + if(!is_null($members)) { |
|
112 | + $this->cachedGroupMembers[$gid] = $members; |
|
113 | + $isInGroup = in_array($userDN, $members, true); |
|
114 | + $this->access->connection->writeToCache($cacheKey, $isInGroup); |
|
115 | + return $isInGroup; |
|
116 | + } |
|
117 | + |
|
118 | + $groupDN = $this->access->groupname2dn($gid); |
|
119 | + // just in case |
|
120 | + if(!$groupDN || !$userDN) { |
|
121 | + $this->access->connection->writeToCache($cacheKey, false); |
|
122 | + return false; |
|
123 | + } |
|
124 | + |
|
125 | + //check primary group first |
|
126 | + if($gid === $this->getUserPrimaryGroup($userDN)) { |
|
127 | + $this->access->connection->writeToCache($cacheKey, true); |
|
128 | + return true; |
|
129 | + } |
|
130 | + |
|
131 | + //usually, LDAP attributes are said to be case insensitive. But there are exceptions of course. |
|
132 | + $members = $this->_groupMembers($groupDN); |
|
133 | + if(!is_array($members) || count($members) === 0) { |
|
134 | + $this->access->connection->writeToCache($cacheKey, false); |
|
135 | + return false; |
|
136 | + } |
|
137 | + |
|
138 | + //extra work if we don't get back user DNs |
|
139 | + if(strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'memberuid') { |
|
140 | + $dns = array(); |
|
141 | + $filterParts = array(); |
|
142 | + $bytes = 0; |
|
143 | + foreach($members as $mid) { |
|
144 | + $filter = str_replace('%uid', $mid, $this->access->connection->ldapLoginFilter); |
|
145 | + $filterParts[] = $filter; |
|
146 | + $bytes += strlen($filter); |
|
147 | + if($bytes >= 9000000) { |
|
148 | + // AD has a default input buffer of 10 MB, we do not want |
|
149 | + // to take even the chance to exceed it |
|
150 | + $filter = $this->access->combineFilterWithOr($filterParts); |
|
151 | + $bytes = 0; |
|
152 | + $filterParts = array(); |
|
153 | + $users = $this->access->fetchListOfUsers($filter, 'dn', count($filterParts)); |
|
154 | + $dns = array_merge($dns, $users); |
|
155 | + } |
|
156 | + } |
|
157 | + if(count($filterParts) > 0) { |
|
158 | + $filter = $this->access->combineFilterWithOr($filterParts); |
|
159 | + $users = $this->access->fetchListOfUsers($filter, 'dn', count($filterParts)); |
|
160 | + $dns = array_merge($dns, $users); |
|
161 | + } |
|
162 | + $members = $dns; |
|
163 | + } |
|
164 | + |
|
165 | + $isInGroup = in_array($userDN, $members); |
|
166 | + $this->access->connection->writeToCache($cacheKey, $isInGroup); |
|
167 | + $this->access->connection->writeToCache($cacheKeyMembers, $members); |
|
168 | + $this->cachedGroupMembers[$gid] = $members; |
|
169 | + |
|
170 | + return $isInGroup; |
|
171 | + } |
|
172 | + |
|
173 | + /** |
|
174 | + * @param string $dnGroup |
|
175 | + * @return array |
|
176 | + * |
|
177 | + * For a group that has user membership defined by an LDAP search url attribute returns the users |
|
178 | + * that match the search url otherwise returns an empty array. |
|
179 | + */ |
|
180 | + public function getDynamicGroupMembers($dnGroup) { |
|
181 | + $dynamicGroupMemberURL = strtolower($this->access->connection->ldapDynamicGroupMemberURL); |
|
182 | + |
|
183 | + if (empty($dynamicGroupMemberURL)) { |
|
184 | + return array(); |
|
185 | + } |
|
186 | + |
|
187 | + $dynamicMembers = array(); |
|
188 | + $memberURLs = $this->access->readAttribute( |
|
189 | + $dnGroup, |
|
190 | + $dynamicGroupMemberURL, |
|
191 | + $this->access->connection->ldapGroupFilter |
|
192 | + ); |
|
193 | + if ($memberURLs !== false) { |
|
194 | + // this group has the 'memberURL' attribute so this is a dynamic group |
|
195 | + // example 1: ldap:///cn=users,cn=accounts,dc=dcsubbase,dc=dcbase??one?(o=HeadOffice) |
|
196 | + // example 2: ldap:///cn=users,cn=accounts,dc=dcsubbase,dc=dcbase??one?(&(o=HeadOffice)(uidNumber>=500)) |
|
197 | + $pos = strpos($memberURLs[0], '('); |
|
198 | + if ($pos !== false) { |
|
199 | + $memberUrlFilter = substr($memberURLs[0], $pos); |
|
200 | + $foundMembers = $this->access->searchUsers($memberUrlFilter,'dn'); |
|
201 | + $dynamicMembers = array(); |
|
202 | + foreach($foundMembers as $value) { |
|
203 | + $dynamicMembers[$value['dn'][0]] = 1; |
|
204 | + } |
|
205 | + } else { |
|
206 | + \OCP\Util::writeLog('user_ldap', 'No search filter found on member url '. |
|
207 | + 'of group ' . $dnGroup, ILogger::DEBUG); |
|
208 | + } |
|
209 | + } |
|
210 | + return $dynamicMembers; |
|
211 | + } |
|
212 | + |
|
213 | + /** |
|
214 | + * @param string $dnGroup |
|
215 | + * @param array|null &$seen |
|
216 | + * @return array|mixed|null |
|
217 | + * @throws \OC\ServerNotAvailableException |
|
218 | + */ |
|
219 | + private function _groupMembers($dnGroup, &$seen = null) { |
|
220 | + if ($seen === null) { |
|
221 | + $seen = []; |
|
222 | + } |
|
223 | + $allMembers = []; |
|
224 | + if (array_key_exists($dnGroup, $seen)) { |
|
225 | + // avoid loops |
|
226 | + return []; |
|
227 | + } |
|
228 | + // used extensively in cron job, caching makes sense for nested groups |
|
229 | + $cacheKey = '_groupMembers'.$dnGroup; |
|
230 | + $groupMembers = $this->access->connection->getFromCache($cacheKey); |
|
231 | + if($groupMembers !== null) { |
|
232 | + return $groupMembers; |
|
233 | + } |
|
234 | + $seen[$dnGroup] = 1; |
|
235 | + $members = $this->access->readAttribute($dnGroup, $this->access->connection->ldapGroupMemberAssocAttr); |
|
236 | + if (is_array($members)) { |
|
237 | + $fetcher = function($memberDN, &$seen) { |
|
238 | + return $this->_groupMembers($memberDN, $seen); |
|
239 | + }; |
|
240 | + $allMembers = $this->walkNestedGroups($dnGroup, $fetcher, $members); |
|
241 | + } |
|
242 | + |
|
243 | + $allMembers += $this->getDynamicGroupMembers($dnGroup); |
|
244 | + |
|
245 | + $this->access->connection->writeToCache($cacheKey, $allMembers); |
|
246 | + return $allMembers; |
|
247 | + } |
|
248 | + |
|
249 | + /** |
|
250 | + * @param string $DN |
|
251 | + * @param array|null &$seen |
|
252 | + * @return array |
|
253 | + * @throws \OC\ServerNotAvailableException |
|
254 | + */ |
|
255 | + private function _getGroupDNsFromMemberOf($DN) { |
|
256 | + $groups = $this->access->readAttribute($DN, 'memberOf'); |
|
257 | + if (!is_array($groups)) { |
|
258 | + return []; |
|
259 | + } |
|
260 | + |
|
261 | + $fetcher = function($groupDN) { |
|
262 | + if (isset($this->cachedNestedGroups[$groupDN])) { |
|
263 | + $nestedGroups = $this->cachedNestedGroups[$groupDN]; |
|
264 | + } else { |
|
265 | + $nestedGroups = $this->access->readAttribute($groupDN, 'memberOf'); |
|
266 | + if (!is_array($nestedGroups)) { |
|
267 | + $nestedGroups = []; |
|
268 | + } |
|
269 | + $this->cachedNestedGroups[$groupDN] = $nestedGroups; |
|
270 | + } |
|
271 | + return $nestedGroups; |
|
272 | + }; |
|
273 | + |
|
274 | + $groups = $this->walkNestedGroups($DN, $fetcher, $groups); |
|
275 | + return $this->access->groupsMatchFilter($groups); |
|
276 | + } |
|
277 | + |
|
278 | + /** |
|
279 | + * @param string $dn |
|
280 | + * @param \Closure $fetcher args: string $dn, array $seen, returns: string[] of dns |
|
281 | + * @param array $list |
|
282 | + * @return array |
|
283 | + */ |
|
284 | + private function walkNestedGroups(string $dn, \Closure $fetcher, array $list): array { |
|
285 | + $nesting = (int) $this->access->connection->ldapNestedGroups; |
|
286 | + // depending on the input, we either have a list of DNs or a list of LDAP records |
|
287 | + // also, the output expects either DNs or records. Testing the first element should suffice. |
|
288 | + $recordMode = is_array($list) && isset($list[0]) && is_array($list[0]) && isset($list[0]['dn'][0]); |
|
289 | + |
|
290 | + if ($nesting !== 1) { |
|
291 | + if($recordMode) { |
|
292 | + // the keys are numeric, but should hold the DN |
|
293 | + return array_reduce($list, function ($transformed, $record) use ($dn) { |
|
294 | + if($record['dn'][0] != $dn) { |
|
295 | + $transformed[$record['dn'][0]] = $record; |
|
296 | + } |
|
297 | + return $transformed; |
|
298 | + }, []); |
|
299 | + } |
|
300 | + return $list; |
|
301 | + } |
|
302 | + |
|
303 | + $seen = []; |
|
304 | + while ($record = array_pop($list)) { |
|
305 | + $recordDN = $recordMode ? $record['dn'][0] : $record; |
|
306 | + if ($recordDN === $dn || array_key_exists($recordDN, $seen)) { |
|
307 | + // Prevent loops |
|
308 | + continue; |
|
309 | + } |
|
310 | + $fetched = $fetcher($record, $seen); |
|
311 | + $list = array_merge($list, $fetched); |
|
312 | + $seen[$recordDN] = $record; |
|
313 | + } |
|
314 | + |
|
315 | + return $recordMode ? $seen : array_keys($seen); |
|
316 | + } |
|
317 | + |
|
318 | + /** |
|
319 | + * translates a gidNumber into an ownCloud internal name |
|
320 | + * @param string $gid as given by gidNumber on POSIX LDAP |
|
321 | + * @param string $dn a DN that belongs to the same domain as the group |
|
322 | + * @return string|bool |
|
323 | + */ |
|
324 | + public function gidNumber2Name($gid, $dn) { |
|
325 | + $cacheKey = 'gidNumberToName' . $gid; |
|
326 | + $groupName = $this->access->connection->getFromCache($cacheKey); |
|
327 | + if(!is_null($groupName) && isset($groupName)) { |
|
328 | + return $groupName; |
|
329 | + } |
|
330 | + |
|
331 | + //we need to get the DN from LDAP |
|
332 | + $filter = $this->access->combineFilterWithAnd([ |
|
333 | + $this->access->connection->ldapGroupFilter, |
|
334 | + 'objectClass=posixGroup', |
|
335 | + $this->access->connection->ldapGidNumber . '=' . $gid |
|
336 | + ]); |
|
337 | + $result = $this->access->searchGroups($filter, array('dn'), 1); |
|
338 | + if(empty($result)) { |
|
339 | + return false; |
|
340 | + } |
|
341 | + $dn = $result[0]['dn'][0]; |
|
342 | + |
|
343 | + //and now the group name |
|
344 | + //NOTE once we have separate ownCloud group IDs and group names we can |
|
345 | + //directly read the display name attribute instead of the DN |
|
346 | + $name = $this->access->dn2groupname($dn); |
|
347 | + |
|
348 | + $this->access->connection->writeToCache($cacheKey, $name); |
|
349 | + |
|
350 | + return $name; |
|
351 | + } |
|
352 | + |
|
353 | + /** |
|
354 | + * returns the entry's gidNumber |
|
355 | + * @param string $dn |
|
356 | + * @param string $attribute |
|
357 | + * @return string|bool |
|
358 | + */ |
|
359 | + private function getEntryGidNumber($dn, $attribute) { |
|
360 | + $value = $this->access->readAttribute($dn, $attribute); |
|
361 | + if(is_array($value) && !empty($value)) { |
|
362 | + return $value[0]; |
|
363 | + } |
|
364 | + return false; |
|
365 | + } |
|
366 | + |
|
367 | + /** |
|
368 | + * returns the group's primary ID |
|
369 | + * @param string $dn |
|
370 | + * @return string|bool |
|
371 | + */ |
|
372 | + public function getGroupGidNumber($dn) { |
|
373 | + return $this->getEntryGidNumber($dn, 'gidNumber'); |
|
374 | + } |
|
375 | + |
|
376 | + /** |
|
377 | + * returns the user's gidNumber |
|
378 | + * @param string $dn |
|
379 | + * @return string|bool |
|
380 | + */ |
|
381 | + public function getUserGidNumber($dn) { |
|
382 | + $gidNumber = false; |
|
383 | + if($this->access->connection->hasGidNumber) { |
|
384 | + $gidNumber = $this->getEntryGidNumber($dn, $this->access->connection->ldapGidNumber); |
|
385 | + if($gidNumber === false) { |
|
386 | + $this->access->connection->hasGidNumber = false; |
|
387 | + } |
|
388 | + } |
|
389 | + return $gidNumber; |
|
390 | + } |
|
391 | + |
|
392 | + /** |
|
393 | + * returns a filter for a "users has specific gid" search or count operation |
|
394 | + * |
|
395 | + * @param string $groupDN |
|
396 | + * @param string $search |
|
397 | + * @return string |
|
398 | + * @throws \Exception |
|
399 | + */ |
|
400 | + private function prepareFilterForUsersHasGidNumber($groupDN, $search = '') { |
|
401 | + $groupID = $this->getGroupGidNumber($groupDN); |
|
402 | + if($groupID === false) { |
|
403 | + throw new \Exception('Not a valid group'); |
|
404 | + } |
|
405 | + |
|
406 | + $filterParts = []; |
|
407 | + $filterParts[] = $this->access->getFilterForUserCount(); |
|
408 | + if ($search !== '') { |
|
409 | + $filterParts[] = $this->access->getFilterPartForUserSearch($search); |
|
410 | + } |
|
411 | + $filterParts[] = $this->access->connection->ldapGidNumber .'=' . $groupID; |
|
412 | + |
|
413 | + return $this->access->combineFilterWithAnd($filterParts); |
|
414 | + } |
|
415 | + |
|
416 | + /** |
|
417 | + * returns a list of users that have the given group as gid number |
|
418 | + * |
|
419 | + * @param string $groupDN |
|
420 | + * @param string $search |
|
421 | + * @param int $limit |
|
422 | + * @param int $offset |
|
423 | + * @return string[] |
|
424 | + */ |
|
425 | + public function getUsersInGidNumber($groupDN, $search = '', $limit = -1, $offset = 0) { |
|
426 | + try { |
|
427 | + $filter = $this->prepareFilterForUsersHasGidNumber($groupDN, $search); |
|
428 | + $users = $this->access->fetchListOfUsers( |
|
429 | + $filter, |
|
430 | + [$this->access->connection->ldapUserDisplayName, 'dn'], |
|
431 | + $limit, |
|
432 | + $offset |
|
433 | + ); |
|
434 | + return $this->access->nextcloudUserNames($users); |
|
435 | + } catch (\Exception $e) { |
|
436 | + return []; |
|
437 | + } |
|
438 | + } |
|
439 | + |
|
440 | + /** |
|
441 | + * returns the number of users that have the given group as gid number |
|
442 | + * |
|
443 | + * @param string $groupDN |
|
444 | + * @param string $search |
|
445 | + * @param int $limit |
|
446 | + * @param int $offset |
|
447 | + * @return int |
|
448 | + */ |
|
449 | + public function countUsersInGidNumber($groupDN, $search = '', $limit = -1, $offset = 0) { |
|
450 | + try { |
|
451 | + $filter = $this->prepareFilterForUsersHasGidNumber($groupDN, $search); |
|
452 | + $users = $this->access->countUsers($filter, ['dn'], $limit, $offset); |
|
453 | + return (int)$users; |
|
454 | + } catch (\Exception $e) { |
|
455 | + return 0; |
|
456 | + } |
|
457 | + } |
|
458 | + |
|
459 | + /** |
|
460 | + * gets the gidNumber of a user |
|
461 | + * @param string $dn |
|
462 | + * @return string |
|
463 | + */ |
|
464 | + public function getUserGroupByGid($dn) { |
|
465 | + $groupID = $this->getUserGidNumber($dn); |
|
466 | + if($groupID !== false) { |
|
467 | + $groupName = $this->gidNumber2Name($groupID, $dn); |
|
468 | + if($groupName !== false) { |
|
469 | + return $groupName; |
|
470 | + } |
|
471 | + } |
|
472 | + |
|
473 | + return false; |
|
474 | + } |
|
475 | + |
|
476 | + /** |
|
477 | + * translates a primary group ID into an Nextcloud internal name |
|
478 | + * @param string $gid as given by primaryGroupID on AD |
|
479 | + * @param string $dn a DN that belongs to the same domain as the group |
|
480 | + * @return string|bool |
|
481 | + */ |
|
482 | + public function primaryGroupID2Name($gid, $dn) { |
|
483 | + $cacheKey = 'primaryGroupIDtoName'; |
|
484 | + $groupNames = $this->access->connection->getFromCache($cacheKey); |
|
485 | + if(!is_null($groupNames) && isset($groupNames[$gid])) { |
|
486 | + return $groupNames[$gid]; |
|
487 | + } |
|
488 | + |
|
489 | + $domainObjectSid = $this->access->getSID($dn); |
|
490 | + if($domainObjectSid === false) { |
|
491 | + return false; |
|
492 | + } |
|
493 | + |
|
494 | + //we need to get the DN from LDAP |
|
495 | + $filter = $this->access->combineFilterWithAnd(array( |
|
496 | + $this->access->connection->ldapGroupFilter, |
|
497 | + 'objectsid=' . $domainObjectSid . '-' . $gid |
|
498 | + )); |
|
499 | + $result = $this->access->searchGroups($filter, array('dn'), 1); |
|
500 | + if(empty($result)) { |
|
501 | + return false; |
|
502 | + } |
|
503 | + $dn = $result[0]['dn'][0]; |
|
504 | + |
|
505 | + //and now the group name |
|
506 | + //NOTE once we have separate Nextcloud group IDs and group names we can |
|
507 | + //directly read the display name attribute instead of the DN |
|
508 | + $name = $this->access->dn2groupname($dn); |
|
509 | + |
|
510 | + $this->access->connection->writeToCache($cacheKey, $name); |
|
511 | + |
|
512 | + return $name; |
|
513 | + } |
|
514 | + |
|
515 | + /** |
|
516 | + * returns the entry's primary group ID |
|
517 | + * @param string $dn |
|
518 | + * @param string $attribute |
|
519 | + * @return string|bool |
|
520 | + */ |
|
521 | + private function getEntryGroupID($dn, $attribute) { |
|
522 | + $value = $this->access->readAttribute($dn, $attribute); |
|
523 | + if(is_array($value) && !empty($value)) { |
|
524 | + return $value[0]; |
|
525 | + } |
|
526 | + return false; |
|
527 | + } |
|
528 | + |
|
529 | + /** |
|
530 | + * returns the group's primary ID |
|
531 | + * @param string $dn |
|
532 | + * @return string|bool |
|
533 | + */ |
|
534 | + public function getGroupPrimaryGroupID($dn) { |
|
535 | + return $this->getEntryGroupID($dn, 'primaryGroupToken'); |
|
536 | + } |
|
537 | + |
|
538 | + /** |
|
539 | + * returns the user's primary group ID |
|
540 | + * @param string $dn |
|
541 | + * @return string|bool |
|
542 | + */ |
|
543 | + public function getUserPrimaryGroupIDs($dn) { |
|
544 | + $primaryGroupID = false; |
|
545 | + if($this->access->connection->hasPrimaryGroups) { |
|
546 | + $primaryGroupID = $this->getEntryGroupID($dn, 'primaryGroupID'); |
|
547 | + if($primaryGroupID === false) { |
|
548 | + $this->access->connection->hasPrimaryGroups = false; |
|
549 | + } |
|
550 | + } |
|
551 | + return $primaryGroupID; |
|
552 | + } |
|
553 | + |
|
554 | + /** |
|
555 | + * returns a filter for a "users in primary group" search or count operation |
|
556 | + * |
|
557 | + * @param string $groupDN |
|
558 | + * @param string $search |
|
559 | + * @return string |
|
560 | + * @throws \Exception |
|
561 | + */ |
|
562 | + private function prepareFilterForUsersInPrimaryGroup($groupDN, $search = '') { |
|
563 | + $groupID = $this->getGroupPrimaryGroupID($groupDN); |
|
564 | + if($groupID === false) { |
|
565 | + throw new \Exception('Not a valid group'); |
|
566 | + } |
|
567 | + |
|
568 | + $filterParts = []; |
|
569 | + $filterParts[] = $this->access->getFilterForUserCount(); |
|
570 | + if ($search !== '') { |
|
571 | + $filterParts[] = $this->access->getFilterPartForUserSearch($search); |
|
572 | + } |
|
573 | + $filterParts[] = 'primaryGroupID=' . $groupID; |
|
574 | + |
|
575 | + return $this->access->combineFilterWithAnd($filterParts); |
|
576 | + } |
|
577 | + |
|
578 | + /** |
|
579 | + * returns a list of users that have the given group as primary group |
|
580 | + * |
|
581 | + * @param string $groupDN |
|
582 | + * @param string $search |
|
583 | + * @param int $limit |
|
584 | + * @param int $offset |
|
585 | + * @return string[] |
|
586 | + */ |
|
587 | + public function getUsersInPrimaryGroup($groupDN, $search = '', $limit = -1, $offset = 0) { |
|
588 | + try { |
|
589 | + $filter = $this->prepareFilterForUsersInPrimaryGroup($groupDN, $search); |
|
590 | + $users = $this->access->fetchListOfUsers( |
|
591 | + $filter, |
|
592 | + array($this->access->connection->ldapUserDisplayName, 'dn'), |
|
593 | + $limit, |
|
594 | + $offset |
|
595 | + ); |
|
596 | + return $this->access->nextcloudUserNames($users); |
|
597 | + } catch (\Exception $e) { |
|
598 | + return array(); |
|
599 | + } |
|
600 | + } |
|
601 | + |
|
602 | + /** |
|
603 | + * returns the number of users that have the given group as primary group |
|
604 | + * |
|
605 | + * @param string $groupDN |
|
606 | + * @param string $search |
|
607 | + * @param int $limit |
|
608 | + * @param int $offset |
|
609 | + * @return int |
|
610 | + */ |
|
611 | + public function countUsersInPrimaryGroup($groupDN, $search = '', $limit = -1, $offset = 0) { |
|
612 | + try { |
|
613 | + $filter = $this->prepareFilterForUsersInPrimaryGroup($groupDN, $search); |
|
614 | + $users = $this->access->countUsers($filter, array('dn'), $limit, $offset); |
|
615 | + return (int)$users; |
|
616 | + } catch (\Exception $e) { |
|
617 | + return 0; |
|
618 | + } |
|
619 | + } |
|
620 | + |
|
621 | + /** |
|
622 | + * gets the primary group of a user |
|
623 | + * @param string $dn |
|
624 | + * @return string |
|
625 | + */ |
|
626 | + public function getUserPrimaryGroup($dn) { |
|
627 | + $groupID = $this->getUserPrimaryGroupIDs($dn); |
|
628 | + if($groupID !== false) { |
|
629 | + $groupName = $this->primaryGroupID2Name($groupID, $dn); |
|
630 | + if($groupName !== false) { |
|
631 | + return $groupName; |
|
632 | + } |
|
633 | + } |
|
634 | + |
|
635 | + return false; |
|
636 | + } |
|
637 | + |
|
638 | + /** |
|
639 | + * Get all groups a user belongs to |
|
640 | + * @param string $uid Name of the user |
|
641 | + * @return array with group names |
|
642 | + * |
|
643 | + * This function fetches all groups a user belongs to. It does not check |
|
644 | + * if the user exists at all. |
|
645 | + * |
|
646 | + * This function includes groups based on dynamic group membership. |
|
647 | + */ |
|
648 | + public function getUserGroups($uid) { |
|
649 | + if(!$this->enabled) { |
|
650 | + return array(); |
|
651 | + } |
|
652 | + $cacheKey = 'getUserGroups'.$uid; |
|
653 | + $userGroups = $this->access->connection->getFromCache($cacheKey); |
|
654 | + if(!is_null($userGroups)) { |
|
655 | + return $userGroups; |
|
656 | + } |
|
657 | + $userDN = $this->access->username2dn($uid); |
|
658 | + if(!$userDN) { |
|
659 | + $this->access->connection->writeToCache($cacheKey, array()); |
|
660 | + return array(); |
|
661 | + } |
|
662 | + |
|
663 | + $groups = []; |
|
664 | + $primaryGroup = $this->getUserPrimaryGroup($userDN); |
|
665 | + $gidGroupName = $this->getUserGroupByGid($userDN); |
|
666 | + |
|
667 | + $dynamicGroupMemberURL = strtolower($this->access->connection->ldapDynamicGroupMemberURL); |
|
668 | + |
|
669 | + if (!empty($dynamicGroupMemberURL)) { |
|
670 | + // look through dynamic groups to add them to the result array if needed |
|
671 | + $groupsToMatch = $this->access->fetchListOfGroups( |
|
672 | + $this->access->connection->ldapGroupFilter,array('dn',$dynamicGroupMemberURL)); |
|
673 | + foreach($groupsToMatch as $dynamicGroup) { |
|
674 | + if (!array_key_exists($dynamicGroupMemberURL, $dynamicGroup)) { |
|
675 | + continue; |
|
676 | + } |
|
677 | + $pos = strpos($dynamicGroup[$dynamicGroupMemberURL][0], '('); |
|
678 | + if ($pos !== false) { |
|
679 | + $memberUrlFilter = substr($dynamicGroup[$dynamicGroupMemberURL][0],$pos); |
|
680 | + // apply filter via ldap search to see if this user is in this |
|
681 | + // dynamic group |
|
682 | + $userMatch = $this->access->readAttribute( |
|
683 | + $userDN, |
|
684 | + $this->access->connection->ldapUserDisplayName, |
|
685 | + $memberUrlFilter |
|
686 | + ); |
|
687 | + if ($userMatch !== false) { |
|
688 | + // match found so this user is in this group |
|
689 | + $groupName = $this->access->dn2groupname($dynamicGroup['dn'][0]); |
|
690 | + if(is_string($groupName)) { |
|
691 | + // be sure to never return false if the dn could not be |
|
692 | + // resolved to a name, for whatever reason. |
|
693 | + $groups[] = $groupName; |
|
694 | + } |
|
695 | + } |
|
696 | + } else { |
|
697 | + \OCP\Util::writeLog('user_ldap', 'No search filter found on member url '. |
|
698 | + 'of group ' . print_r($dynamicGroup, true), ILogger::DEBUG); |
|
699 | + } |
|
700 | + } |
|
701 | + } |
|
702 | + |
|
703 | + // if possible, read out membership via memberOf. It's far faster than |
|
704 | + // performing a search, which still is a fallback later. |
|
705 | + // memberof doesn't support memberuid, so skip it here. |
|
706 | + if((int)$this->access->connection->hasMemberOfFilterSupport === 1 |
|
707 | + && (int)$this->access->connection->useMemberOfToDetectMembership === 1 |
|
708 | + && strtolower($this->access->connection->ldapGroupMemberAssocAttr) !== 'memberuid' |
|
709 | + ) { |
|
710 | + $groupDNs = $this->_getGroupDNsFromMemberOf($userDN); |
|
711 | + if (is_array($groupDNs)) { |
|
712 | + foreach ($groupDNs as $dn) { |
|
713 | + $groupName = $this->access->dn2groupname($dn); |
|
714 | + if(is_string($groupName)) { |
|
715 | + // be sure to never return false if the dn could not be |
|
716 | + // resolved to a name, for whatever reason. |
|
717 | + $groups[] = $groupName; |
|
718 | + } |
|
719 | + } |
|
720 | + } |
|
721 | + |
|
722 | + if($primaryGroup !== false) { |
|
723 | + $groups[] = $primaryGroup; |
|
724 | + } |
|
725 | + if($gidGroupName !== false) { |
|
726 | + $groups[] = $gidGroupName; |
|
727 | + } |
|
728 | + $this->access->connection->writeToCache($cacheKey, $groups); |
|
729 | + return $groups; |
|
730 | + } |
|
731 | + |
|
732 | + //uniqueMember takes DN, memberuid the uid, so we need to distinguish |
|
733 | + if((strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'uniquemember') |
|
734 | + || (strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'member') |
|
735 | + ) { |
|
736 | + $uid = $userDN; |
|
737 | + } else if(strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'memberuid') { |
|
738 | + $result = $this->access->readAttribute($userDN, 'uid'); |
|
739 | + if ($result === false) { |
|
740 | + \OCP\Util::writeLog('user_ldap', 'No uid attribute found for DN ' . $userDN . ' on '. |
|
741 | + $this->access->connection->ldapHost, ILogger::DEBUG); |
|
742 | + } |
|
743 | + $uid = $result[0]; |
|
744 | + } else { |
|
745 | + // just in case |
|
746 | + $uid = $userDN; |
|
747 | + } |
|
748 | + |
|
749 | + if(isset($this->cachedGroupsByMember[$uid])) { |
|
750 | + $groups = array_merge($groups, $this->cachedGroupsByMember[$uid]); |
|
751 | + } else { |
|
752 | + $groupsByMember = array_values($this->getGroupsByMember($uid)); |
|
753 | + $groupsByMember = $this->access->nextcloudGroupNames($groupsByMember); |
|
754 | + $this->cachedGroupsByMember[$uid] = $groupsByMember; |
|
755 | + $groups = array_merge($groups, $groupsByMember); |
|
756 | + } |
|
757 | + |
|
758 | + if($primaryGroup !== false) { |
|
759 | + $groups[] = $primaryGroup; |
|
760 | + } |
|
761 | + if($gidGroupName !== false) { |
|
762 | + $groups[] = $gidGroupName; |
|
763 | + } |
|
764 | + |
|
765 | + $groups = array_unique($groups, SORT_LOCALE_STRING); |
|
766 | + $this->access->connection->writeToCache($cacheKey, $groups); |
|
767 | + |
|
768 | + return $groups; |
|
769 | + } |
|
770 | + |
|
771 | + /** |
|
772 | + * @param string $dn |
|
773 | + * @param array|null &$seen |
|
774 | + * @return array |
|
775 | + */ |
|
776 | + private function getGroupsByMember($dn, &$seen = null) { |
|
777 | + if ($seen === null) { |
|
778 | + $seen = []; |
|
779 | + } |
|
780 | + if (array_key_exists($dn, $seen)) { |
|
781 | + // avoid loops |
|
782 | + return []; |
|
783 | + } |
|
784 | + $allGroups = []; |
|
785 | + $seen[$dn] = true; |
|
786 | + $filter = $this->access->connection->ldapGroupMemberAssocAttr.'='.$dn; |
|
787 | + $groups = $this->access->fetchListOfGroups($filter, |
|
788 | + [$this->access->connection->ldapGroupDisplayName, 'dn']); |
|
789 | + if (is_array($groups)) { |
|
790 | + $fetcher = function ($dn, &$seen) { |
|
791 | + if(is_array($dn) && isset($dn['dn'][0])) { |
|
792 | + $dn = $dn['dn'][0]; |
|
793 | + } |
|
794 | + return $this->getGroupsByMember($dn, $seen); |
|
795 | + }; |
|
796 | + $allGroups = $this->walkNestedGroups($dn, $fetcher, $groups); |
|
797 | + } |
|
798 | + $visibleGroups = $this->access->groupsMatchFilter(array_keys($allGroups)); |
|
799 | + return array_intersect_key($allGroups, array_flip($visibleGroups)); |
|
800 | + } |
|
801 | + |
|
802 | + /** |
|
803 | + * get a list of all users in a group |
|
804 | + * |
|
805 | + * @param string $gid |
|
806 | + * @param string $search |
|
807 | + * @param int $limit |
|
808 | + * @param int $offset |
|
809 | + * @return array with user ids |
|
810 | + */ |
|
811 | + public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) { |
|
812 | + if(!$this->enabled) { |
|
813 | + return array(); |
|
814 | + } |
|
815 | + if(!$this->groupExists($gid)) { |
|
816 | + return array(); |
|
817 | + } |
|
818 | + $search = $this->access->escapeFilterPart($search, true); |
|
819 | + $cacheKey = 'usersInGroup-'.$gid.'-'.$search.'-'.$limit.'-'.$offset; |
|
820 | + // check for cache of the exact query |
|
821 | + $groupUsers = $this->access->connection->getFromCache($cacheKey); |
|
822 | + if(!is_null($groupUsers)) { |
|
823 | + return $groupUsers; |
|
824 | + } |
|
825 | + |
|
826 | + // check for cache of the query without limit and offset |
|
827 | + $groupUsers = $this->access->connection->getFromCache('usersInGroup-'.$gid.'-'.$search); |
|
828 | + if(!is_null($groupUsers)) { |
|
829 | + $groupUsers = array_slice($groupUsers, $offset, $limit); |
|
830 | + $this->access->connection->writeToCache($cacheKey, $groupUsers); |
|
831 | + return $groupUsers; |
|
832 | + } |
|
833 | + |
|
834 | + if($limit === -1) { |
|
835 | + $limit = null; |
|
836 | + } |
|
837 | + $groupDN = $this->access->groupname2dn($gid); |
|
838 | + if(!$groupDN) { |
|
839 | + // group couldn't be found, return empty resultset |
|
840 | + $this->access->connection->writeToCache($cacheKey, array()); |
|
841 | + return array(); |
|
842 | + } |
|
843 | + |
|
844 | + $primaryUsers = $this->getUsersInPrimaryGroup($groupDN, $search, $limit, $offset); |
|
845 | + $posixGroupUsers = $this->getUsersInGidNumber($groupDN, $search, $limit, $offset); |
|
846 | + $members = $this->_groupMembers($groupDN); |
|
847 | + if(!$members && empty($posixGroupUsers) && empty($primaryUsers)) { |
|
848 | + //in case users could not be retrieved, return empty result set |
|
849 | + $this->access->connection->writeToCache($cacheKey, []); |
|
850 | + return []; |
|
851 | + } |
|
852 | + |
|
853 | + $groupUsers = array(); |
|
854 | + $isMemberUid = (strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'memberuid'); |
|
855 | + $attrs = $this->access->userManager->getAttributes(true); |
|
856 | + foreach($members as $member) { |
|
857 | + if($isMemberUid) { |
|
858 | + //we got uids, need to get their DNs to 'translate' them to user names |
|
859 | + $filter = $this->access->combineFilterWithAnd(array( |
|
860 | + str_replace('%uid', trim($member), $this->access->connection->ldapLoginFilter), |
|
861 | + $this->access->getFilterPartForUserSearch($search) |
|
862 | + )); |
|
863 | + $ldap_users = $this->access->fetchListOfUsers($filter, $attrs, 1); |
|
864 | + if(count($ldap_users) < 1) { |
|
865 | + continue; |
|
866 | + } |
|
867 | + $groupUsers[] = $this->access->dn2username($ldap_users[0]['dn'][0]); |
|
868 | + } else { |
|
869 | + //we got DNs, check if we need to filter by search or we can give back all of them |
|
870 | + if ($search !== '') { |
|
871 | + if(!$this->access->readAttribute($member, |
|
872 | + $this->access->connection->ldapUserDisplayName, |
|
873 | + $this->access->getFilterPartForUserSearch($search))) { |
|
874 | + continue; |
|
875 | + } |
|
876 | + } |
|
877 | + // dn2username will also check if the users belong to the allowed base |
|
878 | + if($ocname = $this->access->dn2username($member)) { |
|
879 | + $groupUsers[] = $ocname; |
|
880 | + } |
|
881 | + } |
|
882 | + } |
|
883 | + |
|
884 | + $groupUsers = array_unique(array_merge($groupUsers, $primaryUsers, $posixGroupUsers)); |
|
885 | + natsort($groupUsers); |
|
886 | + $this->access->connection->writeToCache('usersInGroup-'.$gid.'-'.$search, $groupUsers); |
|
887 | + $groupUsers = array_slice($groupUsers, $offset, $limit); |
|
888 | + |
|
889 | + $this->access->connection->writeToCache($cacheKey, $groupUsers); |
|
890 | + |
|
891 | + return $groupUsers; |
|
892 | + } |
|
893 | + |
|
894 | + /** |
|
895 | + * returns the number of users in a group, who match the search term |
|
896 | + * @param string $gid the internal group name |
|
897 | + * @param string $search optional, a search string |
|
898 | + * @return int|bool |
|
899 | + */ |
|
900 | + public function countUsersInGroup($gid, $search = '') { |
|
901 | + if ($this->groupPluginManager->implementsActions(GroupInterface::COUNT_USERS)) { |
|
902 | + return $this->groupPluginManager->countUsersInGroup($gid, $search); |
|
903 | + } |
|
904 | + |
|
905 | + $cacheKey = 'countUsersInGroup-'.$gid.'-'.$search; |
|
906 | + if(!$this->enabled || !$this->groupExists($gid)) { |
|
907 | + return false; |
|
908 | + } |
|
909 | + $groupUsers = $this->access->connection->getFromCache($cacheKey); |
|
910 | + if(!is_null($groupUsers)) { |
|
911 | + return $groupUsers; |
|
912 | + } |
|
913 | + |
|
914 | + $groupDN = $this->access->groupname2dn($gid); |
|
915 | + if(!$groupDN) { |
|
916 | + // group couldn't be found, return empty result set |
|
917 | + $this->access->connection->writeToCache($cacheKey, false); |
|
918 | + return false; |
|
919 | + } |
|
920 | + |
|
921 | + $members = $this->_groupMembers($groupDN); |
|
922 | + $primaryUserCount = $this->countUsersInPrimaryGroup($groupDN, ''); |
|
923 | + if(!$members && $primaryUserCount === 0) { |
|
924 | + //in case users could not be retrieved, return empty result set |
|
925 | + $this->access->connection->writeToCache($cacheKey, false); |
|
926 | + return false; |
|
927 | + } |
|
928 | + |
|
929 | + if ($search === '') { |
|
930 | + $groupUsers = count($members) + $primaryUserCount; |
|
931 | + $this->access->connection->writeToCache($cacheKey, $groupUsers); |
|
932 | + return $groupUsers; |
|
933 | + } |
|
934 | + $search = $this->access->escapeFilterPart($search, true); |
|
935 | + $isMemberUid = |
|
936 | + (strtolower($this->access->connection->ldapGroupMemberAssocAttr) |
|
937 | + === 'memberuid'); |
|
938 | + |
|
939 | + //we need to apply the search filter |
|
940 | + //alternatives that need to be checked: |
|
941 | + //a) get all users by search filter and array_intersect them |
|
942 | + //b) a, but only when less than 1k 10k ?k users like it is |
|
943 | + //c) put all DNs|uids in a LDAP filter, combine with the search string |
|
944 | + // and let it count. |
|
945 | + //For now this is not important, because the only use of this method |
|
946 | + //does not supply a search string |
|
947 | + $groupUsers = array(); |
|
948 | + foreach($members as $member) { |
|
949 | + if($isMemberUid) { |
|
950 | + //we got uids, need to get their DNs to 'translate' them to user names |
|
951 | + $filter = $this->access->combineFilterWithAnd(array( |
|
952 | + str_replace('%uid', $member, $this->access->connection->ldapLoginFilter), |
|
953 | + $this->access->getFilterPartForUserSearch($search) |
|
954 | + )); |
|
955 | + $ldap_users = $this->access->fetchListOfUsers($filter, 'dn', 1); |
|
956 | + if(count($ldap_users) < 1) { |
|
957 | + continue; |
|
958 | + } |
|
959 | + $groupUsers[] = $this->access->dn2username($ldap_users[0]); |
|
960 | + } else { |
|
961 | + //we need to apply the search filter now |
|
962 | + if(!$this->access->readAttribute($member, |
|
963 | + $this->access->connection->ldapUserDisplayName, |
|
964 | + $this->access->getFilterPartForUserSearch($search))) { |
|
965 | + continue; |
|
966 | + } |
|
967 | + // dn2username will also check if the users belong to the allowed base |
|
968 | + if($ocname = $this->access->dn2username($member)) { |
|
969 | + $groupUsers[] = $ocname; |
|
970 | + } |
|
971 | + } |
|
972 | + } |
|
973 | + |
|
974 | + //and get users that have the group as primary |
|
975 | + $primaryUsers = $this->countUsersInPrimaryGroup($groupDN, $search); |
|
976 | + |
|
977 | + return count($groupUsers) + $primaryUsers; |
|
978 | + } |
|
979 | + |
|
980 | + /** |
|
981 | + * get a list of all groups |
|
982 | + * |
|
983 | + * @param string $search |
|
984 | + * @param $limit |
|
985 | + * @param int $offset |
|
986 | + * @return array with group names |
|
987 | + * |
|
988 | + * Returns a list with all groups (used by getGroups) |
|
989 | + */ |
|
990 | + protected function getGroupsChunk($search = '', $limit = -1, $offset = 0) { |
|
991 | + if(!$this->enabled) { |
|
992 | + return array(); |
|
993 | + } |
|
994 | + $cacheKey = 'getGroups-'.$search.'-'.$limit.'-'.$offset; |
|
995 | + |
|
996 | + //Check cache before driving unnecessary searches |
|
997 | + \OCP\Util::writeLog('user_ldap', 'getGroups '.$cacheKey, ILogger::DEBUG); |
|
998 | + $ldap_groups = $this->access->connection->getFromCache($cacheKey); |
|
999 | + if(!is_null($ldap_groups)) { |
|
1000 | + return $ldap_groups; |
|
1001 | + } |
|
1002 | + |
|
1003 | + // if we'd pass -1 to LDAP search, we'd end up in a Protocol |
|
1004 | + // error. With a limit of 0, we get 0 results. So we pass null. |
|
1005 | + if($limit <= 0) { |
|
1006 | + $limit = null; |
|
1007 | + } |
|
1008 | + $filter = $this->access->combineFilterWithAnd(array( |
|
1009 | + $this->access->connection->ldapGroupFilter, |
|
1010 | + $this->access->getFilterPartForGroupSearch($search) |
|
1011 | + )); |
|
1012 | + \OCP\Util::writeLog('user_ldap', 'getGroups Filter '.$filter, ILogger::DEBUG); |
|
1013 | + $ldap_groups = $this->access->fetchListOfGroups($filter, |
|
1014 | + array($this->access->connection->ldapGroupDisplayName, 'dn'), |
|
1015 | + $limit, |
|
1016 | + $offset); |
|
1017 | + $ldap_groups = $this->access->nextcloudGroupNames($ldap_groups); |
|
1018 | + |
|
1019 | + $this->access->connection->writeToCache($cacheKey, $ldap_groups); |
|
1020 | + return $ldap_groups; |
|
1021 | + } |
|
1022 | + |
|
1023 | + /** |
|
1024 | + * get a list of all groups using a paged search |
|
1025 | + * |
|
1026 | + * @param string $search |
|
1027 | + * @param int $limit |
|
1028 | + * @param int $offset |
|
1029 | + * @return array with group names |
|
1030 | + * |
|
1031 | + * Returns a list with all groups |
|
1032 | + * Uses a paged search if available to override a |
|
1033 | + * server side search limit. |
|
1034 | + * (active directory has a limit of 1000 by default) |
|
1035 | + */ |
|
1036 | + public function getGroups($search = '', $limit = -1, $offset = 0) { |
|
1037 | + if(!$this->enabled) { |
|
1038 | + return array(); |
|
1039 | + } |
|
1040 | + $search = $this->access->escapeFilterPart($search, true); |
|
1041 | + $pagingSize = (int)$this->access->connection->ldapPagingSize; |
|
1042 | + if ($pagingSize <= 0) { |
|
1043 | + return $this->getGroupsChunk($search, $limit, $offset); |
|
1044 | + } |
|
1045 | + $maxGroups = 100000; // limit max results (just for safety reasons) |
|
1046 | + if ($limit > -1) { |
|
1047 | + $overallLimit = min($limit + $offset, $maxGroups); |
|
1048 | + } else { |
|
1049 | + $overallLimit = $maxGroups; |
|
1050 | + } |
|
1051 | + $chunkOffset = $offset; |
|
1052 | + $allGroups = array(); |
|
1053 | + while ($chunkOffset < $overallLimit) { |
|
1054 | + $chunkLimit = min($pagingSize, $overallLimit - $chunkOffset); |
|
1055 | + $ldapGroups = $this->getGroupsChunk($search, $chunkLimit, $chunkOffset); |
|
1056 | + $nread = count($ldapGroups); |
|
1057 | + \OCP\Util::writeLog('user_ldap', 'getGroups('.$search.'): read '.$nread.' at offset '.$chunkOffset.' (limit: '.$chunkLimit.')', ILogger::DEBUG); |
|
1058 | + if ($nread) { |
|
1059 | + $allGroups = array_merge($allGroups, $ldapGroups); |
|
1060 | + $chunkOffset += $nread; |
|
1061 | + } |
|
1062 | + if ($nread < $chunkLimit) { |
|
1063 | + break; |
|
1064 | + } |
|
1065 | + } |
|
1066 | + return $allGroups; |
|
1067 | + } |
|
1068 | + |
|
1069 | + /** |
|
1070 | + * @param string $group |
|
1071 | + * @return bool |
|
1072 | + */ |
|
1073 | + public function groupMatchesFilter($group) { |
|
1074 | + return (strripos($group, $this->groupSearch) !== false); |
|
1075 | + } |
|
1076 | + |
|
1077 | + /** |
|
1078 | + * check if a group exists |
|
1079 | + * @param string $gid |
|
1080 | + * @return bool |
|
1081 | + */ |
|
1082 | + public function groupExists($gid) { |
|
1083 | + $groupExists = $this->access->connection->getFromCache('groupExists'.$gid); |
|
1084 | + if(!is_null($groupExists)) { |
|
1085 | + return (bool)$groupExists; |
|
1086 | + } |
|
1087 | + |
|
1088 | + //getting dn, if false the group does not exist. If dn, it may be mapped |
|
1089 | + //only, requires more checking. |
|
1090 | + $dn = $this->access->groupname2dn($gid); |
|
1091 | + if(!$dn) { |
|
1092 | + $this->access->connection->writeToCache('groupExists'.$gid, false); |
|
1093 | + return false; |
|
1094 | + } |
|
1095 | + |
|
1096 | + //if group really still exists, we will be able to read its objectclass |
|
1097 | + if(!is_array($this->access->readAttribute($dn, ''))) { |
|
1098 | + $this->access->connection->writeToCache('groupExists'.$gid, false); |
|
1099 | + return false; |
|
1100 | + } |
|
1101 | + |
|
1102 | + $this->access->connection->writeToCache('groupExists'.$gid, true); |
|
1103 | + return true; |
|
1104 | + } |
|
1105 | + |
|
1106 | + /** |
|
1107 | + * Check if backend implements actions |
|
1108 | + * @param int $actions bitwise-or'ed actions |
|
1109 | + * @return boolean |
|
1110 | + * |
|
1111 | + * Returns the supported actions as int to be |
|
1112 | + * compared with GroupInterface::CREATE_GROUP etc. |
|
1113 | + */ |
|
1114 | + public function implementsActions($actions) { |
|
1115 | + return (bool)((GroupInterface::COUNT_USERS | |
|
1116 | + $this->groupPluginManager->getImplementedActions()) & $actions); |
|
1117 | + } |
|
1118 | + |
|
1119 | + /** |
|
1120 | + * Return access for LDAP interaction. |
|
1121 | + * @return Access instance of Access for LDAP interaction |
|
1122 | + */ |
|
1123 | + public function getLDAPAccess($gid) { |
|
1124 | + return $this->access; |
|
1125 | + } |
|
1126 | + |
|
1127 | + /** |
|
1128 | + * create a group |
|
1129 | + * @param string $gid |
|
1130 | + * @return bool |
|
1131 | + * @throws \Exception |
|
1132 | + */ |
|
1133 | + public function createGroup($gid) { |
|
1134 | + if ($this->groupPluginManager->implementsActions(GroupInterface::CREATE_GROUP)) { |
|
1135 | + if ($dn = $this->groupPluginManager->createGroup($gid)) { |
|
1136 | + //updates group mapping |
|
1137 | + $uuid = $this->access->getUUID($dn, false); |
|
1138 | + if(is_string($uuid)) { |
|
1139 | + $this->access->mapAndAnnounceIfApplicable( |
|
1140 | + $this->access->getGroupMapper(), |
|
1141 | + $dn, |
|
1142 | + $gid, |
|
1143 | + $uuid, |
|
1144 | + false |
|
1145 | + ); |
|
1146 | + $this->access->connection->writeToCache("groupExists" . $gid, true); |
|
1147 | + } |
|
1148 | + } |
|
1149 | + return $dn != null; |
|
1150 | + } |
|
1151 | + throw new \Exception('Could not create group in LDAP backend.'); |
|
1152 | + } |
|
1153 | + |
|
1154 | + /** |
|
1155 | + * delete a group |
|
1156 | + * @param string $gid gid of the group to delete |
|
1157 | + * @return bool |
|
1158 | + * @throws \Exception |
|
1159 | + */ |
|
1160 | + public function deleteGroup($gid) { |
|
1161 | + if ($this->groupPluginManager->implementsActions(GroupInterface::DELETE_GROUP)) { |
|
1162 | + if ($ret = $this->groupPluginManager->deleteGroup($gid)) { |
|
1163 | + #delete group in nextcloud internal db |
|
1164 | + $this->access->getGroupMapper()->unmap($gid); |
|
1165 | + $this->access->connection->writeToCache("groupExists".$gid, false); |
|
1166 | + } |
|
1167 | + return $ret; |
|
1168 | + } |
|
1169 | + throw new \Exception('Could not delete group in LDAP backend.'); |
|
1170 | + } |
|
1171 | + |
|
1172 | + /** |
|
1173 | + * Add a user to a group |
|
1174 | + * @param string $uid Name of the user to add to group |
|
1175 | + * @param string $gid Name of the group in which add the user |
|
1176 | + * @return bool |
|
1177 | + * @throws \Exception |
|
1178 | + */ |
|
1179 | + public function addToGroup($uid, $gid) { |
|
1180 | + if ($this->groupPluginManager->implementsActions(GroupInterface::ADD_TO_GROUP)) { |
|
1181 | + if ($ret = $this->groupPluginManager->addToGroup($uid, $gid)) { |
|
1182 | + $this->access->connection->clearCache(); |
|
1183 | + unset($this->cachedGroupMembers[$gid]); |
|
1184 | + } |
|
1185 | + return $ret; |
|
1186 | + } |
|
1187 | + throw new \Exception('Could not add user to group in LDAP backend.'); |
|
1188 | + } |
|
1189 | + |
|
1190 | + /** |
|
1191 | + * Removes a user from a group |
|
1192 | + * @param string $uid Name of the user to remove from group |
|
1193 | + * @param string $gid Name of the group from which remove the user |
|
1194 | + * @return bool |
|
1195 | + * @throws \Exception |
|
1196 | + */ |
|
1197 | + public function removeFromGroup($uid, $gid) { |
|
1198 | + if ($this->groupPluginManager->implementsActions(GroupInterface::REMOVE_FROM_GROUP)) { |
|
1199 | + if ($ret = $this->groupPluginManager->removeFromGroup($uid, $gid)) { |
|
1200 | + $this->access->connection->clearCache(); |
|
1201 | + unset($this->cachedGroupMembers[$gid]); |
|
1202 | + } |
|
1203 | + return $ret; |
|
1204 | + } |
|
1205 | + throw new \Exception('Could not remove user from group in LDAP backend.'); |
|
1206 | + } |
|
1207 | + |
|
1208 | + /** |
|
1209 | + * Gets group details |
|
1210 | + * @param string $gid Name of the group |
|
1211 | + * @return array | false |
|
1212 | + * @throws \Exception |
|
1213 | + */ |
|
1214 | + public function getGroupDetails($gid) { |
|
1215 | + if ($this->groupPluginManager->implementsActions(GroupInterface::GROUP_DETAILS)) { |
|
1216 | + return $this->groupPluginManager->getGroupDetails($gid); |
|
1217 | + } |
|
1218 | + throw new \Exception('Could not get group details in LDAP backend.'); |
|
1219 | + } |
|
1220 | + |
|
1221 | + /** |
|
1222 | + * Return LDAP connection resource from a cloned connection. |
|
1223 | + * The cloned connection needs to be closed manually. |
|
1224 | + * of the current access. |
|
1225 | + * @param string $gid |
|
1226 | + * @return resource of the LDAP connection |
|
1227 | + */ |
|
1228 | + public function getNewLDAPConnection($gid) { |
|
1229 | + $connection = clone $this->access->getConnection(); |
|
1230 | + return $connection->getConnectionResource(); |
|
1231 | + } |
|
1232 | + |
|
1233 | + /** |
|
1234 | + * @throws \OC\ServerNotAvailableException |
|
1235 | + */ |
|
1236 | + public function getDisplayName(string $gid): string { |
|
1237 | + if ($this->groupPluginManager instanceof IGetDisplayNameBackend) { |
|
1238 | + return $this->groupPluginManager->getDisplayName($gid); |
|
1239 | + } |
|
1240 | + |
|
1241 | + $cacheKey = 'group_getDisplayName' . $gid; |
|
1242 | + if (!is_null($displayName = $this->access->connection->getFromCache($cacheKey))) { |
|
1243 | + return $displayName; |
|
1244 | + } |
|
1245 | + |
|
1246 | + $displayName = $this->access->readAttribute( |
|
1247 | + $this->access->groupname2dn($gid), |
|
1248 | + $this->access->connection->ldapGroupDisplayName); |
|
1249 | + |
|
1250 | + if ($displayName && (count($displayName) > 0)) { |
|
1251 | + $displayName = $displayName[0]; |
|
1252 | + $this->access->connection->writeToCache($cacheKey, $displayName); |
|
1253 | + return $displayName; |
|
1254 | + } |
|
1255 | + |
|
1256 | + return ''; |
|
1257 | + } |
|
1258 | 1258 | } |
@@ -71,7 +71,7 @@ discard block |
||
71 | 71 | parent::__construct($access); |
72 | 72 | $filter = $this->access->connection->ldapGroupFilter; |
73 | 73 | $gassoc = $this->access->connection->ldapGroupMemberAssocAttr; |
74 | - if(!empty($filter) && !empty($gassoc)) { |
|
74 | + if (!empty($filter) && !empty($gassoc)) { |
|
75 | 75 | $this->enabled = true; |
76 | 76 | } |
77 | 77 | |
@@ -90,25 +90,25 @@ discard block |
||
90 | 90 | * Checks whether the user is member of a group or not. |
91 | 91 | */ |
92 | 92 | public function inGroup($uid, $gid) { |
93 | - if(!$this->enabled) { |
|
93 | + if (!$this->enabled) { |
|
94 | 94 | return false; |
95 | 95 | } |
96 | 96 | $cacheKey = 'inGroup'.$uid.':'.$gid; |
97 | 97 | $inGroup = $this->access->connection->getFromCache($cacheKey); |
98 | - if(!is_null($inGroup)) { |
|
99 | - return (bool)$inGroup; |
|
98 | + if (!is_null($inGroup)) { |
|
99 | + return (bool) $inGroup; |
|
100 | 100 | } |
101 | 101 | |
102 | 102 | $userDN = $this->access->username2dn($uid); |
103 | 103 | |
104 | - if(isset($this->cachedGroupMembers[$gid])) { |
|
104 | + if (isset($this->cachedGroupMembers[$gid])) { |
|
105 | 105 | $isInGroup = in_array($userDN, $this->cachedGroupMembers[$gid]); |
106 | 106 | return $isInGroup; |
107 | 107 | } |
108 | 108 | |
109 | 109 | $cacheKeyMembers = 'inGroup-members:'.$gid; |
110 | 110 | $members = $this->access->connection->getFromCache($cacheKeyMembers); |
111 | - if(!is_null($members)) { |
|
111 | + if (!is_null($members)) { |
|
112 | 112 | $this->cachedGroupMembers[$gid] = $members; |
113 | 113 | $isInGroup = in_array($userDN, $members, true); |
114 | 114 | $this->access->connection->writeToCache($cacheKey, $isInGroup); |
@@ -117,34 +117,34 @@ discard block |
||
117 | 117 | |
118 | 118 | $groupDN = $this->access->groupname2dn($gid); |
119 | 119 | // just in case |
120 | - if(!$groupDN || !$userDN) { |
|
120 | + if (!$groupDN || !$userDN) { |
|
121 | 121 | $this->access->connection->writeToCache($cacheKey, false); |
122 | 122 | return false; |
123 | 123 | } |
124 | 124 | |
125 | 125 | //check primary group first |
126 | - if($gid === $this->getUserPrimaryGroup($userDN)) { |
|
126 | + if ($gid === $this->getUserPrimaryGroup($userDN)) { |
|
127 | 127 | $this->access->connection->writeToCache($cacheKey, true); |
128 | 128 | return true; |
129 | 129 | } |
130 | 130 | |
131 | 131 | //usually, LDAP attributes are said to be case insensitive. But there are exceptions of course. |
132 | 132 | $members = $this->_groupMembers($groupDN); |
133 | - if(!is_array($members) || count($members) === 0) { |
|
133 | + if (!is_array($members) || count($members) === 0) { |
|
134 | 134 | $this->access->connection->writeToCache($cacheKey, false); |
135 | 135 | return false; |
136 | 136 | } |
137 | 137 | |
138 | 138 | //extra work if we don't get back user DNs |
139 | - if(strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'memberuid') { |
|
139 | + if (strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'memberuid') { |
|
140 | 140 | $dns = array(); |
141 | 141 | $filterParts = array(); |
142 | 142 | $bytes = 0; |
143 | - foreach($members as $mid) { |
|
143 | + foreach ($members as $mid) { |
|
144 | 144 | $filter = str_replace('%uid', $mid, $this->access->connection->ldapLoginFilter); |
145 | 145 | $filterParts[] = $filter; |
146 | 146 | $bytes += strlen($filter); |
147 | - if($bytes >= 9000000) { |
|
147 | + if ($bytes >= 9000000) { |
|
148 | 148 | // AD has a default input buffer of 10 MB, we do not want |
149 | 149 | // to take even the chance to exceed it |
150 | 150 | $filter = $this->access->combineFilterWithOr($filterParts); |
@@ -154,7 +154,7 @@ discard block |
||
154 | 154 | $dns = array_merge($dns, $users); |
155 | 155 | } |
156 | 156 | } |
157 | - if(count($filterParts) > 0) { |
|
157 | + if (count($filterParts) > 0) { |
|
158 | 158 | $filter = $this->access->combineFilterWithOr($filterParts); |
159 | 159 | $users = $this->access->fetchListOfUsers($filter, 'dn', count($filterParts)); |
160 | 160 | $dns = array_merge($dns, $users); |
@@ -197,14 +197,14 @@ discard block |
||
197 | 197 | $pos = strpos($memberURLs[0], '('); |
198 | 198 | if ($pos !== false) { |
199 | 199 | $memberUrlFilter = substr($memberURLs[0], $pos); |
200 | - $foundMembers = $this->access->searchUsers($memberUrlFilter,'dn'); |
|
200 | + $foundMembers = $this->access->searchUsers($memberUrlFilter, 'dn'); |
|
201 | 201 | $dynamicMembers = array(); |
202 | - foreach($foundMembers as $value) { |
|
202 | + foreach ($foundMembers as $value) { |
|
203 | 203 | $dynamicMembers[$value['dn'][0]] = 1; |
204 | 204 | } |
205 | 205 | } else { |
206 | 206 | \OCP\Util::writeLog('user_ldap', 'No search filter found on member url '. |
207 | - 'of group ' . $dnGroup, ILogger::DEBUG); |
|
207 | + 'of group '.$dnGroup, ILogger::DEBUG); |
|
208 | 208 | } |
209 | 209 | } |
210 | 210 | return $dynamicMembers; |
@@ -228,7 +228,7 @@ discard block |
||
228 | 228 | // used extensively in cron job, caching makes sense for nested groups |
229 | 229 | $cacheKey = '_groupMembers'.$dnGroup; |
230 | 230 | $groupMembers = $this->access->connection->getFromCache($cacheKey); |
231 | - if($groupMembers !== null) { |
|
231 | + if ($groupMembers !== null) { |
|
232 | 232 | return $groupMembers; |
233 | 233 | } |
234 | 234 | $seen[$dnGroup] = 1; |
@@ -288,10 +288,10 @@ discard block |
||
288 | 288 | $recordMode = is_array($list) && isset($list[0]) && is_array($list[0]) && isset($list[0]['dn'][0]); |
289 | 289 | |
290 | 290 | if ($nesting !== 1) { |
291 | - if($recordMode) { |
|
291 | + if ($recordMode) { |
|
292 | 292 | // the keys are numeric, but should hold the DN |
293 | - return array_reduce($list, function ($transformed, $record) use ($dn) { |
|
294 | - if($record['dn'][0] != $dn) { |
|
293 | + return array_reduce($list, function($transformed, $record) use ($dn) { |
|
294 | + if ($record['dn'][0] != $dn) { |
|
295 | 295 | $transformed[$record['dn'][0]] = $record; |
296 | 296 | } |
297 | 297 | return $transformed; |
@@ -322,9 +322,9 @@ discard block |
||
322 | 322 | * @return string|bool |
323 | 323 | */ |
324 | 324 | public function gidNumber2Name($gid, $dn) { |
325 | - $cacheKey = 'gidNumberToName' . $gid; |
|
325 | + $cacheKey = 'gidNumberToName'.$gid; |
|
326 | 326 | $groupName = $this->access->connection->getFromCache($cacheKey); |
327 | - if(!is_null($groupName) && isset($groupName)) { |
|
327 | + if (!is_null($groupName) && isset($groupName)) { |
|
328 | 328 | return $groupName; |
329 | 329 | } |
330 | 330 | |
@@ -332,10 +332,10 @@ discard block |
||
332 | 332 | $filter = $this->access->combineFilterWithAnd([ |
333 | 333 | $this->access->connection->ldapGroupFilter, |
334 | 334 | 'objectClass=posixGroup', |
335 | - $this->access->connection->ldapGidNumber . '=' . $gid |
|
335 | + $this->access->connection->ldapGidNumber.'='.$gid |
|
336 | 336 | ]); |
337 | 337 | $result = $this->access->searchGroups($filter, array('dn'), 1); |
338 | - if(empty($result)) { |
|
338 | + if (empty($result)) { |
|
339 | 339 | return false; |
340 | 340 | } |
341 | 341 | $dn = $result[0]['dn'][0]; |
@@ -358,7 +358,7 @@ discard block |
||
358 | 358 | */ |
359 | 359 | private function getEntryGidNumber($dn, $attribute) { |
360 | 360 | $value = $this->access->readAttribute($dn, $attribute); |
361 | - if(is_array($value) && !empty($value)) { |
|
361 | + if (is_array($value) && !empty($value)) { |
|
362 | 362 | return $value[0]; |
363 | 363 | } |
364 | 364 | return false; |
@@ -380,9 +380,9 @@ discard block |
||
380 | 380 | */ |
381 | 381 | public function getUserGidNumber($dn) { |
382 | 382 | $gidNumber = false; |
383 | - if($this->access->connection->hasGidNumber) { |
|
383 | + if ($this->access->connection->hasGidNumber) { |
|
384 | 384 | $gidNumber = $this->getEntryGidNumber($dn, $this->access->connection->ldapGidNumber); |
385 | - if($gidNumber === false) { |
|
385 | + if ($gidNumber === false) { |
|
386 | 386 | $this->access->connection->hasGidNumber = false; |
387 | 387 | } |
388 | 388 | } |
@@ -399,7 +399,7 @@ discard block |
||
399 | 399 | */ |
400 | 400 | private function prepareFilterForUsersHasGidNumber($groupDN, $search = '') { |
401 | 401 | $groupID = $this->getGroupGidNumber($groupDN); |
402 | - if($groupID === false) { |
|
402 | + if ($groupID === false) { |
|
403 | 403 | throw new \Exception('Not a valid group'); |
404 | 404 | } |
405 | 405 | |
@@ -408,7 +408,7 @@ discard block |
||
408 | 408 | if ($search !== '') { |
409 | 409 | $filterParts[] = $this->access->getFilterPartForUserSearch($search); |
410 | 410 | } |
411 | - $filterParts[] = $this->access->connection->ldapGidNumber .'=' . $groupID; |
|
411 | + $filterParts[] = $this->access->connection->ldapGidNumber.'='.$groupID; |
|
412 | 412 | |
413 | 413 | return $this->access->combineFilterWithAnd($filterParts); |
414 | 414 | } |
@@ -450,7 +450,7 @@ discard block |
||
450 | 450 | try { |
451 | 451 | $filter = $this->prepareFilterForUsersHasGidNumber($groupDN, $search); |
452 | 452 | $users = $this->access->countUsers($filter, ['dn'], $limit, $offset); |
453 | - return (int)$users; |
|
453 | + return (int) $users; |
|
454 | 454 | } catch (\Exception $e) { |
455 | 455 | return 0; |
456 | 456 | } |
@@ -463,9 +463,9 @@ discard block |
||
463 | 463 | */ |
464 | 464 | public function getUserGroupByGid($dn) { |
465 | 465 | $groupID = $this->getUserGidNumber($dn); |
466 | - if($groupID !== false) { |
|
466 | + if ($groupID !== false) { |
|
467 | 467 | $groupName = $this->gidNumber2Name($groupID, $dn); |
468 | - if($groupName !== false) { |
|
468 | + if ($groupName !== false) { |
|
469 | 469 | return $groupName; |
470 | 470 | } |
471 | 471 | } |
@@ -482,22 +482,22 @@ discard block |
||
482 | 482 | public function primaryGroupID2Name($gid, $dn) { |
483 | 483 | $cacheKey = 'primaryGroupIDtoName'; |
484 | 484 | $groupNames = $this->access->connection->getFromCache($cacheKey); |
485 | - if(!is_null($groupNames) && isset($groupNames[$gid])) { |
|
485 | + if (!is_null($groupNames) && isset($groupNames[$gid])) { |
|
486 | 486 | return $groupNames[$gid]; |
487 | 487 | } |
488 | 488 | |
489 | 489 | $domainObjectSid = $this->access->getSID($dn); |
490 | - if($domainObjectSid === false) { |
|
490 | + if ($domainObjectSid === false) { |
|
491 | 491 | return false; |
492 | 492 | } |
493 | 493 | |
494 | 494 | //we need to get the DN from LDAP |
495 | 495 | $filter = $this->access->combineFilterWithAnd(array( |
496 | 496 | $this->access->connection->ldapGroupFilter, |
497 | - 'objectsid=' . $domainObjectSid . '-' . $gid |
|
497 | + 'objectsid='.$domainObjectSid.'-'.$gid |
|
498 | 498 | )); |
499 | 499 | $result = $this->access->searchGroups($filter, array('dn'), 1); |
500 | - if(empty($result)) { |
|
500 | + if (empty($result)) { |
|
501 | 501 | return false; |
502 | 502 | } |
503 | 503 | $dn = $result[0]['dn'][0]; |
@@ -520,7 +520,7 @@ discard block |
||
520 | 520 | */ |
521 | 521 | private function getEntryGroupID($dn, $attribute) { |
522 | 522 | $value = $this->access->readAttribute($dn, $attribute); |
523 | - if(is_array($value) && !empty($value)) { |
|
523 | + if (is_array($value) && !empty($value)) { |
|
524 | 524 | return $value[0]; |
525 | 525 | } |
526 | 526 | return false; |
@@ -542,9 +542,9 @@ discard block |
||
542 | 542 | */ |
543 | 543 | public function getUserPrimaryGroupIDs($dn) { |
544 | 544 | $primaryGroupID = false; |
545 | - if($this->access->connection->hasPrimaryGroups) { |
|
545 | + if ($this->access->connection->hasPrimaryGroups) { |
|
546 | 546 | $primaryGroupID = $this->getEntryGroupID($dn, 'primaryGroupID'); |
547 | - if($primaryGroupID === false) { |
|
547 | + if ($primaryGroupID === false) { |
|
548 | 548 | $this->access->connection->hasPrimaryGroups = false; |
549 | 549 | } |
550 | 550 | } |
@@ -561,7 +561,7 @@ discard block |
||
561 | 561 | */ |
562 | 562 | private function prepareFilterForUsersInPrimaryGroup($groupDN, $search = '') { |
563 | 563 | $groupID = $this->getGroupPrimaryGroupID($groupDN); |
564 | - if($groupID === false) { |
|
564 | + if ($groupID === false) { |
|
565 | 565 | throw new \Exception('Not a valid group'); |
566 | 566 | } |
567 | 567 | |
@@ -570,7 +570,7 @@ discard block |
||
570 | 570 | if ($search !== '') { |
571 | 571 | $filterParts[] = $this->access->getFilterPartForUserSearch($search); |
572 | 572 | } |
573 | - $filterParts[] = 'primaryGroupID=' . $groupID; |
|
573 | + $filterParts[] = 'primaryGroupID='.$groupID; |
|
574 | 574 | |
575 | 575 | return $this->access->combineFilterWithAnd($filterParts); |
576 | 576 | } |
@@ -612,7 +612,7 @@ discard block |
||
612 | 612 | try { |
613 | 613 | $filter = $this->prepareFilterForUsersInPrimaryGroup($groupDN, $search); |
614 | 614 | $users = $this->access->countUsers($filter, array('dn'), $limit, $offset); |
615 | - return (int)$users; |
|
615 | + return (int) $users; |
|
616 | 616 | } catch (\Exception $e) { |
617 | 617 | return 0; |
618 | 618 | } |
@@ -625,9 +625,9 @@ discard block |
||
625 | 625 | */ |
626 | 626 | public function getUserPrimaryGroup($dn) { |
627 | 627 | $groupID = $this->getUserPrimaryGroupIDs($dn); |
628 | - if($groupID !== false) { |
|
628 | + if ($groupID !== false) { |
|
629 | 629 | $groupName = $this->primaryGroupID2Name($groupID, $dn); |
630 | - if($groupName !== false) { |
|
630 | + if ($groupName !== false) { |
|
631 | 631 | return $groupName; |
632 | 632 | } |
633 | 633 | } |
@@ -646,16 +646,16 @@ discard block |
||
646 | 646 | * This function includes groups based on dynamic group membership. |
647 | 647 | */ |
648 | 648 | public function getUserGroups($uid) { |
649 | - if(!$this->enabled) { |
|
649 | + if (!$this->enabled) { |
|
650 | 650 | return array(); |
651 | 651 | } |
652 | 652 | $cacheKey = 'getUserGroups'.$uid; |
653 | 653 | $userGroups = $this->access->connection->getFromCache($cacheKey); |
654 | - if(!is_null($userGroups)) { |
|
654 | + if (!is_null($userGroups)) { |
|
655 | 655 | return $userGroups; |
656 | 656 | } |
657 | 657 | $userDN = $this->access->username2dn($uid); |
658 | - if(!$userDN) { |
|
658 | + if (!$userDN) { |
|
659 | 659 | $this->access->connection->writeToCache($cacheKey, array()); |
660 | 660 | return array(); |
661 | 661 | } |
@@ -669,14 +669,14 @@ discard block |
||
669 | 669 | if (!empty($dynamicGroupMemberURL)) { |
670 | 670 | // look through dynamic groups to add them to the result array if needed |
671 | 671 | $groupsToMatch = $this->access->fetchListOfGroups( |
672 | - $this->access->connection->ldapGroupFilter,array('dn',$dynamicGroupMemberURL)); |
|
673 | - foreach($groupsToMatch as $dynamicGroup) { |
|
672 | + $this->access->connection->ldapGroupFilter, array('dn', $dynamicGroupMemberURL)); |
|
673 | + foreach ($groupsToMatch as $dynamicGroup) { |
|
674 | 674 | if (!array_key_exists($dynamicGroupMemberURL, $dynamicGroup)) { |
675 | 675 | continue; |
676 | 676 | } |
677 | 677 | $pos = strpos($dynamicGroup[$dynamicGroupMemberURL][0], '('); |
678 | 678 | if ($pos !== false) { |
679 | - $memberUrlFilter = substr($dynamicGroup[$dynamicGroupMemberURL][0],$pos); |
|
679 | + $memberUrlFilter = substr($dynamicGroup[$dynamicGroupMemberURL][0], $pos); |
|
680 | 680 | // apply filter via ldap search to see if this user is in this |
681 | 681 | // dynamic group |
682 | 682 | $userMatch = $this->access->readAttribute( |
@@ -687,7 +687,7 @@ discard block |
||
687 | 687 | if ($userMatch !== false) { |
688 | 688 | // match found so this user is in this group |
689 | 689 | $groupName = $this->access->dn2groupname($dynamicGroup['dn'][0]); |
690 | - if(is_string($groupName)) { |
|
690 | + if (is_string($groupName)) { |
|
691 | 691 | // be sure to never return false if the dn could not be |
692 | 692 | // resolved to a name, for whatever reason. |
693 | 693 | $groups[] = $groupName; |
@@ -695,7 +695,7 @@ discard block |
||
695 | 695 | } |
696 | 696 | } else { |
697 | 697 | \OCP\Util::writeLog('user_ldap', 'No search filter found on member url '. |
698 | - 'of group ' . print_r($dynamicGroup, true), ILogger::DEBUG); |
|
698 | + 'of group '.print_r($dynamicGroup, true), ILogger::DEBUG); |
|
699 | 699 | } |
700 | 700 | } |
701 | 701 | } |
@@ -703,15 +703,15 @@ discard block |
||
703 | 703 | // if possible, read out membership via memberOf. It's far faster than |
704 | 704 | // performing a search, which still is a fallback later. |
705 | 705 | // memberof doesn't support memberuid, so skip it here. |
706 | - if((int)$this->access->connection->hasMemberOfFilterSupport === 1 |
|
707 | - && (int)$this->access->connection->useMemberOfToDetectMembership === 1 |
|
706 | + if ((int) $this->access->connection->hasMemberOfFilterSupport === 1 |
|
707 | + && (int) $this->access->connection->useMemberOfToDetectMembership === 1 |
|
708 | 708 | && strtolower($this->access->connection->ldapGroupMemberAssocAttr) !== 'memberuid' |
709 | 709 | ) { |
710 | 710 | $groupDNs = $this->_getGroupDNsFromMemberOf($userDN); |
711 | 711 | if (is_array($groupDNs)) { |
712 | 712 | foreach ($groupDNs as $dn) { |
713 | 713 | $groupName = $this->access->dn2groupname($dn); |
714 | - if(is_string($groupName)) { |
|
714 | + if (is_string($groupName)) { |
|
715 | 715 | // be sure to never return false if the dn could not be |
716 | 716 | // resolved to a name, for whatever reason. |
717 | 717 | $groups[] = $groupName; |
@@ -719,10 +719,10 @@ discard block |
||
719 | 719 | } |
720 | 720 | } |
721 | 721 | |
722 | - if($primaryGroup !== false) { |
|
722 | + if ($primaryGroup !== false) { |
|
723 | 723 | $groups[] = $primaryGroup; |
724 | 724 | } |
725 | - if($gidGroupName !== false) { |
|
725 | + if ($gidGroupName !== false) { |
|
726 | 726 | $groups[] = $gidGroupName; |
727 | 727 | } |
728 | 728 | $this->access->connection->writeToCache($cacheKey, $groups); |
@@ -730,14 +730,14 @@ discard block |
||
730 | 730 | } |
731 | 731 | |
732 | 732 | //uniqueMember takes DN, memberuid the uid, so we need to distinguish |
733 | - if((strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'uniquemember') |
|
733 | + if ((strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'uniquemember') |
|
734 | 734 | || (strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'member') |
735 | 735 | ) { |
736 | 736 | $uid = $userDN; |
737 | - } else if(strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'memberuid') { |
|
737 | + } else if (strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'memberuid') { |
|
738 | 738 | $result = $this->access->readAttribute($userDN, 'uid'); |
739 | 739 | if ($result === false) { |
740 | - \OCP\Util::writeLog('user_ldap', 'No uid attribute found for DN ' . $userDN . ' on '. |
|
740 | + \OCP\Util::writeLog('user_ldap', 'No uid attribute found for DN '.$userDN.' on '. |
|
741 | 741 | $this->access->connection->ldapHost, ILogger::DEBUG); |
742 | 742 | } |
743 | 743 | $uid = $result[0]; |
@@ -746,7 +746,7 @@ discard block |
||
746 | 746 | $uid = $userDN; |
747 | 747 | } |
748 | 748 | |
749 | - if(isset($this->cachedGroupsByMember[$uid])) { |
|
749 | + if (isset($this->cachedGroupsByMember[$uid])) { |
|
750 | 750 | $groups = array_merge($groups, $this->cachedGroupsByMember[$uid]); |
751 | 751 | } else { |
752 | 752 | $groupsByMember = array_values($this->getGroupsByMember($uid)); |
@@ -755,10 +755,10 @@ discard block |
||
755 | 755 | $groups = array_merge($groups, $groupsByMember); |
756 | 756 | } |
757 | 757 | |
758 | - if($primaryGroup !== false) { |
|
758 | + if ($primaryGroup !== false) { |
|
759 | 759 | $groups[] = $primaryGroup; |
760 | 760 | } |
761 | - if($gidGroupName !== false) { |
|
761 | + if ($gidGroupName !== false) { |
|
762 | 762 | $groups[] = $gidGroupName; |
763 | 763 | } |
764 | 764 | |
@@ -787,8 +787,8 @@ discard block |
||
787 | 787 | $groups = $this->access->fetchListOfGroups($filter, |
788 | 788 | [$this->access->connection->ldapGroupDisplayName, 'dn']); |
789 | 789 | if (is_array($groups)) { |
790 | - $fetcher = function ($dn, &$seen) { |
|
791 | - if(is_array($dn) && isset($dn['dn'][0])) { |
|
790 | + $fetcher = function($dn, &$seen) { |
|
791 | + if (is_array($dn) && isset($dn['dn'][0])) { |
|
792 | 792 | $dn = $dn['dn'][0]; |
793 | 793 | } |
794 | 794 | return $this->getGroupsByMember($dn, $seen); |
@@ -809,33 +809,33 @@ discard block |
||
809 | 809 | * @return array with user ids |
810 | 810 | */ |
811 | 811 | public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) { |
812 | - if(!$this->enabled) { |
|
812 | + if (!$this->enabled) { |
|
813 | 813 | return array(); |
814 | 814 | } |
815 | - if(!$this->groupExists($gid)) { |
|
815 | + if (!$this->groupExists($gid)) { |
|
816 | 816 | return array(); |
817 | 817 | } |
818 | 818 | $search = $this->access->escapeFilterPart($search, true); |
819 | 819 | $cacheKey = 'usersInGroup-'.$gid.'-'.$search.'-'.$limit.'-'.$offset; |
820 | 820 | // check for cache of the exact query |
821 | 821 | $groupUsers = $this->access->connection->getFromCache($cacheKey); |
822 | - if(!is_null($groupUsers)) { |
|
822 | + if (!is_null($groupUsers)) { |
|
823 | 823 | return $groupUsers; |
824 | 824 | } |
825 | 825 | |
826 | 826 | // check for cache of the query without limit and offset |
827 | 827 | $groupUsers = $this->access->connection->getFromCache('usersInGroup-'.$gid.'-'.$search); |
828 | - if(!is_null($groupUsers)) { |
|
828 | + if (!is_null($groupUsers)) { |
|
829 | 829 | $groupUsers = array_slice($groupUsers, $offset, $limit); |
830 | 830 | $this->access->connection->writeToCache($cacheKey, $groupUsers); |
831 | 831 | return $groupUsers; |
832 | 832 | } |
833 | 833 | |
834 | - if($limit === -1) { |
|
834 | + if ($limit === -1) { |
|
835 | 835 | $limit = null; |
836 | 836 | } |
837 | 837 | $groupDN = $this->access->groupname2dn($gid); |
838 | - if(!$groupDN) { |
|
838 | + if (!$groupDN) { |
|
839 | 839 | // group couldn't be found, return empty resultset |
840 | 840 | $this->access->connection->writeToCache($cacheKey, array()); |
841 | 841 | return array(); |
@@ -844,7 +844,7 @@ discard block |
||
844 | 844 | $primaryUsers = $this->getUsersInPrimaryGroup($groupDN, $search, $limit, $offset); |
845 | 845 | $posixGroupUsers = $this->getUsersInGidNumber($groupDN, $search, $limit, $offset); |
846 | 846 | $members = $this->_groupMembers($groupDN); |
847 | - if(!$members && empty($posixGroupUsers) && empty($primaryUsers)) { |
|
847 | + if (!$members && empty($posixGroupUsers) && empty($primaryUsers)) { |
|
848 | 848 | //in case users could not be retrieved, return empty result set |
849 | 849 | $this->access->connection->writeToCache($cacheKey, []); |
850 | 850 | return []; |
@@ -853,29 +853,29 @@ discard block |
||
853 | 853 | $groupUsers = array(); |
854 | 854 | $isMemberUid = (strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'memberuid'); |
855 | 855 | $attrs = $this->access->userManager->getAttributes(true); |
856 | - foreach($members as $member) { |
|
857 | - if($isMemberUid) { |
|
856 | + foreach ($members as $member) { |
|
857 | + if ($isMemberUid) { |
|
858 | 858 | //we got uids, need to get their DNs to 'translate' them to user names |
859 | 859 | $filter = $this->access->combineFilterWithAnd(array( |
860 | 860 | str_replace('%uid', trim($member), $this->access->connection->ldapLoginFilter), |
861 | 861 | $this->access->getFilterPartForUserSearch($search) |
862 | 862 | )); |
863 | 863 | $ldap_users = $this->access->fetchListOfUsers($filter, $attrs, 1); |
864 | - if(count($ldap_users) < 1) { |
|
864 | + if (count($ldap_users) < 1) { |
|
865 | 865 | continue; |
866 | 866 | } |
867 | 867 | $groupUsers[] = $this->access->dn2username($ldap_users[0]['dn'][0]); |
868 | 868 | } else { |
869 | 869 | //we got DNs, check if we need to filter by search or we can give back all of them |
870 | 870 | if ($search !== '') { |
871 | - if(!$this->access->readAttribute($member, |
|
871 | + if (!$this->access->readAttribute($member, |
|
872 | 872 | $this->access->connection->ldapUserDisplayName, |
873 | 873 | $this->access->getFilterPartForUserSearch($search))) { |
874 | 874 | continue; |
875 | 875 | } |
876 | 876 | } |
877 | 877 | // dn2username will also check if the users belong to the allowed base |
878 | - if($ocname = $this->access->dn2username($member)) { |
|
878 | + if ($ocname = $this->access->dn2username($member)) { |
|
879 | 879 | $groupUsers[] = $ocname; |
880 | 880 | } |
881 | 881 | } |
@@ -903,16 +903,16 @@ discard block |
||
903 | 903 | } |
904 | 904 | |
905 | 905 | $cacheKey = 'countUsersInGroup-'.$gid.'-'.$search; |
906 | - if(!$this->enabled || !$this->groupExists($gid)) { |
|
906 | + if (!$this->enabled || !$this->groupExists($gid)) { |
|
907 | 907 | return false; |
908 | 908 | } |
909 | 909 | $groupUsers = $this->access->connection->getFromCache($cacheKey); |
910 | - if(!is_null($groupUsers)) { |
|
910 | + if (!is_null($groupUsers)) { |
|
911 | 911 | return $groupUsers; |
912 | 912 | } |
913 | 913 | |
914 | 914 | $groupDN = $this->access->groupname2dn($gid); |
915 | - if(!$groupDN) { |
|
915 | + if (!$groupDN) { |
|
916 | 916 | // group couldn't be found, return empty result set |
917 | 917 | $this->access->connection->writeToCache($cacheKey, false); |
918 | 918 | return false; |
@@ -920,7 +920,7 @@ discard block |
||
920 | 920 | |
921 | 921 | $members = $this->_groupMembers($groupDN); |
922 | 922 | $primaryUserCount = $this->countUsersInPrimaryGroup($groupDN, ''); |
923 | - if(!$members && $primaryUserCount === 0) { |
|
923 | + if (!$members && $primaryUserCount === 0) { |
|
924 | 924 | //in case users could not be retrieved, return empty result set |
925 | 925 | $this->access->connection->writeToCache($cacheKey, false); |
926 | 926 | return false; |
@@ -945,27 +945,27 @@ discard block |
||
945 | 945 | //For now this is not important, because the only use of this method |
946 | 946 | //does not supply a search string |
947 | 947 | $groupUsers = array(); |
948 | - foreach($members as $member) { |
|
949 | - if($isMemberUid) { |
|
948 | + foreach ($members as $member) { |
|
949 | + if ($isMemberUid) { |
|
950 | 950 | //we got uids, need to get their DNs to 'translate' them to user names |
951 | 951 | $filter = $this->access->combineFilterWithAnd(array( |
952 | 952 | str_replace('%uid', $member, $this->access->connection->ldapLoginFilter), |
953 | 953 | $this->access->getFilterPartForUserSearch($search) |
954 | 954 | )); |
955 | 955 | $ldap_users = $this->access->fetchListOfUsers($filter, 'dn', 1); |
956 | - if(count($ldap_users) < 1) { |
|
956 | + if (count($ldap_users) < 1) { |
|
957 | 957 | continue; |
958 | 958 | } |
959 | 959 | $groupUsers[] = $this->access->dn2username($ldap_users[0]); |
960 | 960 | } else { |
961 | 961 | //we need to apply the search filter now |
962 | - if(!$this->access->readAttribute($member, |
|
962 | + if (!$this->access->readAttribute($member, |
|
963 | 963 | $this->access->connection->ldapUserDisplayName, |
964 | 964 | $this->access->getFilterPartForUserSearch($search))) { |
965 | 965 | continue; |
966 | 966 | } |
967 | 967 | // dn2username will also check if the users belong to the allowed base |
968 | - if($ocname = $this->access->dn2username($member)) { |
|
968 | + if ($ocname = $this->access->dn2username($member)) { |
|
969 | 969 | $groupUsers[] = $ocname; |
970 | 970 | } |
971 | 971 | } |
@@ -988,7 +988,7 @@ discard block |
||
988 | 988 | * Returns a list with all groups (used by getGroups) |
989 | 989 | */ |
990 | 990 | protected function getGroupsChunk($search = '', $limit = -1, $offset = 0) { |
991 | - if(!$this->enabled) { |
|
991 | + if (!$this->enabled) { |
|
992 | 992 | return array(); |
993 | 993 | } |
994 | 994 | $cacheKey = 'getGroups-'.$search.'-'.$limit.'-'.$offset; |
@@ -996,13 +996,13 @@ discard block |
||
996 | 996 | //Check cache before driving unnecessary searches |
997 | 997 | \OCP\Util::writeLog('user_ldap', 'getGroups '.$cacheKey, ILogger::DEBUG); |
998 | 998 | $ldap_groups = $this->access->connection->getFromCache($cacheKey); |
999 | - if(!is_null($ldap_groups)) { |
|
999 | + if (!is_null($ldap_groups)) { |
|
1000 | 1000 | return $ldap_groups; |
1001 | 1001 | } |
1002 | 1002 | |
1003 | 1003 | // if we'd pass -1 to LDAP search, we'd end up in a Protocol |
1004 | 1004 | // error. With a limit of 0, we get 0 results. So we pass null. |
1005 | - if($limit <= 0) { |
|
1005 | + if ($limit <= 0) { |
|
1006 | 1006 | $limit = null; |
1007 | 1007 | } |
1008 | 1008 | $filter = $this->access->combineFilterWithAnd(array( |
@@ -1034,11 +1034,11 @@ discard block |
||
1034 | 1034 | * (active directory has a limit of 1000 by default) |
1035 | 1035 | */ |
1036 | 1036 | public function getGroups($search = '', $limit = -1, $offset = 0) { |
1037 | - if(!$this->enabled) { |
|
1037 | + if (!$this->enabled) { |
|
1038 | 1038 | return array(); |
1039 | 1039 | } |
1040 | 1040 | $search = $this->access->escapeFilterPart($search, true); |
1041 | - $pagingSize = (int)$this->access->connection->ldapPagingSize; |
|
1041 | + $pagingSize = (int) $this->access->connection->ldapPagingSize; |
|
1042 | 1042 | if ($pagingSize <= 0) { |
1043 | 1043 | return $this->getGroupsChunk($search, $limit, $offset); |
1044 | 1044 | } |
@@ -1081,20 +1081,20 @@ discard block |
||
1081 | 1081 | */ |
1082 | 1082 | public function groupExists($gid) { |
1083 | 1083 | $groupExists = $this->access->connection->getFromCache('groupExists'.$gid); |
1084 | - if(!is_null($groupExists)) { |
|
1085 | - return (bool)$groupExists; |
|
1084 | + if (!is_null($groupExists)) { |
|
1085 | + return (bool) $groupExists; |
|
1086 | 1086 | } |
1087 | 1087 | |
1088 | 1088 | //getting dn, if false the group does not exist. If dn, it may be mapped |
1089 | 1089 | //only, requires more checking. |
1090 | 1090 | $dn = $this->access->groupname2dn($gid); |
1091 | - if(!$dn) { |
|
1091 | + if (!$dn) { |
|
1092 | 1092 | $this->access->connection->writeToCache('groupExists'.$gid, false); |
1093 | 1093 | return false; |
1094 | 1094 | } |
1095 | 1095 | |
1096 | 1096 | //if group really still exists, we will be able to read its objectclass |
1097 | - if(!is_array($this->access->readAttribute($dn, ''))) { |
|
1097 | + if (!is_array($this->access->readAttribute($dn, ''))) { |
|
1098 | 1098 | $this->access->connection->writeToCache('groupExists'.$gid, false); |
1099 | 1099 | return false; |
1100 | 1100 | } |
@@ -1112,7 +1112,7 @@ discard block |
||
1112 | 1112 | * compared with GroupInterface::CREATE_GROUP etc. |
1113 | 1113 | */ |
1114 | 1114 | public function implementsActions($actions) { |
1115 | - return (bool)((GroupInterface::COUNT_USERS | |
|
1115 | + return (bool) ((GroupInterface::COUNT_USERS | |
|
1116 | 1116 | $this->groupPluginManager->getImplementedActions()) & $actions); |
1117 | 1117 | } |
1118 | 1118 | |
@@ -1135,7 +1135,7 @@ discard block |
||
1135 | 1135 | if ($dn = $this->groupPluginManager->createGroup($gid)) { |
1136 | 1136 | //updates group mapping |
1137 | 1137 | $uuid = $this->access->getUUID($dn, false); |
1138 | - if(is_string($uuid)) { |
|
1138 | + if (is_string($uuid)) { |
|
1139 | 1139 | $this->access->mapAndAnnounceIfApplicable( |
1140 | 1140 | $this->access->getGroupMapper(), |
1141 | 1141 | $dn, |
@@ -1143,7 +1143,7 @@ discard block |
||
1143 | 1143 | $uuid, |
1144 | 1144 | false |
1145 | 1145 | ); |
1146 | - $this->access->connection->writeToCache("groupExists" . $gid, true); |
|
1146 | + $this->access->connection->writeToCache("groupExists".$gid, true); |
|
1147 | 1147 | } |
1148 | 1148 | } |
1149 | 1149 | return $dn != null; |
@@ -1238,7 +1238,7 @@ discard block |
||
1238 | 1238 | return $this->groupPluginManager->getDisplayName($gid); |
1239 | 1239 | } |
1240 | 1240 | |
1241 | - $cacheKey = 'group_getDisplayName' . $gid; |
|
1241 | + $cacheKey = 'group_getDisplayName'.$gid; |
|
1242 | 1242 | if (!is_null($displayName = $this->access->connection->getFromCache($cacheKey))) { |
1243 | 1243 | return $displayName; |
1244 | 1244 | } |