| Conditions | 10 |
| Paths | 17 |
| Total Lines | 199 |
| Code Lines | 128 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 6 | ||
| Bugs | 0 | 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 |
||
| 131 | protected function getGroups(array $attributes): array |
||
| 132 | { |
||
| 133 | // Log the request |
||
| 134 | Logger::debug(sprintf( |
||
| 135 | '%s : Checking for groups based on the best method for the LDAP product.', |
||
| 136 | $this->title |
||
| 137 | )); |
||
| 138 | |||
| 139 | $ldapUtils = new LdapUtils(); |
||
| 140 | $ldapUtils->bind($this->ldapObject, $this->searchUsername, $this->searchPassword); |
||
| 141 | |||
| 142 | $options = [ |
||
| 143 | 'scope' => $this->config->getString('search.scope', Query::SCOPE_SUB), |
||
| 144 | 'timeout' => $this->config->getInteger('timeout', 3), |
||
| 145 | ]; |
||
| 146 | |||
| 147 | // Reference the map, just to make the name shorter |
||
| 148 | $map = &$this->attribute_map; |
||
| 149 | |||
| 150 | |||
| 151 | // All map-properties are guaranteed to exist and have a default value |
||
| 152 | $dn_attribute = $map['dn']; |
||
| 153 | $return_attribute = $map['return']; |
||
| 154 | |||
| 155 | // Based on the directory service, search LDAP for groups |
||
| 156 | // If any attributes are needed, prepare them before calling search method |
||
| 157 | switch ($this->product) { |
||
| 158 | case 'ActiveDirectory': |
||
| 159 | // Log the AD specific search |
||
| 160 | Logger::debug(sprintf( |
||
| 161 | '%s : Searching LDAP using ActiveDirectory specific method.', |
||
| 162 | $this->title |
||
| 163 | )); |
||
| 164 | |||
| 165 | // Make sure the defined DN attribute exists |
||
| 166 | if (!isset($attributes[$dn_attribute])) { |
||
| 167 | Logger::warning(sprintf( |
||
| 168 | "%s : The DN attribute [%s] is not defined in the user's Attributes: %s", |
||
| 169 | $this->title, |
||
| 170 | $dn_attribute, |
||
| 171 | implode(', ', array_keys($attributes)), |
||
| 172 | )); |
||
| 173 | |||
| 174 | return []; |
||
| 175 | } |
||
| 176 | |||
| 177 | // Make sure the defined DN attribute has a value |
||
| 178 | if (!isset($attributes[$dn_attribute][0]) || !$attributes[$dn_attribute][0]) { |
||
| 179 | Logger::warning(sprintf( |
||
| 180 | '%s : The DN attribute [%s] does not have a [0] value defined. %s', |
||
| 181 | $this->title, |
||
| 182 | $dn_attribute, |
||
| 183 | $this->varExport($attributes[$dn_attribute]) |
||
| 184 | )); |
||
| 185 | |||
| 186 | return []; |
||
| 187 | } |
||
| 188 | |||
| 189 | // Log the search |
||
| 190 | $arrayUtils = new Utils\Arrays(); |
||
| 191 | Logger::debug(sprintf( |
||
| 192 | '%s : Searching ActiveDirectory group membership.' |
||
| 193 | . ' DN: %s DN Attribute: %s Member Attribute: %s Type Attribute: %s Type Value: %s Base: %s', |
||
| 194 | $this->title, |
||
| 195 | $attributes[$dn_attribute][0], |
||
| 196 | $dn_attribute, |
||
| 197 | $map['member'], |
||
| 198 | $map['type'], |
||
| 199 | $this->type_map['group'], |
||
| 200 | implode('; ', $arrayUtils->arrayize($this->searchBase)) |
||
| 201 | )); |
||
| 202 | |||
| 203 | $filter = sprintf( |
||
| 204 | "(&(%s=%s)(%s=%s))", |
||
| 205 | $map['type'], |
||
| 206 | $this->type_map['group'], |
||
| 207 | $map['member'] . ':1.2.840.113556.1.4.1941:', |
||
| 208 | $attributes[$dn_attribute][0], |
||
| 209 | ); |
||
| 210 | |||
| 211 | $entries = $ldapUtils->searchForMultiple( |
||
| 212 | $this->ldapObject, |
||
| 213 | $this->searchBase, |
||
| 214 | $filter, |
||
| 215 | $options, |
||
| 216 | true |
||
| 217 | ); |
||
| 218 | |||
| 219 | break; |
||
| 220 | case 'OpenLDAP': |
||
| 221 | // Log the OpenLDAP specific search |
||
| 222 | Logger::debug(sprintf( |
||
| 223 | '%s : Searching LDAP using OpenLDAP specific method.', |
||
| 224 | $this->title |
||
| 225 | )); |
||
| 226 | |||
| 227 | Logger::debug(sprintf( |
||
| 228 | '%s : Searching for groups in base [%s] with filter (%s=%s) and attributes %s', |
||
| 229 | $this->title, |
||
| 230 | implode(', ', $this->searchBase), |
||
| 231 | $map['memberOf'], |
||
| 232 | $attributes[$map['username']][0], |
||
| 233 | $map['member'] |
||
| 234 | )); |
||
| 235 | |||
| 236 | $filter = sprintf( |
||
| 237 | '(&(%s=%s))', |
||
| 238 | $map['memberOf'], |
||
| 239 | $attributes[$map['username']][0] |
||
| 240 | ); |
||
| 241 | |||
| 242 | $entries = $ldapUtils->searchForMultiple( |
||
| 243 | $this->ldapObject, |
||
| 244 | $this->searchBase, |
||
| 245 | $filter, |
||
| 246 | $options, |
||
| 247 | true |
||
| 248 | ); |
||
| 249 | |||
| 250 | break; |
||
| 251 | default: |
||
| 252 | // Log the generic search |
||
| 253 | Logger::debug( |
||
| 254 | $this->title . 'Searching LDAP using the generic search method.' |
||
| 255 | ); |
||
| 256 | |||
| 257 | // Make sure the defined memberOf attribute exists |
||
| 258 | Assert::keyExists( |
||
| 259 | $attributes, |
||
| 260 | $map['memberOf'], |
||
| 261 | sprintf( |
||
| 262 | "%s : The memberOf attribute [%s] is not defined in the user's attributes: [%s]", |
||
| 263 | $this->title, |
||
| 264 | $map['memberOf'], |
||
| 265 | implode(', ', array_keys($attributes)) |
||
| 266 | ), |
||
| 267 | Error\Exception::class, |
||
| 268 | ); |
||
| 269 | |||
| 270 | // MemberOf must be an array of group DN's |
||
| 271 | Assert::isArray( |
||
| 272 | $attributes[$map['memberOf']], |
||
| 273 | sprintf( |
||
| 274 | '%s : The memberOf attribute [%s] is not an array of group DNs; %s', |
||
| 275 | $this->title, |
||
| 276 | $map['memberOf'], |
||
| 277 | $this->varExport($attributes[$map['memberOf']]), |
||
| 278 | ), |
||
| 279 | Error\Exception::class, |
||
| 280 | ); |
||
| 281 | |||
| 282 | Logger::debug(sprintf( |
||
| 283 | '%s : Checking DNs for groups. DNs: %s Attributes: %s, %s Group Type: %s', |
||
| 284 | $this->title, |
||
| 285 | implode('; ', $attributes[$map['memberOf']]), |
||
| 286 | $map['memberOf'], |
||
| 287 | $map['type'], |
||
| 288 | $this->type_map['group'] |
||
| 289 | )); |
||
| 290 | |||
| 291 | // Search for the users group membership, recursively |
||
| 292 | $entries = $this->search($attributes[$map['memberOf']], $options); |
||
| 293 | } |
||
| 294 | |||
| 295 | $groups = []; |
||
| 296 | foreach ($entries as $entry) { |
||
| 297 | if ($entry->hasAttribute($return_attribute)) { |
||
| 298 | $values = $entry->getAttribute($return_attribute); |
||
| 299 | $groups[] = array_pop($values); |
||
| 300 | continue; |
||
| 301 | } elseif ($entry->hasAttribute(strtolower($return_attribute))) { |
||
| 302 | // Some backends return lowercase attributes |
||
| 303 | $values = $entry->getAttribute(strtolower($return_attribute)); |
||
| 304 | $groups[] = array_pop($values); |
||
| 305 | continue; |
||
| 306 | } elseif ($entry->hasAttribute('dn')) { |
||
| 307 | // AD queries also seem to return the objects dn by default |
||
| 308 | $values = $entry->getAttribute('dn'); |
||
| 309 | $groups[] = array_pop($values); |
||
| 310 | continue; |
||
| 311 | } |
||
| 312 | |||
| 313 | // Could not find DN, log and continue |
||
| 314 | Logger::notice(sprintf( |
||
| 315 | '%s : The return attribute [%s] could not be found in the entry. %s', |
||
| 316 | $this->title, |
||
| 317 | implode(', ', [$map['return'], strtolower($map['return']), 'dn']), |
||
| 318 | $this->varExport($entry), |
||
| 319 | )); |
||
| 320 | } |
||
| 321 | |||
| 322 | // All done |
||
| 323 | Logger::debug(sprintf( |
||
| 324 | '%s : User found to be a member of the groups: %s', |
||
| 325 | $this->title, |
||
| 326 | implode('; ', $groups), |
||
| 327 | )); |
||
| 328 | |||
| 329 | return $groups; |
||
| 330 | } |
||
| 393 |