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 |
||
40 | class LDAPService extends Object implements Flushable |
||
41 | { |
||
42 | /** |
||
43 | * @var array |
||
44 | */ |
||
45 | private static $dependencies = [ |
||
|
|||
46 | 'gateway' => '%$' . LDAPGateway::class |
||
47 | ]; |
||
48 | |||
49 | /** |
||
50 | * If configured, only user objects within these locations will be exposed to this service. |
||
51 | * |
||
52 | * @var array |
||
53 | * @config |
||
54 | */ |
||
55 | private static $users_search_locations = []; |
||
56 | |||
57 | /** |
||
58 | * If configured, only group objects within these locations will be exposed to this service. |
||
59 | * @var array |
||
60 | * |
||
61 | * @config |
||
62 | */ |
||
63 | private static $groups_search_locations = []; |
||
64 | |||
65 | /** |
||
66 | * Location to create new users in (distinguished name). |
||
67 | * @var string |
||
68 | * |
||
69 | * @config |
||
70 | */ |
||
71 | private static $new_users_dn; |
||
72 | |||
73 | /** |
||
74 | * Location to create new groups in (distinguished name). |
||
75 | * @var string |
||
76 | * |
||
77 | * @config |
||
78 | */ |
||
79 | private static $new_groups_dn; |
||
80 | |||
81 | /** |
||
82 | * @var array |
||
83 | */ |
||
84 | private static $_cache_nested_groups = []; |
||
85 | |||
86 | /** |
||
87 | * If this is configured to a "Code" value of a {@link Group} in SilverStripe, the user will always |
||
88 | * be added to this group's membership when imported, regardless of any sort of group mappings. |
||
89 | * |
||
90 | * @var string |
||
91 | * @config |
||
92 | */ |
||
93 | private static $default_group; |
||
94 | |||
95 | /** |
||
96 | * For samba4 directory, there is no way to enforce password history on password resets. |
||
97 | * This only happens with changePassword (which requires the old password). |
||
98 | * This works around it by making the old password up and setting it administratively. |
||
99 | * |
||
100 | * A cleaner fix would be to use the LDAP_SERVER_POLICY_HINTS_OID connection flag, |
||
101 | * but it's not implemented in samba https://bugzilla.samba.org/show_bug.cgi?id=12020 |
||
102 | * |
||
103 | * @var bool |
||
104 | */ |
||
105 | private static $password_history_workaround = false; |
||
106 | |||
107 | /** |
||
108 | * Get the cache object used for LDAP results. Note that the default lifetime set here |
||
109 | * is 8 hours, but you can change that by adding configuration: |
||
110 | * |
||
111 | * <code> |
||
112 | * SilverStripe\Core\Injector\Injector: |
||
113 | * Psr\SimpleCache\CacheInterface.ldap: |
||
114 | * constructor: |
||
115 | * defaultLifetime: 3600 # time in seconds |
||
116 | * </code> |
||
117 | * |
||
118 | * @return Psr\SimpleCache\CacheInterface |
||
119 | */ |
||
120 | public static function get_cache() |
||
124 | |||
125 | /** |
||
126 | * Flushes out the LDAP results cache when flush=1 is called. |
||
127 | */ |
||
128 | public static function flush() |
||
134 | |||
135 | /** |
||
136 | * @var LDAPGateway |
||
137 | */ |
||
138 | public $gateway; |
||
139 | |||
140 | /** |
||
141 | * Setter for gateway. Useful for overriding the gateway with a fake for testing. |
||
142 | * @var LDAPGateway |
||
143 | */ |
||
144 | public function setGateway($gateway) |
||
148 | |||
149 | /** |
||
150 | * Checkes whether or not the service is enabled. |
||
151 | * |
||
152 | * @return bool |
||
153 | */ |
||
154 | public function enabled() |
||
159 | |||
160 | /** |
||
161 | * Authenticate the given username and password with LDAP. |
||
162 | * |
||
163 | * @param string $username |
||
164 | * @param string $password |
||
165 | * |
||
166 | * @return array |
||
167 | */ |
||
168 | public function authenticate($username, $password) |
||
204 | |||
205 | /** |
||
206 | * Return all nodes (organizational units, containers, and domains) within the current base DN. |
||
207 | * |
||
208 | * @param boolean $cached Cache the results from AD, so that subsequent calls are faster. Enabled by default. |
||
209 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
210 | * @return array |
||
211 | */ |
||
212 | public function getNodes($cached = true, $attributes = []) |
||
230 | |||
231 | /** |
||
232 | * Return all AD groups in configured search locations, including all nested groups. |
||
233 | * Uses groups_search_locations if defined, otherwise falls back to NULL, which tells LDAPGateway |
||
234 | * to use the default baseDn defined in the connection. |
||
235 | * |
||
236 | * @param boolean $cached Cache the results from AD, so that subsequent calls are faster. Enabled by default. |
||
237 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
238 | * @param string $indexBy Attribute to use as an index. |
||
239 | * @return array |
||
240 | */ |
||
241 | public function getGroups($cached = true, $attributes = [], $indexBy = 'dn') |
||
270 | |||
271 | /** |
||
272 | * Return all member groups (and members of those, recursively) underneath a specific group DN. |
||
273 | * Note that these get cached in-memory per-request for performance to avoid re-querying for the same results. |
||
274 | * |
||
275 | * @param string $dn |
||
276 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
277 | * @return array |
||
278 | */ |
||
279 | public function getNestedGroups($dn, $attributes = []) |
||
297 | |||
298 | /** |
||
299 | * Get a particular AD group's data given a GUID. |
||
300 | * |
||
301 | * @param string $guid |
||
302 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
303 | * @return array |
||
304 | */ |
||
305 | View Code Duplication | public function getGroupByGUID($guid, $attributes = []) |
|
315 | |||
316 | /** |
||
317 | * Get a particular AD group's data given a DN. |
||
318 | * |
||
319 | * @param string $dn |
||
320 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
321 | * @return array |
||
322 | */ |
||
323 | View Code Duplication | public function getGroupByDN($dn, $attributes = []) |
|
333 | |||
334 | /** |
||
335 | * Return all AD users in configured search locations, including all users in nested groups. |
||
336 | * Uses users_search_locations if defined, otherwise falls back to NULL, which tells LDAPGateway |
||
337 | * to use the default baseDn defined in the connection. |
||
338 | * |
||
339 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
340 | * @return array |
||
341 | */ |
||
342 | public function getUsers($attributes = []) |
||
360 | |||
361 | /** |
||
362 | * Get a specific AD user's data given a GUID. |
||
363 | * |
||
364 | * @param string $guid |
||
365 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
366 | * @return array |
||
367 | */ |
||
368 | View Code Duplication | public function getUserByGUID($guid, $attributes = []) |
|
378 | |||
379 | /** |
||
380 | * Get a specific AD user's data given a DN. |
||
381 | * |
||
382 | * @param string $dn |
||
383 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
384 | * |
||
385 | * @return array |
||
386 | */ |
||
387 | View Code Duplication | public function getUserByDN($dn, $attributes = []) |
|
397 | |||
398 | /** |
||
399 | * Get a specific user's data given an email. |
||
400 | * |
||
401 | * @param string $email |
||
402 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
403 | * @return array |
||
404 | */ |
||
405 | View Code Duplication | public function getUserByEmail($email, $attributes = []) |
|
415 | |||
416 | /** |
||
417 | * Get a specific user's data given a username. |
||
418 | * |
||
419 | * @param string $username |
||
420 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
421 | * @return array |
||
422 | */ |
||
423 | View Code Duplication | public function getUserByUsername($username, $attributes = []) |
|
433 | |||
434 | /** |
||
435 | * Get a username for an email. |
||
436 | * |
||
437 | * @param string $email |
||
438 | * @return string|null |
||
439 | */ |
||
440 | public function getUsernameByEmail($email) |
||
449 | |||
450 | /** |
||
451 | * Given a group DN, get the group membership data in LDAP. |
||
452 | * |
||
453 | * @param string $dn |
||
454 | * @return array |
||
455 | */ |
||
456 | public function getLDAPGroupMembers($dn) |
||
469 | |||
470 | /** |
||
471 | * Update the current Member record with data from LDAP. |
||
472 | * |
||
473 | * Constraints: |
||
474 | * - Member *must* be in the database before calling this as it will need the ID to be mapped to a {@link Group}. |
||
475 | * - GUID of the member must have already been set, for integrity reasons we don't allow it to change here. |
||
476 | * |
||
477 | * @param Member |
||
478 | * @param array|null $data If passed, this is pre-existing AD attribute data to update the Member with. |
||
479 | * If not given, the data will be looked up by the user's GUID. |
||
480 | * @return bool |
||
481 | */ |
||
482 | public function updateMemberFromLDAP(Member $member, $data = null) |
||
653 | |||
654 | /** |
||
655 | * Sync a specific Group by updating it with LDAP data. |
||
656 | * |
||
657 | * @param Group $group An existing Group or a new Group object |
||
658 | * @param array $data LDAP group object data |
||
659 | * |
||
660 | * @return bool |
||
661 | */ |
||
662 | public function updateGroupFromLDAP(Group $group, $data) |
||
698 | |||
699 | /** |
||
700 | * Creates a new LDAP user from the passed Member record. |
||
701 | * Note that the Member record must have a non-empty Username field for this to work. |
||
702 | * |
||
703 | * @param Member $member |
||
704 | * @throws ValidationException |
||
705 | * @throws Exception |
||
706 | */ |
||
707 | public function createLDAPUser(Member $member) |
||
749 | |||
750 | /** |
||
751 | * Creates a new LDAP group from the passed Group record. |
||
752 | * |
||
753 | * @param Group $group |
||
754 | * @throws ValidationException |
||
755 | */ |
||
756 | public function createLDAPGroup(Group $group) |
||
799 | |||
800 | /** |
||
801 | * Update the Member data back to the corresponding LDAP user object. |
||
802 | * |
||
803 | * @param Member $member |
||
804 | * @throws ValidationException |
||
805 | */ |
||
806 | public function updateLDAPFromMember(Member $member) |
||
865 | |||
866 | /** |
||
867 | * Ensure the user belongs to the correct groups in LDAP from their membership |
||
868 | * to local LDAP mapped SilverStripe groups. |
||
869 | * |
||
870 | * This also removes them from LDAP groups if they've been taken out of one. |
||
871 | * It will not affect group membership of non-mapped groups, so it will |
||
872 | * not touch such internal AD groups like "Domain Users". |
||
873 | * |
||
874 | * @param Member $member |
||
875 | * @throws ValidationException |
||
876 | */ |
||
877 | public function updateLDAPGroupsForMember(Member $member) |
||
951 | |||
952 | /** |
||
953 | * Add LDAP user by DN to LDAP group. |
||
954 | * |
||
955 | * @param string $userDn |
||
956 | * @param string $groupDn |
||
957 | * @throws Exception |
||
958 | */ |
||
959 | public function addLDAPUserToGroup($userDn, $groupDn) |
||
976 | |||
977 | /** |
||
978 | * Change a members password on the AD. Works with ActiveDirectory compatible services that saves the |
||
979 | * password in the `unicodePwd` attribute. |
||
980 | * |
||
981 | * @todo Use the Zend\Ldap\Attribute::setPassword functionality to create a password in |
||
982 | * an abstract way, so it works on other LDAP directories, not just Active Directory. |
||
983 | * |
||
984 | * Ensure that the LDAP bind:ed user can change passwords and that the connection is secure. |
||
985 | * |
||
986 | * @param Member $member |
||
987 | * @param string $password |
||
988 | * @param string|null $oldPassword Supply old password to perform a password change (as opposed to password reset) |
||
989 | * @return ValidationResult |
||
990 | */ |
||
991 | public function setPassword(Member $member, $password, $oldPassword = null) |
||
1034 | |||
1035 | /** |
||
1036 | * Delete an LDAP user mapped to the Member record |
||
1037 | * @param Member $member |
||
1038 | * @throws ValidationException |
||
1039 | */ |
||
1040 | public function deleteLDAPMember(Member $member) |
||
1059 | |||
1060 | /** |
||
1061 | * A simple proxy to LDAP update operation. |
||
1062 | * |
||
1063 | * @param string $dn Location to add the entry at. |
||
1064 | * @param array $attributes A simple associative array of attributes. |
||
1065 | */ |
||
1066 | public function update($dn, array $attributes) |
||
1070 | |||
1071 | /** |
||
1072 | * A simple proxy to LDAP delete operation. |
||
1073 | * |
||
1074 | * @param string $dn Location of object to delete |
||
1075 | * @param bool $recursively Recursively delete nested objects? |
||
1076 | */ |
||
1077 | public function delete($dn, $recursively = false) |
||
1081 | |||
1082 | /** |
||
1083 | * A simple proxy to LDAP copy/delete operation. |
||
1084 | * |
||
1085 | * @param string $fromDn |
||
1086 | * @param string $toDn |
||
1087 | * @param bool $recursively Recursively move nested objects? |
||
1088 | */ |
||
1089 | public function move($fromDn, $toDn, $recursively = false) |
||
1093 | |||
1094 | /** |
||
1095 | * A simple proxy to LDAP add operation. |
||
1096 | * |
||
1097 | * @param string $dn Location to add the entry at. |
||
1098 | * @param array $attributes A simple associative array of attributes. |
||
1099 | */ |
||
1100 | public function add($dn, array $attributes) |
||
1104 | |||
1105 | /** |
||
1106 | * @param string $dn Distinguished name of the user |
||
1107 | * @param string $password New password. |
||
1108 | * @throws Exception |
||
1109 | */ |
||
1110 | private function passwordHistoryWorkaround($dn, $password) |
||
1118 | |||
1119 | /** |
||
1120 | * Get a logger |
||
1121 | * |
||
1122 | * @return LoggerInterface |
||
1123 | */ |
||
1124 | public function getLogger() |
||
1128 | } |
||
1129 |
This check marks private properties in classes that are never used. Those properties can be removed.