| Total Complexity | 121 |
| Total Lines | 655 |
| Duplicated Lines | 0 % |
| Changes | 5 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Connection 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.
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 Connection, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 77 | class Connection extends LDAPUtility { |
||
| 78 | /** |
||
| 79 | * @var resource|\LDAP\Connection|null |
||
|
|
|||
| 80 | */ |
||
| 81 | private $ldapConnectionRes = null; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var string |
||
| 85 | */ |
||
| 86 | private $configPrefix; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var ?string |
||
| 90 | */ |
||
| 91 | private $configID; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @var bool |
||
| 95 | */ |
||
| 96 | private $configured = false; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @var bool whether connection should be kept on __destruct |
||
| 100 | */ |
||
| 101 | private $dontDestruct = false; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @var bool runtime flag that indicates whether supported primary groups are available |
||
| 105 | */ |
||
| 106 | public $hasPrimaryGroups = true; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @var bool runtime flag that indicates whether supported POSIX gidNumber are available |
||
| 110 | */ |
||
| 111 | public $hasGidNumber = true; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @var \OCP\ICache|null |
||
| 115 | */ |
||
| 116 | protected $cache = null; |
||
| 117 | |||
| 118 | /** @var Configuration settings handler **/ |
||
| 119 | protected $configuration; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @var bool |
||
| 123 | */ |
||
| 124 | protected $doNotValidate = false; |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @var bool |
||
| 128 | */ |
||
| 129 | protected $ignoreValidation = false; |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @var array{sum?: string, result?: bool} |
||
| 133 | */ |
||
| 134 | protected $bindResult = []; |
||
| 135 | |||
| 136 | /** @var LoggerInterface */ |
||
| 137 | protected $logger; |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Constructor |
||
| 141 | * @param string $configPrefix a string with the prefix for the configkey column (appconfig table) |
||
| 142 | * @param string|null $configID a string with the value for the appid column (appconfig table) or null for on-the-fly connections |
||
| 143 | */ |
||
| 144 | public function __construct(ILDAPWrapper $ldap, string $configPrefix = '', ?string $configID = 'user_ldap') { |
||
| 145 | parent::__construct($ldap); |
||
| 146 | $this->configPrefix = $configPrefix; |
||
| 147 | $this->configID = $configID; |
||
| 148 | $this->configuration = new Configuration($configPrefix, !is_null($configID)); |
||
| 149 | $memcache = \OC::$server->getMemCacheFactory(); |
||
| 150 | if ($memcache->isAvailable()) { |
||
| 151 | $this->cache = $memcache->createDistributed(); |
||
| 152 | } |
||
| 153 | $helper = new Helper(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection()); |
||
| 154 | $this->doNotValidate = !in_array($this->configPrefix, |
||
| 155 | $helper->getServerConfigurationPrefixes()); |
||
| 156 | $this->logger = \OC::$server->get(LoggerInterface::class); |
||
| 157 | } |
||
| 158 | |||
| 159 | public function __destruct() { |
||
| 160 | if (!$this->dontDestruct && $this->ldap->isResource($this->ldapConnectionRes)) { |
||
| 161 | @$this->ldap->unbind($this->ldapConnectionRes); |
||
| 162 | $this->bindResult = []; |
||
| 163 | } |
||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * defines behaviour when the instance is cloned |
||
| 168 | */ |
||
| 169 | public function __clone() { |
||
| 170 | $this->configuration = new Configuration($this->configPrefix, |
||
| 171 | !is_null($this->configID)); |
||
| 172 | if (count($this->bindResult) !== 0 && $this->bindResult['result'] === true) { |
||
| 173 | $this->bindResult = []; |
||
| 174 | } |
||
| 175 | $this->ldapConnectionRes = null; |
||
| 176 | $this->dontDestruct = true; |
||
| 177 | } |
||
| 178 | |||
| 179 | public function __get(string $name) { |
||
| 180 | if (!$this->configured) { |
||
| 181 | $this->readConfiguration(); |
||
| 182 | } |
||
| 183 | |||
| 184 | return $this->configuration->$name; |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @param string $name |
||
| 189 | * @param mixed $value |
||
| 190 | */ |
||
| 191 | public function __set($name, $value) { |
||
| 192 | $this->doNotValidate = false; |
||
| 193 | $before = $this->configuration->$name; |
||
| 194 | $this->configuration->$name = $value; |
||
| 195 | $after = $this->configuration->$name; |
||
| 196 | if ($before !== $after) { |
||
| 197 | if ($this->configID !== '' && $this->configID !== null) { |
||
| 198 | $this->configuration->saveConfiguration(); |
||
| 199 | } |
||
| 200 | $this->validateConfiguration(); |
||
| 201 | } |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @param string $rule |
||
| 206 | * @return array |
||
| 207 | * @throws \RuntimeException |
||
| 208 | */ |
||
| 209 | public function resolveRule($rule) { |
||
| 210 | return $this->configuration->resolveRule($rule); |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * sets whether the result of the configuration validation shall |
||
| 215 | * be ignored when establishing the connection. Used by the Wizard |
||
| 216 | * in early configuration state. |
||
| 217 | * @param bool $state |
||
| 218 | */ |
||
| 219 | public function setIgnoreValidation($state) { |
||
| 220 | $this->ignoreValidation = (bool)$state; |
||
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * initializes the LDAP backend |
||
| 225 | * @param bool $force read the config settings no matter what |
||
| 226 | */ |
||
| 227 | public function init($force = false) { |
||
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * @return resource|\LDAP\Connection The LDAP resource |
||
| 234 | */ |
||
| 235 | public function getConnectionResource() { |
||
| 236 | if (!$this->ldapConnectionRes) { |
||
| 237 | $this->init(); |
||
| 238 | } elseif (!$this->ldap->isResource($this->ldapConnectionRes)) { |
||
| 239 | $this->ldapConnectionRes = null; |
||
| 240 | $this->establishConnection(); |
||
| 241 | } |
||
| 242 | if (is_null($this->ldapConnectionRes)) { |
||
| 243 | $this->logger->error( |
||
| 244 | 'No LDAP Connection to server ' . $this->configuration->ldapHost, |
||
| 245 | ['app' => 'user_ldap'] |
||
| 246 | ); |
||
| 247 | throw new ServerNotAvailableException('Connection to LDAP server could not be established'); |
||
| 248 | } |
||
| 249 | return $this->ldapConnectionRes; |
||
| 250 | } |
||
| 251 | |||
| 252 | /** |
||
| 253 | * resets the connection resource |
||
| 254 | */ |
||
| 255 | public function resetConnectionResource() { |
||
| 256 | if (!is_null($this->ldapConnectionRes)) { |
||
| 257 | @$this->ldap->unbind($this->ldapConnectionRes); |
||
| 258 | $this->ldapConnectionRes = null; |
||
| 259 | $this->bindResult = []; |
||
| 260 | } |
||
| 261 | } |
||
| 262 | |||
| 263 | /** |
||
| 264 | * @param string|null $key |
||
| 265 | * @return string |
||
| 266 | */ |
||
| 267 | private function getCacheKey($key) { |
||
| 268 | $prefix = 'LDAP-'.$this->configID.'-'.$this->configPrefix.'-'; |
||
| 269 | if (is_null($key)) { |
||
| 270 | return $prefix; |
||
| 271 | } |
||
| 272 | return $prefix.hash('sha256', $key); |
||
| 273 | } |
||
| 274 | |||
| 275 | /** |
||
| 276 | * @param string $key |
||
| 277 | * @return mixed|null |
||
| 278 | */ |
||
| 279 | public function getFromCache($key) { |
||
| 280 | if (!$this->configured) { |
||
| 281 | $this->readConfiguration(); |
||
| 282 | } |
||
| 283 | if (is_null($this->cache) || !$this->configuration->ldapCacheTTL) { |
||
| 284 | return null; |
||
| 285 | } |
||
| 286 | $key = $this->getCacheKey($key); |
||
| 287 | |||
| 288 | return json_decode(base64_decode($this->cache->get($key) ?? ''), true); |
||
| 289 | } |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @param string $key |
||
| 293 | * @param mixed $value |
||
| 294 | */ |
||
| 295 | public function writeToCache($key, $value): void { |
||
| 296 | if (!$this->configured) { |
||
| 297 | $this->readConfiguration(); |
||
| 298 | } |
||
| 299 | if (is_null($this->cache) |
||
| 300 | || !$this->configuration->ldapCacheTTL |
||
| 301 | || !$this->configuration->ldapConfigurationActive) { |
||
| 302 | return; |
||
| 303 | } |
||
| 304 | $key = $this->getCacheKey($key); |
||
| 305 | $value = base64_encode(json_encode($value)); |
||
| 306 | $this->cache->set($key, $value, $this->configuration->ldapCacheTTL); |
||
| 307 | } |
||
| 308 | |||
| 309 | public function clearCache() { |
||
| 310 | if (!is_null($this->cache)) { |
||
| 311 | $this->cache->clear($this->getCacheKey(null)); |
||
| 312 | } |
||
| 313 | } |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Caches the general LDAP configuration. |
||
| 317 | * @param bool $force optional. true, if the re-read should be forced. defaults |
||
| 318 | * to false. |
||
| 319 | * @return null |
||
| 320 | */ |
||
| 321 | private function readConfiguration($force = false) { |
||
| 325 | } |
||
| 326 | } |
||
| 327 | |||
| 328 | /** |
||
| 329 | * set LDAP configuration with values delivered by an array, not read from configuration |
||
| 330 | * @param array $config array that holds the config parameters in an associated array |
||
| 331 | * @param array &$setParameters optional; array where the set fields will be given to |
||
| 332 | * @return bool true if config validates, false otherwise. Check with $setParameters for detailed success on single parameters |
||
| 333 | */ |
||
| 334 | public function setConfiguration($config, &$setParameters = null): bool { |
||
| 335 | if (is_null($setParameters)) { |
||
| 336 | $setParameters = []; |
||
| 337 | } |
||
| 338 | $this->doNotValidate = false; |
||
| 339 | $this->configuration->setConfiguration($config, $setParameters); |
||
| 340 | if (count($setParameters) > 0) { |
||
| 341 | $this->configured = $this->validateConfiguration(); |
||
| 342 | } |
||
| 343 | |||
| 344 | |||
| 345 | return $this->configured; |
||
| 346 | } |
||
| 347 | |||
| 348 | /** |
||
| 349 | * saves the current Configuration in the database and empties the |
||
| 350 | * cache |
||
| 351 | * @return null |
||
| 352 | */ |
||
| 353 | public function saveConfiguration() { |
||
| 354 | $this->configuration->saveConfiguration(); |
||
| 355 | $this->clearCache(); |
||
| 356 | } |
||
| 357 | |||
| 358 | /** |
||
| 359 | * get the current LDAP configuration |
||
| 360 | * @return array |
||
| 361 | */ |
||
| 362 | public function getConfiguration() { |
||
| 391 | } |
||
| 392 | |||
| 393 | private function doSoftValidation() { |
||
| 394 | //if User or Group Base are not set, take over Base DN setting |
||
| 395 | foreach (['ldapBaseUsers', 'ldapBaseGroups'] as $keyBase) { |
||
| 396 | $val = $this->configuration->$keyBase; |
||
| 397 | if (empty($val)) { |
||
| 398 | $this->configuration->$keyBase = $this->configuration->ldapBase; |
||
| 399 | } |
||
| 400 | } |
||
| 401 | |||
| 402 | foreach (['ldapExpertUUIDUserAttr' => 'ldapUuidUserAttribute', |
||
| 403 | 'ldapExpertUUIDGroupAttr' => 'ldapUuidGroupAttribute'] |
||
| 404 | as $expertSetting => $effectiveSetting) { |
||
| 405 | $uuidOverride = $this->configuration->$expertSetting; |
||
| 406 | if (!empty($uuidOverride)) { |
||
| 407 | $this->configuration->$effectiveSetting = $uuidOverride; |
||
| 408 | } else { |
||
| 409 | $uuidAttributes = Access::UUID_ATTRIBUTES; |
||
| 410 | array_unshift($uuidAttributes, 'auto'); |
||
| 411 | if (!in_array($this->configuration->$effectiveSetting, $uuidAttributes) |
||
| 412 | && !is_null($this->configID)) { |
||
| 413 | $this->configuration->$effectiveSetting = 'auto'; |
||
| 414 | $this->configuration->saveConfiguration(); |
||
| 415 | $this->logger->info( |
||
| 416 | 'Illegal value for the '.$effectiveSetting.', reset to autodetect.', |
||
| 417 | ['app' => 'user_ldap'] |
||
| 418 | ); |
||
| 419 | } |
||
| 420 | } |
||
| 421 | } |
||
| 422 | |||
| 423 | $backupPort = (int)$this->configuration->ldapBackupPort; |
||
| 424 | if ($backupPort <= 0) { |
||
| 425 | $this->configuration->backupPort = $this->configuration->ldapPort; |
||
| 426 | } |
||
| 427 | |||
| 428 | //make sure empty search attributes are saved as simple, empty array |
||
| 429 | $saKeys = ['ldapAttributesForUserSearch', |
||
| 430 | 'ldapAttributesForGroupSearch']; |
||
| 431 | foreach ($saKeys as $key) { |
||
| 432 | $val = $this->configuration->$key; |
||
| 433 | if (is_array($val) && count($val) === 1 && empty($val[0])) { |
||
| 434 | $this->configuration->$key = []; |
||
| 435 | } |
||
| 436 | } |
||
| 437 | |||
| 438 | if ((stripos((string)$this->configuration->ldapHost, 'ldaps://') === 0) |
||
| 439 | && $this->configuration->ldapTLS) { |
||
| 440 | $this->configuration->ldapTLS = false; |
||
| 441 | $this->logger->info( |
||
| 442 | 'LDAPS (already using secure connection) and TLS do not work together. Switched off TLS.', |
||
| 443 | ['app' => 'user_ldap'] |
||
| 444 | ); |
||
| 445 | } |
||
| 446 | } |
||
| 447 | |||
| 448 | /** |
||
| 449 | * @return bool |
||
| 450 | */ |
||
| 451 | private function doCriticalValidation() { |
||
| 533 | } |
||
| 534 | |||
| 535 | /** |
||
| 536 | * Validates the user specified configuration |
||
| 537 | * @return bool true if configuration seems OK, false otherwise |
||
| 538 | */ |
||
| 539 | private function validateConfiguration() { |
||
| 540 | if ($this->doNotValidate) { |
||
| 541 | //don't do a validation if it is a new configuration with pure |
||
| 542 | //default values. Will be allowed on changes via __set or |
||
| 554 | } |
||
| 555 | |||
| 556 | |||
| 557 | /** |
||
| 558 | * Connects and Binds to LDAP |
||
| 559 | * |
||
| 560 | * @throws ServerNotAvailableException |
||
| 561 | */ |
||
| 562 | private function establishConnection() { |
||
| 645 | } |
||
| 646 | |||
| 647 | /** |
||
| 648 | * @param string $host |
||
| 649 | * @param string $port |
||
| 650 | * @return bool |
||
| 651 | * @throws \OC\ServerNotAvailableException |
||
| 652 | */ |
||
| 653 | private function doConnect($host, $port) { |
||
| 679 | } |
||
| 680 | |||
| 681 | /** |
||
| 682 | * Binds to LDAP |
||
| 683 | */ |
||
| 684 | public function bind() { |
||
| 734 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths