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