Conditions | 10 |
Paths | 17 |
Total Lines | 170 |
Code Lines | 104 |
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 | $ldap = $ldapUtils->bind($this->ldapServers, $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 | $arrayUtils = new Utils\Arrays(); |
||
160 | |||
161 | // Log the AD specific search |
||
162 | Logger::debug(sprintf( |
||
163 | '%s : Searching LDAP using ActiveDirectory specific method.', |
||
164 | $this->title |
||
165 | )); |
||
166 | |||
167 | // Make sure the defined DN attribute exists |
||
168 | if (!isset($attributes[$dn_attribute])) { |
||
169 | Logger::warning(sprintf( |
||
170 | "%s : The DN attribute [%s] is not defined in the user's Attributes: %s", |
||
171 | $this->title, |
||
172 | $dn_attribute, |
||
173 | implode(', ', array_keys($attributes)), |
||
174 | )); |
||
175 | |||
176 | return []; |
||
177 | } |
||
178 | |||
179 | // Make sure the defined DN attribute has a value |
||
180 | if (!isset($attributes[$dn_attribute][0]) || !$attributes[$dn_attribute][0]) { |
||
181 | Logger::warning(sprintf( |
||
182 | '%s : The DN attribute [%s] does not have a [0] value defined. %s', |
||
183 | $this->title, |
||
184 | $dn_attribute, |
||
185 | $this->varExport($attributes[$dn_attribute]) |
||
186 | )); |
||
187 | |||
188 | return []; |
||
189 | } |
||
190 | |||
191 | // Log the search |
||
192 | Logger::debug(sprintf( |
||
193 | '%s : Searching ActiveDirectory group membership.' |
||
194 | . ' DN: %s DN Attribute: %s Member Attribute: %s Type Attribute: %s Type Value: %s Base: %s', |
||
195 | $this->title, |
||
196 | $attributes[$dn_attribute][0], |
||
197 | $dn_attribute, |
||
198 | $map['member'], |
||
199 | $map['type'], |
||
200 | $this->type_map['group'], |
||
201 | implode('; ', $arrayUtils->arrayize($this->searchBase)) |
||
202 | )); |
||
203 | |||
204 | $filter = sprintf( |
||
205 | "(&(%s=%s)(%s=%s))", |
||
206 | $map['type'], |
||
207 | $this->type_map['group'], |
||
208 | $map['member'] . ':1.2.840.113556.1.4.1941:', |
||
209 | $attributes[$dn_attribute][0], |
||
210 | ); |
||
211 | |||
212 | break; |
||
213 | case 'OpenLDAP': |
||
214 | // Log the OpenLDAP specific search |
||
215 | Logger::debug(sprintf( |
||
216 | '%s : Searching LDAP using OpenLDAP specific method.', |
||
217 | $this->title |
||
218 | )); |
||
219 | |||
220 | Logger::debug(sprintf( |
||
221 | '%s : Searching for groups in base [%s] with filter (%s=%s) and attributes %s', |
||
222 | $this->title, |
||
223 | implode(', ', $this->searchBase), |
||
224 | $map['memberof'], |
||
225 | $attributes[$map['username']][0], |
||
226 | $map['member'] |
||
227 | )); |
||
228 | |||
229 | $filter = sprintf( |
||
230 | '(&(%s=%s))', |
||
231 | $map['memberof'], |
||
232 | $attributes[$map['username']][0] |
||
233 | ); |
||
234 | break; |
||
235 | default: |
||
236 | // Log the generic search |
||
237 | Logger::debug( |
||
238 | $this->title . 'Searching LDAP using the generic search method.' |
||
239 | ); |
||
240 | |||
241 | Logger::debug(sprintf( |
||
242 | '%s : Checking DNs for groups. DNs: %s Attributes: %s, %s Group Type: %s', |
||
243 | $this->title, |
||
244 | implode('; ', $attributes[$map['memberof']]), |
||
245 | $map['memberof'], |
||
246 | $map['type'], |
||
247 | $this->type_map['group'] |
||
248 | )); |
||
249 | |||
250 | /** |
||
251 | * @ TODO: finish generic search method |
||
252 | * |
||
253 | * // Search for the users group membership, recursively |
||
254 | * $groups = $this->search($attributes[$map['memberof']]); |
||
255 | */ |
||
256 | } |
||
257 | |||
258 | $entries = $ldapUtils->searchForMultiple( |
||
259 | $ldap, |
||
260 | $this->searchBase, |
||
261 | $filter, |
||
|
|||
262 | $options, |
||
263 | true |
||
264 | ); |
||
265 | |||
266 | $groups = []; |
||
267 | foreach ($entries as $entry) { |
||
268 | if ($entry->hasAttribute($return_attribute)) { |
||
269 | $values = $entry->getAttribute($return_attribute); |
||
270 | $groups[] = array_pop($values); |
||
271 | continue; |
||
272 | } elseif ($entry->hasAttribute(strtolower($return_attribute))) { |
||
273 | // Some backends return lowercase attributes |
||
274 | $values = $entry->getAttribute(strtolower($return_attribute)); |
||
275 | $groups[] = array_pop($values); |
||
276 | continue; |
||
277 | } elseif ($entry->hasAttribute('dn')) { |
||
278 | // AD queries also seem to return the objects dn by default |
||
279 | $values = $entry->getAttribute('dn'); |
||
280 | $groups[] = array_pop($values); |
||
281 | continue; |
||
282 | } |
||
283 | |||
284 | // Could not find DN, log and continue |
||
285 | Logger::notice(sprintf( |
||
286 | '%s : The return attribute [%s] could not be found in the entry. %s', |
||
287 | $this->title, |
||
288 | implode(', ', [$map['return'], strtolower($map['return']), 'dn']), |
||
289 | $this->varExport($entry), |
||
290 | )); |
||
291 | } |
||
292 | |||
293 | // All done |
||
294 | Logger::debug(sprintf( |
||
295 | '%s : User found to be a member of the groups: %s', |
||
296 | $this->title, |
||
297 | implode('; ', $groups), |
||
298 | )); |
||
299 | |||
300 | return $groups; |
||
301 | } |
||
371 |