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 | * Location to create new groups in (distinguished name). |
||
50 | * @var string |
||
51 | * |
||
52 | * @config |
||
53 | */ |
||
54 | private static $new_groups_dn; |
||
55 | |||
56 | /** |
||
57 | * @var array |
||
58 | */ |
||
59 | private static $_cache_nested_groups = []; |
||
60 | |||
61 | /** |
||
62 | * If this is configured to a "Code" value of a {@link Group} in SilverStripe, the user will always |
||
63 | * be added to this group's membership when imported, regardless of any sort of group mappings. |
||
64 | * |
||
65 | * @var string |
||
66 | * @config |
||
67 | */ |
||
68 | private static $default_group; |
||
69 | |||
70 | /** |
||
71 | * For samba4 directory, there is no way to enforce password history on password resets. |
||
72 | * This only happens with changePassword (which requires the old password). |
||
73 | * This works around it by making the old password up and setting it administratively. |
||
74 | * |
||
75 | * A cleaner fix would be to use the LDAP_SERVER_POLICY_HINTS_OID connection flag, |
||
76 | * but it's not implemented in samba https://bugzilla.samba.org/show_bug.cgi?id=12020 |
||
77 | * |
||
78 | * @var bool |
||
79 | */ |
||
80 | private static $password_history_workaround = false; |
||
81 | |||
82 | /** |
||
83 | * Get the cache objecgt used for LDAP results. Note that the default lifetime set here |
||
84 | * is 8 hours, but you can change that by calling SS_Cache::set_lifetime('ldap', <lifetime in seconds>) |
||
85 | * |
||
86 | * @return Zend_Cache_Frontend |
||
87 | */ |
||
88 | public static function get_cache() |
||
95 | |||
96 | /** |
||
97 | * Flushes out the LDAP results cache when flush=1 is called. |
||
98 | */ |
||
99 | public static function flush() |
||
104 | |||
105 | /** |
||
106 | * @var LDAPGateway |
||
107 | */ |
||
108 | public $gateway; |
||
109 | |||
110 | /** |
||
111 | * Setter for gateway. Useful for overriding the gateway with a fake for testing. |
||
112 | * @var LDAPGateway |
||
113 | */ |
||
114 | public function setGateway($gateway) |
||
118 | |||
119 | /** |
||
120 | * Checkes whether or not the service is enabled. |
||
121 | * |
||
122 | * @return bool |
||
123 | */ |
||
124 | public function enabled() |
||
129 | |||
130 | /** |
||
131 | * Authenticate the given username and password with LDAP. |
||
132 | * |
||
133 | * @param string $username |
||
134 | * @param string $password |
||
135 | * |
||
136 | * @return array |
||
137 | */ |
||
138 | public function authenticate($username, $password) |
||
174 | |||
175 | /** |
||
176 | * Return all nodes (organizational units, containers, and domains) within the current base DN. |
||
177 | * |
||
178 | * @param boolean $cached Cache the results from AD, so that subsequent calls are faster. Enabled by default. |
||
179 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
180 | * @return array |
||
181 | */ |
||
182 | public function getNodes($cached = true, $attributes = []) |
||
199 | |||
200 | /** |
||
201 | * Return all AD groups in configured search locations, including all nested groups. |
||
202 | * Uses groups_search_locations if defined, otherwise falls back to NULL, which tells LDAPGateway |
||
203 | * to use the default baseDn defined in the connection. |
||
204 | * |
||
205 | * @param boolean $cached Cache the results from AD, so that subsequent calls are faster. Enabled by default. |
||
206 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
207 | * @param string $indexBy Attribute to use as an index. |
||
208 | * @return array |
||
209 | */ |
||
210 | public function getGroups($cached = true, $attributes = [], $indexBy = 'dn') |
||
234 | |||
235 | /** |
||
236 | * Return all member groups (and members of those, recursively) underneath a specific group DN. |
||
237 | * Note that these get cached in-memory per-request for performance to avoid re-querying for the same results. |
||
238 | * |
||
239 | * @param string $dn |
||
240 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
241 | * @return array |
||
242 | */ |
||
243 | public function getNestedGroups($dn, $attributes = []) |
||
261 | |||
262 | /** |
||
263 | * Get a particular AD group's data given a GUID. |
||
264 | * |
||
265 | * @param string $guid |
||
266 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
267 | * @return array |
||
268 | */ |
||
269 | View Code Duplication | public function getGroupByGUID($guid, $attributes = []) |
|
279 | |||
280 | /** |
||
281 | * Get a particular AD group's data given a DN. |
||
282 | * |
||
283 | * @param string $dn |
||
284 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
285 | * @return array |
||
286 | */ |
||
287 | View Code Duplication | public function getGroupByDN($dn, $attributes = []) |
|
297 | |||
298 | /** |
||
299 | * Return all AD users in configured search locations, including all users in nested groups. |
||
300 | * Uses users_search_locations if defined, otherwise falls back to NULL, which tells LDAPGateway |
||
301 | * to use the default baseDn defined in the connection. |
||
302 | * |
||
303 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
304 | * @return array |
||
305 | */ |
||
306 | public function getUsers($attributes = []) |
||
324 | |||
325 | /** |
||
326 | * Get a specific AD user's data given a GUID. |
||
327 | * |
||
328 | * @param string $guid |
||
329 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
330 | * @return array |
||
331 | */ |
||
332 | View Code Duplication | public function getUserByGUID($guid, $attributes = []) |
|
342 | |||
343 | /** |
||
344 | * Get a specific AD user's data given a DN. |
||
345 | * |
||
346 | * @param string $dn |
||
347 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
348 | * |
||
349 | * @return array |
||
350 | */ |
||
351 | View Code Duplication | public function getUserByDN($dn, $attributes = []) |
|
361 | |||
362 | /** |
||
363 | * Get a specific user's data given an email. |
||
364 | * |
||
365 | * @param string $email |
||
366 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
367 | * @return array |
||
368 | */ |
||
369 | View Code Duplication | public function getUserByEmail($email, $attributes = []) |
|
379 | |||
380 | /** |
||
381 | * Get a specific user's data given a username. |
||
382 | * |
||
383 | * @param string $username |
||
384 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
385 | * @return array |
||
386 | */ |
||
387 | View Code Duplication | public function getUserByUsername($username, $attributes = []) |
|
397 | |||
398 | /** |
||
399 | * Get a username for an email. |
||
400 | * |
||
401 | * @param string $email |
||
402 | * @return string|null |
||
403 | */ |
||
404 | public function getUsernameByEmail($email) |
||
413 | |||
414 | /** |
||
415 | * Given a group DN, get the group membership data in LDAP. |
||
416 | * |
||
417 | * @param string $dn |
||
418 | * @return array |
||
419 | */ |
||
420 | public function getLDAPGroupMembers($dn) |
||
433 | |||
434 | /** |
||
435 | * Update the current Member record with data from LDAP. |
||
436 | * |
||
437 | * It's allowed to pass an unwritten Member record here, because it's not always possible to satisfy |
||
438 | * field constraints without importing data from LDAP (for example if the application requires Username |
||
439 | * through a Validator). Even though unwritten, it still must have the GUID set. |
||
440 | * |
||
441 | * Constraints: |
||
442 | * - GUID of the member must have already been set, for integrity reasons we don't allow it to change here. |
||
443 | * |
||
444 | * @param Member |
||
445 | * @param array|null $data If passed, this is pre-existing AD attribute data to update the Member with. |
||
446 | * If not given, the data will be looked up by the user's GUID. |
||
447 | * @return bool |
||
448 | */ |
||
449 | public function updateMemberFromLDAP(Member $member, $data = null) |
||
615 | |||
616 | /** |
||
617 | * Sync a specific Group by updating it with LDAP data. |
||
618 | * |
||
619 | * @param Group $group An existing Group or a new Group object |
||
620 | * @param array $data LDAP group object data |
||
621 | * |
||
622 | * @return bool |
||
623 | */ |
||
624 | public function updateGroupFromLDAP(Group $group, $data) |
||
660 | |||
661 | /** |
||
662 | * Creates a new LDAP user from the passed Member record. |
||
663 | * Note that the Member record must have a non-empty Username field for this to work. |
||
664 | * |
||
665 | * @param Member $member |
||
666 | */ |
||
667 | public function createLDAPUser(Member $member) |
||
709 | |||
710 | /** |
||
711 | * Creates a new LDAP group from the passed Group record. |
||
712 | * |
||
713 | * @param Group $group |
||
714 | * @throws ValidationException |
||
715 | */ |
||
716 | public function createLDAPGroup(Group $group) { |
||
758 | |||
759 | /** |
||
760 | * Update the Member data back to the corresponding LDAP user object. |
||
761 | * |
||
762 | * @param Member $member |
||
763 | * @throws ValidationException |
||
764 | */ |
||
765 | public function updateLDAPFromMember(Member $member) |
||
824 | |||
825 | /** |
||
826 | * Ensure the user belongs to the correct groups in LDAP from their membership |
||
827 | * to local LDAP mapped SilverStripe groups. |
||
828 | * |
||
829 | * This also removes them from LDAP groups if they've been taken out of one. |
||
830 | * It will not affect group membership of non-mapped groups, so it will |
||
831 | * not touch such internal AD groups like "Domain Users". |
||
832 | * |
||
833 | * @param Member $member |
||
834 | */ |
||
835 | public function updateLDAPGroupsForMember(Member $member) |
||
909 | |||
910 | /** |
||
911 | * Add LDAP user by DN to LDAP group. |
||
912 | * |
||
913 | * @param string $userDn |
||
914 | * @param string $groupDn |
||
915 | * @throws \Exception |
||
916 | */ |
||
917 | public function addLDAPUserToGroup($userDn, $groupDn) { |
||
933 | |||
934 | /** |
||
935 | * Change a members password on the AD. Works with ActiveDirectory compatible services that saves the |
||
936 | * password in the `unicodePwd` attribute. |
||
937 | * |
||
938 | * @todo Use the Zend\Ldap\Attribute::setPassword functionality to create a password in |
||
939 | * an abstract way, so it works on other LDAP directories, not just Active Directory. |
||
940 | * |
||
941 | * Ensure that the LDAP bind:ed user can change passwords and that the connection is secure. |
||
942 | * |
||
943 | * @param Member $member |
||
944 | * @param string $password |
||
945 | * @param string|null $oldPassword Supply old password to perform a password change (as opposed to password reset) |
||
946 | * @return ValidationResult |
||
947 | * @throws Exception |
||
948 | */ |
||
949 | public function setPassword(Member $member, $password, $oldPassword = null) |
||
982 | |||
983 | /** |
||
984 | * Delete an LDAP user mapped to the Member record |
||
985 | * @param Member $member |
||
986 | */ |
||
987 | public function deleteLDAPMember(Member $member) { |
||
1005 | |||
1006 | /** |
||
1007 | * A simple proxy to LDAP update operation. |
||
1008 | * |
||
1009 | * @param string $dn Location to add the entry at. |
||
1010 | * @param array $attributes A simple associative array of attributes. |
||
1011 | */ |
||
1012 | public function update($dn, array $attributes) |
||
1016 | |||
1017 | /** |
||
1018 | * A simple proxy to LDAP delete operation. |
||
1019 | * |
||
1020 | * @param string $dn Location of object to delete |
||
1021 | * @param bool $recursively Recursively delete nested objects? |
||
1022 | */ |
||
1023 | public function delete($dn, $recursively = false) |
||
1027 | |||
1028 | /** |
||
1029 | * A simple proxy to LDAP copy/delete operation. |
||
1030 | * |
||
1031 | * @param string $fromDn |
||
1032 | * @param string $toDn |
||
1033 | * @param bool $recursively Recursively move nested objects? |
||
1034 | */ |
||
1035 | public function move($fromDn, $toDn, $recursively = false) |
||
1039 | |||
1040 | /** |
||
1041 | * A simple proxy to LDAP add operation. |
||
1042 | * |
||
1043 | * @param string $dn Location to add the entry at. |
||
1044 | * @param array $attributes A simple associative array of attributes. |
||
1045 | */ |
||
1046 | public function add($dn, array $attributes) |
||
1050 | |||
1051 | /** |
||
1052 | * @param string $dn Distinguished name of the user |
||
1053 | * @param string $password New password. |
||
1054 | * @throws Exception |
||
1055 | */ |
||
1056 | private function passwordHistoryWorkaround($dn, $password) { |
||
1063 | |||
1064 | } |
||
1065 |
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.