@@ -51,576 +51,576 @@ |
||
| 51 | 51 | use OCP\Util; |
| 52 | 52 | |
| 53 | 53 | class User_LDAP extends BackendUtility implements \OCP\IUserBackend, \OCP\UserInterface, IUserLDAP { |
| 54 | - /** @var \OCP\IConfig */ |
|
| 55 | - protected $ocConfig; |
|
| 56 | - |
|
| 57 | - /** @var INotificationManager */ |
|
| 58 | - protected $notificationManager; |
|
| 59 | - |
|
| 60 | - /** @var string */ |
|
| 61 | - protected $currentUserInDeletionProcess; |
|
| 62 | - |
|
| 63 | - /** @var UserPluginManager */ |
|
| 64 | - protected $userPluginManager; |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * @param Access $access |
|
| 68 | - * @param \OCP\IConfig $ocConfig |
|
| 69 | - * @param \OCP\Notification\IManager $notificationManager |
|
| 70 | - * @param IUserSession $userSession |
|
| 71 | - */ |
|
| 72 | - public function __construct(Access $access, IConfig $ocConfig, INotificationManager $notificationManager, IUserSession $userSession, UserPluginManager $userPluginManager) { |
|
| 73 | - parent::__construct($access); |
|
| 74 | - $this->ocConfig = $ocConfig; |
|
| 75 | - $this->notificationManager = $notificationManager; |
|
| 76 | - $this->userPluginManager = $userPluginManager; |
|
| 77 | - $this->registerHooks($userSession); |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - protected function registerHooks(IUserSession $userSession) { |
|
| 81 | - $userSession->listen('\OC\User', 'preDelete', [$this, 'preDeleteUser']); |
|
| 82 | - $userSession->listen('\OC\User', 'postDelete', [$this, 'postDeleteUser']); |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - public function preDeleteUser(IUser $user) { |
|
| 86 | - $this->currentUserInDeletionProcess = $user->getUID(); |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - public function postDeleteUser() { |
|
| 90 | - $this->currentUserInDeletionProcess = null; |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * checks whether the user is allowed to change his avatar in Nextcloud |
|
| 95 | - * @param string $uid the Nextcloud user name |
|
| 96 | - * @return boolean either the user can or cannot |
|
| 97 | - */ |
|
| 98 | - public function canChangeAvatar($uid) { |
|
| 99 | - if ($this->userPluginManager->implementsActions(Backend::PROVIDE_AVATAR)) { |
|
| 100 | - return $this->userPluginManager->canChangeAvatar($uid); |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - $user = $this->access->userManager->get($uid); |
|
| 104 | - if(!$user instanceof User) { |
|
| 105 | - return false; |
|
| 106 | - } |
|
| 107 | - if($user->getAvatarImage() === false) { |
|
| 108 | - return true; |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - return false; |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * returns the username for the given login name, if available |
|
| 116 | - * |
|
| 117 | - * @param string $loginName |
|
| 118 | - * @return string|false |
|
| 119 | - */ |
|
| 120 | - public function loginName2UserName($loginName) { |
|
| 121 | - $cacheKey = 'loginName2UserName-'.$loginName; |
|
| 122 | - $username = $this->access->connection->getFromCache($cacheKey); |
|
| 123 | - if(!is_null($username)) { |
|
| 124 | - return $username; |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - try { |
|
| 128 | - $ldapRecord = $this->getLDAPUserByLoginName($loginName); |
|
| 129 | - $user = $this->access->userManager->get($ldapRecord['dn'][0]); |
|
| 130 | - if($user instanceof OfflineUser) { |
|
| 131 | - // this path is not really possible, however get() is documented |
|
| 132 | - // to return User or OfflineUser so we are very defensive here. |
|
| 133 | - $this->access->connection->writeToCache($cacheKey, false); |
|
| 134 | - return false; |
|
| 135 | - } |
|
| 136 | - $username = $user->getUsername(); |
|
| 137 | - $this->access->connection->writeToCache($cacheKey, $username); |
|
| 138 | - return $username; |
|
| 139 | - } catch (NotOnLDAP $e) { |
|
| 140 | - $this->access->connection->writeToCache($cacheKey, false); |
|
| 141 | - return false; |
|
| 142 | - } |
|
| 143 | - } |
|
| 54 | + /** @var \OCP\IConfig */ |
|
| 55 | + protected $ocConfig; |
|
| 56 | + |
|
| 57 | + /** @var INotificationManager */ |
|
| 58 | + protected $notificationManager; |
|
| 59 | + |
|
| 60 | + /** @var string */ |
|
| 61 | + protected $currentUserInDeletionProcess; |
|
| 62 | + |
|
| 63 | + /** @var UserPluginManager */ |
|
| 64 | + protected $userPluginManager; |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * @param Access $access |
|
| 68 | + * @param \OCP\IConfig $ocConfig |
|
| 69 | + * @param \OCP\Notification\IManager $notificationManager |
|
| 70 | + * @param IUserSession $userSession |
|
| 71 | + */ |
|
| 72 | + public function __construct(Access $access, IConfig $ocConfig, INotificationManager $notificationManager, IUserSession $userSession, UserPluginManager $userPluginManager) { |
|
| 73 | + parent::__construct($access); |
|
| 74 | + $this->ocConfig = $ocConfig; |
|
| 75 | + $this->notificationManager = $notificationManager; |
|
| 76 | + $this->userPluginManager = $userPluginManager; |
|
| 77 | + $this->registerHooks($userSession); |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + protected function registerHooks(IUserSession $userSession) { |
|
| 81 | + $userSession->listen('\OC\User', 'preDelete', [$this, 'preDeleteUser']); |
|
| 82 | + $userSession->listen('\OC\User', 'postDelete', [$this, 'postDeleteUser']); |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + public function preDeleteUser(IUser $user) { |
|
| 86 | + $this->currentUserInDeletionProcess = $user->getUID(); |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + public function postDeleteUser() { |
|
| 90 | + $this->currentUserInDeletionProcess = null; |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * checks whether the user is allowed to change his avatar in Nextcloud |
|
| 95 | + * @param string $uid the Nextcloud user name |
|
| 96 | + * @return boolean either the user can or cannot |
|
| 97 | + */ |
|
| 98 | + public function canChangeAvatar($uid) { |
|
| 99 | + if ($this->userPluginManager->implementsActions(Backend::PROVIDE_AVATAR)) { |
|
| 100 | + return $this->userPluginManager->canChangeAvatar($uid); |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + $user = $this->access->userManager->get($uid); |
|
| 104 | + if(!$user instanceof User) { |
|
| 105 | + return false; |
|
| 106 | + } |
|
| 107 | + if($user->getAvatarImage() === false) { |
|
| 108 | + return true; |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + return false; |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * returns the username for the given login name, if available |
|
| 116 | + * |
|
| 117 | + * @param string $loginName |
|
| 118 | + * @return string|false |
|
| 119 | + */ |
|
| 120 | + public function loginName2UserName($loginName) { |
|
| 121 | + $cacheKey = 'loginName2UserName-'.$loginName; |
|
| 122 | + $username = $this->access->connection->getFromCache($cacheKey); |
|
| 123 | + if(!is_null($username)) { |
|
| 124 | + return $username; |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + try { |
|
| 128 | + $ldapRecord = $this->getLDAPUserByLoginName($loginName); |
|
| 129 | + $user = $this->access->userManager->get($ldapRecord['dn'][0]); |
|
| 130 | + if($user instanceof OfflineUser) { |
|
| 131 | + // this path is not really possible, however get() is documented |
|
| 132 | + // to return User or OfflineUser so we are very defensive here. |
|
| 133 | + $this->access->connection->writeToCache($cacheKey, false); |
|
| 134 | + return false; |
|
| 135 | + } |
|
| 136 | + $username = $user->getUsername(); |
|
| 137 | + $this->access->connection->writeToCache($cacheKey, $username); |
|
| 138 | + return $username; |
|
| 139 | + } catch (NotOnLDAP $e) { |
|
| 140 | + $this->access->connection->writeToCache($cacheKey, false); |
|
| 141 | + return false; |
|
| 142 | + } |
|
| 143 | + } |
|
| 144 | 144 | |
| 145 | - /** |
|
| 146 | - * returns the username for the given LDAP DN, if available |
|
| 147 | - * |
|
| 148 | - * @param string $dn |
|
| 149 | - * @return string|false with the username |
|
| 150 | - */ |
|
| 151 | - public function dn2UserName($dn) { |
|
| 152 | - return $this->access->dn2username($dn); |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - /** |
|
| 156 | - * returns an LDAP record based on a given login name |
|
| 157 | - * |
|
| 158 | - * @param string $loginName |
|
| 159 | - * @return array |
|
| 160 | - * @throws NotOnLDAP |
|
| 161 | - */ |
|
| 162 | - public function getLDAPUserByLoginName($loginName) { |
|
| 163 | - //find out dn of the user name |
|
| 164 | - $attrs = $this->access->userManager->getAttributes(); |
|
| 165 | - $users = $this->access->fetchUsersByLoginName($loginName, $attrs); |
|
| 166 | - if(count($users) < 1) { |
|
| 167 | - throw new NotOnLDAP('No user available for the given login name on ' . |
|
| 168 | - $this->access->connection->ldapHost . ':' . $this->access->connection->ldapPort); |
|
| 169 | - } |
|
| 170 | - return $users[0]; |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - /** |
|
| 174 | - * Check if the password is correct without logging in the user |
|
| 175 | - * |
|
| 176 | - * @param string $uid The username |
|
| 177 | - * @param string $password The password |
|
| 178 | - * @return false|string |
|
| 179 | - */ |
|
| 180 | - public function checkPassword($uid, $password) { |
|
| 181 | - try { |
|
| 182 | - $ldapRecord = $this->getLDAPUserByLoginName($uid); |
|
| 183 | - } catch(NotOnLDAP $e) { |
|
| 184 | - if($this->ocConfig->getSystemValue('loglevel', Util::WARN) === Util::DEBUG) { |
|
| 185 | - \OC::$server->getLogger()->logException($e, ['app' => 'user_ldap']); |
|
| 186 | - } |
|
| 187 | - return false; |
|
| 188 | - } |
|
| 189 | - $dn = $ldapRecord['dn'][0]; |
|
| 190 | - $user = $this->access->userManager->get($dn); |
|
| 191 | - |
|
| 192 | - if(!$user instanceof User) { |
|
| 193 | - Util::writeLog('user_ldap', |
|
| 194 | - 'LDAP Login: Could not get user object for DN ' . $dn . |
|
| 195 | - '. Maybe the LDAP entry has no set display name attribute?', |
|
| 196 | - Util::WARN); |
|
| 197 | - return false; |
|
| 198 | - } |
|
| 199 | - if($user->getUsername() !== false) { |
|
| 200 | - //are the credentials OK? |
|
| 201 | - if(!$this->access->areCredentialsValid($dn, $password)) { |
|
| 202 | - return false; |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - $this->access->cacheUserExists($user->getUsername()); |
|
| 206 | - $user->processAttributes($ldapRecord); |
|
| 207 | - $user->markLogin(); |
|
| 208 | - |
|
| 209 | - return $user->getUsername(); |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - return false; |
|
| 213 | - } |
|
| 214 | - |
|
| 215 | - /** |
|
| 216 | - * Set password |
|
| 217 | - * @param string $uid The username |
|
| 218 | - * @param string $password The new password |
|
| 219 | - * @return bool |
|
| 220 | - */ |
|
| 221 | - public function setPassword($uid, $password) { |
|
| 222 | - if ($this->userPluginManager->implementsActions(Backend::SET_PASSWORD)) { |
|
| 223 | - return $this->userPluginManager->setPassword($uid, $password); |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - $user = $this->access->userManager->get($uid); |
|
| 227 | - |
|
| 228 | - if(!$user instanceof User) { |
|
| 229 | - throw new \Exception('LDAP setPassword: Could not get user object for uid ' . $uid . |
|
| 230 | - '. Maybe the LDAP entry has no set display name attribute?'); |
|
| 231 | - } |
|
| 232 | - if($user->getUsername() !== false && $this->access->setPassword($user->getDN(), $password)) { |
|
| 233 | - $ldapDefaultPPolicyDN = $this->access->connection->ldapDefaultPPolicyDN; |
|
| 234 | - $turnOnPasswordChange = $this->access->connection->turnOnPasswordChange; |
|
| 235 | - if (!empty($ldapDefaultPPolicyDN) && (intval($turnOnPasswordChange) === 1)) { |
|
| 236 | - //remove last password expiry warning if any |
|
| 237 | - $notification = $this->notificationManager->createNotification(); |
|
| 238 | - $notification->setApp('user_ldap') |
|
| 239 | - ->setUser($uid) |
|
| 240 | - ->setObject('pwd_exp_warn', $uid) |
|
| 241 | - ; |
|
| 242 | - $this->notificationManager->markProcessed($notification); |
|
| 243 | - } |
|
| 244 | - return true; |
|
| 245 | - } |
|
| 246 | - |
|
| 247 | - return false; |
|
| 248 | - } |
|
| 249 | - |
|
| 250 | - /** |
|
| 251 | - * Get a list of all users |
|
| 252 | - * |
|
| 253 | - * @param string $search |
|
| 254 | - * @param integer $limit |
|
| 255 | - * @param integer $offset |
|
| 256 | - * @return string[] an array of all uids |
|
| 257 | - */ |
|
| 258 | - public function getUsers($search = '', $limit = 10, $offset = 0) { |
|
| 259 | - $search = $this->access->escapeFilterPart($search, true); |
|
| 260 | - $cachekey = 'getUsers-'.$search.'-'.$limit.'-'.$offset; |
|
| 261 | - |
|
| 262 | - //check if users are cached, if so return |
|
| 263 | - $ldap_users = $this->access->connection->getFromCache($cachekey); |
|
| 264 | - if(!is_null($ldap_users)) { |
|
| 265 | - return $ldap_users; |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - // if we'd pass -1 to LDAP search, we'd end up in a Protocol |
|
| 269 | - // error. With a limit of 0, we get 0 results. So we pass null. |
|
| 270 | - if($limit <= 0) { |
|
| 271 | - $limit = null; |
|
| 272 | - } |
|
| 273 | - $filter = $this->access->combineFilterWithAnd(array( |
|
| 274 | - $this->access->connection->ldapUserFilter, |
|
| 275 | - $this->access->connection->ldapUserDisplayName . '=*', |
|
| 276 | - $this->access->getFilterPartForUserSearch($search) |
|
| 277 | - )); |
|
| 278 | - |
|
| 279 | - Util::writeLog('user_ldap', |
|
| 280 | - 'getUsers: Options: search '.$search.' limit '.$limit.' offset '.$offset.' Filter: '.$filter, |
|
| 281 | - Util::DEBUG); |
|
| 282 | - //do the search and translate results to Nextcloud names |
|
| 283 | - $ldap_users = $this->access->fetchListOfUsers( |
|
| 284 | - $filter, |
|
| 285 | - $this->access->userManager->getAttributes(true), |
|
| 286 | - $limit, $offset); |
|
| 287 | - $ldap_users = $this->access->nextcloudUserNames($ldap_users); |
|
| 288 | - Util::writeLog('user_ldap', 'getUsers: '.count($ldap_users). ' Users found', Util::DEBUG); |
|
| 289 | - |
|
| 290 | - $this->access->connection->writeToCache($cachekey, $ldap_users); |
|
| 291 | - return $ldap_users; |
|
| 292 | - } |
|
| 293 | - |
|
| 294 | - /** |
|
| 295 | - * checks whether a user is still available on LDAP |
|
| 296 | - * |
|
| 297 | - * @param string|\OCA\User_LDAP\User\User $user either the Nextcloud user |
|
| 298 | - * name or an instance of that user |
|
| 299 | - * @return bool |
|
| 300 | - * @throws \Exception |
|
| 301 | - * @throws \OC\ServerNotAvailableException |
|
| 302 | - */ |
|
| 303 | - public function userExistsOnLDAP($user) { |
|
| 304 | - if(is_string($user)) { |
|
| 305 | - $user = $this->access->userManager->get($user); |
|
| 306 | - } |
|
| 307 | - if(is_null($user)) { |
|
| 308 | - return false; |
|
| 309 | - } |
|
| 310 | - |
|
| 311 | - $dn = $user->getDN(); |
|
| 312 | - //check if user really still exists by reading its entry |
|
| 313 | - if(!is_array($this->access->readAttribute($dn, '', $this->access->connection->ldapUserFilter))) { |
|
| 314 | - $lcr = $this->access->connection->getConnectionResource(); |
|
| 315 | - if(is_null($lcr)) { |
|
| 316 | - throw new \Exception('No LDAP Connection to server ' . $this->access->connection->ldapHost); |
|
| 317 | - } |
|
| 318 | - |
|
| 319 | - try { |
|
| 320 | - $uuid = $this->access->getUserMapper()->getUUIDByDN($dn); |
|
| 321 | - if (!$uuid) { |
|
| 322 | - return false; |
|
| 323 | - } |
|
| 324 | - $newDn = $this->access->getUserDnByUuid($uuid); |
|
| 325 | - //check if renamed user is still valid by reapplying the ldap filter |
|
| 326 | - if (!is_array($this->access->readAttribute($newDn, '', $this->access->connection->ldapUserFilter))) { |
|
| 327 | - return false; |
|
| 328 | - } |
|
| 329 | - $this->access->getUserMapper()->setDNbyUUID($newDn, $uuid); |
|
| 330 | - return true; |
|
| 331 | - } catch (ServerNotAvailableException $e) { |
|
| 332 | - throw $e; |
|
| 333 | - } catch (\Exception $e) { |
|
| 334 | - return false; |
|
| 335 | - } |
|
| 336 | - } |
|
| 337 | - |
|
| 338 | - if($user instanceof OfflineUser) { |
|
| 339 | - $user->unmark(); |
|
| 340 | - } |
|
| 341 | - |
|
| 342 | - return true; |
|
| 343 | - } |
|
| 344 | - |
|
| 345 | - /** |
|
| 346 | - * check if a user exists |
|
| 347 | - * @param string $uid the username |
|
| 348 | - * @return boolean |
|
| 349 | - * @throws \Exception when connection could not be established |
|
| 350 | - */ |
|
| 351 | - public function userExists($uid) { |
|
| 352 | - $userExists = $this->access->connection->getFromCache('userExists'.$uid); |
|
| 353 | - if(!is_null($userExists)) { |
|
| 354 | - return (bool)$userExists; |
|
| 355 | - } |
|
| 356 | - //getting dn, if false the user does not exist. If dn, he may be mapped only, requires more checking. |
|
| 357 | - $user = $this->access->userManager->get($uid); |
|
| 358 | - |
|
| 359 | - if(is_null($user)) { |
|
| 360 | - Util::writeLog('user_ldap', 'No DN found for '.$uid.' on '. |
|
| 361 | - $this->access->connection->ldapHost, Util::DEBUG); |
|
| 362 | - $this->access->connection->writeToCache('userExists'.$uid, false); |
|
| 363 | - return false; |
|
| 364 | - } else if($user instanceof OfflineUser) { |
|
| 365 | - //express check for users marked as deleted. Returning true is |
|
| 366 | - //necessary for cleanup |
|
| 367 | - return true; |
|
| 368 | - } |
|
| 369 | - |
|
| 370 | - $result = $this->userExistsOnLDAP($user); |
|
| 371 | - $this->access->connection->writeToCache('userExists'.$uid, $result); |
|
| 372 | - if($result === true) { |
|
| 373 | - $user->update(); |
|
| 374 | - } |
|
| 375 | - return $result; |
|
| 376 | - } |
|
| 377 | - |
|
| 378 | - /** |
|
| 379 | - * returns whether a user was deleted in LDAP |
|
| 380 | - * |
|
| 381 | - * @param string $uid The username of the user to delete |
|
| 382 | - * @return bool |
|
| 383 | - */ |
|
| 384 | - public function deleteUser($uid) { |
|
| 385 | - if ($this->userPluginManager->canDeleteUser()) { |
|
| 386 | - return $this->userPluginManager->deleteUser($uid); |
|
| 387 | - } |
|
| 388 | - |
|
| 389 | - $marked = $this->ocConfig->getUserValue($uid, 'user_ldap', 'isDeleted', 0); |
|
| 390 | - if(intval($marked) === 0) { |
|
| 391 | - \OC::$server->getLogger()->notice( |
|
| 392 | - 'User '.$uid . ' is not marked as deleted, not cleaning up.', |
|
| 393 | - array('app' => 'user_ldap')); |
|
| 394 | - return false; |
|
| 395 | - } |
|
| 396 | - \OC::$server->getLogger()->info('Cleaning up after user ' . $uid, |
|
| 397 | - array('app' => 'user_ldap')); |
|
| 398 | - |
|
| 399 | - $this->access->getUserMapper()->unmap($uid); |
|
| 400 | - $this->access->userManager->invalidate($uid); |
|
| 401 | - return true; |
|
| 402 | - } |
|
| 403 | - |
|
| 404 | - /** |
|
| 405 | - * get the user's home directory |
|
| 406 | - * |
|
| 407 | - * @param string $uid the username |
|
| 408 | - * @return bool|string |
|
| 409 | - * @throws NoUserException |
|
| 410 | - * @throws \Exception |
|
| 411 | - */ |
|
| 412 | - public function getHome($uid) { |
|
| 413 | - // user Exists check required as it is not done in user proxy! |
|
| 414 | - if(!$this->userExists($uid)) { |
|
| 415 | - return false; |
|
| 416 | - } |
|
| 417 | - |
|
| 418 | - if ($this->userPluginManager->implementsActions(Backend::GET_HOME)) { |
|
| 419 | - return $this->userPluginManager->getHome($uid); |
|
| 420 | - } |
|
| 421 | - |
|
| 422 | - $cacheKey = 'getHome'.$uid; |
|
| 423 | - $path = $this->access->connection->getFromCache($cacheKey); |
|
| 424 | - if(!is_null($path)) { |
|
| 425 | - return $path; |
|
| 426 | - } |
|
| 427 | - |
|
| 428 | - // early return path if it is a deleted user |
|
| 429 | - $user = $this->access->userManager->get($uid); |
|
| 430 | - if($user instanceof OfflineUser) { |
|
| 431 | - if($this->currentUserInDeletionProcess !== null |
|
| 432 | - && $this->currentUserInDeletionProcess === $user->getOCName() |
|
| 433 | - ) { |
|
| 434 | - return $user->getHomePath(); |
|
| 435 | - } else { |
|
| 436 | - throw new NoUserException($uid . ' is not a valid user anymore'); |
|
| 437 | - } |
|
| 438 | - } else if ($user === null) { |
|
| 439 | - throw new NoUserException($uid . ' is not a valid user anymore'); |
|
| 440 | - } |
|
| 441 | - |
|
| 442 | - $path = $user->getHomePath(); |
|
| 443 | - $this->access->cacheUserHome($uid, $path); |
|
| 444 | - |
|
| 445 | - return $path; |
|
| 446 | - } |
|
| 447 | - |
|
| 448 | - /** |
|
| 449 | - * get display name of the user |
|
| 450 | - * @param string $uid user ID of the user |
|
| 451 | - * @return string|false display name |
|
| 452 | - */ |
|
| 453 | - public function getDisplayName($uid) { |
|
| 454 | - if ($this->userPluginManager->implementsActions(Backend::GET_DISPLAYNAME)) { |
|
| 455 | - return $this->userPluginManager->getDisplayName($uid); |
|
| 456 | - } |
|
| 457 | - |
|
| 458 | - if(!$this->userExists($uid)) { |
|
| 459 | - return false; |
|
| 460 | - } |
|
| 461 | - |
|
| 462 | - $cacheKey = 'getDisplayName'.$uid; |
|
| 463 | - if(!is_null($displayName = $this->access->connection->getFromCache($cacheKey))) { |
|
| 464 | - return $displayName; |
|
| 465 | - } |
|
| 466 | - |
|
| 467 | - //Check whether the display name is configured to have a 2nd feature |
|
| 468 | - $additionalAttribute = $this->access->connection->ldapUserDisplayName2; |
|
| 469 | - $displayName2 = ''; |
|
| 470 | - if ($additionalAttribute !== '') { |
|
| 471 | - $displayName2 = $this->access->readAttribute( |
|
| 472 | - $this->access->username2dn($uid), |
|
| 473 | - $additionalAttribute); |
|
| 474 | - } |
|
| 475 | - |
|
| 476 | - $displayName = $this->access->readAttribute( |
|
| 477 | - $this->access->username2dn($uid), |
|
| 478 | - $this->access->connection->ldapUserDisplayName); |
|
| 479 | - |
|
| 480 | - if($displayName && (count($displayName) > 0)) { |
|
| 481 | - $displayName = $displayName[0]; |
|
| 482 | - |
|
| 483 | - if (is_array($displayName2)){ |
|
| 484 | - $displayName2 = count($displayName2) > 0 ? $displayName2[0] : ''; |
|
| 485 | - } |
|
| 486 | - |
|
| 487 | - $user = $this->access->userManager->get($uid); |
|
| 488 | - if ($user instanceof User) { |
|
| 489 | - $displayName = $user->composeAndStoreDisplayName($displayName, $displayName2); |
|
| 490 | - $this->access->connection->writeToCache($cacheKey, $displayName); |
|
| 491 | - } |
|
| 492 | - if ($user instanceof OfflineUser) { |
|
| 493 | - /** @var OfflineUser $user*/ |
|
| 494 | - $displayName = $user->getDisplayName(); |
|
| 495 | - } |
|
| 496 | - return $displayName; |
|
| 497 | - } |
|
| 498 | - |
|
| 499 | - return null; |
|
| 500 | - } |
|
| 501 | - |
|
| 502 | - /** |
|
| 503 | - * set display name of the user |
|
| 504 | - * @param string $uid user ID of the user |
|
| 505 | - * @param string $displayName new display name of the user |
|
| 506 | - * @return string|false display name |
|
| 507 | - */ |
|
| 508 | - public function setDisplayName($uid, $displayName) { |
|
| 509 | - if ($this->userPluginManager->implementsActions(Backend::SET_DISPLAYNAME)) { |
|
| 510 | - return $this->userPluginManager->setDisplayName($uid, $displayName); |
|
| 511 | - } |
|
| 512 | - return false; |
|
| 513 | - } |
|
| 514 | - |
|
| 515 | - /** |
|
| 516 | - * Get a list of all display names |
|
| 517 | - * |
|
| 518 | - * @param string $search |
|
| 519 | - * @param string|null $limit |
|
| 520 | - * @param string|null $offset |
|
| 521 | - * @return array an array of all displayNames (value) and the corresponding uids (key) |
|
| 522 | - */ |
|
| 523 | - public function getDisplayNames($search = '', $limit = null, $offset = null) { |
|
| 524 | - $cacheKey = 'getDisplayNames-'.$search.'-'.$limit.'-'.$offset; |
|
| 525 | - if(!is_null($displayNames = $this->access->connection->getFromCache($cacheKey))) { |
|
| 526 | - return $displayNames; |
|
| 527 | - } |
|
| 528 | - |
|
| 529 | - $displayNames = array(); |
|
| 530 | - $users = $this->getUsers($search, $limit, $offset); |
|
| 531 | - foreach ($users as $user) { |
|
| 532 | - $displayNames[$user] = $this->getDisplayName($user); |
|
| 533 | - } |
|
| 534 | - $this->access->connection->writeToCache($cacheKey, $displayNames); |
|
| 535 | - return $displayNames; |
|
| 536 | - } |
|
| 537 | - |
|
| 538 | - /** |
|
| 539 | - * Check if backend implements actions |
|
| 540 | - * @param int $actions bitwise-or'ed actions |
|
| 541 | - * @return boolean |
|
| 542 | - * |
|
| 543 | - * Returns the supported actions as int to be |
|
| 544 | - * compared with \OC\User\Backend::CREATE_USER etc. |
|
| 545 | - */ |
|
| 546 | - public function implementsActions($actions) { |
|
| 547 | - return (bool)((Backend::CHECK_PASSWORD |
|
| 548 | - | Backend::GET_HOME |
|
| 549 | - | Backend::GET_DISPLAYNAME |
|
| 550 | - | Backend::PROVIDE_AVATAR |
|
| 551 | - | Backend::COUNT_USERS |
|
| 552 | - | ((intval($this->access->connection->turnOnPasswordChange) === 1)?(Backend::SET_PASSWORD):0) |
|
| 553 | - | $this->userPluginManager->getImplementedActions()) |
|
| 554 | - & $actions); |
|
| 555 | - } |
|
| 556 | - |
|
| 557 | - /** |
|
| 558 | - * @return bool |
|
| 559 | - */ |
|
| 560 | - public function hasUserListings() { |
|
| 561 | - return true; |
|
| 562 | - } |
|
| 563 | - |
|
| 564 | - /** |
|
| 565 | - * counts the users in LDAP |
|
| 566 | - * |
|
| 567 | - * @return int|bool |
|
| 568 | - */ |
|
| 569 | - public function countUsers() { |
|
| 570 | - if ($this->userPluginManager->implementsActions(Backend::COUNT_USERS)) { |
|
| 571 | - return $this->userPluginManager->countUsers(); |
|
| 572 | - } |
|
| 573 | - |
|
| 574 | - $filter = $this->access->getFilterForUserCount(); |
|
| 575 | - $cacheKey = 'countUsers-'.$filter; |
|
| 576 | - if(!is_null($entries = $this->access->connection->getFromCache($cacheKey))) { |
|
| 577 | - return $entries; |
|
| 578 | - } |
|
| 579 | - $entries = $this->access->countUsers($filter); |
|
| 580 | - $this->access->connection->writeToCache($cacheKey, $entries); |
|
| 581 | - return $entries; |
|
| 582 | - } |
|
| 583 | - |
|
| 584 | - /** |
|
| 585 | - * Backend name to be shown in user management |
|
| 586 | - * @return string the name of the backend to be shown |
|
| 587 | - */ |
|
| 588 | - public function getBackendName(){ |
|
| 589 | - return 'LDAP'; |
|
| 590 | - } |
|
| 145 | + /** |
|
| 146 | + * returns the username for the given LDAP DN, if available |
|
| 147 | + * |
|
| 148 | + * @param string $dn |
|
| 149 | + * @return string|false with the username |
|
| 150 | + */ |
|
| 151 | + public function dn2UserName($dn) { |
|
| 152 | + return $this->access->dn2username($dn); |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + /** |
|
| 156 | + * returns an LDAP record based on a given login name |
|
| 157 | + * |
|
| 158 | + * @param string $loginName |
|
| 159 | + * @return array |
|
| 160 | + * @throws NotOnLDAP |
|
| 161 | + */ |
|
| 162 | + public function getLDAPUserByLoginName($loginName) { |
|
| 163 | + //find out dn of the user name |
|
| 164 | + $attrs = $this->access->userManager->getAttributes(); |
|
| 165 | + $users = $this->access->fetchUsersByLoginName($loginName, $attrs); |
|
| 166 | + if(count($users) < 1) { |
|
| 167 | + throw new NotOnLDAP('No user available for the given login name on ' . |
|
| 168 | + $this->access->connection->ldapHost . ':' . $this->access->connection->ldapPort); |
|
| 169 | + } |
|
| 170 | + return $users[0]; |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + /** |
|
| 174 | + * Check if the password is correct without logging in the user |
|
| 175 | + * |
|
| 176 | + * @param string $uid The username |
|
| 177 | + * @param string $password The password |
|
| 178 | + * @return false|string |
|
| 179 | + */ |
|
| 180 | + public function checkPassword($uid, $password) { |
|
| 181 | + try { |
|
| 182 | + $ldapRecord = $this->getLDAPUserByLoginName($uid); |
|
| 183 | + } catch(NotOnLDAP $e) { |
|
| 184 | + if($this->ocConfig->getSystemValue('loglevel', Util::WARN) === Util::DEBUG) { |
|
| 185 | + \OC::$server->getLogger()->logException($e, ['app' => 'user_ldap']); |
|
| 186 | + } |
|
| 187 | + return false; |
|
| 188 | + } |
|
| 189 | + $dn = $ldapRecord['dn'][0]; |
|
| 190 | + $user = $this->access->userManager->get($dn); |
|
| 191 | + |
|
| 192 | + if(!$user instanceof User) { |
|
| 193 | + Util::writeLog('user_ldap', |
|
| 194 | + 'LDAP Login: Could not get user object for DN ' . $dn . |
|
| 195 | + '. Maybe the LDAP entry has no set display name attribute?', |
|
| 196 | + Util::WARN); |
|
| 197 | + return false; |
|
| 198 | + } |
|
| 199 | + if($user->getUsername() !== false) { |
|
| 200 | + //are the credentials OK? |
|
| 201 | + if(!$this->access->areCredentialsValid($dn, $password)) { |
|
| 202 | + return false; |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + $this->access->cacheUserExists($user->getUsername()); |
|
| 206 | + $user->processAttributes($ldapRecord); |
|
| 207 | + $user->markLogin(); |
|
| 208 | + |
|
| 209 | + return $user->getUsername(); |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + return false; |
|
| 213 | + } |
|
| 214 | + |
|
| 215 | + /** |
|
| 216 | + * Set password |
|
| 217 | + * @param string $uid The username |
|
| 218 | + * @param string $password The new password |
|
| 219 | + * @return bool |
|
| 220 | + */ |
|
| 221 | + public function setPassword($uid, $password) { |
|
| 222 | + if ($this->userPluginManager->implementsActions(Backend::SET_PASSWORD)) { |
|
| 223 | + return $this->userPluginManager->setPassword($uid, $password); |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + $user = $this->access->userManager->get($uid); |
|
| 227 | + |
|
| 228 | + if(!$user instanceof User) { |
|
| 229 | + throw new \Exception('LDAP setPassword: Could not get user object for uid ' . $uid . |
|
| 230 | + '. Maybe the LDAP entry has no set display name attribute?'); |
|
| 231 | + } |
|
| 232 | + if($user->getUsername() !== false && $this->access->setPassword($user->getDN(), $password)) { |
|
| 233 | + $ldapDefaultPPolicyDN = $this->access->connection->ldapDefaultPPolicyDN; |
|
| 234 | + $turnOnPasswordChange = $this->access->connection->turnOnPasswordChange; |
|
| 235 | + if (!empty($ldapDefaultPPolicyDN) && (intval($turnOnPasswordChange) === 1)) { |
|
| 236 | + //remove last password expiry warning if any |
|
| 237 | + $notification = $this->notificationManager->createNotification(); |
|
| 238 | + $notification->setApp('user_ldap') |
|
| 239 | + ->setUser($uid) |
|
| 240 | + ->setObject('pwd_exp_warn', $uid) |
|
| 241 | + ; |
|
| 242 | + $this->notificationManager->markProcessed($notification); |
|
| 243 | + } |
|
| 244 | + return true; |
|
| 245 | + } |
|
| 246 | + |
|
| 247 | + return false; |
|
| 248 | + } |
|
| 249 | + |
|
| 250 | + /** |
|
| 251 | + * Get a list of all users |
|
| 252 | + * |
|
| 253 | + * @param string $search |
|
| 254 | + * @param integer $limit |
|
| 255 | + * @param integer $offset |
|
| 256 | + * @return string[] an array of all uids |
|
| 257 | + */ |
|
| 258 | + public function getUsers($search = '', $limit = 10, $offset = 0) { |
|
| 259 | + $search = $this->access->escapeFilterPart($search, true); |
|
| 260 | + $cachekey = 'getUsers-'.$search.'-'.$limit.'-'.$offset; |
|
| 261 | + |
|
| 262 | + //check if users are cached, if so return |
|
| 263 | + $ldap_users = $this->access->connection->getFromCache($cachekey); |
|
| 264 | + if(!is_null($ldap_users)) { |
|
| 265 | + return $ldap_users; |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + // if we'd pass -1 to LDAP search, we'd end up in a Protocol |
|
| 269 | + // error. With a limit of 0, we get 0 results. So we pass null. |
|
| 270 | + if($limit <= 0) { |
|
| 271 | + $limit = null; |
|
| 272 | + } |
|
| 273 | + $filter = $this->access->combineFilterWithAnd(array( |
|
| 274 | + $this->access->connection->ldapUserFilter, |
|
| 275 | + $this->access->connection->ldapUserDisplayName . '=*', |
|
| 276 | + $this->access->getFilterPartForUserSearch($search) |
|
| 277 | + )); |
|
| 278 | + |
|
| 279 | + Util::writeLog('user_ldap', |
|
| 280 | + 'getUsers: Options: search '.$search.' limit '.$limit.' offset '.$offset.' Filter: '.$filter, |
|
| 281 | + Util::DEBUG); |
|
| 282 | + //do the search and translate results to Nextcloud names |
|
| 283 | + $ldap_users = $this->access->fetchListOfUsers( |
|
| 284 | + $filter, |
|
| 285 | + $this->access->userManager->getAttributes(true), |
|
| 286 | + $limit, $offset); |
|
| 287 | + $ldap_users = $this->access->nextcloudUserNames($ldap_users); |
|
| 288 | + Util::writeLog('user_ldap', 'getUsers: '.count($ldap_users). ' Users found', Util::DEBUG); |
|
| 289 | + |
|
| 290 | + $this->access->connection->writeToCache($cachekey, $ldap_users); |
|
| 291 | + return $ldap_users; |
|
| 292 | + } |
|
| 293 | + |
|
| 294 | + /** |
|
| 295 | + * checks whether a user is still available on LDAP |
|
| 296 | + * |
|
| 297 | + * @param string|\OCA\User_LDAP\User\User $user either the Nextcloud user |
|
| 298 | + * name or an instance of that user |
|
| 299 | + * @return bool |
|
| 300 | + * @throws \Exception |
|
| 301 | + * @throws \OC\ServerNotAvailableException |
|
| 302 | + */ |
|
| 303 | + public function userExistsOnLDAP($user) { |
|
| 304 | + if(is_string($user)) { |
|
| 305 | + $user = $this->access->userManager->get($user); |
|
| 306 | + } |
|
| 307 | + if(is_null($user)) { |
|
| 308 | + return false; |
|
| 309 | + } |
|
| 310 | + |
|
| 311 | + $dn = $user->getDN(); |
|
| 312 | + //check if user really still exists by reading its entry |
|
| 313 | + if(!is_array($this->access->readAttribute($dn, '', $this->access->connection->ldapUserFilter))) { |
|
| 314 | + $lcr = $this->access->connection->getConnectionResource(); |
|
| 315 | + if(is_null($lcr)) { |
|
| 316 | + throw new \Exception('No LDAP Connection to server ' . $this->access->connection->ldapHost); |
|
| 317 | + } |
|
| 318 | + |
|
| 319 | + try { |
|
| 320 | + $uuid = $this->access->getUserMapper()->getUUIDByDN($dn); |
|
| 321 | + if (!$uuid) { |
|
| 322 | + return false; |
|
| 323 | + } |
|
| 324 | + $newDn = $this->access->getUserDnByUuid($uuid); |
|
| 325 | + //check if renamed user is still valid by reapplying the ldap filter |
|
| 326 | + if (!is_array($this->access->readAttribute($newDn, '', $this->access->connection->ldapUserFilter))) { |
|
| 327 | + return false; |
|
| 328 | + } |
|
| 329 | + $this->access->getUserMapper()->setDNbyUUID($newDn, $uuid); |
|
| 330 | + return true; |
|
| 331 | + } catch (ServerNotAvailableException $e) { |
|
| 332 | + throw $e; |
|
| 333 | + } catch (\Exception $e) { |
|
| 334 | + return false; |
|
| 335 | + } |
|
| 336 | + } |
|
| 337 | + |
|
| 338 | + if($user instanceof OfflineUser) { |
|
| 339 | + $user->unmark(); |
|
| 340 | + } |
|
| 341 | + |
|
| 342 | + return true; |
|
| 343 | + } |
|
| 344 | + |
|
| 345 | + /** |
|
| 346 | + * check if a user exists |
|
| 347 | + * @param string $uid the username |
|
| 348 | + * @return boolean |
|
| 349 | + * @throws \Exception when connection could not be established |
|
| 350 | + */ |
|
| 351 | + public function userExists($uid) { |
|
| 352 | + $userExists = $this->access->connection->getFromCache('userExists'.$uid); |
|
| 353 | + if(!is_null($userExists)) { |
|
| 354 | + return (bool)$userExists; |
|
| 355 | + } |
|
| 356 | + //getting dn, if false the user does not exist. If dn, he may be mapped only, requires more checking. |
|
| 357 | + $user = $this->access->userManager->get($uid); |
|
| 358 | + |
|
| 359 | + if(is_null($user)) { |
|
| 360 | + Util::writeLog('user_ldap', 'No DN found for '.$uid.' on '. |
|
| 361 | + $this->access->connection->ldapHost, Util::DEBUG); |
|
| 362 | + $this->access->connection->writeToCache('userExists'.$uid, false); |
|
| 363 | + return false; |
|
| 364 | + } else if($user instanceof OfflineUser) { |
|
| 365 | + //express check for users marked as deleted. Returning true is |
|
| 366 | + //necessary for cleanup |
|
| 367 | + return true; |
|
| 368 | + } |
|
| 369 | + |
|
| 370 | + $result = $this->userExistsOnLDAP($user); |
|
| 371 | + $this->access->connection->writeToCache('userExists'.$uid, $result); |
|
| 372 | + if($result === true) { |
|
| 373 | + $user->update(); |
|
| 374 | + } |
|
| 375 | + return $result; |
|
| 376 | + } |
|
| 377 | + |
|
| 378 | + /** |
|
| 379 | + * returns whether a user was deleted in LDAP |
|
| 380 | + * |
|
| 381 | + * @param string $uid The username of the user to delete |
|
| 382 | + * @return bool |
|
| 383 | + */ |
|
| 384 | + public function deleteUser($uid) { |
|
| 385 | + if ($this->userPluginManager->canDeleteUser()) { |
|
| 386 | + return $this->userPluginManager->deleteUser($uid); |
|
| 387 | + } |
|
| 388 | + |
|
| 389 | + $marked = $this->ocConfig->getUserValue($uid, 'user_ldap', 'isDeleted', 0); |
|
| 390 | + if(intval($marked) === 0) { |
|
| 391 | + \OC::$server->getLogger()->notice( |
|
| 392 | + 'User '.$uid . ' is not marked as deleted, not cleaning up.', |
|
| 393 | + array('app' => 'user_ldap')); |
|
| 394 | + return false; |
|
| 395 | + } |
|
| 396 | + \OC::$server->getLogger()->info('Cleaning up after user ' . $uid, |
|
| 397 | + array('app' => 'user_ldap')); |
|
| 398 | + |
|
| 399 | + $this->access->getUserMapper()->unmap($uid); |
|
| 400 | + $this->access->userManager->invalidate($uid); |
|
| 401 | + return true; |
|
| 402 | + } |
|
| 403 | + |
|
| 404 | + /** |
|
| 405 | + * get the user's home directory |
|
| 406 | + * |
|
| 407 | + * @param string $uid the username |
|
| 408 | + * @return bool|string |
|
| 409 | + * @throws NoUserException |
|
| 410 | + * @throws \Exception |
|
| 411 | + */ |
|
| 412 | + public function getHome($uid) { |
|
| 413 | + // user Exists check required as it is not done in user proxy! |
|
| 414 | + if(!$this->userExists($uid)) { |
|
| 415 | + return false; |
|
| 416 | + } |
|
| 417 | + |
|
| 418 | + if ($this->userPluginManager->implementsActions(Backend::GET_HOME)) { |
|
| 419 | + return $this->userPluginManager->getHome($uid); |
|
| 420 | + } |
|
| 421 | + |
|
| 422 | + $cacheKey = 'getHome'.$uid; |
|
| 423 | + $path = $this->access->connection->getFromCache($cacheKey); |
|
| 424 | + if(!is_null($path)) { |
|
| 425 | + return $path; |
|
| 426 | + } |
|
| 427 | + |
|
| 428 | + // early return path if it is a deleted user |
|
| 429 | + $user = $this->access->userManager->get($uid); |
|
| 430 | + if($user instanceof OfflineUser) { |
|
| 431 | + if($this->currentUserInDeletionProcess !== null |
|
| 432 | + && $this->currentUserInDeletionProcess === $user->getOCName() |
|
| 433 | + ) { |
|
| 434 | + return $user->getHomePath(); |
|
| 435 | + } else { |
|
| 436 | + throw new NoUserException($uid . ' is not a valid user anymore'); |
|
| 437 | + } |
|
| 438 | + } else if ($user === null) { |
|
| 439 | + throw new NoUserException($uid . ' is not a valid user anymore'); |
|
| 440 | + } |
|
| 441 | + |
|
| 442 | + $path = $user->getHomePath(); |
|
| 443 | + $this->access->cacheUserHome($uid, $path); |
|
| 444 | + |
|
| 445 | + return $path; |
|
| 446 | + } |
|
| 447 | + |
|
| 448 | + /** |
|
| 449 | + * get display name of the user |
|
| 450 | + * @param string $uid user ID of the user |
|
| 451 | + * @return string|false display name |
|
| 452 | + */ |
|
| 453 | + public function getDisplayName($uid) { |
|
| 454 | + if ($this->userPluginManager->implementsActions(Backend::GET_DISPLAYNAME)) { |
|
| 455 | + return $this->userPluginManager->getDisplayName($uid); |
|
| 456 | + } |
|
| 457 | + |
|
| 458 | + if(!$this->userExists($uid)) { |
|
| 459 | + return false; |
|
| 460 | + } |
|
| 461 | + |
|
| 462 | + $cacheKey = 'getDisplayName'.$uid; |
|
| 463 | + if(!is_null($displayName = $this->access->connection->getFromCache($cacheKey))) { |
|
| 464 | + return $displayName; |
|
| 465 | + } |
|
| 466 | + |
|
| 467 | + //Check whether the display name is configured to have a 2nd feature |
|
| 468 | + $additionalAttribute = $this->access->connection->ldapUserDisplayName2; |
|
| 469 | + $displayName2 = ''; |
|
| 470 | + if ($additionalAttribute !== '') { |
|
| 471 | + $displayName2 = $this->access->readAttribute( |
|
| 472 | + $this->access->username2dn($uid), |
|
| 473 | + $additionalAttribute); |
|
| 474 | + } |
|
| 475 | + |
|
| 476 | + $displayName = $this->access->readAttribute( |
|
| 477 | + $this->access->username2dn($uid), |
|
| 478 | + $this->access->connection->ldapUserDisplayName); |
|
| 479 | + |
|
| 480 | + if($displayName && (count($displayName) > 0)) { |
|
| 481 | + $displayName = $displayName[0]; |
|
| 482 | + |
|
| 483 | + if (is_array($displayName2)){ |
|
| 484 | + $displayName2 = count($displayName2) > 0 ? $displayName2[0] : ''; |
|
| 485 | + } |
|
| 486 | + |
|
| 487 | + $user = $this->access->userManager->get($uid); |
|
| 488 | + if ($user instanceof User) { |
|
| 489 | + $displayName = $user->composeAndStoreDisplayName($displayName, $displayName2); |
|
| 490 | + $this->access->connection->writeToCache($cacheKey, $displayName); |
|
| 491 | + } |
|
| 492 | + if ($user instanceof OfflineUser) { |
|
| 493 | + /** @var OfflineUser $user*/ |
|
| 494 | + $displayName = $user->getDisplayName(); |
|
| 495 | + } |
|
| 496 | + return $displayName; |
|
| 497 | + } |
|
| 498 | + |
|
| 499 | + return null; |
|
| 500 | + } |
|
| 501 | + |
|
| 502 | + /** |
|
| 503 | + * set display name of the user |
|
| 504 | + * @param string $uid user ID of the user |
|
| 505 | + * @param string $displayName new display name of the user |
|
| 506 | + * @return string|false display name |
|
| 507 | + */ |
|
| 508 | + public function setDisplayName($uid, $displayName) { |
|
| 509 | + if ($this->userPluginManager->implementsActions(Backend::SET_DISPLAYNAME)) { |
|
| 510 | + return $this->userPluginManager->setDisplayName($uid, $displayName); |
|
| 511 | + } |
|
| 512 | + return false; |
|
| 513 | + } |
|
| 514 | + |
|
| 515 | + /** |
|
| 516 | + * Get a list of all display names |
|
| 517 | + * |
|
| 518 | + * @param string $search |
|
| 519 | + * @param string|null $limit |
|
| 520 | + * @param string|null $offset |
|
| 521 | + * @return array an array of all displayNames (value) and the corresponding uids (key) |
|
| 522 | + */ |
|
| 523 | + public function getDisplayNames($search = '', $limit = null, $offset = null) { |
|
| 524 | + $cacheKey = 'getDisplayNames-'.$search.'-'.$limit.'-'.$offset; |
|
| 525 | + if(!is_null($displayNames = $this->access->connection->getFromCache($cacheKey))) { |
|
| 526 | + return $displayNames; |
|
| 527 | + } |
|
| 528 | + |
|
| 529 | + $displayNames = array(); |
|
| 530 | + $users = $this->getUsers($search, $limit, $offset); |
|
| 531 | + foreach ($users as $user) { |
|
| 532 | + $displayNames[$user] = $this->getDisplayName($user); |
|
| 533 | + } |
|
| 534 | + $this->access->connection->writeToCache($cacheKey, $displayNames); |
|
| 535 | + return $displayNames; |
|
| 536 | + } |
|
| 537 | + |
|
| 538 | + /** |
|
| 539 | + * Check if backend implements actions |
|
| 540 | + * @param int $actions bitwise-or'ed actions |
|
| 541 | + * @return boolean |
|
| 542 | + * |
|
| 543 | + * Returns the supported actions as int to be |
|
| 544 | + * compared with \OC\User\Backend::CREATE_USER etc. |
|
| 545 | + */ |
|
| 546 | + public function implementsActions($actions) { |
|
| 547 | + return (bool)((Backend::CHECK_PASSWORD |
|
| 548 | + | Backend::GET_HOME |
|
| 549 | + | Backend::GET_DISPLAYNAME |
|
| 550 | + | Backend::PROVIDE_AVATAR |
|
| 551 | + | Backend::COUNT_USERS |
|
| 552 | + | ((intval($this->access->connection->turnOnPasswordChange) === 1)?(Backend::SET_PASSWORD):0) |
|
| 553 | + | $this->userPluginManager->getImplementedActions()) |
|
| 554 | + & $actions); |
|
| 555 | + } |
|
| 556 | + |
|
| 557 | + /** |
|
| 558 | + * @return bool |
|
| 559 | + */ |
|
| 560 | + public function hasUserListings() { |
|
| 561 | + return true; |
|
| 562 | + } |
|
| 563 | + |
|
| 564 | + /** |
|
| 565 | + * counts the users in LDAP |
|
| 566 | + * |
|
| 567 | + * @return int|bool |
|
| 568 | + */ |
|
| 569 | + public function countUsers() { |
|
| 570 | + if ($this->userPluginManager->implementsActions(Backend::COUNT_USERS)) { |
|
| 571 | + return $this->userPluginManager->countUsers(); |
|
| 572 | + } |
|
| 573 | + |
|
| 574 | + $filter = $this->access->getFilterForUserCount(); |
|
| 575 | + $cacheKey = 'countUsers-'.$filter; |
|
| 576 | + if(!is_null($entries = $this->access->connection->getFromCache($cacheKey))) { |
|
| 577 | + return $entries; |
|
| 578 | + } |
|
| 579 | + $entries = $this->access->countUsers($filter); |
|
| 580 | + $this->access->connection->writeToCache($cacheKey, $entries); |
|
| 581 | + return $entries; |
|
| 582 | + } |
|
| 583 | + |
|
| 584 | + /** |
|
| 585 | + * Backend name to be shown in user management |
|
| 586 | + * @return string the name of the backend to be shown |
|
| 587 | + */ |
|
| 588 | + public function getBackendName(){ |
|
| 589 | + return 'LDAP'; |
|
| 590 | + } |
|
| 591 | 591 | |
| 592 | - /** |
|
| 593 | - * Return access for LDAP interaction. |
|
| 594 | - * @param string $uid |
|
| 595 | - * @return Access instance of Access for LDAP interaction |
|
| 596 | - */ |
|
| 597 | - public function getLDAPAccess($uid) { |
|
| 598 | - return $this->access; |
|
| 599 | - } |
|
| 592 | + /** |
|
| 593 | + * Return access for LDAP interaction. |
|
| 594 | + * @param string $uid |
|
| 595 | + * @return Access instance of Access for LDAP interaction |
|
| 596 | + */ |
|
| 597 | + public function getLDAPAccess($uid) { |
|
| 598 | + return $this->access; |
|
| 599 | + } |
|
| 600 | 600 | |
| 601 | - /** |
|
| 602 | - * Return LDAP connection resource from a cloned connection. |
|
| 603 | - * The cloned connection needs to be closed manually. |
|
| 604 | - * of the current access. |
|
| 605 | - * @param string $uid |
|
| 606 | - * @return resource of the LDAP connection |
|
| 607 | - */ |
|
| 608 | - public function getNewLDAPConnection($uid) { |
|
| 609 | - $connection = clone $this->access->getConnection(); |
|
| 610 | - return $connection->getConnectionResource(); |
|
| 611 | - } |
|
| 612 | - |
|
| 613 | - /** |
|
| 614 | - * create new user |
|
| 615 | - * @param string $username username of the new user |
|
| 616 | - * @param string $password password of the new user |
|
| 617 | - * @return bool was the user created? |
|
| 618 | - */ |
|
| 619 | - public function createUser($username, $password) { |
|
| 620 | - if ($this->userPluginManager->implementsActions(Backend::CREATE_USER)) { |
|
| 621 | - return $this->userPluginManager->createUser($username, $password); |
|
| 622 | - } |
|
| 623 | - return false; |
|
| 624 | - } |
|
| 601 | + /** |
|
| 602 | + * Return LDAP connection resource from a cloned connection. |
|
| 603 | + * The cloned connection needs to be closed manually. |
|
| 604 | + * of the current access. |
|
| 605 | + * @param string $uid |
|
| 606 | + * @return resource of the LDAP connection |
|
| 607 | + */ |
|
| 608 | + public function getNewLDAPConnection($uid) { |
|
| 609 | + $connection = clone $this->access->getConnection(); |
|
| 610 | + return $connection->getConnectionResource(); |
|
| 611 | + } |
|
| 612 | + |
|
| 613 | + /** |
|
| 614 | + * create new user |
|
| 615 | + * @param string $username username of the new user |
|
| 616 | + * @param string $password password of the new user |
|
| 617 | + * @return bool was the user created? |
|
| 618 | + */ |
|
| 619 | + public function createUser($username, $password) { |
|
| 620 | + if ($this->userPluginManager->implementsActions(Backend::CREATE_USER)) { |
|
| 621 | + return $this->userPluginManager->createUser($username, $password); |
|
| 622 | + } |
|
| 623 | + return false; |
|
| 624 | + } |
|
| 625 | 625 | |
| 626 | 626 | } |
@@ -101,10 +101,10 @@ discard block |
||
| 101 | 101 | } |
| 102 | 102 | |
| 103 | 103 | $user = $this->access->userManager->get($uid); |
| 104 | - if(!$user instanceof User) { |
|
| 104 | + if (!$user instanceof User) { |
|
| 105 | 105 | return false; |
| 106 | 106 | } |
| 107 | - if($user->getAvatarImage() === false) { |
|
| 107 | + if ($user->getAvatarImage() === false) { |
|
| 108 | 108 | return true; |
| 109 | 109 | } |
| 110 | 110 | |
@@ -120,14 +120,14 @@ discard block |
||
| 120 | 120 | public function loginName2UserName($loginName) { |
| 121 | 121 | $cacheKey = 'loginName2UserName-'.$loginName; |
| 122 | 122 | $username = $this->access->connection->getFromCache($cacheKey); |
| 123 | - if(!is_null($username)) { |
|
| 123 | + if (!is_null($username)) { |
|
| 124 | 124 | return $username; |
| 125 | 125 | } |
| 126 | 126 | |
| 127 | 127 | try { |
| 128 | 128 | $ldapRecord = $this->getLDAPUserByLoginName($loginName); |
| 129 | 129 | $user = $this->access->userManager->get($ldapRecord['dn'][0]); |
| 130 | - if($user instanceof OfflineUser) { |
|
| 130 | + if ($user instanceof OfflineUser) { |
|
| 131 | 131 | // this path is not really possible, however get() is documented |
| 132 | 132 | // to return User or OfflineUser so we are very defensive here. |
| 133 | 133 | $this->access->connection->writeToCache($cacheKey, false); |
@@ -163,9 +163,9 @@ discard block |
||
| 163 | 163 | //find out dn of the user name |
| 164 | 164 | $attrs = $this->access->userManager->getAttributes(); |
| 165 | 165 | $users = $this->access->fetchUsersByLoginName($loginName, $attrs); |
| 166 | - if(count($users) < 1) { |
|
| 167 | - throw new NotOnLDAP('No user available for the given login name on ' . |
|
| 168 | - $this->access->connection->ldapHost . ':' . $this->access->connection->ldapPort); |
|
| 166 | + if (count($users) < 1) { |
|
| 167 | + throw new NotOnLDAP('No user available for the given login name on '. |
|
| 168 | + $this->access->connection->ldapHost.':'.$this->access->connection->ldapPort); |
|
| 169 | 169 | } |
| 170 | 170 | return $users[0]; |
| 171 | 171 | } |
@@ -180,8 +180,8 @@ discard block |
||
| 180 | 180 | public function checkPassword($uid, $password) { |
| 181 | 181 | try { |
| 182 | 182 | $ldapRecord = $this->getLDAPUserByLoginName($uid); |
| 183 | - } catch(NotOnLDAP $e) { |
|
| 184 | - if($this->ocConfig->getSystemValue('loglevel', Util::WARN) === Util::DEBUG) { |
|
| 183 | + } catch (NotOnLDAP $e) { |
|
| 184 | + if ($this->ocConfig->getSystemValue('loglevel', Util::WARN) === Util::DEBUG) { |
|
| 185 | 185 | \OC::$server->getLogger()->logException($e, ['app' => 'user_ldap']); |
| 186 | 186 | } |
| 187 | 187 | return false; |
@@ -189,16 +189,16 @@ discard block |
||
| 189 | 189 | $dn = $ldapRecord['dn'][0]; |
| 190 | 190 | $user = $this->access->userManager->get($dn); |
| 191 | 191 | |
| 192 | - if(!$user instanceof User) { |
|
| 192 | + if (!$user instanceof User) { |
|
| 193 | 193 | Util::writeLog('user_ldap', |
| 194 | - 'LDAP Login: Could not get user object for DN ' . $dn . |
|
| 194 | + 'LDAP Login: Could not get user object for DN '.$dn. |
|
| 195 | 195 | '. Maybe the LDAP entry has no set display name attribute?', |
| 196 | 196 | Util::WARN); |
| 197 | 197 | return false; |
| 198 | 198 | } |
| 199 | - if($user->getUsername() !== false) { |
|
| 199 | + if ($user->getUsername() !== false) { |
|
| 200 | 200 | //are the credentials OK? |
| 201 | - if(!$this->access->areCredentialsValid($dn, $password)) { |
|
| 201 | + if (!$this->access->areCredentialsValid($dn, $password)) { |
|
| 202 | 202 | return false; |
| 203 | 203 | } |
| 204 | 204 | |
@@ -225,11 +225,11 @@ discard block |
||
| 225 | 225 | |
| 226 | 226 | $user = $this->access->userManager->get($uid); |
| 227 | 227 | |
| 228 | - if(!$user instanceof User) { |
|
| 229 | - throw new \Exception('LDAP setPassword: Could not get user object for uid ' . $uid . |
|
| 228 | + if (!$user instanceof User) { |
|
| 229 | + throw new \Exception('LDAP setPassword: Could not get user object for uid '.$uid. |
|
| 230 | 230 | '. Maybe the LDAP entry has no set display name attribute?'); |
| 231 | 231 | } |
| 232 | - if($user->getUsername() !== false && $this->access->setPassword($user->getDN(), $password)) { |
|
| 232 | + if ($user->getUsername() !== false && $this->access->setPassword($user->getDN(), $password)) { |
|
| 233 | 233 | $ldapDefaultPPolicyDN = $this->access->connection->ldapDefaultPPolicyDN; |
| 234 | 234 | $turnOnPasswordChange = $this->access->connection->turnOnPasswordChange; |
| 235 | 235 | if (!empty($ldapDefaultPPolicyDN) && (intval($turnOnPasswordChange) === 1)) { |
@@ -261,18 +261,18 @@ discard block |
||
| 261 | 261 | |
| 262 | 262 | //check if users are cached, if so return |
| 263 | 263 | $ldap_users = $this->access->connection->getFromCache($cachekey); |
| 264 | - if(!is_null($ldap_users)) { |
|
| 264 | + if (!is_null($ldap_users)) { |
|
| 265 | 265 | return $ldap_users; |
| 266 | 266 | } |
| 267 | 267 | |
| 268 | 268 | // if we'd pass -1 to LDAP search, we'd end up in a Protocol |
| 269 | 269 | // error. With a limit of 0, we get 0 results. So we pass null. |
| 270 | - if($limit <= 0) { |
|
| 270 | + if ($limit <= 0) { |
|
| 271 | 271 | $limit = null; |
| 272 | 272 | } |
| 273 | 273 | $filter = $this->access->combineFilterWithAnd(array( |
| 274 | 274 | $this->access->connection->ldapUserFilter, |
| 275 | - $this->access->connection->ldapUserDisplayName . '=*', |
|
| 275 | + $this->access->connection->ldapUserDisplayName.'=*', |
|
| 276 | 276 | $this->access->getFilterPartForUserSearch($search) |
| 277 | 277 | )); |
| 278 | 278 | |
@@ -285,7 +285,7 @@ discard block |
||
| 285 | 285 | $this->access->userManager->getAttributes(true), |
| 286 | 286 | $limit, $offset); |
| 287 | 287 | $ldap_users = $this->access->nextcloudUserNames($ldap_users); |
| 288 | - Util::writeLog('user_ldap', 'getUsers: '.count($ldap_users). ' Users found', Util::DEBUG); |
|
| 288 | + Util::writeLog('user_ldap', 'getUsers: '.count($ldap_users).' Users found', Util::DEBUG); |
|
| 289 | 289 | |
| 290 | 290 | $this->access->connection->writeToCache($cachekey, $ldap_users); |
| 291 | 291 | return $ldap_users; |
@@ -301,19 +301,19 @@ discard block |
||
| 301 | 301 | * @throws \OC\ServerNotAvailableException |
| 302 | 302 | */ |
| 303 | 303 | public function userExistsOnLDAP($user) { |
| 304 | - if(is_string($user)) { |
|
| 304 | + if (is_string($user)) { |
|
| 305 | 305 | $user = $this->access->userManager->get($user); |
| 306 | 306 | } |
| 307 | - if(is_null($user)) { |
|
| 307 | + if (is_null($user)) { |
|
| 308 | 308 | return false; |
| 309 | 309 | } |
| 310 | 310 | |
| 311 | 311 | $dn = $user->getDN(); |
| 312 | 312 | //check if user really still exists by reading its entry |
| 313 | - if(!is_array($this->access->readAttribute($dn, '', $this->access->connection->ldapUserFilter))) { |
|
| 313 | + if (!is_array($this->access->readAttribute($dn, '', $this->access->connection->ldapUserFilter))) { |
|
| 314 | 314 | $lcr = $this->access->connection->getConnectionResource(); |
| 315 | - if(is_null($lcr)) { |
|
| 316 | - throw new \Exception('No LDAP Connection to server ' . $this->access->connection->ldapHost); |
|
| 315 | + if (is_null($lcr)) { |
|
| 316 | + throw new \Exception('No LDAP Connection to server '.$this->access->connection->ldapHost); |
|
| 317 | 317 | } |
| 318 | 318 | |
| 319 | 319 | try { |
@@ -335,7 +335,7 @@ discard block |
||
| 335 | 335 | } |
| 336 | 336 | } |
| 337 | 337 | |
| 338 | - if($user instanceof OfflineUser) { |
|
| 338 | + if ($user instanceof OfflineUser) { |
|
| 339 | 339 | $user->unmark(); |
| 340 | 340 | } |
| 341 | 341 | |
@@ -350,18 +350,18 @@ discard block |
||
| 350 | 350 | */ |
| 351 | 351 | public function userExists($uid) { |
| 352 | 352 | $userExists = $this->access->connection->getFromCache('userExists'.$uid); |
| 353 | - if(!is_null($userExists)) { |
|
| 354 | - return (bool)$userExists; |
|
| 353 | + if (!is_null($userExists)) { |
|
| 354 | + return (bool) $userExists; |
|
| 355 | 355 | } |
| 356 | 356 | //getting dn, if false the user does not exist. If dn, he may be mapped only, requires more checking. |
| 357 | 357 | $user = $this->access->userManager->get($uid); |
| 358 | 358 | |
| 359 | - if(is_null($user)) { |
|
| 359 | + if (is_null($user)) { |
|
| 360 | 360 | Util::writeLog('user_ldap', 'No DN found for '.$uid.' on '. |
| 361 | 361 | $this->access->connection->ldapHost, Util::DEBUG); |
| 362 | 362 | $this->access->connection->writeToCache('userExists'.$uid, false); |
| 363 | 363 | return false; |
| 364 | - } else if($user instanceof OfflineUser) { |
|
| 364 | + } else if ($user instanceof OfflineUser) { |
|
| 365 | 365 | //express check for users marked as deleted. Returning true is |
| 366 | 366 | //necessary for cleanup |
| 367 | 367 | return true; |
@@ -369,7 +369,7 @@ discard block |
||
| 369 | 369 | |
| 370 | 370 | $result = $this->userExistsOnLDAP($user); |
| 371 | 371 | $this->access->connection->writeToCache('userExists'.$uid, $result); |
| 372 | - if($result === true) { |
|
| 372 | + if ($result === true) { |
|
| 373 | 373 | $user->update(); |
| 374 | 374 | } |
| 375 | 375 | return $result; |
@@ -387,13 +387,13 @@ discard block |
||
| 387 | 387 | } |
| 388 | 388 | |
| 389 | 389 | $marked = $this->ocConfig->getUserValue($uid, 'user_ldap', 'isDeleted', 0); |
| 390 | - if(intval($marked) === 0) { |
|
| 390 | + if (intval($marked) === 0) { |
|
| 391 | 391 | \OC::$server->getLogger()->notice( |
| 392 | - 'User '.$uid . ' is not marked as deleted, not cleaning up.', |
|
| 392 | + 'User '.$uid.' is not marked as deleted, not cleaning up.', |
|
| 393 | 393 | array('app' => 'user_ldap')); |
| 394 | 394 | return false; |
| 395 | 395 | } |
| 396 | - \OC::$server->getLogger()->info('Cleaning up after user ' . $uid, |
|
| 396 | + \OC::$server->getLogger()->info('Cleaning up after user '.$uid, |
|
| 397 | 397 | array('app' => 'user_ldap')); |
| 398 | 398 | |
| 399 | 399 | $this->access->getUserMapper()->unmap($uid); |
@@ -411,7 +411,7 @@ discard block |
||
| 411 | 411 | */ |
| 412 | 412 | public function getHome($uid) { |
| 413 | 413 | // user Exists check required as it is not done in user proxy! |
| 414 | - if(!$this->userExists($uid)) { |
|
| 414 | + if (!$this->userExists($uid)) { |
|
| 415 | 415 | return false; |
| 416 | 416 | } |
| 417 | 417 | |
@@ -421,22 +421,22 @@ discard block |
||
| 421 | 421 | |
| 422 | 422 | $cacheKey = 'getHome'.$uid; |
| 423 | 423 | $path = $this->access->connection->getFromCache($cacheKey); |
| 424 | - if(!is_null($path)) { |
|
| 424 | + if (!is_null($path)) { |
|
| 425 | 425 | return $path; |
| 426 | 426 | } |
| 427 | 427 | |
| 428 | 428 | // early return path if it is a deleted user |
| 429 | 429 | $user = $this->access->userManager->get($uid); |
| 430 | - if($user instanceof OfflineUser) { |
|
| 431 | - if($this->currentUserInDeletionProcess !== null |
|
| 430 | + if ($user instanceof OfflineUser) { |
|
| 431 | + if ($this->currentUserInDeletionProcess !== null |
|
| 432 | 432 | && $this->currentUserInDeletionProcess === $user->getOCName() |
| 433 | 433 | ) { |
| 434 | 434 | return $user->getHomePath(); |
| 435 | 435 | } else { |
| 436 | - throw new NoUserException($uid . ' is not a valid user anymore'); |
|
| 436 | + throw new NoUserException($uid.' is not a valid user anymore'); |
|
| 437 | 437 | } |
| 438 | 438 | } else if ($user === null) { |
| 439 | - throw new NoUserException($uid . ' is not a valid user anymore'); |
|
| 439 | + throw new NoUserException($uid.' is not a valid user anymore'); |
|
| 440 | 440 | } |
| 441 | 441 | |
| 442 | 442 | $path = $user->getHomePath(); |
@@ -455,12 +455,12 @@ discard block |
||
| 455 | 455 | return $this->userPluginManager->getDisplayName($uid); |
| 456 | 456 | } |
| 457 | 457 | |
| 458 | - if(!$this->userExists($uid)) { |
|
| 458 | + if (!$this->userExists($uid)) { |
|
| 459 | 459 | return false; |
| 460 | 460 | } |
| 461 | 461 | |
| 462 | 462 | $cacheKey = 'getDisplayName'.$uid; |
| 463 | - if(!is_null($displayName = $this->access->connection->getFromCache($cacheKey))) { |
|
| 463 | + if (!is_null($displayName = $this->access->connection->getFromCache($cacheKey))) { |
|
| 464 | 464 | return $displayName; |
| 465 | 465 | } |
| 466 | 466 | |
@@ -477,10 +477,10 @@ discard block |
||
| 477 | 477 | $this->access->username2dn($uid), |
| 478 | 478 | $this->access->connection->ldapUserDisplayName); |
| 479 | 479 | |
| 480 | - if($displayName && (count($displayName) > 0)) { |
|
| 480 | + if ($displayName && (count($displayName) > 0)) { |
|
| 481 | 481 | $displayName = $displayName[0]; |
| 482 | 482 | |
| 483 | - if (is_array($displayName2)){ |
|
| 483 | + if (is_array($displayName2)) { |
|
| 484 | 484 | $displayName2 = count($displayName2) > 0 ? $displayName2[0] : ''; |
| 485 | 485 | } |
| 486 | 486 | |
@@ -522,7 +522,7 @@ discard block |
||
| 522 | 522 | */ |
| 523 | 523 | public function getDisplayNames($search = '', $limit = null, $offset = null) { |
| 524 | 524 | $cacheKey = 'getDisplayNames-'.$search.'-'.$limit.'-'.$offset; |
| 525 | - if(!is_null($displayNames = $this->access->connection->getFromCache($cacheKey))) { |
|
| 525 | + if (!is_null($displayNames = $this->access->connection->getFromCache($cacheKey))) { |
|
| 526 | 526 | return $displayNames; |
| 527 | 527 | } |
| 528 | 528 | |
@@ -544,12 +544,12 @@ discard block |
||
| 544 | 544 | * compared with \OC\User\Backend::CREATE_USER etc. |
| 545 | 545 | */ |
| 546 | 546 | public function implementsActions($actions) { |
| 547 | - return (bool)((Backend::CHECK_PASSWORD |
|
| 547 | + return (bool) ((Backend::CHECK_PASSWORD |
|
| 548 | 548 | | Backend::GET_HOME |
| 549 | 549 | | Backend::GET_DISPLAYNAME |
| 550 | 550 | | Backend::PROVIDE_AVATAR |
| 551 | 551 | | Backend::COUNT_USERS |
| 552 | - | ((intval($this->access->connection->turnOnPasswordChange) === 1)?(Backend::SET_PASSWORD):0) |
|
| 552 | + | ((intval($this->access->connection->turnOnPasswordChange) === 1) ? (Backend::SET_PASSWORD) : 0) |
|
| 553 | 553 | | $this->userPluginManager->getImplementedActions()) |
| 554 | 554 | & $actions); |
| 555 | 555 | } |
@@ -573,7 +573,7 @@ discard block |
||
| 573 | 573 | |
| 574 | 574 | $filter = $this->access->getFilterForUserCount(); |
| 575 | 575 | $cacheKey = 'countUsers-'.$filter; |
| 576 | - if(!is_null($entries = $this->access->connection->getFromCache($cacheKey))) { |
|
| 576 | + if (!is_null($entries = $this->access->connection->getFromCache($cacheKey))) { |
|
| 577 | 577 | return $entries; |
| 578 | 578 | } |
| 579 | 579 | $entries = $this->access->countUsers($filter); |
@@ -585,7 +585,7 @@ discard block |
||
| 585 | 585 | * Backend name to be shown in user management |
| 586 | 586 | * @return string the name of the backend to be shown |
| 587 | 587 | */ |
| 588 | - public function getBackendName(){ |
|
| 588 | + public function getBackendName() { |
|
| 589 | 589 | return 'LDAP'; |
| 590 | 590 | } |
| 591 | 591 | |
@@ -59,1636 +59,1636 @@ discard block |
||
| 59 | 59 | * @package OCA\User_LDAP |
| 60 | 60 | */ |
| 61 | 61 | class Access extends LDAPUtility implements IUserTools { |
| 62 | - const UUID_ATTRIBUTES = ['entryuuid', 'nsuniqueid', 'objectguid', 'guid', 'ipauniqueid']; |
|
| 63 | - |
|
| 64 | - /** @var \OCA\User_LDAP\Connection */ |
|
| 65 | - public $connection; |
|
| 66 | - /** @var Manager */ |
|
| 67 | - public $userManager; |
|
| 68 | - //never ever check this var directly, always use getPagedSearchResultState |
|
| 69 | - protected $pagedSearchedSuccessful; |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * @var string[] $cookies an array of returned Paged Result cookies |
|
| 73 | - */ |
|
| 74 | - protected $cookies = array(); |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * @var string $lastCookie the last cookie returned from a Paged Results |
|
| 78 | - * operation, defaults to an empty string |
|
| 79 | - */ |
|
| 80 | - protected $lastCookie = ''; |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * @var AbstractMapping $userMapper |
|
| 84 | - */ |
|
| 85 | - protected $userMapper; |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * @var AbstractMapping $userMapper |
|
| 89 | - */ |
|
| 90 | - protected $groupMapper; |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * @var \OCA\User_LDAP\Helper |
|
| 94 | - */ |
|
| 95 | - private $helper; |
|
| 96 | - /** @var IConfig */ |
|
| 97 | - private $config; |
|
| 98 | - |
|
| 99 | - public function __construct( |
|
| 100 | - Connection $connection, |
|
| 101 | - ILDAPWrapper $ldap, |
|
| 102 | - Manager $userManager, |
|
| 103 | - Helper $helper, |
|
| 104 | - IConfig $config |
|
| 105 | - ) { |
|
| 106 | - parent::__construct($ldap); |
|
| 107 | - $this->connection = $connection; |
|
| 108 | - $this->userManager = $userManager; |
|
| 109 | - $this->userManager->setLdapAccess($this); |
|
| 110 | - $this->helper = $helper; |
|
| 111 | - $this->config = $config; |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * sets the User Mapper |
|
| 116 | - * @param AbstractMapping $mapper |
|
| 117 | - */ |
|
| 118 | - public function setUserMapper(AbstractMapping $mapper) { |
|
| 119 | - $this->userMapper = $mapper; |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * returns the User Mapper |
|
| 124 | - * @throws \Exception |
|
| 125 | - * @return AbstractMapping |
|
| 126 | - */ |
|
| 127 | - public function getUserMapper() { |
|
| 128 | - if(is_null($this->userMapper)) { |
|
| 129 | - throw new \Exception('UserMapper was not assigned to this Access instance.'); |
|
| 130 | - } |
|
| 131 | - return $this->userMapper; |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - /** |
|
| 135 | - * sets the Group Mapper |
|
| 136 | - * @param AbstractMapping $mapper |
|
| 137 | - */ |
|
| 138 | - public function setGroupMapper(AbstractMapping $mapper) { |
|
| 139 | - $this->groupMapper = $mapper; |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - /** |
|
| 143 | - * returns the Group Mapper |
|
| 144 | - * @throws \Exception |
|
| 145 | - * @return AbstractMapping |
|
| 146 | - */ |
|
| 147 | - public function getGroupMapper() { |
|
| 148 | - if(is_null($this->groupMapper)) { |
|
| 149 | - throw new \Exception('GroupMapper was not assigned to this Access instance.'); |
|
| 150 | - } |
|
| 151 | - return $this->groupMapper; |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - /** |
|
| 155 | - * @return bool |
|
| 156 | - */ |
|
| 157 | - private function checkConnection() { |
|
| 158 | - return ($this->connection instanceof Connection); |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - /** |
|
| 162 | - * returns the Connection instance |
|
| 163 | - * @return \OCA\User_LDAP\Connection |
|
| 164 | - */ |
|
| 165 | - public function getConnection() { |
|
| 166 | - return $this->connection; |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - /** |
|
| 170 | - * reads a given attribute for an LDAP record identified by a DN |
|
| 171 | - * |
|
| 172 | - * @param string $dn the record in question |
|
| 173 | - * @param string $attr the attribute that shall be retrieved |
|
| 174 | - * if empty, just check the record's existence |
|
| 175 | - * @param string $filter |
|
| 176 | - * @return array|false an array of values on success or an empty |
|
| 177 | - * array if $attr is empty, false otherwise |
|
| 178 | - * @throws ServerNotAvailableException |
|
| 179 | - */ |
|
| 180 | - public function readAttribute($dn, $attr, $filter = 'objectClass=*') { |
|
| 181 | - if(!$this->checkConnection()) { |
|
| 182 | - \OCP\Util::writeLog('user_ldap', |
|
| 183 | - 'No LDAP Connector assigned, access impossible for readAttribute.', |
|
| 184 | - \OCP\Util::WARN); |
|
| 185 | - return false; |
|
| 186 | - } |
|
| 187 | - $cr = $this->connection->getConnectionResource(); |
|
| 188 | - if(!$this->ldap->isResource($cr)) { |
|
| 189 | - //LDAP not available |
|
| 190 | - \OCP\Util::writeLog('user_ldap', 'LDAP resource not available.', \OCP\Util::DEBUG); |
|
| 191 | - return false; |
|
| 192 | - } |
|
| 193 | - //Cancel possibly running Paged Results operation, otherwise we run in |
|
| 194 | - //LDAP protocol errors |
|
| 195 | - $this->abandonPagedSearch(); |
|
| 196 | - // openLDAP requires that we init a new Paged Search. Not needed by AD, |
|
| 197 | - // but does not hurt either. |
|
| 198 | - $pagingSize = intval($this->connection->ldapPagingSize); |
|
| 199 | - // 0 won't result in replies, small numbers may leave out groups |
|
| 200 | - // (cf. #12306), 500 is default for paging and should work everywhere. |
|
| 201 | - $maxResults = $pagingSize > 20 ? $pagingSize : 500; |
|
| 202 | - $attr = mb_strtolower($attr, 'UTF-8'); |
|
| 203 | - // the actual read attribute later may contain parameters on a ranged |
|
| 204 | - // request, e.g. member;range=99-199. Depends on server reply. |
|
| 205 | - $attrToRead = $attr; |
|
| 206 | - |
|
| 207 | - $values = []; |
|
| 208 | - $isRangeRequest = false; |
|
| 209 | - do { |
|
| 210 | - $result = $this->executeRead($cr, $dn, $attrToRead, $filter, $maxResults); |
|
| 211 | - if(is_bool($result)) { |
|
| 212 | - // when an exists request was run and it was successful, an empty |
|
| 213 | - // array must be returned |
|
| 214 | - return $result ? [] : false; |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - if (!$isRangeRequest) { |
|
| 218 | - $values = $this->extractAttributeValuesFromResult($result, $attr); |
|
| 219 | - if (!empty($values)) { |
|
| 220 | - return $values; |
|
| 221 | - } |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - $isRangeRequest = false; |
|
| 225 | - $result = $this->extractRangeData($result, $attr); |
|
| 226 | - if (!empty($result)) { |
|
| 227 | - $normalizedResult = $this->extractAttributeValuesFromResult( |
|
| 228 | - [ $attr => $result['values'] ], |
|
| 229 | - $attr |
|
| 230 | - ); |
|
| 231 | - $values = array_merge($values, $normalizedResult); |
|
| 232 | - |
|
| 233 | - if($result['rangeHigh'] === '*') { |
|
| 234 | - // when server replies with * as high range value, there are |
|
| 235 | - // no more results left |
|
| 236 | - return $values; |
|
| 237 | - } else { |
|
| 238 | - $low = $result['rangeHigh'] + 1; |
|
| 239 | - $attrToRead = $result['attributeName'] . ';range=' . $low . '-*'; |
|
| 240 | - $isRangeRequest = true; |
|
| 241 | - } |
|
| 242 | - } |
|
| 243 | - } while($isRangeRequest); |
|
| 244 | - |
|
| 245 | - \OCP\Util::writeLog('user_ldap', 'Requested attribute '.$attr.' not found for '.$dn, \OCP\Util::DEBUG); |
|
| 246 | - return false; |
|
| 247 | - } |
|
| 248 | - |
|
| 249 | - /** |
|
| 250 | - * Runs an read operation against LDAP |
|
| 251 | - * |
|
| 252 | - * @param resource $cr the LDAP connection |
|
| 253 | - * @param string $dn |
|
| 254 | - * @param string $attribute |
|
| 255 | - * @param string $filter |
|
| 256 | - * @param int $maxResults |
|
| 257 | - * @return array|bool false if there was any error, true if an exists check |
|
| 258 | - * was performed and the requested DN found, array with the |
|
| 259 | - * returned data on a successful usual operation |
|
| 260 | - * @throws ServerNotAvailableException |
|
| 261 | - */ |
|
| 262 | - public function executeRead($cr, $dn, $attribute, $filter, $maxResults) { |
|
| 263 | - $this->initPagedSearch($filter, array($dn), array($attribute), $maxResults, 0); |
|
| 264 | - $dn = $this->helper->DNasBaseParameter($dn); |
|
| 265 | - $rr = @$this->invokeLDAPMethod('read', $cr, $dn, $filter, array($attribute)); |
|
| 266 | - if (!$this->ldap->isResource($rr)) { |
|
| 267 | - if ($attribute !== '') { |
|
| 268 | - //do not throw this message on userExists check, irritates |
|
| 269 | - \OCP\Util::writeLog('user_ldap', 'readAttribute failed for DN ' . $dn, \OCP\Util::DEBUG); |
|
| 270 | - } |
|
| 271 | - //in case an error occurs , e.g. object does not exist |
|
| 272 | - return false; |
|
| 273 | - } |
|
| 274 | - if ($attribute === '' && ($filter === 'objectclass=*' || $this->invokeLDAPMethod('countEntries', $cr, $rr) === 1)) { |
|
| 275 | - \OCP\Util::writeLog('user_ldap', 'readAttribute: ' . $dn . ' found', \OCP\Util::DEBUG); |
|
| 276 | - return true; |
|
| 277 | - } |
|
| 278 | - $er = $this->invokeLDAPMethod('firstEntry', $cr, $rr); |
|
| 279 | - if (!$this->ldap->isResource($er)) { |
|
| 280 | - //did not match the filter, return false |
|
| 281 | - return false; |
|
| 282 | - } |
|
| 283 | - //LDAP attributes are not case sensitive |
|
| 284 | - $result = \OCP\Util::mb_array_change_key_case( |
|
| 285 | - $this->invokeLDAPMethod('getAttributes', $cr, $er), MB_CASE_LOWER, 'UTF-8'); |
|
| 286 | - |
|
| 287 | - return $result; |
|
| 288 | - } |
|
| 289 | - |
|
| 290 | - /** |
|
| 291 | - * Normalizes a result grom getAttributes(), i.e. handles DNs and binary |
|
| 292 | - * data if present. |
|
| 293 | - * |
|
| 294 | - * @param array $result from ILDAPWrapper::getAttributes() |
|
| 295 | - * @param string $attribute the attribute name that was read |
|
| 296 | - * @return string[] |
|
| 297 | - */ |
|
| 298 | - public function extractAttributeValuesFromResult($result, $attribute) { |
|
| 299 | - $values = []; |
|
| 300 | - if(isset($result[$attribute]) && $result[$attribute]['count'] > 0) { |
|
| 301 | - $lowercaseAttribute = strtolower($attribute); |
|
| 302 | - for($i=0;$i<$result[$attribute]['count'];$i++) { |
|
| 303 | - if($this->resemblesDN($attribute)) { |
|
| 304 | - $values[] = $this->helper->sanitizeDN($result[$attribute][$i]); |
|
| 305 | - } elseif($lowercaseAttribute === 'objectguid' || $lowercaseAttribute === 'guid') { |
|
| 306 | - $values[] = $this->convertObjectGUID2Str($result[$attribute][$i]); |
|
| 307 | - } else { |
|
| 308 | - $values[] = $result[$attribute][$i]; |
|
| 309 | - } |
|
| 310 | - } |
|
| 311 | - } |
|
| 312 | - return $values; |
|
| 313 | - } |
|
| 314 | - |
|
| 315 | - /** |
|
| 316 | - * Attempts to find ranged data in a getAttribute results and extracts the |
|
| 317 | - * returned values as well as information on the range and full attribute |
|
| 318 | - * name for further processing. |
|
| 319 | - * |
|
| 320 | - * @param array $result from ILDAPWrapper::getAttributes() |
|
| 321 | - * @param string $attribute the attribute name that was read. Without ";range=…" |
|
| 322 | - * @return array If a range was detected with keys 'values', 'attributeName', |
|
| 323 | - * 'attributeFull' and 'rangeHigh', otherwise empty. |
|
| 324 | - */ |
|
| 325 | - public function extractRangeData($result, $attribute) { |
|
| 326 | - $keys = array_keys($result); |
|
| 327 | - foreach($keys as $key) { |
|
| 328 | - if($key !== $attribute && strpos($key, $attribute) === 0) { |
|
| 329 | - $queryData = explode(';', $key); |
|
| 330 | - if(strpos($queryData[1], 'range=') === 0) { |
|
| 331 | - $high = substr($queryData[1], 1 + strpos($queryData[1], '-')); |
|
| 332 | - $data = [ |
|
| 333 | - 'values' => $result[$key], |
|
| 334 | - 'attributeName' => $queryData[0], |
|
| 335 | - 'attributeFull' => $key, |
|
| 336 | - 'rangeHigh' => $high, |
|
| 337 | - ]; |
|
| 338 | - return $data; |
|
| 339 | - } |
|
| 340 | - } |
|
| 341 | - } |
|
| 342 | - return []; |
|
| 343 | - } |
|
| 62 | + const UUID_ATTRIBUTES = ['entryuuid', 'nsuniqueid', 'objectguid', 'guid', 'ipauniqueid']; |
|
| 63 | + |
|
| 64 | + /** @var \OCA\User_LDAP\Connection */ |
|
| 65 | + public $connection; |
|
| 66 | + /** @var Manager */ |
|
| 67 | + public $userManager; |
|
| 68 | + //never ever check this var directly, always use getPagedSearchResultState |
|
| 69 | + protected $pagedSearchedSuccessful; |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * @var string[] $cookies an array of returned Paged Result cookies |
|
| 73 | + */ |
|
| 74 | + protected $cookies = array(); |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * @var string $lastCookie the last cookie returned from a Paged Results |
|
| 78 | + * operation, defaults to an empty string |
|
| 79 | + */ |
|
| 80 | + protected $lastCookie = ''; |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * @var AbstractMapping $userMapper |
|
| 84 | + */ |
|
| 85 | + protected $userMapper; |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * @var AbstractMapping $userMapper |
|
| 89 | + */ |
|
| 90 | + protected $groupMapper; |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * @var \OCA\User_LDAP\Helper |
|
| 94 | + */ |
|
| 95 | + private $helper; |
|
| 96 | + /** @var IConfig */ |
|
| 97 | + private $config; |
|
| 98 | + |
|
| 99 | + public function __construct( |
|
| 100 | + Connection $connection, |
|
| 101 | + ILDAPWrapper $ldap, |
|
| 102 | + Manager $userManager, |
|
| 103 | + Helper $helper, |
|
| 104 | + IConfig $config |
|
| 105 | + ) { |
|
| 106 | + parent::__construct($ldap); |
|
| 107 | + $this->connection = $connection; |
|
| 108 | + $this->userManager = $userManager; |
|
| 109 | + $this->userManager->setLdapAccess($this); |
|
| 110 | + $this->helper = $helper; |
|
| 111 | + $this->config = $config; |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * sets the User Mapper |
|
| 116 | + * @param AbstractMapping $mapper |
|
| 117 | + */ |
|
| 118 | + public function setUserMapper(AbstractMapping $mapper) { |
|
| 119 | + $this->userMapper = $mapper; |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * returns the User Mapper |
|
| 124 | + * @throws \Exception |
|
| 125 | + * @return AbstractMapping |
|
| 126 | + */ |
|
| 127 | + public function getUserMapper() { |
|
| 128 | + if(is_null($this->userMapper)) { |
|
| 129 | + throw new \Exception('UserMapper was not assigned to this Access instance.'); |
|
| 130 | + } |
|
| 131 | + return $this->userMapper; |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + /** |
|
| 135 | + * sets the Group Mapper |
|
| 136 | + * @param AbstractMapping $mapper |
|
| 137 | + */ |
|
| 138 | + public function setGroupMapper(AbstractMapping $mapper) { |
|
| 139 | + $this->groupMapper = $mapper; |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + /** |
|
| 143 | + * returns the Group Mapper |
|
| 144 | + * @throws \Exception |
|
| 145 | + * @return AbstractMapping |
|
| 146 | + */ |
|
| 147 | + public function getGroupMapper() { |
|
| 148 | + if(is_null($this->groupMapper)) { |
|
| 149 | + throw new \Exception('GroupMapper was not assigned to this Access instance.'); |
|
| 150 | + } |
|
| 151 | + return $this->groupMapper; |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + /** |
|
| 155 | + * @return bool |
|
| 156 | + */ |
|
| 157 | + private function checkConnection() { |
|
| 158 | + return ($this->connection instanceof Connection); |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + /** |
|
| 162 | + * returns the Connection instance |
|
| 163 | + * @return \OCA\User_LDAP\Connection |
|
| 164 | + */ |
|
| 165 | + public function getConnection() { |
|
| 166 | + return $this->connection; |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + /** |
|
| 170 | + * reads a given attribute for an LDAP record identified by a DN |
|
| 171 | + * |
|
| 172 | + * @param string $dn the record in question |
|
| 173 | + * @param string $attr the attribute that shall be retrieved |
|
| 174 | + * if empty, just check the record's existence |
|
| 175 | + * @param string $filter |
|
| 176 | + * @return array|false an array of values on success or an empty |
|
| 177 | + * array if $attr is empty, false otherwise |
|
| 178 | + * @throws ServerNotAvailableException |
|
| 179 | + */ |
|
| 180 | + public function readAttribute($dn, $attr, $filter = 'objectClass=*') { |
|
| 181 | + if(!$this->checkConnection()) { |
|
| 182 | + \OCP\Util::writeLog('user_ldap', |
|
| 183 | + 'No LDAP Connector assigned, access impossible for readAttribute.', |
|
| 184 | + \OCP\Util::WARN); |
|
| 185 | + return false; |
|
| 186 | + } |
|
| 187 | + $cr = $this->connection->getConnectionResource(); |
|
| 188 | + if(!$this->ldap->isResource($cr)) { |
|
| 189 | + //LDAP not available |
|
| 190 | + \OCP\Util::writeLog('user_ldap', 'LDAP resource not available.', \OCP\Util::DEBUG); |
|
| 191 | + return false; |
|
| 192 | + } |
|
| 193 | + //Cancel possibly running Paged Results operation, otherwise we run in |
|
| 194 | + //LDAP protocol errors |
|
| 195 | + $this->abandonPagedSearch(); |
|
| 196 | + // openLDAP requires that we init a new Paged Search. Not needed by AD, |
|
| 197 | + // but does not hurt either. |
|
| 198 | + $pagingSize = intval($this->connection->ldapPagingSize); |
|
| 199 | + // 0 won't result in replies, small numbers may leave out groups |
|
| 200 | + // (cf. #12306), 500 is default for paging and should work everywhere. |
|
| 201 | + $maxResults = $pagingSize > 20 ? $pagingSize : 500; |
|
| 202 | + $attr = mb_strtolower($attr, 'UTF-8'); |
|
| 203 | + // the actual read attribute later may contain parameters on a ranged |
|
| 204 | + // request, e.g. member;range=99-199. Depends on server reply. |
|
| 205 | + $attrToRead = $attr; |
|
| 206 | + |
|
| 207 | + $values = []; |
|
| 208 | + $isRangeRequest = false; |
|
| 209 | + do { |
|
| 210 | + $result = $this->executeRead($cr, $dn, $attrToRead, $filter, $maxResults); |
|
| 211 | + if(is_bool($result)) { |
|
| 212 | + // when an exists request was run and it was successful, an empty |
|
| 213 | + // array must be returned |
|
| 214 | + return $result ? [] : false; |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + if (!$isRangeRequest) { |
|
| 218 | + $values = $this->extractAttributeValuesFromResult($result, $attr); |
|
| 219 | + if (!empty($values)) { |
|
| 220 | + return $values; |
|
| 221 | + } |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + $isRangeRequest = false; |
|
| 225 | + $result = $this->extractRangeData($result, $attr); |
|
| 226 | + if (!empty($result)) { |
|
| 227 | + $normalizedResult = $this->extractAttributeValuesFromResult( |
|
| 228 | + [ $attr => $result['values'] ], |
|
| 229 | + $attr |
|
| 230 | + ); |
|
| 231 | + $values = array_merge($values, $normalizedResult); |
|
| 232 | + |
|
| 233 | + if($result['rangeHigh'] === '*') { |
|
| 234 | + // when server replies with * as high range value, there are |
|
| 235 | + // no more results left |
|
| 236 | + return $values; |
|
| 237 | + } else { |
|
| 238 | + $low = $result['rangeHigh'] + 1; |
|
| 239 | + $attrToRead = $result['attributeName'] . ';range=' . $low . '-*'; |
|
| 240 | + $isRangeRequest = true; |
|
| 241 | + } |
|
| 242 | + } |
|
| 243 | + } while($isRangeRequest); |
|
| 244 | + |
|
| 245 | + \OCP\Util::writeLog('user_ldap', 'Requested attribute '.$attr.' not found for '.$dn, \OCP\Util::DEBUG); |
|
| 246 | + return false; |
|
| 247 | + } |
|
| 248 | + |
|
| 249 | + /** |
|
| 250 | + * Runs an read operation against LDAP |
|
| 251 | + * |
|
| 252 | + * @param resource $cr the LDAP connection |
|
| 253 | + * @param string $dn |
|
| 254 | + * @param string $attribute |
|
| 255 | + * @param string $filter |
|
| 256 | + * @param int $maxResults |
|
| 257 | + * @return array|bool false if there was any error, true if an exists check |
|
| 258 | + * was performed and the requested DN found, array with the |
|
| 259 | + * returned data on a successful usual operation |
|
| 260 | + * @throws ServerNotAvailableException |
|
| 261 | + */ |
|
| 262 | + public function executeRead($cr, $dn, $attribute, $filter, $maxResults) { |
|
| 263 | + $this->initPagedSearch($filter, array($dn), array($attribute), $maxResults, 0); |
|
| 264 | + $dn = $this->helper->DNasBaseParameter($dn); |
|
| 265 | + $rr = @$this->invokeLDAPMethod('read', $cr, $dn, $filter, array($attribute)); |
|
| 266 | + if (!$this->ldap->isResource($rr)) { |
|
| 267 | + if ($attribute !== '') { |
|
| 268 | + //do not throw this message on userExists check, irritates |
|
| 269 | + \OCP\Util::writeLog('user_ldap', 'readAttribute failed for DN ' . $dn, \OCP\Util::DEBUG); |
|
| 270 | + } |
|
| 271 | + //in case an error occurs , e.g. object does not exist |
|
| 272 | + return false; |
|
| 273 | + } |
|
| 274 | + if ($attribute === '' && ($filter === 'objectclass=*' || $this->invokeLDAPMethod('countEntries', $cr, $rr) === 1)) { |
|
| 275 | + \OCP\Util::writeLog('user_ldap', 'readAttribute: ' . $dn . ' found', \OCP\Util::DEBUG); |
|
| 276 | + return true; |
|
| 277 | + } |
|
| 278 | + $er = $this->invokeLDAPMethod('firstEntry', $cr, $rr); |
|
| 279 | + if (!$this->ldap->isResource($er)) { |
|
| 280 | + //did not match the filter, return false |
|
| 281 | + return false; |
|
| 282 | + } |
|
| 283 | + //LDAP attributes are not case sensitive |
|
| 284 | + $result = \OCP\Util::mb_array_change_key_case( |
|
| 285 | + $this->invokeLDAPMethod('getAttributes', $cr, $er), MB_CASE_LOWER, 'UTF-8'); |
|
| 286 | + |
|
| 287 | + return $result; |
|
| 288 | + } |
|
| 289 | + |
|
| 290 | + /** |
|
| 291 | + * Normalizes a result grom getAttributes(), i.e. handles DNs and binary |
|
| 292 | + * data if present. |
|
| 293 | + * |
|
| 294 | + * @param array $result from ILDAPWrapper::getAttributes() |
|
| 295 | + * @param string $attribute the attribute name that was read |
|
| 296 | + * @return string[] |
|
| 297 | + */ |
|
| 298 | + public function extractAttributeValuesFromResult($result, $attribute) { |
|
| 299 | + $values = []; |
|
| 300 | + if(isset($result[$attribute]) && $result[$attribute]['count'] > 0) { |
|
| 301 | + $lowercaseAttribute = strtolower($attribute); |
|
| 302 | + for($i=0;$i<$result[$attribute]['count'];$i++) { |
|
| 303 | + if($this->resemblesDN($attribute)) { |
|
| 304 | + $values[] = $this->helper->sanitizeDN($result[$attribute][$i]); |
|
| 305 | + } elseif($lowercaseAttribute === 'objectguid' || $lowercaseAttribute === 'guid') { |
|
| 306 | + $values[] = $this->convertObjectGUID2Str($result[$attribute][$i]); |
|
| 307 | + } else { |
|
| 308 | + $values[] = $result[$attribute][$i]; |
|
| 309 | + } |
|
| 310 | + } |
|
| 311 | + } |
|
| 312 | + return $values; |
|
| 313 | + } |
|
| 314 | + |
|
| 315 | + /** |
|
| 316 | + * Attempts to find ranged data in a getAttribute results and extracts the |
|
| 317 | + * returned values as well as information on the range and full attribute |
|
| 318 | + * name for further processing. |
|
| 319 | + * |
|
| 320 | + * @param array $result from ILDAPWrapper::getAttributes() |
|
| 321 | + * @param string $attribute the attribute name that was read. Without ";range=…" |
|
| 322 | + * @return array If a range was detected with keys 'values', 'attributeName', |
|
| 323 | + * 'attributeFull' and 'rangeHigh', otherwise empty. |
|
| 324 | + */ |
|
| 325 | + public function extractRangeData($result, $attribute) { |
|
| 326 | + $keys = array_keys($result); |
|
| 327 | + foreach($keys as $key) { |
|
| 328 | + if($key !== $attribute && strpos($key, $attribute) === 0) { |
|
| 329 | + $queryData = explode(';', $key); |
|
| 330 | + if(strpos($queryData[1], 'range=') === 0) { |
|
| 331 | + $high = substr($queryData[1], 1 + strpos($queryData[1], '-')); |
|
| 332 | + $data = [ |
|
| 333 | + 'values' => $result[$key], |
|
| 334 | + 'attributeName' => $queryData[0], |
|
| 335 | + 'attributeFull' => $key, |
|
| 336 | + 'rangeHigh' => $high, |
|
| 337 | + ]; |
|
| 338 | + return $data; |
|
| 339 | + } |
|
| 340 | + } |
|
| 341 | + } |
|
| 342 | + return []; |
|
| 343 | + } |
|
| 344 | 344 | |
| 345 | - /** |
|
| 346 | - * Set password for an LDAP user identified by a DN |
|
| 347 | - * |
|
| 348 | - * @param string $userDN the user in question |
|
| 349 | - * @param string $password the new password |
|
| 350 | - * @return bool |
|
| 351 | - * @throws HintException |
|
| 352 | - * @throws \Exception |
|
| 353 | - */ |
|
| 354 | - public function setPassword($userDN, $password) { |
|
| 355 | - if(intval($this->connection->turnOnPasswordChange) !== 1) { |
|
| 356 | - throw new \Exception('LDAP password changes are disabled.'); |
|
| 357 | - } |
|
| 358 | - $cr = $this->connection->getConnectionResource(); |
|
| 359 | - if(!$this->ldap->isResource($cr)) { |
|
| 360 | - //LDAP not available |
|
| 361 | - \OCP\Util::writeLog('user_ldap', 'LDAP resource not available.', \OCP\Util::DEBUG); |
|
| 362 | - return false; |
|
| 363 | - } |
|
| 364 | - try { |
|
| 365 | - return @$this->invokeLDAPMethod('modReplace', $cr, $userDN, $password); |
|
| 366 | - } catch(ConstraintViolationException $e) { |
|
| 367 | - throw new HintException('Password change rejected.', \OC::$server->getL10N('user_ldap')->t('Password change rejected. Hint: ').$e->getMessage(), $e->getCode()); |
|
| 368 | - } |
|
| 369 | - } |
|
| 370 | - |
|
| 371 | - /** |
|
| 372 | - * checks whether the given attributes value is probably a DN |
|
| 373 | - * @param string $attr the attribute in question |
|
| 374 | - * @return boolean if so true, otherwise false |
|
| 375 | - */ |
|
| 376 | - private function resemblesDN($attr) { |
|
| 377 | - $resemblingAttributes = array( |
|
| 378 | - 'dn', |
|
| 379 | - 'uniquemember', |
|
| 380 | - 'member', |
|
| 381 | - // memberOf is an "operational" attribute, without a definition in any RFC |
|
| 382 | - 'memberof' |
|
| 383 | - ); |
|
| 384 | - return in_array($attr, $resemblingAttributes); |
|
| 385 | - } |
|
| 386 | - |
|
| 387 | - /** |
|
| 388 | - * checks whether the given string is probably a DN |
|
| 389 | - * @param string $string |
|
| 390 | - * @return boolean |
|
| 391 | - */ |
|
| 392 | - public function stringResemblesDN($string) { |
|
| 393 | - $r = $this->ldap->explodeDN($string, 0); |
|
| 394 | - // if exploding a DN succeeds and does not end up in |
|
| 395 | - // an empty array except for $r[count] being 0. |
|
| 396 | - return (is_array($r) && count($r) > 1); |
|
| 397 | - } |
|
| 398 | - |
|
| 399 | - /** |
|
| 400 | - * returns a DN-string that is cleaned from not domain parts, e.g. |
|
| 401 | - * cn=foo,cn=bar,dc=foobar,dc=server,dc=org |
|
| 402 | - * becomes dc=foobar,dc=server,dc=org |
|
| 403 | - * @param string $dn |
|
| 404 | - * @return string |
|
| 405 | - */ |
|
| 406 | - public function getDomainDNFromDN($dn) { |
|
| 407 | - $allParts = $this->ldap->explodeDN($dn, 0); |
|
| 408 | - if($allParts === false) { |
|
| 409 | - //not a valid DN |
|
| 410 | - return ''; |
|
| 411 | - } |
|
| 412 | - $domainParts = array(); |
|
| 413 | - $dcFound = false; |
|
| 414 | - foreach($allParts as $part) { |
|
| 415 | - if(!$dcFound && strpos($part, 'dc=') === 0) { |
|
| 416 | - $dcFound = true; |
|
| 417 | - } |
|
| 418 | - if($dcFound) { |
|
| 419 | - $domainParts[] = $part; |
|
| 420 | - } |
|
| 421 | - } |
|
| 422 | - $domainDN = implode(',', $domainParts); |
|
| 423 | - return $domainDN; |
|
| 424 | - } |
|
| 425 | - |
|
| 426 | - /** |
|
| 427 | - * returns the LDAP DN for the given internal Nextcloud name of the group |
|
| 428 | - * @param string $name the Nextcloud name in question |
|
| 429 | - * @return string|false LDAP DN on success, otherwise false |
|
| 430 | - */ |
|
| 431 | - public function groupname2dn($name) { |
|
| 432 | - return $this->groupMapper->getDNByName($name); |
|
| 433 | - } |
|
| 434 | - |
|
| 435 | - /** |
|
| 436 | - * returns the LDAP DN for the given internal Nextcloud name of the user |
|
| 437 | - * @param string $name the Nextcloud name in question |
|
| 438 | - * @return string|false with the LDAP DN on success, otherwise false |
|
| 439 | - */ |
|
| 440 | - public function username2dn($name) { |
|
| 441 | - $fdn = $this->userMapper->getDNByName($name); |
|
| 442 | - |
|
| 443 | - //Check whether the DN belongs to the Base, to avoid issues on multi- |
|
| 444 | - //server setups |
|
| 445 | - if(is_string($fdn) && $this->isDNPartOfBase($fdn, $this->connection->ldapBaseUsers)) { |
|
| 446 | - return $fdn; |
|
| 447 | - } |
|
| 448 | - |
|
| 449 | - return false; |
|
| 450 | - } |
|
| 451 | - |
|
| 452 | - /** |
|
| 453 | - * returns the internal Nextcloud name for the given LDAP DN of the group, false on DN outside of search DN or failure |
|
| 454 | - * @param string $fdn the dn of the group object |
|
| 455 | - * @param string $ldapName optional, the display name of the object |
|
| 456 | - * @return string|false with the name to use in Nextcloud, false on DN outside of search DN |
|
| 457 | - */ |
|
| 458 | - public function dn2groupname($fdn, $ldapName = null) { |
|
| 459 | - //To avoid bypassing the base DN settings under certain circumstances |
|
| 460 | - //with the group support, check whether the provided DN matches one of |
|
| 461 | - //the given Bases |
|
| 462 | - if(!$this->isDNPartOfBase($fdn, $this->connection->ldapBaseGroups)) { |
|
| 463 | - return false; |
|
| 464 | - } |
|
| 465 | - |
|
| 466 | - return $this->dn2ocname($fdn, $ldapName, false); |
|
| 467 | - } |
|
| 468 | - |
|
| 469 | - /** |
|
| 470 | - * accepts an array of group DNs and tests whether they match the user |
|
| 471 | - * filter by doing read operations against the group entries. Returns an |
|
| 472 | - * array of DNs that match the filter. |
|
| 473 | - * |
|
| 474 | - * @param string[] $groupDNs |
|
| 475 | - * @return string[] |
|
| 476 | - */ |
|
| 477 | - public function groupsMatchFilter($groupDNs) { |
|
| 478 | - $validGroupDNs = []; |
|
| 479 | - foreach($groupDNs as $dn) { |
|
| 480 | - $cacheKey = 'groupsMatchFilter-'.$dn; |
|
| 481 | - $groupMatchFilter = $this->connection->getFromCache($cacheKey); |
|
| 482 | - if(!is_null($groupMatchFilter)) { |
|
| 483 | - if($groupMatchFilter) { |
|
| 484 | - $validGroupDNs[] = $dn; |
|
| 485 | - } |
|
| 486 | - continue; |
|
| 487 | - } |
|
| 488 | - |
|
| 489 | - // Check the base DN first. If this is not met already, we don't |
|
| 490 | - // need to ask the server at all. |
|
| 491 | - if(!$this->isDNPartOfBase($dn, $this->connection->ldapBaseGroups)) { |
|
| 492 | - $this->connection->writeToCache($cacheKey, false); |
|
| 493 | - continue; |
|
| 494 | - } |
|
| 495 | - |
|
| 496 | - $result = $this->readAttribute($dn, 'cn', $this->connection->ldapGroupFilter); |
|
| 497 | - if(is_array($result)) { |
|
| 498 | - $this->connection->writeToCache($cacheKey, true); |
|
| 499 | - $validGroupDNs[] = $dn; |
|
| 500 | - } else { |
|
| 501 | - $this->connection->writeToCache($cacheKey, false); |
|
| 502 | - } |
|
| 503 | - |
|
| 504 | - } |
|
| 505 | - return $validGroupDNs; |
|
| 506 | - } |
|
| 507 | - |
|
| 508 | - /** |
|
| 509 | - * returns the internal Nextcloud name for the given LDAP DN of the user, false on DN outside of search DN or failure |
|
| 510 | - * @param string $dn the dn of the user object |
|
| 511 | - * @param string $ldapName optional, the display name of the object |
|
| 512 | - * @return string|false with with the name to use in Nextcloud |
|
| 513 | - */ |
|
| 514 | - public function dn2username($fdn, $ldapName = null) { |
|
| 515 | - //To avoid bypassing the base DN settings under certain circumstances |
|
| 516 | - //with the group support, check whether the provided DN matches one of |
|
| 517 | - //the given Bases |
|
| 518 | - if(!$this->isDNPartOfBase($fdn, $this->connection->ldapBaseUsers)) { |
|
| 519 | - return false; |
|
| 520 | - } |
|
| 521 | - |
|
| 522 | - return $this->dn2ocname($fdn, $ldapName, true); |
|
| 523 | - } |
|
| 524 | - |
|
| 525 | - /** |
|
| 526 | - * returns an internal Nextcloud name for the given LDAP DN, false on DN outside of search DN |
|
| 527 | - * |
|
| 528 | - * @param string $fdn the dn of the user object |
|
| 529 | - * @param string|null $ldapName optional, the display name of the object |
|
| 530 | - * @param bool $isUser optional, whether it is a user object (otherwise group assumed) |
|
| 531 | - * @param bool|null $newlyMapped |
|
| 532 | - * @param array|null $record |
|
| 533 | - * @return false|string with with the name to use in Nextcloud |
|
| 534 | - * @throws \Exception |
|
| 535 | - */ |
|
| 536 | - public function dn2ocname($fdn, $ldapName = null, $isUser = true, &$newlyMapped = null, array $record = null) { |
|
| 537 | - $newlyMapped = false; |
|
| 538 | - if($isUser) { |
|
| 539 | - $mapper = $this->getUserMapper(); |
|
| 540 | - $nameAttribute = $this->connection->ldapUserDisplayName; |
|
| 541 | - } else { |
|
| 542 | - $mapper = $this->getGroupMapper(); |
|
| 543 | - $nameAttribute = $this->connection->ldapGroupDisplayName; |
|
| 544 | - } |
|
| 545 | - |
|
| 546 | - //let's try to retrieve the Nextcloud name from the mappings table |
|
| 547 | - $ncName = $mapper->getNameByDN($fdn); |
|
| 548 | - if(is_string($ncName)) { |
|
| 549 | - return $ncName; |
|
| 550 | - } |
|
| 551 | - |
|
| 552 | - //second try: get the UUID and check if it is known. Then, update the DN and return the name. |
|
| 553 | - $uuid = $this->getUUID($fdn, $isUser, $record); |
|
| 554 | - if(is_string($uuid)) { |
|
| 555 | - $ncName = $mapper->getNameByUUID($uuid); |
|
| 556 | - if(is_string($ncName)) { |
|
| 557 | - $mapper->setDNbyUUID($fdn, $uuid); |
|
| 558 | - return $ncName; |
|
| 559 | - } |
|
| 560 | - } else { |
|
| 561 | - //If the UUID can't be detected something is foul. |
|
| 562 | - \OCP\Util::writeLog('user_ldap', 'Cannot determine UUID for '.$fdn.'. Skipping.', \OCP\Util::INFO); |
|
| 563 | - return false; |
|
| 564 | - } |
|
| 565 | - |
|
| 566 | - if(is_null($ldapName)) { |
|
| 567 | - $ldapName = $this->readAttribute($fdn, $nameAttribute); |
|
| 568 | - if(!isset($ldapName[0]) && empty($ldapName[0])) { |
|
| 569 | - \OCP\Util::writeLog('user_ldap', 'No or empty name for '.$fdn.'.', \OCP\Util::INFO); |
|
| 570 | - return false; |
|
| 571 | - } |
|
| 572 | - $ldapName = $ldapName[0]; |
|
| 573 | - } |
|
| 574 | - |
|
| 575 | - if($isUser) { |
|
| 576 | - $usernameAttribute = strval($this->connection->ldapExpertUsernameAttr); |
|
| 577 | - if ($usernameAttribute !== '') { |
|
| 578 | - $username = $this->readAttribute($fdn, $usernameAttribute); |
|
| 579 | - $username = $username[0]; |
|
| 580 | - } else { |
|
| 581 | - $username = $uuid; |
|
| 582 | - } |
|
| 583 | - $intName = $this->sanitizeUsername($username); |
|
| 584 | - } else { |
|
| 585 | - $intName = $ldapName; |
|
| 586 | - } |
|
| 587 | - |
|
| 588 | - //a new user/group! Add it only if it doesn't conflict with other backend's users or existing groups |
|
| 589 | - //disabling Cache is required to avoid that the new user is cached as not-existing in fooExists check |
|
| 590 | - //NOTE: mind, disabling cache affects only this instance! Using it |
|
| 591 | - // outside of core user management will still cache the user as non-existing. |
|
| 592 | - $originalTTL = $this->connection->ldapCacheTTL; |
|
| 593 | - $this->connection->setConfiguration(array('ldapCacheTTL' => 0)); |
|
| 594 | - if(($isUser && $intName !== '' && !\OC::$server->getUserManager()->userExists($intName)) |
|
| 595 | - || (!$isUser && !\OC::$server->getGroupManager()->groupExists($intName))) { |
|
| 596 | - if($mapper->map($fdn, $intName, $uuid)) { |
|
| 597 | - $this->connection->setConfiguration(array('ldapCacheTTL' => $originalTTL)); |
|
| 598 | - $newlyMapped = true; |
|
| 599 | - return $intName; |
|
| 600 | - } |
|
| 601 | - } |
|
| 602 | - $this->connection->setConfiguration(array('ldapCacheTTL' => $originalTTL)); |
|
| 603 | - |
|
| 604 | - $altName = $this->createAltInternalOwnCloudName($intName, $isUser); |
|
| 605 | - if(is_string($altName) && $mapper->map($fdn, $altName, $uuid)) { |
|
| 606 | - $newlyMapped = true; |
|
| 607 | - return $altName; |
|
| 608 | - } |
|
| 609 | - |
|
| 610 | - //if everything else did not help.. |
|
| 611 | - \OCP\Util::writeLog('user_ldap', 'Could not create unique name for '.$fdn.'.', \OCP\Util::INFO); |
|
| 612 | - return false; |
|
| 613 | - } |
|
| 614 | - |
|
| 615 | - /** |
|
| 616 | - * gives back the user names as they are used ownClod internally |
|
| 617 | - * @param array $ldapUsers as returned by fetchList() |
|
| 618 | - * @return array an array with the user names to use in Nextcloud |
|
| 619 | - * |
|
| 620 | - * gives back the user names as they are used ownClod internally |
|
| 621 | - */ |
|
| 622 | - public function nextcloudUserNames($ldapUsers) { |
|
| 623 | - return $this->ldap2NextcloudNames($ldapUsers, true); |
|
| 624 | - } |
|
| 625 | - |
|
| 626 | - /** |
|
| 627 | - * gives back the group names as they are used ownClod internally |
|
| 628 | - * @param array $ldapGroups as returned by fetchList() |
|
| 629 | - * @return array an array with the group names to use in Nextcloud |
|
| 630 | - * |
|
| 631 | - * gives back the group names as they are used ownClod internally |
|
| 632 | - */ |
|
| 633 | - public function nextcloudGroupNames($ldapGroups) { |
|
| 634 | - return $this->ldap2NextcloudNames($ldapGroups, false); |
|
| 635 | - } |
|
| 636 | - |
|
| 637 | - /** |
|
| 638 | - * @param array $ldapObjects as returned by fetchList() |
|
| 639 | - * @param bool $isUsers |
|
| 640 | - * @return array |
|
| 641 | - */ |
|
| 642 | - private function ldap2NextcloudNames($ldapObjects, $isUsers) { |
|
| 643 | - if($isUsers) { |
|
| 644 | - $nameAttribute = $this->connection->ldapUserDisplayName; |
|
| 645 | - $sndAttribute = $this->connection->ldapUserDisplayName2; |
|
| 646 | - } else { |
|
| 647 | - $nameAttribute = $this->connection->ldapGroupDisplayName; |
|
| 648 | - } |
|
| 649 | - $nextcloudNames = array(); |
|
| 650 | - |
|
| 651 | - foreach($ldapObjects as $ldapObject) { |
|
| 652 | - $nameByLDAP = null; |
|
| 653 | - if( isset($ldapObject[$nameAttribute]) |
|
| 654 | - && is_array($ldapObject[$nameAttribute]) |
|
| 655 | - && isset($ldapObject[$nameAttribute][0]) |
|
| 656 | - ) { |
|
| 657 | - // might be set, but not necessarily. if so, we use it. |
|
| 658 | - $nameByLDAP = $ldapObject[$nameAttribute][0]; |
|
| 659 | - } |
|
| 660 | - |
|
| 661 | - $ncName = $this->dn2ocname($ldapObject['dn'][0], $nameByLDAP, $isUsers); |
|
| 662 | - if($ncName) { |
|
| 663 | - $nextcloudNames[] = $ncName; |
|
| 664 | - if($isUsers) { |
|
| 665 | - //cache the user names so it does not need to be retrieved |
|
| 666 | - //again later (e.g. sharing dialogue). |
|
| 667 | - if(is_null($nameByLDAP)) { |
|
| 668 | - continue; |
|
| 669 | - } |
|
| 670 | - $sndName = isset($ldapObject[$sndAttribute][0]) |
|
| 671 | - ? $ldapObject[$sndAttribute][0] : ''; |
|
| 672 | - $this->cacheUserDisplayName($ncName, $nameByLDAP, $sndName); |
|
| 673 | - } |
|
| 674 | - } |
|
| 675 | - } |
|
| 676 | - return $nextcloudNames; |
|
| 677 | - } |
|
| 678 | - |
|
| 679 | - /** |
|
| 680 | - * caches the user display name |
|
| 681 | - * @param string $ocName the internal Nextcloud username |
|
| 682 | - * @param string|false $home the home directory path |
|
| 683 | - */ |
|
| 684 | - public function cacheUserHome($ocName, $home) { |
|
| 685 | - $cacheKey = 'getHome'.$ocName; |
|
| 686 | - $this->connection->writeToCache($cacheKey, $home); |
|
| 687 | - } |
|
| 688 | - |
|
| 689 | - /** |
|
| 690 | - * caches a user as existing |
|
| 691 | - * @param string $ocName the internal Nextcloud username |
|
| 692 | - */ |
|
| 693 | - public function cacheUserExists($ocName) { |
|
| 694 | - $this->connection->writeToCache('userExists'.$ocName, true); |
|
| 695 | - } |
|
| 696 | - |
|
| 697 | - /** |
|
| 698 | - * caches the user display name |
|
| 699 | - * @param string $ocName the internal Nextcloud username |
|
| 700 | - * @param string $displayName the display name |
|
| 701 | - * @param string $displayName2 the second display name |
|
| 702 | - */ |
|
| 703 | - public function cacheUserDisplayName($ocName, $displayName, $displayName2 = '') { |
|
| 704 | - $user = $this->userManager->get($ocName); |
|
| 705 | - if($user === null) { |
|
| 706 | - return; |
|
| 707 | - } |
|
| 708 | - $displayName = $user->composeAndStoreDisplayName($displayName, $displayName2); |
|
| 709 | - $cacheKeyTrunk = 'getDisplayName'; |
|
| 710 | - $this->connection->writeToCache($cacheKeyTrunk.$ocName, $displayName); |
|
| 711 | - } |
|
| 712 | - |
|
| 713 | - /** |
|
| 714 | - * creates a unique name for internal Nextcloud use for users. Don't call it directly. |
|
| 715 | - * @param string $name the display name of the object |
|
| 716 | - * @return string|false with with the name to use in Nextcloud or false if unsuccessful |
|
| 717 | - * |
|
| 718 | - * Instead of using this method directly, call |
|
| 719 | - * createAltInternalOwnCloudName($name, true) |
|
| 720 | - */ |
|
| 721 | - private function _createAltInternalOwnCloudNameForUsers($name) { |
|
| 722 | - $attempts = 0; |
|
| 723 | - //while loop is just a precaution. If a name is not generated within |
|
| 724 | - //20 attempts, something else is very wrong. Avoids infinite loop. |
|
| 725 | - while($attempts < 20){ |
|
| 726 | - $altName = $name . '_' . rand(1000,9999); |
|
| 727 | - if(!\OC::$server->getUserManager()->userExists($altName)) { |
|
| 728 | - return $altName; |
|
| 729 | - } |
|
| 730 | - $attempts++; |
|
| 731 | - } |
|
| 732 | - return false; |
|
| 733 | - } |
|
| 734 | - |
|
| 735 | - /** |
|
| 736 | - * creates a unique name for internal Nextcloud use for groups. Don't call it directly. |
|
| 737 | - * @param string $name the display name of the object |
|
| 738 | - * @return string|false with with the name to use in Nextcloud or false if unsuccessful. |
|
| 739 | - * |
|
| 740 | - * Instead of using this method directly, call |
|
| 741 | - * createAltInternalOwnCloudName($name, false) |
|
| 742 | - * |
|
| 743 | - * Group names are also used as display names, so we do a sequential |
|
| 744 | - * numbering, e.g. Developers_42 when there are 41 other groups called |
|
| 745 | - * "Developers" |
|
| 746 | - */ |
|
| 747 | - private function _createAltInternalOwnCloudNameForGroups($name) { |
|
| 748 | - $usedNames = $this->groupMapper->getNamesBySearch($name, "", '_%'); |
|
| 749 | - if(!($usedNames) || count($usedNames) === 0) { |
|
| 750 | - $lastNo = 1; //will become name_2 |
|
| 751 | - } else { |
|
| 752 | - natsort($usedNames); |
|
| 753 | - $lastName = array_pop($usedNames); |
|
| 754 | - $lastNo = intval(substr($lastName, strrpos($lastName, '_') + 1)); |
|
| 755 | - } |
|
| 756 | - $altName = $name.'_'.strval($lastNo+1); |
|
| 757 | - unset($usedNames); |
|
| 758 | - |
|
| 759 | - $attempts = 1; |
|
| 760 | - while($attempts < 21){ |
|
| 761 | - // Check to be really sure it is unique |
|
| 762 | - // while loop is just a precaution. If a name is not generated within |
|
| 763 | - // 20 attempts, something else is very wrong. Avoids infinite loop. |
|
| 764 | - if(!\OC::$server->getGroupManager()->groupExists($altName)) { |
|
| 765 | - return $altName; |
|
| 766 | - } |
|
| 767 | - $altName = $name . '_' . ($lastNo + $attempts); |
|
| 768 | - $attempts++; |
|
| 769 | - } |
|
| 770 | - return false; |
|
| 771 | - } |
|
| 772 | - |
|
| 773 | - /** |
|
| 774 | - * creates a unique name for internal Nextcloud use. |
|
| 775 | - * @param string $name the display name of the object |
|
| 776 | - * @param boolean $isUser whether name should be created for a user (true) or a group (false) |
|
| 777 | - * @return string|false with with the name to use in Nextcloud or false if unsuccessful |
|
| 778 | - */ |
|
| 779 | - private function createAltInternalOwnCloudName($name, $isUser) { |
|
| 780 | - $originalTTL = $this->connection->ldapCacheTTL; |
|
| 781 | - $this->connection->setConfiguration(array('ldapCacheTTL' => 0)); |
|
| 782 | - if($isUser) { |
|
| 783 | - $altName = $this->_createAltInternalOwnCloudNameForUsers($name); |
|
| 784 | - } else { |
|
| 785 | - $altName = $this->_createAltInternalOwnCloudNameForGroups($name); |
|
| 786 | - } |
|
| 787 | - $this->connection->setConfiguration(array('ldapCacheTTL' => $originalTTL)); |
|
| 788 | - |
|
| 789 | - return $altName; |
|
| 790 | - } |
|
| 791 | - |
|
| 792 | - /** |
|
| 793 | - * fetches a list of users according to a provided loginName and utilizing |
|
| 794 | - * the login filter. |
|
| 795 | - * |
|
| 796 | - * @param string $loginName |
|
| 797 | - * @param array $attributes optional, list of attributes to read |
|
| 798 | - * @return array |
|
| 799 | - */ |
|
| 800 | - public function fetchUsersByLoginName($loginName, $attributes = array('dn')) { |
|
| 801 | - $loginName = $this->escapeFilterPart($loginName); |
|
| 802 | - $filter = str_replace('%uid', $loginName, $this->connection->ldapLoginFilter); |
|
| 803 | - $users = $this->fetchListOfUsers($filter, $attributes); |
|
| 804 | - return $users; |
|
| 805 | - } |
|
| 806 | - |
|
| 807 | - /** |
|
| 808 | - * counts the number of users according to a provided loginName and |
|
| 809 | - * utilizing the login filter. |
|
| 810 | - * |
|
| 811 | - * @param string $loginName |
|
| 812 | - * @return int |
|
| 813 | - */ |
|
| 814 | - public function countUsersByLoginName($loginName) { |
|
| 815 | - $loginName = $this->escapeFilterPart($loginName); |
|
| 816 | - $filter = str_replace('%uid', $loginName, $this->connection->ldapLoginFilter); |
|
| 817 | - $users = $this->countUsers($filter); |
|
| 818 | - return $users; |
|
| 819 | - } |
|
| 820 | - |
|
| 821 | - /** |
|
| 822 | - * @param string $filter |
|
| 823 | - * @param string|string[] $attr |
|
| 824 | - * @param int $limit |
|
| 825 | - * @param int $offset |
|
| 826 | - * @param bool $forceApplyAttributes |
|
| 827 | - * @return array |
|
| 828 | - */ |
|
| 829 | - public function fetchListOfUsers($filter, $attr, $limit = null, $offset = null, $forceApplyAttributes = false) { |
|
| 830 | - $ldapRecords = $this->searchUsers($filter, $attr, $limit, $offset); |
|
| 831 | - $recordsToUpdate = $ldapRecords; |
|
| 832 | - if(!$forceApplyAttributes) { |
|
| 833 | - $isBackgroundJobModeAjax = $this->config |
|
| 834 | - ->getAppValue('core', 'backgroundjobs_mode', 'ajax') === 'ajax'; |
|
| 835 | - $recordsToUpdate = array_filter($ldapRecords, function($record) use ($isBackgroundJobModeAjax) { |
|
| 836 | - $newlyMapped = false; |
|
| 837 | - $uid = $this->dn2ocname($record['dn'][0], null, true, $newlyMapped, $record); |
|
| 838 | - if(is_string($uid)) { |
|
| 839 | - $this->cacheUserExists($uid); |
|
| 840 | - } |
|
| 841 | - return ($uid !== false) && ($newlyMapped || $isBackgroundJobModeAjax); |
|
| 842 | - }); |
|
| 843 | - } |
|
| 844 | - $this->batchApplyUserAttributes($recordsToUpdate); |
|
| 845 | - return $this->fetchList($ldapRecords, (count($attr) > 1)); |
|
| 846 | - } |
|
| 847 | - |
|
| 848 | - /** |
|
| 849 | - * provided with an array of LDAP user records the method will fetch the |
|
| 850 | - * user object and requests it to process the freshly fetched attributes and |
|
| 851 | - * and their values |
|
| 852 | - * @param array $ldapRecords |
|
| 853 | - */ |
|
| 854 | - public function batchApplyUserAttributes(array $ldapRecords){ |
|
| 855 | - $displayNameAttribute = strtolower($this->connection->ldapUserDisplayName); |
|
| 856 | - foreach($ldapRecords as $userRecord) { |
|
| 857 | - if(!isset($userRecord[$displayNameAttribute])) { |
|
| 858 | - // displayName is obligatory |
|
| 859 | - continue; |
|
| 860 | - } |
|
| 861 | - $ocName = $this->dn2ocname($userRecord['dn'][0], null, true); |
|
| 862 | - if($ocName === false) { |
|
| 863 | - continue; |
|
| 864 | - } |
|
| 865 | - $user = $this->userManager->get($ocName); |
|
| 866 | - if($user instanceof OfflineUser) { |
|
| 867 | - $user->unmark(); |
|
| 868 | - $user = $this->userManager->get($ocName); |
|
| 869 | - } |
|
| 870 | - if ($user !== null) { |
|
| 871 | - $user->processAttributes($userRecord); |
|
| 872 | - } else { |
|
| 873 | - \OC::$server->getLogger()->debug( |
|
| 874 | - "The ldap user manager returned null for $ocName", |
|
| 875 | - ['app'=>'user_ldap'] |
|
| 876 | - ); |
|
| 877 | - } |
|
| 878 | - } |
|
| 879 | - } |
|
| 880 | - |
|
| 881 | - /** |
|
| 882 | - * @param string $filter |
|
| 883 | - * @param string|string[] $attr |
|
| 884 | - * @param int $limit |
|
| 885 | - * @param int $offset |
|
| 886 | - * @return array |
|
| 887 | - */ |
|
| 888 | - public function fetchListOfGroups($filter, $attr, $limit = null, $offset = null) { |
|
| 889 | - return $this->fetchList($this->searchGroups($filter, $attr, $limit, $offset), (count($attr) > 1)); |
|
| 890 | - } |
|
| 891 | - |
|
| 892 | - /** |
|
| 893 | - * @param array $list |
|
| 894 | - * @param bool $manyAttributes |
|
| 895 | - * @return array |
|
| 896 | - */ |
|
| 897 | - private function fetchList($list, $manyAttributes) { |
|
| 898 | - if(is_array($list)) { |
|
| 899 | - if($manyAttributes) { |
|
| 900 | - return $list; |
|
| 901 | - } else { |
|
| 902 | - $list = array_reduce($list, function($carry, $item) { |
|
| 903 | - $attribute = array_keys($item)[0]; |
|
| 904 | - $carry[] = $item[$attribute][0]; |
|
| 905 | - return $carry; |
|
| 906 | - }, array()); |
|
| 907 | - return array_unique($list, SORT_LOCALE_STRING); |
|
| 908 | - } |
|
| 909 | - } |
|
| 910 | - |
|
| 911 | - //error cause actually, maybe throw an exception in future. |
|
| 912 | - return array(); |
|
| 913 | - } |
|
| 914 | - |
|
| 915 | - /** |
|
| 916 | - * executes an LDAP search, optimized for Users |
|
| 917 | - * @param string $filter the LDAP filter for the search |
|
| 918 | - * @param string|string[] $attr optional, when a certain attribute shall be filtered out |
|
| 919 | - * @param integer $limit |
|
| 920 | - * @param integer $offset |
|
| 921 | - * @return array with the search result |
|
| 922 | - * |
|
| 923 | - * Executes an LDAP search |
|
| 924 | - */ |
|
| 925 | - public function searchUsers($filter, $attr = null, $limit = null, $offset = null) { |
|
| 926 | - return $this->search($filter, $this->connection->ldapBaseUsers, $attr, $limit, $offset); |
|
| 927 | - } |
|
| 928 | - |
|
| 929 | - /** |
|
| 930 | - * @param string $filter |
|
| 931 | - * @param string|string[] $attr |
|
| 932 | - * @param int $limit |
|
| 933 | - * @param int $offset |
|
| 934 | - * @return false|int |
|
| 935 | - */ |
|
| 936 | - public function countUsers($filter, $attr = array('dn'), $limit = null, $offset = null) { |
|
| 937 | - return $this->count($filter, $this->connection->ldapBaseUsers, $attr, $limit, $offset); |
|
| 938 | - } |
|
| 939 | - |
|
| 940 | - /** |
|
| 941 | - * executes an LDAP search, optimized for Groups |
|
| 942 | - * @param string $filter the LDAP filter for the search |
|
| 943 | - * @param string|string[] $attr optional, when a certain attribute shall be filtered out |
|
| 944 | - * @param integer $limit |
|
| 945 | - * @param integer $offset |
|
| 946 | - * @return array with the search result |
|
| 947 | - * |
|
| 948 | - * Executes an LDAP search |
|
| 949 | - */ |
|
| 950 | - public function searchGroups($filter, $attr = null, $limit = null, $offset = null) { |
|
| 951 | - return $this->search($filter, $this->connection->ldapBaseGroups, $attr, $limit, $offset); |
|
| 952 | - } |
|
| 953 | - |
|
| 954 | - /** |
|
| 955 | - * returns the number of available groups |
|
| 956 | - * @param string $filter the LDAP search filter |
|
| 957 | - * @param string[] $attr optional |
|
| 958 | - * @param int|null $limit |
|
| 959 | - * @param int|null $offset |
|
| 960 | - * @return int|bool |
|
| 961 | - */ |
|
| 962 | - public function countGroups($filter, $attr = array('dn'), $limit = null, $offset = null) { |
|
| 963 | - return $this->count($filter, $this->connection->ldapBaseGroups, $attr, $limit, $offset); |
|
| 964 | - } |
|
| 965 | - |
|
| 966 | - /** |
|
| 967 | - * returns the number of available objects on the base DN |
|
| 968 | - * |
|
| 969 | - * @param int|null $limit |
|
| 970 | - * @param int|null $offset |
|
| 971 | - * @return int|bool |
|
| 972 | - */ |
|
| 973 | - public function countObjects($limit = null, $offset = null) { |
|
| 974 | - return $this->count('objectclass=*', $this->connection->ldapBase, array('dn'), $limit, $offset); |
|
| 975 | - } |
|
| 976 | - |
|
| 977 | - /** |
|
| 978 | - * Returns the LDAP handler |
|
| 979 | - * @throws \OC\ServerNotAvailableException |
|
| 980 | - */ |
|
| 981 | - |
|
| 982 | - /** |
|
| 983 | - * @return mixed |
|
| 984 | - * @throws \OC\ServerNotAvailableException |
|
| 985 | - */ |
|
| 986 | - private function invokeLDAPMethod() { |
|
| 987 | - $arguments = func_get_args(); |
|
| 988 | - $command = array_shift($arguments); |
|
| 989 | - $cr = array_shift($arguments); |
|
| 990 | - if (!method_exists($this->ldap, $command)) { |
|
| 991 | - return null; |
|
| 992 | - } |
|
| 993 | - array_unshift($arguments, $cr); |
|
| 994 | - // php no longer supports call-time pass-by-reference |
|
| 995 | - // thus cannot support controlPagedResultResponse as the third argument |
|
| 996 | - // is a reference |
|
| 997 | - $doMethod = function () use ($command, &$arguments) { |
|
| 998 | - if ($command == 'controlPagedResultResponse') { |
|
| 999 | - throw new \InvalidArgumentException('Invoker does not support controlPagedResultResponse, call LDAP Wrapper directly instead.'); |
|
| 1000 | - } else { |
|
| 1001 | - return call_user_func_array(array($this->ldap, $command), $arguments); |
|
| 1002 | - } |
|
| 1003 | - }; |
|
| 1004 | - try { |
|
| 1005 | - $ret = $doMethod(); |
|
| 1006 | - } catch (ServerNotAvailableException $e) { |
|
| 1007 | - /* Server connection lost, attempt to reestablish it |
|
| 345 | + /** |
|
| 346 | + * Set password for an LDAP user identified by a DN |
|
| 347 | + * |
|
| 348 | + * @param string $userDN the user in question |
|
| 349 | + * @param string $password the new password |
|
| 350 | + * @return bool |
|
| 351 | + * @throws HintException |
|
| 352 | + * @throws \Exception |
|
| 353 | + */ |
|
| 354 | + public function setPassword($userDN, $password) { |
|
| 355 | + if(intval($this->connection->turnOnPasswordChange) !== 1) { |
|
| 356 | + throw new \Exception('LDAP password changes are disabled.'); |
|
| 357 | + } |
|
| 358 | + $cr = $this->connection->getConnectionResource(); |
|
| 359 | + if(!$this->ldap->isResource($cr)) { |
|
| 360 | + //LDAP not available |
|
| 361 | + \OCP\Util::writeLog('user_ldap', 'LDAP resource not available.', \OCP\Util::DEBUG); |
|
| 362 | + return false; |
|
| 363 | + } |
|
| 364 | + try { |
|
| 365 | + return @$this->invokeLDAPMethod('modReplace', $cr, $userDN, $password); |
|
| 366 | + } catch(ConstraintViolationException $e) { |
|
| 367 | + throw new HintException('Password change rejected.', \OC::$server->getL10N('user_ldap')->t('Password change rejected. Hint: ').$e->getMessage(), $e->getCode()); |
|
| 368 | + } |
|
| 369 | + } |
|
| 370 | + |
|
| 371 | + /** |
|
| 372 | + * checks whether the given attributes value is probably a DN |
|
| 373 | + * @param string $attr the attribute in question |
|
| 374 | + * @return boolean if so true, otherwise false |
|
| 375 | + */ |
|
| 376 | + private function resemblesDN($attr) { |
|
| 377 | + $resemblingAttributes = array( |
|
| 378 | + 'dn', |
|
| 379 | + 'uniquemember', |
|
| 380 | + 'member', |
|
| 381 | + // memberOf is an "operational" attribute, without a definition in any RFC |
|
| 382 | + 'memberof' |
|
| 383 | + ); |
|
| 384 | + return in_array($attr, $resemblingAttributes); |
|
| 385 | + } |
|
| 386 | + |
|
| 387 | + /** |
|
| 388 | + * checks whether the given string is probably a DN |
|
| 389 | + * @param string $string |
|
| 390 | + * @return boolean |
|
| 391 | + */ |
|
| 392 | + public function stringResemblesDN($string) { |
|
| 393 | + $r = $this->ldap->explodeDN($string, 0); |
|
| 394 | + // if exploding a DN succeeds and does not end up in |
|
| 395 | + // an empty array except for $r[count] being 0. |
|
| 396 | + return (is_array($r) && count($r) > 1); |
|
| 397 | + } |
|
| 398 | + |
|
| 399 | + /** |
|
| 400 | + * returns a DN-string that is cleaned from not domain parts, e.g. |
|
| 401 | + * cn=foo,cn=bar,dc=foobar,dc=server,dc=org |
|
| 402 | + * becomes dc=foobar,dc=server,dc=org |
|
| 403 | + * @param string $dn |
|
| 404 | + * @return string |
|
| 405 | + */ |
|
| 406 | + public function getDomainDNFromDN($dn) { |
|
| 407 | + $allParts = $this->ldap->explodeDN($dn, 0); |
|
| 408 | + if($allParts === false) { |
|
| 409 | + //not a valid DN |
|
| 410 | + return ''; |
|
| 411 | + } |
|
| 412 | + $domainParts = array(); |
|
| 413 | + $dcFound = false; |
|
| 414 | + foreach($allParts as $part) { |
|
| 415 | + if(!$dcFound && strpos($part, 'dc=') === 0) { |
|
| 416 | + $dcFound = true; |
|
| 417 | + } |
|
| 418 | + if($dcFound) { |
|
| 419 | + $domainParts[] = $part; |
|
| 420 | + } |
|
| 421 | + } |
|
| 422 | + $domainDN = implode(',', $domainParts); |
|
| 423 | + return $domainDN; |
|
| 424 | + } |
|
| 425 | + |
|
| 426 | + /** |
|
| 427 | + * returns the LDAP DN for the given internal Nextcloud name of the group |
|
| 428 | + * @param string $name the Nextcloud name in question |
|
| 429 | + * @return string|false LDAP DN on success, otherwise false |
|
| 430 | + */ |
|
| 431 | + public function groupname2dn($name) { |
|
| 432 | + return $this->groupMapper->getDNByName($name); |
|
| 433 | + } |
|
| 434 | + |
|
| 435 | + /** |
|
| 436 | + * returns the LDAP DN for the given internal Nextcloud name of the user |
|
| 437 | + * @param string $name the Nextcloud name in question |
|
| 438 | + * @return string|false with the LDAP DN on success, otherwise false |
|
| 439 | + */ |
|
| 440 | + public function username2dn($name) { |
|
| 441 | + $fdn = $this->userMapper->getDNByName($name); |
|
| 442 | + |
|
| 443 | + //Check whether the DN belongs to the Base, to avoid issues on multi- |
|
| 444 | + //server setups |
|
| 445 | + if(is_string($fdn) && $this->isDNPartOfBase($fdn, $this->connection->ldapBaseUsers)) { |
|
| 446 | + return $fdn; |
|
| 447 | + } |
|
| 448 | + |
|
| 449 | + return false; |
|
| 450 | + } |
|
| 451 | + |
|
| 452 | + /** |
|
| 453 | + * returns the internal Nextcloud name for the given LDAP DN of the group, false on DN outside of search DN or failure |
|
| 454 | + * @param string $fdn the dn of the group object |
|
| 455 | + * @param string $ldapName optional, the display name of the object |
|
| 456 | + * @return string|false with the name to use in Nextcloud, false on DN outside of search DN |
|
| 457 | + */ |
|
| 458 | + public function dn2groupname($fdn, $ldapName = null) { |
|
| 459 | + //To avoid bypassing the base DN settings under certain circumstances |
|
| 460 | + //with the group support, check whether the provided DN matches one of |
|
| 461 | + //the given Bases |
|
| 462 | + if(!$this->isDNPartOfBase($fdn, $this->connection->ldapBaseGroups)) { |
|
| 463 | + return false; |
|
| 464 | + } |
|
| 465 | + |
|
| 466 | + return $this->dn2ocname($fdn, $ldapName, false); |
|
| 467 | + } |
|
| 468 | + |
|
| 469 | + /** |
|
| 470 | + * accepts an array of group DNs and tests whether they match the user |
|
| 471 | + * filter by doing read operations against the group entries. Returns an |
|
| 472 | + * array of DNs that match the filter. |
|
| 473 | + * |
|
| 474 | + * @param string[] $groupDNs |
|
| 475 | + * @return string[] |
|
| 476 | + */ |
|
| 477 | + public function groupsMatchFilter($groupDNs) { |
|
| 478 | + $validGroupDNs = []; |
|
| 479 | + foreach($groupDNs as $dn) { |
|
| 480 | + $cacheKey = 'groupsMatchFilter-'.$dn; |
|
| 481 | + $groupMatchFilter = $this->connection->getFromCache($cacheKey); |
|
| 482 | + if(!is_null($groupMatchFilter)) { |
|
| 483 | + if($groupMatchFilter) { |
|
| 484 | + $validGroupDNs[] = $dn; |
|
| 485 | + } |
|
| 486 | + continue; |
|
| 487 | + } |
|
| 488 | + |
|
| 489 | + // Check the base DN first. If this is not met already, we don't |
|
| 490 | + // need to ask the server at all. |
|
| 491 | + if(!$this->isDNPartOfBase($dn, $this->connection->ldapBaseGroups)) { |
|
| 492 | + $this->connection->writeToCache($cacheKey, false); |
|
| 493 | + continue; |
|
| 494 | + } |
|
| 495 | + |
|
| 496 | + $result = $this->readAttribute($dn, 'cn', $this->connection->ldapGroupFilter); |
|
| 497 | + if(is_array($result)) { |
|
| 498 | + $this->connection->writeToCache($cacheKey, true); |
|
| 499 | + $validGroupDNs[] = $dn; |
|
| 500 | + } else { |
|
| 501 | + $this->connection->writeToCache($cacheKey, false); |
|
| 502 | + } |
|
| 503 | + |
|
| 504 | + } |
|
| 505 | + return $validGroupDNs; |
|
| 506 | + } |
|
| 507 | + |
|
| 508 | + /** |
|
| 509 | + * returns the internal Nextcloud name for the given LDAP DN of the user, false on DN outside of search DN or failure |
|
| 510 | + * @param string $dn the dn of the user object |
|
| 511 | + * @param string $ldapName optional, the display name of the object |
|
| 512 | + * @return string|false with with the name to use in Nextcloud |
|
| 513 | + */ |
|
| 514 | + public function dn2username($fdn, $ldapName = null) { |
|
| 515 | + //To avoid bypassing the base DN settings under certain circumstances |
|
| 516 | + //with the group support, check whether the provided DN matches one of |
|
| 517 | + //the given Bases |
|
| 518 | + if(!$this->isDNPartOfBase($fdn, $this->connection->ldapBaseUsers)) { |
|
| 519 | + return false; |
|
| 520 | + } |
|
| 521 | + |
|
| 522 | + return $this->dn2ocname($fdn, $ldapName, true); |
|
| 523 | + } |
|
| 524 | + |
|
| 525 | + /** |
|
| 526 | + * returns an internal Nextcloud name for the given LDAP DN, false on DN outside of search DN |
|
| 527 | + * |
|
| 528 | + * @param string $fdn the dn of the user object |
|
| 529 | + * @param string|null $ldapName optional, the display name of the object |
|
| 530 | + * @param bool $isUser optional, whether it is a user object (otherwise group assumed) |
|
| 531 | + * @param bool|null $newlyMapped |
|
| 532 | + * @param array|null $record |
|
| 533 | + * @return false|string with with the name to use in Nextcloud |
|
| 534 | + * @throws \Exception |
|
| 535 | + */ |
|
| 536 | + public function dn2ocname($fdn, $ldapName = null, $isUser = true, &$newlyMapped = null, array $record = null) { |
|
| 537 | + $newlyMapped = false; |
|
| 538 | + if($isUser) { |
|
| 539 | + $mapper = $this->getUserMapper(); |
|
| 540 | + $nameAttribute = $this->connection->ldapUserDisplayName; |
|
| 541 | + } else { |
|
| 542 | + $mapper = $this->getGroupMapper(); |
|
| 543 | + $nameAttribute = $this->connection->ldapGroupDisplayName; |
|
| 544 | + } |
|
| 545 | + |
|
| 546 | + //let's try to retrieve the Nextcloud name from the mappings table |
|
| 547 | + $ncName = $mapper->getNameByDN($fdn); |
|
| 548 | + if(is_string($ncName)) { |
|
| 549 | + return $ncName; |
|
| 550 | + } |
|
| 551 | + |
|
| 552 | + //second try: get the UUID and check if it is known. Then, update the DN and return the name. |
|
| 553 | + $uuid = $this->getUUID($fdn, $isUser, $record); |
|
| 554 | + if(is_string($uuid)) { |
|
| 555 | + $ncName = $mapper->getNameByUUID($uuid); |
|
| 556 | + if(is_string($ncName)) { |
|
| 557 | + $mapper->setDNbyUUID($fdn, $uuid); |
|
| 558 | + return $ncName; |
|
| 559 | + } |
|
| 560 | + } else { |
|
| 561 | + //If the UUID can't be detected something is foul. |
|
| 562 | + \OCP\Util::writeLog('user_ldap', 'Cannot determine UUID for '.$fdn.'. Skipping.', \OCP\Util::INFO); |
|
| 563 | + return false; |
|
| 564 | + } |
|
| 565 | + |
|
| 566 | + if(is_null($ldapName)) { |
|
| 567 | + $ldapName = $this->readAttribute($fdn, $nameAttribute); |
|
| 568 | + if(!isset($ldapName[0]) && empty($ldapName[0])) { |
|
| 569 | + \OCP\Util::writeLog('user_ldap', 'No or empty name for '.$fdn.'.', \OCP\Util::INFO); |
|
| 570 | + return false; |
|
| 571 | + } |
|
| 572 | + $ldapName = $ldapName[0]; |
|
| 573 | + } |
|
| 574 | + |
|
| 575 | + if($isUser) { |
|
| 576 | + $usernameAttribute = strval($this->connection->ldapExpertUsernameAttr); |
|
| 577 | + if ($usernameAttribute !== '') { |
|
| 578 | + $username = $this->readAttribute($fdn, $usernameAttribute); |
|
| 579 | + $username = $username[0]; |
|
| 580 | + } else { |
|
| 581 | + $username = $uuid; |
|
| 582 | + } |
|
| 583 | + $intName = $this->sanitizeUsername($username); |
|
| 584 | + } else { |
|
| 585 | + $intName = $ldapName; |
|
| 586 | + } |
|
| 587 | + |
|
| 588 | + //a new user/group! Add it only if it doesn't conflict with other backend's users or existing groups |
|
| 589 | + //disabling Cache is required to avoid that the new user is cached as not-existing in fooExists check |
|
| 590 | + //NOTE: mind, disabling cache affects only this instance! Using it |
|
| 591 | + // outside of core user management will still cache the user as non-existing. |
|
| 592 | + $originalTTL = $this->connection->ldapCacheTTL; |
|
| 593 | + $this->connection->setConfiguration(array('ldapCacheTTL' => 0)); |
|
| 594 | + if(($isUser && $intName !== '' && !\OC::$server->getUserManager()->userExists($intName)) |
|
| 595 | + || (!$isUser && !\OC::$server->getGroupManager()->groupExists($intName))) { |
|
| 596 | + if($mapper->map($fdn, $intName, $uuid)) { |
|
| 597 | + $this->connection->setConfiguration(array('ldapCacheTTL' => $originalTTL)); |
|
| 598 | + $newlyMapped = true; |
|
| 599 | + return $intName; |
|
| 600 | + } |
|
| 601 | + } |
|
| 602 | + $this->connection->setConfiguration(array('ldapCacheTTL' => $originalTTL)); |
|
| 603 | + |
|
| 604 | + $altName = $this->createAltInternalOwnCloudName($intName, $isUser); |
|
| 605 | + if(is_string($altName) && $mapper->map($fdn, $altName, $uuid)) { |
|
| 606 | + $newlyMapped = true; |
|
| 607 | + return $altName; |
|
| 608 | + } |
|
| 609 | + |
|
| 610 | + //if everything else did not help.. |
|
| 611 | + \OCP\Util::writeLog('user_ldap', 'Could not create unique name for '.$fdn.'.', \OCP\Util::INFO); |
|
| 612 | + return false; |
|
| 613 | + } |
|
| 614 | + |
|
| 615 | + /** |
|
| 616 | + * gives back the user names as they are used ownClod internally |
|
| 617 | + * @param array $ldapUsers as returned by fetchList() |
|
| 618 | + * @return array an array with the user names to use in Nextcloud |
|
| 619 | + * |
|
| 620 | + * gives back the user names as they are used ownClod internally |
|
| 621 | + */ |
|
| 622 | + public function nextcloudUserNames($ldapUsers) { |
|
| 623 | + return $this->ldap2NextcloudNames($ldapUsers, true); |
|
| 624 | + } |
|
| 625 | + |
|
| 626 | + /** |
|
| 627 | + * gives back the group names as they are used ownClod internally |
|
| 628 | + * @param array $ldapGroups as returned by fetchList() |
|
| 629 | + * @return array an array with the group names to use in Nextcloud |
|
| 630 | + * |
|
| 631 | + * gives back the group names as they are used ownClod internally |
|
| 632 | + */ |
|
| 633 | + public function nextcloudGroupNames($ldapGroups) { |
|
| 634 | + return $this->ldap2NextcloudNames($ldapGroups, false); |
|
| 635 | + } |
|
| 636 | + |
|
| 637 | + /** |
|
| 638 | + * @param array $ldapObjects as returned by fetchList() |
|
| 639 | + * @param bool $isUsers |
|
| 640 | + * @return array |
|
| 641 | + */ |
|
| 642 | + private function ldap2NextcloudNames($ldapObjects, $isUsers) { |
|
| 643 | + if($isUsers) { |
|
| 644 | + $nameAttribute = $this->connection->ldapUserDisplayName; |
|
| 645 | + $sndAttribute = $this->connection->ldapUserDisplayName2; |
|
| 646 | + } else { |
|
| 647 | + $nameAttribute = $this->connection->ldapGroupDisplayName; |
|
| 648 | + } |
|
| 649 | + $nextcloudNames = array(); |
|
| 650 | + |
|
| 651 | + foreach($ldapObjects as $ldapObject) { |
|
| 652 | + $nameByLDAP = null; |
|
| 653 | + if( isset($ldapObject[$nameAttribute]) |
|
| 654 | + && is_array($ldapObject[$nameAttribute]) |
|
| 655 | + && isset($ldapObject[$nameAttribute][0]) |
|
| 656 | + ) { |
|
| 657 | + // might be set, but not necessarily. if so, we use it. |
|
| 658 | + $nameByLDAP = $ldapObject[$nameAttribute][0]; |
|
| 659 | + } |
|
| 660 | + |
|
| 661 | + $ncName = $this->dn2ocname($ldapObject['dn'][0], $nameByLDAP, $isUsers); |
|
| 662 | + if($ncName) { |
|
| 663 | + $nextcloudNames[] = $ncName; |
|
| 664 | + if($isUsers) { |
|
| 665 | + //cache the user names so it does not need to be retrieved |
|
| 666 | + //again later (e.g. sharing dialogue). |
|
| 667 | + if(is_null($nameByLDAP)) { |
|
| 668 | + continue; |
|
| 669 | + } |
|
| 670 | + $sndName = isset($ldapObject[$sndAttribute][0]) |
|
| 671 | + ? $ldapObject[$sndAttribute][0] : ''; |
|
| 672 | + $this->cacheUserDisplayName($ncName, $nameByLDAP, $sndName); |
|
| 673 | + } |
|
| 674 | + } |
|
| 675 | + } |
|
| 676 | + return $nextcloudNames; |
|
| 677 | + } |
|
| 678 | + |
|
| 679 | + /** |
|
| 680 | + * caches the user display name |
|
| 681 | + * @param string $ocName the internal Nextcloud username |
|
| 682 | + * @param string|false $home the home directory path |
|
| 683 | + */ |
|
| 684 | + public function cacheUserHome($ocName, $home) { |
|
| 685 | + $cacheKey = 'getHome'.$ocName; |
|
| 686 | + $this->connection->writeToCache($cacheKey, $home); |
|
| 687 | + } |
|
| 688 | + |
|
| 689 | + /** |
|
| 690 | + * caches a user as existing |
|
| 691 | + * @param string $ocName the internal Nextcloud username |
|
| 692 | + */ |
|
| 693 | + public function cacheUserExists($ocName) { |
|
| 694 | + $this->connection->writeToCache('userExists'.$ocName, true); |
|
| 695 | + } |
|
| 696 | + |
|
| 697 | + /** |
|
| 698 | + * caches the user display name |
|
| 699 | + * @param string $ocName the internal Nextcloud username |
|
| 700 | + * @param string $displayName the display name |
|
| 701 | + * @param string $displayName2 the second display name |
|
| 702 | + */ |
|
| 703 | + public function cacheUserDisplayName($ocName, $displayName, $displayName2 = '') { |
|
| 704 | + $user = $this->userManager->get($ocName); |
|
| 705 | + if($user === null) { |
|
| 706 | + return; |
|
| 707 | + } |
|
| 708 | + $displayName = $user->composeAndStoreDisplayName($displayName, $displayName2); |
|
| 709 | + $cacheKeyTrunk = 'getDisplayName'; |
|
| 710 | + $this->connection->writeToCache($cacheKeyTrunk.$ocName, $displayName); |
|
| 711 | + } |
|
| 712 | + |
|
| 713 | + /** |
|
| 714 | + * creates a unique name for internal Nextcloud use for users. Don't call it directly. |
|
| 715 | + * @param string $name the display name of the object |
|
| 716 | + * @return string|false with with the name to use in Nextcloud or false if unsuccessful |
|
| 717 | + * |
|
| 718 | + * Instead of using this method directly, call |
|
| 719 | + * createAltInternalOwnCloudName($name, true) |
|
| 720 | + */ |
|
| 721 | + private function _createAltInternalOwnCloudNameForUsers($name) { |
|
| 722 | + $attempts = 0; |
|
| 723 | + //while loop is just a precaution. If a name is not generated within |
|
| 724 | + //20 attempts, something else is very wrong. Avoids infinite loop. |
|
| 725 | + while($attempts < 20){ |
|
| 726 | + $altName = $name . '_' . rand(1000,9999); |
|
| 727 | + if(!\OC::$server->getUserManager()->userExists($altName)) { |
|
| 728 | + return $altName; |
|
| 729 | + } |
|
| 730 | + $attempts++; |
|
| 731 | + } |
|
| 732 | + return false; |
|
| 733 | + } |
|
| 734 | + |
|
| 735 | + /** |
|
| 736 | + * creates a unique name for internal Nextcloud use for groups. Don't call it directly. |
|
| 737 | + * @param string $name the display name of the object |
|
| 738 | + * @return string|false with with the name to use in Nextcloud or false if unsuccessful. |
|
| 739 | + * |
|
| 740 | + * Instead of using this method directly, call |
|
| 741 | + * createAltInternalOwnCloudName($name, false) |
|
| 742 | + * |
|
| 743 | + * Group names are also used as display names, so we do a sequential |
|
| 744 | + * numbering, e.g. Developers_42 when there are 41 other groups called |
|
| 745 | + * "Developers" |
|
| 746 | + */ |
|
| 747 | + private function _createAltInternalOwnCloudNameForGroups($name) { |
|
| 748 | + $usedNames = $this->groupMapper->getNamesBySearch($name, "", '_%'); |
|
| 749 | + if(!($usedNames) || count($usedNames) === 0) { |
|
| 750 | + $lastNo = 1; //will become name_2 |
|
| 751 | + } else { |
|
| 752 | + natsort($usedNames); |
|
| 753 | + $lastName = array_pop($usedNames); |
|
| 754 | + $lastNo = intval(substr($lastName, strrpos($lastName, '_') + 1)); |
|
| 755 | + } |
|
| 756 | + $altName = $name.'_'.strval($lastNo+1); |
|
| 757 | + unset($usedNames); |
|
| 758 | + |
|
| 759 | + $attempts = 1; |
|
| 760 | + while($attempts < 21){ |
|
| 761 | + // Check to be really sure it is unique |
|
| 762 | + // while loop is just a precaution. If a name is not generated within |
|
| 763 | + // 20 attempts, something else is very wrong. Avoids infinite loop. |
|
| 764 | + if(!\OC::$server->getGroupManager()->groupExists($altName)) { |
|
| 765 | + return $altName; |
|
| 766 | + } |
|
| 767 | + $altName = $name . '_' . ($lastNo + $attempts); |
|
| 768 | + $attempts++; |
|
| 769 | + } |
|
| 770 | + return false; |
|
| 771 | + } |
|
| 772 | + |
|
| 773 | + /** |
|
| 774 | + * creates a unique name for internal Nextcloud use. |
|
| 775 | + * @param string $name the display name of the object |
|
| 776 | + * @param boolean $isUser whether name should be created for a user (true) or a group (false) |
|
| 777 | + * @return string|false with with the name to use in Nextcloud or false if unsuccessful |
|
| 778 | + */ |
|
| 779 | + private function createAltInternalOwnCloudName($name, $isUser) { |
|
| 780 | + $originalTTL = $this->connection->ldapCacheTTL; |
|
| 781 | + $this->connection->setConfiguration(array('ldapCacheTTL' => 0)); |
|
| 782 | + if($isUser) { |
|
| 783 | + $altName = $this->_createAltInternalOwnCloudNameForUsers($name); |
|
| 784 | + } else { |
|
| 785 | + $altName = $this->_createAltInternalOwnCloudNameForGroups($name); |
|
| 786 | + } |
|
| 787 | + $this->connection->setConfiguration(array('ldapCacheTTL' => $originalTTL)); |
|
| 788 | + |
|
| 789 | + return $altName; |
|
| 790 | + } |
|
| 791 | + |
|
| 792 | + /** |
|
| 793 | + * fetches a list of users according to a provided loginName and utilizing |
|
| 794 | + * the login filter. |
|
| 795 | + * |
|
| 796 | + * @param string $loginName |
|
| 797 | + * @param array $attributes optional, list of attributes to read |
|
| 798 | + * @return array |
|
| 799 | + */ |
|
| 800 | + public function fetchUsersByLoginName($loginName, $attributes = array('dn')) { |
|
| 801 | + $loginName = $this->escapeFilterPart($loginName); |
|
| 802 | + $filter = str_replace('%uid', $loginName, $this->connection->ldapLoginFilter); |
|
| 803 | + $users = $this->fetchListOfUsers($filter, $attributes); |
|
| 804 | + return $users; |
|
| 805 | + } |
|
| 806 | + |
|
| 807 | + /** |
|
| 808 | + * counts the number of users according to a provided loginName and |
|
| 809 | + * utilizing the login filter. |
|
| 810 | + * |
|
| 811 | + * @param string $loginName |
|
| 812 | + * @return int |
|
| 813 | + */ |
|
| 814 | + public function countUsersByLoginName($loginName) { |
|
| 815 | + $loginName = $this->escapeFilterPart($loginName); |
|
| 816 | + $filter = str_replace('%uid', $loginName, $this->connection->ldapLoginFilter); |
|
| 817 | + $users = $this->countUsers($filter); |
|
| 818 | + return $users; |
|
| 819 | + } |
|
| 820 | + |
|
| 821 | + /** |
|
| 822 | + * @param string $filter |
|
| 823 | + * @param string|string[] $attr |
|
| 824 | + * @param int $limit |
|
| 825 | + * @param int $offset |
|
| 826 | + * @param bool $forceApplyAttributes |
|
| 827 | + * @return array |
|
| 828 | + */ |
|
| 829 | + public function fetchListOfUsers($filter, $attr, $limit = null, $offset = null, $forceApplyAttributes = false) { |
|
| 830 | + $ldapRecords = $this->searchUsers($filter, $attr, $limit, $offset); |
|
| 831 | + $recordsToUpdate = $ldapRecords; |
|
| 832 | + if(!$forceApplyAttributes) { |
|
| 833 | + $isBackgroundJobModeAjax = $this->config |
|
| 834 | + ->getAppValue('core', 'backgroundjobs_mode', 'ajax') === 'ajax'; |
|
| 835 | + $recordsToUpdate = array_filter($ldapRecords, function($record) use ($isBackgroundJobModeAjax) { |
|
| 836 | + $newlyMapped = false; |
|
| 837 | + $uid = $this->dn2ocname($record['dn'][0], null, true, $newlyMapped, $record); |
|
| 838 | + if(is_string($uid)) { |
|
| 839 | + $this->cacheUserExists($uid); |
|
| 840 | + } |
|
| 841 | + return ($uid !== false) && ($newlyMapped || $isBackgroundJobModeAjax); |
|
| 842 | + }); |
|
| 843 | + } |
|
| 844 | + $this->batchApplyUserAttributes($recordsToUpdate); |
|
| 845 | + return $this->fetchList($ldapRecords, (count($attr) > 1)); |
|
| 846 | + } |
|
| 847 | + |
|
| 848 | + /** |
|
| 849 | + * provided with an array of LDAP user records the method will fetch the |
|
| 850 | + * user object and requests it to process the freshly fetched attributes and |
|
| 851 | + * and their values |
|
| 852 | + * @param array $ldapRecords |
|
| 853 | + */ |
|
| 854 | + public function batchApplyUserAttributes(array $ldapRecords){ |
|
| 855 | + $displayNameAttribute = strtolower($this->connection->ldapUserDisplayName); |
|
| 856 | + foreach($ldapRecords as $userRecord) { |
|
| 857 | + if(!isset($userRecord[$displayNameAttribute])) { |
|
| 858 | + // displayName is obligatory |
|
| 859 | + continue; |
|
| 860 | + } |
|
| 861 | + $ocName = $this->dn2ocname($userRecord['dn'][0], null, true); |
|
| 862 | + if($ocName === false) { |
|
| 863 | + continue; |
|
| 864 | + } |
|
| 865 | + $user = $this->userManager->get($ocName); |
|
| 866 | + if($user instanceof OfflineUser) { |
|
| 867 | + $user->unmark(); |
|
| 868 | + $user = $this->userManager->get($ocName); |
|
| 869 | + } |
|
| 870 | + if ($user !== null) { |
|
| 871 | + $user->processAttributes($userRecord); |
|
| 872 | + } else { |
|
| 873 | + \OC::$server->getLogger()->debug( |
|
| 874 | + "The ldap user manager returned null for $ocName", |
|
| 875 | + ['app'=>'user_ldap'] |
|
| 876 | + ); |
|
| 877 | + } |
|
| 878 | + } |
|
| 879 | + } |
|
| 880 | + |
|
| 881 | + /** |
|
| 882 | + * @param string $filter |
|
| 883 | + * @param string|string[] $attr |
|
| 884 | + * @param int $limit |
|
| 885 | + * @param int $offset |
|
| 886 | + * @return array |
|
| 887 | + */ |
|
| 888 | + public function fetchListOfGroups($filter, $attr, $limit = null, $offset = null) { |
|
| 889 | + return $this->fetchList($this->searchGroups($filter, $attr, $limit, $offset), (count($attr) > 1)); |
|
| 890 | + } |
|
| 891 | + |
|
| 892 | + /** |
|
| 893 | + * @param array $list |
|
| 894 | + * @param bool $manyAttributes |
|
| 895 | + * @return array |
|
| 896 | + */ |
|
| 897 | + private function fetchList($list, $manyAttributes) { |
|
| 898 | + if(is_array($list)) { |
|
| 899 | + if($manyAttributes) { |
|
| 900 | + return $list; |
|
| 901 | + } else { |
|
| 902 | + $list = array_reduce($list, function($carry, $item) { |
|
| 903 | + $attribute = array_keys($item)[0]; |
|
| 904 | + $carry[] = $item[$attribute][0]; |
|
| 905 | + return $carry; |
|
| 906 | + }, array()); |
|
| 907 | + return array_unique($list, SORT_LOCALE_STRING); |
|
| 908 | + } |
|
| 909 | + } |
|
| 910 | + |
|
| 911 | + //error cause actually, maybe throw an exception in future. |
|
| 912 | + return array(); |
|
| 913 | + } |
|
| 914 | + |
|
| 915 | + /** |
|
| 916 | + * executes an LDAP search, optimized for Users |
|
| 917 | + * @param string $filter the LDAP filter for the search |
|
| 918 | + * @param string|string[] $attr optional, when a certain attribute shall be filtered out |
|
| 919 | + * @param integer $limit |
|
| 920 | + * @param integer $offset |
|
| 921 | + * @return array with the search result |
|
| 922 | + * |
|
| 923 | + * Executes an LDAP search |
|
| 924 | + */ |
|
| 925 | + public function searchUsers($filter, $attr = null, $limit = null, $offset = null) { |
|
| 926 | + return $this->search($filter, $this->connection->ldapBaseUsers, $attr, $limit, $offset); |
|
| 927 | + } |
|
| 928 | + |
|
| 929 | + /** |
|
| 930 | + * @param string $filter |
|
| 931 | + * @param string|string[] $attr |
|
| 932 | + * @param int $limit |
|
| 933 | + * @param int $offset |
|
| 934 | + * @return false|int |
|
| 935 | + */ |
|
| 936 | + public function countUsers($filter, $attr = array('dn'), $limit = null, $offset = null) { |
|
| 937 | + return $this->count($filter, $this->connection->ldapBaseUsers, $attr, $limit, $offset); |
|
| 938 | + } |
|
| 939 | + |
|
| 940 | + /** |
|
| 941 | + * executes an LDAP search, optimized for Groups |
|
| 942 | + * @param string $filter the LDAP filter for the search |
|
| 943 | + * @param string|string[] $attr optional, when a certain attribute shall be filtered out |
|
| 944 | + * @param integer $limit |
|
| 945 | + * @param integer $offset |
|
| 946 | + * @return array with the search result |
|
| 947 | + * |
|
| 948 | + * Executes an LDAP search |
|
| 949 | + */ |
|
| 950 | + public function searchGroups($filter, $attr = null, $limit = null, $offset = null) { |
|
| 951 | + return $this->search($filter, $this->connection->ldapBaseGroups, $attr, $limit, $offset); |
|
| 952 | + } |
|
| 953 | + |
|
| 954 | + /** |
|
| 955 | + * returns the number of available groups |
|
| 956 | + * @param string $filter the LDAP search filter |
|
| 957 | + * @param string[] $attr optional |
|
| 958 | + * @param int|null $limit |
|
| 959 | + * @param int|null $offset |
|
| 960 | + * @return int|bool |
|
| 961 | + */ |
|
| 962 | + public function countGroups($filter, $attr = array('dn'), $limit = null, $offset = null) { |
|
| 963 | + return $this->count($filter, $this->connection->ldapBaseGroups, $attr, $limit, $offset); |
|
| 964 | + } |
|
| 965 | + |
|
| 966 | + /** |
|
| 967 | + * returns the number of available objects on the base DN |
|
| 968 | + * |
|
| 969 | + * @param int|null $limit |
|
| 970 | + * @param int|null $offset |
|
| 971 | + * @return int|bool |
|
| 972 | + */ |
|
| 973 | + public function countObjects($limit = null, $offset = null) { |
|
| 974 | + return $this->count('objectclass=*', $this->connection->ldapBase, array('dn'), $limit, $offset); |
|
| 975 | + } |
|
| 976 | + |
|
| 977 | + /** |
|
| 978 | + * Returns the LDAP handler |
|
| 979 | + * @throws \OC\ServerNotAvailableException |
|
| 980 | + */ |
|
| 981 | + |
|
| 982 | + /** |
|
| 983 | + * @return mixed |
|
| 984 | + * @throws \OC\ServerNotAvailableException |
|
| 985 | + */ |
|
| 986 | + private function invokeLDAPMethod() { |
|
| 987 | + $arguments = func_get_args(); |
|
| 988 | + $command = array_shift($arguments); |
|
| 989 | + $cr = array_shift($arguments); |
|
| 990 | + if (!method_exists($this->ldap, $command)) { |
|
| 991 | + return null; |
|
| 992 | + } |
|
| 993 | + array_unshift($arguments, $cr); |
|
| 994 | + // php no longer supports call-time pass-by-reference |
|
| 995 | + // thus cannot support controlPagedResultResponse as the third argument |
|
| 996 | + // is a reference |
|
| 997 | + $doMethod = function () use ($command, &$arguments) { |
|
| 998 | + if ($command == 'controlPagedResultResponse') { |
|
| 999 | + throw new \InvalidArgumentException('Invoker does not support controlPagedResultResponse, call LDAP Wrapper directly instead.'); |
|
| 1000 | + } else { |
|
| 1001 | + return call_user_func_array(array($this->ldap, $command), $arguments); |
|
| 1002 | + } |
|
| 1003 | + }; |
|
| 1004 | + try { |
|
| 1005 | + $ret = $doMethod(); |
|
| 1006 | + } catch (ServerNotAvailableException $e) { |
|
| 1007 | + /* Server connection lost, attempt to reestablish it |
|
| 1008 | 1008 | * Maybe implement exponential backoff? |
| 1009 | 1009 | * This was enough to get solr indexer working which has large delays between LDAP fetches. |
| 1010 | 1010 | */ |
| 1011 | - \OCP\Util::writeLog('user_ldap', "Connection lost on $command, attempting to reestablish.", \OCP\Util::DEBUG); |
|
| 1012 | - $this->connection->resetConnectionResource(); |
|
| 1013 | - $cr = $this->connection->getConnectionResource(); |
|
| 1014 | - |
|
| 1015 | - if(!$this->ldap->isResource($cr)) { |
|
| 1016 | - // Seems like we didn't find any resource. |
|
| 1017 | - \OCP\Util::writeLog('user_ldap', "Could not $command, because resource is missing.", \OCP\Util::DEBUG); |
|
| 1018 | - throw $e; |
|
| 1019 | - } |
|
| 1020 | - |
|
| 1021 | - $arguments[0] = array_pad([], count($arguments[0]), $cr); |
|
| 1022 | - $ret = $doMethod(); |
|
| 1023 | - } |
|
| 1024 | - return $ret; |
|
| 1025 | - } |
|
| 1026 | - |
|
| 1027 | - /** |
|
| 1028 | - * retrieved. Results will according to the order in the array. |
|
| 1029 | - * |
|
| 1030 | - * @param $filter |
|
| 1031 | - * @param $base |
|
| 1032 | - * @param string[]|string|null $attr |
|
| 1033 | - * @param int $limit optional, maximum results to be counted |
|
| 1034 | - * @param int $offset optional, a starting point |
|
| 1035 | - * @return array|false array with the search result as first value and pagedSearchOK as |
|
| 1036 | - * second | false if not successful |
|
| 1037 | - * @throws ServerNotAvailableException |
|
| 1038 | - */ |
|
| 1039 | - private function executeSearch($filter, $base, &$attr = null, $limit = null, $offset = null) { |
|
| 1040 | - if(!is_null($attr) && !is_array($attr)) { |
|
| 1041 | - $attr = array(mb_strtolower($attr, 'UTF-8')); |
|
| 1042 | - } |
|
| 1043 | - |
|
| 1044 | - // See if we have a resource, in case not cancel with message |
|
| 1045 | - $cr = $this->connection->getConnectionResource(); |
|
| 1046 | - if(!$this->ldap->isResource($cr)) { |
|
| 1047 | - // Seems like we didn't find any resource. |
|
| 1048 | - // Return an empty array just like before. |
|
| 1049 | - \OCP\Util::writeLog('user_ldap', 'Could not search, because resource is missing.', \OCP\Util::DEBUG); |
|
| 1050 | - return false; |
|
| 1051 | - } |
|
| 1052 | - |
|
| 1053 | - //check whether paged search should be attempted |
|
| 1054 | - $pagedSearchOK = $this->initPagedSearch($filter, $base, $attr, intval($limit), $offset); |
|
| 1055 | - |
|
| 1056 | - $linkResources = array_pad(array(), count($base), $cr); |
|
| 1057 | - $sr = $this->invokeLDAPMethod('search', $linkResources, $base, $filter, $attr); |
|
| 1058 | - // cannot use $cr anymore, might have changed in the previous call! |
|
| 1059 | - $error = $this->ldap->errno($this->connection->getConnectionResource()); |
|
| 1060 | - if(!is_array($sr) || $error !== 0) { |
|
| 1061 | - \OCP\Util::writeLog('user_ldap', 'Attempt for Paging? '.print_r($pagedSearchOK, true), \OCP\Util::ERROR); |
|
| 1062 | - return false; |
|
| 1063 | - } |
|
| 1064 | - |
|
| 1065 | - return array($sr, $pagedSearchOK); |
|
| 1066 | - } |
|
| 1067 | - |
|
| 1068 | - /** |
|
| 1069 | - * processes an LDAP paged search operation |
|
| 1070 | - * @param array $sr the array containing the LDAP search resources |
|
| 1071 | - * @param string $filter the LDAP filter for the search |
|
| 1072 | - * @param array $base an array containing the LDAP subtree(s) that shall be searched |
|
| 1073 | - * @param int $iFoundItems number of results in the single search operation |
|
| 1074 | - * @param int $limit maximum results to be counted |
|
| 1075 | - * @param int $offset a starting point |
|
| 1076 | - * @param bool $pagedSearchOK whether a paged search has been executed |
|
| 1077 | - * @param bool $skipHandling required for paged search when cookies to |
|
| 1078 | - * prior results need to be gained |
|
| 1079 | - * @return bool cookie validity, true if we have more pages, false otherwise. |
|
| 1080 | - */ |
|
| 1081 | - private function processPagedSearchStatus($sr, $filter, $base, $iFoundItems, $limit, $offset, $pagedSearchOK, $skipHandling) { |
|
| 1082 | - $cookie = null; |
|
| 1083 | - if($pagedSearchOK) { |
|
| 1084 | - $cr = $this->connection->getConnectionResource(); |
|
| 1085 | - foreach($sr as $key => $res) { |
|
| 1086 | - if($this->ldap->controlPagedResultResponse($cr, $res, $cookie)) { |
|
| 1087 | - $this->setPagedResultCookie($base[$key], $filter, $limit, $offset, $cookie); |
|
| 1088 | - } |
|
| 1089 | - } |
|
| 1090 | - |
|
| 1091 | - //browsing through prior pages to get the cookie for the new one |
|
| 1092 | - if($skipHandling) { |
|
| 1093 | - return false; |
|
| 1094 | - } |
|
| 1095 | - // if count is bigger, then the server does not support |
|
| 1096 | - // paged search. Instead, he did a normal search. We set a |
|
| 1097 | - // flag here, so the callee knows how to deal with it. |
|
| 1098 | - if($iFoundItems <= $limit) { |
|
| 1099 | - $this->pagedSearchedSuccessful = true; |
|
| 1100 | - } |
|
| 1101 | - } else { |
|
| 1102 | - if(!is_null($limit) && intval($this->connection->ldapPagingSize) !== 0) { |
|
| 1103 | - \OC::$server->getLogger()->debug( |
|
| 1104 | - 'Paged search was not available', |
|
| 1105 | - [ 'app' => 'user_ldap' ] |
|
| 1106 | - ); |
|
| 1107 | - } |
|
| 1108 | - } |
|
| 1109 | - /* ++ Fixing RHDS searches with pages with zero results ++ |
|
| 1011 | + \OCP\Util::writeLog('user_ldap', "Connection lost on $command, attempting to reestablish.", \OCP\Util::DEBUG); |
|
| 1012 | + $this->connection->resetConnectionResource(); |
|
| 1013 | + $cr = $this->connection->getConnectionResource(); |
|
| 1014 | + |
|
| 1015 | + if(!$this->ldap->isResource($cr)) { |
|
| 1016 | + // Seems like we didn't find any resource. |
|
| 1017 | + \OCP\Util::writeLog('user_ldap', "Could not $command, because resource is missing.", \OCP\Util::DEBUG); |
|
| 1018 | + throw $e; |
|
| 1019 | + } |
|
| 1020 | + |
|
| 1021 | + $arguments[0] = array_pad([], count($arguments[0]), $cr); |
|
| 1022 | + $ret = $doMethod(); |
|
| 1023 | + } |
|
| 1024 | + return $ret; |
|
| 1025 | + } |
|
| 1026 | + |
|
| 1027 | + /** |
|
| 1028 | + * retrieved. Results will according to the order in the array. |
|
| 1029 | + * |
|
| 1030 | + * @param $filter |
|
| 1031 | + * @param $base |
|
| 1032 | + * @param string[]|string|null $attr |
|
| 1033 | + * @param int $limit optional, maximum results to be counted |
|
| 1034 | + * @param int $offset optional, a starting point |
|
| 1035 | + * @return array|false array with the search result as first value and pagedSearchOK as |
|
| 1036 | + * second | false if not successful |
|
| 1037 | + * @throws ServerNotAvailableException |
|
| 1038 | + */ |
|
| 1039 | + private function executeSearch($filter, $base, &$attr = null, $limit = null, $offset = null) { |
|
| 1040 | + if(!is_null($attr) && !is_array($attr)) { |
|
| 1041 | + $attr = array(mb_strtolower($attr, 'UTF-8')); |
|
| 1042 | + } |
|
| 1043 | + |
|
| 1044 | + // See if we have a resource, in case not cancel with message |
|
| 1045 | + $cr = $this->connection->getConnectionResource(); |
|
| 1046 | + if(!$this->ldap->isResource($cr)) { |
|
| 1047 | + // Seems like we didn't find any resource. |
|
| 1048 | + // Return an empty array just like before. |
|
| 1049 | + \OCP\Util::writeLog('user_ldap', 'Could not search, because resource is missing.', \OCP\Util::DEBUG); |
|
| 1050 | + return false; |
|
| 1051 | + } |
|
| 1052 | + |
|
| 1053 | + //check whether paged search should be attempted |
|
| 1054 | + $pagedSearchOK = $this->initPagedSearch($filter, $base, $attr, intval($limit), $offset); |
|
| 1055 | + |
|
| 1056 | + $linkResources = array_pad(array(), count($base), $cr); |
|
| 1057 | + $sr = $this->invokeLDAPMethod('search', $linkResources, $base, $filter, $attr); |
|
| 1058 | + // cannot use $cr anymore, might have changed in the previous call! |
|
| 1059 | + $error = $this->ldap->errno($this->connection->getConnectionResource()); |
|
| 1060 | + if(!is_array($sr) || $error !== 0) { |
|
| 1061 | + \OCP\Util::writeLog('user_ldap', 'Attempt for Paging? '.print_r($pagedSearchOK, true), \OCP\Util::ERROR); |
|
| 1062 | + return false; |
|
| 1063 | + } |
|
| 1064 | + |
|
| 1065 | + return array($sr, $pagedSearchOK); |
|
| 1066 | + } |
|
| 1067 | + |
|
| 1068 | + /** |
|
| 1069 | + * processes an LDAP paged search operation |
|
| 1070 | + * @param array $sr the array containing the LDAP search resources |
|
| 1071 | + * @param string $filter the LDAP filter for the search |
|
| 1072 | + * @param array $base an array containing the LDAP subtree(s) that shall be searched |
|
| 1073 | + * @param int $iFoundItems number of results in the single search operation |
|
| 1074 | + * @param int $limit maximum results to be counted |
|
| 1075 | + * @param int $offset a starting point |
|
| 1076 | + * @param bool $pagedSearchOK whether a paged search has been executed |
|
| 1077 | + * @param bool $skipHandling required for paged search when cookies to |
|
| 1078 | + * prior results need to be gained |
|
| 1079 | + * @return bool cookie validity, true if we have more pages, false otherwise. |
|
| 1080 | + */ |
|
| 1081 | + private function processPagedSearchStatus($sr, $filter, $base, $iFoundItems, $limit, $offset, $pagedSearchOK, $skipHandling) { |
|
| 1082 | + $cookie = null; |
|
| 1083 | + if($pagedSearchOK) { |
|
| 1084 | + $cr = $this->connection->getConnectionResource(); |
|
| 1085 | + foreach($sr as $key => $res) { |
|
| 1086 | + if($this->ldap->controlPagedResultResponse($cr, $res, $cookie)) { |
|
| 1087 | + $this->setPagedResultCookie($base[$key], $filter, $limit, $offset, $cookie); |
|
| 1088 | + } |
|
| 1089 | + } |
|
| 1090 | + |
|
| 1091 | + //browsing through prior pages to get the cookie for the new one |
|
| 1092 | + if($skipHandling) { |
|
| 1093 | + return false; |
|
| 1094 | + } |
|
| 1095 | + // if count is bigger, then the server does not support |
|
| 1096 | + // paged search. Instead, he did a normal search. We set a |
|
| 1097 | + // flag here, so the callee knows how to deal with it. |
|
| 1098 | + if($iFoundItems <= $limit) { |
|
| 1099 | + $this->pagedSearchedSuccessful = true; |
|
| 1100 | + } |
|
| 1101 | + } else { |
|
| 1102 | + if(!is_null($limit) && intval($this->connection->ldapPagingSize) !== 0) { |
|
| 1103 | + \OC::$server->getLogger()->debug( |
|
| 1104 | + 'Paged search was not available', |
|
| 1105 | + [ 'app' => 'user_ldap' ] |
|
| 1106 | + ); |
|
| 1107 | + } |
|
| 1108 | + } |
|
| 1109 | + /* ++ Fixing RHDS searches with pages with zero results ++ |
|
| 1110 | 1110 | * Return cookie status. If we don't have more pages, with RHDS |
| 1111 | 1111 | * cookie is null, with openldap cookie is an empty string and |
| 1112 | 1112 | * to 386ds '0' is a valid cookie. Even if $iFoundItems == 0 |
| 1113 | 1113 | */ |
| 1114 | - return !empty($cookie) || $cookie === '0'; |
|
| 1115 | - } |
|
| 1116 | - |
|
| 1117 | - /** |
|
| 1118 | - * executes an LDAP search, but counts the results only |
|
| 1119 | - * |
|
| 1120 | - * @param string $filter the LDAP filter for the search |
|
| 1121 | - * @param array $base an array containing the LDAP subtree(s) that shall be searched |
|
| 1122 | - * @param string|string[] $attr optional, array, one or more attributes that shall be |
|
| 1123 | - * retrieved. Results will according to the order in the array. |
|
| 1124 | - * @param int $limit optional, maximum results to be counted |
|
| 1125 | - * @param int $offset optional, a starting point |
|
| 1126 | - * @param bool $skipHandling indicates whether the pages search operation is |
|
| 1127 | - * completed |
|
| 1128 | - * @return int|false Integer or false if the search could not be initialized |
|
| 1129 | - * @throws ServerNotAvailableException |
|
| 1130 | - */ |
|
| 1131 | - private function count($filter, $base, $attr = null, $limit = null, $offset = null, $skipHandling = false) { |
|
| 1132 | - \OCP\Util::writeLog('user_ldap', 'Count filter: '.print_r($filter, true), \OCP\Util::DEBUG); |
|
| 1133 | - |
|
| 1134 | - $limitPerPage = intval($this->connection->ldapPagingSize); |
|
| 1135 | - if(!is_null($limit) && $limit < $limitPerPage && $limit > 0) { |
|
| 1136 | - $limitPerPage = $limit; |
|
| 1137 | - } |
|
| 1138 | - |
|
| 1139 | - $counter = 0; |
|
| 1140 | - $count = null; |
|
| 1141 | - $this->connection->getConnectionResource(); |
|
| 1142 | - |
|
| 1143 | - do { |
|
| 1144 | - $search = $this->executeSearch($filter, $base, $attr, $limitPerPage, $offset); |
|
| 1145 | - if($search === false) { |
|
| 1146 | - return $counter > 0 ? $counter : false; |
|
| 1147 | - } |
|
| 1148 | - list($sr, $pagedSearchOK) = $search; |
|
| 1149 | - |
|
| 1150 | - /* ++ Fixing RHDS searches with pages with zero results ++ |
|
| 1114 | + return !empty($cookie) || $cookie === '0'; |
|
| 1115 | + } |
|
| 1116 | + |
|
| 1117 | + /** |
|
| 1118 | + * executes an LDAP search, but counts the results only |
|
| 1119 | + * |
|
| 1120 | + * @param string $filter the LDAP filter for the search |
|
| 1121 | + * @param array $base an array containing the LDAP subtree(s) that shall be searched |
|
| 1122 | + * @param string|string[] $attr optional, array, one or more attributes that shall be |
|
| 1123 | + * retrieved. Results will according to the order in the array. |
|
| 1124 | + * @param int $limit optional, maximum results to be counted |
|
| 1125 | + * @param int $offset optional, a starting point |
|
| 1126 | + * @param bool $skipHandling indicates whether the pages search operation is |
|
| 1127 | + * completed |
|
| 1128 | + * @return int|false Integer or false if the search could not be initialized |
|
| 1129 | + * @throws ServerNotAvailableException |
|
| 1130 | + */ |
|
| 1131 | + private function count($filter, $base, $attr = null, $limit = null, $offset = null, $skipHandling = false) { |
|
| 1132 | + \OCP\Util::writeLog('user_ldap', 'Count filter: '.print_r($filter, true), \OCP\Util::DEBUG); |
|
| 1133 | + |
|
| 1134 | + $limitPerPage = intval($this->connection->ldapPagingSize); |
|
| 1135 | + if(!is_null($limit) && $limit < $limitPerPage && $limit > 0) { |
|
| 1136 | + $limitPerPage = $limit; |
|
| 1137 | + } |
|
| 1138 | + |
|
| 1139 | + $counter = 0; |
|
| 1140 | + $count = null; |
|
| 1141 | + $this->connection->getConnectionResource(); |
|
| 1142 | + |
|
| 1143 | + do { |
|
| 1144 | + $search = $this->executeSearch($filter, $base, $attr, $limitPerPage, $offset); |
|
| 1145 | + if($search === false) { |
|
| 1146 | + return $counter > 0 ? $counter : false; |
|
| 1147 | + } |
|
| 1148 | + list($sr, $pagedSearchOK) = $search; |
|
| 1149 | + |
|
| 1150 | + /* ++ Fixing RHDS searches with pages with zero results ++ |
|
| 1151 | 1151 | * countEntriesInSearchResults() method signature changed |
| 1152 | 1152 | * by removing $limit and &$hasHitLimit parameters |
| 1153 | 1153 | */ |
| 1154 | - $count = $this->countEntriesInSearchResults($sr); |
|
| 1155 | - $counter += $count; |
|
| 1154 | + $count = $this->countEntriesInSearchResults($sr); |
|
| 1155 | + $counter += $count; |
|
| 1156 | 1156 | |
| 1157 | - $hasMorePages = $this->processPagedSearchStatus($sr, $filter, $base, $count, $limitPerPage, |
|
| 1158 | - $offset, $pagedSearchOK, $skipHandling); |
|
| 1159 | - $offset += $limitPerPage; |
|
| 1160 | - /* ++ Fixing RHDS searches with pages with zero results ++ |
|
| 1157 | + $hasMorePages = $this->processPagedSearchStatus($sr, $filter, $base, $count, $limitPerPage, |
|
| 1158 | + $offset, $pagedSearchOK, $skipHandling); |
|
| 1159 | + $offset += $limitPerPage; |
|
| 1160 | + /* ++ Fixing RHDS searches with pages with zero results ++ |
|
| 1161 | 1161 | * Continue now depends on $hasMorePages value |
| 1162 | 1162 | */ |
| 1163 | - $continue = $pagedSearchOK && $hasMorePages; |
|
| 1164 | - } while($continue && (is_null($limit) || $limit <= 0 || $limit > $counter)); |
|
| 1165 | - |
|
| 1166 | - return $counter; |
|
| 1167 | - } |
|
| 1168 | - |
|
| 1169 | - /** |
|
| 1170 | - * @param array $searchResults |
|
| 1171 | - * @return int |
|
| 1172 | - */ |
|
| 1173 | - private function countEntriesInSearchResults($searchResults) { |
|
| 1174 | - $counter = 0; |
|
| 1175 | - |
|
| 1176 | - foreach($searchResults as $res) { |
|
| 1177 | - $count = intval($this->invokeLDAPMethod('countEntries', $this->connection->getConnectionResource(), $res)); |
|
| 1178 | - $counter += $count; |
|
| 1179 | - } |
|
| 1180 | - |
|
| 1181 | - return $counter; |
|
| 1182 | - } |
|
| 1183 | - |
|
| 1184 | - /** |
|
| 1185 | - * Executes an LDAP search |
|
| 1186 | - * |
|
| 1187 | - * @param string $filter the LDAP filter for the search |
|
| 1188 | - * @param array $base an array containing the LDAP subtree(s) that shall be searched |
|
| 1189 | - * @param string|string[] $attr optional, array, one or more attributes that shall be |
|
| 1190 | - * @param int $limit |
|
| 1191 | - * @param int $offset |
|
| 1192 | - * @param bool $skipHandling |
|
| 1193 | - * @return array with the search result |
|
| 1194 | - * @throws ServerNotAvailableException |
|
| 1195 | - */ |
|
| 1196 | - public function search($filter, $base, $attr = null, $limit = null, $offset = null, $skipHandling = false) { |
|
| 1197 | - $limitPerPage = intval($this->connection->ldapPagingSize); |
|
| 1198 | - if(!is_null($limit) && $limit < $limitPerPage && $limit > 0) { |
|
| 1199 | - $limitPerPage = $limit; |
|
| 1200 | - } |
|
| 1201 | - |
|
| 1202 | - /* ++ Fixing RHDS searches with pages with zero results ++ |
|
| 1163 | + $continue = $pagedSearchOK && $hasMorePages; |
|
| 1164 | + } while($continue && (is_null($limit) || $limit <= 0 || $limit > $counter)); |
|
| 1165 | + |
|
| 1166 | + return $counter; |
|
| 1167 | + } |
|
| 1168 | + |
|
| 1169 | + /** |
|
| 1170 | + * @param array $searchResults |
|
| 1171 | + * @return int |
|
| 1172 | + */ |
|
| 1173 | + private function countEntriesInSearchResults($searchResults) { |
|
| 1174 | + $counter = 0; |
|
| 1175 | + |
|
| 1176 | + foreach($searchResults as $res) { |
|
| 1177 | + $count = intval($this->invokeLDAPMethod('countEntries', $this->connection->getConnectionResource(), $res)); |
|
| 1178 | + $counter += $count; |
|
| 1179 | + } |
|
| 1180 | + |
|
| 1181 | + return $counter; |
|
| 1182 | + } |
|
| 1183 | + |
|
| 1184 | + /** |
|
| 1185 | + * Executes an LDAP search |
|
| 1186 | + * |
|
| 1187 | + * @param string $filter the LDAP filter for the search |
|
| 1188 | + * @param array $base an array containing the LDAP subtree(s) that shall be searched |
|
| 1189 | + * @param string|string[] $attr optional, array, one or more attributes that shall be |
|
| 1190 | + * @param int $limit |
|
| 1191 | + * @param int $offset |
|
| 1192 | + * @param bool $skipHandling |
|
| 1193 | + * @return array with the search result |
|
| 1194 | + * @throws ServerNotAvailableException |
|
| 1195 | + */ |
|
| 1196 | + public function search($filter, $base, $attr = null, $limit = null, $offset = null, $skipHandling = false) { |
|
| 1197 | + $limitPerPage = intval($this->connection->ldapPagingSize); |
|
| 1198 | + if(!is_null($limit) && $limit < $limitPerPage && $limit > 0) { |
|
| 1199 | + $limitPerPage = $limit; |
|
| 1200 | + } |
|
| 1201 | + |
|
| 1202 | + /* ++ Fixing RHDS searches with pages with zero results ++ |
|
| 1203 | 1203 | * As we can have pages with zero results and/or pages with less |
| 1204 | 1204 | * than $limit results but with a still valid server 'cookie', |
| 1205 | 1205 | * loops through until we get $continue equals true and |
| 1206 | 1206 | * $findings['count'] < $limit |
| 1207 | 1207 | */ |
| 1208 | - $findings = []; |
|
| 1209 | - $savedoffset = $offset; |
|
| 1210 | - do { |
|
| 1211 | - $search = $this->executeSearch($filter, $base, $attr, $limitPerPage, $offset); |
|
| 1212 | - if($search === false) { |
|
| 1213 | - return []; |
|
| 1214 | - } |
|
| 1215 | - list($sr, $pagedSearchOK) = $search; |
|
| 1216 | - $cr = $this->connection->getConnectionResource(); |
|
| 1217 | - |
|
| 1218 | - if($skipHandling) { |
|
| 1219 | - //i.e. result do not need to be fetched, we just need the cookie |
|
| 1220 | - //thus pass 1 or any other value as $iFoundItems because it is not |
|
| 1221 | - //used |
|
| 1222 | - $this->processPagedSearchStatus($sr, $filter, $base, 1, $limitPerPage, |
|
| 1223 | - $offset, $pagedSearchOK, |
|
| 1224 | - $skipHandling); |
|
| 1225 | - return array(); |
|
| 1226 | - } |
|
| 1227 | - |
|
| 1228 | - $iFoundItems = 0; |
|
| 1229 | - foreach($sr as $res) { |
|
| 1230 | - $findings = array_merge($findings, $this->invokeLDAPMethod('getEntries', $cr, $res)); |
|
| 1231 | - $iFoundItems = max($iFoundItems, $findings['count']); |
|
| 1232 | - unset($findings['count']); |
|
| 1233 | - } |
|
| 1234 | - |
|
| 1235 | - $continue = $this->processPagedSearchStatus($sr, $filter, $base, $iFoundItems, |
|
| 1236 | - $limitPerPage, $offset, $pagedSearchOK, |
|
| 1237 | - $skipHandling); |
|
| 1238 | - $offset += $limitPerPage; |
|
| 1239 | - } while ($continue && $pagedSearchOK && ($limit === null || count($findings) < $limit)); |
|
| 1240 | - // reseting offset |
|
| 1241 | - $offset = $savedoffset; |
|
| 1242 | - |
|
| 1243 | - // if we're here, probably no connection resource is returned. |
|
| 1244 | - // to make Nextcloud behave nicely, we simply give back an empty array. |
|
| 1245 | - if(is_null($findings)) { |
|
| 1246 | - return array(); |
|
| 1247 | - } |
|
| 1248 | - |
|
| 1249 | - if(!is_null($attr)) { |
|
| 1250 | - $selection = []; |
|
| 1251 | - $i = 0; |
|
| 1252 | - foreach($findings as $item) { |
|
| 1253 | - if(!is_array($item)) { |
|
| 1254 | - continue; |
|
| 1255 | - } |
|
| 1256 | - $item = \OCP\Util::mb_array_change_key_case($item, MB_CASE_LOWER, 'UTF-8'); |
|
| 1257 | - foreach($attr as $key) { |
|
| 1258 | - if(isset($item[$key])) { |
|
| 1259 | - if(is_array($item[$key]) && isset($item[$key]['count'])) { |
|
| 1260 | - unset($item[$key]['count']); |
|
| 1261 | - } |
|
| 1262 | - if($key !== 'dn') { |
|
| 1263 | - if($this->resemblesDN($key)) { |
|
| 1264 | - $selection[$i][$key] = $this->helper->sanitizeDN($item[$key]); |
|
| 1265 | - } else if($key === 'objectguid' || $key === 'guid') { |
|
| 1266 | - $selection[$i][$key] = [$this->convertObjectGUID2Str($item[$key][0])]; |
|
| 1267 | - } else { |
|
| 1268 | - $selection[$i][$key] = $item[$key]; |
|
| 1269 | - } |
|
| 1270 | - } else { |
|
| 1271 | - $selection[$i][$key] = [$this->helper->sanitizeDN($item[$key])]; |
|
| 1272 | - } |
|
| 1273 | - } |
|
| 1274 | - |
|
| 1275 | - } |
|
| 1276 | - $i++; |
|
| 1277 | - } |
|
| 1278 | - $findings = $selection; |
|
| 1279 | - } |
|
| 1280 | - //we slice the findings, when |
|
| 1281 | - //a) paged search unsuccessful, though attempted |
|
| 1282 | - //b) no paged search, but limit set |
|
| 1283 | - if((!$this->getPagedSearchResultState() |
|
| 1284 | - && $pagedSearchOK) |
|
| 1285 | - || ( |
|
| 1286 | - !$pagedSearchOK |
|
| 1287 | - && !is_null($limit) |
|
| 1288 | - ) |
|
| 1289 | - ) { |
|
| 1290 | - $findings = array_slice($findings, intval($offset), $limit); |
|
| 1291 | - } |
|
| 1292 | - return $findings; |
|
| 1293 | - } |
|
| 1294 | - |
|
| 1295 | - /** |
|
| 1296 | - * @param string $name |
|
| 1297 | - * @return bool|mixed|string |
|
| 1298 | - */ |
|
| 1299 | - public function sanitizeUsername($name) { |
|
| 1300 | - if($this->connection->ldapIgnoreNamingRules) { |
|
| 1301 | - return trim($name); |
|
| 1302 | - } |
|
| 1303 | - |
|
| 1304 | - // Transliteration |
|
| 1305 | - // latin characters to ASCII |
|
| 1306 | - $name = iconv('UTF-8', 'ASCII//TRANSLIT', $name); |
|
| 1307 | - |
|
| 1308 | - // Replacements |
|
| 1309 | - $name = str_replace(' ', '_', $name); |
|
| 1310 | - |
|
| 1311 | - // Every remaining disallowed characters will be removed |
|
| 1312 | - $name = preg_replace('/[^a-zA-Z0-9_.@-]/u', '', $name); |
|
| 1313 | - |
|
| 1314 | - return $name; |
|
| 1315 | - } |
|
| 1316 | - |
|
| 1317 | - /** |
|
| 1318 | - * escapes (user provided) parts for LDAP filter |
|
| 1319 | - * @param string $input, the provided value |
|
| 1320 | - * @param bool $allowAsterisk whether in * at the beginning should be preserved |
|
| 1321 | - * @return string the escaped string |
|
| 1322 | - */ |
|
| 1323 | - public function escapeFilterPart($input, $allowAsterisk = false) { |
|
| 1324 | - $asterisk = ''; |
|
| 1325 | - if($allowAsterisk && strlen($input) > 0 && $input[0] === '*') { |
|
| 1326 | - $asterisk = '*'; |
|
| 1327 | - $input = mb_substr($input, 1, null, 'UTF-8'); |
|
| 1328 | - } |
|
| 1329 | - $search = array('*', '\\', '(', ')'); |
|
| 1330 | - $replace = array('\\*', '\\\\', '\\(', '\\)'); |
|
| 1331 | - return $asterisk . str_replace($search, $replace, $input); |
|
| 1332 | - } |
|
| 1333 | - |
|
| 1334 | - /** |
|
| 1335 | - * combines the input filters with AND |
|
| 1336 | - * @param string[] $filters the filters to connect |
|
| 1337 | - * @return string the combined filter |
|
| 1338 | - */ |
|
| 1339 | - public function combineFilterWithAnd($filters) { |
|
| 1340 | - return $this->combineFilter($filters, '&'); |
|
| 1341 | - } |
|
| 1342 | - |
|
| 1343 | - /** |
|
| 1344 | - * combines the input filters with OR |
|
| 1345 | - * @param string[] $filters the filters to connect |
|
| 1346 | - * @return string the combined filter |
|
| 1347 | - * Combines Filter arguments with OR |
|
| 1348 | - */ |
|
| 1349 | - public function combineFilterWithOr($filters) { |
|
| 1350 | - return $this->combineFilter($filters, '|'); |
|
| 1351 | - } |
|
| 1352 | - |
|
| 1353 | - /** |
|
| 1354 | - * combines the input filters with given operator |
|
| 1355 | - * @param string[] $filters the filters to connect |
|
| 1356 | - * @param string $operator either & or | |
|
| 1357 | - * @return string the combined filter |
|
| 1358 | - */ |
|
| 1359 | - private function combineFilter($filters, $operator) { |
|
| 1360 | - $combinedFilter = '('.$operator; |
|
| 1361 | - foreach($filters as $filter) { |
|
| 1362 | - if ($filter !== '' && $filter[0] !== '(') { |
|
| 1363 | - $filter = '('.$filter.')'; |
|
| 1364 | - } |
|
| 1365 | - $combinedFilter.=$filter; |
|
| 1366 | - } |
|
| 1367 | - $combinedFilter.=')'; |
|
| 1368 | - return $combinedFilter; |
|
| 1369 | - } |
|
| 1370 | - |
|
| 1371 | - /** |
|
| 1372 | - * creates a filter part for to perform search for users |
|
| 1373 | - * @param string $search the search term |
|
| 1374 | - * @return string the final filter part to use in LDAP searches |
|
| 1375 | - */ |
|
| 1376 | - public function getFilterPartForUserSearch($search) { |
|
| 1377 | - return $this->getFilterPartForSearch($search, |
|
| 1378 | - $this->connection->ldapAttributesForUserSearch, |
|
| 1379 | - $this->connection->ldapUserDisplayName); |
|
| 1380 | - } |
|
| 1381 | - |
|
| 1382 | - /** |
|
| 1383 | - * creates a filter part for to perform search for groups |
|
| 1384 | - * @param string $search the search term |
|
| 1385 | - * @return string the final filter part to use in LDAP searches |
|
| 1386 | - */ |
|
| 1387 | - public function getFilterPartForGroupSearch($search) { |
|
| 1388 | - return $this->getFilterPartForSearch($search, |
|
| 1389 | - $this->connection->ldapAttributesForGroupSearch, |
|
| 1390 | - $this->connection->ldapGroupDisplayName); |
|
| 1391 | - } |
|
| 1392 | - |
|
| 1393 | - /** |
|
| 1394 | - * creates a filter part for searches by splitting up the given search |
|
| 1395 | - * string into single words |
|
| 1396 | - * @param string $search the search term |
|
| 1397 | - * @param string[] $searchAttributes needs to have at least two attributes, |
|
| 1398 | - * otherwise it does not make sense :) |
|
| 1399 | - * @return string the final filter part to use in LDAP searches |
|
| 1400 | - * @throws \Exception |
|
| 1401 | - */ |
|
| 1402 | - private function getAdvancedFilterPartForSearch($search, $searchAttributes) { |
|
| 1403 | - if(!is_array($searchAttributes) || count($searchAttributes) < 2) { |
|
| 1404 | - throw new \Exception('searchAttributes must be an array with at least two string'); |
|
| 1405 | - } |
|
| 1406 | - $searchWords = explode(' ', trim($search)); |
|
| 1407 | - $wordFilters = array(); |
|
| 1408 | - foreach($searchWords as $word) { |
|
| 1409 | - $word = $this->prepareSearchTerm($word); |
|
| 1410 | - //every word needs to appear at least once |
|
| 1411 | - $wordMatchOneAttrFilters = array(); |
|
| 1412 | - foreach($searchAttributes as $attr) { |
|
| 1413 | - $wordMatchOneAttrFilters[] = $attr . '=' . $word; |
|
| 1414 | - } |
|
| 1415 | - $wordFilters[] = $this->combineFilterWithOr($wordMatchOneAttrFilters); |
|
| 1416 | - } |
|
| 1417 | - return $this->combineFilterWithAnd($wordFilters); |
|
| 1418 | - } |
|
| 1419 | - |
|
| 1420 | - /** |
|
| 1421 | - * creates a filter part for searches |
|
| 1422 | - * @param string $search the search term |
|
| 1423 | - * @param string[]|null $searchAttributes |
|
| 1424 | - * @param string $fallbackAttribute a fallback attribute in case the user |
|
| 1425 | - * did not define search attributes. Typically the display name attribute. |
|
| 1426 | - * @return string the final filter part to use in LDAP searches |
|
| 1427 | - */ |
|
| 1428 | - private function getFilterPartForSearch($search, $searchAttributes, $fallbackAttribute) { |
|
| 1429 | - $filter = array(); |
|
| 1430 | - $haveMultiSearchAttributes = (is_array($searchAttributes) && count($searchAttributes) > 0); |
|
| 1431 | - if($haveMultiSearchAttributes && strpos(trim($search), ' ') !== false) { |
|
| 1432 | - try { |
|
| 1433 | - return $this->getAdvancedFilterPartForSearch($search, $searchAttributes); |
|
| 1434 | - } catch(\Exception $e) { |
|
| 1435 | - \OCP\Util::writeLog( |
|
| 1436 | - 'user_ldap', |
|
| 1437 | - 'Creating advanced filter for search failed, falling back to simple method.', |
|
| 1438 | - \OCP\Util::INFO |
|
| 1439 | - ); |
|
| 1440 | - } |
|
| 1441 | - } |
|
| 1442 | - |
|
| 1443 | - $search = $this->prepareSearchTerm($search); |
|
| 1444 | - if(!is_array($searchAttributes) || count($searchAttributes) === 0) { |
|
| 1445 | - if ($fallbackAttribute === '') { |
|
| 1446 | - return ''; |
|
| 1447 | - } |
|
| 1448 | - $filter[] = $fallbackAttribute . '=' . $search; |
|
| 1449 | - } else { |
|
| 1450 | - foreach($searchAttributes as $attribute) { |
|
| 1451 | - $filter[] = $attribute . '=' . $search; |
|
| 1452 | - } |
|
| 1453 | - } |
|
| 1454 | - if(count($filter) === 1) { |
|
| 1455 | - return '('.$filter[0].')'; |
|
| 1456 | - } |
|
| 1457 | - return $this->combineFilterWithOr($filter); |
|
| 1458 | - } |
|
| 1459 | - |
|
| 1460 | - /** |
|
| 1461 | - * returns the search term depending on whether we are allowed |
|
| 1462 | - * list users found by ldap with the current input appended by |
|
| 1463 | - * a * |
|
| 1464 | - * @return string |
|
| 1465 | - */ |
|
| 1466 | - private function prepareSearchTerm($term) { |
|
| 1467 | - $config = \OC::$server->getConfig(); |
|
| 1468 | - |
|
| 1469 | - $allowEnum = $config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes'); |
|
| 1470 | - |
|
| 1471 | - $result = $term; |
|
| 1472 | - if ($term === '') { |
|
| 1473 | - $result = '*'; |
|
| 1474 | - } else if ($allowEnum !== 'no') { |
|
| 1475 | - $result = $term . '*'; |
|
| 1476 | - } |
|
| 1477 | - return $result; |
|
| 1478 | - } |
|
| 1479 | - |
|
| 1480 | - /** |
|
| 1481 | - * returns the filter used for counting users |
|
| 1482 | - * @return string |
|
| 1483 | - */ |
|
| 1484 | - public function getFilterForUserCount() { |
|
| 1485 | - $filter = $this->combineFilterWithAnd(array( |
|
| 1486 | - $this->connection->ldapUserFilter, |
|
| 1487 | - $this->connection->ldapUserDisplayName . '=*' |
|
| 1488 | - )); |
|
| 1489 | - |
|
| 1490 | - return $filter; |
|
| 1491 | - } |
|
| 1492 | - |
|
| 1493 | - /** |
|
| 1494 | - * @param string $name |
|
| 1495 | - * @param string $password |
|
| 1496 | - * @return bool |
|
| 1497 | - */ |
|
| 1498 | - public function areCredentialsValid($name, $password) { |
|
| 1499 | - $name = $this->helper->DNasBaseParameter($name); |
|
| 1500 | - $testConnection = clone $this->connection; |
|
| 1501 | - $credentials = array( |
|
| 1502 | - 'ldapAgentName' => $name, |
|
| 1503 | - 'ldapAgentPassword' => $password |
|
| 1504 | - ); |
|
| 1505 | - if(!$testConnection->setConfiguration($credentials)) { |
|
| 1506 | - return false; |
|
| 1507 | - } |
|
| 1508 | - return $testConnection->bind(); |
|
| 1509 | - } |
|
| 1510 | - |
|
| 1511 | - /** |
|
| 1512 | - * reverse lookup of a DN given a known UUID |
|
| 1513 | - * |
|
| 1514 | - * @param string $uuid |
|
| 1515 | - * @return string |
|
| 1516 | - * @throws \Exception |
|
| 1517 | - */ |
|
| 1518 | - public function getUserDnByUuid($uuid) { |
|
| 1519 | - $uuidOverride = $this->connection->ldapExpertUUIDUserAttr; |
|
| 1520 | - $filter = $this->connection->ldapUserFilter; |
|
| 1521 | - $base = $this->connection->ldapBaseUsers; |
|
| 1522 | - |
|
| 1523 | - if ($this->connection->ldapUuidUserAttribute === 'auto' && $uuidOverride === '') { |
|
| 1524 | - // Sacrebleu! The UUID attribute is unknown :( We need first an |
|
| 1525 | - // existing DN to be able to reliably detect it. |
|
| 1526 | - $result = $this->search($filter, $base, ['dn'], 1); |
|
| 1527 | - if(!isset($result[0]) || !isset($result[0]['dn'])) { |
|
| 1528 | - throw new \Exception('Cannot determine UUID attribute'); |
|
| 1529 | - } |
|
| 1530 | - $dn = $result[0]['dn'][0]; |
|
| 1531 | - if(!$this->detectUuidAttribute($dn, true)) { |
|
| 1532 | - throw new \Exception('Cannot determine UUID attribute'); |
|
| 1533 | - } |
|
| 1534 | - } else { |
|
| 1535 | - // The UUID attribute is either known or an override is given. |
|
| 1536 | - // By calling this method we ensure that $this->connection->$uuidAttr |
|
| 1537 | - // is definitely set |
|
| 1538 | - if(!$this->detectUuidAttribute('', true)) { |
|
| 1539 | - throw new \Exception('Cannot determine UUID attribute'); |
|
| 1540 | - } |
|
| 1541 | - } |
|
| 1542 | - |
|
| 1543 | - $uuidAttr = $this->connection->ldapUuidUserAttribute; |
|
| 1544 | - if($uuidAttr === 'guid' || $uuidAttr === 'objectguid') { |
|
| 1545 | - $uuid = $this->formatGuid2ForFilterUser($uuid); |
|
| 1546 | - } |
|
| 1547 | - |
|
| 1548 | - $filter = $uuidAttr . '=' . $uuid; |
|
| 1549 | - $result = $this->searchUsers($filter, ['dn'], 2); |
|
| 1550 | - if(is_array($result) && isset($result[0]) && isset($result[0]['dn']) && count($result) === 1) { |
|
| 1551 | - // we put the count into account to make sure that this is |
|
| 1552 | - // really unique |
|
| 1553 | - return $result[0]['dn'][0]; |
|
| 1554 | - } |
|
| 1555 | - |
|
| 1556 | - throw new \Exception('Cannot determine UUID attribute'); |
|
| 1557 | - } |
|
| 1558 | - |
|
| 1559 | - /** |
|
| 1560 | - * auto-detects the directory's UUID attribute |
|
| 1561 | - * |
|
| 1562 | - * @param string $dn a known DN used to check against |
|
| 1563 | - * @param bool $isUser |
|
| 1564 | - * @param bool $force the detection should be run, even if it is not set to auto |
|
| 1565 | - * @param array|null $ldapRecord |
|
| 1566 | - * @return bool true on success, false otherwise |
|
| 1567 | - */ |
|
| 1568 | - private function detectUuidAttribute($dn, $isUser = true, $force = false, array $ldapRecord = null) { |
|
| 1569 | - if($isUser) { |
|
| 1570 | - $uuidAttr = 'ldapUuidUserAttribute'; |
|
| 1571 | - $uuidOverride = $this->connection->ldapExpertUUIDUserAttr; |
|
| 1572 | - } else { |
|
| 1573 | - $uuidAttr = 'ldapUuidGroupAttribute'; |
|
| 1574 | - $uuidOverride = $this->connection->ldapExpertUUIDGroupAttr; |
|
| 1575 | - } |
|
| 1576 | - |
|
| 1577 | - if(($this->connection->$uuidAttr !== 'auto') && !$force) { |
|
| 1578 | - return true; |
|
| 1579 | - } |
|
| 1580 | - |
|
| 1581 | - if (is_string($uuidOverride) && trim($uuidOverride) !== '' && !$force) { |
|
| 1582 | - $this->connection->$uuidAttr = $uuidOverride; |
|
| 1583 | - return true; |
|
| 1584 | - } |
|
| 1585 | - |
|
| 1586 | - foreach(self::UUID_ATTRIBUTES as $attribute) { |
|
| 1587 | - if($ldapRecord !== null) { |
|
| 1588 | - // we have the info from LDAP already, we don't need to talk to the server again |
|
| 1589 | - if(isset($ldapRecord[$attribute])) { |
|
| 1590 | - $this->connection->$uuidAttr = $attribute; |
|
| 1591 | - return true; |
|
| 1592 | - } else { |
|
| 1593 | - continue; |
|
| 1594 | - } |
|
| 1595 | - } |
|
| 1596 | - |
|
| 1597 | - $value = $this->readAttribute($dn, $attribute); |
|
| 1598 | - if(is_array($value) && isset($value[0]) && !empty($value[0])) { |
|
| 1599 | - \OCP\Util::writeLog('user_ldap', |
|
| 1600 | - 'Setting '.$attribute.' as '.$uuidAttr, |
|
| 1601 | - \OCP\Util::DEBUG); |
|
| 1602 | - $this->connection->$uuidAttr = $attribute; |
|
| 1603 | - return true; |
|
| 1604 | - } |
|
| 1605 | - } |
|
| 1606 | - \OCP\Util::writeLog('user_ldap', |
|
| 1607 | - 'Could not autodetect the UUID attribute', |
|
| 1608 | - \OCP\Util::ERROR); |
|
| 1609 | - |
|
| 1610 | - return false; |
|
| 1611 | - } |
|
| 1612 | - |
|
| 1613 | - /** |
|
| 1614 | - * @param string $dn |
|
| 1615 | - * @param bool $isUser |
|
| 1616 | - * @param null $ldapRecord |
|
| 1617 | - * @return bool|string |
|
| 1618 | - */ |
|
| 1619 | - public function getUUID($dn, $isUser = true, $ldapRecord = null) { |
|
| 1620 | - if($isUser) { |
|
| 1621 | - $uuidAttr = 'ldapUuidUserAttribute'; |
|
| 1622 | - $uuidOverride = $this->connection->ldapExpertUUIDUserAttr; |
|
| 1623 | - } else { |
|
| 1624 | - $uuidAttr = 'ldapUuidGroupAttribute'; |
|
| 1625 | - $uuidOverride = $this->connection->ldapExpertUUIDGroupAttr; |
|
| 1626 | - } |
|
| 1627 | - |
|
| 1628 | - $uuid = false; |
|
| 1629 | - if($this->detectUuidAttribute($dn, $isUser, false, $ldapRecord)) { |
|
| 1630 | - $attr = $this->connection->$uuidAttr; |
|
| 1631 | - $uuid = isset($ldapRecord[$attr]) ? $ldapRecord[$attr] : $this->readAttribute($dn, $attr); |
|
| 1632 | - if( !is_array($uuid) |
|
| 1633 | - && $uuidOverride !== '' |
|
| 1634 | - && $this->detectUuidAttribute($dn, $isUser, true, $ldapRecord)) |
|
| 1635 | - { |
|
| 1636 | - $uuid = isset($ldapRecord[$this->connection->$uuidAttr]) |
|
| 1637 | - ? $ldapRecord[$this->connection->$uuidAttr] |
|
| 1638 | - : $this->readAttribute($dn, $this->connection->$uuidAttr); |
|
| 1639 | - } |
|
| 1640 | - if(is_array($uuid) && isset($uuid[0]) && !empty($uuid[0])) { |
|
| 1641 | - $uuid = $uuid[0]; |
|
| 1642 | - } |
|
| 1643 | - } |
|
| 1644 | - |
|
| 1645 | - return $uuid; |
|
| 1646 | - } |
|
| 1647 | - |
|
| 1648 | - /** |
|
| 1649 | - * converts a binary ObjectGUID into a string representation |
|
| 1650 | - * @param string $oguid the ObjectGUID in it's binary form as retrieved from AD |
|
| 1651 | - * @return string |
|
| 1652 | - * @link http://www.php.net/manual/en/function.ldap-get-values-len.php#73198 |
|
| 1653 | - */ |
|
| 1654 | - private function convertObjectGUID2Str($oguid) { |
|
| 1655 | - $hex_guid = bin2hex($oguid); |
|
| 1656 | - $hex_guid_to_guid_str = ''; |
|
| 1657 | - for($k = 1; $k <= 4; ++$k) { |
|
| 1658 | - $hex_guid_to_guid_str .= substr($hex_guid, 8 - 2 * $k, 2); |
|
| 1659 | - } |
|
| 1660 | - $hex_guid_to_guid_str .= '-'; |
|
| 1661 | - for($k = 1; $k <= 2; ++$k) { |
|
| 1662 | - $hex_guid_to_guid_str .= substr($hex_guid, 12 - 2 * $k, 2); |
|
| 1663 | - } |
|
| 1664 | - $hex_guid_to_guid_str .= '-'; |
|
| 1665 | - for($k = 1; $k <= 2; ++$k) { |
|
| 1666 | - $hex_guid_to_guid_str .= substr($hex_guid, 16 - 2 * $k, 2); |
|
| 1667 | - } |
|
| 1668 | - $hex_guid_to_guid_str .= '-' . substr($hex_guid, 16, 4); |
|
| 1669 | - $hex_guid_to_guid_str .= '-' . substr($hex_guid, 20); |
|
| 1670 | - |
|
| 1671 | - return strtoupper($hex_guid_to_guid_str); |
|
| 1672 | - } |
|
| 1673 | - |
|
| 1674 | - /** |
|
| 1675 | - * the first three blocks of the string-converted GUID happen to be in |
|
| 1676 | - * reverse order. In order to use it in a filter, this needs to be |
|
| 1677 | - * corrected. Furthermore the dashes need to be replaced and \\ preprended |
|
| 1678 | - * to every two hax figures. |
|
| 1679 | - * |
|
| 1680 | - * If an invalid string is passed, it will be returned without change. |
|
| 1681 | - * |
|
| 1682 | - * @param string $guid |
|
| 1683 | - * @return string |
|
| 1684 | - */ |
|
| 1685 | - public function formatGuid2ForFilterUser($guid) { |
|
| 1686 | - if(!is_string($guid)) { |
|
| 1687 | - throw new \InvalidArgumentException('String expected'); |
|
| 1688 | - } |
|
| 1689 | - $blocks = explode('-', $guid); |
|
| 1690 | - if(count($blocks) !== 5) { |
|
| 1691 | - /* |
|
| 1208 | + $findings = []; |
|
| 1209 | + $savedoffset = $offset; |
|
| 1210 | + do { |
|
| 1211 | + $search = $this->executeSearch($filter, $base, $attr, $limitPerPage, $offset); |
|
| 1212 | + if($search === false) { |
|
| 1213 | + return []; |
|
| 1214 | + } |
|
| 1215 | + list($sr, $pagedSearchOK) = $search; |
|
| 1216 | + $cr = $this->connection->getConnectionResource(); |
|
| 1217 | + |
|
| 1218 | + if($skipHandling) { |
|
| 1219 | + //i.e. result do not need to be fetched, we just need the cookie |
|
| 1220 | + //thus pass 1 or any other value as $iFoundItems because it is not |
|
| 1221 | + //used |
|
| 1222 | + $this->processPagedSearchStatus($sr, $filter, $base, 1, $limitPerPage, |
|
| 1223 | + $offset, $pagedSearchOK, |
|
| 1224 | + $skipHandling); |
|
| 1225 | + return array(); |
|
| 1226 | + } |
|
| 1227 | + |
|
| 1228 | + $iFoundItems = 0; |
|
| 1229 | + foreach($sr as $res) { |
|
| 1230 | + $findings = array_merge($findings, $this->invokeLDAPMethod('getEntries', $cr, $res)); |
|
| 1231 | + $iFoundItems = max($iFoundItems, $findings['count']); |
|
| 1232 | + unset($findings['count']); |
|
| 1233 | + } |
|
| 1234 | + |
|
| 1235 | + $continue = $this->processPagedSearchStatus($sr, $filter, $base, $iFoundItems, |
|
| 1236 | + $limitPerPage, $offset, $pagedSearchOK, |
|
| 1237 | + $skipHandling); |
|
| 1238 | + $offset += $limitPerPage; |
|
| 1239 | + } while ($continue && $pagedSearchOK && ($limit === null || count($findings) < $limit)); |
|
| 1240 | + // reseting offset |
|
| 1241 | + $offset = $savedoffset; |
|
| 1242 | + |
|
| 1243 | + // if we're here, probably no connection resource is returned. |
|
| 1244 | + // to make Nextcloud behave nicely, we simply give back an empty array. |
|
| 1245 | + if(is_null($findings)) { |
|
| 1246 | + return array(); |
|
| 1247 | + } |
|
| 1248 | + |
|
| 1249 | + if(!is_null($attr)) { |
|
| 1250 | + $selection = []; |
|
| 1251 | + $i = 0; |
|
| 1252 | + foreach($findings as $item) { |
|
| 1253 | + if(!is_array($item)) { |
|
| 1254 | + continue; |
|
| 1255 | + } |
|
| 1256 | + $item = \OCP\Util::mb_array_change_key_case($item, MB_CASE_LOWER, 'UTF-8'); |
|
| 1257 | + foreach($attr as $key) { |
|
| 1258 | + if(isset($item[$key])) { |
|
| 1259 | + if(is_array($item[$key]) && isset($item[$key]['count'])) { |
|
| 1260 | + unset($item[$key]['count']); |
|
| 1261 | + } |
|
| 1262 | + if($key !== 'dn') { |
|
| 1263 | + if($this->resemblesDN($key)) { |
|
| 1264 | + $selection[$i][$key] = $this->helper->sanitizeDN($item[$key]); |
|
| 1265 | + } else if($key === 'objectguid' || $key === 'guid') { |
|
| 1266 | + $selection[$i][$key] = [$this->convertObjectGUID2Str($item[$key][0])]; |
|
| 1267 | + } else { |
|
| 1268 | + $selection[$i][$key] = $item[$key]; |
|
| 1269 | + } |
|
| 1270 | + } else { |
|
| 1271 | + $selection[$i][$key] = [$this->helper->sanitizeDN($item[$key])]; |
|
| 1272 | + } |
|
| 1273 | + } |
|
| 1274 | + |
|
| 1275 | + } |
|
| 1276 | + $i++; |
|
| 1277 | + } |
|
| 1278 | + $findings = $selection; |
|
| 1279 | + } |
|
| 1280 | + //we slice the findings, when |
|
| 1281 | + //a) paged search unsuccessful, though attempted |
|
| 1282 | + //b) no paged search, but limit set |
|
| 1283 | + if((!$this->getPagedSearchResultState() |
|
| 1284 | + && $pagedSearchOK) |
|
| 1285 | + || ( |
|
| 1286 | + !$pagedSearchOK |
|
| 1287 | + && !is_null($limit) |
|
| 1288 | + ) |
|
| 1289 | + ) { |
|
| 1290 | + $findings = array_slice($findings, intval($offset), $limit); |
|
| 1291 | + } |
|
| 1292 | + return $findings; |
|
| 1293 | + } |
|
| 1294 | + |
|
| 1295 | + /** |
|
| 1296 | + * @param string $name |
|
| 1297 | + * @return bool|mixed|string |
|
| 1298 | + */ |
|
| 1299 | + public function sanitizeUsername($name) { |
|
| 1300 | + if($this->connection->ldapIgnoreNamingRules) { |
|
| 1301 | + return trim($name); |
|
| 1302 | + } |
|
| 1303 | + |
|
| 1304 | + // Transliteration |
|
| 1305 | + // latin characters to ASCII |
|
| 1306 | + $name = iconv('UTF-8', 'ASCII//TRANSLIT', $name); |
|
| 1307 | + |
|
| 1308 | + // Replacements |
|
| 1309 | + $name = str_replace(' ', '_', $name); |
|
| 1310 | + |
|
| 1311 | + // Every remaining disallowed characters will be removed |
|
| 1312 | + $name = preg_replace('/[^a-zA-Z0-9_.@-]/u', '', $name); |
|
| 1313 | + |
|
| 1314 | + return $name; |
|
| 1315 | + } |
|
| 1316 | + |
|
| 1317 | + /** |
|
| 1318 | + * escapes (user provided) parts for LDAP filter |
|
| 1319 | + * @param string $input, the provided value |
|
| 1320 | + * @param bool $allowAsterisk whether in * at the beginning should be preserved |
|
| 1321 | + * @return string the escaped string |
|
| 1322 | + */ |
|
| 1323 | + public function escapeFilterPart($input, $allowAsterisk = false) { |
|
| 1324 | + $asterisk = ''; |
|
| 1325 | + if($allowAsterisk && strlen($input) > 0 && $input[0] === '*') { |
|
| 1326 | + $asterisk = '*'; |
|
| 1327 | + $input = mb_substr($input, 1, null, 'UTF-8'); |
|
| 1328 | + } |
|
| 1329 | + $search = array('*', '\\', '(', ')'); |
|
| 1330 | + $replace = array('\\*', '\\\\', '\\(', '\\)'); |
|
| 1331 | + return $asterisk . str_replace($search, $replace, $input); |
|
| 1332 | + } |
|
| 1333 | + |
|
| 1334 | + /** |
|
| 1335 | + * combines the input filters with AND |
|
| 1336 | + * @param string[] $filters the filters to connect |
|
| 1337 | + * @return string the combined filter |
|
| 1338 | + */ |
|
| 1339 | + public function combineFilterWithAnd($filters) { |
|
| 1340 | + return $this->combineFilter($filters, '&'); |
|
| 1341 | + } |
|
| 1342 | + |
|
| 1343 | + /** |
|
| 1344 | + * combines the input filters with OR |
|
| 1345 | + * @param string[] $filters the filters to connect |
|
| 1346 | + * @return string the combined filter |
|
| 1347 | + * Combines Filter arguments with OR |
|
| 1348 | + */ |
|
| 1349 | + public function combineFilterWithOr($filters) { |
|
| 1350 | + return $this->combineFilter($filters, '|'); |
|
| 1351 | + } |
|
| 1352 | + |
|
| 1353 | + /** |
|
| 1354 | + * combines the input filters with given operator |
|
| 1355 | + * @param string[] $filters the filters to connect |
|
| 1356 | + * @param string $operator either & or | |
|
| 1357 | + * @return string the combined filter |
|
| 1358 | + */ |
|
| 1359 | + private function combineFilter($filters, $operator) { |
|
| 1360 | + $combinedFilter = '('.$operator; |
|
| 1361 | + foreach($filters as $filter) { |
|
| 1362 | + if ($filter !== '' && $filter[0] !== '(') { |
|
| 1363 | + $filter = '('.$filter.')'; |
|
| 1364 | + } |
|
| 1365 | + $combinedFilter.=$filter; |
|
| 1366 | + } |
|
| 1367 | + $combinedFilter.=')'; |
|
| 1368 | + return $combinedFilter; |
|
| 1369 | + } |
|
| 1370 | + |
|
| 1371 | + /** |
|
| 1372 | + * creates a filter part for to perform search for users |
|
| 1373 | + * @param string $search the search term |
|
| 1374 | + * @return string the final filter part to use in LDAP searches |
|
| 1375 | + */ |
|
| 1376 | + public function getFilterPartForUserSearch($search) { |
|
| 1377 | + return $this->getFilterPartForSearch($search, |
|
| 1378 | + $this->connection->ldapAttributesForUserSearch, |
|
| 1379 | + $this->connection->ldapUserDisplayName); |
|
| 1380 | + } |
|
| 1381 | + |
|
| 1382 | + /** |
|
| 1383 | + * creates a filter part for to perform search for groups |
|
| 1384 | + * @param string $search the search term |
|
| 1385 | + * @return string the final filter part to use in LDAP searches |
|
| 1386 | + */ |
|
| 1387 | + public function getFilterPartForGroupSearch($search) { |
|
| 1388 | + return $this->getFilterPartForSearch($search, |
|
| 1389 | + $this->connection->ldapAttributesForGroupSearch, |
|
| 1390 | + $this->connection->ldapGroupDisplayName); |
|
| 1391 | + } |
|
| 1392 | + |
|
| 1393 | + /** |
|
| 1394 | + * creates a filter part for searches by splitting up the given search |
|
| 1395 | + * string into single words |
|
| 1396 | + * @param string $search the search term |
|
| 1397 | + * @param string[] $searchAttributes needs to have at least two attributes, |
|
| 1398 | + * otherwise it does not make sense :) |
|
| 1399 | + * @return string the final filter part to use in LDAP searches |
|
| 1400 | + * @throws \Exception |
|
| 1401 | + */ |
|
| 1402 | + private function getAdvancedFilterPartForSearch($search, $searchAttributes) { |
|
| 1403 | + if(!is_array($searchAttributes) || count($searchAttributes) < 2) { |
|
| 1404 | + throw new \Exception('searchAttributes must be an array with at least two string'); |
|
| 1405 | + } |
|
| 1406 | + $searchWords = explode(' ', trim($search)); |
|
| 1407 | + $wordFilters = array(); |
|
| 1408 | + foreach($searchWords as $word) { |
|
| 1409 | + $word = $this->prepareSearchTerm($word); |
|
| 1410 | + //every word needs to appear at least once |
|
| 1411 | + $wordMatchOneAttrFilters = array(); |
|
| 1412 | + foreach($searchAttributes as $attr) { |
|
| 1413 | + $wordMatchOneAttrFilters[] = $attr . '=' . $word; |
|
| 1414 | + } |
|
| 1415 | + $wordFilters[] = $this->combineFilterWithOr($wordMatchOneAttrFilters); |
|
| 1416 | + } |
|
| 1417 | + return $this->combineFilterWithAnd($wordFilters); |
|
| 1418 | + } |
|
| 1419 | + |
|
| 1420 | + /** |
|
| 1421 | + * creates a filter part for searches |
|
| 1422 | + * @param string $search the search term |
|
| 1423 | + * @param string[]|null $searchAttributes |
|
| 1424 | + * @param string $fallbackAttribute a fallback attribute in case the user |
|
| 1425 | + * did not define search attributes. Typically the display name attribute. |
|
| 1426 | + * @return string the final filter part to use in LDAP searches |
|
| 1427 | + */ |
|
| 1428 | + private function getFilterPartForSearch($search, $searchAttributes, $fallbackAttribute) { |
|
| 1429 | + $filter = array(); |
|
| 1430 | + $haveMultiSearchAttributes = (is_array($searchAttributes) && count($searchAttributes) > 0); |
|
| 1431 | + if($haveMultiSearchAttributes && strpos(trim($search), ' ') !== false) { |
|
| 1432 | + try { |
|
| 1433 | + return $this->getAdvancedFilterPartForSearch($search, $searchAttributes); |
|
| 1434 | + } catch(\Exception $e) { |
|
| 1435 | + \OCP\Util::writeLog( |
|
| 1436 | + 'user_ldap', |
|
| 1437 | + 'Creating advanced filter for search failed, falling back to simple method.', |
|
| 1438 | + \OCP\Util::INFO |
|
| 1439 | + ); |
|
| 1440 | + } |
|
| 1441 | + } |
|
| 1442 | + |
|
| 1443 | + $search = $this->prepareSearchTerm($search); |
|
| 1444 | + if(!is_array($searchAttributes) || count($searchAttributes) === 0) { |
|
| 1445 | + if ($fallbackAttribute === '') { |
|
| 1446 | + return ''; |
|
| 1447 | + } |
|
| 1448 | + $filter[] = $fallbackAttribute . '=' . $search; |
|
| 1449 | + } else { |
|
| 1450 | + foreach($searchAttributes as $attribute) { |
|
| 1451 | + $filter[] = $attribute . '=' . $search; |
|
| 1452 | + } |
|
| 1453 | + } |
|
| 1454 | + if(count($filter) === 1) { |
|
| 1455 | + return '('.$filter[0].')'; |
|
| 1456 | + } |
|
| 1457 | + return $this->combineFilterWithOr($filter); |
|
| 1458 | + } |
|
| 1459 | + |
|
| 1460 | + /** |
|
| 1461 | + * returns the search term depending on whether we are allowed |
|
| 1462 | + * list users found by ldap with the current input appended by |
|
| 1463 | + * a * |
|
| 1464 | + * @return string |
|
| 1465 | + */ |
|
| 1466 | + private function prepareSearchTerm($term) { |
|
| 1467 | + $config = \OC::$server->getConfig(); |
|
| 1468 | + |
|
| 1469 | + $allowEnum = $config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes'); |
|
| 1470 | + |
|
| 1471 | + $result = $term; |
|
| 1472 | + if ($term === '') { |
|
| 1473 | + $result = '*'; |
|
| 1474 | + } else if ($allowEnum !== 'no') { |
|
| 1475 | + $result = $term . '*'; |
|
| 1476 | + } |
|
| 1477 | + return $result; |
|
| 1478 | + } |
|
| 1479 | + |
|
| 1480 | + /** |
|
| 1481 | + * returns the filter used for counting users |
|
| 1482 | + * @return string |
|
| 1483 | + */ |
|
| 1484 | + public function getFilterForUserCount() { |
|
| 1485 | + $filter = $this->combineFilterWithAnd(array( |
|
| 1486 | + $this->connection->ldapUserFilter, |
|
| 1487 | + $this->connection->ldapUserDisplayName . '=*' |
|
| 1488 | + )); |
|
| 1489 | + |
|
| 1490 | + return $filter; |
|
| 1491 | + } |
|
| 1492 | + |
|
| 1493 | + /** |
|
| 1494 | + * @param string $name |
|
| 1495 | + * @param string $password |
|
| 1496 | + * @return bool |
|
| 1497 | + */ |
|
| 1498 | + public function areCredentialsValid($name, $password) { |
|
| 1499 | + $name = $this->helper->DNasBaseParameter($name); |
|
| 1500 | + $testConnection = clone $this->connection; |
|
| 1501 | + $credentials = array( |
|
| 1502 | + 'ldapAgentName' => $name, |
|
| 1503 | + 'ldapAgentPassword' => $password |
|
| 1504 | + ); |
|
| 1505 | + if(!$testConnection->setConfiguration($credentials)) { |
|
| 1506 | + return false; |
|
| 1507 | + } |
|
| 1508 | + return $testConnection->bind(); |
|
| 1509 | + } |
|
| 1510 | + |
|
| 1511 | + /** |
|
| 1512 | + * reverse lookup of a DN given a known UUID |
|
| 1513 | + * |
|
| 1514 | + * @param string $uuid |
|
| 1515 | + * @return string |
|
| 1516 | + * @throws \Exception |
|
| 1517 | + */ |
|
| 1518 | + public function getUserDnByUuid($uuid) { |
|
| 1519 | + $uuidOverride = $this->connection->ldapExpertUUIDUserAttr; |
|
| 1520 | + $filter = $this->connection->ldapUserFilter; |
|
| 1521 | + $base = $this->connection->ldapBaseUsers; |
|
| 1522 | + |
|
| 1523 | + if ($this->connection->ldapUuidUserAttribute === 'auto' && $uuidOverride === '') { |
|
| 1524 | + // Sacrebleu! The UUID attribute is unknown :( We need first an |
|
| 1525 | + // existing DN to be able to reliably detect it. |
|
| 1526 | + $result = $this->search($filter, $base, ['dn'], 1); |
|
| 1527 | + if(!isset($result[0]) || !isset($result[0]['dn'])) { |
|
| 1528 | + throw new \Exception('Cannot determine UUID attribute'); |
|
| 1529 | + } |
|
| 1530 | + $dn = $result[0]['dn'][0]; |
|
| 1531 | + if(!$this->detectUuidAttribute($dn, true)) { |
|
| 1532 | + throw new \Exception('Cannot determine UUID attribute'); |
|
| 1533 | + } |
|
| 1534 | + } else { |
|
| 1535 | + // The UUID attribute is either known or an override is given. |
|
| 1536 | + // By calling this method we ensure that $this->connection->$uuidAttr |
|
| 1537 | + // is definitely set |
|
| 1538 | + if(!$this->detectUuidAttribute('', true)) { |
|
| 1539 | + throw new \Exception('Cannot determine UUID attribute'); |
|
| 1540 | + } |
|
| 1541 | + } |
|
| 1542 | + |
|
| 1543 | + $uuidAttr = $this->connection->ldapUuidUserAttribute; |
|
| 1544 | + if($uuidAttr === 'guid' || $uuidAttr === 'objectguid') { |
|
| 1545 | + $uuid = $this->formatGuid2ForFilterUser($uuid); |
|
| 1546 | + } |
|
| 1547 | + |
|
| 1548 | + $filter = $uuidAttr . '=' . $uuid; |
|
| 1549 | + $result = $this->searchUsers($filter, ['dn'], 2); |
|
| 1550 | + if(is_array($result) && isset($result[0]) && isset($result[0]['dn']) && count($result) === 1) { |
|
| 1551 | + // we put the count into account to make sure that this is |
|
| 1552 | + // really unique |
|
| 1553 | + return $result[0]['dn'][0]; |
|
| 1554 | + } |
|
| 1555 | + |
|
| 1556 | + throw new \Exception('Cannot determine UUID attribute'); |
|
| 1557 | + } |
|
| 1558 | + |
|
| 1559 | + /** |
|
| 1560 | + * auto-detects the directory's UUID attribute |
|
| 1561 | + * |
|
| 1562 | + * @param string $dn a known DN used to check against |
|
| 1563 | + * @param bool $isUser |
|
| 1564 | + * @param bool $force the detection should be run, even if it is not set to auto |
|
| 1565 | + * @param array|null $ldapRecord |
|
| 1566 | + * @return bool true on success, false otherwise |
|
| 1567 | + */ |
|
| 1568 | + private function detectUuidAttribute($dn, $isUser = true, $force = false, array $ldapRecord = null) { |
|
| 1569 | + if($isUser) { |
|
| 1570 | + $uuidAttr = 'ldapUuidUserAttribute'; |
|
| 1571 | + $uuidOverride = $this->connection->ldapExpertUUIDUserAttr; |
|
| 1572 | + } else { |
|
| 1573 | + $uuidAttr = 'ldapUuidGroupAttribute'; |
|
| 1574 | + $uuidOverride = $this->connection->ldapExpertUUIDGroupAttr; |
|
| 1575 | + } |
|
| 1576 | + |
|
| 1577 | + if(($this->connection->$uuidAttr !== 'auto') && !$force) { |
|
| 1578 | + return true; |
|
| 1579 | + } |
|
| 1580 | + |
|
| 1581 | + if (is_string($uuidOverride) && trim($uuidOverride) !== '' && !$force) { |
|
| 1582 | + $this->connection->$uuidAttr = $uuidOverride; |
|
| 1583 | + return true; |
|
| 1584 | + } |
|
| 1585 | + |
|
| 1586 | + foreach(self::UUID_ATTRIBUTES as $attribute) { |
|
| 1587 | + if($ldapRecord !== null) { |
|
| 1588 | + // we have the info from LDAP already, we don't need to talk to the server again |
|
| 1589 | + if(isset($ldapRecord[$attribute])) { |
|
| 1590 | + $this->connection->$uuidAttr = $attribute; |
|
| 1591 | + return true; |
|
| 1592 | + } else { |
|
| 1593 | + continue; |
|
| 1594 | + } |
|
| 1595 | + } |
|
| 1596 | + |
|
| 1597 | + $value = $this->readAttribute($dn, $attribute); |
|
| 1598 | + if(is_array($value) && isset($value[0]) && !empty($value[0])) { |
|
| 1599 | + \OCP\Util::writeLog('user_ldap', |
|
| 1600 | + 'Setting '.$attribute.' as '.$uuidAttr, |
|
| 1601 | + \OCP\Util::DEBUG); |
|
| 1602 | + $this->connection->$uuidAttr = $attribute; |
|
| 1603 | + return true; |
|
| 1604 | + } |
|
| 1605 | + } |
|
| 1606 | + \OCP\Util::writeLog('user_ldap', |
|
| 1607 | + 'Could not autodetect the UUID attribute', |
|
| 1608 | + \OCP\Util::ERROR); |
|
| 1609 | + |
|
| 1610 | + return false; |
|
| 1611 | + } |
|
| 1612 | + |
|
| 1613 | + /** |
|
| 1614 | + * @param string $dn |
|
| 1615 | + * @param bool $isUser |
|
| 1616 | + * @param null $ldapRecord |
|
| 1617 | + * @return bool|string |
|
| 1618 | + */ |
|
| 1619 | + public function getUUID($dn, $isUser = true, $ldapRecord = null) { |
|
| 1620 | + if($isUser) { |
|
| 1621 | + $uuidAttr = 'ldapUuidUserAttribute'; |
|
| 1622 | + $uuidOverride = $this->connection->ldapExpertUUIDUserAttr; |
|
| 1623 | + } else { |
|
| 1624 | + $uuidAttr = 'ldapUuidGroupAttribute'; |
|
| 1625 | + $uuidOverride = $this->connection->ldapExpertUUIDGroupAttr; |
|
| 1626 | + } |
|
| 1627 | + |
|
| 1628 | + $uuid = false; |
|
| 1629 | + if($this->detectUuidAttribute($dn, $isUser, false, $ldapRecord)) { |
|
| 1630 | + $attr = $this->connection->$uuidAttr; |
|
| 1631 | + $uuid = isset($ldapRecord[$attr]) ? $ldapRecord[$attr] : $this->readAttribute($dn, $attr); |
|
| 1632 | + if( !is_array($uuid) |
|
| 1633 | + && $uuidOverride !== '' |
|
| 1634 | + && $this->detectUuidAttribute($dn, $isUser, true, $ldapRecord)) |
|
| 1635 | + { |
|
| 1636 | + $uuid = isset($ldapRecord[$this->connection->$uuidAttr]) |
|
| 1637 | + ? $ldapRecord[$this->connection->$uuidAttr] |
|
| 1638 | + : $this->readAttribute($dn, $this->connection->$uuidAttr); |
|
| 1639 | + } |
|
| 1640 | + if(is_array($uuid) && isset($uuid[0]) && !empty($uuid[0])) { |
|
| 1641 | + $uuid = $uuid[0]; |
|
| 1642 | + } |
|
| 1643 | + } |
|
| 1644 | + |
|
| 1645 | + return $uuid; |
|
| 1646 | + } |
|
| 1647 | + |
|
| 1648 | + /** |
|
| 1649 | + * converts a binary ObjectGUID into a string representation |
|
| 1650 | + * @param string $oguid the ObjectGUID in it's binary form as retrieved from AD |
|
| 1651 | + * @return string |
|
| 1652 | + * @link http://www.php.net/manual/en/function.ldap-get-values-len.php#73198 |
|
| 1653 | + */ |
|
| 1654 | + private function convertObjectGUID2Str($oguid) { |
|
| 1655 | + $hex_guid = bin2hex($oguid); |
|
| 1656 | + $hex_guid_to_guid_str = ''; |
|
| 1657 | + for($k = 1; $k <= 4; ++$k) { |
|
| 1658 | + $hex_guid_to_guid_str .= substr($hex_guid, 8 - 2 * $k, 2); |
|
| 1659 | + } |
|
| 1660 | + $hex_guid_to_guid_str .= '-'; |
|
| 1661 | + for($k = 1; $k <= 2; ++$k) { |
|
| 1662 | + $hex_guid_to_guid_str .= substr($hex_guid, 12 - 2 * $k, 2); |
|
| 1663 | + } |
|
| 1664 | + $hex_guid_to_guid_str .= '-'; |
|
| 1665 | + for($k = 1; $k <= 2; ++$k) { |
|
| 1666 | + $hex_guid_to_guid_str .= substr($hex_guid, 16 - 2 * $k, 2); |
|
| 1667 | + } |
|
| 1668 | + $hex_guid_to_guid_str .= '-' . substr($hex_guid, 16, 4); |
|
| 1669 | + $hex_guid_to_guid_str .= '-' . substr($hex_guid, 20); |
|
| 1670 | + |
|
| 1671 | + return strtoupper($hex_guid_to_guid_str); |
|
| 1672 | + } |
|
| 1673 | + |
|
| 1674 | + /** |
|
| 1675 | + * the first three blocks of the string-converted GUID happen to be in |
|
| 1676 | + * reverse order. In order to use it in a filter, this needs to be |
|
| 1677 | + * corrected. Furthermore the dashes need to be replaced and \\ preprended |
|
| 1678 | + * to every two hax figures. |
|
| 1679 | + * |
|
| 1680 | + * If an invalid string is passed, it will be returned without change. |
|
| 1681 | + * |
|
| 1682 | + * @param string $guid |
|
| 1683 | + * @return string |
|
| 1684 | + */ |
|
| 1685 | + public function formatGuid2ForFilterUser($guid) { |
|
| 1686 | + if(!is_string($guid)) { |
|
| 1687 | + throw new \InvalidArgumentException('String expected'); |
|
| 1688 | + } |
|
| 1689 | + $blocks = explode('-', $guid); |
|
| 1690 | + if(count($blocks) !== 5) { |
|
| 1691 | + /* |
|
| 1692 | 1692 | * Why not throw an Exception instead? This method is a utility |
| 1693 | 1693 | * called only when trying to figure out whether a "missing" known |
| 1694 | 1694 | * LDAP user was or was not renamed on the LDAP server. And this |
@@ -1699,270 +1699,270 @@ discard block |
||
| 1699 | 1699 | * an exception here would kill the experience for a valid, acting |
| 1700 | 1700 | * user. Instead we write a log message. |
| 1701 | 1701 | */ |
| 1702 | - \OC::$server->getLogger()->info( |
|
| 1703 | - 'Passed string does not resemble a valid GUID. Known UUID ' . |
|
| 1704 | - '({uuid}) probably does not match UUID configuration.', |
|
| 1705 | - [ 'app' => 'user_ldap', 'uuid' => $guid ] |
|
| 1706 | - ); |
|
| 1707 | - return $guid; |
|
| 1708 | - } |
|
| 1709 | - for($i=0; $i < 3; $i++) { |
|
| 1710 | - $pairs = str_split($blocks[$i], 2); |
|
| 1711 | - $pairs = array_reverse($pairs); |
|
| 1712 | - $blocks[$i] = implode('', $pairs); |
|
| 1713 | - } |
|
| 1714 | - for($i=0; $i < 5; $i++) { |
|
| 1715 | - $pairs = str_split($blocks[$i], 2); |
|
| 1716 | - $blocks[$i] = '\\' . implode('\\', $pairs); |
|
| 1717 | - } |
|
| 1718 | - return implode('', $blocks); |
|
| 1719 | - } |
|
| 1720 | - |
|
| 1721 | - /** |
|
| 1722 | - * gets a SID of the domain of the given dn |
|
| 1723 | - * @param string $dn |
|
| 1724 | - * @return string|bool |
|
| 1725 | - */ |
|
| 1726 | - public function getSID($dn) { |
|
| 1727 | - $domainDN = $this->getDomainDNFromDN($dn); |
|
| 1728 | - $cacheKey = 'getSID-'.$domainDN; |
|
| 1729 | - $sid = $this->connection->getFromCache($cacheKey); |
|
| 1730 | - if(!is_null($sid)) { |
|
| 1731 | - return $sid; |
|
| 1732 | - } |
|
| 1733 | - |
|
| 1734 | - $objectSid = $this->readAttribute($domainDN, 'objectsid'); |
|
| 1735 | - if(!is_array($objectSid) || empty($objectSid)) { |
|
| 1736 | - $this->connection->writeToCache($cacheKey, false); |
|
| 1737 | - return false; |
|
| 1738 | - } |
|
| 1739 | - $domainObjectSid = $this->convertSID2Str($objectSid[0]); |
|
| 1740 | - $this->connection->writeToCache($cacheKey, $domainObjectSid); |
|
| 1741 | - |
|
| 1742 | - return $domainObjectSid; |
|
| 1743 | - } |
|
| 1744 | - |
|
| 1745 | - /** |
|
| 1746 | - * converts a binary SID into a string representation |
|
| 1747 | - * @param string $sid |
|
| 1748 | - * @return string |
|
| 1749 | - */ |
|
| 1750 | - public function convertSID2Str($sid) { |
|
| 1751 | - // The format of a SID binary string is as follows: |
|
| 1752 | - // 1 byte for the revision level |
|
| 1753 | - // 1 byte for the number n of variable sub-ids |
|
| 1754 | - // 6 bytes for identifier authority value |
|
| 1755 | - // n*4 bytes for n sub-ids |
|
| 1756 | - // |
|
| 1757 | - // Example: 010400000000000515000000a681e50e4d6c6c2bca32055f |
|
| 1758 | - // Legend: RRNNAAAAAAAAAAAA11111111222222223333333344444444 |
|
| 1759 | - $revision = ord($sid[0]); |
|
| 1760 | - $numberSubID = ord($sid[1]); |
|
| 1761 | - |
|
| 1762 | - $subIdStart = 8; // 1 + 1 + 6 |
|
| 1763 | - $subIdLength = 4; |
|
| 1764 | - if (strlen($sid) !== $subIdStart + $subIdLength * $numberSubID) { |
|
| 1765 | - // Incorrect number of bytes present. |
|
| 1766 | - return ''; |
|
| 1767 | - } |
|
| 1768 | - |
|
| 1769 | - // 6 bytes = 48 bits can be represented using floats without loss of |
|
| 1770 | - // precision (see https://gist.github.com/bantu/886ac680b0aef5812f71) |
|
| 1771 | - $iav = number_format(hexdec(bin2hex(substr($sid, 2, 6))), 0, '', ''); |
|
| 1772 | - |
|
| 1773 | - $subIDs = array(); |
|
| 1774 | - for ($i = 0; $i < $numberSubID; $i++) { |
|
| 1775 | - $subID = unpack('V', substr($sid, $subIdStart + $subIdLength * $i, $subIdLength)); |
|
| 1776 | - $subIDs[] = sprintf('%u', $subID[1]); |
|
| 1777 | - } |
|
| 1778 | - |
|
| 1779 | - // Result for example above: S-1-5-21-249921958-728525901-1594176202 |
|
| 1780 | - return sprintf('S-%d-%s-%s', $revision, $iav, implode('-', $subIDs)); |
|
| 1781 | - } |
|
| 1782 | - |
|
| 1783 | - /** |
|
| 1784 | - * checks if the given DN is part of the given base DN(s) |
|
| 1785 | - * @param string $dn the DN |
|
| 1786 | - * @param string[] $bases array containing the allowed base DN or DNs |
|
| 1787 | - * @return bool |
|
| 1788 | - */ |
|
| 1789 | - public function isDNPartOfBase($dn, $bases) { |
|
| 1790 | - $belongsToBase = false; |
|
| 1791 | - $bases = $this->helper->sanitizeDN($bases); |
|
| 1792 | - |
|
| 1793 | - foreach($bases as $base) { |
|
| 1794 | - $belongsToBase = true; |
|
| 1795 | - if(mb_strripos($dn, $base, 0, 'UTF-8') !== (mb_strlen($dn, 'UTF-8')-mb_strlen($base, 'UTF-8'))) { |
|
| 1796 | - $belongsToBase = false; |
|
| 1797 | - } |
|
| 1798 | - if($belongsToBase) { |
|
| 1799 | - break; |
|
| 1800 | - } |
|
| 1801 | - } |
|
| 1802 | - return $belongsToBase; |
|
| 1803 | - } |
|
| 1804 | - |
|
| 1805 | - /** |
|
| 1806 | - * resets a running Paged Search operation |
|
| 1807 | - */ |
|
| 1808 | - private function abandonPagedSearch() { |
|
| 1809 | - if($this->connection->hasPagedResultSupport) { |
|
| 1810 | - $cr = $this->connection->getConnectionResource(); |
|
| 1811 | - $this->invokeLDAPMethod('controlPagedResult', $cr, 0, false, $this->lastCookie); |
|
| 1812 | - $this->getPagedSearchResultState(); |
|
| 1813 | - $this->lastCookie = ''; |
|
| 1814 | - $this->cookies = array(); |
|
| 1815 | - } |
|
| 1816 | - } |
|
| 1817 | - |
|
| 1818 | - /** |
|
| 1819 | - * get a cookie for the next LDAP paged search |
|
| 1820 | - * @param string $base a string with the base DN for the search |
|
| 1821 | - * @param string $filter the search filter to identify the correct search |
|
| 1822 | - * @param int $limit the limit (or 'pageSize'), to identify the correct search well |
|
| 1823 | - * @param int $offset the offset for the new search to identify the correct search really good |
|
| 1824 | - * @return string containing the key or empty if none is cached |
|
| 1825 | - */ |
|
| 1826 | - private function getPagedResultCookie($base, $filter, $limit, $offset) { |
|
| 1827 | - if($offset === 0) { |
|
| 1828 | - return ''; |
|
| 1829 | - } |
|
| 1830 | - $offset -= $limit; |
|
| 1831 | - //we work with cache here |
|
| 1832 | - $cacheKey = 'lc' . crc32($base) . '-' . crc32($filter) . '-' . intval($limit) . '-' . intval($offset); |
|
| 1833 | - $cookie = ''; |
|
| 1834 | - if(isset($this->cookies[$cacheKey])) { |
|
| 1835 | - $cookie = $this->cookies[$cacheKey]; |
|
| 1836 | - if(is_null($cookie)) { |
|
| 1837 | - $cookie = ''; |
|
| 1838 | - } |
|
| 1839 | - } |
|
| 1840 | - return $cookie; |
|
| 1841 | - } |
|
| 1842 | - |
|
| 1843 | - /** |
|
| 1844 | - * checks whether an LDAP paged search operation has more pages that can be |
|
| 1845 | - * retrieved, typically when offset and limit are provided. |
|
| 1846 | - * |
|
| 1847 | - * Be very careful to use it: the last cookie value, which is inspected, can |
|
| 1848 | - * be reset by other operations. Best, call it immediately after a search(), |
|
| 1849 | - * searchUsers() or searchGroups() call. count-methods are probably safe as |
|
| 1850 | - * well. Don't rely on it with any fetchList-method. |
|
| 1851 | - * @return bool |
|
| 1852 | - */ |
|
| 1853 | - public function hasMoreResults() { |
|
| 1854 | - if(!$this->connection->hasPagedResultSupport) { |
|
| 1855 | - return false; |
|
| 1856 | - } |
|
| 1857 | - |
|
| 1858 | - if(empty($this->lastCookie) && $this->lastCookie !== '0') { |
|
| 1859 | - // as in RFC 2696, when all results are returned, the cookie will |
|
| 1860 | - // be empty. |
|
| 1861 | - return false; |
|
| 1862 | - } |
|
| 1863 | - |
|
| 1864 | - return true; |
|
| 1865 | - } |
|
| 1866 | - |
|
| 1867 | - /** |
|
| 1868 | - * set a cookie for LDAP paged search run |
|
| 1869 | - * @param string $base a string with the base DN for the search |
|
| 1870 | - * @param string $filter the search filter to identify the correct search |
|
| 1871 | - * @param int $limit the limit (or 'pageSize'), to identify the correct search well |
|
| 1872 | - * @param int $offset the offset for the run search to identify the correct search really good |
|
| 1873 | - * @param string $cookie string containing the cookie returned by ldap_control_paged_result_response |
|
| 1874 | - * @return void |
|
| 1875 | - */ |
|
| 1876 | - private function setPagedResultCookie($base, $filter, $limit, $offset, $cookie) { |
|
| 1877 | - // allow '0' for 389ds |
|
| 1878 | - if(!empty($cookie) || $cookie === '0') { |
|
| 1879 | - $cacheKey = 'lc' . crc32($base) . '-' . crc32($filter) . '-' .intval($limit) . '-' . intval($offset); |
|
| 1880 | - $this->cookies[$cacheKey] = $cookie; |
|
| 1881 | - $this->lastCookie = $cookie; |
|
| 1882 | - } |
|
| 1883 | - } |
|
| 1884 | - |
|
| 1885 | - /** |
|
| 1886 | - * Check whether the most recent paged search was successful. It flushed the state var. Use it always after a possible paged search. |
|
| 1887 | - * @return boolean|null true on success, null or false otherwise |
|
| 1888 | - */ |
|
| 1889 | - public function getPagedSearchResultState() { |
|
| 1890 | - $result = $this->pagedSearchedSuccessful; |
|
| 1891 | - $this->pagedSearchedSuccessful = null; |
|
| 1892 | - return $result; |
|
| 1893 | - } |
|
| 1894 | - |
|
| 1895 | - /** |
|
| 1896 | - * Prepares a paged search, if possible |
|
| 1897 | - * @param string $filter the LDAP filter for the search |
|
| 1898 | - * @param string[] $bases an array containing the LDAP subtree(s) that shall be searched |
|
| 1899 | - * @param string[] $attr optional, when a certain attribute shall be filtered outside |
|
| 1900 | - * @param int $limit |
|
| 1901 | - * @param int $offset |
|
| 1902 | - * @return bool|true |
|
| 1903 | - */ |
|
| 1904 | - private function initPagedSearch($filter, $bases, $attr, $limit, $offset) { |
|
| 1905 | - $pagedSearchOK = false; |
|
| 1906 | - if($this->connection->hasPagedResultSupport && ($limit !== 0)) { |
|
| 1907 | - $offset = intval($offset); //can be null |
|
| 1908 | - \OCP\Util::writeLog('user_ldap', |
|
| 1909 | - 'initializing paged search for Filter '.$filter.' base '.print_r($bases, true) |
|
| 1910 | - .' attr '.print_r($attr, true). ' limit ' .$limit.' offset '.$offset, |
|
| 1911 | - \OCP\Util::DEBUG); |
|
| 1912 | - //get the cookie from the search for the previous search, required by LDAP |
|
| 1913 | - foreach($bases as $base) { |
|
| 1914 | - |
|
| 1915 | - $cookie = $this->getPagedResultCookie($base, $filter, $limit, $offset); |
|
| 1916 | - if(empty($cookie) && $cookie !== "0" && ($offset > 0)) { |
|
| 1917 | - // no cookie known from a potential previous search. We need |
|
| 1918 | - // to start from 0 to come to the desired page. cookie value |
|
| 1919 | - // of '0' is valid, because 389ds |
|
| 1920 | - $reOffset = ($offset - $limit) < 0 ? 0 : $offset - $limit; |
|
| 1921 | - $this->search($filter, array($base), $attr, $limit, $reOffset, true); |
|
| 1922 | - $cookie = $this->getPagedResultCookie($base, $filter, $limit, $offset); |
|
| 1923 | - //still no cookie? obviously, the server does not like us. Let's skip paging efforts. |
|
| 1924 | - // '0' is valid, because 389ds |
|
| 1925 | - //TODO: remember this, probably does not change in the next request... |
|
| 1926 | - if(empty($cookie) && $cookie !== '0') { |
|
| 1927 | - $cookie = null; |
|
| 1928 | - } |
|
| 1929 | - } |
|
| 1930 | - if(!is_null($cookie)) { |
|
| 1931 | - //since offset = 0, this is a new search. We abandon other searches that might be ongoing. |
|
| 1932 | - $this->abandonPagedSearch(); |
|
| 1933 | - $pagedSearchOK = $this->invokeLDAPMethod('controlPagedResult', |
|
| 1934 | - $this->connection->getConnectionResource(), $limit, |
|
| 1935 | - false, $cookie); |
|
| 1936 | - if(!$pagedSearchOK) { |
|
| 1937 | - return false; |
|
| 1938 | - } |
|
| 1939 | - \OCP\Util::writeLog('user_ldap', 'Ready for a paged search', \OCP\Util::DEBUG); |
|
| 1940 | - } else { |
|
| 1941 | - $e = new \Exception('No paged search possible, Limit '.$limit.' Offset '.$offset); |
|
| 1942 | - \OC::$server->getLogger()->logException($e, ['level' => Util::DEBUG]); |
|
| 1943 | - } |
|
| 1944 | - |
|
| 1945 | - } |
|
| 1946 | - /* ++ Fixing RHDS searches with pages with zero results ++ |
|
| 1702 | + \OC::$server->getLogger()->info( |
|
| 1703 | + 'Passed string does not resemble a valid GUID. Known UUID ' . |
|
| 1704 | + '({uuid}) probably does not match UUID configuration.', |
|
| 1705 | + [ 'app' => 'user_ldap', 'uuid' => $guid ] |
|
| 1706 | + ); |
|
| 1707 | + return $guid; |
|
| 1708 | + } |
|
| 1709 | + for($i=0; $i < 3; $i++) { |
|
| 1710 | + $pairs = str_split($blocks[$i], 2); |
|
| 1711 | + $pairs = array_reverse($pairs); |
|
| 1712 | + $blocks[$i] = implode('', $pairs); |
|
| 1713 | + } |
|
| 1714 | + for($i=0; $i < 5; $i++) { |
|
| 1715 | + $pairs = str_split($blocks[$i], 2); |
|
| 1716 | + $blocks[$i] = '\\' . implode('\\', $pairs); |
|
| 1717 | + } |
|
| 1718 | + return implode('', $blocks); |
|
| 1719 | + } |
|
| 1720 | + |
|
| 1721 | + /** |
|
| 1722 | + * gets a SID of the domain of the given dn |
|
| 1723 | + * @param string $dn |
|
| 1724 | + * @return string|bool |
|
| 1725 | + */ |
|
| 1726 | + public function getSID($dn) { |
|
| 1727 | + $domainDN = $this->getDomainDNFromDN($dn); |
|
| 1728 | + $cacheKey = 'getSID-'.$domainDN; |
|
| 1729 | + $sid = $this->connection->getFromCache($cacheKey); |
|
| 1730 | + if(!is_null($sid)) { |
|
| 1731 | + return $sid; |
|
| 1732 | + } |
|
| 1733 | + |
|
| 1734 | + $objectSid = $this->readAttribute($domainDN, 'objectsid'); |
|
| 1735 | + if(!is_array($objectSid) || empty($objectSid)) { |
|
| 1736 | + $this->connection->writeToCache($cacheKey, false); |
|
| 1737 | + return false; |
|
| 1738 | + } |
|
| 1739 | + $domainObjectSid = $this->convertSID2Str($objectSid[0]); |
|
| 1740 | + $this->connection->writeToCache($cacheKey, $domainObjectSid); |
|
| 1741 | + |
|
| 1742 | + return $domainObjectSid; |
|
| 1743 | + } |
|
| 1744 | + |
|
| 1745 | + /** |
|
| 1746 | + * converts a binary SID into a string representation |
|
| 1747 | + * @param string $sid |
|
| 1748 | + * @return string |
|
| 1749 | + */ |
|
| 1750 | + public function convertSID2Str($sid) { |
|
| 1751 | + // The format of a SID binary string is as follows: |
|
| 1752 | + // 1 byte for the revision level |
|
| 1753 | + // 1 byte for the number n of variable sub-ids |
|
| 1754 | + // 6 bytes for identifier authority value |
|
| 1755 | + // n*4 bytes for n sub-ids |
|
| 1756 | + // |
|
| 1757 | + // Example: 010400000000000515000000a681e50e4d6c6c2bca32055f |
|
| 1758 | + // Legend: RRNNAAAAAAAAAAAA11111111222222223333333344444444 |
|
| 1759 | + $revision = ord($sid[0]); |
|
| 1760 | + $numberSubID = ord($sid[1]); |
|
| 1761 | + |
|
| 1762 | + $subIdStart = 8; // 1 + 1 + 6 |
|
| 1763 | + $subIdLength = 4; |
|
| 1764 | + if (strlen($sid) !== $subIdStart + $subIdLength * $numberSubID) { |
|
| 1765 | + // Incorrect number of bytes present. |
|
| 1766 | + return ''; |
|
| 1767 | + } |
|
| 1768 | + |
|
| 1769 | + // 6 bytes = 48 bits can be represented using floats without loss of |
|
| 1770 | + // precision (see https://gist.github.com/bantu/886ac680b0aef5812f71) |
|
| 1771 | + $iav = number_format(hexdec(bin2hex(substr($sid, 2, 6))), 0, '', ''); |
|
| 1772 | + |
|
| 1773 | + $subIDs = array(); |
|
| 1774 | + for ($i = 0; $i < $numberSubID; $i++) { |
|
| 1775 | + $subID = unpack('V', substr($sid, $subIdStart + $subIdLength * $i, $subIdLength)); |
|
| 1776 | + $subIDs[] = sprintf('%u', $subID[1]); |
|
| 1777 | + } |
|
| 1778 | + |
|
| 1779 | + // Result for example above: S-1-5-21-249921958-728525901-1594176202 |
|
| 1780 | + return sprintf('S-%d-%s-%s', $revision, $iav, implode('-', $subIDs)); |
|
| 1781 | + } |
|
| 1782 | + |
|
| 1783 | + /** |
|
| 1784 | + * checks if the given DN is part of the given base DN(s) |
|
| 1785 | + * @param string $dn the DN |
|
| 1786 | + * @param string[] $bases array containing the allowed base DN or DNs |
|
| 1787 | + * @return bool |
|
| 1788 | + */ |
|
| 1789 | + public function isDNPartOfBase($dn, $bases) { |
|
| 1790 | + $belongsToBase = false; |
|
| 1791 | + $bases = $this->helper->sanitizeDN($bases); |
|
| 1792 | + |
|
| 1793 | + foreach($bases as $base) { |
|
| 1794 | + $belongsToBase = true; |
|
| 1795 | + if(mb_strripos($dn, $base, 0, 'UTF-8') !== (mb_strlen($dn, 'UTF-8')-mb_strlen($base, 'UTF-8'))) { |
|
| 1796 | + $belongsToBase = false; |
|
| 1797 | + } |
|
| 1798 | + if($belongsToBase) { |
|
| 1799 | + break; |
|
| 1800 | + } |
|
| 1801 | + } |
|
| 1802 | + return $belongsToBase; |
|
| 1803 | + } |
|
| 1804 | + |
|
| 1805 | + /** |
|
| 1806 | + * resets a running Paged Search operation |
|
| 1807 | + */ |
|
| 1808 | + private function abandonPagedSearch() { |
|
| 1809 | + if($this->connection->hasPagedResultSupport) { |
|
| 1810 | + $cr = $this->connection->getConnectionResource(); |
|
| 1811 | + $this->invokeLDAPMethod('controlPagedResult', $cr, 0, false, $this->lastCookie); |
|
| 1812 | + $this->getPagedSearchResultState(); |
|
| 1813 | + $this->lastCookie = ''; |
|
| 1814 | + $this->cookies = array(); |
|
| 1815 | + } |
|
| 1816 | + } |
|
| 1817 | + |
|
| 1818 | + /** |
|
| 1819 | + * get a cookie for the next LDAP paged search |
|
| 1820 | + * @param string $base a string with the base DN for the search |
|
| 1821 | + * @param string $filter the search filter to identify the correct search |
|
| 1822 | + * @param int $limit the limit (or 'pageSize'), to identify the correct search well |
|
| 1823 | + * @param int $offset the offset for the new search to identify the correct search really good |
|
| 1824 | + * @return string containing the key or empty if none is cached |
|
| 1825 | + */ |
|
| 1826 | + private function getPagedResultCookie($base, $filter, $limit, $offset) { |
|
| 1827 | + if($offset === 0) { |
|
| 1828 | + return ''; |
|
| 1829 | + } |
|
| 1830 | + $offset -= $limit; |
|
| 1831 | + //we work with cache here |
|
| 1832 | + $cacheKey = 'lc' . crc32($base) . '-' . crc32($filter) . '-' . intval($limit) . '-' . intval($offset); |
|
| 1833 | + $cookie = ''; |
|
| 1834 | + if(isset($this->cookies[$cacheKey])) { |
|
| 1835 | + $cookie = $this->cookies[$cacheKey]; |
|
| 1836 | + if(is_null($cookie)) { |
|
| 1837 | + $cookie = ''; |
|
| 1838 | + } |
|
| 1839 | + } |
|
| 1840 | + return $cookie; |
|
| 1841 | + } |
|
| 1842 | + |
|
| 1843 | + /** |
|
| 1844 | + * checks whether an LDAP paged search operation has more pages that can be |
|
| 1845 | + * retrieved, typically when offset and limit are provided. |
|
| 1846 | + * |
|
| 1847 | + * Be very careful to use it: the last cookie value, which is inspected, can |
|
| 1848 | + * be reset by other operations. Best, call it immediately after a search(), |
|
| 1849 | + * searchUsers() or searchGroups() call. count-methods are probably safe as |
|
| 1850 | + * well. Don't rely on it with any fetchList-method. |
|
| 1851 | + * @return bool |
|
| 1852 | + */ |
|
| 1853 | + public function hasMoreResults() { |
|
| 1854 | + if(!$this->connection->hasPagedResultSupport) { |
|
| 1855 | + return false; |
|
| 1856 | + } |
|
| 1857 | + |
|
| 1858 | + if(empty($this->lastCookie) && $this->lastCookie !== '0') { |
|
| 1859 | + // as in RFC 2696, when all results are returned, the cookie will |
|
| 1860 | + // be empty. |
|
| 1861 | + return false; |
|
| 1862 | + } |
|
| 1863 | + |
|
| 1864 | + return true; |
|
| 1865 | + } |
|
| 1866 | + |
|
| 1867 | + /** |
|
| 1868 | + * set a cookie for LDAP paged search run |
|
| 1869 | + * @param string $base a string with the base DN for the search |
|
| 1870 | + * @param string $filter the search filter to identify the correct search |
|
| 1871 | + * @param int $limit the limit (or 'pageSize'), to identify the correct search well |
|
| 1872 | + * @param int $offset the offset for the run search to identify the correct search really good |
|
| 1873 | + * @param string $cookie string containing the cookie returned by ldap_control_paged_result_response |
|
| 1874 | + * @return void |
|
| 1875 | + */ |
|
| 1876 | + private function setPagedResultCookie($base, $filter, $limit, $offset, $cookie) { |
|
| 1877 | + // allow '0' for 389ds |
|
| 1878 | + if(!empty($cookie) || $cookie === '0') { |
|
| 1879 | + $cacheKey = 'lc' . crc32($base) . '-' . crc32($filter) . '-' .intval($limit) . '-' . intval($offset); |
|
| 1880 | + $this->cookies[$cacheKey] = $cookie; |
|
| 1881 | + $this->lastCookie = $cookie; |
|
| 1882 | + } |
|
| 1883 | + } |
|
| 1884 | + |
|
| 1885 | + /** |
|
| 1886 | + * Check whether the most recent paged search was successful. It flushed the state var. Use it always after a possible paged search. |
|
| 1887 | + * @return boolean|null true on success, null or false otherwise |
|
| 1888 | + */ |
|
| 1889 | + public function getPagedSearchResultState() { |
|
| 1890 | + $result = $this->pagedSearchedSuccessful; |
|
| 1891 | + $this->pagedSearchedSuccessful = null; |
|
| 1892 | + return $result; |
|
| 1893 | + } |
|
| 1894 | + |
|
| 1895 | + /** |
|
| 1896 | + * Prepares a paged search, if possible |
|
| 1897 | + * @param string $filter the LDAP filter for the search |
|
| 1898 | + * @param string[] $bases an array containing the LDAP subtree(s) that shall be searched |
|
| 1899 | + * @param string[] $attr optional, when a certain attribute shall be filtered outside |
|
| 1900 | + * @param int $limit |
|
| 1901 | + * @param int $offset |
|
| 1902 | + * @return bool|true |
|
| 1903 | + */ |
|
| 1904 | + private function initPagedSearch($filter, $bases, $attr, $limit, $offset) { |
|
| 1905 | + $pagedSearchOK = false; |
|
| 1906 | + if($this->connection->hasPagedResultSupport && ($limit !== 0)) { |
|
| 1907 | + $offset = intval($offset); //can be null |
|
| 1908 | + \OCP\Util::writeLog('user_ldap', |
|
| 1909 | + 'initializing paged search for Filter '.$filter.' base '.print_r($bases, true) |
|
| 1910 | + .' attr '.print_r($attr, true). ' limit ' .$limit.' offset '.$offset, |
|
| 1911 | + \OCP\Util::DEBUG); |
|
| 1912 | + //get the cookie from the search for the previous search, required by LDAP |
|
| 1913 | + foreach($bases as $base) { |
|
| 1914 | + |
|
| 1915 | + $cookie = $this->getPagedResultCookie($base, $filter, $limit, $offset); |
|
| 1916 | + if(empty($cookie) && $cookie !== "0" && ($offset > 0)) { |
|
| 1917 | + // no cookie known from a potential previous search. We need |
|
| 1918 | + // to start from 0 to come to the desired page. cookie value |
|
| 1919 | + // of '0' is valid, because 389ds |
|
| 1920 | + $reOffset = ($offset - $limit) < 0 ? 0 : $offset - $limit; |
|
| 1921 | + $this->search($filter, array($base), $attr, $limit, $reOffset, true); |
|
| 1922 | + $cookie = $this->getPagedResultCookie($base, $filter, $limit, $offset); |
|
| 1923 | + //still no cookie? obviously, the server does not like us. Let's skip paging efforts. |
|
| 1924 | + // '0' is valid, because 389ds |
|
| 1925 | + //TODO: remember this, probably does not change in the next request... |
|
| 1926 | + if(empty($cookie) && $cookie !== '0') { |
|
| 1927 | + $cookie = null; |
|
| 1928 | + } |
|
| 1929 | + } |
|
| 1930 | + if(!is_null($cookie)) { |
|
| 1931 | + //since offset = 0, this is a new search. We abandon other searches that might be ongoing. |
|
| 1932 | + $this->abandonPagedSearch(); |
|
| 1933 | + $pagedSearchOK = $this->invokeLDAPMethod('controlPagedResult', |
|
| 1934 | + $this->connection->getConnectionResource(), $limit, |
|
| 1935 | + false, $cookie); |
|
| 1936 | + if(!$pagedSearchOK) { |
|
| 1937 | + return false; |
|
| 1938 | + } |
|
| 1939 | + \OCP\Util::writeLog('user_ldap', 'Ready for a paged search', \OCP\Util::DEBUG); |
|
| 1940 | + } else { |
|
| 1941 | + $e = new \Exception('No paged search possible, Limit '.$limit.' Offset '.$offset); |
|
| 1942 | + \OC::$server->getLogger()->logException($e, ['level' => Util::DEBUG]); |
|
| 1943 | + } |
|
| 1944 | + |
|
| 1945 | + } |
|
| 1946 | + /* ++ Fixing RHDS searches with pages with zero results ++ |
|
| 1947 | 1947 | * We coudn't get paged searches working with our RHDS for login ($limit = 0), |
| 1948 | 1948 | * due to pages with zero results. |
| 1949 | 1949 | * So we added "&& !empty($this->lastCookie)" to this test to ignore pagination |
| 1950 | 1950 | * if we don't have a previous paged search. |
| 1951 | 1951 | */ |
| 1952 | - } else if($this->connection->hasPagedResultSupport && $limit === 0 && !empty($this->lastCookie)) { |
|
| 1953 | - // a search without limit was requested. However, if we do use |
|
| 1954 | - // Paged Search once, we always must do it. This requires us to |
|
| 1955 | - // initialize it with the configured page size. |
|
| 1956 | - $this->abandonPagedSearch(); |
|
| 1957 | - // in case someone set it to 0 … use 500, otherwise no results will |
|
| 1958 | - // be returned. |
|
| 1959 | - $pageSize = intval($this->connection->ldapPagingSize) > 0 ? intval($this->connection->ldapPagingSize) : 500; |
|
| 1960 | - $pagedSearchOK = $this->invokeLDAPMethod('controlPagedResult', |
|
| 1961 | - $this->connection->getConnectionResource(), |
|
| 1962 | - $pageSize, false, ''); |
|
| 1963 | - } |
|
| 1964 | - |
|
| 1965 | - return $pagedSearchOK; |
|
| 1966 | - } |
|
| 1952 | + } else if($this->connection->hasPagedResultSupport && $limit === 0 && !empty($this->lastCookie)) { |
|
| 1953 | + // a search without limit was requested. However, if we do use |
|
| 1954 | + // Paged Search once, we always must do it. This requires us to |
|
| 1955 | + // initialize it with the configured page size. |
|
| 1956 | + $this->abandonPagedSearch(); |
|
| 1957 | + // in case someone set it to 0 … use 500, otherwise no results will |
|
| 1958 | + // be returned. |
|
| 1959 | + $pageSize = intval($this->connection->ldapPagingSize) > 0 ? intval($this->connection->ldapPagingSize) : 500; |
|
| 1960 | + $pagedSearchOK = $this->invokeLDAPMethod('controlPagedResult', |
|
| 1961 | + $this->connection->getConnectionResource(), |
|
| 1962 | + $pageSize, false, ''); |
|
| 1963 | + } |
|
| 1964 | + |
|
| 1965 | + return $pagedSearchOK; |
|
| 1966 | + } |
|
| 1967 | 1967 | |
| 1968 | 1968 | } |
@@ -125,7 +125,7 @@ discard block |
||
| 125 | 125 | * @return AbstractMapping |
| 126 | 126 | */ |
| 127 | 127 | public function getUserMapper() { |
| 128 | - if(is_null($this->userMapper)) { |
|
| 128 | + if (is_null($this->userMapper)) { |
|
| 129 | 129 | throw new \Exception('UserMapper was not assigned to this Access instance.'); |
| 130 | 130 | } |
| 131 | 131 | return $this->userMapper; |
@@ -145,7 +145,7 @@ discard block |
||
| 145 | 145 | * @return AbstractMapping |
| 146 | 146 | */ |
| 147 | 147 | public function getGroupMapper() { |
| 148 | - if(is_null($this->groupMapper)) { |
|
| 148 | + if (is_null($this->groupMapper)) { |
|
| 149 | 149 | throw new \Exception('GroupMapper was not assigned to this Access instance.'); |
| 150 | 150 | } |
| 151 | 151 | return $this->groupMapper; |
@@ -178,14 +178,14 @@ discard block |
||
| 178 | 178 | * @throws ServerNotAvailableException |
| 179 | 179 | */ |
| 180 | 180 | public function readAttribute($dn, $attr, $filter = 'objectClass=*') { |
| 181 | - if(!$this->checkConnection()) { |
|
| 181 | + if (!$this->checkConnection()) { |
|
| 182 | 182 | \OCP\Util::writeLog('user_ldap', |
| 183 | 183 | 'No LDAP Connector assigned, access impossible for readAttribute.', |
| 184 | 184 | \OCP\Util::WARN); |
| 185 | 185 | return false; |
| 186 | 186 | } |
| 187 | 187 | $cr = $this->connection->getConnectionResource(); |
| 188 | - if(!$this->ldap->isResource($cr)) { |
|
| 188 | + if (!$this->ldap->isResource($cr)) { |
|
| 189 | 189 | //LDAP not available |
| 190 | 190 | \OCP\Util::writeLog('user_ldap', 'LDAP resource not available.', \OCP\Util::DEBUG); |
| 191 | 191 | return false; |
@@ -208,7 +208,7 @@ discard block |
||
| 208 | 208 | $isRangeRequest = false; |
| 209 | 209 | do { |
| 210 | 210 | $result = $this->executeRead($cr, $dn, $attrToRead, $filter, $maxResults); |
| 211 | - if(is_bool($result)) { |
|
| 211 | + if (is_bool($result)) { |
|
| 212 | 212 | // when an exists request was run and it was successful, an empty |
| 213 | 213 | // array must be returned |
| 214 | 214 | return $result ? [] : false; |
@@ -225,22 +225,22 @@ discard block |
||
| 225 | 225 | $result = $this->extractRangeData($result, $attr); |
| 226 | 226 | if (!empty($result)) { |
| 227 | 227 | $normalizedResult = $this->extractAttributeValuesFromResult( |
| 228 | - [ $attr => $result['values'] ], |
|
| 228 | + [$attr => $result['values']], |
|
| 229 | 229 | $attr |
| 230 | 230 | ); |
| 231 | 231 | $values = array_merge($values, $normalizedResult); |
| 232 | 232 | |
| 233 | - if($result['rangeHigh'] === '*') { |
|
| 233 | + if ($result['rangeHigh'] === '*') { |
|
| 234 | 234 | // when server replies with * as high range value, there are |
| 235 | 235 | // no more results left |
| 236 | 236 | return $values; |
| 237 | 237 | } else { |
| 238 | - $low = $result['rangeHigh'] + 1; |
|
| 239 | - $attrToRead = $result['attributeName'] . ';range=' . $low . '-*'; |
|
| 238 | + $low = $result['rangeHigh'] + 1; |
|
| 239 | + $attrToRead = $result['attributeName'].';range='.$low.'-*'; |
|
| 240 | 240 | $isRangeRequest = true; |
| 241 | 241 | } |
| 242 | 242 | } |
| 243 | - } while($isRangeRequest); |
|
| 243 | + } while ($isRangeRequest); |
|
| 244 | 244 | |
| 245 | 245 | \OCP\Util::writeLog('user_ldap', 'Requested attribute '.$attr.' not found for '.$dn, \OCP\Util::DEBUG); |
| 246 | 246 | return false; |
@@ -266,13 +266,13 @@ discard block |
||
| 266 | 266 | if (!$this->ldap->isResource($rr)) { |
| 267 | 267 | if ($attribute !== '') { |
| 268 | 268 | //do not throw this message on userExists check, irritates |
| 269 | - \OCP\Util::writeLog('user_ldap', 'readAttribute failed for DN ' . $dn, \OCP\Util::DEBUG); |
|
| 269 | + \OCP\Util::writeLog('user_ldap', 'readAttribute failed for DN '.$dn, \OCP\Util::DEBUG); |
|
| 270 | 270 | } |
| 271 | 271 | //in case an error occurs , e.g. object does not exist |
| 272 | 272 | return false; |
| 273 | 273 | } |
| 274 | 274 | if ($attribute === '' && ($filter === 'objectclass=*' || $this->invokeLDAPMethod('countEntries', $cr, $rr) === 1)) { |
| 275 | - \OCP\Util::writeLog('user_ldap', 'readAttribute: ' . $dn . ' found', \OCP\Util::DEBUG); |
|
| 275 | + \OCP\Util::writeLog('user_ldap', 'readAttribute: '.$dn.' found', \OCP\Util::DEBUG); |
|
| 276 | 276 | return true; |
| 277 | 277 | } |
| 278 | 278 | $er = $this->invokeLDAPMethod('firstEntry', $cr, $rr); |
@@ -297,12 +297,12 @@ discard block |
||
| 297 | 297 | */ |
| 298 | 298 | public function extractAttributeValuesFromResult($result, $attribute) { |
| 299 | 299 | $values = []; |
| 300 | - if(isset($result[$attribute]) && $result[$attribute]['count'] > 0) { |
|
| 300 | + if (isset($result[$attribute]) && $result[$attribute]['count'] > 0) { |
|
| 301 | 301 | $lowercaseAttribute = strtolower($attribute); |
| 302 | - for($i=0;$i<$result[$attribute]['count'];$i++) { |
|
| 303 | - if($this->resemblesDN($attribute)) { |
|
| 302 | + for ($i = 0; $i < $result[$attribute]['count']; $i++) { |
|
| 303 | + if ($this->resemblesDN($attribute)) { |
|
| 304 | 304 | $values[] = $this->helper->sanitizeDN($result[$attribute][$i]); |
| 305 | - } elseif($lowercaseAttribute === 'objectguid' || $lowercaseAttribute === 'guid') { |
|
| 305 | + } elseif ($lowercaseAttribute === 'objectguid' || $lowercaseAttribute === 'guid') { |
|
| 306 | 306 | $values[] = $this->convertObjectGUID2Str($result[$attribute][$i]); |
| 307 | 307 | } else { |
| 308 | 308 | $values[] = $result[$attribute][$i]; |
@@ -324,10 +324,10 @@ discard block |
||
| 324 | 324 | */ |
| 325 | 325 | public function extractRangeData($result, $attribute) { |
| 326 | 326 | $keys = array_keys($result); |
| 327 | - foreach($keys as $key) { |
|
| 328 | - if($key !== $attribute && strpos($key, $attribute) === 0) { |
|
| 327 | + foreach ($keys as $key) { |
|
| 328 | + if ($key !== $attribute && strpos($key, $attribute) === 0) { |
|
| 329 | 329 | $queryData = explode(';', $key); |
| 330 | - if(strpos($queryData[1], 'range=') === 0) { |
|
| 330 | + if (strpos($queryData[1], 'range=') === 0) { |
|
| 331 | 331 | $high = substr($queryData[1], 1 + strpos($queryData[1], '-')); |
| 332 | 332 | $data = [ |
| 333 | 333 | 'values' => $result[$key], |
@@ -352,18 +352,18 @@ discard block |
||
| 352 | 352 | * @throws \Exception |
| 353 | 353 | */ |
| 354 | 354 | public function setPassword($userDN, $password) { |
| 355 | - if(intval($this->connection->turnOnPasswordChange) !== 1) { |
|
| 355 | + if (intval($this->connection->turnOnPasswordChange) !== 1) { |
|
| 356 | 356 | throw new \Exception('LDAP password changes are disabled.'); |
| 357 | 357 | } |
| 358 | 358 | $cr = $this->connection->getConnectionResource(); |
| 359 | - if(!$this->ldap->isResource($cr)) { |
|
| 359 | + if (!$this->ldap->isResource($cr)) { |
|
| 360 | 360 | //LDAP not available |
| 361 | 361 | \OCP\Util::writeLog('user_ldap', 'LDAP resource not available.', \OCP\Util::DEBUG); |
| 362 | 362 | return false; |
| 363 | 363 | } |
| 364 | 364 | try { |
| 365 | 365 | return @$this->invokeLDAPMethod('modReplace', $cr, $userDN, $password); |
| 366 | - } catch(ConstraintViolationException $e) { |
|
| 366 | + } catch (ConstraintViolationException $e) { |
|
| 367 | 367 | throw new HintException('Password change rejected.', \OC::$server->getL10N('user_ldap')->t('Password change rejected. Hint: ').$e->getMessage(), $e->getCode()); |
| 368 | 368 | } |
| 369 | 369 | } |
@@ -405,17 +405,17 @@ discard block |
||
| 405 | 405 | */ |
| 406 | 406 | public function getDomainDNFromDN($dn) { |
| 407 | 407 | $allParts = $this->ldap->explodeDN($dn, 0); |
| 408 | - if($allParts === false) { |
|
| 408 | + if ($allParts === false) { |
|
| 409 | 409 | //not a valid DN |
| 410 | 410 | return ''; |
| 411 | 411 | } |
| 412 | 412 | $domainParts = array(); |
| 413 | 413 | $dcFound = false; |
| 414 | - foreach($allParts as $part) { |
|
| 415 | - if(!$dcFound && strpos($part, 'dc=') === 0) { |
|
| 414 | + foreach ($allParts as $part) { |
|
| 415 | + if (!$dcFound && strpos($part, 'dc=') === 0) { |
|
| 416 | 416 | $dcFound = true; |
| 417 | 417 | } |
| 418 | - if($dcFound) { |
|
| 418 | + if ($dcFound) { |
|
| 419 | 419 | $domainParts[] = $part; |
| 420 | 420 | } |
| 421 | 421 | } |
@@ -442,7 +442,7 @@ discard block |
||
| 442 | 442 | |
| 443 | 443 | //Check whether the DN belongs to the Base, to avoid issues on multi- |
| 444 | 444 | //server setups |
| 445 | - if(is_string($fdn) && $this->isDNPartOfBase($fdn, $this->connection->ldapBaseUsers)) { |
|
| 445 | + if (is_string($fdn) && $this->isDNPartOfBase($fdn, $this->connection->ldapBaseUsers)) { |
|
| 446 | 446 | return $fdn; |
| 447 | 447 | } |
| 448 | 448 | |
@@ -459,7 +459,7 @@ discard block |
||
| 459 | 459 | //To avoid bypassing the base DN settings under certain circumstances |
| 460 | 460 | //with the group support, check whether the provided DN matches one of |
| 461 | 461 | //the given Bases |
| 462 | - if(!$this->isDNPartOfBase($fdn, $this->connection->ldapBaseGroups)) { |
|
| 462 | + if (!$this->isDNPartOfBase($fdn, $this->connection->ldapBaseGroups)) { |
|
| 463 | 463 | return false; |
| 464 | 464 | } |
| 465 | 465 | |
@@ -476,11 +476,11 @@ discard block |
||
| 476 | 476 | */ |
| 477 | 477 | public function groupsMatchFilter($groupDNs) { |
| 478 | 478 | $validGroupDNs = []; |
| 479 | - foreach($groupDNs as $dn) { |
|
| 479 | + foreach ($groupDNs as $dn) { |
|
| 480 | 480 | $cacheKey = 'groupsMatchFilter-'.$dn; |
| 481 | 481 | $groupMatchFilter = $this->connection->getFromCache($cacheKey); |
| 482 | - if(!is_null($groupMatchFilter)) { |
|
| 483 | - if($groupMatchFilter) { |
|
| 482 | + if (!is_null($groupMatchFilter)) { |
|
| 483 | + if ($groupMatchFilter) { |
|
| 484 | 484 | $validGroupDNs[] = $dn; |
| 485 | 485 | } |
| 486 | 486 | continue; |
@@ -488,13 +488,13 @@ discard block |
||
| 488 | 488 | |
| 489 | 489 | // Check the base DN first. If this is not met already, we don't |
| 490 | 490 | // need to ask the server at all. |
| 491 | - if(!$this->isDNPartOfBase($dn, $this->connection->ldapBaseGroups)) { |
|
| 491 | + if (!$this->isDNPartOfBase($dn, $this->connection->ldapBaseGroups)) { |
|
| 492 | 492 | $this->connection->writeToCache($cacheKey, false); |
| 493 | 493 | continue; |
| 494 | 494 | } |
| 495 | 495 | |
| 496 | 496 | $result = $this->readAttribute($dn, 'cn', $this->connection->ldapGroupFilter); |
| 497 | - if(is_array($result)) { |
|
| 497 | + if (is_array($result)) { |
|
| 498 | 498 | $this->connection->writeToCache($cacheKey, true); |
| 499 | 499 | $validGroupDNs[] = $dn; |
| 500 | 500 | } else { |
@@ -515,7 +515,7 @@ discard block |
||
| 515 | 515 | //To avoid bypassing the base DN settings under certain circumstances |
| 516 | 516 | //with the group support, check whether the provided DN matches one of |
| 517 | 517 | //the given Bases |
| 518 | - if(!$this->isDNPartOfBase($fdn, $this->connection->ldapBaseUsers)) { |
|
| 518 | + if (!$this->isDNPartOfBase($fdn, $this->connection->ldapBaseUsers)) { |
|
| 519 | 519 | return false; |
| 520 | 520 | } |
| 521 | 521 | |
@@ -535,7 +535,7 @@ discard block |
||
| 535 | 535 | */ |
| 536 | 536 | public function dn2ocname($fdn, $ldapName = null, $isUser = true, &$newlyMapped = null, array $record = null) { |
| 537 | 537 | $newlyMapped = false; |
| 538 | - if($isUser) { |
|
| 538 | + if ($isUser) { |
|
| 539 | 539 | $mapper = $this->getUserMapper(); |
| 540 | 540 | $nameAttribute = $this->connection->ldapUserDisplayName; |
| 541 | 541 | } else { |
@@ -545,15 +545,15 @@ discard block |
||
| 545 | 545 | |
| 546 | 546 | //let's try to retrieve the Nextcloud name from the mappings table |
| 547 | 547 | $ncName = $mapper->getNameByDN($fdn); |
| 548 | - if(is_string($ncName)) { |
|
| 548 | + if (is_string($ncName)) { |
|
| 549 | 549 | return $ncName; |
| 550 | 550 | } |
| 551 | 551 | |
| 552 | 552 | //second try: get the UUID and check if it is known. Then, update the DN and return the name. |
| 553 | 553 | $uuid = $this->getUUID($fdn, $isUser, $record); |
| 554 | - if(is_string($uuid)) { |
|
| 554 | + if (is_string($uuid)) { |
|
| 555 | 555 | $ncName = $mapper->getNameByUUID($uuid); |
| 556 | - if(is_string($ncName)) { |
|
| 556 | + if (is_string($ncName)) { |
|
| 557 | 557 | $mapper->setDNbyUUID($fdn, $uuid); |
| 558 | 558 | return $ncName; |
| 559 | 559 | } |
@@ -563,16 +563,16 @@ discard block |
||
| 563 | 563 | return false; |
| 564 | 564 | } |
| 565 | 565 | |
| 566 | - if(is_null($ldapName)) { |
|
| 566 | + if (is_null($ldapName)) { |
|
| 567 | 567 | $ldapName = $this->readAttribute($fdn, $nameAttribute); |
| 568 | - if(!isset($ldapName[0]) && empty($ldapName[0])) { |
|
| 568 | + if (!isset($ldapName[0]) && empty($ldapName[0])) { |
|
| 569 | 569 | \OCP\Util::writeLog('user_ldap', 'No or empty name for '.$fdn.'.', \OCP\Util::INFO); |
| 570 | 570 | return false; |
| 571 | 571 | } |
| 572 | 572 | $ldapName = $ldapName[0]; |
| 573 | 573 | } |
| 574 | 574 | |
| 575 | - if($isUser) { |
|
| 575 | + if ($isUser) { |
|
| 576 | 576 | $usernameAttribute = strval($this->connection->ldapExpertUsernameAttr); |
| 577 | 577 | if ($usernameAttribute !== '') { |
| 578 | 578 | $username = $this->readAttribute($fdn, $usernameAttribute); |
@@ -591,9 +591,9 @@ discard block |
||
| 591 | 591 | // outside of core user management will still cache the user as non-existing. |
| 592 | 592 | $originalTTL = $this->connection->ldapCacheTTL; |
| 593 | 593 | $this->connection->setConfiguration(array('ldapCacheTTL' => 0)); |
| 594 | - if(($isUser && $intName !== '' && !\OC::$server->getUserManager()->userExists($intName)) |
|
| 594 | + if (($isUser && $intName !== '' && !\OC::$server->getUserManager()->userExists($intName)) |
|
| 595 | 595 | || (!$isUser && !\OC::$server->getGroupManager()->groupExists($intName))) { |
| 596 | - if($mapper->map($fdn, $intName, $uuid)) { |
|
| 596 | + if ($mapper->map($fdn, $intName, $uuid)) { |
|
| 597 | 597 | $this->connection->setConfiguration(array('ldapCacheTTL' => $originalTTL)); |
| 598 | 598 | $newlyMapped = true; |
| 599 | 599 | return $intName; |
@@ -602,7 +602,7 @@ discard block |
||
| 602 | 602 | $this->connection->setConfiguration(array('ldapCacheTTL' => $originalTTL)); |
| 603 | 603 | |
| 604 | 604 | $altName = $this->createAltInternalOwnCloudName($intName, $isUser); |
| 605 | - if(is_string($altName) && $mapper->map($fdn, $altName, $uuid)) { |
|
| 605 | + if (is_string($altName) && $mapper->map($fdn, $altName, $uuid)) { |
|
| 606 | 606 | $newlyMapped = true; |
| 607 | 607 | return $altName; |
| 608 | 608 | } |
@@ -640,7 +640,7 @@ discard block |
||
| 640 | 640 | * @return array |
| 641 | 641 | */ |
| 642 | 642 | private function ldap2NextcloudNames($ldapObjects, $isUsers) { |
| 643 | - if($isUsers) { |
|
| 643 | + if ($isUsers) { |
|
| 644 | 644 | $nameAttribute = $this->connection->ldapUserDisplayName; |
| 645 | 645 | $sndAttribute = $this->connection->ldapUserDisplayName2; |
| 646 | 646 | } else { |
@@ -648,9 +648,9 @@ discard block |
||
| 648 | 648 | } |
| 649 | 649 | $nextcloudNames = array(); |
| 650 | 650 | |
| 651 | - foreach($ldapObjects as $ldapObject) { |
|
| 651 | + foreach ($ldapObjects as $ldapObject) { |
|
| 652 | 652 | $nameByLDAP = null; |
| 653 | - if( isset($ldapObject[$nameAttribute]) |
|
| 653 | + if (isset($ldapObject[$nameAttribute]) |
|
| 654 | 654 | && is_array($ldapObject[$nameAttribute]) |
| 655 | 655 | && isset($ldapObject[$nameAttribute][0]) |
| 656 | 656 | ) { |
@@ -659,12 +659,12 @@ discard block |
||
| 659 | 659 | } |
| 660 | 660 | |
| 661 | 661 | $ncName = $this->dn2ocname($ldapObject['dn'][0], $nameByLDAP, $isUsers); |
| 662 | - if($ncName) { |
|
| 662 | + if ($ncName) { |
|
| 663 | 663 | $nextcloudNames[] = $ncName; |
| 664 | - if($isUsers) { |
|
| 664 | + if ($isUsers) { |
|
| 665 | 665 | //cache the user names so it does not need to be retrieved |
| 666 | 666 | //again later (e.g. sharing dialogue). |
| 667 | - if(is_null($nameByLDAP)) { |
|
| 667 | + if (is_null($nameByLDAP)) { |
|
| 668 | 668 | continue; |
| 669 | 669 | } |
| 670 | 670 | $sndName = isset($ldapObject[$sndAttribute][0]) |
@@ -702,7 +702,7 @@ discard block |
||
| 702 | 702 | */ |
| 703 | 703 | public function cacheUserDisplayName($ocName, $displayName, $displayName2 = '') { |
| 704 | 704 | $user = $this->userManager->get($ocName); |
| 705 | - if($user === null) { |
|
| 705 | + if ($user === null) { |
|
| 706 | 706 | return; |
| 707 | 707 | } |
| 708 | 708 | $displayName = $user->composeAndStoreDisplayName($displayName, $displayName2); |
@@ -722,9 +722,9 @@ discard block |
||
| 722 | 722 | $attempts = 0; |
| 723 | 723 | //while loop is just a precaution. If a name is not generated within |
| 724 | 724 | //20 attempts, something else is very wrong. Avoids infinite loop. |
| 725 | - while($attempts < 20){ |
|
| 726 | - $altName = $name . '_' . rand(1000,9999); |
|
| 727 | - if(!\OC::$server->getUserManager()->userExists($altName)) { |
|
| 725 | + while ($attempts < 20) { |
|
| 726 | + $altName = $name.'_'.rand(1000, 9999); |
|
| 727 | + if (!\OC::$server->getUserManager()->userExists($altName)) { |
|
| 728 | 728 | return $altName; |
| 729 | 729 | } |
| 730 | 730 | $attempts++; |
@@ -746,25 +746,25 @@ discard block |
||
| 746 | 746 | */ |
| 747 | 747 | private function _createAltInternalOwnCloudNameForGroups($name) { |
| 748 | 748 | $usedNames = $this->groupMapper->getNamesBySearch($name, "", '_%'); |
| 749 | - if(!($usedNames) || count($usedNames) === 0) { |
|
| 749 | + if (!($usedNames) || count($usedNames) === 0) { |
|
| 750 | 750 | $lastNo = 1; //will become name_2 |
| 751 | 751 | } else { |
| 752 | 752 | natsort($usedNames); |
| 753 | 753 | $lastName = array_pop($usedNames); |
| 754 | 754 | $lastNo = intval(substr($lastName, strrpos($lastName, '_') + 1)); |
| 755 | 755 | } |
| 756 | - $altName = $name.'_'.strval($lastNo+1); |
|
| 756 | + $altName = $name.'_'.strval($lastNo + 1); |
|
| 757 | 757 | unset($usedNames); |
| 758 | 758 | |
| 759 | 759 | $attempts = 1; |
| 760 | - while($attempts < 21){ |
|
| 760 | + while ($attempts < 21) { |
|
| 761 | 761 | // Check to be really sure it is unique |
| 762 | 762 | // while loop is just a precaution. If a name is not generated within |
| 763 | 763 | // 20 attempts, something else is very wrong. Avoids infinite loop. |
| 764 | - if(!\OC::$server->getGroupManager()->groupExists($altName)) { |
|
| 764 | + if (!\OC::$server->getGroupManager()->groupExists($altName)) { |
|
| 765 | 765 | return $altName; |
| 766 | 766 | } |
| 767 | - $altName = $name . '_' . ($lastNo + $attempts); |
|
| 767 | + $altName = $name.'_'.($lastNo + $attempts); |
|
| 768 | 768 | $attempts++; |
| 769 | 769 | } |
| 770 | 770 | return false; |
@@ -779,7 +779,7 @@ discard block |
||
| 779 | 779 | private function createAltInternalOwnCloudName($name, $isUser) { |
| 780 | 780 | $originalTTL = $this->connection->ldapCacheTTL; |
| 781 | 781 | $this->connection->setConfiguration(array('ldapCacheTTL' => 0)); |
| 782 | - if($isUser) { |
|
| 782 | + if ($isUser) { |
|
| 783 | 783 | $altName = $this->_createAltInternalOwnCloudNameForUsers($name); |
| 784 | 784 | } else { |
| 785 | 785 | $altName = $this->_createAltInternalOwnCloudNameForGroups($name); |
@@ -829,13 +829,13 @@ discard block |
||
| 829 | 829 | public function fetchListOfUsers($filter, $attr, $limit = null, $offset = null, $forceApplyAttributes = false) { |
| 830 | 830 | $ldapRecords = $this->searchUsers($filter, $attr, $limit, $offset); |
| 831 | 831 | $recordsToUpdate = $ldapRecords; |
| 832 | - if(!$forceApplyAttributes) { |
|
| 832 | + if (!$forceApplyAttributes) { |
|
| 833 | 833 | $isBackgroundJobModeAjax = $this->config |
| 834 | 834 | ->getAppValue('core', 'backgroundjobs_mode', 'ajax') === 'ajax'; |
| 835 | 835 | $recordsToUpdate = array_filter($ldapRecords, function($record) use ($isBackgroundJobModeAjax) { |
| 836 | 836 | $newlyMapped = false; |
| 837 | 837 | $uid = $this->dn2ocname($record['dn'][0], null, true, $newlyMapped, $record); |
| 838 | - if(is_string($uid)) { |
|
| 838 | + if (is_string($uid)) { |
|
| 839 | 839 | $this->cacheUserExists($uid); |
| 840 | 840 | } |
| 841 | 841 | return ($uid !== false) && ($newlyMapped || $isBackgroundJobModeAjax); |
@@ -851,19 +851,19 @@ discard block |
||
| 851 | 851 | * and their values |
| 852 | 852 | * @param array $ldapRecords |
| 853 | 853 | */ |
| 854 | - public function batchApplyUserAttributes(array $ldapRecords){ |
|
| 854 | + public function batchApplyUserAttributes(array $ldapRecords) { |
|
| 855 | 855 | $displayNameAttribute = strtolower($this->connection->ldapUserDisplayName); |
| 856 | - foreach($ldapRecords as $userRecord) { |
|
| 857 | - if(!isset($userRecord[$displayNameAttribute])) { |
|
| 856 | + foreach ($ldapRecords as $userRecord) { |
|
| 857 | + if (!isset($userRecord[$displayNameAttribute])) { |
|
| 858 | 858 | // displayName is obligatory |
| 859 | 859 | continue; |
| 860 | 860 | } |
| 861 | - $ocName = $this->dn2ocname($userRecord['dn'][0], null, true); |
|
| 862 | - if($ocName === false) { |
|
| 861 | + $ocName = $this->dn2ocname($userRecord['dn'][0], null, true); |
|
| 862 | + if ($ocName === false) { |
|
| 863 | 863 | continue; |
| 864 | 864 | } |
| 865 | 865 | $user = $this->userManager->get($ocName); |
| 866 | - if($user instanceof OfflineUser) { |
|
| 866 | + if ($user instanceof OfflineUser) { |
|
| 867 | 867 | $user->unmark(); |
| 868 | 868 | $user = $this->userManager->get($ocName); |
| 869 | 869 | } |
@@ -895,8 +895,8 @@ discard block |
||
| 895 | 895 | * @return array |
| 896 | 896 | */ |
| 897 | 897 | private function fetchList($list, $manyAttributes) { |
| 898 | - if(is_array($list)) { |
|
| 899 | - if($manyAttributes) { |
|
| 898 | + if (is_array($list)) { |
|
| 899 | + if ($manyAttributes) { |
|
| 900 | 900 | return $list; |
| 901 | 901 | } else { |
| 902 | 902 | $list = array_reduce($list, function($carry, $item) { |
@@ -994,7 +994,7 @@ discard block |
||
| 994 | 994 | // php no longer supports call-time pass-by-reference |
| 995 | 995 | // thus cannot support controlPagedResultResponse as the third argument |
| 996 | 996 | // is a reference |
| 997 | - $doMethod = function () use ($command, &$arguments) { |
|
| 997 | + $doMethod = function() use ($command, &$arguments) { |
|
| 998 | 998 | if ($command == 'controlPagedResultResponse') { |
| 999 | 999 | throw new \InvalidArgumentException('Invoker does not support controlPagedResultResponse, call LDAP Wrapper directly instead.'); |
| 1000 | 1000 | } else { |
@@ -1012,7 +1012,7 @@ discard block |
||
| 1012 | 1012 | $this->connection->resetConnectionResource(); |
| 1013 | 1013 | $cr = $this->connection->getConnectionResource(); |
| 1014 | 1014 | |
| 1015 | - if(!$this->ldap->isResource($cr)) { |
|
| 1015 | + if (!$this->ldap->isResource($cr)) { |
|
| 1016 | 1016 | // Seems like we didn't find any resource. |
| 1017 | 1017 | \OCP\Util::writeLog('user_ldap', "Could not $command, because resource is missing.", \OCP\Util::DEBUG); |
| 1018 | 1018 | throw $e; |
@@ -1037,13 +1037,13 @@ discard block |
||
| 1037 | 1037 | * @throws ServerNotAvailableException |
| 1038 | 1038 | */ |
| 1039 | 1039 | private function executeSearch($filter, $base, &$attr = null, $limit = null, $offset = null) { |
| 1040 | - if(!is_null($attr) && !is_array($attr)) { |
|
| 1040 | + if (!is_null($attr) && !is_array($attr)) { |
|
| 1041 | 1041 | $attr = array(mb_strtolower($attr, 'UTF-8')); |
| 1042 | 1042 | } |
| 1043 | 1043 | |
| 1044 | 1044 | // See if we have a resource, in case not cancel with message |
| 1045 | 1045 | $cr = $this->connection->getConnectionResource(); |
| 1046 | - if(!$this->ldap->isResource($cr)) { |
|
| 1046 | + if (!$this->ldap->isResource($cr)) { |
|
| 1047 | 1047 | // Seems like we didn't find any resource. |
| 1048 | 1048 | // Return an empty array just like before. |
| 1049 | 1049 | \OCP\Util::writeLog('user_ldap', 'Could not search, because resource is missing.', \OCP\Util::DEBUG); |
@@ -1057,7 +1057,7 @@ discard block |
||
| 1057 | 1057 | $sr = $this->invokeLDAPMethod('search', $linkResources, $base, $filter, $attr); |
| 1058 | 1058 | // cannot use $cr anymore, might have changed in the previous call! |
| 1059 | 1059 | $error = $this->ldap->errno($this->connection->getConnectionResource()); |
| 1060 | - if(!is_array($sr) || $error !== 0) { |
|
| 1060 | + if (!is_array($sr) || $error !== 0) { |
|
| 1061 | 1061 | \OCP\Util::writeLog('user_ldap', 'Attempt for Paging? '.print_r($pagedSearchOK, true), \OCP\Util::ERROR); |
| 1062 | 1062 | return false; |
| 1063 | 1063 | } |
@@ -1080,29 +1080,29 @@ discard block |
||
| 1080 | 1080 | */ |
| 1081 | 1081 | private function processPagedSearchStatus($sr, $filter, $base, $iFoundItems, $limit, $offset, $pagedSearchOK, $skipHandling) { |
| 1082 | 1082 | $cookie = null; |
| 1083 | - if($pagedSearchOK) { |
|
| 1083 | + if ($pagedSearchOK) { |
|
| 1084 | 1084 | $cr = $this->connection->getConnectionResource(); |
| 1085 | - foreach($sr as $key => $res) { |
|
| 1086 | - if($this->ldap->controlPagedResultResponse($cr, $res, $cookie)) { |
|
| 1085 | + foreach ($sr as $key => $res) { |
|
| 1086 | + if ($this->ldap->controlPagedResultResponse($cr, $res, $cookie)) { |
|
| 1087 | 1087 | $this->setPagedResultCookie($base[$key], $filter, $limit, $offset, $cookie); |
| 1088 | 1088 | } |
| 1089 | 1089 | } |
| 1090 | 1090 | |
| 1091 | 1091 | //browsing through prior pages to get the cookie for the new one |
| 1092 | - if($skipHandling) { |
|
| 1092 | + if ($skipHandling) { |
|
| 1093 | 1093 | return false; |
| 1094 | 1094 | } |
| 1095 | 1095 | // if count is bigger, then the server does not support |
| 1096 | 1096 | // paged search. Instead, he did a normal search. We set a |
| 1097 | 1097 | // flag here, so the callee knows how to deal with it. |
| 1098 | - if($iFoundItems <= $limit) { |
|
| 1098 | + if ($iFoundItems <= $limit) { |
|
| 1099 | 1099 | $this->pagedSearchedSuccessful = true; |
| 1100 | 1100 | } |
| 1101 | 1101 | } else { |
| 1102 | - if(!is_null($limit) && intval($this->connection->ldapPagingSize) !== 0) { |
|
| 1102 | + if (!is_null($limit) && intval($this->connection->ldapPagingSize) !== 0) { |
|
| 1103 | 1103 | \OC::$server->getLogger()->debug( |
| 1104 | 1104 | 'Paged search was not available', |
| 1105 | - [ 'app' => 'user_ldap' ] |
|
| 1105 | + ['app' => 'user_ldap'] |
|
| 1106 | 1106 | ); |
| 1107 | 1107 | } |
| 1108 | 1108 | } |
@@ -1132,7 +1132,7 @@ discard block |
||
| 1132 | 1132 | \OCP\Util::writeLog('user_ldap', 'Count filter: '.print_r($filter, true), \OCP\Util::DEBUG); |
| 1133 | 1133 | |
| 1134 | 1134 | $limitPerPage = intval($this->connection->ldapPagingSize); |
| 1135 | - if(!is_null($limit) && $limit < $limitPerPage && $limit > 0) { |
|
| 1135 | + if (!is_null($limit) && $limit < $limitPerPage && $limit > 0) { |
|
| 1136 | 1136 | $limitPerPage = $limit; |
| 1137 | 1137 | } |
| 1138 | 1138 | |
@@ -1142,7 +1142,7 @@ discard block |
||
| 1142 | 1142 | |
| 1143 | 1143 | do { |
| 1144 | 1144 | $search = $this->executeSearch($filter, $base, $attr, $limitPerPage, $offset); |
| 1145 | - if($search === false) { |
|
| 1145 | + if ($search === false) { |
|
| 1146 | 1146 | return $counter > 0 ? $counter : false; |
| 1147 | 1147 | } |
| 1148 | 1148 | list($sr, $pagedSearchOK) = $search; |
@@ -1161,7 +1161,7 @@ discard block |
||
| 1161 | 1161 | * Continue now depends on $hasMorePages value |
| 1162 | 1162 | */ |
| 1163 | 1163 | $continue = $pagedSearchOK && $hasMorePages; |
| 1164 | - } while($continue && (is_null($limit) || $limit <= 0 || $limit > $counter)); |
|
| 1164 | + } while ($continue && (is_null($limit) || $limit <= 0 || $limit > $counter)); |
|
| 1165 | 1165 | |
| 1166 | 1166 | return $counter; |
| 1167 | 1167 | } |
@@ -1173,7 +1173,7 @@ discard block |
||
| 1173 | 1173 | private function countEntriesInSearchResults($searchResults) { |
| 1174 | 1174 | $counter = 0; |
| 1175 | 1175 | |
| 1176 | - foreach($searchResults as $res) { |
|
| 1176 | + foreach ($searchResults as $res) { |
|
| 1177 | 1177 | $count = intval($this->invokeLDAPMethod('countEntries', $this->connection->getConnectionResource(), $res)); |
| 1178 | 1178 | $counter += $count; |
| 1179 | 1179 | } |
@@ -1195,7 +1195,7 @@ discard block |
||
| 1195 | 1195 | */ |
| 1196 | 1196 | public function search($filter, $base, $attr = null, $limit = null, $offset = null, $skipHandling = false) { |
| 1197 | 1197 | $limitPerPage = intval($this->connection->ldapPagingSize); |
| 1198 | - if(!is_null($limit) && $limit < $limitPerPage && $limit > 0) { |
|
| 1198 | + if (!is_null($limit) && $limit < $limitPerPage && $limit > 0) { |
|
| 1199 | 1199 | $limitPerPage = $limit; |
| 1200 | 1200 | } |
| 1201 | 1201 | |
@@ -1209,13 +1209,13 @@ discard block |
||
| 1209 | 1209 | $savedoffset = $offset; |
| 1210 | 1210 | do { |
| 1211 | 1211 | $search = $this->executeSearch($filter, $base, $attr, $limitPerPage, $offset); |
| 1212 | - if($search === false) { |
|
| 1212 | + if ($search === false) { |
|
| 1213 | 1213 | return []; |
| 1214 | 1214 | } |
| 1215 | 1215 | list($sr, $pagedSearchOK) = $search; |
| 1216 | 1216 | $cr = $this->connection->getConnectionResource(); |
| 1217 | 1217 | |
| 1218 | - if($skipHandling) { |
|
| 1218 | + if ($skipHandling) { |
|
| 1219 | 1219 | //i.e. result do not need to be fetched, we just need the cookie |
| 1220 | 1220 | //thus pass 1 or any other value as $iFoundItems because it is not |
| 1221 | 1221 | //used |
@@ -1226,7 +1226,7 @@ discard block |
||
| 1226 | 1226 | } |
| 1227 | 1227 | |
| 1228 | 1228 | $iFoundItems = 0; |
| 1229 | - foreach($sr as $res) { |
|
| 1229 | + foreach ($sr as $res) { |
|
| 1230 | 1230 | $findings = array_merge($findings, $this->invokeLDAPMethod('getEntries', $cr, $res)); |
| 1231 | 1231 | $iFoundItems = max($iFoundItems, $findings['count']); |
| 1232 | 1232 | unset($findings['count']); |
@@ -1242,27 +1242,27 @@ discard block |
||
| 1242 | 1242 | |
| 1243 | 1243 | // if we're here, probably no connection resource is returned. |
| 1244 | 1244 | // to make Nextcloud behave nicely, we simply give back an empty array. |
| 1245 | - if(is_null($findings)) { |
|
| 1245 | + if (is_null($findings)) { |
|
| 1246 | 1246 | return array(); |
| 1247 | 1247 | } |
| 1248 | 1248 | |
| 1249 | - if(!is_null($attr)) { |
|
| 1249 | + if (!is_null($attr)) { |
|
| 1250 | 1250 | $selection = []; |
| 1251 | 1251 | $i = 0; |
| 1252 | - foreach($findings as $item) { |
|
| 1253 | - if(!is_array($item)) { |
|
| 1252 | + foreach ($findings as $item) { |
|
| 1253 | + if (!is_array($item)) { |
|
| 1254 | 1254 | continue; |
| 1255 | 1255 | } |
| 1256 | 1256 | $item = \OCP\Util::mb_array_change_key_case($item, MB_CASE_LOWER, 'UTF-8'); |
| 1257 | - foreach($attr as $key) { |
|
| 1258 | - if(isset($item[$key])) { |
|
| 1259 | - if(is_array($item[$key]) && isset($item[$key]['count'])) { |
|
| 1257 | + foreach ($attr as $key) { |
|
| 1258 | + if (isset($item[$key])) { |
|
| 1259 | + if (is_array($item[$key]) && isset($item[$key]['count'])) { |
|
| 1260 | 1260 | unset($item[$key]['count']); |
| 1261 | 1261 | } |
| 1262 | - if($key !== 'dn') { |
|
| 1263 | - if($this->resemblesDN($key)) { |
|
| 1262 | + if ($key !== 'dn') { |
|
| 1263 | + if ($this->resemblesDN($key)) { |
|
| 1264 | 1264 | $selection[$i][$key] = $this->helper->sanitizeDN($item[$key]); |
| 1265 | - } else if($key === 'objectguid' || $key === 'guid') { |
|
| 1265 | + } else if ($key === 'objectguid' || $key === 'guid') { |
|
| 1266 | 1266 | $selection[$i][$key] = [$this->convertObjectGUID2Str($item[$key][0])]; |
| 1267 | 1267 | } else { |
| 1268 | 1268 | $selection[$i][$key] = $item[$key]; |
@@ -1280,7 +1280,7 @@ discard block |
||
| 1280 | 1280 | //we slice the findings, when |
| 1281 | 1281 | //a) paged search unsuccessful, though attempted |
| 1282 | 1282 | //b) no paged search, but limit set |
| 1283 | - if((!$this->getPagedSearchResultState() |
|
| 1283 | + if ((!$this->getPagedSearchResultState() |
|
| 1284 | 1284 | && $pagedSearchOK) |
| 1285 | 1285 | || ( |
| 1286 | 1286 | !$pagedSearchOK |
@@ -1297,7 +1297,7 @@ discard block |
||
| 1297 | 1297 | * @return bool|mixed|string |
| 1298 | 1298 | */ |
| 1299 | 1299 | public function sanitizeUsername($name) { |
| 1300 | - if($this->connection->ldapIgnoreNamingRules) { |
|
| 1300 | + if ($this->connection->ldapIgnoreNamingRules) { |
|
| 1301 | 1301 | return trim($name); |
| 1302 | 1302 | } |
| 1303 | 1303 | |
@@ -1322,13 +1322,13 @@ discard block |
||
| 1322 | 1322 | */ |
| 1323 | 1323 | public function escapeFilterPart($input, $allowAsterisk = false) { |
| 1324 | 1324 | $asterisk = ''; |
| 1325 | - if($allowAsterisk && strlen($input) > 0 && $input[0] === '*') { |
|
| 1325 | + if ($allowAsterisk && strlen($input) > 0 && $input[0] === '*') { |
|
| 1326 | 1326 | $asterisk = '*'; |
| 1327 | 1327 | $input = mb_substr($input, 1, null, 'UTF-8'); |
| 1328 | 1328 | } |
| 1329 | 1329 | $search = array('*', '\\', '(', ')'); |
| 1330 | 1330 | $replace = array('\\*', '\\\\', '\\(', '\\)'); |
| 1331 | - return $asterisk . str_replace($search, $replace, $input); |
|
| 1331 | + return $asterisk.str_replace($search, $replace, $input); |
|
| 1332 | 1332 | } |
| 1333 | 1333 | |
| 1334 | 1334 | /** |
@@ -1358,13 +1358,13 @@ discard block |
||
| 1358 | 1358 | */ |
| 1359 | 1359 | private function combineFilter($filters, $operator) { |
| 1360 | 1360 | $combinedFilter = '('.$operator; |
| 1361 | - foreach($filters as $filter) { |
|
| 1361 | + foreach ($filters as $filter) { |
|
| 1362 | 1362 | if ($filter !== '' && $filter[0] !== '(') { |
| 1363 | 1363 | $filter = '('.$filter.')'; |
| 1364 | 1364 | } |
| 1365 | - $combinedFilter.=$filter; |
|
| 1365 | + $combinedFilter .= $filter; |
|
| 1366 | 1366 | } |
| 1367 | - $combinedFilter.=')'; |
|
| 1367 | + $combinedFilter .= ')'; |
|
| 1368 | 1368 | return $combinedFilter; |
| 1369 | 1369 | } |
| 1370 | 1370 | |
@@ -1400,17 +1400,17 @@ discard block |
||
| 1400 | 1400 | * @throws \Exception |
| 1401 | 1401 | */ |
| 1402 | 1402 | private function getAdvancedFilterPartForSearch($search, $searchAttributes) { |
| 1403 | - if(!is_array($searchAttributes) || count($searchAttributes) < 2) { |
|
| 1403 | + if (!is_array($searchAttributes) || count($searchAttributes) < 2) { |
|
| 1404 | 1404 | throw new \Exception('searchAttributes must be an array with at least two string'); |
| 1405 | 1405 | } |
| 1406 | 1406 | $searchWords = explode(' ', trim($search)); |
| 1407 | 1407 | $wordFilters = array(); |
| 1408 | - foreach($searchWords as $word) { |
|
| 1408 | + foreach ($searchWords as $word) { |
|
| 1409 | 1409 | $word = $this->prepareSearchTerm($word); |
| 1410 | 1410 | //every word needs to appear at least once |
| 1411 | 1411 | $wordMatchOneAttrFilters = array(); |
| 1412 | - foreach($searchAttributes as $attr) { |
|
| 1413 | - $wordMatchOneAttrFilters[] = $attr . '=' . $word; |
|
| 1412 | + foreach ($searchAttributes as $attr) { |
|
| 1413 | + $wordMatchOneAttrFilters[] = $attr.'='.$word; |
|
| 1414 | 1414 | } |
| 1415 | 1415 | $wordFilters[] = $this->combineFilterWithOr($wordMatchOneAttrFilters); |
| 1416 | 1416 | } |
@@ -1428,10 +1428,10 @@ discard block |
||
| 1428 | 1428 | private function getFilterPartForSearch($search, $searchAttributes, $fallbackAttribute) { |
| 1429 | 1429 | $filter = array(); |
| 1430 | 1430 | $haveMultiSearchAttributes = (is_array($searchAttributes) && count($searchAttributes) > 0); |
| 1431 | - if($haveMultiSearchAttributes && strpos(trim($search), ' ') !== false) { |
|
| 1431 | + if ($haveMultiSearchAttributes && strpos(trim($search), ' ') !== false) { |
|
| 1432 | 1432 | try { |
| 1433 | 1433 | return $this->getAdvancedFilterPartForSearch($search, $searchAttributes); |
| 1434 | - } catch(\Exception $e) { |
|
| 1434 | + } catch (\Exception $e) { |
|
| 1435 | 1435 | \OCP\Util::writeLog( |
| 1436 | 1436 | 'user_ldap', |
| 1437 | 1437 | 'Creating advanced filter for search failed, falling back to simple method.', |
@@ -1441,17 +1441,17 @@ discard block |
||
| 1441 | 1441 | } |
| 1442 | 1442 | |
| 1443 | 1443 | $search = $this->prepareSearchTerm($search); |
| 1444 | - if(!is_array($searchAttributes) || count($searchAttributes) === 0) { |
|
| 1444 | + if (!is_array($searchAttributes) || count($searchAttributes) === 0) { |
|
| 1445 | 1445 | if ($fallbackAttribute === '') { |
| 1446 | 1446 | return ''; |
| 1447 | 1447 | } |
| 1448 | - $filter[] = $fallbackAttribute . '=' . $search; |
|
| 1448 | + $filter[] = $fallbackAttribute.'='.$search; |
|
| 1449 | 1449 | } else { |
| 1450 | - foreach($searchAttributes as $attribute) { |
|
| 1451 | - $filter[] = $attribute . '=' . $search; |
|
| 1450 | + foreach ($searchAttributes as $attribute) { |
|
| 1451 | + $filter[] = $attribute.'='.$search; |
|
| 1452 | 1452 | } |
| 1453 | 1453 | } |
| 1454 | - if(count($filter) === 1) { |
|
| 1454 | + if (count($filter) === 1) { |
|
| 1455 | 1455 | return '('.$filter[0].')'; |
| 1456 | 1456 | } |
| 1457 | 1457 | return $this->combineFilterWithOr($filter); |
@@ -1472,7 +1472,7 @@ discard block |
||
| 1472 | 1472 | if ($term === '') { |
| 1473 | 1473 | $result = '*'; |
| 1474 | 1474 | } else if ($allowEnum !== 'no') { |
| 1475 | - $result = $term . '*'; |
|
| 1475 | + $result = $term.'*'; |
|
| 1476 | 1476 | } |
| 1477 | 1477 | return $result; |
| 1478 | 1478 | } |
@@ -1484,7 +1484,7 @@ discard block |
||
| 1484 | 1484 | public function getFilterForUserCount() { |
| 1485 | 1485 | $filter = $this->combineFilterWithAnd(array( |
| 1486 | 1486 | $this->connection->ldapUserFilter, |
| 1487 | - $this->connection->ldapUserDisplayName . '=*' |
|
| 1487 | + $this->connection->ldapUserDisplayName.'=*' |
|
| 1488 | 1488 | )); |
| 1489 | 1489 | |
| 1490 | 1490 | return $filter; |
@@ -1502,7 +1502,7 @@ discard block |
||
| 1502 | 1502 | 'ldapAgentName' => $name, |
| 1503 | 1503 | 'ldapAgentPassword' => $password |
| 1504 | 1504 | ); |
| 1505 | - if(!$testConnection->setConfiguration($credentials)) { |
|
| 1505 | + if (!$testConnection->setConfiguration($credentials)) { |
|
| 1506 | 1506 | return false; |
| 1507 | 1507 | } |
| 1508 | 1508 | return $testConnection->bind(); |
@@ -1524,30 +1524,30 @@ discard block |
||
| 1524 | 1524 | // Sacrebleu! The UUID attribute is unknown :( We need first an |
| 1525 | 1525 | // existing DN to be able to reliably detect it. |
| 1526 | 1526 | $result = $this->search($filter, $base, ['dn'], 1); |
| 1527 | - if(!isset($result[0]) || !isset($result[0]['dn'])) { |
|
| 1527 | + if (!isset($result[0]) || !isset($result[0]['dn'])) { |
|
| 1528 | 1528 | throw new \Exception('Cannot determine UUID attribute'); |
| 1529 | 1529 | } |
| 1530 | 1530 | $dn = $result[0]['dn'][0]; |
| 1531 | - if(!$this->detectUuidAttribute($dn, true)) { |
|
| 1531 | + if (!$this->detectUuidAttribute($dn, true)) { |
|
| 1532 | 1532 | throw new \Exception('Cannot determine UUID attribute'); |
| 1533 | 1533 | } |
| 1534 | 1534 | } else { |
| 1535 | 1535 | // The UUID attribute is either known or an override is given. |
| 1536 | 1536 | // By calling this method we ensure that $this->connection->$uuidAttr |
| 1537 | 1537 | // is definitely set |
| 1538 | - if(!$this->detectUuidAttribute('', true)) { |
|
| 1538 | + if (!$this->detectUuidAttribute('', true)) { |
|
| 1539 | 1539 | throw new \Exception('Cannot determine UUID attribute'); |
| 1540 | 1540 | } |
| 1541 | 1541 | } |
| 1542 | 1542 | |
| 1543 | 1543 | $uuidAttr = $this->connection->ldapUuidUserAttribute; |
| 1544 | - if($uuidAttr === 'guid' || $uuidAttr === 'objectguid') { |
|
| 1544 | + if ($uuidAttr === 'guid' || $uuidAttr === 'objectguid') { |
|
| 1545 | 1545 | $uuid = $this->formatGuid2ForFilterUser($uuid); |
| 1546 | 1546 | } |
| 1547 | 1547 | |
| 1548 | - $filter = $uuidAttr . '=' . $uuid; |
|
| 1548 | + $filter = $uuidAttr.'='.$uuid; |
|
| 1549 | 1549 | $result = $this->searchUsers($filter, ['dn'], 2); |
| 1550 | - if(is_array($result) && isset($result[0]) && isset($result[0]['dn']) && count($result) === 1) { |
|
| 1550 | + if (is_array($result) && isset($result[0]) && isset($result[0]['dn']) && count($result) === 1) { |
|
| 1551 | 1551 | // we put the count into account to make sure that this is |
| 1552 | 1552 | // really unique |
| 1553 | 1553 | return $result[0]['dn'][0]; |
@@ -1566,7 +1566,7 @@ discard block |
||
| 1566 | 1566 | * @return bool true on success, false otherwise |
| 1567 | 1567 | */ |
| 1568 | 1568 | private function detectUuidAttribute($dn, $isUser = true, $force = false, array $ldapRecord = null) { |
| 1569 | - if($isUser) { |
|
| 1569 | + if ($isUser) { |
|
| 1570 | 1570 | $uuidAttr = 'ldapUuidUserAttribute'; |
| 1571 | 1571 | $uuidOverride = $this->connection->ldapExpertUUIDUserAttr; |
| 1572 | 1572 | } else { |
@@ -1574,7 +1574,7 @@ discard block |
||
| 1574 | 1574 | $uuidOverride = $this->connection->ldapExpertUUIDGroupAttr; |
| 1575 | 1575 | } |
| 1576 | 1576 | |
| 1577 | - if(($this->connection->$uuidAttr !== 'auto') && !$force) { |
|
| 1577 | + if (($this->connection->$uuidAttr !== 'auto') && !$force) { |
|
| 1578 | 1578 | return true; |
| 1579 | 1579 | } |
| 1580 | 1580 | |
@@ -1583,10 +1583,10 @@ discard block |
||
| 1583 | 1583 | return true; |
| 1584 | 1584 | } |
| 1585 | 1585 | |
| 1586 | - foreach(self::UUID_ATTRIBUTES as $attribute) { |
|
| 1587 | - if($ldapRecord !== null) { |
|
| 1586 | + foreach (self::UUID_ATTRIBUTES as $attribute) { |
|
| 1587 | + if ($ldapRecord !== null) { |
|
| 1588 | 1588 | // we have the info from LDAP already, we don't need to talk to the server again |
| 1589 | - if(isset($ldapRecord[$attribute])) { |
|
| 1589 | + if (isset($ldapRecord[$attribute])) { |
|
| 1590 | 1590 | $this->connection->$uuidAttr = $attribute; |
| 1591 | 1591 | return true; |
| 1592 | 1592 | } else { |
@@ -1595,7 +1595,7 @@ discard block |
||
| 1595 | 1595 | } |
| 1596 | 1596 | |
| 1597 | 1597 | $value = $this->readAttribute($dn, $attribute); |
| 1598 | - if(is_array($value) && isset($value[0]) && !empty($value[0])) { |
|
| 1598 | + if (is_array($value) && isset($value[0]) && !empty($value[0])) { |
|
| 1599 | 1599 | \OCP\Util::writeLog('user_ldap', |
| 1600 | 1600 | 'Setting '.$attribute.' as '.$uuidAttr, |
| 1601 | 1601 | \OCP\Util::DEBUG); |
@@ -1617,7 +1617,7 @@ discard block |
||
| 1617 | 1617 | * @return bool|string |
| 1618 | 1618 | */ |
| 1619 | 1619 | public function getUUID($dn, $isUser = true, $ldapRecord = null) { |
| 1620 | - if($isUser) { |
|
| 1620 | + if ($isUser) { |
|
| 1621 | 1621 | $uuidAttr = 'ldapUuidUserAttribute'; |
| 1622 | 1622 | $uuidOverride = $this->connection->ldapExpertUUIDUserAttr; |
| 1623 | 1623 | } else { |
@@ -1626,10 +1626,10 @@ discard block |
||
| 1626 | 1626 | } |
| 1627 | 1627 | |
| 1628 | 1628 | $uuid = false; |
| 1629 | - if($this->detectUuidAttribute($dn, $isUser, false, $ldapRecord)) { |
|
| 1629 | + if ($this->detectUuidAttribute($dn, $isUser, false, $ldapRecord)) { |
|
| 1630 | 1630 | $attr = $this->connection->$uuidAttr; |
| 1631 | 1631 | $uuid = isset($ldapRecord[$attr]) ? $ldapRecord[$attr] : $this->readAttribute($dn, $attr); |
| 1632 | - if( !is_array($uuid) |
|
| 1632 | + if (!is_array($uuid) |
|
| 1633 | 1633 | && $uuidOverride !== '' |
| 1634 | 1634 | && $this->detectUuidAttribute($dn, $isUser, true, $ldapRecord)) |
| 1635 | 1635 | { |
@@ -1637,7 +1637,7 @@ discard block |
||
| 1637 | 1637 | ? $ldapRecord[$this->connection->$uuidAttr] |
| 1638 | 1638 | : $this->readAttribute($dn, $this->connection->$uuidAttr); |
| 1639 | 1639 | } |
| 1640 | - if(is_array($uuid) && isset($uuid[0]) && !empty($uuid[0])) { |
|
| 1640 | + if (is_array($uuid) && isset($uuid[0]) && !empty($uuid[0])) { |
|
| 1641 | 1641 | $uuid = $uuid[0]; |
| 1642 | 1642 | } |
| 1643 | 1643 | } |
@@ -1654,19 +1654,19 @@ discard block |
||
| 1654 | 1654 | private function convertObjectGUID2Str($oguid) { |
| 1655 | 1655 | $hex_guid = bin2hex($oguid); |
| 1656 | 1656 | $hex_guid_to_guid_str = ''; |
| 1657 | - for($k = 1; $k <= 4; ++$k) { |
|
| 1657 | + for ($k = 1; $k <= 4; ++$k) { |
|
| 1658 | 1658 | $hex_guid_to_guid_str .= substr($hex_guid, 8 - 2 * $k, 2); |
| 1659 | 1659 | } |
| 1660 | 1660 | $hex_guid_to_guid_str .= '-'; |
| 1661 | - for($k = 1; $k <= 2; ++$k) { |
|
| 1661 | + for ($k = 1; $k <= 2; ++$k) { |
|
| 1662 | 1662 | $hex_guid_to_guid_str .= substr($hex_guid, 12 - 2 * $k, 2); |
| 1663 | 1663 | } |
| 1664 | 1664 | $hex_guid_to_guid_str .= '-'; |
| 1665 | - for($k = 1; $k <= 2; ++$k) { |
|
| 1665 | + for ($k = 1; $k <= 2; ++$k) { |
|
| 1666 | 1666 | $hex_guid_to_guid_str .= substr($hex_guid, 16 - 2 * $k, 2); |
| 1667 | 1667 | } |
| 1668 | - $hex_guid_to_guid_str .= '-' . substr($hex_guid, 16, 4); |
|
| 1669 | - $hex_guid_to_guid_str .= '-' . substr($hex_guid, 20); |
|
| 1668 | + $hex_guid_to_guid_str .= '-'.substr($hex_guid, 16, 4); |
|
| 1669 | + $hex_guid_to_guid_str .= '-'.substr($hex_guid, 20); |
|
| 1670 | 1670 | |
| 1671 | 1671 | return strtoupper($hex_guid_to_guid_str); |
| 1672 | 1672 | } |
@@ -1683,11 +1683,11 @@ discard block |
||
| 1683 | 1683 | * @return string |
| 1684 | 1684 | */ |
| 1685 | 1685 | public function formatGuid2ForFilterUser($guid) { |
| 1686 | - if(!is_string($guid)) { |
|
| 1686 | + if (!is_string($guid)) { |
|
| 1687 | 1687 | throw new \InvalidArgumentException('String expected'); |
| 1688 | 1688 | } |
| 1689 | 1689 | $blocks = explode('-', $guid); |
| 1690 | - if(count($blocks) !== 5) { |
|
| 1690 | + if (count($blocks) !== 5) { |
|
| 1691 | 1691 | /* |
| 1692 | 1692 | * Why not throw an Exception instead? This method is a utility |
| 1693 | 1693 | * called only when trying to figure out whether a "missing" known |
@@ -1700,20 +1700,20 @@ discard block |
||
| 1700 | 1700 | * user. Instead we write a log message. |
| 1701 | 1701 | */ |
| 1702 | 1702 | \OC::$server->getLogger()->info( |
| 1703 | - 'Passed string does not resemble a valid GUID. Known UUID ' . |
|
| 1703 | + 'Passed string does not resemble a valid GUID. Known UUID '. |
|
| 1704 | 1704 | '({uuid}) probably does not match UUID configuration.', |
| 1705 | - [ 'app' => 'user_ldap', 'uuid' => $guid ] |
|
| 1705 | + ['app' => 'user_ldap', 'uuid' => $guid] |
|
| 1706 | 1706 | ); |
| 1707 | 1707 | return $guid; |
| 1708 | 1708 | } |
| 1709 | - for($i=0; $i < 3; $i++) { |
|
| 1709 | + for ($i = 0; $i < 3; $i++) { |
|
| 1710 | 1710 | $pairs = str_split($blocks[$i], 2); |
| 1711 | 1711 | $pairs = array_reverse($pairs); |
| 1712 | 1712 | $blocks[$i] = implode('', $pairs); |
| 1713 | 1713 | } |
| 1714 | - for($i=0; $i < 5; $i++) { |
|
| 1714 | + for ($i = 0; $i < 5; $i++) { |
|
| 1715 | 1715 | $pairs = str_split($blocks[$i], 2); |
| 1716 | - $blocks[$i] = '\\' . implode('\\', $pairs); |
|
| 1716 | + $blocks[$i] = '\\'.implode('\\', $pairs); |
|
| 1717 | 1717 | } |
| 1718 | 1718 | return implode('', $blocks); |
| 1719 | 1719 | } |
@@ -1727,12 +1727,12 @@ discard block |
||
| 1727 | 1727 | $domainDN = $this->getDomainDNFromDN($dn); |
| 1728 | 1728 | $cacheKey = 'getSID-'.$domainDN; |
| 1729 | 1729 | $sid = $this->connection->getFromCache($cacheKey); |
| 1730 | - if(!is_null($sid)) { |
|
| 1730 | + if (!is_null($sid)) { |
|
| 1731 | 1731 | return $sid; |
| 1732 | 1732 | } |
| 1733 | 1733 | |
| 1734 | 1734 | $objectSid = $this->readAttribute($domainDN, 'objectsid'); |
| 1735 | - if(!is_array($objectSid) || empty($objectSid)) { |
|
| 1735 | + if (!is_array($objectSid) || empty($objectSid)) { |
|
| 1736 | 1736 | $this->connection->writeToCache($cacheKey, false); |
| 1737 | 1737 | return false; |
| 1738 | 1738 | } |
@@ -1790,12 +1790,12 @@ discard block |
||
| 1790 | 1790 | $belongsToBase = false; |
| 1791 | 1791 | $bases = $this->helper->sanitizeDN($bases); |
| 1792 | 1792 | |
| 1793 | - foreach($bases as $base) { |
|
| 1793 | + foreach ($bases as $base) { |
|
| 1794 | 1794 | $belongsToBase = true; |
| 1795 | - if(mb_strripos($dn, $base, 0, 'UTF-8') !== (mb_strlen($dn, 'UTF-8')-mb_strlen($base, 'UTF-8'))) { |
|
| 1795 | + if (mb_strripos($dn, $base, 0, 'UTF-8') !== (mb_strlen($dn, 'UTF-8') - mb_strlen($base, 'UTF-8'))) { |
|
| 1796 | 1796 | $belongsToBase = false; |
| 1797 | 1797 | } |
| 1798 | - if($belongsToBase) { |
|
| 1798 | + if ($belongsToBase) { |
|
| 1799 | 1799 | break; |
| 1800 | 1800 | } |
| 1801 | 1801 | } |
@@ -1806,7 +1806,7 @@ discard block |
||
| 1806 | 1806 | * resets a running Paged Search operation |
| 1807 | 1807 | */ |
| 1808 | 1808 | private function abandonPagedSearch() { |
| 1809 | - if($this->connection->hasPagedResultSupport) { |
|
| 1809 | + if ($this->connection->hasPagedResultSupport) { |
|
| 1810 | 1810 | $cr = $this->connection->getConnectionResource(); |
| 1811 | 1811 | $this->invokeLDAPMethod('controlPagedResult', $cr, 0, false, $this->lastCookie); |
| 1812 | 1812 | $this->getPagedSearchResultState(); |
@@ -1824,16 +1824,16 @@ discard block |
||
| 1824 | 1824 | * @return string containing the key or empty if none is cached |
| 1825 | 1825 | */ |
| 1826 | 1826 | private function getPagedResultCookie($base, $filter, $limit, $offset) { |
| 1827 | - if($offset === 0) { |
|
| 1827 | + if ($offset === 0) { |
|
| 1828 | 1828 | return ''; |
| 1829 | 1829 | } |
| 1830 | 1830 | $offset -= $limit; |
| 1831 | 1831 | //we work with cache here |
| 1832 | - $cacheKey = 'lc' . crc32($base) . '-' . crc32($filter) . '-' . intval($limit) . '-' . intval($offset); |
|
| 1832 | + $cacheKey = 'lc'.crc32($base).'-'.crc32($filter).'-'.intval($limit).'-'.intval($offset); |
|
| 1833 | 1833 | $cookie = ''; |
| 1834 | - if(isset($this->cookies[$cacheKey])) { |
|
| 1834 | + if (isset($this->cookies[$cacheKey])) { |
|
| 1835 | 1835 | $cookie = $this->cookies[$cacheKey]; |
| 1836 | - if(is_null($cookie)) { |
|
| 1836 | + if (is_null($cookie)) { |
|
| 1837 | 1837 | $cookie = ''; |
| 1838 | 1838 | } |
| 1839 | 1839 | } |
@@ -1851,11 +1851,11 @@ discard block |
||
| 1851 | 1851 | * @return bool |
| 1852 | 1852 | */ |
| 1853 | 1853 | public function hasMoreResults() { |
| 1854 | - if(!$this->connection->hasPagedResultSupport) { |
|
| 1854 | + if (!$this->connection->hasPagedResultSupport) { |
|
| 1855 | 1855 | return false; |
| 1856 | 1856 | } |
| 1857 | 1857 | |
| 1858 | - if(empty($this->lastCookie) && $this->lastCookie !== '0') { |
|
| 1858 | + if (empty($this->lastCookie) && $this->lastCookie !== '0') { |
|
| 1859 | 1859 | // as in RFC 2696, when all results are returned, the cookie will |
| 1860 | 1860 | // be empty. |
| 1861 | 1861 | return false; |
@@ -1875,8 +1875,8 @@ discard block |
||
| 1875 | 1875 | */ |
| 1876 | 1876 | private function setPagedResultCookie($base, $filter, $limit, $offset, $cookie) { |
| 1877 | 1877 | // allow '0' for 389ds |
| 1878 | - if(!empty($cookie) || $cookie === '0') { |
|
| 1879 | - $cacheKey = 'lc' . crc32($base) . '-' . crc32($filter) . '-' .intval($limit) . '-' . intval($offset); |
|
| 1878 | + if (!empty($cookie) || $cookie === '0') { |
|
| 1879 | + $cacheKey = 'lc'.crc32($base).'-'.crc32($filter).'-'.intval($limit).'-'.intval($offset); |
|
| 1880 | 1880 | $this->cookies[$cacheKey] = $cookie; |
| 1881 | 1881 | $this->lastCookie = $cookie; |
| 1882 | 1882 | } |
@@ -1903,17 +1903,17 @@ discard block |
||
| 1903 | 1903 | */ |
| 1904 | 1904 | private function initPagedSearch($filter, $bases, $attr, $limit, $offset) { |
| 1905 | 1905 | $pagedSearchOK = false; |
| 1906 | - if($this->connection->hasPagedResultSupport && ($limit !== 0)) { |
|
| 1906 | + if ($this->connection->hasPagedResultSupport && ($limit !== 0)) { |
|
| 1907 | 1907 | $offset = intval($offset); //can be null |
| 1908 | 1908 | \OCP\Util::writeLog('user_ldap', |
| 1909 | 1909 | 'initializing paged search for Filter '.$filter.' base '.print_r($bases, true) |
| 1910 | - .' attr '.print_r($attr, true). ' limit ' .$limit.' offset '.$offset, |
|
| 1910 | + .' attr '.print_r($attr, true).' limit '.$limit.' offset '.$offset, |
|
| 1911 | 1911 | \OCP\Util::DEBUG); |
| 1912 | 1912 | //get the cookie from the search for the previous search, required by LDAP |
| 1913 | - foreach($bases as $base) { |
|
| 1913 | + foreach ($bases as $base) { |
|
| 1914 | 1914 | |
| 1915 | 1915 | $cookie = $this->getPagedResultCookie($base, $filter, $limit, $offset); |
| 1916 | - if(empty($cookie) && $cookie !== "0" && ($offset > 0)) { |
|
| 1916 | + if (empty($cookie) && $cookie !== "0" && ($offset > 0)) { |
|
| 1917 | 1917 | // no cookie known from a potential previous search. We need |
| 1918 | 1918 | // to start from 0 to come to the desired page. cookie value |
| 1919 | 1919 | // of '0' is valid, because 389ds |
@@ -1923,17 +1923,17 @@ discard block |
||
| 1923 | 1923 | //still no cookie? obviously, the server does not like us. Let's skip paging efforts. |
| 1924 | 1924 | // '0' is valid, because 389ds |
| 1925 | 1925 | //TODO: remember this, probably does not change in the next request... |
| 1926 | - if(empty($cookie) && $cookie !== '0') { |
|
| 1926 | + if (empty($cookie) && $cookie !== '0') { |
|
| 1927 | 1927 | $cookie = null; |
| 1928 | 1928 | } |
| 1929 | 1929 | } |
| 1930 | - if(!is_null($cookie)) { |
|
| 1930 | + if (!is_null($cookie)) { |
|
| 1931 | 1931 | //since offset = 0, this is a new search. We abandon other searches that might be ongoing. |
| 1932 | 1932 | $this->abandonPagedSearch(); |
| 1933 | 1933 | $pagedSearchOK = $this->invokeLDAPMethod('controlPagedResult', |
| 1934 | 1934 | $this->connection->getConnectionResource(), $limit, |
| 1935 | 1935 | false, $cookie); |
| 1936 | - if(!$pagedSearchOK) { |
|
| 1936 | + if (!$pagedSearchOK) { |
|
| 1937 | 1937 | return false; |
| 1938 | 1938 | } |
| 1939 | 1939 | \OCP\Util::writeLog('user_ldap', 'Ready for a paged search', \OCP\Util::DEBUG); |
@@ -1949,7 +1949,7 @@ discard block |
||
| 1949 | 1949 | * So we added "&& !empty($this->lastCookie)" to this test to ignore pagination |
| 1950 | 1950 | * if we don't have a previous paged search. |
| 1951 | 1951 | */ |
| 1952 | - } else if($this->connection->hasPagedResultSupport && $limit === 0 && !empty($this->lastCookie)) { |
|
| 1952 | + } else if ($this->connection->hasPagedResultSupport && $limit === 0 && !empty($this->lastCookie)) { |
|
| 1953 | 1953 | // a search without limit was requested. However, if we do use |
| 1954 | 1954 | // Paged Search once, we always must do it. This requires us to |
| 1955 | 1955 | // initialize it with the configured page size. |