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