Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like LDAPService 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 LDAPService, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class LDAPService extends Object implements Flushable |
||
|
|||
16 | { |
||
17 | /** |
||
18 | * @var array |
||
19 | */ |
||
20 | private static $dependencies = [ |
||
21 | 'gateway' => '%$LDAPGateway' |
||
22 | ]; |
||
23 | |||
24 | /** |
||
25 | * If configured, only user objects within these locations will be exposed to this service. |
||
26 | * |
||
27 | * @var array |
||
28 | * @config |
||
29 | */ |
||
30 | private static $users_search_locations = []; |
||
31 | |||
32 | /** |
||
33 | * If configured, only group objects within these locations will be exposed to this service. |
||
34 | * @var array |
||
35 | * |
||
36 | * @config |
||
37 | */ |
||
38 | private static $groups_search_locations = []; |
||
39 | |||
40 | /** |
||
41 | * Location to create new users in (distinguished name). |
||
42 | * @var string |
||
43 | * |
||
44 | * @config |
||
45 | */ |
||
46 | private static $new_users_dn; |
||
47 | |||
48 | /** |
||
49 | * @var array |
||
50 | */ |
||
51 | private static $_cache_nested_groups = []; |
||
52 | |||
53 | /** |
||
54 | * If this is configured to a "Code" value of a {@link Group} in SilverStripe, the user will always |
||
55 | * be added to this group's membership when imported, regardless of any sort of group mappings. |
||
56 | * |
||
57 | * @var string |
||
58 | * @config |
||
59 | */ |
||
60 | private static $default_group; |
||
61 | |||
62 | /** |
||
63 | * @var bool |
||
64 | */ |
||
65 | private static $password_history_workaround = false; |
||
66 | |||
67 | /** |
||
68 | * Get the cache objecgt used for LDAP results. Note that the default lifetime set here |
||
69 | * is 8 hours, but you can change that by calling SS_Cache::set_lifetime('ldap', <lifetime in seconds>) |
||
70 | * |
||
71 | * @return Zend_Cache_Frontend |
||
72 | */ |
||
73 | public static function get_cache() |
||
80 | |||
81 | /** |
||
82 | * Flushes out the LDAP results cache when flush=1 is called. |
||
83 | */ |
||
84 | public static function flush() |
||
89 | |||
90 | /** |
||
91 | * @var LDAPGateway |
||
92 | */ |
||
93 | public $gateway; |
||
94 | |||
95 | /** |
||
96 | * Setter for gateway. Useful for overriding the gateway with a fake for testing. |
||
97 | * @var LDAPGateway |
||
98 | */ |
||
99 | public function setGateway($gateway) |
||
103 | |||
104 | /** |
||
105 | * Checkes whether or not the service is enabled. |
||
106 | * |
||
107 | * @return bool |
||
108 | */ |
||
109 | public function enabled() |
||
114 | |||
115 | /** |
||
116 | * Authenticate the given username and password with LDAP. |
||
117 | * |
||
118 | * @param string $username |
||
119 | * @param string $password |
||
120 | * |
||
121 | * @return array |
||
122 | */ |
||
123 | public function authenticate($username, $password) |
||
159 | |||
160 | /** |
||
161 | * Return all nodes (organizational units, containers, and domains) within the current base DN. |
||
162 | * |
||
163 | * @param boolean $cached Cache the results from AD, so that subsequent calls are faster. Enabled by default. |
||
164 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
165 | * @return array |
||
166 | */ |
||
167 | public function getNodes($cached = true, $attributes = []) |
||
184 | |||
185 | /** |
||
186 | * Return all AD groups in configured search locations, including all nested groups. |
||
187 | * Uses groups_search_locations if defined, otherwise falls back to NULL, which tells LDAPGateway |
||
188 | * to use the default baseDn defined in the connection. |
||
189 | * |
||
190 | * @param boolean $cached Cache the results from AD, so that subsequent calls are faster. Enabled by default. |
||
191 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
192 | * @param string $indexBy Attribute to use as an index. |
||
193 | * @return array |
||
194 | */ |
||
195 | public function getGroups($cached = true, $attributes = [], $indexBy = 'dn') |
||
219 | |||
220 | /** |
||
221 | * Return all member groups (and members of those, recursively) underneath a specific group DN. |
||
222 | * Note that these get cached in-memory per-request for performance to avoid re-querying for the same results. |
||
223 | * |
||
224 | * @param string $dn |
||
225 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
226 | * @return array |
||
227 | */ |
||
228 | public function getNestedGroups($dn, $attributes = []) |
||
246 | |||
247 | /** |
||
248 | * Get a particular AD group's data given a GUID. |
||
249 | * |
||
250 | * @param string $guid |
||
251 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
252 | * @return array |
||
253 | */ |
||
254 | View Code Duplication | public function getGroupByGUID($guid, $attributes = []) |
|
264 | |||
265 | /** |
||
266 | * Get a particular AD group's data given a DN. |
||
267 | * |
||
268 | * @param string $dn |
||
269 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
270 | * @return array |
||
271 | */ |
||
272 | View Code Duplication | public function getGroupByDN($dn, $attributes = []) |
|
282 | |||
283 | /** |
||
284 | * Return all AD users in configured search locations, including all users in nested groups. |
||
285 | * Uses users_search_locations if defined, otherwise falls back to NULL, which tells LDAPGateway |
||
286 | * to use the default baseDn defined in the connection. |
||
287 | * |
||
288 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
289 | * @return array |
||
290 | */ |
||
291 | public function getUsers($attributes = []) |
||
309 | |||
310 | /** |
||
311 | * Get a specific AD user's data given a GUID. |
||
312 | * |
||
313 | * @param string $guid |
||
314 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
315 | * @return array |
||
316 | */ |
||
317 | View Code Duplication | public function getUserByGUID($guid, $attributes = []) |
|
327 | |||
328 | /** |
||
329 | * Get a specific AD user's data given a DN. |
||
330 | * |
||
331 | * @param string $dn |
||
332 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
333 | * |
||
334 | * @return array |
||
335 | */ |
||
336 | View Code Duplication | public function getUserByDN($dn, $attributes = []) |
|
346 | |||
347 | /** |
||
348 | * Get a specific user's data given an email. |
||
349 | * |
||
350 | * @param string $email |
||
351 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
352 | * @return array |
||
353 | */ |
||
354 | View Code Duplication | public function getUserByEmail($email, $attributes = []) |
|
364 | |||
365 | /** |
||
366 | * Get a specific user's data given a username. |
||
367 | * |
||
368 | * @param string $username |
||
369 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
370 | * @return array |
||
371 | */ |
||
372 | View Code Duplication | public function getUserByUsername($username, $attributes = []) |
|
382 | |||
383 | /** |
||
384 | * Get a username for an email. |
||
385 | * |
||
386 | * @param string $email |
||
387 | * @return string|null |
||
388 | */ |
||
389 | public function getUsernameByEmail($email) |
||
398 | |||
399 | /** |
||
400 | * Given a group DN, get the group membership data in LDAP. |
||
401 | * |
||
402 | * @param string $dn |
||
403 | * @return array |
||
404 | */ |
||
405 | public function getLDAPGroupMembers($dn) |
||
418 | |||
419 | /** |
||
420 | * Update the current Member record with data from LDAP. |
||
421 | * |
||
422 | * Constraints: |
||
423 | * - Member *must* be in the database before calling this as it will need the ID to be mapped to a {@link Group}. |
||
424 | * - GUID of the member must have already been set, for integrity reasons we don't allow it to change here. |
||
425 | * |
||
426 | * @param Member |
||
427 | * @param array|null $data If passed, this is pre-existing AD attribute data to update the Member with. |
||
428 | * If not given, the data will be looked up by the user's GUID. |
||
429 | * @return bool |
||
430 | */ |
||
431 | public function updateMemberFromLDAP(Member $member, $data = null) |
||
592 | |||
593 | /** |
||
594 | * Sync a specific Group by updating it with LDAP data. |
||
595 | * |
||
596 | * @param Group $group An existing Group or a new Group object |
||
597 | * @param array $data LDAP group object data |
||
598 | * |
||
599 | * @return bool |
||
600 | */ |
||
601 | public function updateGroupFromLDAP(Group $group, $data) |
||
641 | |||
642 | /** |
||
643 | * Creates a new LDAP user from the passed Member record. |
||
644 | * Note that the Member record must have a non-empty Username field for this to work. |
||
645 | * |
||
646 | * @param Member $member |
||
647 | */ |
||
648 | public function createLDAPUser(Member $member) |
||
690 | |||
691 | /** |
||
692 | * Update the Member data back to the corresponding LDAP user object. |
||
693 | * |
||
694 | * @param Member $member |
||
695 | * @throws ValidationException |
||
696 | */ |
||
697 | public function updateLDAPFromMember(Member $member) |
||
756 | |||
757 | /** |
||
758 | * Ensure the user belongs to the correct groups in LDAP from their membership |
||
759 | * to local LDAP mapped SilverStripe groups. |
||
760 | * |
||
761 | * This also removes them from LDAP groups if they've been taken out of one. |
||
762 | * It will not affect group membership of non-mapped groups, so it will |
||
763 | * not touch such internal AD groups like "Domain Users". |
||
764 | * |
||
765 | * @param Member $member |
||
766 | */ |
||
767 | public function updateLDAPGroupsForMember(Member $member) |
||
854 | |||
855 | /** |
||
856 | * Change a members password on the AD. Works with ActiveDirectory compatible services that saves the |
||
857 | * password in the `unicodePwd` attribute. |
||
858 | * |
||
859 | * @todo Use the Zend\Ldap\Attribute::setPassword functionality to create a password in |
||
860 | * an abstract way, so it works on other LDAP directories, not just Active Directory. |
||
861 | * |
||
862 | * Ensure that the LDAP bind:ed user can change passwords and that the connection is secure. |
||
863 | * |
||
864 | * @param Member $member |
||
865 | * @param string $password |
||
866 | * @param string|null $oldPassword Supply old password to perform a password change (as opposed to password reset) |
||
867 | * @return ValidationResult |
||
868 | * @throws Exception |
||
869 | */ |
||
870 | public function setPassword(Member $member, $password, $oldPassword = null) |
||
905 | |||
906 | /** |
||
907 | * For samba4 directory, there is no way to enforce password history on password resets. |
||
908 | * This only happens with changePassword (which requires the old password). |
||
909 | * This works around it by making the old password up and setting it administratively. |
||
910 | * |
||
911 | * A cleaner fix would be to use the LDAP_SERVER_POLICY_HINTS_OID connection flag, |
||
912 | * but it's not implemented in samba https://bugzilla.samba.org/show_bug.cgi?id=12020 |
||
913 | * |
||
914 | * @param string $dn Distinguished name of the user |
||
915 | * @param string $password New password. |
||
916 | * @throws Exception |
||
917 | */ |
||
918 | private function passwordHistoryWorkaround($dn, $password) { |
||
925 | |||
926 | /** |
||
927 | * Delete an LDAP user mapped to the Member record |
||
928 | * @param Member $member |
||
929 | */ |
||
930 | public function deleteLDAPMember(Member $member) { |
||
948 | |||
949 | /** |
||
950 | * A simple proxy to LDAP update operation. |
||
951 | * |
||
952 | * @param string $dn Location to add the entry at. |
||
953 | * @param array $attributes A simple associative array of attributes. |
||
954 | */ |
||
955 | public function update($dn, array $attributes) |
||
959 | |||
960 | /** |
||
961 | * A simple proxy to LDAP delete operation. |
||
962 | * |
||
963 | * @param string $dn Location of object to delete |
||
964 | * @param bool $recursively Recursively delete nested objects? |
||
965 | */ |
||
966 | public function delete($dn, $recursively = false) |
||
970 | |||
971 | /** |
||
972 | * A simple proxy to LDAP copy/delete operation. |
||
973 | * |
||
974 | * @param string $fromDn |
||
975 | * @param string $toDn |
||
976 | * @param bool $recursively Recursively move nested objects? |
||
977 | */ |
||
978 | public function move($fromDn, $toDn, $recursively = false) |
||
982 | |||
983 | /** |
||
984 | * A simple proxy to LDAP add operation. |
||
985 | * |
||
986 | * @param string $dn Location to add the entry at. |
||
987 | * @param array $attributes A simple associative array of attributes. |
||
988 | */ |
||
989 | public function add($dn, array $attributes) |
||
993 | } |
||
994 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.