@@ -45,516 +45,516 @@ |
||
45 | 45 | use OCP\Util; |
46 | 46 | |
47 | 47 | class User_LDAP extends BackendUtility implements \OCP\IUserBackend, \OCP\UserInterface, IUserLDAP { |
48 | - /** @var \OCP\IConfig */ |
|
49 | - protected $ocConfig; |
|
50 | - |
|
51 | - /** @var INotificationManager */ |
|
52 | - protected $notificationManager; |
|
53 | - |
|
54 | - /** @var string */ |
|
55 | - protected $currentUserInDeletionProcess; |
|
56 | - |
|
57 | - /** |
|
58 | - * @param Access $access |
|
59 | - * @param \OCP\IConfig $ocConfig |
|
60 | - * @param \OCP\Notification\IManager $notificationManager |
|
61 | - */ |
|
62 | - public function __construct(Access $access, IConfig $ocConfig, INotificationManager $notificationManager) { |
|
63 | - parent::__construct($access); |
|
64 | - $this->ocConfig = $ocConfig; |
|
65 | - $this->notificationManager = $notificationManager; |
|
66 | - $this->registerHooks(); |
|
67 | - } |
|
68 | - |
|
69 | - protected function registerHooks() { |
|
70 | - Util::connectHook('OC_User','pre_deleteUser', $this, 'preDeleteUser'); |
|
71 | - Util::connectHook('OC_User','post_deleteUser', $this, 'postDeleteUser'); |
|
72 | - } |
|
73 | - |
|
74 | - public function preDeleteUser(\OC\User\User $user) { |
|
75 | - $this->currentUserInDeletionProcess = $user->getUID(); |
|
76 | - } |
|
77 | - |
|
78 | - public function postDeleteUser() { |
|
79 | - $this->currentUserInDeletionProcess = null; |
|
80 | - } |
|
81 | - |
|
82 | - /** |
|
83 | - * checks whether the user is allowed to change his avatar in Nextcloud |
|
84 | - * @param string $uid the Nextcloud user name |
|
85 | - * @return boolean either the user can or cannot |
|
86 | - */ |
|
87 | - public function canChangeAvatar($uid) { |
|
88 | - $user = $this->access->userManager->get($uid); |
|
89 | - if(!$user instanceof User) { |
|
90 | - return false; |
|
91 | - } |
|
92 | - if($user->getAvatarImage() === false) { |
|
93 | - return true; |
|
94 | - } |
|
95 | - |
|
96 | - return false; |
|
97 | - } |
|
98 | - |
|
99 | - /** |
|
100 | - * returns the username for the given login name, if available |
|
101 | - * |
|
102 | - * @param string $loginName |
|
103 | - * @return string|false |
|
104 | - */ |
|
105 | - public function loginName2UserName($loginName) { |
|
106 | - $cacheKey = 'loginName2UserName-'.$loginName; |
|
107 | - $username = $this->access->connection->getFromCache($cacheKey); |
|
108 | - if(!is_null($username)) { |
|
109 | - return $username; |
|
110 | - } |
|
111 | - |
|
112 | - try { |
|
113 | - $ldapRecord = $this->getLDAPUserByLoginName($loginName); |
|
114 | - $user = $this->access->userManager->get($ldapRecord['dn'][0]); |
|
115 | - if($user instanceof OfflineUser) { |
|
116 | - // this path is not really possible, however get() is documented |
|
117 | - // to return User or OfflineUser so we are very defensive here. |
|
118 | - $this->access->connection->writeToCache($cacheKey, false); |
|
119 | - return false; |
|
120 | - } |
|
121 | - $username = $user->getUsername(); |
|
122 | - $this->access->connection->writeToCache($cacheKey, $username); |
|
123 | - return $username; |
|
124 | - } catch (NotOnLDAP $e) { |
|
125 | - $this->access->connection->writeToCache($cacheKey, false); |
|
126 | - return false; |
|
127 | - } |
|
128 | - } |
|
48 | + /** @var \OCP\IConfig */ |
|
49 | + protected $ocConfig; |
|
50 | + |
|
51 | + /** @var INotificationManager */ |
|
52 | + protected $notificationManager; |
|
53 | + |
|
54 | + /** @var string */ |
|
55 | + protected $currentUserInDeletionProcess; |
|
56 | + |
|
57 | + /** |
|
58 | + * @param Access $access |
|
59 | + * @param \OCP\IConfig $ocConfig |
|
60 | + * @param \OCP\Notification\IManager $notificationManager |
|
61 | + */ |
|
62 | + public function __construct(Access $access, IConfig $ocConfig, INotificationManager $notificationManager) { |
|
63 | + parent::__construct($access); |
|
64 | + $this->ocConfig = $ocConfig; |
|
65 | + $this->notificationManager = $notificationManager; |
|
66 | + $this->registerHooks(); |
|
67 | + } |
|
68 | + |
|
69 | + protected function registerHooks() { |
|
70 | + Util::connectHook('OC_User','pre_deleteUser', $this, 'preDeleteUser'); |
|
71 | + Util::connectHook('OC_User','post_deleteUser', $this, 'postDeleteUser'); |
|
72 | + } |
|
73 | + |
|
74 | + public function preDeleteUser(\OC\User\User $user) { |
|
75 | + $this->currentUserInDeletionProcess = $user->getUID(); |
|
76 | + } |
|
77 | + |
|
78 | + public function postDeleteUser() { |
|
79 | + $this->currentUserInDeletionProcess = null; |
|
80 | + } |
|
81 | + |
|
82 | + /** |
|
83 | + * checks whether the user is allowed to change his avatar in Nextcloud |
|
84 | + * @param string $uid the Nextcloud user name |
|
85 | + * @return boolean either the user can or cannot |
|
86 | + */ |
|
87 | + public function canChangeAvatar($uid) { |
|
88 | + $user = $this->access->userManager->get($uid); |
|
89 | + if(!$user instanceof User) { |
|
90 | + return false; |
|
91 | + } |
|
92 | + if($user->getAvatarImage() === false) { |
|
93 | + return true; |
|
94 | + } |
|
95 | + |
|
96 | + return false; |
|
97 | + } |
|
98 | + |
|
99 | + /** |
|
100 | + * returns the username for the given login name, if available |
|
101 | + * |
|
102 | + * @param string $loginName |
|
103 | + * @return string|false |
|
104 | + */ |
|
105 | + public function loginName2UserName($loginName) { |
|
106 | + $cacheKey = 'loginName2UserName-'.$loginName; |
|
107 | + $username = $this->access->connection->getFromCache($cacheKey); |
|
108 | + if(!is_null($username)) { |
|
109 | + return $username; |
|
110 | + } |
|
111 | + |
|
112 | + try { |
|
113 | + $ldapRecord = $this->getLDAPUserByLoginName($loginName); |
|
114 | + $user = $this->access->userManager->get($ldapRecord['dn'][0]); |
|
115 | + if($user instanceof OfflineUser) { |
|
116 | + // this path is not really possible, however get() is documented |
|
117 | + // to return User or OfflineUser so we are very defensive here. |
|
118 | + $this->access->connection->writeToCache($cacheKey, false); |
|
119 | + return false; |
|
120 | + } |
|
121 | + $username = $user->getUsername(); |
|
122 | + $this->access->connection->writeToCache($cacheKey, $username); |
|
123 | + return $username; |
|
124 | + } catch (NotOnLDAP $e) { |
|
125 | + $this->access->connection->writeToCache($cacheKey, false); |
|
126 | + return false; |
|
127 | + } |
|
128 | + } |
|
129 | 129 | |
130 | - /** |
|
131 | - * returns the username for the given LDAP DN, if available |
|
132 | - * |
|
133 | - * @param string $dn |
|
134 | - * @return string|false with the username |
|
135 | - */ |
|
136 | - public function dn2UserName($dn) { |
|
137 | - return $this->access->dn2username($dn); |
|
138 | - } |
|
139 | - |
|
140 | - /** |
|
141 | - * returns an LDAP record based on a given login name |
|
142 | - * |
|
143 | - * @param string $loginName |
|
144 | - * @return array |
|
145 | - * @throws NotOnLDAP |
|
146 | - */ |
|
147 | - public function getLDAPUserByLoginName($loginName) { |
|
148 | - //find out dn of the user name |
|
149 | - $attrs = $this->access->userManager->getAttributes(); |
|
150 | - $users = $this->access->fetchUsersByLoginName($loginName, $attrs); |
|
151 | - if(count($users) < 1) { |
|
152 | - throw new NotOnLDAP('No user available for the given login name on ' . |
|
153 | - $this->access->connection->ldapHost . ':' . $this->access->connection->ldapPort); |
|
154 | - } |
|
155 | - return $users[0]; |
|
156 | - } |
|
157 | - |
|
158 | - /** |
|
159 | - * Check if the password is correct without logging in the user |
|
160 | - * |
|
161 | - * @param string $uid The username |
|
162 | - * @param string $password The password |
|
163 | - * @return false|string |
|
164 | - */ |
|
165 | - public function checkPassword($uid, $password) { |
|
166 | - try { |
|
167 | - $ldapRecord = $this->getLDAPUserByLoginName($uid); |
|
168 | - } catch(NotOnLDAP $e) { |
|
169 | - if($this->ocConfig->getSystemValue('loglevel', Util::WARN) === Util::DEBUG) { |
|
170 | - \OC::$server->getLogger()->logException($e, ['app' => 'user_ldap']); |
|
171 | - } |
|
172 | - return false; |
|
173 | - } |
|
174 | - $dn = $ldapRecord['dn'][0]; |
|
175 | - $user = $this->access->userManager->get($dn); |
|
176 | - |
|
177 | - if(!$user instanceof User) { |
|
178 | - Util::writeLog('user_ldap', |
|
179 | - 'LDAP Login: Could not get user object for DN ' . $dn . |
|
180 | - '. Maybe the LDAP entry has no set display name attribute?', |
|
181 | - Util::WARN); |
|
182 | - return false; |
|
183 | - } |
|
184 | - if($user->getUsername() !== false) { |
|
185 | - //are the credentials OK? |
|
186 | - if(!$this->access->areCredentialsValid($dn, $password)) { |
|
187 | - return false; |
|
188 | - } |
|
189 | - |
|
190 | - $this->access->cacheUserExists($user->getUsername()); |
|
191 | - $user->processAttributes($ldapRecord); |
|
192 | - $user->markLogin(); |
|
193 | - |
|
194 | - return $user->getUsername(); |
|
195 | - } |
|
196 | - |
|
197 | - return false; |
|
198 | - } |
|
199 | - |
|
200 | - /** |
|
201 | - * Set password |
|
202 | - * @param string $uid The username |
|
203 | - * @param string $password The new password |
|
204 | - * @return bool |
|
205 | - */ |
|
206 | - public function setPassword($uid, $password) { |
|
207 | - $user = $this->access->userManager->get($uid); |
|
208 | - |
|
209 | - if(!$user instanceof User) { |
|
210 | - throw new \Exception('LDAP setPassword: Could not get user object for uid ' . $uid . |
|
211 | - '. Maybe the LDAP entry has no set display name attribute?'); |
|
212 | - } |
|
213 | - if($user->getUsername() !== false && $this->access->setPassword($user->getDN(), $password)) { |
|
214 | - $ldapDefaultPPolicyDN = $this->access->connection->ldapDefaultPPolicyDN; |
|
215 | - $turnOnPasswordChange = $this->access->connection->turnOnPasswordChange; |
|
216 | - if (!empty($ldapDefaultPPolicyDN) && (intval($turnOnPasswordChange) === 1)) { |
|
217 | - //remove last password expiry warning if any |
|
218 | - $notification = $this->notificationManager->createNotification(); |
|
219 | - $notification->setApp('user_ldap') |
|
220 | - ->setUser($uid) |
|
221 | - ->setObject('pwd_exp_warn', $uid) |
|
222 | - ; |
|
223 | - $this->notificationManager->markProcessed($notification); |
|
224 | - } |
|
225 | - return true; |
|
226 | - } |
|
227 | - |
|
228 | - return false; |
|
229 | - } |
|
230 | - |
|
231 | - /** |
|
232 | - * Get a list of all users |
|
233 | - * |
|
234 | - * @param string $search |
|
235 | - * @param integer $limit |
|
236 | - * @param integer $offset |
|
237 | - * @return string[] an array of all uids |
|
238 | - */ |
|
239 | - public function getUsers($search = '', $limit = 10, $offset = 0) { |
|
240 | - $search = $this->access->escapeFilterPart($search, true); |
|
241 | - $cachekey = 'getUsers-'.$search.'-'.$limit.'-'.$offset; |
|
242 | - |
|
243 | - //check if users are cached, if so return |
|
244 | - $ldap_users = $this->access->connection->getFromCache($cachekey); |
|
245 | - if(!is_null($ldap_users)) { |
|
246 | - return $ldap_users; |
|
247 | - } |
|
248 | - |
|
249 | - // if we'd pass -1 to LDAP search, we'd end up in a Protocol |
|
250 | - // error. With a limit of 0, we get 0 results. So we pass null. |
|
251 | - if($limit <= 0) { |
|
252 | - $limit = null; |
|
253 | - } |
|
254 | - $filter = $this->access->combineFilterWithAnd(array( |
|
255 | - $this->access->connection->ldapUserFilter, |
|
256 | - $this->access->connection->ldapUserDisplayName . '=*', |
|
257 | - $this->access->getFilterPartForUserSearch($search) |
|
258 | - )); |
|
259 | - |
|
260 | - Util::writeLog('user_ldap', |
|
261 | - 'getUsers: Options: search '.$search.' limit '.$limit.' offset '.$offset.' Filter: '.$filter, |
|
262 | - Util::DEBUG); |
|
263 | - //do the search and translate results to owncloud names |
|
264 | - $ldap_users = $this->access->fetchListOfUsers( |
|
265 | - $filter, |
|
266 | - $this->access->userManager->getAttributes(true), |
|
267 | - $limit, $offset); |
|
268 | - $ldap_users = $this->access->nextcloudUserNames($ldap_users); |
|
269 | - Util::writeLog('user_ldap', 'getUsers: '.count($ldap_users). ' Users found', Util::DEBUG); |
|
270 | - |
|
271 | - $this->access->connection->writeToCache($cachekey, $ldap_users); |
|
272 | - return $ldap_users; |
|
273 | - } |
|
274 | - |
|
275 | - /** |
|
276 | - * checks whether a user is still available on LDAP |
|
277 | - * |
|
278 | - * @param string|\OCA\User_LDAP\User\User $user either the Nextcloud user |
|
279 | - * name or an instance of that user |
|
280 | - * @return bool |
|
281 | - * @throws \Exception |
|
282 | - * @throws \OC\ServerNotAvailableException |
|
283 | - */ |
|
284 | - public function userExistsOnLDAP($user) { |
|
285 | - if(is_string($user)) { |
|
286 | - $user = $this->access->userManager->get($user); |
|
287 | - } |
|
288 | - if(is_null($user)) { |
|
289 | - return false; |
|
290 | - } |
|
291 | - |
|
292 | - $dn = $user->getDN(); |
|
293 | - //check if user really still exists by reading its entry |
|
294 | - if(!is_array($this->access->readAttribute($dn, '', $this->access->connection->ldapUserFilter))) { |
|
295 | - $lcr = $this->access->connection->getConnectionResource(); |
|
296 | - if(is_null($lcr)) { |
|
297 | - throw new \Exception('No LDAP Connection to server ' . $this->access->connection->ldapHost); |
|
298 | - } |
|
299 | - |
|
300 | - try { |
|
301 | - $uuid = $this->access->getUserMapper()->getUUIDByDN($dn); |
|
302 | - if(!$uuid) { |
|
303 | - return false; |
|
304 | - } |
|
305 | - $newDn = $this->access->getUserDnByUuid($uuid); |
|
306 | - //check if renamed user is still valid by reapplying the ldap filter |
|
307 | - if(!is_array($this->access->readAttribute($newDn, '', $this->access->connection->ldapUserFilter))) { |
|
308 | - return false; |
|
309 | - } |
|
310 | - $this->access->getUserMapper()->setDNbyUUID($newDn, $uuid); |
|
311 | - return true; |
|
312 | - } catch (\Exception $e) { |
|
313 | - return false; |
|
314 | - } |
|
315 | - } |
|
316 | - |
|
317 | - if($user instanceof OfflineUser) { |
|
318 | - $user->unmark(); |
|
319 | - } |
|
320 | - |
|
321 | - return true; |
|
322 | - } |
|
323 | - |
|
324 | - /** |
|
325 | - * check if a user exists |
|
326 | - * @param string $uid the username |
|
327 | - * @return boolean |
|
328 | - * @throws \Exception when connection could not be established |
|
329 | - */ |
|
330 | - public function userExists($uid) { |
|
331 | - $userExists = $this->access->connection->getFromCache('userExists'.$uid); |
|
332 | - if(!is_null($userExists)) { |
|
333 | - return (bool)$userExists; |
|
334 | - } |
|
335 | - //getting dn, if false the user does not exist. If dn, he may be mapped only, requires more checking. |
|
336 | - $user = $this->access->userManager->get($uid); |
|
337 | - |
|
338 | - if(is_null($user)) { |
|
339 | - Util::writeLog('user_ldap', 'No DN found for '.$uid.' on '. |
|
340 | - $this->access->connection->ldapHost, Util::DEBUG); |
|
341 | - $this->access->connection->writeToCache('userExists'.$uid, false); |
|
342 | - return false; |
|
343 | - } else if($user instanceof OfflineUser) { |
|
344 | - //express check for users marked as deleted. Returning true is |
|
345 | - //necessary for cleanup |
|
346 | - return true; |
|
347 | - } |
|
348 | - |
|
349 | - $result = $this->userExistsOnLDAP($user); |
|
350 | - $this->access->connection->writeToCache('userExists'.$uid, $result); |
|
351 | - if($result === true) { |
|
352 | - $user->update(); |
|
353 | - } |
|
354 | - return $result; |
|
355 | - } |
|
356 | - |
|
357 | - /** |
|
358 | - * returns whether a user was deleted in LDAP |
|
359 | - * |
|
360 | - * @param string $uid The username of the user to delete |
|
361 | - * @return bool |
|
362 | - */ |
|
363 | - public function deleteUser($uid) { |
|
364 | - $marked = $this->ocConfig->getUserValue($uid, 'user_ldap', 'isDeleted', 0); |
|
365 | - if(intval($marked) === 0) { |
|
366 | - \OC::$server->getLogger()->notice( |
|
367 | - 'User '.$uid . ' is not marked as deleted, not cleaning up.', |
|
368 | - array('app' => 'user_ldap')); |
|
369 | - return false; |
|
370 | - } |
|
371 | - \OC::$server->getLogger()->info('Cleaning up after user ' . $uid, |
|
372 | - array('app' => 'user_ldap')); |
|
373 | - |
|
374 | - //Get Home Directory out of user preferences so we can return it later, |
|
375 | - //necessary for removing directories as done by OC_User. |
|
376 | - $this->access->getUserMapper()->unmap($uid); |
|
377 | - return true; |
|
378 | - } |
|
379 | - |
|
380 | - /** |
|
381 | - * get the user's home directory |
|
382 | - * |
|
383 | - * @param string $uid the username |
|
384 | - * @return bool|string |
|
385 | - * @throws NoUserException |
|
386 | - * @throws \Exception |
|
387 | - */ |
|
388 | - public function getHome($uid) { |
|
389 | - // user Exists check required as it is not done in user proxy! |
|
390 | - if(!$this->userExists($uid)) { |
|
391 | - return false; |
|
392 | - } |
|
393 | - |
|
394 | - $cacheKey = 'getHome'.$uid; |
|
395 | - $path = $this->access->connection->getFromCache($cacheKey); |
|
396 | - if(!is_null($path)) { |
|
397 | - return $path; |
|
398 | - } |
|
399 | - |
|
400 | - // early return path if it is a deleted user |
|
401 | - $user = $this->access->userManager->get($uid); |
|
402 | - if($user instanceof OfflineUser) { |
|
403 | - if($this->currentUserInDeletionProcess === $user->getUID()) { |
|
404 | - return $user->getHomePath(); |
|
405 | - } else { |
|
406 | - throw new NoUserException($uid . ' is not a valid user anymore'); |
|
407 | - } |
|
408 | - } else if ($user === null) { |
|
409 | - throw new NoUserException($uid . ' is not a valid user anymore'); |
|
410 | - } |
|
411 | - |
|
412 | - $path = $user->getHomePath(); |
|
413 | - $this->access->cacheUserHome($uid, $path); |
|
414 | - |
|
415 | - return $path; |
|
416 | - } |
|
417 | - |
|
418 | - /** |
|
419 | - * get display name of the user |
|
420 | - * @param string $uid user ID of the user |
|
421 | - * @return string|false display name |
|
422 | - */ |
|
423 | - public function getDisplayName($uid) { |
|
424 | - if(!$this->userExists($uid)) { |
|
425 | - return false; |
|
426 | - } |
|
427 | - |
|
428 | - $cacheKey = 'getDisplayName'.$uid; |
|
429 | - if(!is_null($displayName = $this->access->connection->getFromCache($cacheKey))) { |
|
430 | - return $displayName; |
|
431 | - } |
|
432 | - |
|
433 | - //Check whether the display name is configured to have a 2nd feature |
|
434 | - $additionalAttribute = $this->access->connection->ldapUserDisplayName2; |
|
435 | - $displayName2 = ''; |
|
436 | - if ($additionalAttribute !== '') { |
|
437 | - $displayName2 = $this->access->readAttribute( |
|
438 | - $this->access->username2dn($uid), |
|
439 | - $additionalAttribute); |
|
440 | - } |
|
441 | - |
|
442 | - $displayName = $this->access->readAttribute( |
|
443 | - $this->access->username2dn($uid), |
|
444 | - $this->access->connection->ldapUserDisplayName); |
|
445 | - |
|
446 | - if($displayName && (count($displayName) > 0)) { |
|
447 | - $displayName = $displayName[0]; |
|
448 | - |
|
449 | - if (is_array($displayName2)){ |
|
450 | - $displayName2 = count($displayName2) > 0 ? $displayName2[0] : ''; |
|
451 | - } |
|
452 | - |
|
453 | - $user = $this->access->userManager->get($uid); |
|
454 | - if ($user instanceof User) { |
|
455 | - $displayName = $user->composeAndStoreDisplayName($displayName, $displayName2); |
|
456 | - $this->access->connection->writeToCache($cacheKey, $displayName); |
|
457 | - } |
|
458 | - if ($user instanceof OfflineUser) { |
|
459 | - /** @var OfflineUser $user*/ |
|
460 | - $displayName = $user->getDisplayName(); |
|
461 | - } |
|
462 | - return $displayName; |
|
463 | - } |
|
464 | - |
|
465 | - return null; |
|
466 | - } |
|
467 | - |
|
468 | - /** |
|
469 | - * Get a list of all display names |
|
470 | - * |
|
471 | - * @param string $search |
|
472 | - * @param string|null $limit |
|
473 | - * @param string|null $offset |
|
474 | - * @return array an array of all displayNames (value) and the corresponding uids (key) |
|
475 | - */ |
|
476 | - public function getDisplayNames($search = '', $limit = null, $offset = null) { |
|
477 | - $cacheKey = 'getDisplayNames-'.$search.'-'.$limit.'-'.$offset; |
|
478 | - if(!is_null($displayNames = $this->access->connection->getFromCache($cacheKey))) { |
|
479 | - return $displayNames; |
|
480 | - } |
|
481 | - |
|
482 | - $displayNames = array(); |
|
483 | - $users = $this->getUsers($search, $limit, $offset); |
|
484 | - foreach ($users as $user) { |
|
485 | - $displayNames[$user] = $this->getDisplayName($user); |
|
486 | - } |
|
487 | - $this->access->connection->writeToCache($cacheKey, $displayNames); |
|
488 | - return $displayNames; |
|
489 | - } |
|
490 | - |
|
491 | - /** |
|
492 | - * Check if backend implements actions |
|
493 | - * @param int $actions bitwise-or'ed actions |
|
494 | - * @return boolean |
|
495 | - * |
|
496 | - * Returns the supported actions as int to be |
|
497 | - * compared with OC_USER_BACKEND_CREATE_USER etc. |
|
498 | - */ |
|
499 | - public function implementsActions($actions) { |
|
500 | - return (bool)((Backend::CHECK_PASSWORD |
|
501 | - | Backend::GET_HOME |
|
502 | - | Backend::GET_DISPLAYNAME |
|
503 | - | Backend::PROVIDE_AVATAR |
|
504 | - | Backend::COUNT_USERS |
|
505 | - | ((intval($this->access->connection->turnOnPasswordChange) === 1)?(Backend::SET_PASSWORD):0)) |
|
506 | - & $actions); |
|
507 | - } |
|
508 | - |
|
509 | - /** |
|
510 | - * @return bool |
|
511 | - */ |
|
512 | - public function hasUserListings() { |
|
513 | - return true; |
|
514 | - } |
|
515 | - |
|
516 | - /** |
|
517 | - * counts the users in LDAP |
|
518 | - * |
|
519 | - * @return int|bool |
|
520 | - */ |
|
521 | - public function countUsers() { |
|
522 | - $filter = $this->access->getFilterForUserCount(); |
|
523 | - $cacheKey = 'countUsers-'.$filter; |
|
524 | - if(!is_null($entries = $this->access->connection->getFromCache($cacheKey))) { |
|
525 | - return $entries; |
|
526 | - } |
|
527 | - $entries = $this->access->countUsers($filter); |
|
528 | - $this->access->connection->writeToCache($cacheKey, $entries); |
|
529 | - return $entries; |
|
530 | - } |
|
531 | - |
|
532 | - /** |
|
533 | - * Backend name to be shown in user management |
|
534 | - * @return string the name of the backend to be shown |
|
535 | - */ |
|
536 | - public function getBackendName(){ |
|
537 | - return 'LDAP'; |
|
538 | - } |
|
130 | + /** |
|
131 | + * returns the username for the given LDAP DN, if available |
|
132 | + * |
|
133 | + * @param string $dn |
|
134 | + * @return string|false with the username |
|
135 | + */ |
|
136 | + public function dn2UserName($dn) { |
|
137 | + return $this->access->dn2username($dn); |
|
138 | + } |
|
139 | + |
|
140 | + /** |
|
141 | + * returns an LDAP record based on a given login name |
|
142 | + * |
|
143 | + * @param string $loginName |
|
144 | + * @return array |
|
145 | + * @throws NotOnLDAP |
|
146 | + */ |
|
147 | + public function getLDAPUserByLoginName($loginName) { |
|
148 | + //find out dn of the user name |
|
149 | + $attrs = $this->access->userManager->getAttributes(); |
|
150 | + $users = $this->access->fetchUsersByLoginName($loginName, $attrs); |
|
151 | + if(count($users) < 1) { |
|
152 | + throw new NotOnLDAP('No user available for the given login name on ' . |
|
153 | + $this->access->connection->ldapHost . ':' . $this->access->connection->ldapPort); |
|
154 | + } |
|
155 | + return $users[0]; |
|
156 | + } |
|
157 | + |
|
158 | + /** |
|
159 | + * Check if the password is correct without logging in the user |
|
160 | + * |
|
161 | + * @param string $uid The username |
|
162 | + * @param string $password The password |
|
163 | + * @return false|string |
|
164 | + */ |
|
165 | + public function checkPassword($uid, $password) { |
|
166 | + try { |
|
167 | + $ldapRecord = $this->getLDAPUserByLoginName($uid); |
|
168 | + } catch(NotOnLDAP $e) { |
|
169 | + if($this->ocConfig->getSystemValue('loglevel', Util::WARN) === Util::DEBUG) { |
|
170 | + \OC::$server->getLogger()->logException($e, ['app' => 'user_ldap']); |
|
171 | + } |
|
172 | + return false; |
|
173 | + } |
|
174 | + $dn = $ldapRecord['dn'][0]; |
|
175 | + $user = $this->access->userManager->get($dn); |
|
176 | + |
|
177 | + if(!$user instanceof User) { |
|
178 | + Util::writeLog('user_ldap', |
|
179 | + 'LDAP Login: Could not get user object for DN ' . $dn . |
|
180 | + '. Maybe the LDAP entry has no set display name attribute?', |
|
181 | + Util::WARN); |
|
182 | + return false; |
|
183 | + } |
|
184 | + if($user->getUsername() !== false) { |
|
185 | + //are the credentials OK? |
|
186 | + if(!$this->access->areCredentialsValid($dn, $password)) { |
|
187 | + return false; |
|
188 | + } |
|
189 | + |
|
190 | + $this->access->cacheUserExists($user->getUsername()); |
|
191 | + $user->processAttributes($ldapRecord); |
|
192 | + $user->markLogin(); |
|
193 | + |
|
194 | + return $user->getUsername(); |
|
195 | + } |
|
196 | + |
|
197 | + return false; |
|
198 | + } |
|
199 | + |
|
200 | + /** |
|
201 | + * Set password |
|
202 | + * @param string $uid The username |
|
203 | + * @param string $password The new password |
|
204 | + * @return bool |
|
205 | + */ |
|
206 | + public function setPassword($uid, $password) { |
|
207 | + $user = $this->access->userManager->get($uid); |
|
208 | + |
|
209 | + if(!$user instanceof User) { |
|
210 | + throw new \Exception('LDAP setPassword: Could not get user object for uid ' . $uid . |
|
211 | + '. Maybe the LDAP entry has no set display name attribute?'); |
|
212 | + } |
|
213 | + if($user->getUsername() !== false && $this->access->setPassword($user->getDN(), $password)) { |
|
214 | + $ldapDefaultPPolicyDN = $this->access->connection->ldapDefaultPPolicyDN; |
|
215 | + $turnOnPasswordChange = $this->access->connection->turnOnPasswordChange; |
|
216 | + if (!empty($ldapDefaultPPolicyDN) && (intval($turnOnPasswordChange) === 1)) { |
|
217 | + //remove last password expiry warning if any |
|
218 | + $notification = $this->notificationManager->createNotification(); |
|
219 | + $notification->setApp('user_ldap') |
|
220 | + ->setUser($uid) |
|
221 | + ->setObject('pwd_exp_warn', $uid) |
|
222 | + ; |
|
223 | + $this->notificationManager->markProcessed($notification); |
|
224 | + } |
|
225 | + return true; |
|
226 | + } |
|
227 | + |
|
228 | + return false; |
|
229 | + } |
|
230 | + |
|
231 | + /** |
|
232 | + * Get a list of all users |
|
233 | + * |
|
234 | + * @param string $search |
|
235 | + * @param integer $limit |
|
236 | + * @param integer $offset |
|
237 | + * @return string[] an array of all uids |
|
238 | + */ |
|
239 | + public function getUsers($search = '', $limit = 10, $offset = 0) { |
|
240 | + $search = $this->access->escapeFilterPart($search, true); |
|
241 | + $cachekey = 'getUsers-'.$search.'-'.$limit.'-'.$offset; |
|
242 | + |
|
243 | + //check if users are cached, if so return |
|
244 | + $ldap_users = $this->access->connection->getFromCache($cachekey); |
|
245 | + if(!is_null($ldap_users)) { |
|
246 | + return $ldap_users; |
|
247 | + } |
|
248 | + |
|
249 | + // if we'd pass -1 to LDAP search, we'd end up in a Protocol |
|
250 | + // error. With a limit of 0, we get 0 results. So we pass null. |
|
251 | + if($limit <= 0) { |
|
252 | + $limit = null; |
|
253 | + } |
|
254 | + $filter = $this->access->combineFilterWithAnd(array( |
|
255 | + $this->access->connection->ldapUserFilter, |
|
256 | + $this->access->connection->ldapUserDisplayName . '=*', |
|
257 | + $this->access->getFilterPartForUserSearch($search) |
|
258 | + )); |
|
259 | + |
|
260 | + Util::writeLog('user_ldap', |
|
261 | + 'getUsers: Options: search '.$search.' limit '.$limit.' offset '.$offset.' Filter: '.$filter, |
|
262 | + Util::DEBUG); |
|
263 | + //do the search and translate results to owncloud names |
|
264 | + $ldap_users = $this->access->fetchListOfUsers( |
|
265 | + $filter, |
|
266 | + $this->access->userManager->getAttributes(true), |
|
267 | + $limit, $offset); |
|
268 | + $ldap_users = $this->access->nextcloudUserNames($ldap_users); |
|
269 | + Util::writeLog('user_ldap', 'getUsers: '.count($ldap_users). ' Users found', Util::DEBUG); |
|
270 | + |
|
271 | + $this->access->connection->writeToCache($cachekey, $ldap_users); |
|
272 | + return $ldap_users; |
|
273 | + } |
|
274 | + |
|
275 | + /** |
|
276 | + * checks whether a user is still available on LDAP |
|
277 | + * |
|
278 | + * @param string|\OCA\User_LDAP\User\User $user either the Nextcloud user |
|
279 | + * name or an instance of that user |
|
280 | + * @return bool |
|
281 | + * @throws \Exception |
|
282 | + * @throws \OC\ServerNotAvailableException |
|
283 | + */ |
|
284 | + public function userExistsOnLDAP($user) { |
|
285 | + if(is_string($user)) { |
|
286 | + $user = $this->access->userManager->get($user); |
|
287 | + } |
|
288 | + if(is_null($user)) { |
|
289 | + return false; |
|
290 | + } |
|
291 | + |
|
292 | + $dn = $user->getDN(); |
|
293 | + //check if user really still exists by reading its entry |
|
294 | + if(!is_array($this->access->readAttribute($dn, '', $this->access->connection->ldapUserFilter))) { |
|
295 | + $lcr = $this->access->connection->getConnectionResource(); |
|
296 | + if(is_null($lcr)) { |
|
297 | + throw new \Exception('No LDAP Connection to server ' . $this->access->connection->ldapHost); |
|
298 | + } |
|
299 | + |
|
300 | + try { |
|
301 | + $uuid = $this->access->getUserMapper()->getUUIDByDN($dn); |
|
302 | + if(!$uuid) { |
|
303 | + return false; |
|
304 | + } |
|
305 | + $newDn = $this->access->getUserDnByUuid($uuid); |
|
306 | + //check if renamed user is still valid by reapplying the ldap filter |
|
307 | + if(!is_array($this->access->readAttribute($newDn, '', $this->access->connection->ldapUserFilter))) { |
|
308 | + return false; |
|
309 | + } |
|
310 | + $this->access->getUserMapper()->setDNbyUUID($newDn, $uuid); |
|
311 | + return true; |
|
312 | + } catch (\Exception $e) { |
|
313 | + return false; |
|
314 | + } |
|
315 | + } |
|
316 | + |
|
317 | + if($user instanceof OfflineUser) { |
|
318 | + $user->unmark(); |
|
319 | + } |
|
320 | + |
|
321 | + return true; |
|
322 | + } |
|
323 | + |
|
324 | + /** |
|
325 | + * check if a user exists |
|
326 | + * @param string $uid the username |
|
327 | + * @return boolean |
|
328 | + * @throws \Exception when connection could not be established |
|
329 | + */ |
|
330 | + public function userExists($uid) { |
|
331 | + $userExists = $this->access->connection->getFromCache('userExists'.$uid); |
|
332 | + if(!is_null($userExists)) { |
|
333 | + return (bool)$userExists; |
|
334 | + } |
|
335 | + //getting dn, if false the user does not exist. If dn, he may be mapped only, requires more checking. |
|
336 | + $user = $this->access->userManager->get($uid); |
|
337 | + |
|
338 | + if(is_null($user)) { |
|
339 | + Util::writeLog('user_ldap', 'No DN found for '.$uid.' on '. |
|
340 | + $this->access->connection->ldapHost, Util::DEBUG); |
|
341 | + $this->access->connection->writeToCache('userExists'.$uid, false); |
|
342 | + return false; |
|
343 | + } else if($user instanceof OfflineUser) { |
|
344 | + //express check for users marked as deleted. Returning true is |
|
345 | + //necessary for cleanup |
|
346 | + return true; |
|
347 | + } |
|
348 | + |
|
349 | + $result = $this->userExistsOnLDAP($user); |
|
350 | + $this->access->connection->writeToCache('userExists'.$uid, $result); |
|
351 | + if($result === true) { |
|
352 | + $user->update(); |
|
353 | + } |
|
354 | + return $result; |
|
355 | + } |
|
356 | + |
|
357 | + /** |
|
358 | + * returns whether a user was deleted in LDAP |
|
359 | + * |
|
360 | + * @param string $uid The username of the user to delete |
|
361 | + * @return bool |
|
362 | + */ |
|
363 | + public function deleteUser($uid) { |
|
364 | + $marked = $this->ocConfig->getUserValue($uid, 'user_ldap', 'isDeleted', 0); |
|
365 | + if(intval($marked) === 0) { |
|
366 | + \OC::$server->getLogger()->notice( |
|
367 | + 'User '.$uid . ' is not marked as deleted, not cleaning up.', |
|
368 | + array('app' => 'user_ldap')); |
|
369 | + return false; |
|
370 | + } |
|
371 | + \OC::$server->getLogger()->info('Cleaning up after user ' . $uid, |
|
372 | + array('app' => 'user_ldap')); |
|
373 | + |
|
374 | + //Get Home Directory out of user preferences so we can return it later, |
|
375 | + //necessary for removing directories as done by OC_User. |
|
376 | + $this->access->getUserMapper()->unmap($uid); |
|
377 | + return true; |
|
378 | + } |
|
379 | + |
|
380 | + /** |
|
381 | + * get the user's home directory |
|
382 | + * |
|
383 | + * @param string $uid the username |
|
384 | + * @return bool|string |
|
385 | + * @throws NoUserException |
|
386 | + * @throws \Exception |
|
387 | + */ |
|
388 | + public function getHome($uid) { |
|
389 | + // user Exists check required as it is not done in user proxy! |
|
390 | + if(!$this->userExists($uid)) { |
|
391 | + return false; |
|
392 | + } |
|
393 | + |
|
394 | + $cacheKey = 'getHome'.$uid; |
|
395 | + $path = $this->access->connection->getFromCache($cacheKey); |
|
396 | + if(!is_null($path)) { |
|
397 | + return $path; |
|
398 | + } |
|
399 | + |
|
400 | + // early return path if it is a deleted user |
|
401 | + $user = $this->access->userManager->get($uid); |
|
402 | + if($user instanceof OfflineUser) { |
|
403 | + if($this->currentUserInDeletionProcess === $user->getUID()) { |
|
404 | + return $user->getHomePath(); |
|
405 | + } else { |
|
406 | + throw new NoUserException($uid . ' is not a valid user anymore'); |
|
407 | + } |
|
408 | + } else if ($user === null) { |
|
409 | + throw new NoUserException($uid . ' is not a valid user anymore'); |
|
410 | + } |
|
411 | + |
|
412 | + $path = $user->getHomePath(); |
|
413 | + $this->access->cacheUserHome($uid, $path); |
|
414 | + |
|
415 | + return $path; |
|
416 | + } |
|
417 | + |
|
418 | + /** |
|
419 | + * get display name of the user |
|
420 | + * @param string $uid user ID of the user |
|
421 | + * @return string|false display name |
|
422 | + */ |
|
423 | + public function getDisplayName($uid) { |
|
424 | + if(!$this->userExists($uid)) { |
|
425 | + return false; |
|
426 | + } |
|
427 | + |
|
428 | + $cacheKey = 'getDisplayName'.$uid; |
|
429 | + if(!is_null($displayName = $this->access->connection->getFromCache($cacheKey))) { |
|
430 | + return $displayName; |
|
431 | + } |
|
432 | + |
|
433 | + //Check whether the display name is configured to have a 2nd feature |
|
434 | + $additionalAttribute = $this->access->connection->ldapUserDisplayName2; |
|
435 | + $displayName2 = ''; |
|
436 | + if ($additionalAttribute !== '') { |
|
437 | + $displayName2 = $this->access->readAttribute( |
|
438 | + $this->access->username2dn($uid), |
|
439 | + $additionalAttribute); |
|
440 | + } |
|
441 | + |
|
442 | + $displayName = $this->access->readAttribute( |
|
443 | + $this->access->username2dn($uid), |
|
444 | + $this->access->connection->ldapUserDisplayName); |
|
445 | + |
|
446 | + if($displayName && (count($displayName) > 0)) { |
|
447 | + $displayName = $displayName[0]; |
|
448 | + |
|
449 | + if (is_array($displayName2)){ |
|
450 | + $displayName2 = count($displayName2) > 0 ? $displayName2[0] : ''; |
|
451 | + } |
|
452 | + |
|
453 | + $user = $this->access->userManager->get($uid); |
|
454 | + if ($user instanceof User) { |
|
455 | + $displayName = $user->composeAndStoreDisplayName($displayName, $displayName2); |
|
456 | + $this->access->connection->writeToCache($cacheKey, $displayName); |
|
457 | + } |
|
458 | + if ($user instanceof OfflineUser) { |
|
459 | + /** @var OfflineUser $user*/ |
|
460 | + $displayName = $user->getDisplayName(); |
|
461 | + } |
|
462 | + return $displayName; |
|
463 | + } |
|
464 | + |
|
465 | + return null; |
|
466 | + } |
|
467 | + |
|
468 | + /** |
|
469 | + * Get a list of all display names |
|
470 | + * |
|
471 | + * @param string $search |
|
472 | + * @param string|null $limit |
|
473 | + * @param string|null $offset |
|
474 | + * @return array an array of all displayNames (value) and the corresponding uids (key) |
|
475 | + */ |
|
476 | + public function getDisplayNames($search = '', $limit = null, $offset = null) { |
|
477 | + $cacheKey = 'getDisplayNames-'.$search.'-'.$limit.'-'.$offset; |
|
478 | + if(!is_null($displayNames = $this->access->connection->getFromCache($cacheKey))) { |
|
479 | + return $displayNames; |
|
480 | + } |
|
481 | + |
|
482 | + $displayNames = array(); |
|
483 | + $users = $this->getUsers($search, $limit, $offset); |
|
484 | + foreach ($users as $user) { |
|
485 | + $displayNames[$user] = $this->getDisplayName($user); |
|
486 | + } |
|
487 | + $this->access->connection->writeToCache($cacheKey, $displayNames); |
|
488 | + return $displayNames; |
|
489 | + } |
|
490 | + |
|
491 | + /** |
|
492 | + * Check if backend implements actions |
|
493 | + * @param int $actions bitwise-or'ed actions |
|
494 | + * @return boolean |
|
495 | + * |
|
496 | + * Returns the supported actions as int to be |
|
497 | + * compared with OC_USER_BACKEND_CREATE_USER etc. |
|
498 | + */ |
|
499 | + public function implementsActions($actions) { |
|
500 | + return (bool)((Backend::CHECK_PASSWORD |
|
501 | + | Backend::GET_HOME |
|
502 | + | Backend::GET_DISPLAYNAME |
|
503 | + | Backend::PROVIDE_AVATAR |
|
504 | + | Backend::COUNT_USERS |
|
505 | + | ((intval($this->access->connection->turnOnPasswordChange) === 1)?(Backend::SET_PASSWORD):0)) |
|
506 | + & $actions); |
|
507 | + } |
|
508 | + |
|
509 | + /** |
|
510 | + * @return bool |
|
511 | + */ |
|
512 | + public function hasUserListings() { |
|
513 | + return true; |
|
514 | + } |
|
515 | + |
|
516 | + /** |
|
517 | + * counts the users in LDAP |
|
518 | + * |
|
519 | + * @return int|bool |
|
520 | + */ |
|
521 | + public function countUsers() { |
|
522 | + $filter = $this->access->getFilterForUserCount(); |
|
523 | + $cacheKey = 'countUsers-'.$filter; |
|
524 | + if(!is_null($entries = $this->access->connection->getFromCache($cacheKey))) { |
|
525 | + return $entries; |
|
526 | + } |
|
527 | + $entries = $this->access->countUsers($filter); |
|
528 | + $this->access->connection->writeToCache($cacheKey, $entries); |
|
529 | + return $entries; |
|
530 | + } |
|
531 | + |
|
532 | + /** |
|
533 | + * Backend name to be shown in user management |
|
534 | + * @return string the name of the backend to be shown |
|
535 | + */ |
|
536 | + public function getBackendName(){ |
|
537 | + return 'LDAP'; |
|
538 | + } |
|
539 | 539 | |
540 | - /** |
|
541 | - * Return access for LDAP interaction. |
|
542 | - * @param string $uid |
|
543 | - * @return Access instance of Access for LDAP interaction |
|
544 | - */ |
|
545 | - public function getLDAPAccess($uid) { |
|
546 | - return $this->access; |
|
547 | - } |
|
540 | + /** |
|
541 | + * Return access for LDAP interaction. |
|
542 | + * @param string $uid |
|
543 | + * @return Access instance of Access for LDAP interaction |
|
544 | + */ |
|
545 | + public function getLDAPAccess($uid) { |
|
546 | + return $this->access; |
|
547 | + } |
|
548 | 548 | |
549 | - /** |
|
550 | - * Return LDAP connection resource from a cloned connection. |
|
551 | - * The cloned connection needs to be closed manually. |
|
552 | - * of the current access. |
|
553 | - * @param string $uid |
|
554 | - * @return resource of the LDAP connection |
|
555 | - */ |
|
556 | - public function getNewLDAPConnection($uid) { |
|
557 | - $connection = clone $this->access->getConnection(); |
|
558 | - return $connection->getConnectionResource(); |
|
559 | - } |
|
549 | + /** |
|
550 | + * Return LDAP connection resource from a cloned connection. |
|
551 | + * The cloned connection needs to be closed manually. |
|
552 | + * of the current access. |
|
553 | + * @param string $uid |
|
554 | + * @return resource of the LDAP connection |
|
555 | + */ |
|
556 | + public function getNewLDAPConnection($uid) { |
|
557 | + $connection = clone $this->access->getConnection(); |
|
558 | + return $connection->getConnectionResource(); |
|
559 | + } |
|
560 | 560 | } |
@@ -67,8 +67,8 @@ discard block |
||
67 | 67 | } |
68 | 68 | |
69 | 69 | protected function registerHooks() { |
70 | - Util::connectHook('OC_User','pre_deleteUser', $this, 'preDeleteUser'); |
|
71 | - Util::connectHook('OC_User','post_deleteUser', $this, 'postDeleteUser'); |
|
70 | + Util::connectHook('OC_User', 'pre_deleteUser', $this, 'preDeleteUser'); |
|
71 | + Util::connectHook('OC_User', 'post_deleteUser', $this, 'postDeleteUser'); |
|
72 | 72 | } |
73 | 73 | |
74 | 74 | public function preDeleteUser(\OC\User\User $user) { |
@@ -86,10 +86,10 @@ discard block |
||
86 | 86 | */ |
87 | 87 | public function canChangeAvatar($uid) { |
88 | 88 | $user = $this->access->userManager->get($uid); |
89 | - if(!$user instanceof User) { |
|
89 | + if (!$user instanceof User) { |
|
90 | 90 | return false; |
91 | 91 | } |
92 | - if($user->getAvatarImage() === false) { |
|
92 | + if ($user->getAvatarImage() === false) { |
|
93 | 93 | return true; |
94 | 94 | } |
95 | 95 | |
@@ -105,14 +105,14 @@ discard block |
||
105 | 105 | public function loginName2UserName($loginName) { |
106 | 106 | $cacheKey = 'loginName2UserName-'.$loginName; |
107 | 107 | $username = $this->access->connection->getFromCache($cacheKey); |
108 | - if(!is_null($username)) { |
|
108 | + if (!is_null($username)) { |
|
109 | 109 | return $username; |
110 | 110 | } |
111 | 111 | |
112 | 112 | try { |
113 | 113 | $ldapRecord = $this->getLDAPUserByLoginName($loginName); |
114 | 114 | $user = $this->access->userManager->get($ldapRecord['dn'][0]); |
115 | - if($user instanceof OfflineUser) { |
|
115 | + if ($user instanceof OfflineUser) { |
|
116 | 116 | // this path is not really possible, however get() is documented |
117 | 117 | // to return User or OfflineUser so we are very defensive here. |
118 | 118 | $this->access->connection->writeToCache($cacheKey, false); |
@@ -148,9 +148,9 @@ discard block |
||
148 | 148 | //find out dn of the user name |
149 | 149 | $attrs = $this->access->userManager->getAttributes(); |
150 | 150 | $users = $this->access->fetchUsersByLoginName($loginName, $attrs); |
151 | - if(count($users) < 1) { |
|
152 | - throw new NotOnLDAP('No user available for the given login name on ' . |
|
153 | - $this->access->connection->ldapHost . ':' . $this->access->connection->ldapPort); |
|
151 | + if (count($users) < 1) { |
|
152 | + throw new NotOnLDAP('No user available for the given login name on '. |
|
153 | + $this->access->connection->ldapHost.':'.$this->access->connection->ldapPort); |
|
154 | 154 | } |
155 | 155 | return $users[0]; |
156 | 156 | } |
@@ -165,8 +165,8 @@ discard block |
||
165 | 165 | public function checkPassword($uid, $password) { |
166 | 166 | try { |
167 | 167 | $ldapRecord = $this->getLDAPUserByLoginName($uid); |
168 | - } catch(NotOnLDAP $e) { |
|
169 | - if($this->ocConfig->getSystemValue('loglevel', Util::WARN) === Util::DEBUG) { |
|
168 | + } catch (NotOnLDAP $e) { |
|
169 | + if ($this->ocConfig->getSystemValue('loglevel', Util::WARN) === Util::DEBUG) { |
|
170 | 170 | \OC::$server->getLogger()->logException($e, ['app' => 'user_ldap']); |
171 | 171 | } |
172 | 172 | return false; |
@@ -174,16 +174,16 @@ discard block |
||
174 | 174 | $dn = $ldapRecord['dn'][0]; |
175 | 175 | $user = $this->access->userManager->get($dn); |
176 | 176 | |
177 | - if(!$user instanceof User) { |
|
177 | + if (!$user instanceof User) { |
|
178 | 178 | Util::writeLog('user_ldap', |
179 | - 'LDAP Login: Could not get user object for DN ' . $dn . |
|
179 | + 'LDAP Login: Could not get user object for DN '.$dn. |
|
180 | 180 | '. Maybe the LDAP entry has no set display name attribute?', |
181 | 181 | Util::WARN); |
182 | 182 | return false; |
183 | 183 | } |
184 | - if($user->getUsername() !== false) { |
|
184 | + if ($user->getUsername() !== false) { |
|
185 | 185 | //are the credentials OK? |
186 | - if(!$this->access->areCredentialsValid($dn, $password)) { |
|
186 | + if (!$this->access->areCredentialsValid($dn, $password)) { |
|
187 | 187 | return false; |
188 | 188 | } |
189 | 189 | |
@@ -206,11 +206,11 @@ discard block |
||
206 | 206 | public function setPassword($uid, $password) { |
207 | 207 | $user = $this->access->userManager->get($uid); |
208 | 208 | |
209 | - if(!$user instanceof User) { |
|
210 | - throw new \Exception('LDAP setPassword: Could not get user object for uid ' . $uid . |
|
209 | + if (!$user instanceof User) { |
|
210 | + throw new \Exception('LDAP setPassword: Could not get user object for uid '.$uid. |
|
211 | 211 | '. Maybe the LDAP entry has no set display name attribute?'); |
212 | 212 | } |
213 | - if($user->getUsername() !== false && $this->access->setPassword($user->getDN(), $password)) { |
|
213 | + if ($user->getUsername() !== false && $this->access->setPassword($user->getDN(), $password)) { |
|
214 | 214 | $ldapDefaultPPolicyDN = $this->access->connection->ldapDefaultPPolicyDN; |
215 | 215 | $turnOnPasswordChange = $this->access->connection->turnOnPasswordChange; |
216 | 216 | if (!empty($ldapDefaultPPolicyDN) && (intval($turnOnPasswordChange) === 1)) { |
@@ -242,18 +242,18 @@ discard block |
||
242 | 242 | |
243 | 243 | //check if users are cached, if so return |
244 | 244 | $ldap_users = $this->access->connection->getFromCache($cachekey); |
245 | - if(!is_null($ldap_users)) { |
|
245 | + if (!is_null($ldap_users)) { |
|
246 | 246 | return $ldap_users; |
247 | 247 | } |
248 | 248 | |
249 | 249 | // if we'd pass -1 to LDAP search, we'd end up in a Protocol |
250 | 250 | // error. With a limit of 0, we get 0 results. So we pass null. |
251 | - if($limit <= 0) { |
|
251 | + if ($limit <= 0) { |
|
252 | 252 | $limit = null; |
253 | 253 | } |
254 | 254 | $filter = $this->access->combineFilterWithAnd(array( |
255 | 255 | $this->access->connection->ldapUserFilter, |
256 | - $this->access->connection->ldapUserDisplayName . '=*', |
|
256 | + $this->access->connection->ldapUserDisplayName.'=*', |
|
257 | 257 | $this->access->getFilterPartForUserSearch($search) |
258 | 258 | )); |
259 | 259 | |
@@ -266,7 +266,7 @@ discard block |
||
266 | 266 | $this->access->userManager->getAttributes(true), |
267 | 267 | $limit, $offset); |
268 | 268 | $ldap_users = $this->access->nextcloudUserNames($ldap_users); |
269 | - Util::writeLog('user_ldap', 'getUsers: '.count($ldap_users). ' Users found', Util::DEBUG); |
|
269 | + Util::writeLog('user_ldap', 'getUsers: '.count($ldap_users).' Users found', Util::DEBUG); |
|
270 | 270 | |
271 | 271 | $this->access->connection->writeToCache($cachekey, $ldap_users); |
272 | 272 | return $ldap_users; |
@@ -282,29 +282,29 @@ discard block |
||
282 | 282 | * @throws \OC\ServerNotAvailableException |
283 | 283 | */ |
284 | 284 | public function userExistsOnLDAP($user) { |
285 | - if(is_string($user)) { |
|
285 | + if (is_string($user)) { |
|
286 | 286 | $user = $this->access->userManager->get($user); |
287 | 287 | } |
288 | - if(is_null($user)) { |
|
288 | + if (is_null($user)) { |
|
289 | 289 | return false; |
290 | 290 | } |
291 | 291 | |
292 | 292 | $dn = $user->getDN(); |
293 | 293 | //check if user really still exists by reading its entry |
294 | - if(!is_array($this->access->readAttribute($dn, '', $this->access->connection->ldapUserFilter))) { |
|
294 | + if (!is_array($this->access->readAttribute($dn, '', $this->access->connection->ldapUserFilter))) { |
|
295 | 295 | $lcr = $this->access->connection->getConnectionResource(); |
296 | - if(is_null($lcr)) { |
|
297 | - throw new \Exception('No LDAP Connection to server ' . $this->access->connection->ldapHost); |
|
296 | + if (is_null($lcr)) { |
|
297 | + throw new \Exception('No LDAP Connection to server '.$this->access->connection->ldapHost); |
|
298 | 298 | } |
299 | 299 | |
300 | 300 | try { |
301 | 301 | $uuid = $this->access->getUserMapper()->getUUIDByDN($dn); |
302 | - if(!$uuid) { |
|
302 | + if (!$uuid) { |
|
303 | 303 | return false; |
304 | 304 | } |
305 | 305 | $newDn = $this->access->getUserDnByUuid($uuid); |
306 | 306 | //check if renamed user is still valid by reapplying the ldap filter |
307 | - if(!is_array($this->access->readAttribute($newDn, '', $this->access->connection->ldapUserFilter))) { |
|
307 | + if (!is_array($this->access->readAttribute($newDn, '', $this->access->connection->ldapUserFilter))) { |
|
308 | 308 | return false; |
309 | 309 | } |
310 | 310 | $this->access->getUserMapper()->setDNbyUUID($newDn, $uuid); |
@@ -314,7 +314,7 @@ discard block |
||
314 | 314 | } |
315 | 315 | } |
316 | 316 | |
317 | - if($user instanceof OfflineUser) { |
|
317 | + if ($user instanceof OfflineUser) { |
|
318 | 318 | $user->unmark(); |
319 | 319 | } |
320 | 320 | |
@@ -329,18 +329,18 @@ discard block |
||
329 | 329 | */ |
330 | 330 | public function userExists($uid) { |
331 | 331 | $userExists = $this->access->connection->getFromCache('userExists'.$uid); |
332 | - if(!is_null($userExists)) { |
|
333 | - return (bool)$userExists; |
|
332 | + if (!is_null($userExists)) { |
|
333 | + return (bool) $userExists; |
|
334 | 334 | } |
335 | 335 | //getting dn, if false the user does not exist. If dn, he may be mapped only, requires more checking. |
336 | 336 | $user = $this->access->userManager->get($uid); |
337 | 337 | |
338 | - if(is_null($user)) { |
|
338 | + if (is_null($user)) { |
|
339 | 339 | Util::writeLog('user_ldap', 'No DN found for '.$uid.' on '. |
340 | 340 | $this->access->connection->ldapHost, Util::DEBUG); |
341 | 341 | $this->access->connection->writeToCache('userExists'.$uid, false); |
342 | 342 | return false; |
343 | - } else if($user instanceof OfflineUser) { |
|
343 | + } else if ($user instanceof OfflineUser) { |
|
344 | 344 | //express check for users marked as deleted. Returning true is |
345 | 345 | //necessary for cleanup |
346 | 346 | return true; |
@@ -348,7 +348,7 @@ discard block |
||
348 | 348 | |
349 | 349 | $result = $this->userExistsOnLDAP($user); |
350 | 350 | $this->access->connection->writeToCache('userExists'.$uid, $result); |
351 | - if($result === true) { |
|
351 | + if ($result === true) { |
|
352 | 352 | $user->update(); |
353 | 353 | } |
354 | 354 | return $result; |
@@ -362,13 +362,13 @@ discard block |
||
362 | 362 | */ |
363 | 363 | public function deleteUser($uid) { |
364 | 364 | $marked = $this->ocConfig->getUserValue($uid, 'user_ldap', 'isDeleted', 0); |
365 | - if(intval($marked) === 0) { |
|
365 | + if (intval($marked) === 0) { |
|
366 | 366 | \OC::$server->getLogger()->notice( |
367 | - 'User '.$uid . ' is not marked as deleted, not cleaning up.', |
|
367 | + 'User '.$uid.' is not marked as deleted, not cleaning up.', |
|
368 | 368 | array('app' => 'user_ldap')); |
369 | 369 | return false; |
370 | 370 | } |
371 | - \OC::$server->getLogger()->info('Cleaning up after user ' . $uid, |
|
371 | + \OC::$server->getLogger()->info('Cleaning up after user '.$uid, |
|
372 | 372 | array('app' => 'user_ldap')); |
373 | 373 | |
374 | 374 | //Get Home Directory out of user preferences so we can return it later, |
@@ -387,26 +387,26 @@ discard block |
||
387 | 387 | */ |
388 | 388 | public function getHome($uid) { |
389 | 389 | // user Exists check required as it is not done in user proxy! |
390 | - if(!$this->userExists($uid)) { |
|
390 | + if (!$this->userExists($uid)) { |
|
391 | 391 | return false; |
392 | 392 | } |
393 | 393 | |
394 | 394 | $cacheKey = 'getHome'.$uid; |
395 | 395 | $path = $this->access->connection->getFromCache($cacheKey); |
396 | - if(!is_null($path)) { |
|
396 | + if (!is_null($path)) { |
|
397 | 397 | return $path; |
398 | 398 | } |
399 | 399 | |
400 | 400 | // early return path if it is a deleted user |
401 | 401 | $user = $this->access->userManager->get($uid); |
402 | - if($user instanceof OfflineUser) { |
|
403 | - if($this->currentUserInDeletionProcess === $user->getUID()) { |
|
402 | + if ($user instanceof OfflineUser) { |
|
403 | + if ($this->currentUserInDeletionProcess === $user->getUID()) { |
|
404 | 404 | return $user->getHomePath(); |
405 | 405 | } else { |
406 | - throw new NoUserException($uid . ' is not a valid user anymore'); |
|
406 | + throw new NoUserException($uid.' is not a valid user anymore'); |
|
407 | 407 | } |
408 | 408 | } else if ($user === null) { |
409 | - throw new NoUserException($uid . ' is not a valid user anymore'); |
|
409 | + throw new NoUserException($uid.' is not a valid user anymore'); |
|
410 | 410 | } |
411 | 411 | |
412 | 412 | $path = $user->getHomePath(); |
@@ -421,12 +421,12 @@ discard block |
||
421 | 421 | * @return string|false display name |
422 | 422 | */ |
423 | 423 | public function getDisplayName($uid) { |
424 | - if(!$this->userExists($uid)) { |
|
424 | + if (!$this->userExists($uid)) { |
|
425 | 425 | return false; |
426 | 426 | } |
427 | 427 | |
428 | 428 | $cacheKey = 'getDisplayName'.$uid; |
429 | - if(!is_null($displayName = $this->access->connection->getFromCache($cacheKey))) { |
|
429 | + if (!is_null($displayName = $this->access->connection->getFromCache($cacheKey))) { |
|
430 | 430 | return $displayName; |
431 | 431 | } |
432 | 432 | |
@@ -443,10 +443,10 @@ discard block |
||
443 | 443 | $this->access->username2dn($uid), |
444 | 444 | $this->access->connection->ldapUserDisplayName); |
445 | 445 | |
446 | - if($displayName && (count($displayName) > 0)) { |
|
446 | + if ($displayName && (count($displayName) > 0)) { |
|
447 | 447 | $displayName = $displayName[0]; |
448 | 448 | |
449 | - if (is_array($displayName2)){ |
|
449 | + if (is_array($displayName2)) { |
|
450 | 450 | $displayName2 = count($displayName2) > 0 ? $displayName2[0] : ''; |
451 | 451 | } |
452 | 452 | |
@@ -475,7 +475,7 @@ discard block |
||
475 | 475 | */ |
476 | 476 | public function getDisplayNames($search = '', $limit = null, $offset = null) { |
477 | 477 | $cacheKey = 'getDisplayNames-'.$search.'-'.$limit.'-'.$offset; |
478 | - if(!is_null($displayNames = $this->access->connection->getFromCache($cacheKey))) { |
|
478 | + if (!is_null($displayNames = $this->access->connection->getFromCache($cacheKey))) { |
|
479 | 479 | return $displayNames; |
480 | 480 | } |
481 | 481 | |
@@ -497,12 +497,12 @@ discard block |
||
497 | 497 | * compared with OC_USER_BACKEND_CREATE_USER etc. |
498 | 498 | */ |
499 | 499 | public function implementsActions($actions) { |
500 | - return (bool)((Backend::CHECK_PASSWORD |
|
500 | + return (bool) ((Backend::CHECK_PASSWORD |
|
501 | 501 | | Backend::GET_HOME |
502 | 502 | | Backend::GET_DISPLAYNAME |
503 | 503 | | Backend::PROVIDE_AVATAR |
504 | 504 | | Backend::COUNT_USERS |
505 | - | ((intval($this->access->connection->turnOnPasswordChange) === 1)?(Backend::SET_PASSWORD):0)) |
|
505 | + | ((intval($this->access->connection->turnOnPasswordChange) === 1) ? (Backend::SET_PASSWORD) : 0)) |
|
506 | 506 | & $actions); |
507 | 507 | } |
508 | 508 | |
@@ -521,7 +521,7 @@ discard block |
||
521 | 521 | public function countUsers() { |
522 | 522 | $filter = $this->access->getFilterForUserCount(); |
523 | 523 | $cacheKey = 'countUsers-'.$filter; |
524 | - if(!is_null($entries = $this->access->connection->getFromCache($cacheKey))) { |
|
524 | + if (!is_null($entries = $this->access->connection->getFromCache($cacheKey))) { |
|
525 | 525 | return $entries; |
526 | 526 | } |
527 | 527 | $entries = $this->access->countUsers($filter); |
@@ -533,7 +533,7 @@ discard block |
||
533 | 533 | * Backend name to be shown in user management |
534 | 534 | * @return string the name of the backend to be shown |
535 | 535 | */ |
536 | - public function getBackendName(){ |
|
536 | + public function getBackendName() { |
|
537 | 537 | return 'LDAP'; |
538 | 538 | } |
539 | 539 |