@@ -52,576 +52,576 @@ |
||
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 | - if($this->ocConfig->getSystemValue('loglevel', ILogger::WARN) === ILogger::DEBUG) { |
|
194 | - \OC::$server->getLogger()->logException($e, ['app' => 'user_ldap']); |
|
195 | - } |
|
196 | - return false; |
|
197 | - } |
|
198 | - $dn = $ldapRecord['dn'][0]; |
|
199 | - $user = $this->access->userManager->get($dn); |
|
200 | - |
|
201 | - if(!$user instanceof User) { |
|
202 | - Util::writeLog('user_ldap', |
|
203 | - 'LDAP Login: Could not get user object for DN ' . $dn . |
|
204 | - '. Maybe the LDAP entry has no set display name attribute?', |
|
205 | - ILogger::WARN); |
|
206 | - return false; |
|
207 | - } |
|
208 | - if($user->getUsername() !== false) { |
|
209 | - //are the credentials OK? |
|
210 | - if(!$this->access->areCredentialsValid($dn, $password)) { |
|
211 | - return false; |
|
212 | - } |
|
213 | - |
|
214 | - $this->access->cacheUserExists($user->getUsername()); |
|
215 | - $user->processAttributes($ldapRecord); |
|
216 | - $user->markLogin(); |
|
217 | - |
|
218 | - return $user->getUsername(); |
|
219 | - } |
|
220 | - |
|
221 | - return false; |
|
222 | - } |
|
223 | - |
|
224 | - /** |
|
225 | - * Set password |
|
226 | - * @param string $uid The username |
|
227 | - * @param string $password The new password |
|
228 | - * @return bool |
|
229 | - */ |
|
230 | - public function setPassword($uid, $password) { |
|
231 | - if ($this->userPluginManager->implementsActions(Backend::SET_PASSWORD)) { |
|
232 | - return $this->userPluginManager->setPassword($uid, $password); |
|
233 | - } |
|
234 | - |
|
235 | - $user = $this->access->userManager->get($uid); |
|
236 | - |
|
237 | - if(!$user instanceof User) { |
|
238 | - throw new \Exception('LDAP setPassword: Could not get user object for uid ' . $uid . |
|
239 | - '. Maybe the LDAP entry has no set display name attribute?'); |
|
240 | - } |
|
241 | - if($user->getUsername() !== false && $this->access->setPassword($user->getDN(), $password)) { |
|
242 | - $ldapDefaultPPolicyDN = $this->access->connection->ldapDefaultPPolicyDN; |
|
243 | - $turnOnPasswordChange = $this->access->connection->turnOnPasswordChange; |
|
244 | - if (!empty($ldapDefaultPPolicyDN) && ((int)$turnOnPasswordChange === 1)) { |
|
245 | - //remove last password expiry warning if any |
|
246 | - $notification = $this->notificationManager->createNotification(); |
|
247 | - $notification->setApp('user_ldap') |
|
248 | - ->setUser($uid) |
|
249 | - ->setObject('pwd_exp_warn', $uid) |
|
250 | - ; |
|
251 | - $this->notificationManager->markProcessed($notification); |
|
252 | - } |
|
253 | - return true; |
|
254 | - } |
|
255 | - |
|
256 | - return false; |
|
257 | - } |
|
258 | - |
|
259 | - /** |
|
260 | - * Get a list of all users |
|
261 | - * |
|
262 | - * @param string $search |
|
263 | - * @param integer $limit |
|
264 | - * @param integer $offset |
|
265 | - * @return string[] an array of all uids |
|
266 | - */ |
|
267 | - public function getUsers($search = '', $limit = 10, $offset = 0) { |
|
268 | - $search = $this->access->escapeFilterPart($search, true); |
|
269 | - $cachekey = 'getUsers-'.$search.'-'.$limit.'-'.$offset; |
|
270 | - |
|
271 | - //check if users are cached, if so return |
|
272 | - $ldap_users = $this->access->connection->getFromCache($cachekey); |
|
273 | - if(!is_null($ldap_users)) { |
|
274 | - return $ldap_users; |
|
275 | - } |
|
276 | - |
|
277 | - // if we'd pass -1 to LDAP search, we'd end up in a Protocol |
|
278 | - // error. With a limit of 0, we get 0 results. So we pass null. |
|
279 | - if($limit <= 0) { |
|
280 | - $limit = null; |
|
281 | - } |
|
282 | - $filter = $this->access->combineFilterWithAnd(array( |
|
283 | - $this->access->connection->ldapUserFilter, |
|
284 | - $this->access->connection->ldapUserDisplayName . '=*', |
|
285 | - $this->access->getFilterPartForUserSearch($search) |
|
286 | - )); |
|
287 | - |
|
288 | - Util::writeLog('user_ldap', |
|
289 | - 'getUsers: Options: search '.$search.' limit '.$limit.' offset '.$offset.' Filter: '.$filter, |
|
290 | - ILogger::DEBUG); |
|
291 | - //do the search and translate results to Nextcloud names |
|
292 | - $ldap_users = $this->access->fetchListOfUsers( |
|
293 | - $filter, |
|
294 | - $this->access->userManager->getAttributes(true), |
|
295 | - $limit, $offset); |
|
296 | - $ldap_users = $this->access->nextcloudUserNames($ldap_users); |
|
297 | - Util::writeLog('user_ldap', 'getUsers: '.count($ldap_users). ' Users found', ILogger::DEBUG); |
|
298 | - |
|
299 | - $this->access->connection->writeToCache($cachekey, $ldap_users); |
|
300 | - return $ldap_users; |
|
301 | - } |
|
302 | - |
|
303 | - /** |
|
304 | - * checks whether a user is still available on LDAP |
|
305 | - * |
|
306 | - * @param string|\OCA\User_LDAP\User\User $user either the Nextcloud user |
|
307 | - * name or an instance of that user |
|
308 | - * @return bool |
|
309 | - * @throws \Exception |
|
310 | - * @throws \OC\ServerNotAvailableException |
|
311 | - */ |
|
312 | - public function userExistsOnLDAP($user) { |
|
313 | - if(is_string($user)) { |
|
314 | - $user = $this->access->userManager->get($user); |
|
315 | - } |
|
316 | - if(is_null($user)) { |
|
317 | - return false; |
|
318 | - } |
|
319 | - |
|
320 | - $dn = $user->getDN(); |
|
321 | - //check if user really still exists by reading its entry |
|
322 | - if(!is_array($this->access->readAttribute($dn, '', $this->access->connection->ldapUserFilter))) { |
|
323 | - try { |
|
324 | - $uuid = $this->access->getUserMapper()->getUUIDByDN($dn); |
|
325 | - if (!$uuid) { |
|
326 | - return false; |
|
327 | - } |
|
328 | - $newDn = $this->access->getUserDnByUuid($uuid); |
|
329 | - //check if renamed user is still valid by reapplying the ldap filter |
|
330 | - if ($newDn === $dn || !is_array($this->access->readAttribute($newDn, '', $this->access->connection->ldapUserFilter))) { |
|
331 | - return false; |
|
332 | - } |
|
333 | - $this->access->getUserMapper()->setDNbyUUID($newDn, $uuid); |
|
334 | - return true; |
|
335 | - } catch (ServerNotAvailableException $e) { |
|
336 | - throw $e; |
|
337 | - } catch (\Exception $e) { |
|
338 | - return false; |
|
339 | - } |
|
340 | - } |
|
341 | - |
|
342 | - if($user instanceof OfflineUser) { |
|
343 | - $user->unmark(); |
|
344 | - } |
|
345 | - |
|
346 | - return true; |
|
347 | - } |
|
348 | - |
|
349 | - /** |
|
350 | - * check if a user exists |
|
351 | - * @param string $uid the username |
|
352 | - * @return boolean |
|
353 | - * @throws \Exception when connection could not be established |
|
354 | - */ |
|
355 | - public function userExists($uid) { |
|
356 | - $userExists = $this->access->connection->getFromCache('userExists'.$uid); |
|
357 | - if(!is_null($userExists)) { |
|
358 | - return (bool)$userExists; |
|
359 | - } |
|
360 | - //getting dn, if false the user does not exist. If dn, he may be mapped only, requires more checking. |
|
361 | - $user = $this->access->userManager->get($uid); |
|
362 | - |
|
363 | - if(is_null($user)) { |
|
364 | - Util::writeLog('user_ldap', 'No DN found for '.$uid.' on '. |
|
365 | - $this->access->connection->ldapHost, ILogger::DEBUG); |
|
366 | - $this->access->connection->writeToCache('userExists'.$uid, false); |
|
367 | - return false; |
|
368 | - } else if($user instanceof OfflineUser) { |
|
369 | - //express check for users marked as deleted. Returning true is |
|
370 | - //necessary for cleanup |
|
371 | - return true; |
|
372 | - } |
|
373 | - |
|
374 | - $result = $this->userExistsOnLDAP($user); |
|
375 | - $this->access->connection->writeToCache('userExists'.$uid, $result); |
|
376 | - return $result; |
|
377 | - } |
|
378 | - |
|
379 | - /** |
|
380 | - * returns whether a user was deleted in LDAP |
|
381 | - * |
|
382 | - * @param string $uid The username of the user to delete |
|
383 | - * @return bool |
|
384 | - */ |
|
385 | - public function deleteUser($uid) { |
|
386 | - if ($this->userPluginManager->canDeleteUser()) { |
|
387 | - return $this->userPluginManager->deleteUser($uid); |
|
388 | - } |
|
389 | - |
|
390 | - $marked = $this->ocConfig->getUserValue($uid, 'user_ldap', 'isDeleted', 0); |
|
391 | - if((int)$marked === 0) { |
|
392 | - \OC::$server->getLogger()->notice( |
|
393 | - 'User '.$uid . ' is not marked as deleted, not cleaning up.', |
|
394 | - array('app' => 'user_ldap')); |
|
395 | - return false; |
|
396 | - } |
|
397 | - \OC::$server->getLogger()->info('Cleaning up after user ' . $uid, |
|
398 | - array('app' => 'user_ldap')); |
|
399 | - |
|
400 | - $this->access->getUserMapper()->unmap($uid); // we don't emit unassign signals here, since it is implicit to delete signals fired from core |
|
401 | - $this->access->userManager->invalidate($uid); |
|
402 | - return true; |
|
403 | - } |
|
404 | - |
|
405 | - /** |
|
406 | - * get the user's home directory |
|
407 | - * |
|
408 | - * @param string $uid the username |
|
409 | - * @return bool|string |
|
410 | - * @throws NoUserException |
|
411 | - * @throws \Exception |
|
412 | - */ |
|
413 | - public function getHome($uid) { |
|
414 | - // user Exists check required as it is not done in user proxy! |
|
415 | - if(!$this->userExists($uid)) { |
|
416 | - return false; |
|
417 | - } |
|
418 | - |
|
419 | - if ($this->userPluginManager->implementsActions(Backend::GET_HOME)) { |
|
420 | - return $this->userPluginManager->getHome($uid); |
|
421 | - } |
|
422 | - |
|
423 | - $cacheKey = 'getHome'.$uid; |
|
424 | - $path = $this->access->connection->getFromCache($cacheKey); |
|
425 | - if(!is_null($path)) { |
|
426 | - return $path; |
|
427 | - } |
|
428 | - |
|
429 | - // early return path if it is a deleted user |
|
430 | - $user = $this->access->userManager->get($uid); |
|
431 | - if($user instanceof OfflineUser) { |
|
432 | - if($this->currentUserInDeletionProcess !== null |
|
433 | - && $this->currentUserInDeletionProcess === $user->getOCName() |
|
434 | - ) { |
|
435 | - return $user->getHomePath(); |
|
436 | - } else { |
|
437 | - throw new NoUserException($uid . ' is not a valid user anymore'); |
|
438 | - } |
|
439 | - } else if ($user === null) { |
|
440 | - throw new NoUserException($uid . ' is not a valid user anymore'); |
|
441 | - } |
|
442 | - |
|
443 | - $path = $user->getHomePath(); |
|
444 | - $this->access->cacheUserHome($uid, $path); |
|
445 | - |
|
446 | - return $path; |
|
447 | - } |
|
448 | - |
|
449 | - /** |
|
450 | - * get display name of the user |
|
451 | - * @param string $uid user ID of the user |
|
452 | - * @return string|false display name |
|
453 | - */ |
|
454 | - public function getDisplayName($uid) { |
|
455 | - if ($this->userPluginManager->implementsActions(Backend::GET_DISPLAYNAME)) { |
|
456 | - return $this->userPluginManager->getDisplayName($uid); |
|
457 | - } |
|
458 | - |
|
459 | - if(!$this->userExists($uid)) { |
|
460 | - return false; |
|
461 | - } |
|
462 | - |
|
463 | - $cacheKey = 'getDisplayName'.$uid; |
|
464 | - if(!is_null($displayName = $this->access->connection->getFromCache($cacheKey))) { |
|
465 | - return $displayName; |
|
466 | - } |
|
467 | - |
|
468 | - //Check whether the display name is configured to have a 2nd feature |
|
469 | - $additionalAttribute = $this->access->connection->ldapUserDisplayName2; |
|
470 | - $displayName2 = ''; |
|
471 | - if ($additionalAttribute !== '') { |
|
472 | - $displayName2 = $this->access->readAttribute( |
|
473 | - $this->access->username2dn($uid), |
|
474 | - $additionalAttribute); |
|
475 | - } |
|
476 | - |
|
477 | - $displayName = $this->access->readAttribute( |
|
478 | - $this->access->username2dn($uid), |
|
479 | - $this->access->connection->ldapUserDisplayName); |
|
480 | - |
|
481 | - if($displayName && (count($displayName) > 0)) { |
|
482 | - $displayName = $displayName[0]; |
|
483 | - |
|
484 | - if (is_array($displayName2)){ |
|
485 | - $displayName2 = count($displayName2) > 0 ? $displayName2[0] : ''; |
|
486 | - } |
|
487 | - |
|
488 | - $user = $this->access->userManager->get($uid); |
|
489 | - if ($user instanceof User) { |
|
490 | - $displayName = $user->composeAndStoreDisplayName($displayName, $displayName2); |
|
491 | - $this->access->connection->writeToCache($cacheKey, $displayName); |
|
492 | - } |
|
493 | - if ($user instanceof OfflineUser) { |
|
494 | - /** @var OfflineUser $user*/ |
|
495 | - $displayName = $user->getDisplayName(); |
|
496 | - } |
|
497 | - return $displayName; |
|
498 | - } |
|
499 | - |
|
500 | - return null; |
|
501 | - } |
|
502 | - |
|
503 | - /** |
|
504 | - * set display name of the user |
|
505 | - * @param string $uid user ID of the user |
|
506 | - * @param string $displayName new display name of the user |
|
507 | - * @return string|false display name |
|
508 | - */ |
|
509 | - public function setDisplayName($uid, $displayName) { |
|
510 | - if ($this->userPluginManager->implementsActions(Backend::SET_DISPLAYNAME)) { |
|
511 | - return $this->userPluginManager->setDisplayName($uid, $displayName); |
|
512 | - } |
|
513 | - return false; |
|
514 | - } |
|
515 | - |
|
516 | - /** |
|
517 | - * Get a list of all display names |
|
518 | - * |
|
519 | - * @param string $search |
|
520 | - * @param string|null $limit |
|
521 | - * @param string|null $offset |
|
522 | - * @return array an array of all displayNames (value) and the corresponding uids (key) |
|
523 | - */ |
|
524 | - public function getDisplayNames($search = '', $limit = null, $offset = null) { |
|
525 | - $cacheKey = 'getDisplayNames-'.$search.'-'.$limit.'-'.$offset; |
|
526 | - if(!is_null($displayNames = $this->access->connection->getFromCache($cacheKey))) { |
|
527 | - return $displayNames; |
|
528 | - } |
|
529 | - |
|
530 | - $displayNames = array(); |
|
531 | - $users = $this->getUsers($search, $limit, $offset); |
|
532 | - foreach ($users as $user) { |
|
533 | - $displayNames[$user] = $this->getDisplayName($user); |
|
534 | - } |
|
535 | - $this->access->connection->writeToCache($cacheKey, $displayNames); |
|
536 | - return $displayNames; |
|
537 | - } |
|
538 | - |
|
539 | - /** |
|
540 | - * Check if backend implements actions |
|
541 | - * @param int $actions bitwise-or'ed actions |
|
542 | - * @return boolean |
|
543 | - * |
|
544 | - * Returns the supported actions as int to be |
|
545 | - * compared with \OC\User\Backend::CREATE_USER etc. |
|
546 | - */ |
|
547 | - public function implementsActions($actions) { |
|
548 | - return (bool)((Backend::CHECK_PASSWORD |
|
549 | - | Backend::GET_HOME |
|
550 | - | Backend::GET_DISPLAYNAME |
|
551 | - | (($this->access->connection->ldapUserAvatarRule !== 'none') ? Backend::PROVIDE_AVATAR : 0) |
|
552 | - | Backend::COUNT_USERS |
|
553 | - | (((int)$this->access->connection->turnOnPasswordChange === 1)? Backend::SET_PASSWORD :0) |
|
554 | - | $this->userPluginManager->getImplementedActions()) |
|
555 | - & $actions); |
|
556 | - } |
|
557 | - |
|
558 | - /** |
|
559 | - * @return bool |
|
560 | - */ |
|
561 | - public function hasUserListings() { |
|
562 | - return true; |
|
563 | - } |
|
564 | - |
|
565 | - /** |
|
566 | - * counts the users in LDAP |
|
567 | - * |
|
568 | - * @return int|bool |
|
569 | - */ |
|
570 | - public function countUsers() { |
|
571 | - if ($this->userPluginManager->implementsActions(Backend::COUNT_USERS)) { |
|
572 | - return $this->userPluginManager->countUsers(); |
|
573 | - } |
|
574 | - |
|
575 | - $filter = $this->access->getFilterForUserCount(); |
|
576 | - $cacheKey = 'countUsers-'.$filter; |
|
577 | - if(!is_null($entries = $this->access->connection->getFromCache($cacheKey))) { |
|
578 | - return $entries; |
|
579 | - } |
|
580 | - $entries = $this->access->countUsers($filter); |
|
581 | - $this->access->connection->writeToCache($cacheKey, $entries); |
|
582 | - return $entries; |
|
583 | - } |
|
584 | - |
|
585 | - /** |
|
586 | - * Backend name to be shown in user management |
|
587 | - * @return string the name of the backend to be shown |
|
588 | - */ |
|
589 | - public function getBackendName(){ |
|
590 | - return 'LDAP'; |
|
591 | - } |
|
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 | + if($this->ocConfig->getSystemValue('loglevel', ILogger::WARN) === ILogger::DEBUG) { |
|
194 | + \OC::$server->getLogger()->logException($e, ['app' => 'user_ldap']); |
|
195 | + } |
|
196 | + return false; |
|
197 | + } |
|
198 | + $dn = $ldapRecord['dn'][0]; |
|
199 | + $user = $this->access->userManager->get($dn); |
|
200 | + |
|
201 | + if(!$user instanceof User) { |
|
202 | + Util::writeLog('user_ldap', |
|
203 | + 'LDAP Login: Could not get user object for DN ' . $dn . |
|
204 | + '. Maybe the LDAP entry has no set display name attribute?', |
|
205 | + ILogger::WARN); |
|
206 | + return false; |
|
207 | + } |
|
208 | + if($user->getUsername() !== false) { |
|
209 | + //are the credentials OK? |
|
210 | + if(!$this->access->areCredentialsValid($dn, $password)) { |
|
211 | + return false; |
|
212 | + } |
|
213 | + |
|
214 | + $this->access->cacheUserExists($user->getUsername()); |
|
215 | + $user->processAttributes($ldapRecord); |
|
216 | + $user->markLogin(); |
|
217 | + |
|
218 | + return $user->getUsername(); |
|
219 | + } |
|
220 | + |
|
221 | + return false; |
|
222 | + } |
|
223 | + |
|
224 | + /** |
|
225 | + * Set password |
|
226 | + * @param string $uid The username |
|
227 | + * @param string $password The new password |
|
228 | + * @return bool |
|
229 | + */ |
|
230 | + public function setPassword($uid, $password) { |
|
231 | + if ($this->userPluginManager->implementsActions(Backend::SET_PASSWORD)) { |
|
232 | + return $this->userPluginManager->setPassword($uid, $password); |
|
233 | + } |
|
234 | + |
|
235 | + $user = $this->access->userManager->get($uid); |
|
236 | + |
|
237 | + if(!$user instanceof User) { |
|
238 | + throw new \Exception('LDAP setPassword: Could not get user object for uid ' . $uid . |
|
239 | + '. Maybe the LDAP entry has no set display name attribute?'); |
|
240 | + } |
|
241 | + if($user->getUsername() !== false && $this->access->setPassword($user->getDN(), $password)) { |
|
242 | + $ldapDefaultPPolicyDN = $this->access->connection->ldapDefaultPPolicyDN; |
|
243 | + $turnOnPasswordChange = $this->access->connection->turnOnPasswordChange; |
|
244 | + if (!empty($ldapDefaultPPolicyDN) && ((int)$turnOnPasswordChange === 1)) { |
|
245 | + //remove last password expiry warning if any |
|
246 | + $notification = $this->notificationManager->createNotification(); |
|
247 | + $notification->setApp('user_ldap') |
|
248 | + ->setUser($uid) |
|
249 | + ->setObject('pwd_exp_warn', $uid) |
|
250 | + ; |
|
251 | + $this->notificationManager->markProcessed($notification); |
|
252 | + } |
|
253 | + return true; |
|
254 | + } |
|
255 | + |
|
256 | + return false; |
|
257 | + } |
|
258 | + |
|
259 | + /** |
|
260 | + * Get a list of all users |
|
261 | + * |
|
262 | + * @param string $search |
|
263 | + * @param integer $limit |
|
264 | + * @param integer $offset |
|
265 | + * @return string[] an array of all uids |
|
266 | + */ |
|
267 | + public function getUsers($search = '', $limit = 10, $offset = 0) { |
|
268 | + $search = $this->access->escapeFilterPart($search, true); |
|
269 | + $cachekey = 'getUsers-'.$search.'-'.$limit.'-'.$offset; |
|
270 | + |
|
271 | + //check if users are cached, if so return |
|
272 | + $ldap_users = $this->access->connection->getFromCache($cachekey); |
|
273 | + if(!is_null($ldap_users)) { |
|
274 | + return $ldap_users; |
|
275 | + } |
|
276 | + |
|
277 | + // if we'd pass -1 to LDAP search, we'd end up in a Protocol |
|
278 | + // error. With a limit of 0, we get 0 results. So we pass null. |
|
279 | + if($limit <= 0) { |
|
280 | + $limit = null; |
|
281 | + } |
|
282 | + $filter = $this->access->combineFilterWithAnd(array( |
|
283 | + $this->access->connection->ldapUserFilter, |
|
284 | + $this->access->connection->ldapUserDisplayName . '=*', |
|
285 | + $this->access->getFilterPartForUserSearch($search) |
|
286 | + )); |
|
287 | + |
|
288 | + Util::writeLog('user_ldap', |
|
289 | + 'getUsers: Options: search '.$search.' limit '.$limit.' offset '.$offset.' Filter: '.$filter, |
|
290 | + ILogger::DEBUG); |
|
291 | + //do the search and translate results to Nextcloud names |
|
292 | + $ldap_users = $this->access->fetchListOfUsers( |
|
293 | + $filter, |
|
294 | + $this->access->userManager->getAttributes(true), |
|
295 | + $limit, $offset); |
|
296 | + $ldap_users = $this->access->nextcloudUserNames($ldap_users); |
|
297 | + Util::writeLog('user_ldap', 'getUsers: '.count($ldap_users). ' Users found', ILogger::DEBUG); |
|
298 | + |
|
299 | + $this->access->connection->writeToCache($cachekey, $ldap_users); |
|
300 | + return $ldap_users; |
|
301 | + } |
|
302 | + |
|
303 | + /** |
|
304 | + * checks whether a user is still available on LDAP |
|
305 | + * |
|
306 | + * @param string|\OCA\User_LDAP\User\User $user either the Nextcloud user |
|
307 | + * name or an instance of that user |
|
308 | + * @return bool |
|
309 | + * @throws \Exception |
|
310 | + * @throws \OC\ServerNotAvailableException |
|
311 | + */ |
|
312 | + public function userExistsOnLDAP($user) { |
|
313 | + if(is_string($user)) { |
|
314 | + $user = $this->access->userManager->get($user); |
|
315 | + } |
|
316 | + if(is_null($user)) { |
|
317 | + return false; |
|
318 | + } |
|
319 | + |
|
320 | + $dn = $user->getDN(); |
|
321 | + //check if user really still exists by reading its entry |
|
322 | + if(!is_array($this->access->readAttribute($dn, '', $this->access->connection->ldapUserFilter))) { |
|
323 | + try { |
|
324 | + $uuid = $this->access->getUserMapper()->getUUIDByDN($dn); |
|
325 | + if (!$uuid) { |
|
326 | + return false; |
|
327 | + } |
|
328 | + $newDn = $this->access->getUserDnByUuid($uuid); |
|
329 | + //check if renamed user is still valid by reapplying the ldap filter |
|
330 | + if ($newDn === $dn || !is_array($this->access->readAttribute($newDn, '', $this->access->connection->ldapUserFilter))) { |
|
331 | + return false; |
|
332 | + } |
|
333 | + $this->access->getUserMapper()->setDNbyUUID($newDn, $uuid); |
|
334 | + return true; |
|
335 | + } catch (ServerNotAvailableException $e) { |
|
336 | + throw $e; |
|
337 | + } catch (\Exception $e) { |
|
338 | + return false; |
|
339 | + } |
|
340 | + } |
|
341 | + |
|
342 | + if($user instanceof OfflineUser) { |
|
343 | + $user->unmark(); |
|
344 | + } |
|
345 | + |
|
346 | + return true; |
|
347 | + } |
|
348 | + |
|
349 | + /** |
|
350 | + * check if a user exists |
|
351 | + * @param string $uid the username |
|
352 | + * @return boolean |
|
353 | + * @throws \Exception when connection could not be established |
|
354 | + */ |
|
355 | + public function userExists($uid) { |
|
356 | + $userExists = $this->access->connection->getFromCache('userExists'.$uid); |
|
357 | + if(!is_null($userExists)) { |
|
358 | + return (bool)$userExists; |
|
359 | + } |
|
360 | + //getting dn, if false the user does not exist. If dn, he may be mapped only, requires more checking. |
|
361 | + $user = $this->access->userManager->get($uid); |
|
362 | + |
|
363 | + if(is_null($user)) { |
|
364 | + Util::writeLog('user_ldap', 'No DN found for '.$uid.' on '. |
|
365 | + $this->access->connection->ldapHost, ILogger::DEBUG); |
|
366 | + $this->access->connection->writeToCache('userExists'.$uid, false); |
|
367 | + return false; |
|
368 | + } else if($user instanceof OfflineUser) { |
|
369 | + //express check for users marked as deleted. Returning true is |
|
370 | + //necessary for cleanup |
|
371 | + return true; |
|
372 | + } |
|
373 | + |
|
374 | + $result = $this->userExistsOnLDAP($user); |
|
375 | + $this->access->connection->writeToCache('userExists'.$uid, $result); |
|
376 | + return $result; |
|
377 | + } |
|
378 | + |
|
379 | + /** |
|
380 | + * returns whether a user was deleted in LDAP |
|
381 | + * |
|
382 | + * @param string $uid The username of the user to delete |
|
383 | + * @return bool |
|
384 | + */ |
|
385 | + public function deleteUser($uid) { |
|
386 | + if ($this->userPluginManager->canDeleteUser()) { |
|
387 | + return $this->userPluginManager->deleteUser($uid); |
|
388 | + } |
|
389 | + |
|
390 | + $marked = $this->ocConfig->getUserValue($uid, 'user_ldap', 'isDeleted', 0); |
|
391 | + if((int)$marked === 0) { |
|
392 | + \OC::$server->getLogger()->notice( |
|
393 | + 'User '.$uid . ' is not marked as deleted, not cleaning up.', |
|
394 | + array('app' => 'user_ldap')); |
|
395 | + return false; |
|
396 | + } |
|
397 | + \OC::$server->getLogger()->info('Cleaning up after user ' . $uid, |
|
398 | + array('app' => 'user_ldap')); |
|
399 | + |
|
400 | + $this->access->getUserMapper()->unmap($uid); // we don't emit unassign signals here, since it is implicit to delete signals fired from core |
|
401 | + $this->access->userManager->invalidate($uid); |
|
402 | + return true; |
|
403 | + } |
|
404 | + |
|
405 | + /** |
|
406 | + * get the user's home directory |
|
407 | + * |
|
408 | + * @param string $uid the username |
|
409 | + * @return bool|string |
|
410 | + * @throws NoUserException |
|
411 | + * @throws \Exception |
|
412 | + */ |
|
413 | + public function getHome($uid) { |
|
414 | + // user Exists check required as it is not done in user proxy! |
|
415 | + if(!$this->userExists($uid)) { |
|
416 | + return false; |
|
417 | + } |
|
418 | + |
|
419 | + if ($this->userPluginManager->implementsActions(Backend::GET_HOME)) { |
|
420 | + return $this->userPluginManager->getHome($uid); |
|
421 | + } |
|
422 | + |
|
423 | + $cacheKey = 'getHome'.$uid; |
|
424 | + $path = $this->access->connection->getFromCache($cacheKey); |
|
425 | + if(!is_null($path)) { |
|
426 | + return $path; |
|
427 | + } |
|
428 | + |
|
429 | + // early return path if it is a deleted user |
|
430 | + $user = $this->access->userManager->get($uid); |
|
431 | + if($user instanceof OfflineUser) { |
|
432 | + if($this->currentUserInDeletionProcess !== null |
|
433 | + && $this->currentUserInDeletionProcess === $user->getOCName() |
|
434 | + ) { |
|
435 | + return $user->getHomePath(); |
|
436 | + } else { |
|
437 | + throw new NoUserException($uid . ' is not a valid user anymore'); |
|
438 | + } |
|
439 | + } else if ($user === null) { |
|
440 | + throw new NoUserException($uid . ' is not a valid user anymore'); |
|
441 | + } |
|
442 | + |
|
443 | + $path = $user->getHomePath(); |
|
444 | + $this->access->cacheUserHome($uid, $path); |
|
445 | + |
|
446 | + return $path; |
|
447 | + } |
|
448 | + |
|
449 | + /** |
|
450 | + * get display name of the user |
|
451 | + * @param string $uid user ID of the user |
|
452 | + * @return string|false display name |
|
453 | + */ |
|
454 | + public function getDisplayName($uid) { |
|
455 | + if ($this->userPluginManager->implementsActions(Backend::GET_DISPLAYNAME)) { |
|
456 | + return $this->userPluginManager->getDisplayName($uid); |
|
457 | + } |
|
458 | + |
|
459 | + if(!$this->userExists($uid)) { |
|
460 | + return false; |
|
461 | + } |
|
462 | + |
|
463 | + $cacheKey = 'getDisplayName'.$uid; |
|
464 | + if(!is_null($displayName = $this->access->connection->getFromCache($cacheKey))) { |
|
465 | + return $displayName; |
|
466 | + } |
|
467 | + |
|
468 | + //Check whether the display name is configured to have a 2nd feature |
|
469 | + $additionalAttribute = $this->access->connection->ldapUserDisplayName2; |
|
470 | + $displayName2 = ''; |
|
471 | + if ($additionalAttribute !== '') { |
|
472 | + $displayName2 = $this->access->readAttribute( |
|
473 | + $this->access->username2dn($uid), |
|
474 | + $additionalAttribute); |
|
475 | + } |
|
476 | + |
|
477 | + $displayName = $this->access->readAttribute( |
|
478 | + $this->access->username2dn($uid), |
|
479 | + $this->access->connection->ldapUserDisplayName); |
|
480 | + |
|
481 | + if($displayName && (count($displayName) > 0)) { |
|
482 | + $displayName = $displayName[0]; |
|
483 | + |
|
484 | + if (is_array($displayName2)){ |
|
485 | + $displayName2 = count($displayName2) > 0 ? $displayName2[0] : ''; |
|
486 | + } |
|
487 | + |
|
488 | + $user = $this->access->userManager->get($uid); |
|
489 | + if ($user instanceof User) { |
|
490 | + $displayName = $user->composeAndStoreDisplayName($displayName, $displayName2); |
|
491 | + $this->access->connection->writeToCache($cacheKey, $displayName); |
|
492 | + } |
|
493 | + if ($user instanceof OfflineUser) { |
|
494 | + /** @var OfflineUser $user*/ |
|
495 | + $displayName = $user->getDisplayName(); |
|
496 | + } |
|
497 | + return $displayName; |
|
498 | + } |
|
499 | + |
|
500 | + return null; |
|
501 | + } |
|
502 | + |
|
503 | + /** |
|
504 | + * set display name of the user |
|
505 | + * @param string $uid user ID of the user |
|
506 | + * @param string $displayName new display name of the user |
|
507 | + * @return string|false display name |
|
508 | + */ |
|
509 | + public function setDisplayName($uid, $displayName) { |
|
510 | + if ($this->userPluginManager->implementsActions(Backend::SET_DISPLAYNAME)) { |
|
511 | + return $this->userPluginManager->setDisplayName($uid, $displayName); |
|
512 | + } |
|
513 | + return false; |
|
514 | + } |
|
515 | + |
|
516 | + /** |
|
517 | + * Get a list of all display names |
|
518 | + * |
|
519 | + * @param string $search |
|
520 | + * @param string|null $limit |
|
521 | + * @param string|null $offset |
|
522 | + * @return array an array of all displayNames (value) and the corresponding uids (key) |
|
523 | + */ |
|
524 | + public function getDisplayNames($search = '', $limit = null, $offset = null) { |
|
525 | + $cacheKey = 'getDisplayNames-'.$search.'-'.$limit.'-'.$offset; |
|
526 | + if(!is_null($displayNames = $this->access->connection->getFromCache($cacheKey))) { |
|
527 | + return $displayNames; |
|
528 | + } |
|
529 | + |
|
530 | + $displayNames = array(); |
|
531 | + $users = $this->getUsers($search, $limit, $offset); |
|
532 | + foreach ($users as $user) { |
|
533 | + $displayNames[$user] = $this->getDisplayName($user); |
|
534 | + } |
|
535 | + $this->access->connection->writeToCache($cacheKey, $displayNames); |
|
536 | + return $displayNames; |
|
537 | + } |
|
538 | + |
|
539 | + /** |
|
540 | + * Check if backend implements actions |
|
541 | + * @param int $actions bitwise-or'ed actions |
|
542 | + * @return boolean |
|
543 | + * |
|
544 | + * Returns the supported actions as int to be |
|
545 | + * compared with \OC\User\Backend::CREATE_USER etc. |
|
546 | + */ |
|
547 | + public function implementsActions($actions) { |
|
548 | + return (bool)((Backend::CHECK_PASSWORD |
|
549 | + | Backend::GET_HOME |
|
550 | + | Backend::GET_DISPLAYNAME |
|
551 | + | (($this->access->connection->ldapUserAvatarRule !== 'none') ? Backend::PROVIDE_AVATAR : 0) |
|
552 | + | Backend::COUNT_USERS |
|
553 | + | (((int)$this->access->connection->turnOnPasswordChange === 1)? Backend::SET_PASSWORD :0) |
|
554 | + | $this->userPluginManager->getImplementedActions()) |
|
555 | + & $actions); |
|
556 | + } |
|
557 | + |
|
558 | + /** |
|
559 | + * @return bool |
|
560 | + */ |
|
561 | + public function hasUserListings() { |
|
562 | + return true; |
|
563 | + } |
|
564 | + |
|
565 | + /** |
|
566 | + * counts the users in LDAP |
|
567 | + * |
|
568 | + * @return int|bool |
|
569 | + */ |
|
570 | + public function countUsers() { |
|
571 | + if ($this->userPluginManager->implementsActions(Backend::COUNT_USERS)) { |
|
572 | + return $this->userPluginManager->countUsers(); |
|
573 | + } |
|
574 | + |
|
575 | + $filter = $this->access->getFilterForUserCount(); |
|
576 | + $cacheKey = 'countUsers-'.$filter; |
|
577 | + if(!is_null($entries = $this->access->connection->getFromCache($cacheKey))) { |
|
578 | + return $entries; |
|
579 | + } |
|
580 | + $entries = $this->access->countUsers($filter); |
|
581 | + $this->access->connection->writeToCache($cacheKey, $entries); |
|
582 | + return $entries; |
|
583 | + } |
|
584 | + |
|
585 | + /** |
|
586 | + * Backend name to be shown in user management |
|
587 | + * @return string the name of the backend to be shown |
|
588 | + */ |
|
589 | + public function getBackendName(){ |
|
590 | + return 'LDAP'; |
|
591 | + } |
|
592 | 592 | |
593 | - /** |
|
594 | - * Return access for LDAP interaction. |
|
595 | - * @param string $uid |
|
596 | - * @return Access instance of Access for LDAP interaction |
|
597 | - */ |
|
598 | - public function getLDAPAccess($uid) { |
|
599 | - return $this->access; |
|
600 | - } |
|
593 | + /** |
|
594 | + * Return access for LDAP interaction. |
|
595 | + * @param string $uid |
|
596 | + * @return Access instance of Access for LDAP interaction |
|
597 | + */ |
|
598 | + public function getLDAPAccess($uid) { |
|
599 | + return $this->access; |
|
600 | + } |
|
601 | 601 | |
602 | - /** |
|
603 | - * Return LDAP connection resource from a cloned connection. |
|
604 | - * The cloned connection needs to be closed manually. |
|
605 | - * of the current access. |
|
606 | - * @param string $uid |
|
607 | - * @return resource of the LDAP connection |
|
608 | - */ |
|
609 | - public function getNewLDAPConnection($uid) { |
|
610 | - $connection = clone $this->access->getConnection(); |
|
611 | - return $connection->getConnectionResource(); |
|
612 | - } |
|
613 | - |
|
614 | - /** |
|
615 | - * create new user |
|
616 | - * @param string $username username of the new user |
|
617 | - * @param string $password password of the new user |
|
618 | - * @return bool was the user created? |
|
619 | - */ |
|
620 | - public function createUser($username, $password) { |
|
621 | - if ($this->userPluginManager->implementsActions(Backend::CREATE_USER)) { |
|
622 | - return $this->userPluginManager->createUser($username, $password); |
|
623 | - } |
|
624 | - return false; |
|
625 | - } |
|
602 | + /** |
|
603 | + * Return LDAP connection resource from a cloned connection. |
|
604 | + * The cloned connection needs to be closed manually. |
|
605 | + * of the current access. |
|
606 | + * @param string $uid |
|
607 | + * @return resource of the LDAP connection |
|
608 | + */ |
|
609 | + public function getNewLDAPConnection($uid) { |
|
610 | + $connection = clone $this->access->getConnection(); |
|
611 | + return $connection->getConnectionResource(); |
|
612 | + } |
|
613 | + |
|
614 | + /** |
|
615 | + * create new user |
|
616 | + * @param string $username username of the new user |
|
617 | + * @param string $password password of the new user |
|
618 | + * @return bool was the user created? |
|
619 | + */ |
|
620 | + public function createUser($username, $password) { |
|
621 | + if ($this->userPluginManager->implementsActions(Backend::CREATE_USER)) { |
|
622 | + return $this->userPluginManager->createUser($username, $password); |
|
623 | + } |
|
624 | + return false; |
|
625 | + } |
|
626 | 626 | |
627 | 627 | } |
@@ -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,8 +189,8 @@ discard block |
||
189 | 189 | public function checkPassword($uid, $password) { |
190 | 190 | try { |
191 | 191 | $ldapRecord = $this->getLDAPUserByLoginName($uid); |
192 | - } catch(NotOnLDAP $e) { |
|
193 | - if($this->ocConfig->getSystemValue('loglevel', ILogger::WARN) === ILogger::DEBUG) { |
|
192 | + } catch (NotOnLDAP $e) { |
|
193 | + if ($this->ocConfig->getSystemValue('loglevel', ILogger::WARN) === ILogger::DEBUG) { |
|
194 | 194 | \OC::$server->getLogger()->logException($e, ['app' => 'user_ldap']); |
195 | 195 | } |
196 | 196 | return false; |
@@ -198,16 +198,16 @@ discard block |
||
198 | 198 | $dn = $ldapRecord['dn'][0]; |
199 | 199 | $user = $this->access->userManager->get($dn); |
200 | 200 | |
201 | - if(!$user instanceof User) { |
|
201 | + if (!$user instanceof User) { |
|
202 | 202 | Util::writeLog('user_ldap', |
203 | - 'LDAP Login: Could not get user object for DN ' . $dn . |
|
203 | + 'LDAP Login: Could not get user object for DN '.$dn. |
|
204 | 204 | '. Maybe the LDAP entry has no set display name attribute?', |
205 | 205 | ILogger::WARN); |
206 | 206 | return false; |
207 | 207 | } |
208 | - if($user->getUsername() !== false) { |
|
208 | + if ($user->getUsername() !== false) { |
|
209 | 209 | //are the credentials OK? |
210 | - if(!$this->access->areCredentialsValid($dn, $password)) { |
|
210 | + if (!$this->access->areCredentialsValid($dn, $password)) { |
|
211 | 211 | return false; |
212 | 212 | } |
213 | 213 | |
@@ -234,14 +234,14 @@ discard block |
||
234 | 234 | |
235 | 235 | $user = $this->access->userManager->get($uid); |
236 | 236 | |
237 | - if(!$user instanceof User) { |
|
238 | - throw new \Exception('LDAP setPassword: Could not get user object for uid ' . $uid . |
|
237 | + if (!$user instanceof User) { |
|
238 | + throw new \Exception('LDAP setPassword: Could not get user object for uid '.$uid. |
|
239 | 239 | '. Maybe the LDAP entry has no set display name attribute?'); |
240 | 240 | } |
241 | - if($user->getUsername() !== false && $this->access->setPassword($user->getDN(), $password)) { |
|
241 | + if ($user->getUsername() !== false && $this->access->setPassword($user->getDN(), $password)) { |
|
242 | 242 | $ldapDefaultPPolicyDN = $this->access->connection->ldapDefaultPPolicyDN; |
243 | 243 | $turnOnPasswordChange = $this->access->connection->turnOnPasswordChange; |
244 | - if (!empty($ldapDefaultPPolicyDN) && ((int)$turnOnPasswordChange === 1)) { |
|
244 | + if (!empty($ldapDefaultPPolicyDN) && ((int) $turnOnPasswordChange === 1)) { |
|
245 | 245 | //remove last password expiry warning if any |
246 | 246 | $notification = $this->notificationManager->createNotification(); |
247 | 247 | $notification->setApp('user_ldap') |
@@ -270,18 +270,18 @@ discard block |
||
270 | 270 | |
271 | 271 | //check if users are cached, if so return |
272 | 272 | $ldap_users = $this->access->connection->getFromCache($cachekey); |
273 | - if(!is_null($ldap_users)) { |
|
273 | + if (!is_null($ldap_users)) { |
|
274 | 274 | return $ldap_users; |
275 | 275 | } |
276 | 276 | |
277 | 277 | // if we'd pass -1 to LDAP search, we'd end up in a Protocol |
278 | 278 | // error. With a limit of 0, we get 0 results. So we pass null. |
279 | - if($limit <= 0) { |
|
279 | + if ($limit <= 0) { |
|
280 | 280 | $limit = null; |
281 | 281 | } |
282 | 282 | $filter = $this->access->combineFilterWithAnd(array( |
283 | 283 | $this->access->connection->ldapUserFilter, |
284 | - $this->access->connection->ldapUserDisplayName . '=*', |
|
284 | + $this->access->connection->ldapUserDisplayName.'=*', |
|
285 | 285 | $this->access->getFilterPartForUserSearch($search) |
286 | 286 | )); |
287 | 287 | |
@@ -294,7 +294,7 @@ discard block |
||
294 | 294 | $this->access->userManager->getAttributes(true), |
295 | 295 | $limit, $offset); |
296 | 296 | $ldap_users = $this->access->nextcloudUserNames($ldap_users); |
297 | - Util::writeLog('user_ldap', 'getUsers: '.count($ldap_users). ' Users found', ILogger::DEBUG); |
|
297 | + Util::writeLog('user_ldap', 'getUsers: '.count($ldap_users).' Users found', ILogger::DEBUG); |
|
298 | 298 | |
299 | 299 | $this->access->connection->writeToCache($cachekey, $ldap_users); |
300 | 300 | return $ldap_users; |
@@ -310,16 +310,16 @@ discard block |
||
310 | 310 | * @throws \OC\ServerNotAvailableException |
311 | 311 | */ |
312 | 312 | public function userExistsOnLDAP($user) { |
313 | - if(is_string($user)) { |
|
313 | + if (is_string($user)) { |
|
314 | 314 | $user = $this->access->userManager->get($user); |
315 | 315 | } |
316 | - if(is_null($user)) { |
|
316 | + if (is_null($user)) { |
|
317 | 317 | return false; |
318 | 318 | } |
319 | 319 | |
320 | 320 | $dn = $user->getDN(); |
321 | 321 | //check if user really still exists by reading its entry |
322 | - if(!is_array($this->access->readAttribute($dn, '', $this->access->connection->ldapUserFilter))) { |
|
322 | + if (!is_array($this->access->readAttribute($dn, '', $this->access->connection->ldapUserFilter))) { |
|
323 | 323 | try { |
324 | 324 | $uuid = $this->access->getUserMapper()->getUUIDByDN($dn); |
325 | 325 | if (!$uuid) { |
@@ -339,7 +339,7 @@ discard block |
||
339 | 339 | } |
340 | 340 | } |
341 | 341 | |
342 | - if($user instanceof OfflineUser) { |
|
342 | + if ($user instanceof OfflineUser) { |
|
343 | 343 | $user->unmark(); |
344 | 344 | } |
345 | 345 | |
@@ -354,18 +354,18 @@ discard block |
||
354 | 354 | */ |
355 | 355 | public function userExists($uid) { |
356 | 356 | $userExists = $this->access->connection->getFromCache('userExists'.$uid); |
357 | - if(!is_null($userExists)) { |
|
358 | - return (bool)$userExists; |
|
357 | + if (!is_null($userExists)) { |
|
358 | + return (bool) $userExists; |
|
359 | 359 | } |
360 | 360 | //getting dn, if false the user does not exist. If dn, he may be mapped only, requires more checking. |
361 | 361 | $user = $this->access->userManager->get($uid); |
362 | 362 | |
363 | - if(is_null($user)) { |
|
363 | + if (is_null($user)) { |
|
364 | 364 | Util::writeLog('user_ldap', 'No DN found for '.$uid.' on '. |
365 | 365 | $this->access->connection->ldapHost, ILogger::DEBUG); |
366 | 366 | $this->access->connection->writeToCache('userExists'.$uid, false); |
367 | 367 | return false; |
368 | - } else if($user instanceof OfflineUser) { |
|
368 | + } else if ($user instanceof OfflineUser) { |
|
369 | 369 | //express check for users marked as deleted. Returning true is |
370 | 370 | //necessary for cleanup |
371 | 371 | return true; |
@@ -388,13 +388,13 @@ discard block |
||
388 | 388 | } |
389 | 389 | |
390 | 390 | $marked = $this->ocConfig->getUserValue($uid, 'user_ldap', 'isDeleted', 0); |
391 | - if((int)$marked === 0) { |
|
391 | + if ((int) $marked === 0) { |
|
392 | 392 | \OC::$server->getLogger()->notice( |
393 | - 'User '.$uid . ' is not marked as deleted, not cleaning up.', |
|
393 | + 'User '.$uid.' is not marked as deleted, not cleaning up.', |
|
394 | 394 | array('app' => 'user_ldap')); |
395 | 395 | return false; |
396 | 396 | } |
397 | - \OC::$server->getLogger()->info('Cleaning up after user ' . $uid, |
|
397 | + \OC::$server->getLogger()->info('Cleaning up after user '.$uid, |
|
398 | 398 | array('app' => 'user_ldap')); |
399 | 399 | |
400 | 400 | $this->access->getUserMapper()->unmap($uid); // we don't emit unassign signals here, since it is implicit to delete signals fired from core |
@@ -412,7 +412,7 @@ discard block |
||
412 | 412 | */ |
413 | 413 | public function getHome($uid) { |
414 | 414 | // user Exists check required as it is not done in user proxy! |
415 | - if(!$this->userExists($uid)) { |
|
415 | + if (!$this->userExists($uid)) { |
|
416 | 416 | return false; |
417 | 417 | } |
418 | 418 | |
@@ -422,22 +422,22 @@ discard block |
||
422 | 422 | |
423 | 423 | $cacheKey = 'getHome'.$uid; |
424 | 424 | $path = $this->access->connection->getFromCache($cacheKey); |
425 | - if(!is_null($path)) { |
|
425 | + if (!is_null($path)) { |
|
426 | 426 | return $path; |
427 | 427 | } |
428 | 428 | |
429 | 429 | // early return path if it is a deleted user |
430 | 430 | $user = $this->access->userManager->get($uid); |
431 | - if($user instanceof OfflineUser) { |
|
432 | - if($this->currentUserInDeletionProcess !== null |
|
431 | + if ($user instanceof OfflineUser) { |
|
432 | + if ($this->currentUserInDeletionProcess !== null |
|
433 | 433 | && $this->currentUserInDeletionProcess === $user->getOCName() |
434 | 434 | ) { |
435 | 435 | return $user->getHomePath(); |
436 | 436 | } else { |
437 | - throw new NoUserException($uid . ' is not a valid user anymore'); |
|
437 | + throw new NoUserException($uid.' is not a valid user anymore'); |
|
438 | 438 | } |
439 | 439 | } else if ($user === null) { |
440 | - throw new NoUserException($uid . ' is not a valid user anymore'); |
|
440 | + throw new NoUserException($uid.' is not a valid user anymore'); |
|
441 | 441 | } |
442 | 442 | |
443 | 443 | $path = $user->getHomePath(); |
@@ -456,12 +456,12 @@ discard block |
||
456 | 456 | return $this->userPluginManager->getDisplayName($uid); |
457 | 457 | } |
458 | 458 | |
459 | - if(!$this->userExists($uid)) { |
|
459 | + if (!$this->userExists($uid)) { |
|
460 | 460 | return false; |
461 | 461 | } |
462 | 462 | |
463 | 463 | $cacheKey = 'getDisplayName'.$uid; |
464 | - if(!is_null($displayName = $this->access->connection->getFromCache($cacheKey))) { |
|
464 | + if (!is_null($displayName = $this->access->connection->getFromCache($cacheKey))) { |
|
465 | 465 | return $displayName; |
466 | 466 | } |
467 | 467 | |
@@ -478,10 +478,10 @@ discard block |
||
478 | 478 | $this->access->username2dn($uid), |
479 | 479 | $this->access->connection->ldapUserDisplayName); |
480 | 480 | |
481 | - if($displayName && (count($displayName) > 0)) { |
|
481 | + if ($displayName && (count($displayName) > 0)) { |
|
482 | 482 | $displayName = $displayName[0]; |
483 | 483 | |
484 | - if (is_array($displayName2)){ |
|
484 | + if (is_array($displayName2)) { |
|
485 | 485 | $displayName2 = count($displayName2) > 0 ? $displayName2[0] : ''; |
486 | 486 | } |
487 | 487 | |
@@ -523,7 +523,7 @@ discard block |
||
523 | 523 | */ |
524 | 524 | public function getDisplayNames($search = '', $limit = null, $offset = null) { |
525 | 525 | $cacheKey = 'getDisplayNames-'.$search.'-'.$limit.'-'.$offset; |
526 | - if(!is_null($displayNames = $this->access->connection->getFromCache($cacheKey))) { |
|
526 | + if (!is_null($displayNames = $this->access->connection->getFromCache($cacheKey))) { |
|
527 | 527 | return $displayNames; |
528 | 528 | } |
529 | 529 | |
@@ -545,12 +545,12 @@ discard block |
||
545 | 545 | * compared with \OC\User\Backend::CREATE_USER etc. |
546 | 546 | */ |
547 | 547 | public function implementsActions($actions) { |
548 | - return (bool)((Backend::CHECK_PASSWORD |
|
548 | + return (bool) ((Backend::CHECK_PASSWORD |
|
549 | 549 | | Backend::GET_HOME |
550 | 550 | | Backend::GET_DISPLAYNAME |
551 | 551 | | (($this->access->connection->ldapUserAvatarRule !== 'none') ? Backend::PROVIDE_AVATAR : 0) |
552 | 552 | | Backend::COUNT_USERS |
553 | - | (((int)$this->access->connection->turnOnPasswordChange === 1)? Backend::SET_PASSWORD :0) |
|
553 | + | (((int) $this->access->connection->turnOnPasswordChange === 1) ? Backend::SET_PASSWORD : 0) |
|
554 | 554 | | $this->userPluginManager->getImplementedActions()) |
555 | 555 | & $actions); |
556 | 556 | } |
@@ -574,7 +574,7 @@ discard block |
||
574 | 574 | |
575 | 575 | $filter = $this->access->getFilterForUserCount(); |
576 | 576 | $cacheKey = 'countUsers-'.$filter; |
577 | - if(!is_null($entries = $this->access->connection->getFromCache($cacheKey))) { |
|
577 | + if (!is_null($entries = $this->access->connection->getFromCache($cacheKey))) { |
|
578 | 578 | return $entries; |
579 | 579 | } |
580 | 580 | $entries = $this->access->countUsers($filter); |
@@ -586,7 +586,7 @@ discard block |
||
586 | 586 | * Backend name to be shown in user management |
587 | 587 | * @return string the name of the backend to be shown |
588 | 588 | */ |
589 | - public function getBackendName(){ |
|
589 | + public function getBackendName() { |
|
590 | 590 | return 'LDAP'; |
591 | 591 | } |
592 | 592 |