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 |
||
42 | class LDAPService implements Flushable |
||
43 | { |
||
44 | use Injectable; |
||
45 | use Extensible; |
||
46 | use Configurable; |
||
47 | |||
48 | /** |
||
49 | * @var array |
||
50 | */ |
||
51 | private static $dependencies = [ |
||
|
|||
52 | 'gateway' => '%$' . LDAPGateway::class |
||
53 | ]; |
||
54 | |||
55 | /** |
||
56 | * If configured, only user objects within these locations will be exposed to this service. |
||
57 | * |
||
58 | * @var array |
||
59 | * @config |
||
60 | */ |
||
61 | private static $users_search_locations = []; |
||
62 | |||
63 | /** |
||
64 | * If configured, only group objects within these locations will be exposed to this service. |
||
65 | * @var array |
||
66 | * |
||
67 | * @config |
||
68 | */ |
||
69 | private static $groups_search_locations = []; |
||
70 | |||
71 | /** |
||
72 | * Location to create new users in (distinguished name). |
||
73 | * @var string |
||
74 | * |
||
75 | * @config |
||
76 | */ |
||
77 | private static $new_users_dn; |
||
78 | |||
79 | /** |
||
80 | * Location to create new groups in (distinguished name). |
||
81 | * @var string |
||
82 | * |
||
83 | * @config |
||
84 | */ |
||
85 | private static $new_groups_dn; |
||
86 | |||
87 | /** |
||
88 | * @var array |
||
89 | */ |
||
90 | private static $_cache_nested_groups = []; |
||
91 | |||
92 | /** |
||
93 | * If this is configured to a "Code" value of a {@link Group} in SilverStripe, the user will always |
||
94 | * be added to this group's membership when imported, regardless of any sort of group mappings. |
||
95 | * |
||
96 | * @var string |
||
97 | * @config |
||
98 | */ |
||
99 | private static $default_group; |
||
100 | |||
101 | /** |
||
102 | * For samba4 directory, there is no way to enforce password history on password resets. |
||
103 | * This only happens with changePassword (which requires the old password). |
||
104 | * This works around it by making the old password up and setting it administratively. |
||
105 | * |
||
106 | * A cleaner fix would be to use the LDAP_SERVER_POLICY_HINTS_OID connection flag, |
||
107 | * but it's not implemented in samba https://bugzilla.samba.org/show_bug.cgi?id=12020 |
||
108 | * |
||
109 | * @var bool |
||
110 | */ |
||
111 | private static $password_history_workaround = false; |
||
112 | |||
113 | /** |
||
114 | * Get the cache object used for LDAP results. Note that the default lifetime set here |
||
115 | * is 8 hours, but you can change that by adding configuration: |
||
116 | * |
||
117 | * <code> |
||
118 | * SilverStripe\Core\Injector\Injector: |
||
119 | * Psr\SimpleCache\CacheInterface.ldap: |
||
120 | * constructor: |
||
121 | * defaultLifetime: 3600 # time in seconds |
||
122 | * </code> |
||
123 | * |
||
124 | * @return Psr\SimpleCache\CacheInterface |
||
125 | */ |
||
126 | public static function get_cache() |
||
130 | |||
131 | /** |
||
132 | * Flushes out the LDAP results cache when flush=1 is called. |
||
133 | */ |
||
134 | public static function flush() |
||
140 | |||
141 | /** |
||
142 | * @var LDAPGateway |
||
143 | */ |
||
144 | public $gateway; |
||
145 | |||
146 | public function __construct() |
||
150 | |||
151 | /** |
||
152 | * Setter for gateway. Useful for overriding the gateway with a fake for testing. |
||
153 | * @var LDAPGateway |
||
154 | */ |
||
155 | public function setGateway($gateway) |
||
159 | |||
160 | /** |
||
161 | * Checkes whether or not the service is enabled. |
||
162 | * |
||
163 | * @return bool |
||
164 | */ |
||
165 | public function enabled() |
||
170 | |||
171 | /** |
||
172 | * Authenticate the given username and password with LDAP. |
||
173 | * |
||
174 | * @param string $username |
||
175 | * @param string $password |
||
176 | * |
||
177 | * @return array |
||
178 | */ |
||
179 | public function authenticate($username, $password) |
||
216 | |||
217 | /** |
||
218 | * Return all nodes (organizational units, containers, and domains) within the current base DN. |
||
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 | * @return array |
||
223 | */ |
||
224 | public function getNodes($cached = true, $attributes = []) |
||
242 | |||
243 | /** |
||
244 | * Return all AD groups in configured search locations, including all nested groups. |
||
245 | * Uses groups_search_locations if defined, otherwise falls back to NULL, which tells LDAPGateway |
||
246 | * to use the default baseDn defined in the connection. |
||
247 | * |
||
248 | * @param boolean $cached Cache the results from AD, so that subsequent calls are faster. Enabled by default. |
||
249 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
250 | * @param string $indexBy Attribute to use as an index. |
||
251 | * @return array |
||
252 | */ |
||
253 | public function getGroups($cached = true, $attributes = [], $indexBy = 'dn') |
||
282 | |||
283 | /** |
||
284 | * Return all member groups (and members of those, recursively) underneath a specific group DN. |
||
285 | * Note that these get cached in-memory per-request for performance to avoid re-querying for the same results. |
||
286 | * |
||
287 | * @param string $dn |
||
288 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
289 | * @return array |
||
290 | */ |
||
291 | public function getNestedGroups($dn, $attributes = []) |
||
309 | |||
310 | /** |
||
311 | * Get a particular AD group'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 getGroupByGUID($guid, $attributes = []) |
|
327 | |||
328 | /** |
||
329 | * Get a particular AD group'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 | * @return array |
||
334 | */ |
||
335 | View Code Duplication | public function getGroupByDN($dn, $attributes = []) |
|
345 | |||
346 | /** |
||
347 | * Return all AD users in configured search locations, including all users in nested groups. |
||
348 | * Uses users_search_locations if defined, otherwise falls back to NULL, which tells LDAPGateway |
||
349 | * to use the default baseDn defined in the connection. |
||
350 | * |
||
351 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
352 | * @return array |
||
353 | */ |
||
354 | public function getUsers($attributes = []) |
||
372 | |||
373 | /** |
||
374 | * Get a specific AD user's data given a GUID. |
||
375 | * |
||
376 | * @param string $guid |
||
377 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
378 | * @return array |
||
379 | */ |
||
380 | View Code Duplication | public function getUserByGUID($guid, $attributes = []) |
|
390 | |||
391 | /** |
||
392 | * Get a specific AD user's data given a DN. |
||
393 | * |
||
394 | * @param string $dn |
||
395 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
396 | * |
||
397 | * @return array |
||
398 | */ |
||
399 | View Code Duplication | public function getUserByDN($dn, $attributes = []) |
|
409 | |||
410 | /** |
||
411 | * Get a specific user's data given an email. |
||
412 | * |
||
413 | * @param string $email |
||
414 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
415 | * @return array |
||
416 | */ |
||
417 | View Code Duplication | public function getUserByEmail($email, $attributes = []) |
|
427 | |||
428 | /** |
||
429 | * Get a specific user's data given a username. |
||
430 | * |
||
431 | * @param string $username |
||
432 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
433 | * @return array |
||
434 | */ |
||
435 | View Code Duplication | public function getUserByUsername($username, $attributes = []) |
|
450 | |||
451 | /** |
||
452 | * Get a username for an email. |
||
453 | * |
||
454 | * @param string $email |
||
455 | * @return string|null |
||
456 | */ |
||
457 | public function getUsernameByEmail($email) |
||
466 | |||
467 | /** |
||
468 | * Given a group DN, get the group membership data in LDAP. |
||
469 | * |
||
470 | * @param string $dn |
||
471 | * @return array |
||
472 | */ |
||
473 | public function getLDAPGroupMembers($dn) |
||
486 | |||
487 | /** |
||
488 | * Update the current Member record with data from LDAP. |
||
489 | * |
||
490 | * It's allowed to pass an unwritten Member record here, because it's not always possible to satisfy |
||
491 | * field constraints without importing data from LDAP (for example if the application requires Username |
||
492 | * through a Validator). Even though unwritten, it still must have the GUID set. |
||
493 | * |
||
494 | * Constraints: |
||
495 | * - GUID of the member must have already been set, for integrity reasons we don't allow it to change here. |
||
496 | * |
||
497 | * @param Member $member |
||
498 | * @param array|null $data If passed, this is pre-existing AD attribute data to update the Member with. |
||
499 | * If not given, the data will be looked up by the user's GUID. |
||
500 | * @param bool $updateGroups controls whether to run the resource-intensive group update function as well. This is |
||
501 | * skipped during login to reduce load. |
||
502 | * @return bool |
||
503 | * @internal param $Member |
||
504 | */ |
||
505 | public function updateMemberFromLDAP(Member $member, $data = null, $updateGroups = true) |
||
619 | |||
620 | /** |
||
621 | * Ensure the user is mapped to any applicable groups. |
||
622 | * @param array $data |
||
623 | * @param Member $member |
||
624 | */ |
||
625 | public function updateMemberGroups($data, Member $member) |
||
695 | |||
696 | /** |
||
697 | * Sync a specific Group by updating it with LDAP data. |
||
698 | * |
||
699 | * @param Group $group An existing Group or a new Group object |
||
700 | * @param array $data LDAP group object data |
||
701 | * |
||
702 | * @return bool |
||
703 | */ |
||
704 | public function updateGroupFromLDAP(Group $group, $data) |
||
740 | |||
741 | /** |
||
742 | * Creates a new LDAP user from the passed Member record. |
||
743 | * Note that the Member record must have a non-empty Username field for this to work. |
||
744 | * |
||
745 | * @param Member $member |
||
746 | * @throws ValidationException |
||
747 | * @throws Exception |
||
748 | */ |
||
749 | public function createLDAPUser(Member $member) |
||
791 | |||
792 | /** |
||
793 | * Creates a new LDAP group from the passed Group record. |
||
794 | * |
||
795 | * @param Group $group |
||
796 | * @throws ValidationException |
||
797 | */ |
||
798 | public function createLDAPGroup(Group $group) |
||
841 | |||
842 | /** |
||
843 | * Update the Member data back to the corresponding LDAP user object. |
||
844 | * |
||
845 | * @param Member $member |
||
846 | * @throws ValidationException |
||
847 | */ |
||
848 | public function updateLDAPFromMember(Member $member) |
||
907 | |||
908 | /** |
||
909 | * Ensure the user belongs to the correct groups in LDAP from their membership |
||
910 | * to local LDAP mapped SilverStripe groups. |
||
911 | * |
||
912 | * This also removes them from LDAP groups if they've been taken out of one. |
||
913 | * It will not affect group membership of non-mapped groups, so it will |
||
914 | * not touch such internal AD groups like "Domain Users". |
||
915 | * |
||
916 | * @param Member $member |
||
917 | * @throws ValidationException |
||
918 | */ |
||
919 | public function updateLDAPGroupsForMember(Member $member) |
||
993 | |||
994 | /** |
||
995 | * Add LDAP user by DN to LDAP group. |
||
996 | * |
||
997 | * @param string $userDn |
||
998 | * @param string $groupDn |
||
999 | * @throws Exception |
||
1000 | */ |
||
1001 | public function addLDAPUserToGroup($userDn, $groupDn) |
||
1018 | |||
1019 | /** |
||
1020 | * Change a members password on the AD. Works with ActiveDirectory compatible services that saves the |
||
1021 | * password in the `unicodePwd` attribute. |
||
1022 | * |
||
1023 | * @todo Use the Zend\Ldap\Attribute::setPassword functionality to create a password in |
||
1024 | * an abstract way, so it works on other LDAP directories, not just Active Directory. |
||
1025 | * |
||
1026 | * Ensure that the LDAP bind:ed user can change passwords and that the connection is secure. |
||
1027 | * |
||
1028 | * @param Member $member |
||
1029 | * @param string $password |
||
1030 | * @param string|null $oldPassword Supply old password to perform a password change (as opposed to password reset) |
||
1031 | * @return ValidationResult |
||
1032 | */ |
||
1033 | public function setPassword(Member $member, $password, $oldPassword = null) |
||
1076 | |||
1077 | /** |
||
1078 | * Delete an LDAP user mapped to the Member record |
||
1079 | * @param Member $member |
||
1080 | * @throws ValidationException |
||
1081 | */ |
||
1082 | public function deleteLDAPMember(Member $member) |
||
1101 | |||
1102 | /** |
||
1103 | * A simple proxy to LDAP update operation. |
||
1104 | * |
||
1105 | * @param string $dn Location to add the entry at. |
||
1106 | * @param array $attributes A simple associative array of attributes. |
||
1107 | */ |
||
1108 | public function update($dn, array $attributes) |
||
1112 | |||
1113 | /** |
||
1114 | * A simple proxy to LDAP delete operation. |
||
1115 | * |
||
1116 | * @param string $dn Location of object to delete |
||
1117 | * @param bool $recursively Recursively delete nested objects? |
||
1118 | */ |
||
1119 | public function delete($dn, $recursively = false) |
||
1123 | |||
1124 | /** |
||
1125 | * A simple proxy to LDAP copy/delete operation. |
||
1126 | * |
||
1127 | * @param string $fromDn |
||
1128 | * @param string $toDn |
||
1129 | * @param bool $recursively Recursively move nested objects? |
||
1130 | */ |
||
1131 | public function move($fromDn, $toDn, $recursively = false) |
||
1135 | |||
1136 | /** |
||
1137 | * A simple proxy to LDAP add operation. |
||
1138 | * |
||
1139 | * @param string $dn Location to add the entry at. |
||
1140 | * @param array $attributes A simple associative array of attributes. |
||
1141 | */ |
||
1142 | public function add($dn, array $attributes) |
||
1146 | |||
1147 | /** |
||
1148 | * @param string $dn Distinguished name of the user |
||
1149 | * @param string $password New password. |
||
1150 | * @throws Exception |
||
1151 | */ |
||
1152 | private function passwordHistoryWorkaround($dn, $password) |
||
1160 | |||
1161 | /** |
||
1162 | * Get a logger |
||
1163 | * |
||
1164 | * @return LoggerInterface |
||
1165 | */ |
||
1166 | public function getLogger() |
||
1170 | } |
||
1171 |
This check marks private properties in classes that are never used. Those properties can be removed.