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 Wizard 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 Wizard, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 37 | class Wizard extends LDAPUtility { |
||
| 38 | static protected $l; |
||
| 39 | protected $access; |
||
| 40 | protected $cr; |
||
| 41 | protected $configuration; |
||
| 42 | protected $result; |
||
| 43 | protected $resultCache = array(); |
||
| 44 | |||
| 45 | const LRESULT_PROCESSED_OK = 2; |
||
| 46 | const LRESULT_PROCESSED_INVALID = 3; |
||
| 47 | const LRESULT_PROCESSED_SKIP = 4; |
||
| 48 | |||
| 49 | const LFILTER_LOGIN = 2; |
||
| 50 | const LFILTER_USER_LIST = 3; |
||
| 51 | const LFILTER_GROUP_LIST = 4; |
||
| 52 | |||
| 53 | const LFILTER_MODE_ASSISTED = 2; |
||
| 54 | const LFILTER_MODE_RAW = 1; |
||
| 55 | |||
| 56 | const LDAP_NW_TIMEOUT = 4; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Constructor |
||
| 60 | * @param Configuration $configuration an instance of Configuration |
||
| 61 | * @param ILDAPWrapper $ldap an instance of ILDAPWrapper |
||
| 62 | */ |
||
| 63 | 7 | public function __construct(Configuration $configuration, ILDAPWrapper $ldap, Access $access) { |
|
| 64 | 7 | parent::__construct($ldap); |
|
| 65 | 7 | $this->configuration = $configuration; |
|
| 66 | 7 | if(is_null(Wizard::$l)) { |
|
| 67 | 1 | Wizard::$l = \OC::$server->getL10N('user_ldap'); |
|
| 68 | 1 | } |
|
| 69 | 7 | $this->access = $access; |
|
| 70 | 7 | $this->result = new WizardResult(); |
|
| 71 | 7 | } |
|
| 72 | |||
| 73 | 7 | public function __destruct() { |
|
| 74 | 7 | if($this->result->hasChanges()) { |
|
| 75 | 2 | $this->configuration->saveConfiguration(); |
|
| 76 | 2 | } |
|
| 77 | 7 | } |
|
| 78 | |||
| 79 | /** |
||
| 80 | * counts entries in the LDAP directory |
||
| 81 | * |
||
| 82 | * @param string $filter the LDAP search filter |
||
| 83 | * @param string $type a string being either 'users' or 'groups'; |
||
| 84 | * @return bool|int |
||
| 85 | * @throws \Exception |
||
| 86 | */ |
||
| 87 | public function countEntries($filter, $type) { |
||
| 88 | $reqs = array('ldapHost', 'ldapPort', 'ldapBase'); |
||
| 89 | if($type === 'users') { |
||
| 90 | $reqs[] = 'ldapUserFilter'; |
||
| 91 | } |
||
| 92 | if(!$this->checkRequirements($reqs)) { |
||
| 93 | throw new \Exception('Requirements not met', 400); |
||
| 94 | } |
||
| 95 | |||
| 96 | $attr = array('dn'); // default |
||
| 97 | $limit = 1001; |
||
| 98 | if($type === 'groups') { |
||
| 99 | $result = $this->access->countGroups($filter, $attr, $limit); |
||
| 100 | } else if($type === 'users') { |
||
| 101 | $result = $this->access->countUsers($filter, $attr, $limit); |
||
| 102 | } else if ($type === 'objects') { |
||
| 103 | $result = $this->access->countObjects($limit); |
||
| 104 | } else { |
||
| 105 | throw new \Exception('internal error: invalid object type', 500); |
||
| 106 | } |
||
| 107 | |||
| 108 | return $result; |
||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * formats the return value of a count operation to the string to be |
||
| 113 | * inserted. |
||
| 114 | * |
||
| 115 | * @param bool|int $count |
||
| 116 | * @return int|string |
||
| 117 | */ |
||
| 118 | private function formatCountResult($count) { |
||
| 119 | $formatted = ($count !== false) ? $count : 0; |
||
| 120 | if($formatted > 1000) { |
||
| 121 | $formatted = '> 1000'; |
||
| 122 | } |
||
| 123 | return $formatted; |
||
| 124 | } |
||
| 125 | |||
| 126 | public function countGroups() { |
||
| 127 | $filter = $this->configuration->ldapGroupFilter; |
||
|
|
|||
| 128 | |||
| 129 | if(empty($filter)) { |
||
| 130 | $output = self::$l->n('%s group found', '%s groups found', 0, array(0)); |
||
| 131 | $this->result->addChange('ldap_group_count', $output); |
||
| 132 | return $this->result; |
||
| 133 | } |
||
| 134 | |||
| 135 | try { |
||
| 136 | $groupsTotal = $this->formatCountResult($this->countEntries($filter, 'groups')); |
||
| 137 | } catch (\Exception $e) { |
||
| 138 | //400 can be ignored, 500 is forwarded |
||
| 139 | if($e->getCode() === 500) { |
||
| 140 | throw $e; |
||
| 141 | } |
||
| 142 | return false; |
||
| 143 | } |
||
| 144 | $output = self::$l->n('%s group found', '%s groups found', $groupsTotal, array($groupsTotal)); |
||
| 145 | $this->result->addChange('ldap_group_count', $output); |
||
| 146 | return $this->result; |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @return WizardResult |
||
| 151 | * @throws \Exception |
||
| 152 | */ |
||
| 153 | public function countUsers() { |
||
| 154 | $filter = $this->access->getFilterForUserCount(); |
||
| 155 | |||
| 156 | $usersTotal = $this->formatCountResult($this->countEntries($filter, 'users')); |
||
| 157 | $output = self::$l->n('%s user found', '%s users found', $usersTotal, array($usersTotal)); |
||
| 158 | $this->result->addChange('ldap_user_count', $output); |
||
| 159 | return $this->result; |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * counts any objects in the currently set base dn |
||
| 164 | * |
||
| 165 | * @return WizardResult |
||
| 166 | * @throws \Exception |
||
| 167 | */ |
||
| 168 | public function countInBaseDN() { |
||
| 169 | // we don't need to provide a filter in this case |
||
| 170 | $total = $this->countEntries(null, 'objects'); |
||
| 171 | if($total === false) { |
||
| 172 | throw new \Exception('invalid results received'); |
||
| 173 | } |
||
| 174 | $this->result->addChange('ldap_test_base', $total); |
||
| 175 | return $this->result; |
||
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * counts users with a specified attribute |
||
| 180 | * @param string $attr |
||
| 181 | * @param bool $existsCheck |
||
| 182 | * @return int|bool |
||
| 183 | */ |
||
| 184 | 4 | public function countUsersWithAttribute($attr, $existsCheck = false) { |
|
| 185 | 4 | View Code Duplication | if(!$this->checkRequirements(array('ldapHost', |
| 186 | 4 | 'ldapPort', |
|
| 187 | 4 | 'ldapBase', |
|
| 188 | 4 | 'ldapUserFilter', |
|
| 189 | 4 | ))) { |
|
| 190 | return false; |
||
| 191 | } |
||
| 192 | |||
| 193 | 4 | $filter = $this->access->combineFilterWithAnd(array( |
|
| 194 | 4 | $this->configuration->ldapUserFilter, |
|
| 195 | $attr . '=*' |
||
| 196 | 4 | )); |
|
| 197 | |||
| 198 | 4 | $limit = ($existsCheck === false) ? null : 1; |
|
| 199 | |||
| 200 | 4 | return $this->access->countUsers($filter, array('dn'), $limit); |
|
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * detects the display name attribute. If a setting is already present that |
||
| 205 | * returns at least one hit, the detection will be canceled. |
||
| 206 | * @return WizardResult|bool |
||
| 207 | * @throws \Exception |
||
| 208 | */ |
||
| 209 | public function detectUserDisplayNameAttribute() { |
||
| 210 | View Code Duplication | if(!$this->checkRequirements(array('ldapHost', |
|
| 211 | 'ldapPort', |
||
| 212 | 'ldapBase', |
||
| 213 | 'ldapUserFilter', |
||
| 214 | ))) { |
||
| 215 | return false; |
||
| 216 | } |
||
| 217 | |||
| 218 | $attr = $this->configuration->ldapUserDisplayName; |
||
| 219 | if($attr !== 'displayName' && !empty($attr)) { |
||
| 220 | // most likely not the default value with upper case N, |
||
| 221 | // verify it still produces a result |
||
| 222 | $count = intval($this->countUsersWithAttribute($attr, true)); |
||
| 223 | if($count > 0) { |
||
| 224 | //no change, but we sent it back to make sure the user interface |
||
| 225 | //is still correct, even if the ajax call was cancelled inbetween |
||
| 226 | $this->result->addChange('ldap_display_name', $attr); |
||
| 227 | return $this->result; |
||
| 228 | } |
||
| 229 | } |
||
| 230 | |||
| 231 | // first attribute that has at least one result wins |
||
| 232 | $displayNameAttrs = array('displayname', 'cn'); |
||
| 233 | foreach ($displayNameAttrs as $attr) { |
||
| 234 | $count = intval($this->countUsersWithAttribute($attr, true)); |
||
| 235 | |||
| 236 | if($count > 0) { |
||
| 237 | $this->applyFind('ldap_display_name', $attr); |
||
| 238 | return $this->result; |
||
| 239 | } |
||
| 240 | }; |
||
| 241 | |||
| 242 | throw new \Exception(self::$l->t('Could not detect user display name attribute. Please specify it yourself in advanced ldap settings.')); |
||
| 243 | } |
||
| 244 | |||
| 245 | /** |
||
| 246 | * detects the most often used email attribute for users applying to the |
||
| 247 | * user list filter. If a setting is already present that returns at least |
||
| 248 | * one hit, the detection will be canceled. |
||
| 249 | * @return WizardResult|bool |
||
| 250 | */ |
||
| 251 | 4 | public function detectEmailAttribute() { |
|
| 252 | 4 | View Code Duplication | if(!$this->checkRequirements(array('ldapHost', |
| 253 | 4 | 'ldapPort', |
|
| 254 | 4 | 'ldapBase', |
|
| 255 | 4 | 'ldapUserFilter', |
|
| 256 | 4 | ))) { |
|
| 257 | return false; |
||
| 258 | } |
||
| 259 | |||
| 260 | 4 | $attr = $this->configuration->ldapEmailAttribute; |
|
| 261 | 4 | if(!empty($attr)) { |
|
| 262 | 3 | $count = intval($this->countUsersWithAttribute($attr, true)); |
|
| 263 | 3 | if($count > 0) { |
|
| 264 | 1 | return false; |
|
| 265 | } |
||
| 266 | 2 | $writeLog = true; |
|
| 267 | 2 | } else { |
|
| 268 | 1 | $writeLog = false; |
|
| 269 | } |
||
| 270 | |||
| 271 | 3 | $emailAttributes = array('mail', 'mailPrimaryAddress'); |
|
| 272 | 3 | $winner = ''; |
|
| 273 | 3 | $maxUsers = 0; |
|
| 274 | 3 | foreach($emailAttributes as $attr) { |
|
| 275 | 3 | $count = $this->countUsersWithAttribute($attr); |
|
| 276 | 3 | if($count > $maxUsers) { |
|
| 277 | 2 | $maxUsers = $count; |
|
| 278 | 2 | $winner = $attr; |
|
| 279 | 2 | } |
|
| 280 | 3 | } |
|
| 281 | |||
| 282 | 3 | if($winner !== '') { |
|
| 283 | 2 | $this->applyFind('ldap_email_attr', $winner); |
|
| 284 | 2 | if($writeLog) { |
|
| 285 | 1 | \OCP\Util::writeLog('user_ldap', 'The mail attribute has ' . |
|
| 286 | 1 | 'automatically been reset, because the original value ' . |
|
| 287 | 1 | 'did not return any results.', \OCP\Util::INFO); |
|
| 288 | 1 | } |
|
| 289 | 2 | } |
|
| 290 | |||
| 291 | 3 | return $this->result; |
|
| 292 | } |
||
| 293 | |||
| 294 | /** |
||
| 295 | * @return WizardResult |
||
| 296 | * @throws \Exception |
||
| 297 | */ |
||
| 298 | public function determineAttributes() { |
||
| 299 | View Code Duplication | if(!$this->checkRequirements(array('ldapHost', |
|
| 300 | 'ldapPort', |
||
| 301 | 'ldapBase', |
||
| 302 | 'ldapUserFilter', |
||
| 303 | ))) { |
||
| 304 | return false; |
||
| 305 | } |
||
| 306 | |||
| 307 | $attributes = $this->getUserAttributes(); |
||
| 308 | |||
| 309 | natcasesort($attributes); |
||
| 310 | $attributes = array_values($attributes); |
||
| 311 | |||
| 312 | $this->result->addOptions('ldap_loginfilter_attributes', $attributes); |
||
| 313 | |||
| 314 | $selected = $this->configuration->ldapLoginFilterAttributes; |
||
| 315 | if(is_array($selected) && !empty($selected)) { |
||
| 316 | $this->result->addChange('ldap_loginfilter_attributes', $selected); |
||
| 317 | } |
||
| 318 | |||
| 319 | return $this->result; |
||
| 320 | } |
||
| 321 | |||
| 322 | /** |
||
| 323 | * detects the available LDAP attributes |
||
| 324 | * @return array|false The instance's WizardResult instance |
||
| 325 | * @throws \Exception |
||
| 326 | */ |
||
| 327 | private function getUserAttributes() { |
||
| 328 | View Code Duplication | if(!$this->checkRequirements(array('ldapHost', |
|
| 329 | 'ldapPort', |
||
| 330 | 'ldapBase', |
||
| 331 | 'ldapUserFilter', |
||
| 332 | ))) { |
||
| 333 | return false; |
||
| 334 | } |
||
| 335 | $cr = $this->getConnection(); |
||
| 336 | if(!$cr) { |
||
| 337 | throw new \Exception('Could not connect to LDAP'); |
||
| 338 | } |
||
| 339 | |||
| 340 | $base = $this->configuration->ldapBase[0]; |
||
| 341 | $filter = $this->configuration->ldapUserFilter; |
||
| 342 | $rr = $this->ldap->search($cr, $base, $filter, array(), 1, 1); |
||
| 343 | if(!$this->ldap->isResource($rr)) { |
||
| 344 | return false; |
||
| 345 | } |
||
| 346 | $er = $this->ldap->firstEntry($cr, $rr); |
||
| 347 | $attributes = $this->ldap->getAttributes($cr, $er); |
||
| 348 | $pureAttributes = array(); |
||
| 349 | for($i = 0; $i < $attributes['count']; $i++) { |
||
| 350 | $pureAttributes[] = $attributes[$i]; |
||
| 351 | } |
||
| 352 | |||
| 353 | return $pureAttributes; |
||
| 354 | } |
||
| 355 | |||
| 356 | /** |
||
| 357 | * detects the available LDAP groups |
||
| 358 | * @return WizardResult|false the instance's WizardResult instance |
||
| 359 | */ |
||
| 360 | public function determineGroupsForGroups() { |
||
| 361 | return $this->determineGroups('ldap_groupfilter_groups', |
||
| 362 | 'ldapGroupFilterGroups', |
||
| 363 | false); |
||
| 364 | } |
||
| 365 | |||
| 366 | /** |
||
| 367 | * detects the available LDAP groups |
||
| 368 | * @return WizardResult|false the instance's WizardResult instance |
||
| 369 | */ |
||
| 370 | public function determineGroupsForUsers() { |
||
| 374 | |||
| 375 | /** |
||
| 376 | * detects the available LDAP groups |
||
| 377 | * @param string $dbKey |
||
| 378 | * @param string $confKey |
||
| 379 | * @param bool $testMemberOf |
||
| 380 | * @return WizardResult|false the instance's WizardResult instance |
||
| 381 | * @throws \Exception |
||
| 382 | */ |
||
| 383 | private function determineGroups($dbKey, $confKey, $testMemberOf = true) { |
||
| 384 | if(!$this->checkRequirements(array('ldapHost', |
||
| 385 | 'ldapPort', |
||
| 386 | 'ldapBase', |
||
| 387 | ))) { |
||
| 388 | return false; |
||
| 389 | } |
||
| 390 | $cr = $this->getConnection(); |
||
| 391 | if(!$cr) { |
||
| 392 | throw new \Exception('Could not connect to LDAP'); |
||
| 393 | } |
||
| 394 | |||
| 395 | $this->fetchGroups($dbKey, $confKey); |
||
| 396 | |||
| 397 | if($testMemberOf) { |
||
| 398 | $this->configuration->hasMemberOfFilterSupport = $this->testMemberOf(); |
||
| 399 | $this->result->markChange(); |
||
| 400 | if(!$this->configuration->hasMemberOfFilterSupport) { |
||
| 401 | throw new \Exception('memberOf is not supported by the server'); |
||
| 402 | } |
||
| 403 | } |
||
| 404 | |||
| 405 | return $this->result; |
||
| 406 | } |
||
| 407 | |||
| 408 | /** |
||
| 409 | * fetches all groups from LDAP and adds them to the result object |
||
| 410 | * |
||
| 411 | * @param string $dbKey |
||
| 412 | * @param string $confKey |
||
| 413 | * @return array $groupEntries |
||
| 414 | * @throws \Exception |
||
| 415 | */ |
||
| 416 | public function fetchGroups($dbKey, $confKey) { |
||
| 417 | $obclasses = array('posixGroup', 'group', 'zimbraDistributionList', 'groupOfNames'); |
||
| 418 | |||
| 419 | $filterParts = array(); |
||
| 420 | foreach($obclasses as $obclass) { |
||
| 421 | $filterParts[] = 'objectclass='.$obclass; |
||
| 422 | } |
||
| 423 | //we filter for everything |
||
| 424 | //- that looks like a group and |
||
| 425 | //- has the group display name set |
||
| 426 | $filter = $this->access->combineFilterWithOr($filterParts); |
||
| 427 | $filter = $this->access->combineFilterWithAnd(array($filter, 'cn=*')); |
||
| 428 | |||
| 429 | $groupNames = array(); |
||
| 430 | $groupEntries = array(); |
||
| 431 | $limit = 400; |
||
| 432 | $offset = 0; |
||
| 433 | do { |
||
| 434 | // we need to request dn additionally here, otherwise memberOf |
||
| 435 | // detection will fail later |
||
| 436 | $result = $this->access->searchGroups($filter, array('cn', 'dn'), $limit, $offset); |
||
| 437 | foreach($result as $item) { |
||
| 438 | if(!isset($item['cn']) && !is_array($item['cn']) && !isset($item['cn'][0])) { |
||
| 439 | // just in case - no issue known |
||
| 440 | continue; |
||
| 441 | } |
||
| 442 | $groupNames[] = $item['cn'][0]; |
||
| 443 | $groupEntries[] = $item; |
||
| 444 | } |
||
| 445 | $offset += $limit; |
||
| 446 | } while ($this->access->hasMoreResults()); |
||
| 447 | |||
| 448 | View Code Duplication | if(count($groupNames) > 0) { |
|
| 449 | natsort($groupNames); |
||
| 450 | $this->result->addOptions($dbKey, array_values($groupNames)); |
||
| 451 | } else { |
||
| 452 | throw new \Exception(self::$l->t('Could not find the desired feature')); |
||
| 453 | } |
||
| 454 | |||
| 455 | $setFeatures = $this->configuration->$confKey; |
||
| 456 | if(is_array($setFeatures) && !empty($setFeatures)) { |
||
| 457 | //something is already configured? pre-select it. |
||
| 458 | $this->result->addChange($dbKey, $setFeatures); |
||
| 459 | } |
||
| 460 | return $groupEntries; |
||
| 461 | } |
||
| 462 | |||
| 463 | public function determineGroupMemberAssoc() { |
||
| 464 | if(!$this->checkRequirements(array('ldapHost', |
||
| 465 | 'ldapPort', |
||
| 466 | 'ldapGroupFilter', |
||
| 467 | ))) { |
||
| 468 | return false; |
||
| 469 | } |
||
| 470 | $attribute = $this->detectGroupMemberAssoc(); |
||
| 471 | if($attribute === false) { |
||
| 472 | return false; |
||
| 473 | } |
||
| 474 | $this->configuration->setConfiguration(array('ldapGroupMemberAssocAttr' => $attribute)); |
||
| 475 | $this->result->addChange('ldap_group_member_assoc_attribute', $attribute); |
||
| 476 | |||
| 477 | return $this->result; |
||
| 478 | } |
||
| 479 | |||
| 480 | /** |
||
| 481 | * Detects the available object classes |
||
| 482 | * @return WizardResult|false the instance's WizardResult instance |
||
| 483 | * @throws \Exception |
||
| 484 | */ |
||
| 485 | public function determineGroupObjectClasses() { |
||
| 486 | if(!$this->checkRequirements(array('ldapHost', |
||
| 487 | 'ldapPort', |
||
| 488 | 'ldapBase', |
||
| 489 | ))) { |
||
| 490 | return false; |
||
| 491 | } |
||
| 492 | $cr = $this->getConnection(); |
||
| 493 | if(!$cr) { |
||
| 494 | throw new \Exception('Could not connect to LDAP'); |
||
| 495 | } |
||
| 496 | |||
| 497 | $obclasses = array('groupOfNames', 'group', 'posixGroup', '*'); |
||
| 498 | $this->determineFeature($obclasses, |
||
| 499 | 'objectclass', |
||
| 500 | 'ldap_groupfilter_objectclass', |
||
| 501 | 'ldapGroupFilterObjectclass', |
||
| 502 | false); |
||
| 503 | |||
| 504 | return $this->result; |
||
| 505 | } |
||
| 506 | |||
| 507 | /** |
||
| 508 | * detects the available object classes |
||
| 509 | * @return WizardResult |
||
| 510 | * @throws \Exception |
||
| 511 | */ |
||
| 512 | public function determineUserObjectClasses() { |
||
| 513 | if(!$this->checkRequirements(array('ldapHost', |
||
| 514 | 'ldapPort', |
||
| 515 | 'ldapBase', |
||
| 516 | ))) { |
||
| 517 | return false; |
||
| 518 | } |
||
| 519 | $cr = $this->getConnection(); |
||
| 520 | if(!$cr) { |
||
| 521 | throw new \Exception('Could not connect to LDAP'); |
||
| 522 | } |
||
| 523 | |||
| 524 | $obclasses = array('inetOrgPerson', 'person', 'organizationalPerson', |
||
| 525 | 'user', 'posixAccount', '*'); |
||
| 526 | $filter = $this->configuration->ldapUserFilter; |
||
| 527 | //if filter is empty, it is probably the first time the wizard is called |
||
| 528 | //then, apply suggestions. |
||
| 529 | $this->determineFeature($obclasses, |
||
| 530 | 'objectclass', |
||
| 531 | 'ldap_userfilter_objectclass', |
||
| 532 | 'ldapUserFilterObjectclass', |
||
| 533 | empty($filter)); |
||
| 534 | |||
| 535 | return $this->result; |
||
| 536 | } |
||
| 537 | |||
| 538 | /** |
||
| 539 | * @return WizardResult|false |
||
| 540 | * @throws \Exception |
||
| 541 | */ |
||
| 542 | View Code Duplication | public function getGroupFilter() { |
|
| 543 | if(!$this->checkRequirements(array('ldapHost', |
||
| 544 | 'ldapPort', |
||
| 545 | 'ldapBase', |
||
| 546 | ))) { |
||
| 547 | return false; |
||
| 548 | } |
||
| 549 | //make sure the use display name is set |
||
| 550 | $displayName = $this->configuration->ldapGroupDisplayName; |
||
| 551 | if(empty($displayName)) { |
||
| 552 | $d = $this->configuration->getDefaults(); |
||
| 553 | $this->applyFind('ldap_group_display_name', |
||
| 554 | $d['ldap_group_display_name']); |
||
| 555 | } |
||
| 556 | $filter = $this->composeLdapFilter(self::LFILTER_GROUP_LIST); |
||
| 557 | |||
| 558 | $this->applyFind('ldap_group_filter', $filter); |
||
| 559 | return $this->result; |
||
| 560 | } |
||
| 561 | |||
| 562 | /** |
||
| 563 | * @return WizardResult|false |
||
| 564 | * @throws \Exception |
||
| 565 | */ |
||
| 566 | View Code Duplication | public function getUserListFilter() { |
|
| 567 | if(!$this->checkRequirements(array('ldapHost', |
||
| 568 | 'ldapPort', |
||
| 569 | 'ldapBase', |
||
| 570 | ))) { |
||
| 571 | return false; |
||
| 572 | } |
||
| 573 | //make sure the use display name is set |
||
| 574 | $displayName = $this->configuration->ldapUserDisplayName; |
||
| 575 | if(empty($displayName)) { |
||
| 576 | $d = $this->configuration->getDefaults(); |
||
| 577 | $this->applyFind('ldap_display_name', $d['ldap_display_name']); |
||
| 578 | } |
||
| 579 | $filter = $this->composeLdapFilter(self::LFILTER_USER_LIST); |
||
| 580 | if(!$filter) { |
||
| 581 | throw new \Exception('Cannot create filter'); |
||
| 582 | } |
||
| 583 | |||
| 584 | $this->applyFind('ldap_userlist_filter', $filter); |
||
| 585 | return $this->result; |
||
| 586 | } |
||
| 587 | |||
| 588 | /** |
||
| 589 | * @return bool|WizardResult |
||
| 590 | * @throws \Exception |
||
| 591 | */ |
||
| 592 | public function getUserLoginFilter() { |
||
| 593 | View Code Duplication | if(!$this->checkRequirements(array('ldapHost', |
|
| 594 | 'ldapPort', |
||
| 595 | 'ldapBase', |
||
| 596 | 'ldapUserFilter', |
||
| 597 | ))) { |
||
| 598 | return false; |
||
| 599 | } |
||
| 600 | |||
| 601 | $filter = $this->composeLdapFilter(self::LFILTER_LOGIN); |
||
| 602 | if(!$filter) { |
||
| 603 | throw new \Exception('Cannot create filter'); |
||
| 604 | } |
||
| 605 | |||
| 606 | $this->applyFind('ldap_login_filter', $filter); |
||
| 607 | return $this->result; |
||
| 608 | } |
||
| 609 | |||
| 610 | /** |
||
| 611 | * @return bool|WizardResult |
||
| 612 | * @param string $loginName |
||
| 613 | * @throws \Exception |
||
| 614 | */ |
||
| 615 | public function testLoginName($loginName) { |
||
| 616 | View Code Duplication | if(!$this->checkRequirements(array('ldapHost', |
|
| 617 | 'ldapPort', |
||
| 618 | 'ldapBase', |
||
| 619 | 'ldapLoginFilter', |
||
| 620 | ))) { |
||
| 621 | return false; |
||
| 622 | } |
||
| 623 | |||
| 624 | $cr = $this->access->connection->getConnectionResource(); |
||
| 625 | if(!$this->ldap->isResource($cr)) { |
||
| 626 | throw new \Exception('connection error'); |
||
| 627 | } |
||
| 628 | |||
| 629 | if(mb_strpos($this->access->connection->ldapLoginFilter, '%uid', 0, 'UTF-8') |
||
| 630 | === false) { |
||
| 631 | throw new \Exception('missing placeholder'); |
||
| 632 | } |
||
| 633 | |||
| 634 | $users = $this->access->countUsersByLoginName($loginName); |
||
| 635 | if($this->ldap->errno($cr) !== 0) { |
||
| 636 | throw new \Exception($this->ldap->error($cr)); |
||
| 637 | } |
||
| 638 | $filter = str_replace('%uid', $loginName, $this->access->connection->ldapLoginFilter); |
||
| 639 | $this->result->addChange('ldap_test_loginname', $users); |
||
| 640 | $this->result->addChange('ldap_test_effective_filter', $filter); |
||
| 641 | return $this->result; |
||
| 642 | } |
||
| 643 | |||
| 644 | /** |
||
| 645 | * Tries to determine the port, requires given Host, User DN and Password |
||
| 646 | * @return WizardResult|false WizardResult on success, false otherwise |
||
| 647 | * @throws \Exception |
||
| 648 | */ |
||
| 649 | public function guessPortAndTLS() { |
||
| 650 | if(!$this->checkRequirements(array('ldapHost', |
||
| 651 | ))) { |
||
| 652 | return false; |
||
| 653 | } |
||
| 654 | $this->checkHost(); |
||
| 655 | $portSettings = $this->getPortSettingsToTry(); |
||
| 656 | |||
| 657 | if(!is_array($portSettings)) { |
||
| 658 | throw new \Exception(print_r($portSettings, true)); |
||
| 659 | } |
||
| 660 | |||
| 661 | //proceed from the best configuration and return on first success |
||
| 662 | foreach($portSettings as $setting) { |
||
| 663 | $p = $setting['port']; |
||
| 664 | $t = $setting['tls']; |
||
| 665 | \OCP\Util::writeLog('user_ldap', 'Wiz: trying port '. $p . ', TLS '. $t, \OCP\Util::DEBUG); |
||
| 666 | //connectAndBind may throw Exception, it needs to be catched by the |
||
| 667 | //callee of this method |
||
| 668 | |||
| 669 | try { |
||
| 670 | $settingsFound = $this->connectAndBind($p, $t); |
||
| 671 | } catch (\Exception $e) { |
||
| 672 | // any reply other than -1 (= cannot connect) is already okay, |
||
| 673 | // because then we found the server |
||
| 674 | // unavailable startTLS returns -11 |
||
| 675 | if($e->getCode() > 0) { |
||
| 676 | $settingsFound = true; |
||
| 677 | } else { |
||
| 678 | throw $e; |
||
| 679 | } |
||
| 680 | } |
||
| 681 | |||
| 682 | if ($settingsFound === true) { |
||
| 683 | $config = array( |
||
| 684 | 'ldapPort' => $p, |
||
| 685 | 'ldapTLS' => intval($t) |
||
| 686 | ); |
||
| 687 | $this->configuration->setConfiguration($config); |
||
| 688 | \OCP\Util::writeLog('user_ldap', 'Wiz: detected Port ' . $p, \OCP\Util::DEBUG); |
||
| 689 | $this->result->addChange('ldap_port', $p); |
||
| 690 | return $this->result; |
||
| 691 | } |
||
| 692 | } |
||
| 693 | |||
| 694 | //custom port, undetected (we do not brute force) |
||
| 695 | return false; |
||
| 696 | } |
||
| 697 | |||
| 698 | /** |
||
| 699 | * tries to determine a base dn from User DN or LDAP Host |
||
| 700 | * @return WizardResult|false WizardResult on success, false otherwise |
||
| 701 | */ |
||
| 702 | public function guessBaseDN() { |
||
| 703 | if(!$this->checkRequirements(array('ldapHost', |
||
| 704 | 'ldapPort', |
||
| 705 | ))) { |
||
| 706 | return false; |
||
| 707 | } |
||
| 708 | |||
| 709 | //check whether a DN is given in the agent name (99.9% of all cases) |
||
| 710 | $base = null; |
||
| 711 | $i = stripos($this->configuration->ldapAgentName, 'dc='); |
||
| 712 | if($i !== false) { |
||
| 713 | $base = substr($this->configuration->ldapAgentName, $i); |
||
| 714 | if($this->testBaseDN($base)) { |
||
| 715 | $this->applyFind('ldap_base', $base); |
||
| 716 | return $this->result; |
||
| 717 | } |
||
| 718 | } |
||
| 719 | |||
| 720 | //this did not help :( |
||
| 721 | //Let's see whether we can parse the Host URL and convert the domain to |
||
| 722 | //a base DN |
||
| 723 | $helper = new Helper(); |
||
| 724 | $domain = $helper->getDomainFromURL($this->configuration->ldapHost); |
||
| 725 | if(!$domain) { |
||
| 726 | return false; |
||
| 727 | } |
||
| 728 | |||
| 729 | $dparts = explode('.', $domain); |
||
| 730 | while(count($dparts) > 0) { |
||
| 731 | $base2 = 'dc=' . implode(',dc=', $dparts); |
||
| 732 | if ($base !== $base2 && $this->testBaseDN($base2)) { |
||
| 733 | $this->applyFind('ldap_base', $base2); |
||
| 734 | return $this->result; |
||
| 735 | } |
||
| 736 | array_shift($dparts); |
||
| 737 | } |
||
| 738 | |||
| 739 | return false; |
||
| 740 | } |
||
| 741 | |||
| 742 | /** |
||
| 743 | * sets the found value for the configuration key in the WizardResult |
||
| 744 | * as well as in the Configuration instance |
||
| 745 | * @param string $key the configuration key |
||
| 746 | * @param string $value the (detected) value |
||
| 747 | * |
||
| 748 | */ |
||
| 749 | 2 | private function applyFind($key, $value) { |
|
| 753 | |||
| 754 | /** |
||
| 755 | * Checks, whether a port was entered in the Host configuration |
||
| 756 | * field. In this case the port will be stripped off, but also stored as |
||
| 757 | * setting. |
||
| 758 | */ |
||
| 759 | private function checkHost() { |
||
| 760 | $host = $this->configuration->ldapHost; |
||
| 761 | $hostInfo = parse_url($host); |
||
| 762 | |||
| 763 | //removes Port from Host |
||
| 764 | if(is_array($hostInfo) && isset($hostInfo['port'])) { |
||
| 765 | $port = $hostInfo['port']; |
||
| 766 | $host = str_replace(':'.$port, '', $host); |
||
| 767 | $this->applyFind('ldap_host', $host); |
||
| 768 | $this->applyFind('ldap_port', $port); |
||
| 769 | } |
||
| 770 | } |
||
| 771 | |||
| 772 | /** |
||
| 773 | * tries to detect the group member association attribute which is |
||
| 774 | * one of 'uniqueMember', 'memberUid', 'member' |
||
| 775 | * @return string|false, string with the attribute name, false on error |
||
| 776 | * @throws \Exception |
||
| 777 | */ |
||
| 778 | private function detectGroupMemberAssoc() { |
||
| 779 | $possibleAttrs = array('uniqueMember', 'memberUid', 'member'); |
||
| 780 | $filter = $this->configuration->ldapGroupFilter; |
||
| 781 | if(empty($filter)) { |
||
| 782 | return false; |
||
| 783 | } |
||
| 784 | $cr = $this->getConnection(); |
||
| 785 | if(!$cr) { |
||
| 786 | throw new \Exception('Could not connect to LDAP'); |
||
| 787 | } |
||
| 788 | $base = $this->configuration->ldapBase[0]; |
||
| 789 | $rr = $this->ldap->search($cr, $base, $filter, $possibleAttrs, 0, 1000); |
||
| 790 | if(!$this->ldap->isResource($rr)) { |
||
| 791 | return false; |
||
| 792 | } |
||
| 793 | $er = $this->ldap->firstEntry($cr, $rr); |
||
| 794 | while(is_resource($er)) { |
||
| 795 | $this->ldap->getDN($cr, $er); |
||
| 796 | $attrs = $this->ldap->getAttributes($cr, $er); |
||
| 797 | $result = array(); |
||
| 798 | $possibleAttrsCount = count($possibleAttrs); |
||
| 799 | for($i = 0; $i < $possibleAttrsCount; $i++) { |
||
| 800 | if(isset($attrs[$possibleAttrs[$i]])) { |
||
| 801 | $result[$possibleAttrs[$i]] = $attrs[$possibleAttrs[$i]]['count']; |
||
| 802 | } |
||
| 803 | } |
||
| 804 | if(!empty($result)) { |
||
| 805 | natsort($result); |
||
| 806 | return key($result); |
||
| 807 | } |
||
| 808 | |||
| 809 | $er = $this->ldap->nextEntry($cr, $er); |
||
| 810 | } |
||
| 811 | |||
| 812 | return false; |
||
| 813 | } |
||
| 814 | |||
| 815 | /** |
||
| 816 | * Checks whether for a given BaseDN results will be returned |
||
| 817 | * @param string $base the BaseDN to test |
||
| 818 | * @return bool true on success, false otherwise |
||
| 819 | * @throws \Exception |
||
| 820 | */ |
||
| 821 | private function testBaseDN($base) { |
||
| 840 | |||
| 841 | /** |
||
| 842 | * Checks whether the server supports memberOf in LDAP Filter. |
||
| 843 | * Note: at least in OpenLDAP, availability of memberOf is dependent on |
||
| 844 | * a configured objectClass. I.e. not necessarily for all available groups |
||
| 845 | * memberOf does work. |
||
| 846 | * |
||
| 847 | * @return bool true if it does, false otherwise |
||
| 848 | * @throws \Exception |
||
| 849 | */ |
||
| 850 | private function testMemberOf() { |
||
| 861 | |||
| 862 | /** |
||
| 863 | * creates an LDAP Filter from given configuration |
||
| 864 | * @param integer $filterType int, for which use case the filter shall be created |
||
| 865 | * can be any of self::LFILTER_USER_LIST, self::LFILTER_LOGIN or |
||
| 866 | * self::LFILTER_GROUP_LIST |
||
| 867 | * @return string|false string with the filter on success, false otherwise |
||
| 868 | * @throws \Exception |
||
| 869 | */ |
||
| 870 | private function composeLdapFilter($filterType) { |
||
| 871 | $filter = ''; |
||
| 872 | $parts = 0; |
||
| 873 | switch ($filterType) { |
||
| 874 | case self::LFILTER_USER_LIST: |
||
| 875 | $objcs = $this->configuration->ldapUserFilterObjectclass; |
||
| 876 | //glue objectclasses |
||
| 877 | View Code Duplication | if(is_array($objcs) && count($objcs) > 0) { |
|
| 878 | $filter .= '(|'; |
||
| 879 | foreach($objcs as $objc) { |
||
| 880 | $filter .= '(objectclass=' . $objc . ')'; |
||
| 881 | } |
||
| 882 | $filter .= ')'; |
||
| 883 | $parts++; |
||
| 884 | } |
||
| 885 | //glue group memberships |
||
| 886 | if($this->configuration->hasMemberOfFilterSupport) { |
||
| 887 | $cns = $this->configuration->ldapUserFilterGroups; |
||
| 888 | if(is_array($cns) && count($cns) > 0) { |
||
| 889 | $filter .= '(|'; |
||
| 890 | $cr = $this->getConnection(); |
||
| 891 | if(!$cr) { |
||
| 892 | throw new \Exception('Could not connect to LDAP'); |
||
| 893 | } |
||
| 894 | $base = $this->configuration->ldapBase[0]; |
||
| 895 | foreach($cns as $cn) { |
||
| 896 | $rr = $this->ldap->search($cr, $base, 'cn=' . $cn, array('dn', 'primaryGroupToken')); |
||
| 897 | if(!$this->ldap->isResource($rr)) { |
||
| 898 | continue; |
||
| 899 | } |
||
| 900 | $er = $this->ldap->firstEntry($cr, $rr); |
||
| 901 | $attrs = $this->ldap->getAttributes($cr, $er); |
||
| 902 | $dn = $this->ldap->getDN($cr, $er); |
||
| 903 | if(empty($dn)) { |
||
| 904 | continue; |
||
| 905 | } |
||
| 906 | $filterPart = '(memberof=' . $dn . ')'; |
||
| 907 | if(isset($attrs['primaryGroupToken'])) { |
||
| 908 | $pgt = $attrs['primaryGroupToken'][0]; |
||
| 909 | $primaryFilterPart = '(primaryGroupID=' . $pgt .')'; |
||
| 910 | $filterPart = '(|' . $filterPart . $primaryFilterPart . ')'; |
||
| 911 | } |
||
| 912 | $filter .= $filterPart; |
||
| 913 | } |
||
| 914 | $filter .= ')'; |
||
| 915 | } |
||
| 916 | $parts++; |
||
| 917 | } |
||
| 918 | //wrap parts in AND condition |
||
| 919 | if($parts > 1) { |
||
| 920 | $filter = '(&' . $filter . ')'; |
||
| 921 | } |
||
| 922 | if(empty($filter)) { |
||
| 923 | $filter = '(objectclass=*)'; |
||
| 924 | } |
||
| 925 | break; |
||
| 926 | |||
| 927 | case self::LFILTER_GROUP_LIST: |
||
| 928 | $objcs = $this->configuration->ldapGroupFilterObjectclass; |
||
| 929 | //glue objectclasses |
||
| 930 | View Code Duplication | if(is_array($objcs) && count($objcs) > 0) { |
|
| 931 | $filter .= '(|'; |
||
| 932 | foreach($objcs as $objc) { |
||
| 933 | $filter .= '(objectclass=' . $objc . ')'; |
||
| 934 | } |
||
| 935 | $filter .= ')'; |
||
| 936 | $parts++; |
||
| 937 | } |
||
| 938 | //glue group memberships |
||
| 939 | $cns = $this->configuration->ldapGroupFilterGroups; |
||
| 940 | if(is_array($cns) && count($cns) > 0) { |
||
| 941 | $filter .= '(|'; |
||
| 942 | $base = $this->configuration->ldapBase[0]; |
||
| 943 | foreach($cns as $cn) { |
||
| 944 | $filter .= '(cn=' . $cn . ')'; |
||
| 945 | } |
||
| 946 | $filter .= ')'; |
||
| 947 | } |
||
| 948 | $parts++; |
||
| 949 | //wrap parts in AND condition |
||
| 950 | if($parts > 1) { |
||
| 951 | $filter = '(&' . $filter . ')'; |
||
| 952 | } |
||
| 953 | break; |
||
| 954 | |||
| 955 | case self::LFILTER_LOGIN: |
||
| 956 | $ulf = $this->configuration->ldapUserFilter; |
||
| 957 | $loginpart = '=%uid'; |
||
| 958 | $filterUsername = ''; |
||
| 959 | $userAttributes = $this->getUserAttributes(); |
||
| 960 | $userAttributes = array_change_key_case(array_flip($userAttributes)); |
||
| 961 | $parts = 0; |
||
| 962 | |||
| 963 | if($this->configuration->ldapLoginFilterUsername === '1') { |
||
| 964 | $attr = ''; |
||
| 965 | if(isset($userAttributes['uid'])) { |
||
| 966 | $attr = 'uid'; |
||
| 967 | } else if(isset($userAttributes['samaccountname'])) { |
||
| 968 | $attr = 'samaccountname'; |
||
| 969 | } else if(isset($userAttributes['cn'])) { |
||
| 970 | //fallback |
||
| 971 | $attr = 'cn'; |
||
| 972 | } |
||
| 973 | if(!empty($attr)) { |
||
| 974 | $filterUsername = '(' . $attr . $loginpart . ')'; |
||
| 975 | $parts++; |
||
| 976 | } |
||
| 977 | } |
||
| 978 | |||
| 979 | $filterEmail = ''; |
||
| 980 | if($this->configuration->ldapLoginFilterEmail === '1') { |
||
| 981 | $filterEmail = '(|(mailPrimaryAddress=%uid)(mail=%uid))'; |
||
| 982 | $parts++; |
||
| 983 | } |
||
| 984 | |||
| 985 | $filterAttributes = ''; |
||
| 986 | $attrsToFilter = $this->configuration->ldapLoginFilterAttributes; |
||
| 987 | if(is_array($attrsToFilter) && count($attrsToFilter) > 0) { |
||
| 988 | $filterAttributes = '(|'; |
||
| 989 | foreach($attrsToFilter as $attribute) { |
||
| 990 | $filterAttributes .= '(' . $attribute . $loginpart . ')'; |
||
| 991 | } |
||
| 992 | $filterAttributes .= ')'; |
||
| 993 | $parts++; |
||
| 994 | } |
||
| 995 | |||
| 996 | $filterLogin = ''; |
||
| 997 | if($parts > 1) { |
||
| 998 | $filterLogin = '(|'; |
||
| 999 | } |
||
| 1000 | $filterLogin .= $filterUsername; |
||
| 1001 | $filterLogin .= $filterEmail; |
||
| 1002 | $filterLogin .= $filterAttributes; |
||
| 1003 | if($parts > 1) { |
||
| 1004 | $filterLogin .= ')'; |
||
| 1005 | } |
||
| 1006 | |||
| 1007 | $filter = '(&'.$ulf.$filterLogin.')'; |
||
| 1008 | break; |
||
| 1009 | } |
||
| 1010 | |||
| 1011 | \OCP\Util::writeLog('user_ldap', 'Wiz: Final filter '.$filter, \OCP\Util::DEBUG); |
||
| 1012 | |||
| 1013 | return $filter; |
||
| 1014 | } |
||
| 1015 | |||
| 1016 | /** |
||
| 1017 | * Connects and Binds to an LDAP Server |
||
| 1018 | * @param int $port the port to connect with |
||
| 1019 | * @param bool $tls whether startTLS is to be used |
||
| 1020 | * @param bool $ncc |
||
| 1021 | * @return bool |
||
| 1022 | * @throws \Exception |
||
| 1023 | */ |
||
| 1024 | private function connectAndBind($port = 389, $tls = false, $ncc = false) { |
||
| 1025 | if($ncc) { |
||
| 1026 | //No certificate check |
||
| 1027 | //FIXME: undo afterwards |
||
| 1028 | putenv('LDAPTLS_REQCERT=never'); |
||
| 1029 | } |
||
| 1030 | |||
| 1031 | //connect, does not really trigger any server communication |
||
| 1032 | \OCP\Util::writeLog('user_ldap', 'Wiz: Checking Host Info ', \OCP\Util::DEBUG); |
||
| 1033 | $host = $this->configuration->ldapHost; |
||
| 1034 | $hostInfo = parse_url($host); |
||
| 1035 | if(!$hostInfo) { |
||
| 1036 | throw new \Exception($this->l->t('Invalid Host')); |
||
| 1037 | } |
||
| 1038 | \OCP\Util::writeLog('user_ldap', 'Wiz: Attempting to connect ', \OCP\Util::DEBUG); |
||
| 1039 | $cr = $this->ldap->connect($host, $port); |
||
| 1040 | if(!is_resource($cr)) { |
||
| 1041 | throw new \Exception($this->l->t('Invalid Host')); |
||
| 1042 | } |
||
| 1043 | |||
| 1044 | \OCP\Util::writeLog('user_ldap', 'Wiz: Setting LDAP Options ', \OCP\Util::DEBUG); |
||
| 1045 | //set LDAP options |
||
| 1046 | $this->ldap->setOption($cr, LDAP_OPT_PROTOCOL_VERSION, 3); |
||
| 1047 | $this->ldap->setOption($cr, LDAP_OPT_REFERRALS, 0); |
||
| 1048 | $this->ldap->setOption($cr, LDAP_OPT_NETWORK_TIMEOUT, self::LDAP_NW_TIMEOUT); |
||
| 1049 | |||
| 1050 | try { |
||
| 1051 | if($tls) { |
||
| 1052 | $isTlsWorking = @$this->ldap->startTls($cr); |
||
| 1053 | if(!$isTlsWorking) { |
||
| 1054 | return false; |
||
| 1055 | } |
||
| 1056 | } |
||
| 1057 | |||
| 1058 | \OCP\Util::writeLog('user_ldap', 'Wiz: Attemping to Bind ', \OCP\Util::DEBUG); |
||
| 1059 | //interesting part: do the bind! |
||
| 1060 | $login = $this->ldap->bind($cr, |
||
| 1061 | $this->configuration->ldapAgentName, |
||
| 1062 | $this->configuration->ldapAgentPassword |
||
| 1063 | ); |
||
| 1064 | $errNo = $this->ldap->errno($cr); |
||
| 1065 | $error = ldap_error($cr); |
||
| 1066 | $this->ldap->unbind($cr); |
||
| 1067 | } catch(ServerNotAvailableException $e) { |
||
| 1068 | return false; |
||
| 1069 | } |
||
| 1070 | |||
| 1071 | if($login === true) { |
||
| 1072 | $this->ldap->unbind($cr); |
||
| 1073 | if($ncc) { |
||
| 1074 | throw new \Exception('Certificate cannot be validated.'); |
||
| 1075 | } |
||
| 1076 | \OCP\Util::writeLog('user_ldap', 'Wiz: Bind successful to Port '. $port . ' TLS ' . intval($tls), \OCP\Util::DEBUG); |
||
| 1077 | return true; |
||
| 1078 | } |
||
| 1079 | |||
| 1080 | if($errNo === -1 || ($errNo === 2 && $ncc)) { |
||
| 1081 | //host, port or TLS wrong |
||
| 1082 | return false; |
||
| 1083 | } else if ($errNo === 2) { |
||
| 1084 | return $this->connectAndBind($port, $tls, true); |
||
| 1085 | } |
||
| 1086 | throw new \Exception($error, $errNo); |
||
| 1087 | } |
||
| 1088 | |||
| 1089 | /** |
||
| 1090 | * checks whether a valid combination of agent and password has been |
||
| 1091 | * provided (either two values or nothing for anonymous connect) |
||
| 1092 | * @return bool, true if everything is fine, false otherwise |
||
| 1093 | */ |
||
| 1094 | 4 | private function checkAgentRequirements() { |
|
| 1101 | |||
| 1102 | /** |
||
| 1103 | * @param array $reqs |
||
| 1104 | * @return bool |
||
| 1105 | */ |
||
| 1106 | 4 | private function checkRequirements($reqs) { |
|
| 1107 | 4 | $this->checkAgentRequirements(); |
|
| 1108 | 4 | foreach($reqs as $option) { |
|
| 1109 | 4 | $value = $this->configuration->$option; |
|
| 1110 | 4 | if(empty($value)) { |
|
| 1111 | return false; |
||
| 1112 | } |
||
| 1113 | 4 | } |
|
| 1114 | 4 | return true; |
|
| 1115 | } |
||
| 1116 | |||
| 1117 | /** |
||
| 1118 | * does a cumulativeSearch on LDAP to get different values of a |
||
| 1119 | * specified attribute |
||
| 1120 | * @param string[] $filters array, the filters that shall be used in the search |
||
| 1121 | * @param string $attr the attribute of which a list of values shall be returned |
||
| 1122 | * @param int $dnReadLimit the amount of how many DNs should be analyzed. |
||
| 1123 | * The lower, the faster |
||
| 1124 | * @param string $maxF string. if not null, this variable will have the filter that |
||
| 1125 | * yields most result entries |
||
| 1126 | * @return array|false an array with the values on success, false otherwise |
||
| 1127 | */ |
||
| 1128 | 3 | public function cumulativeSearchOnAttribute($filters, $attr, $dnReadLimit = 3, &$maxF = null) { |
|
| 1129 | 3 | $dnRead = array(); |
|
| 1191 | |||
| 1192 | /** |
||
| 1193 | * determines if and which $attr are available on the LDAP server |
||
| 1194 | * @param string[] $objectclasses the objectclasses to use as search filter |
||
| 1195 | * @param string $attr the attribute to look for |
||
| 1196 | * @param string $dbkey the dbkey of the setting the feature is connected to |
||
| 1197 | * @param string $confkey the confkey counterpart for the $dbkey as used in the |
||
| 1198 | * Configuration class |
||
| 1199 | * @param bool $po whether the objectClass with most result entries |
||
| 1200 | * shall be pre-selected via the result |
||
| 1201 | * @return array|false list of found items. |
||
| 1202 | * @throws \Exception |
||
| 1203 | */ |
||
| 1204 | private function determineFeature($objectclasses, $attr, $dbkey, $confkey, $po = false) { |
||
| 1245 | |||
| 1246 | /** |
||
| 1247 | * appends a list of values fr |
||
| 1248 | * @param resource $result the return value from ldap_get_attributes |
||
| 1249 | * @param string $attribute the attribute values to look for |
||
| 1250 | * @param array &$known new values will be appended here |
||
| 1251 | * @return int, state on of the class constants LRESULT_PROCESSED_OK, |
||
| 1252 | * LRESULT_PROCESSED_INVALID or LRESULT_PROCESSED_SKIP |
||
| 1253 | */ |
||
| 1254 | 3 | private function getAttributeValuesFromEntry($result, $attribute, &$known) { |
|
| 1278 | |||
| 1279 | /** |
||
| 1280 | * @return bool|mixed |
||
| 1281 | */ |
||
| 1282 | 3 | private function getConnection() { |
|
| 1283 | 3 | if(!is_null($this->cr)) { |
|
| 1284 | return $this->cr; |
||
| 1285 | } |
||
| 1286 | |||
| 1287 | 3 | $cr = $this->ldap->connect( |
|
| 1288 | 3 | $this->configuration->ldapHost, |
|
| 1289 | 3 | $this->configuration->ldapPort |
|
| 1290 | 3 | ); |
|
| 1291 | |||
| 1292 | 3 | $this->ldap->setOption($cr, LDAP_OPT_PROTOCOL_VERSION, 3); |
|
| 1293 | 3 | $this->ldap->setOption($cr, LDAP_OPT_REFERRALS, 0); |
|
| 1294 | 3 | $this->ldap->setOption($cr, LDAP_OPT_NETWORK_TIMEOUT, self::LDAP_NW_TIMEOUT); |
|
| 1295 | 3 | if($this->configuration->ldapTLS === 1) { |
|
| 1296 | $this->ldap->startTls($cr); |
||
| 1297 | } |
||
| 1298 | |||
| 1299 | 3 | $lo = @$this->ldap->bind($cr, |
|
| 1300 | 3 | $this->configuration->ldapAgentName, |
|
| 1301 | 3 | $this->configuration->ldapAgentPassword); |
|
| 1302 | 3 | if($lo === true) { |
|
| 1303 | 3 | $this->$cr = $cr; |
|
| 1304 | 3 | return $cr; |
|
| 1305 | } |
||
| 1306 | |||
| 1307 | return false; |
||
| 1308 | } |
||
| 1309 | |||
| 1310 | /** |
||
| 1311 | * @return array |
||
| 1312 | */ |
||
| 1313 | private function getDefaultLdapPortSettings() { |
||
| 1324 | |||
| 1325 | /** |
||
| 1326 | * @return array |
||
| 1327 | */ |
||
| 1328 | private function getPortSettingsToTry() { |
||
| 1353 | |||
| 1354 | |||
| 1355 | } |
||
| 1356 |
Since your code implements the magic setter
_set, this function will be called for any write access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.