| Conditions | 8 |
| Paths | 12 |
| Total Lines | 87 |
| Code Lines | 53 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 1 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 77 | protected function loginSasl( |
||
| 78 | string $username, |
||
| 79 | #[\SensitiveParameter] |
||
| 80 | string $password, |
||
| 81 | array $sasl_args = [], |
||
| 82 | ): array { |
||
| 83 | if (preg_match('/^\s*$/', $password)) { |
||
| 84 | // The empty string is considered an anonymous bind to Symfony |
||
| 85 | throw new Error\Error('WRONGUSERPASS'); |
||
| 86 | } |
||
| 87 | |||
| 88 | $searchScope = $this->ldapConfig->getOptionalString('search.scope', Query::SCOPE_SUB); |
||
| 89 | Assert::oneOf($searchScope, [Query::SCOPE_BASE, Query::SCOPE_ONE, Query::SCOPE_SUB]); |
||
| 90 | |||
| 91 | $timeout = $this->ldapConfig->getOptionalInteger('timeout', 3); |
||
| 92 | Assert::natural($timeout); |
||
| 93 | |||
| 94 | $attributes = $this->ldapConfig->getOptionalValue( |
||
| 95 | 'attributes', |
||
| 96 | // If specifically set to NULL return all attributes, if not set at all return nothing (safe default) |
||
| 97 | in_array('attributes', $this->ldapConfig->getOptions(), true) ? ['*'] : [], |
||
| 98 | ); |
||
| 99 | |||
| 100 | $searchBase = $this->ldapConfig->getArray('search.base'); |
||
| 101 | |||
| 102 | $options = [ |
||
| 103 | 'scope' => $searchScope, |
||
| 104 | 'timeout' => $timeout, |
||
| 105 | 'filter' => $attributes, |
||
| 106 | ]; |
||
| 107 | |||
| 108 | $searchEnable = $this->ldapConfig->getOptionalBoolean('search.enable', false); |
||
| 109 | if ($searchEnable === false) { |
||
| 110 | $dnPattern = $this->ldapConfig->getString('dnpattern'); |
||
| 111 | $dn = str_replace('%username%', $username, $dnPattern); |
||
| 112 | } else { |
||
| 113 | $searchUsername = $this->ldapConfig->getOptionalString('search.username', null); |
||
| 114 | Assert::nullOrNotWhitespaceOnly($searchUsername); |
||
| 115 | |||
| 116 | $searchPassword = $this->ldapConfig->getOptionalString('search.password', null); |
||
| 117 | Assert::nullOrNotWhitespaceOnly($searchPassword); |
||
| 118 | |||
| 119 | try { |
||
| 120 | $this->connector->bind($searchUsername, $searchPassword); |
||
| 121 | } catch (Error\Error $e) { |
||
| 122 | throw new Error\Exception("Unable to bind using the configured search.username and search.password."); |
||
| 123 | } |
||
| 124 | |||
| 125 | $filter = $this->buildSearchFilter($username); |
||
| 126 | |||
| 127 | try { |
||
| 128 | $entry = /** @scrutinizer-ignore-type */$this->connector->search($searchBase, $filter, $options, false); |
||
| 129 | $dn = $entry->getDn(); |
||
| 130 | } catch (Error\Exception $e) { |
||
| 131 | throw new Error\Error('WRONGUSERPASS'); |
||
| 132 | } |
||
| 133 | } |
||
| 134 | |||
| 135 | /* Verify the credentials */ |
||
| 136 | if (!empty($sasl_args)) { |
||
| 137 | $this->connector->saslBind( |
||
| 138 | $dn, |
||
| 139 | $password, |
||
| 140 | $sasl_args['mech'], |
||
| 141 | $sasl_args['realm'], |
||
| 142 | $sasl_args['authc_id'], |
||
| 143 | $sasl_args['authz_id'], |
||
| 144 | $sasl_args['props'], |
||
| 145 | ); |
||
| 146 | $dn = $this->connector->whoami(); |
||
| 147 | } else { |
||
| 148 | $this->connector->bind($dn, $password); |
||
| 149 | } |
||
| 150 | |||
| 151 | /* If the credentials were correct, rebind using a privileged account to read attributes */ |
||
| 152 | $readUsername = $this->ldapConfig->getOptionalString('priv.username', null); |
||
| 153 | $readPassword = $this->ldapConfig->getOptionalString('priv.password', null); |
||
| 154 | if ($readUsername !== null) { |
||
| 155 | $this->connector->bind($readUsername, $readPassword); |
||
| 156 | } |
||
| 157 | |||
| 158 | $options['scope'] = Query::SCOPE_BASE; |
||
| 159 | $filter = '(objectClass=*)'; |
||
| 160 | |||
| 161 | $entry = $this->connector->search([$dn], $filter, $options, false); |
||
| 162 | |||
| 163 | return $this->processAttributes(/** @scrutinizer-ignore-type */$entry); |
||
| 164 | } |
||
| 292 |