Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Access often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Access, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 45 | class Access extends LDAPUtility implements user\IUserTools { |
||
| 46 | /** |
||
| 47 | * @var \OCA\user_ldap\lib\Connection |
||
| 48 | */ |
||
| 49 | public $connection; |
||
| 50 | public $userManager; |
||
| 51 | //never ever check this var directly, always use getPagedSearchResultState |
||
| 52 | protected $pagedSearchedSuccessful; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var string[] $cookies an array of returned Paged Result cookies |
||
| 56 | */ |
||
| 57 | protected $cookies = array(); |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var string $lastCookie the last cookie returned from a Paged Results |
||
| 61 | * operation, defaults to an empty string |
||
| 62 | */ |
||
| 63 | protected $lastCookie = ''; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var AbstractMapping $userMapper |
||
| 67 | */ |
||
| 68 | protected $userMapper; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var AbstractMapping $userMapper |
||
| 72 | */ |
||
| 73 | protected $groupMapper; |
||
| 74 | |||
| 75 | 91 | public function __construct(Connection $connection, ILDAPWrapper $ldap, |
|
| 76 | user\Manager $userManager) { |
||
| 77 | 91 | parent::__construct($ldap); |
|
| 78 | 91 | $this->connection = $connection; |
|
| 79 | 91 | $this->userManager = $userManager; |
|
| 80 | 91 | $this->userManager->setLdapAccess($this); |
|
| 81 | 91 | } |
|
| 82 | |||
| 83 | /** |
||
| 84 | * sets the User Mapper |
||
| 85 | * @param AbstractMapping $mapper |
||
| 86 | */ |
||
| 87 | 1 | public function setUserMapper(AbstractMapping $mapper) { |
|
| 90 | |||
| 91 | /** |
||
| 92 | * returns the User Mapper |
||
| 93 | * @throws \Exception |
||
| 94 | * @return AbstractMapping |
||
| 95 | */ |
||
| 96 | 1 | public function getUserMapper() { |
|
| 97 | 1 | if(is_null($this->userMapper)) { |
|
| 98 | throw new \Exception('UserMapper was not assigned to this Access instance.'); |
||
| 99 | } |
||
| 100 | 1 | return $this->userMapper; |
|
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * sets the Group Mapper |
||
| 105 | * @param AbstractMapping $mapper |
||
| 106 | */ |
||
| 107 | public function setGroupMapper(AbstractMapping $mapper) { |
||
| 110 | |||
| 111 | /** |
||
| 112 | * returns the Group Mapper |
||
| 113 | * @throws \Exception |
||
| 114 | * @return AbstractMapping |
||
| 115 | */ |
||
| 116 | public function getGroupMapper() { |
||
| 117 | if(is_null($this->groupMapper)) { |
||
| 118 | throw new \Exception('GroupMapper was not assigned to this Access instance.'); |
||
| 119 | } |
||
| 120 | return $this->groupMapper; |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @return bool |
||
| 125 | */ |
||
| 126 | 4 | private function checkConnection() { |
|
| 129 | |||
| 130 | /** |
||
| 131 | * returns the Connection instance |
||
| 132 | * @return \OCA\user_ldap\lib\Connection |
||
| 133 | */ |
||
| 134 | 46 | public function getConnection() { |
|
| 137 | |||
| 138 | /** |
||
| 139 | * reads a given attribute for an LDAP record identified by a DN |
||
| 140 | * @param string $dn the record in question |
||
| 141 | * @param string $attr the attribute that shall be retrieved |
||
| 142 | * if empty, just check the record's existence |
||
| 143 | * @param string $filter |
||
| 144 | * @return array|false an array of values on success or an empty |
||
| 145 | * array if $attr is empty, false otherwise |
||
| 146 | */ |
||
| 147 | 4 | public function readAttribute($dn, $attr, $filter = 'objectClass=*') { |
|
| 148 | 4 | if(!$this->checkConnection()) { |
|
| 149 | \OCP\Util::writeLog('user_ldap', |
||
| 150 | 'No LDAP Connector assigned, access impossible for readAttribute.', |
||
| 151 | \OCP\Util::WARN); |
||
| 152 | return false; |
||
| 153 | } |
||
| 154 | 4 | $cr = $this->connection->getConnectionResource(); |
|
| 155 | 4 | if(!$this->ldap->isResource($cr)) { |
|
| 156 | //LDAP not available |
||
| 157 | \OCP\Util::writeLog('user_ldap', 'LDAP resource not available.', \OCP\Util::DEBUG); |
||
| 158 | return false; |
||
| 159 | } |
||
| 160 | //Cancel possibly running Paged Results operation, otherwise we run in |
||
| 161 | //LDAP protocol errors |
||
| 162 | 4 | $this->abandonPagedSearch(); |
|
| 163 | // openLDAP requires that we init a new Paged Search. Not needed by AD, |
||
| 164 | // but does not hurt either. |
||
| 165 | 4 | $pagingSize = intval($this->connection->ldapPagingSize); |
|
| 166 | // 0 won't result in replies, small numbers may leave out groups |
||
| 167 | // (cf. #12306), 500 is default for paging and should work everywhere. |
||
| 168 | 4 | $maxResults = $pagingSize > 20 ? $pagingSize : 500; |
|
| 169 | 4 | $this->initPagedSearch($filter, array($dn), array($attr), $maxResults, 0); |
|
| 170 | 4 | $dn = $this->DNasBaseParameter($dn); |
|
| 171 | 4 | $rr = @$this->ldap->read($cr, $dn, $filter, array($attr)); |
|
|
|
|||
| 172 | 4 | if(!$this->ldap->isResource($rr)) { |
|
| 173 | if(!empty($attr)) { |
||
| 174 | //do not throw this message on userExists check, irritates |
||
| 175 | \OCP\Util::writeLog('user_ldap', 'readAttribute failed for DN '.$dn, \OCP\Util::DEBUG); |
||
| 176 | } |
||
| 177 | //in case an error occurs , e.g. object does not exist |
||
| 178 | return false; |
||
| 179 | } |
||
| 180 | 4 | if (empty($attr)) { |
|
| 181 | \OCP\Util::writeLog('user_ldap', 'readAttribute: '.$dn.' found', \OCP\Util::DEBUG); |
||
| 182 | return array(); |
||
| 183 | } |
||
| 184 | 4 | $er = $this->ldap->firstEntry($cr, $rr); |
|
| 185 | 4 | if(!$this->ldap->isResource($er)) { |
|
| 186 | //did not match the filter, return false |
||
| 187 | return false; |
||
| 188 | } |
||
| 189 | //LDAP attributes are not case sensitive |
||
| 190 | 4 | $result = \OCP\Util::mb_array_change_key_case( |
|
| 191 | 4 | $this->ldap->getAttributes($cr, $er), MB_CASE_LOWER, 'UTF-8'); |
|
| 192 | 4 | $attr = mb_strtolower($attr, 'UTF-8'); |
|
| 193 | |||
| 194 | 4 | if(isset($result[$attr]) && $result[$attr]['count'] > 0) { |
|
| 195 | 4 | $values = array(); |
|
| 196 | 4 | for($i=0;$i<$result[$attr]['count'];$i++) { |
|
| 197 | 4 | if($this->resemblesDN($attr)) { |
|
| 198 | 4 | $values[] = $this->sanitizeDN($result[$attr][$i]); |
|
| 199 | 4 | } elseif(strtolower($attr) === 'objectguid' || strtolower($attr) === 'guid') { |
|
| 200 | $values[] = $this->convertObjectGUID2Str($result[$attr][$i]); |
||
| 201 | } else { |
||
| 202 | $values[] = $result[$attr][$i]; |
||
| 203 | } |
||
| 204 | 4 | } |
|
| 205 | 4 | return $values; |
|
| 206 | } |
||
| 207 | \OCP\Util::writeLog('user_ldap', 'Requested attribute '.$attr.' not found for '.$dn, \OCP\Util::DEBUG); |
||
| 208 | return false; |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * checks whether the given attributes value is probably a DN |
||
| 213 | * @param string $attr the attribute in question |
||
| 214 | * @return boolean if so true, otherwise false |
||
| 215 | */ |
||
| 216 | 4 | private function resemblesDN($attr) { |
|
| 217 | $resemblingAttributes = array( |
||
| 218 | 4 | 'dn', |
|
| 219 | 4 | 'uniquemember', |
|
| 220 | 4 | 'member', |
|
| 221 | // memberOf is an "operational" attribute, without a definition in any RFC |
||
| 222 | 'memberof' |
||
| 223 | 4 | ); |
|
| 224 | 4 | return in_array($attr, $resemblingAttributes); |
|
| 225 | } |
||
| 226 | |||
| 227 | /** |
||
| 228 | * checks whether the given string is probably a DN |
||
| 229 | * @param string $string |
||
| 230 | * @return boolean |
||
| 231 | */ |
||
| 232 | 2 | public function stringResemblesDN($string) { |
|
| 233 | 2 | $r = $this->ldap->explodeDN($string, 0); |
|
| 234 | // if exploding a DN succeeds and does not end up in |
||
| 235 | // an empty array except for $r[count] being 0. |
||
| 236 | 2 | return (is_array($r) && count($r) > 1); |
|
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * sanitizes a DN received from the LDAP server |
||
| 241 | * @param array $dn the DN in question |
||
| 242 | * @return array the sanitized DN |
||
| 243 | */ |
||
| 244 | 4 | private function sanitizeDN($dn) { |
|
| 245 | //treating multiple base DNs |
||
| 246 | 4 | if(is_array($dn)) { |
|
| 247 | $result = array(); |
||
| 248 | foreach($dn as $singleDN) { |
||
| 249 | $result[] = $this->sanitizeDN($singleDN); |
||
| 250 | } |
||
| 251 | return $result; |
||
| 252 | } |
||
| 253 | |||
| 254 | //OID sometimes gives back DNs with whitespace after the comma |
||
| 255 | // a la "uid=foo, cn=bar, dn=..." We need to tackle this! |
||
| 256 | 4 | $dn = preg_replace('/([^\\\]),(\s+)/u', '\1,', $dn); |
|
| 257 | |||
| 258 | //make comparisons and everything work |
||
| 259 | 4 | $dn = mb_strtolower($dn, 'UTF-8'); |
|
| 260 | |||
| 261 | //escape DN values according to RFC 2253 – this is already done by ldap_explode_dn |
||
| 262 | //to use the DN in search filters, \ needs to be escaped to \5c additionally |
||
| 263 | //to use them in bases, we convert them back to simple backslashes in readAttribute() |
||
| 264 | $replacements = array( |
||
| 265 | 4 | '\,' => '\5c2C', |
|
| 266 | 4 | '\=' => '\5c3D', |
|
| 267 | 4 | '\+' => '\5c2B', |
|
| 268 | 4 | '\<' => '\5c3C', |
|
| 269 | 4 | '\>' => '\5c3E', |
|
| 270 | 4 | '\;' => '\5c3B', |
|
| 271 | 4 | '\"' => '\5c22', |
|
| 272 | 4 | '\#' => '\5c23', |
|
| 273 | 4 | '(' => '\28', |
|
| 274 | 4 | ')' => '\29', |
|
| 275 | 4 | '*' => '\2A', |
|
| 276 | 4 | ); |
|
| 277 | 4 | $dn = str_replace(array_keys($replacements), array_values($replacements), $dn); |
|
| 278 | |||
| 279 | 4 | return $dn; |
|
| 280 | } |
||
| 281 | |||
| 282 | /** |
||
| 283 | * returns a DN-string that is cleaned from not domain parts, e.g. |
||
| 284 | * cn=foo,cn=bar,dc=foobar,dc=server,dc=org |
||
| 285 | * becomes dc=foobar,dc=server,dc=org |
||
| 286 | * @param string $dn |
||
| 287 | * @return string |
||
| 288 | */ |
||
| 289 | 2 | public function getDomainDNFromDN($dn) { |
|
| 290 | 2 | $allParts = $this->ldap->explodeDN($dn, 0); |
|
| 291 | 2 | if($allParts === false) { |
|
| 292 | //not a valid DN |
||
| 293 | 1 | return ''; |
|
| 294 | } |
||
| 295 | 1 | $domainParts = array(); |
|
| 296 | 1 | $dcFound = false; |
|
| 297 | 1 | foreach($allParts as $part) { |
|
| 298 | 1 | if(!$dcFound && strpos($part, 'dc=') === 0) { |
|
| 299 | 1 | $dcFound = true; |
|
| 300 | 1 | } |
|
| 301 | 1 | if($dcFound) { |
|
| 302 | 1 | $domainParts[] = $part; |
|
| 303 | 1 | } |
|
| 304 | 1 | } |
|
| 305 | 1 | $domainDN = implode(',', $domainParts); |
|
| 306 | 1 | return $domainDN; |
|
| 307 | } |
||
| 308 | |||
| 309 | /** |
||
| 310 | * returns the LDAP DN for the given internal ownCloud name of the group |
||
| 311 | * @param string $name the ownCloud name in question |
||
| 312 | * @return string|false LDAP DN on success, otherwise false |
||
| 313 | */ |
||
| 314 | public function groupname2dn($name) { |
||
| 317 | |||
| 318 | /** |
||
| 319 | * returns the LDAP DN for the given internal ownCloud name of the user |
||
| 320 | * @param string $name the ownCloud name in question |
||
| 321 | * @return string|false with the LDAP DN on success, otherwise false |
||
| 322 | */ |
||
| 323 | public function username2dn($name) { |
||
| 324 | $fdn = $this->userMapper->getDNbyName($name); |
||
| 325 | |||
| 326 | //Check whether the DN belongs to the Base, to avoid issues on multi- |
||
| 327 | //server setups |
||
| 328 | if(is_string($fdn) && $this->isDNPartOfBase($fdn, $this->connection->ldapBaseUsers)) { |
||
| 329 | return $fdn; |
||
| 330 | } |
||
| 331 | |||
| 332 | return false; |
||
| 333 | } |
||
| 334 | |||
| 335 | /** |
||
| 336 | public function ocname2dn($name, $isUser) { |
||
| 337 | * returns the internal ownCloud name for the given LDAP DN of the group, false on DN outside of search DN or failure |
||
| 338 | * @param string $fdn the dn of the group object |
||
| 339 | * @param string $ldapName optional, the display name of the object |
||
| 340 | * @return string|false with the name to use in ownCloud, false on DN outside of search DN |
||
| 341 | */ |
||
| 342 | View Code Duplication | public function dn2groupname($fdn, $ldapName = null) { |
|
| 343 | //To avoid bypassing the base DN settings under certain circumstances |
||
| 344 | //with the group support, check whether the provided DN matches one of |
||
| 345 | //the given Bases |
||
| 346 | if(!$this->isDNPartOfBase($fdn, $this->connection->ldapBaseGroups)) { |
||
| 347 | return false; |
||
| 348 | } |
||
| 349 | |||
| 350 | return $this->dn2ocname($fdn, $ldapName, false); |
||
| 351 | } |
||
| 352 | |||
| 353 | /** |
||
| 354 | * accepts an array of group DNs and tests whether they match the user |
||
| 355 | * filter by doing read operations against the group entries. Returns an |
||
| 356 | * array of DNs that match the filter. |
||
| 357 | * |
||
| 358 | * @param string[] $groupDNs |
||
| 359 | * @return string[] |
||
| 360 | */ |
||
| 361 | public function groupsMatchFilter($groupDNs) { |
||
| 362 | $validGroupDNs = []; |
||
| 363 | foreach($groupDNs as $dn) { |
||
| 364 | $cacheKey = 'groupsMatchFilter-'.$dn; |
||
| 365 | if($this->connection->isCached($cacheKey)) { |
||
| 366 | if($this->connection->getFromCache($cacheKey)) { |
||
| 367 | $validGroupDNs[] = $dn; |
||
| 368 | } |
||
| 369 | continue; |
||
| 370 | } |
||
| 371 | |||
| 372 | // Check the base DN first. If this is not met already, we don't |
||
| 373 | // need to ask the server at all. |
||
| 374 | if(!$this->isDNPartOfBase($dn, $this->connection->ldapBaseGroups)) { |
||
| 375 | $this->connection->writeToCache($cacheKey, false); |
||
| 376 | continue; |
||
| 377 | } |
||
| 378 | |||
| 379 | $result = $this->readAttribute($dn, 'cn', $this->connection->ldapGroupFilter); |
||
| 380 | if(is_array($result)) { |
||
| 381 | $this->connection->writeToCache($cacheKey, true); |
||
| 382 | $validGroupDNs[] = $dn; |
||
| 383 | } else { |
||
| 384 | $this->connection->writeToCache($cacheKey, false); |
||
| 385 | } |
||
| 386 | |||
| 387 | } |
||
| 388 | return $validGroupDNs; |
||
| 389 | } |
||
| 390 | |||
| 391 | /** |
||
| 392 | * returns the internal ownCloud name for the given LDAP DN of the user, false on DN outside of search DN or failure |
||
| 393 | * @param string $dn the dn of the user object |
||
| 394 | * @param string $ldapName optional, the display name of the object |
||
| 395 | * @return string|false with with the name to use in ownCloud |
||
| 396 | */ |
||
| 397 | View Code Duplication | public function dn2username($fdn, $ldapName = null) { |
|
| 398 | //To avoid bypassing the base DN settings under certain circumstances |
||
| 399 | //with the group support, check whether the provided DN matches one of |
||
| 400 | //the given Bases |
||
| 401 | if(!$this->isDNPartOfBase($fdn, $this->connection->ldapBaseUsers)) { |
||
| 402 | return false; |
||
| 403 | } |
||
| 404 | |||
| 405 | return $this->dn2ocname($fdn, $ldapName, true); |
||
| 406 | } |
||
| 407 | |||
| 408 | /** |
||
| 409 | * returns an internal ownCloud name for the given LDAP DN, false on DN outside of search DN |
||
| 410 | * @param string $dn the dn of the user object |
||
| 411 | * @param string $ldapName optional, the display name of the object |
||
| 412 | * @param bool $isUser optional, whether it is a user object (otherwise group assumed) |
||
| 413 | * @return string|false with with the name to use in ownCloud |
||
| 414 | */ |
||
| 415 | 1 | public function dn2ocname($fdn, $ldapName = null, $isUser = true) { |
|
| 416 | 1 | if($isUser) { |
|
| 417 | 1 | $mapper = $this->getUserMapper(); |
|
| 418 | 1 | $nameAttribute = $this->connection->ldapUserDisplayName; |
|
| 419 | 1 | } else { |
|
| 420 | $mapper = $this->getGroupMapper(); |
||
| 421 | $nameAttribute = $this->connection->ldapGroupDisplayName; |
||
| 422 | } |
||
| 423 | |||
| 424 | //let's try to retrieve the ownCloud name from the mappings table |
||
| 425 | 1 | $ocName = $mapper->getNameByDN($fdn); |
|
| 426 | 1 | if(is_string($ocName)) { |
|
| 427 | 1 | return $ocName; |
|
| 428 | } |
||
| 429 | |||
| 430 | //second try: get the UUID and check if it is known. Then, update the DN and return the name. |
||
| 431 | $uuid = $this->getUUID($fdn, $isUser); |
||
| 432 | if(is_string($uuid)) { |
||
| 433 | $ocName = $mapper->getNameByUUID($uuid); |
||
| 434 | if(is_string($ocName)) { |
||
| 435 | $mapper->setDNbyUUID($fdn, $uuid); |
||
| 436 | return $ocName; |
||
| 437 | } |
||
| 438 | } else { |
||
| 439 | //If the UUID can't be detected something is foul. |
||
| 440 | \OCP\Util::writeLog('user_ldap', 'Cannot determine UUID for '.$fdn.'. Skipping.', \OCP\Util::INFO); |
||
| 441 | return false; |
||
| 442 | } |
||
| 443 | |||
| 444 | if(is_null($ldapName)) { |
||
| 445 | $ldapName = $this->readAttribute($fdn, $nameAttribute); |
||
| 446 | if(!isset($ldapName[0]) && empty($ldapName[0])) { |
||
| 447 | \OCP\Util::writeLog('user_ldap', 'No or empty name for '.$fdn.'.', \OCP\Util::INFO); |
||
| 448 | return false; |
||
| 449 | } |
||
| 450 | $ldapName = $ldapName[0]; |
||
| 451 | } |
||
| 452 | |||
| 453 | if($isUser) { |
||
| 454 | $usernameAttribute = $this->connection->ldapExpertUsernameAttr; |
||
| 455 | if(!empty($usernameAttribute)) { |
||
| 456 | $username = $this->readAttribute($fdn, $usernameAttribute); |
||
| 457 | $username = $username[0]; |
||
| 458 | } else { |
||
| 459 | $username = $uuid; |
||
| 460 | } |
||
| 461 | $intName = $this->sanitizeUsername($username); |
||
| 462 | } else { |
||
| 463 | $intName = $ldapName; |
||
| 464 | } |
||
| 465 | |||
| 466 | //a new user/group! Add it only if it doesn't conflict with other backend's users or existing groups |
||
| 467 | //disabling Cache is required to avoid that the new user is cached as not-existing in fooExists check |
||
| 468 | //NOTE: mind, disabling cache affects only this instance! Using it |
||
| 469 | // outside of core user management will still cache the user as non-existing. |
||
| 470 | $originalTTL = $this->connection->ldapCacheTTL; |
||
| 471 | $this->connection->setConfiguration(array('ldapCacheTTL' => 0)); |
||
| 472 | if(($isUser && !\OCP\User::userExists($intName)) |
||
| 473 | || (!$isUser && !\OC_Group::groupExists($intName))) { |
||
| 474 | if($mapper->map($fdn, $intName, $uuid)) { |
||
| 475 | $this->connection->setConfiguration(array('ldapCacheTTL' => $originalTTL)); |
||
| 476 | return $intName; |
||
| 477 | } |
||
| 478 | } |
||
| 479 | $this->connection->setConfiguration(array('ldapCacheTTL' => $originalTTL)); |
||
| 480 | |||
| 481 | $altName = $this->createAltInternalOwnCloudName($intName, $isUser); |
||
| 482 | if(is_string($altName) && $mapper->map($fdn, $altName, $uuid)) { |
||
| 483 | return $altName; |
||
| 484 | } |
||
| 485 | |||
| 486 | //if everything else did not help.. |
||
| 487 | \OCP\Util::writeLog('user_ldap', 'Could not create unique name for '.$fdn.'.', \OCP\Util::INFO); |
||
| 488 | return false; |
||
| 489 | } |
||
| 490 | |||
| 491 | /** |
||
| 492 | * gives back the user names as they are used ownClod internally |
||
| 493 | * @param array $ldapUsers as returned by fetchList() |
||
| 494 | * @return array an array with the user names to use in ownCloud |
||
| 495 | * |
||
| 496 | * gives back the user names as they are used ownClod internally |
||
| 497 | */ |
||
| 498 | public function ownCloudUserNames($ldapUsers) { |
||
| 501 | |||
| 502 | /** |
||
| 503 | * gives back the group names as they are used ownClod internally |
||
| 504 | * @param array $ldapGroups as returned by fetchList() |
||
| 505 | * @return array an array with the group names to use in ownCloud |
||
| 506 | * |
||
| 507 | * gives back the group names as they are used ownClod internally |
||
| 508 | */ |
||
| 509 | public function ownCloudGroupNames($ldapGroups) { |
||
| 512 | |||
| 513 | /** |
||
| 514 | * @param array $ldapObjects as returned by fetchList() |
||
| 515 | * @param bool $isUsers |
||
| 516 | * @return array |
||
| 517 | */ |
||
| 518 | private function ldap2ownCloudNames($ldapObjects, $isUsers) { |
||
| 519 | if($isUsers) { |
||
| 520 | $nameAttribute = $this->connection->ldapUserDisplayName; |
||
| 521 | $sndAttribute = $this->connection->ldapUserDisplayName2; |
||
| 522 | } else { |
||
| 523 | $nameAttribute = $this->connection->ldapGroupDisplayName; |
||
| 524 | } |
||
| 525 | $ownCloudNames = array(); |
||
| 554 | |||
| 555 | /** |
||
| 556 | * caches the user display name |
||
| 557 | * @param string $ocName the internal ownCloud username |
||
| 558 | * @param string|false $home the home directory path |
||
| 559 | */ |
||
| 560 | 1 | public function cacheUserHome($ocName, $home) { |
|
| 564 | |||
| 565 | /** |
||
| 566 | * caches a user as existing |
||
| 567 | * @param string $ocName the internal ownCloud username |
||
| 568 | */ |
||
| 569 | 1 | public function cacheUserExists($ocName) { |
|
| 572 | |||
| 573 | /** |
||
| 574 | * caches the user display name |
||
| 575 | * @param string $ocName the internal ownCloud username |
||
| 576 | * @param string $displayName the display name |
||
| 577 | * @param string $displayName2 the second display name |
||
| 578 | */ |
||
| 579 | public function cacheUserDisplayName($ocName, $displayName, $displayName2 = '') { |
||
| 585 | |||
| 586 | /** |
||
| 587 | * creates a unique name for internal ownCloud use for users. Don't call it directly. |
||
| 588 | * @param string $name the display name of the object |
||
| 589 | * @return string|false with with the name to use in ownCloud or false if unsuccessful |
||
| 590 | * |
||
| 591 | * Instead of using this method directly, call |
||
| 592 | * createAltInternalOwnCloudName($name, true) |
||
| 593 | */ |
||
| 594 | private function _createAltInternalOwnCloudNameForUsers($name) { |
||
| 607 | |||
| 608 | /** |
||
| 609 | * creates a unique name for internal ownCloud use for groups. Don't call it directly. |
||
| 610 | * @param string $name the display name of the object |
||
| 611 | * @return string|false with with the name to use in ownCloud or false if unsuccessful. |
||
| 612 | * |
||
| 613 | * Instead of using this method directly, call |
||
| 614 | * createAltInternalOwnCloudName($name, false) |
||
| 615 | * |
||
| 616 | * Group names are also used as display names, so we do a sequential |
||
| 617 | * numbering, e.g. Developers_42 when there are 41 other groups called |
||
| 618 | * "Developers" |
||
| 619 | */ |
||
| 620 | private function _createAltInternalOwnCloudNameForGroups($name) { |
||
| 645 | |||
| 646 | /** |
||
| 647 | * creates a unique name for internal ownCloud use. |
||
| 648 | * @param string $name the display name of the object |
||
| 649 | * @param boolean $isUser whether name should be created for a user (true) or a group (false) |
||
| 650 | * @return string|false with with the name to use in ownCloud or false if unsuccessful |
||
| 651 | */ |
||
| 652 | private function createAltInternalOwnCloudName($name, $isUser) { |
||
| 664 | |||
| 665 | /** |
||
| 666 | * fetches a list of users according to a provided loginName and utilizing |
||
| 667 | * the login filter. |
||
| 668 | * |
||
| 669 | * @param string $loginName |
||
| 670 | * @param array $attributes optional, list of attributes to read |
||
| 671 | * @return array |
||
| 672 | */ |
||
| 673 | View Code Duplication | public function fetchUsersByLoginName($loginName, $attributes = array('dn')) { |
|
| 679 | |||
| 680 | /** |
||
| 681 | * counts the number of users according to a provided loginName and |
||
| 682 | * utilizing the login filter. |
||
| 683 | * |
||
| 684 | * @param string $loginName |
||
| 685 | * @return array |
||
| 686 | */ |
||
| 687 | View Code Duplication | public function countUsersByLoginName($loginName) { |
|
| 693 | |||
| 694 | /** |
||
| 695 | * @param string $filter |
||
| 696 | * @param string|string[] $attr |
||
| 697 | * @param int $limit |
||
| 698 | * @param int $offset |
||
| 699 | * @return array |
||
| 700 | */ |
||
| 701 | public function fetchListOfUsers($filter, $attr, $limit = null, $offset = null) { |
||
| 706 | |||
| 707 | /** |
||
| 708 | * provided with an array of LDAP user records the method will fetch the |
||
| 709 | * user object and requests it to process the freshly fetched attributes and |
||
| 710 | * and their values |
||
| 711 | * @param array $ldapRecords |
||
| 712 | */ |
||
| 713 | 1 | public function batchApplyUserAttributes(array $ldapRecords){ |
|
| 725 | |||
| 726 | /** |
||
| 727 | * @param string $filter |
||
| 728 | * @param string|string[] $attr |
||
| 729 | * @param int $limit |
||
| 730 | * @param int $offset |
||
| 731 | * @return array |
||
| 732 | */ |
||
| 733 | public function fetchListOfGroups($filter, $attr, $limit = null, $offset = null) { |
||
| 736 | |||
| 737 | /** |
||
| 738 | * @param array $list |
||
| 739 | * @param bool $manyAttributes |
||
| 740 | * @return array |
||
| 741 | */ |
||
| 742 | private function fetchList($list, $manyAttributes) { |
||
| 759 | |||
| 760 | /** |
||
| 761 | * executes an LDAP search, optimized for Users |
||
| 762 | * @param string $filter the LDAP filter for the search |
||
| 763 | * @param string|string[] $attr optional, when a certain attribute shall be filtered out |
||
| 764 | * @param integer $limit |
||
| 765 | * @param integer $offset |
||
| 766 | * @return array with the search result |
||
| 767 | * |
||
| 768 | * Executes an LDAP search |
||
| 769 | */ |
||
| 770 | public function searchUsers($filter, $attr = null, $limit = null, $offset = null) { |
||
| 773 | |||
| 774 | /** |
||
| 775 | * @param string $filter |
||
| 776 | * @param string|string[] $attr |
||
| 777 | * @param int $limit |
||
| 778 | * @param int $offset |
||
| 779 | * @return false|int |
||
| 780 | */ |
||
| 781 | public function countUsers($filter, $attr = array('dn'), $limit = null, $offset = null) { |
||
| 784 | |||
| 785 | /** |
||
| 786 | * executes an LDAP search, optimized for Groups |
||
| 787 | * @param string $filter the LDAP filter for the search |
||
| 788 | * @param string|string[] $attr optional, when a certain attribute shall be filtered out |
||
| 789 | * @param integer $limit |
||
| 790 | * @param integer $offset |
||
| 791 | * @return array with the search result |
||
| 792 | * |
||
| 793 | * Executes an LDAP search |
||
| 794 | */ |
||
| 795 | public function searchGroups($filter, $attr = null, $limit = null, $offset = null) { |
||
| 798 | |||
| 799 | /** |
||
| 800 | * returns the number of available groups |
||
| 801 | * @param string $filter the LDAP search filter |
||
| 802 | * @param string[] $attr optional |
||
| 803 | * @param int|null $limit |
||
| 804 | * @param int|null $offset |
||
| 805 | * @return int|bool |
||
| 806 | */ |
||
| 807 | public function countGroups($filter, $attr = array('dn'), $limit = null, $offset = null) { |
||
| 810 | |||
| 811 | /** |
||
| 812 | * returns the number of available objects on the base DN |
||
| 813 | * |
||
| 814 | * @param int|null $limit |
||
| 815 | * @param int|null $offset |
||
| 816 | * @return int|bool |
||
| 817 | */ |
||
| 818 | public function countObjects($limit = null, $offset = null) { |
||
| 821 | |||
| 822 | /** |
||
| 823 | * retrieved. Results will according to the order in the array. |
||
| 824 | * @param int $limit optional, maximum results to be counted |
||
| 825 | * @param int $offset optional, a starting point |
||
| 826 | * @return array|false array with the search result as first value and pagedSearchOK as |
||
| 827 | * second | false if not successful |
||
| 828 | */ |
||
| 829 | private function executeSearch($filter, $base, &$attr = null, $limit = null, $offset = null) { |
||
| 860 | |||
| 861 | /** |
||
| 862 | * processes an LDAP paged search operation |
||
| 863 | * @param array $sr the array containing the LDAP search resources |
||
| 864 | * @param string $filter the LDAP filter for the search |
||
| 865 | * @param array $base an array containing the LDAP subtree(s) that shall be searched |
||
| 866 | * @param int $iFoundItems number of results in the search operation |
||
| 867 | * @param int $limit maximum results to be counted |
||
| 868 | * @param int $offset a starting point |
||
| 869 | * @param bool $pagedSearchOK whether a paged search has been executed |
||
| 870 | * @param bool $skipHandling required for paged search when cookies to |
||
| 871 | * prior results need to be gained |
||
| 872 | * @return array|false array with the search result as first value and pagedSearchOK as |
||
| 873 | * second | false if not successful |
||
| 874 | */ |
||
| 875 | private function processPagedSearchStatus($sr, $filter, $base, $iFoundItems, $limit, $offset, $pagedSearchOK, $skipHandling) { |
||
| 901 | |||
| 902 | /** |
||
| 903 | * executes an LDAP search, but counts the results only |
||
| 904 | * @param string $filter the LDAP filter for the search |
||
| 905 | * @param array $base an array containing the LDAP subtree(s) that shall be searched |
||
| 906 | * @param string|string[] $attr optional, array, one or more attributes that shall be |
||
| 907 | * retrieved. Results will according to the order in the array. |
||
| 908 | * @param int $limit optional, maximum results to be counted |
||
| 909 | * @param int $offset optional, a starting point |
||
| 910 | * @param bool $skipHandling indicates whether the pages search operation is |
||
| 911 | * completed |
||
| 912 | * @return int|false Integer or false if the search could not be initialized |
||
| 913 | * |
||
| 914 | */ |
||
| 915 | private function count($filter, $base, $attr = null, $limit = null, $offset = null, $skipHandling = false) { |
||
| 946 | |||
| 947 | /** |
||
| 948 | * @param array $searchResults |
||
| 949 | * @param int $limit |
||
| 950 | * @param bool $hasHitLimit |
||
| 951 | * @return int |
||
| 952 | */ |
||
| 953 | private function countEntriesInSearchResults($searchResults, $limit, &$hasHitLimit) { |
||
| 967 | |||
| 968 | /** |
||
| 969 | * Executes an LDAP search |
||
| 970 | * @param string $filter the LDAP filter for the search |
||
| 971 | * @param array $base an array containing the LDAP subtree(s) that shall be searched |
||
| 972 | * @param string|string[] $attr optional, array, one or more attributes that shall be |
||
| 973 | * @param int $limit |
||
| 974 | * @param int $offset |
||
| 975 | * @param bool $skipHandling |
||
| 976 | * @return array with the search result |
||
| 977 | */ |
||
| 978 | private function search($filter, $base, $attr = null, $limit = null, $offset = null, $skipHandling = false) { |
||
| 1064 | |||
| 1065 | /** |
||
| 1066 | * @param string $name |
||
| 1067 | * @return bool|mixed|string |
||
| 1068 | */ |
||
| 1069 | public function sanitizeUsername($name) { |
||
| 1086 | |||
| 1087 | /** |
||
| 1088 | * escapes (user provided) parts for LDAP filter |
||
| 1089 | * @param string $input, the provided value |
||
| 1090 | * @param bool $allowAsterisk whether in * at the beginning should be preserved |
||
| 1091 | * @return string the escaped string |
||
| 1092 | */ |
||
| 1093 | 3 | public function escapeFilterPart($input, $allowAsterisk = false) { |
|
| 1103 | |||
| 1104 | /** |
||
| 1105 | * combines the input filters with AND |
||
| 1106 | * @param string[] $filters the filters to connect |
||
| 1107 | * @return string the combined filter |
||
| 1108 | */ |
||
| 1109 | public function combineFilterWithAnd($filters) { |
||
| 1112 | |||
| 1113 | /** |
||
| 1114 | * combines the input filters with OR |
||
| 1115 | * @param string[] $filters the filters to connect |
||
| 1116 | * @return string the combined filter |
||
| 1117 | * Combines Filter arguments with OR |
||
| 1118 | */ |
||
| 1119 | public function combineFilterWithOr($filters) { |
||
| 1122 | |||
| 1123 | /** |
||
| 1124 | * combines the input filters with given operator |
||
| 1125 | * @param string[] $filters the filters to connect |
||
| 1126 | * @param string $operator either & or | |
||
| 1127 | * @return string the combined filter |
||
| 1128 | */ |
||
| 1129 | private function combineFilter($filters, $operator) { |
||
| 1140 | |||
| 1141 | /** |
||
| 1142 | * creates a filter part for to perform search for users |
||
| 1143 | * @param string $search the search term |
||
| 1144 | * @return string the final filter part to use in LDAP searches |
||
| 1145 | */ |
||
| 1146 | public function getFilterPartForUserSearch($search) { |
||
| 1151 | |||
| 1152 | /** |
||
| 1153 | * creates a filter part for to perform search for groups |
||
| 1154 | * @param string $search the search term |
||
| 1155 | * @return string the final filter part to use in LDAP searches |
||
| 1156 | */ |
||
| 1157 | public function getFilterPartForGroupSearch($search) { |
||
| 1162 | |||
| 1163 | /** |
||
| 1164 | * creates a filter part for searches by splitting up the given search |
||
| 1165 | * string into single words |
||
| 1166 | * @param string $search the search term |
||
| 1167 | * @param string[] $searchAttributes needs to have at least two attributes, |
||
| 1168 | * otherwise it does not make sense :) |
||
| 1169 | * @return string the final filter part to use in LDAP searches |
||
| 1170 | * @throws \Exception |
||
| 1171 | */ |
||
| 1172 | private function getAdvancedFilterPartForSearch($search, $searchAttributes) { |
||
| 1189 | |||
| 1190 | /** |
||
| 1191 | * creates a filter part for searches |
||
| 1192 | * @param string $search the search term |
||
| 1193 | * @param string[]|null $searchAttributes |
||
| 1194 | * @param string $fallbackAttribute a fallback attribute in case the user |
||
| 1195 | * did not define search attributes. Typically the display name attribute. |
||
| 1196 | * @return string the final filter part to use in LDAP searches |
||
| 1197 | */ |
||
| 1198 | private function getFilterPartForSearch($search, $searchAttributes, $fallbackAttribute) { |
||
| 1228 | |||
| 1229 | /** |
||
| 1230 | * returns the filter used for counting users |
||
| 1231 | * @return string |
||
| 1232 | */ |
||
| 1233 | public function getFilterForUserCount() { |
||
| 1241 | |||
| 1242 | /** |
||
| 1243 | * @param string $name |
||
| 1244 | * @param string $password |
||
| 1245 | * @return bool |
||
| 1246 | */ |
||
| 1247 | public function areCredentialsValid($name, $password) { |
||
| 1261 | |||
| 1262 | /** |
||
| 1263 | * reverse lookup of a DN given a known UUID |
||
| 1264 | * |
||
| 1265 | * @param string $uuid |
||
| 1266 | * @return string |
||
| 1267 | * @throws \Exception |
||
| 1268 | */ |
||
| 1269 | public function getUserDnByUuid($uuid) { |
||
| 1309 | |||
| 1310 | /** |
||
| 1311 | * auto-detects the directory's UUID attribute |
||
| 1312 | * @param string $dn a known DN used to check against |
||
| 1313 | * @param bool $isUser |
||
| 1314 | * @param bool $force the detection should be run, even if it is not set to auto |
||
| 1315 | * @return bool true on success, false otherwise |
||
| 1316 | */ |
||
| 1317 | private function detectUuidAttribute($dn, $isUser = true, $force = false) { |
||
| 1354 | |||
| 1355 | /** |
||
| 1356 | * @param string $dn |
||
| 1357 | * @param bool $isUser |
||
| 1358 | * @return string|bool |
||
| 1359 | */ |
||
| 1360 | public function getUUID($dn, $isUser = true) { |
||
| 1385 | |||
| 1386 | /** |
||
| 1387 | * converts a binary ObjectGUID into a string representation |
||
| 1388 | * @param string $oguid the ObjectGUID in it's binary form as retrieved from AD |
||
| 1389 | * @return string |
||
| 1390 | * @link http://www.php.net/manual/en/function.ldap-get-values-len.php#73198 |
||
| 1391 | */ |
||
| 1392 | private function convertObjectGUID2Str($oguid) { |
||
| 1411 | |||
| 1412 | /** |
||
| 1413 | * the first three blocks of the string-converted GUID happen to be in |
||
| 1414 | * reverse order. In order to use it in a filter, this needs to be |
||
| 1415 | * corrected. Furthermore the dashes need to be replaced and \\ preprended |
||
| 1416 | * to every two hax figures. |
||
| 1417 | * |
||
| 1418 | * If an invalid string is passed, it will be returned without change. |
||
| 1419 | * |
||
| 1420 | * @param string $guid |
||
| 1421 | * @return string |
||
| 1422 | */ |
||
| 1423 | public function formatGuid2ForFilterUser($guid) { |
||
| 1458 | |||
| 1459 | /** |
||
| 1460 | * gets a SID of the domain of the given dn |
||
| 1461 | * @param string $dn |
||
| 1462 | * @return string|bool |
||
| 1463 | */ |
||
| 1464 | public function getSID($dn) { |
||
| 1481 | |||
| 1482 | /** |
||
| 1483 | * converts a binary SID into a string representation |
||
| 1484 | * @param string $sid |
||
| 1485 | * @return string |
||
| 1486 | */ |
||
| 1487 | 3 | public function convertSID2Str($sid) { |
|
| 1519 | |||
| 1520 | /** |
||
| 1521 | * converts a stored DN so it can be used as base parameter for LDAP queries, internally we store them for usage in LDAP filters |
||
| 1522 | * @param string $dn the DN |
||
| 1523 | * @return string |
||
| 1524 | */ |
||
| 1525 | 4 | private function DNasBaseParameter($dn) { |
|
| 1528 | |||
| 1529 | /** |
||
| 1530 | * checks if the given DN is part of the given base DN(s) |
||
| 1531 | * @param string $dn the DN |
||
| 1532 | * @param string[] $bases array containing the allowed base DN or DNs |
||
| 1533 | * @return bool |
||
| 1534 | */ |
||
| 1535 | public function isDNPartOfBase($dn, $bases) { |
||
| 1550 | |||
| 1551 | /** |
||
| 1552 | * resets a running Paged Search operation |
||
| 1553 | */ |
||
| 1554 | 4 | private function abandonPagedSearch() { |
|
| 1563 | |||
| 1564 | /** |
||
| 1565 | * get a cookie for the next LDAP paged search |
||
| 1566 | * @param string $base a string with the base DN for the search |
||
| 1567 | * @param string $filter the search filter to identify the correct search |
||
| 1568 | * @param int $limit the limit (or 'pageSize'), to identify the correct search well |
||
| 1569 | * @param int $offset the offset for the new search to identify the correct search really good |
||
| 1570 | * @return string containing the key or empty if none is cached |
||
| 1571 | */ |
||
| 1572 | private function getPagedResultCookie($base, $filter, $limit, $offset) { |
||
| 1588 | |||
| 1589 | /** |
||
| 1590 | * checks whether an LDAP paged search operation has more pages that can be |
||
| 1591 | * retrieved, typically when offset and limit are provided. |
||
| 1592 | * |
||
| 1593 | * Be very careful to use it: the last cookie value, which is inspected, can |
||
| 1594 | * be reset by other operations. Best, call it immediately after a search(), |
||
| 1595 | * searchUsers() or searchGroups() call. count-methods are probably safe as |
||
| 1596 | * well. Don't rely on it with any fetchList-method. |
||
| 1597 | * @return bool |
||
| 1598 | */ |
||
| 1599 | public function hasMoreResults() { |
||
| 1612 | |||
| 1613 | /** |
||
| 1614 | * set a cookie for LDAP paged search run |
||
| 1615 | * @param string $base a string with the base DN for the search |
||
| 1616 | * @param string $filter the search filter to identify the correct search |
||
| 1617 | * @param int $limit the limit (or 'pageSize'), to identify the correct search well |
||
| 1618 | * @param int $offset the offset for the run search to identify the correct search really good |
||
| 1619 | * @param string $cookie string containing the cookie returned by ldap_control_paged_result_response |
||
| 1620 | * @return void |
||
| 1621 | */ |
||
| 1622 | private function setPagedResultCookie($base, $filter, $limit, $offset, $cookie) { |
||
| 1630 | |||
| 1631 | /** |
||
| 1632 | * Check whether the most recent paged search was successful. It flushed the state var. Use it always after a possible paged search. |
||
| 1633 | * @return boolean|null true on success, null or false otherwise |
||
| 1634 | */ |
||
| 1635 | public function getPagedSearchResultState() { |
||
| 1640 | |||
| 1641 | /** |
||
| 1642 | * Prepares a paged search, if possible |
||
| 1643 | * @param string $filter the LDAP filter for the search |
||
| 1644 | * @param string[] $bases an array containing the LDAP subtree(s) that shall be searched |
||
| 1645 | * @param string[] $attr optional, when a certain attribute shall be filtered outside |
||
| 1646 | * @param int $limit |
||
| 1647 | * @param int $offset |
||
| 1648 | * @return bool|true |
||
| 1649 | */ |
||
| 1650 | 4 | private function initPagedSearch($filter, $bases, $attr, $limit, $offset) { |
|
| 1712 | |||
| 1713 | } |
||
| 1714 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: