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 |
||
38 | class LDAPService extends Object implements Flushable |
||
39 | { |
||
40 | /** |
||
41 | * @var array |
||
42 | */ |
||
43 | private static $dependencies = [ |
||
|
|||
44 | 'gateway' => '%$SilverStripe\\ActiveDirectory\\Model\\LDAPGateway' |
||
45 | ]; |
||
46 | |||
47 | /** |
||
48 | * If configured, only user objects within these locations will be exposed to this service. |
||
49 | * |
||
50 | * @var array |
||
51 | * @config |
||
52 | */ |
||
53 | private static $users_search_locations = []; |
||
54 | |||
55 | /** |
||
56 | * If configured, only group objects within these locations will be exposed to this service. |
||
57 | * @var array |
||
58 | * |
||
59 | * @config |
||
60 | */ |
||
61 | private static $groups_search_locations = []; |
||
62 | |||
63 | /** |
||
64 | * Location to create new users in (distinguished name). |
||
65 | * @var string |
||
66 | * |
||
67 | * @config |
||
68 | */ |
||
69 | private static $new_users_dn; |
||
70 | |||
71 | /** |
||
72 | * @var array |
||
73 | */ |
||
74 | private static $_cache_nested_groups = []; |
||
75 | |||
76 | /** |
||
77 | * If this is configured to a "Code" value of a {@link Group} in SilverStripe, the user will always |
||
78 | * be added to this group's membership when imported, regardless of any sort of group mappings. |
||
79 | * |
||
80 | * @var string |
||
81 | * @config |
||
82 | */ |
||
83 | private static $default_group; |
||
84 | |||
85 | /** |
||
86 | * For samba4 directory, there is no way to enforce password history on password resets. |
||
87 | * This only happens with changePassword (which requires the old password). |
||
88 | * This works around it by making the old password up and setting it administratively. |
||
89 | * |
||
90 | * A cleaner fix would be to use the LDAP_SERVER_POLICY_HINTS_OID connection flag, |
||
91 | * but it's not implemented in samba https://bugzilla.samba.org/show_bug.cgi?id=12020 |
||
92 | * |
||
93 | * @var bool |
||
94 | */ |
||
95 | private static $password_history_workaround = false; |
||
96 | |||
97 | /** |
||
98 | * Get the cache object used for LDAP results. Note that the default lifetime set here |
||
99 | * is 8 hours, but you can change that by calling Cache::set_lifetime('ldap', <lifetime in seconds>) |
||
100 | * |
||
101 | * @return Zend_Cache_Frontend |
||
102 | */ |
||
103 | public static function get_cache() |
||
110 | |||
111 | /** |
||
112 | * Flushes out the LDAP results cache when flush=1 is called. |
||
113 | */ |
||
114 | public static function flush() |
||
119 | |||
120 | /** |
||
121 | * @var LDAPGateway |
||
122 | */ |
||
123 | public $gateway; |
||
124 | |||
125 | /** |
||
126 | * Setter for gateway. Useful for overriding the gateway with a fake for testing. |
||
127 | * @var LDAPGateway |
||
128 | */ |
||
129 | public function setGateway($gateway) |
||
133 | |||
134 | /** |
||
135 | * Checkes whether or not the service is enabled. |
||
136 | * |
||
137 | * @return bool |
||
138 | */ |
||
139 | public function enabled() |
||
144 | |||
145 | /** |
||
146 | * Authenticate the given username and password with LDAP. |
||
147 | * |
||
148 | * @param string $username |
||
149 | * @param string $password |
||
150 | * |
||
151 | * @return array |
||
152 | */ |
||
153 | public function authenticate($username, $password) |
||
189 | |||
190 | /** |
||
191 | * Return all nodes (organizational units, containers, and domains) within the current base DN. |
||
192 | * |
||
193 | * @param boolean $cached Cache the results from AD, so that subsequent calls are faster. Enabled by default. |
||
194 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
195 | * @return array |
||
196 | */ |
||
197 | public function getNodes($cached = true, $attributes = []) |
||
214 | |||
215 | /** |
||
216 | * Return all AD groups in configured search locations, including all nested groups. |
||
217 | * Uses groups_search_locations if defined, otherwise falls back to NULL, which tells LDAPGateway |
||
218 | * to use the default baseDn defined in the connection. |
||
219 | * |
||
220 | * @param boolean $cached Cache the results from AD, so that subsequent calls are faster. Enabled by default. |
||
221 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
222 | * @param string $indexBy Attribute to use as an index. |
||
223 | * @return array |
||
224 | */ |
||
225 | public function getGroups($cached = true, $attributes = [], $indexBy = 'dn') |
||
249 | |||
250 | /** |
||
251 | * Return all member groups (and members of those, recursively) underneath a specific group DN. |
||
252 | * Note that these get cached in-memory per-request for performance to avoid re-querying for the same results. |
||
253 | * |
||
254 | * @param string $dn |
||
255 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
256 | * @return array |
||
257 | */ |
||
258 | public function getNestedGroups($dn, $attributes = []) |
||
276 | |||
277 | /** |
||
278 | * Get a particular AD group's data given a GUID. |
||
279 | * |
||
280 | * @param string $guid |
||
281 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
282 | * @return array |
||
283 | */ |
||
284 | View Code Duplication | public function getGroupByGUID($guid, $attributes = []) |
|
294 | |||
295 | /** |
||
296 | * Get a particular AD group's data given a DN. |
||
297 | * |
||
298 | * @param string $dn |
||
299 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
300 | * @return array |
||
301 | */ |
||
302 | View Code Duplication | public function getGroupByDN($dn, $attributes = []) |
|
312 | |||
313 | /** |
||
314 | * Return all AD users in configured search locations, including all users in nested groups. |
||
315 | * Uses users_search_locations if defined, otherwise falls back to NULL, which tells LDAPGateway |
||
316 | * to use the default baseDn defined in the connection. |
||
317 | * |
||
318 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
319 | * @return array |
||
320 | */ |
||
321 | public function getUsers($attributes = []) |
||
339 | |||
340 | /** |
||
341 | * Get a specific AD user's data given a GUID. |
||
342 | * |
||
343 | * @param string $guid |
||
344 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
345 | * @return array |
||
346 | */ |
||
347 | View Code Duplication | public function getUserByGUID($guid, $attributes = []) |
|
357 | |||
358 | /** |
||
359 | * Get a specific AD user's data given a DN. |
||
360 | * |
||
361 | * @param string $dn |
||
362 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
363 | * |
||
364 | * @return array |
||
365 | */ |
||
366 | View Code Duplication | public function getUserByDN($dn, $attributes = []) |
|
376 | |||
377 | /** |
||
378 | * Get a specific user's data given an email. |
||
379 | * |
||
380 | * @param string $email |
||
381 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
382 | * @return array |
||
383 | */ |
||
384 | View Code Duplication | public function getUserByEmail($email, $attributes = []) |
|
394 | |||
395 | /** |
||
396 | * Get a specific user's data given a username. |
||
397 | * |
||
398 | * @param string $username |
||
399 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
400 | * @return array |
||
401 | */ |
||
402 | View Code Duplication | public function getUserByUsername($username, $attributes = []) |
|
412 | |||
413 | /** |
||
414 | * Get a username for an email. |
||
415 | * |
||
416 | * @param string $email |
||
417 | * @return string|null |
||
418 | */ |
||
419 | public function getUsernameByEmail($email) |
||
428 | |||
429 | /** |
||
430 | * Given a group DN, get the group membership data in LDAP. |
||
431 | * |
||
432 | * @param string $dn |
||
433 | * @return array |
||
434 | */ |
||
435 | public function getLDAPGroupMembers($dn) |
||
448 | |||
449 | /** |
||
450 | * Update the current Member record with data from LDAP. |
||
451 | * |
||
452 | * Constraints: |
||
453 | * - Member *must* be in the database before calling this as it will need the ID to be mapped to a {@link Group}. |
||
454 | * - GUID of the member must have already been set, for integrity reasons we don't allow it to change here. |
||
455 | * |
||
456 | * @param Member |
||
457 | * @param array|null $data If passed, this is pre-existing AD attribute data to update the Member with. |
||
458 | * If not given, the data will be looked up by the user's GUID. |
||
459 | * @return bool |
||
460 | */ |
||
461 | public function updateMemberFromLDAP(Member $member, $data = null) |
||
632 | |||
633 | /** |
||
634 | * Sync a specific Group by updating it with LDAP data. |
||
635 | * |
||
636 | * @param Group $group An existing Group or a new Group object |
||
637 | * @param array $data LDAP group object data |
||
638 | * |
||
639 | * @return bool |
||
640 | */ |
||
641 | public function updateGroupFromLDAP(Group $group, $data) |
||
681 | |||
682 | /** |
||
683 | * Creates a new LDAP user from the passed Member record. |
||
684 | * Note that the Member record must have a non-empty Username field for this to work. |
||
685 | * |
||
686 | * @param Member $member |
||
687 | * @throws ValidationException |
||
688 | * @throws Exception |
||
689 | */ |
||
690 | public function createLDAPUser(Member $member) |
||
732 | |||
733 | /** |
||
734 | * Update the Member data back to the corresponding LDAP user object. |
||
735 | * |
||
736 | * @param Member $member |
||
737 | * @throws ValidationException |
||
738 | */ |
||
739 | public function updateLDAPFromMember(Member $member) |
||
798 | |||
799 | /** |
||
800 | * Ensure the user belongs to the correct groups in LDAP from their membership |
||
801 | * to local LDAP mapped SilverStripe groups. |
||
802 | * |
||
803 | * This also removes them from LDAP groups if they've been taken out of one. |
||
804 | * It will not affect group membership of non-mapped groups, so it will |
||
805 | * not touch such internal AD groups like "Domain Users". |
||
806 | * |
||
807 | * @param Member $member |
||
808 | * @throws ValidationException |
||
809 | */ |
||
810 | public function updateLDAPGroupsForMember(Member $member) |
||
884 | |||
885 | /** |
||
886 | * Add LDAP user by DN to LDAP group. |
||
887 | * |
||
888 | * @param string $userDn |
||
889 | * @param string $groupDn |
||
890 | * @throws \Exception |
||
891 | */ |
||
892 | public function addLDAPUserToGroup($userDn, $groupDn) { |
||
908 | |||
909 | /** |
||
910 | * Change a members password on the AD. Works with ActiveDirectory compatible services that saves the |
||
911 | * password in the `unicodePwd` attribute. |
||
912 | * |
||
913 | * @todo Use the Zend\Ldap\Attribute::setPassword functionality to create a password in |
||
914 | * an abstract way, so it works on other LDAP directories, not just Active Directory. |
||
915 | * |
||
916 | * Ensure that the LDAP bind:ed user can change passwords and that the connection is secure. |
||
917 | * |
||
918 | * @param Member $member |
||
919 | * @param string $password |
||
920 | * @param string|null $oldPassword Supply old password to perform a password change (as opposed to password reset) |
||
921 | * @return ValidationResult |
||
922 | */ |
||
923 | public function setPassword(Member $member, $password, $oldPassword = null) |
||
966 | |||
967 | /** |
||
968 | * Delete an LDAP user mapped to the Member record |
||
969 | * @param Member $member |
||
970 | * @throws ValidationException |
||
971 | */ |
||
972 | public function deleteLDAPMember(Member $member) |
||
991 | |||
992 | /** |
||
993 | * A simple proxy to LDAP update operation. |
||
994 | * |
||
995 | * @param string $dn Location to add the entry at. |
||
996 | * @param array $attributes A simple associative array of attributes. |
||
997 | */ |
||
998 | public function update($dn, array $attributes) |
||
1002 | |||
1003 | /** |
||
1004 | * A simple proxy to LDAP delete operation. |
||
1005 | * |
||
1006 | * @param string $dn Location of object to delete |
||
1007 | * @param bool $recursively Recursively delete nested objects? |
||
1008 | */ |
||
1009 | public function delete($dn, $recursively = false) |
||
1013 | |||
1014 | /** |
||
1015 | * A simple proxy to LDAP copy/delete operation. |
||
1016 | * |
||
1017 | * @param string $fromDn |
||
1018 | * @param string $toDn |
||
1019 | * @param bool $recursively Recursively move nested objects? |
||
1020 | */ |
||
1021 | public function move($fromDn, $toDn, $recursively = false) |
||
1025 | |||
1026 | /** |
||
1027 | * A simple proxy to LDAP add operation. |
||
1028 | * |
||
1029 | * @param string $dn Location to add the entry at. |
||
1030 | * @param array $attributes A simple associative array of attributes. |
||
1031 | */ |
||
1032 | public function add($dn, array $attributes) |
||
1036 | |||
1037 | /** |
||
1038 | * @param string $dn Distinguished name of the user |
||
1039 | * @param string $password New password. |
||
1040 | * @throws Exception |
||
1041 | */ |
||
1042 | private function passwordHistoryWorkaround($dn, $password) |
||
1050 | |||
1051 | /** |
||
1052 | * Get a logger |
||
1053 | * |
||
1054 | * @return LoggerInterface |
||
1055 | */ |
||
1056 | public function getLogger() |
||
1060 | } |
||
1061 |
This check marks private properties in classes that are never used. Those properties can be removed.