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) |
||
215 | |||
216 | /** |
||
217 | * Return all nodes (organizational units, containers, and domains) within the current base DN. |
||
218 | * |
||
219 | * @param boolean $cached Cache the results from AD, so that subsequent calls are faster. Enabled by default. |
||
220 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
221 | * @return array |
||
222 | */ |
||
223 | public function getNodes($cached = true, $attributes = []) |
||
241 | |||
242 | /** |
||
243 | * Return all AD groups in configured search locations, including all nested groups. |
||
244 | * Uses groups_search_locations if defined, otherwise falls back to NULL, which tells LDAPGateway |
||
245 | * to use the default baseDn defined in the connection. |
||
246 | * |
||
247 | * @param boolean $cached Cache the results from AD, so that subsequent calls are faster. Enabled by default. |
||
248 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
249 | * @param string $indexBy Attribute to use as an index. |
||
250 | * @return array |
||
251 | */ |
||
252 | public function getGroups($cached = true, $attributes = [], $indexBy = 'dn') |
||
281 | |||
282 | /** |
||
283 | * Return all member groups (and members of those, recursively) underneath a specific group DN. |
||
284 | * Note that these get cached in-memory per-request for performance to avoid re-querying for the same results. |
||
285 | * |
||
286 | * @param string $dn |
||
287 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
288 | * @return array |
||
289 | */ |
||
290 | public function getNestedGroups($dn, $attributes = []) |
||
308 | |||
309 | /** |
||
310 | * Get a particular AD group's data given a GUID. |
||
311 | * |
||
312 | * @param string $guid |
||
313 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
314 | * @return array |
||
315 | */ |
||
316 | View Code Duplication | public function getGroupByGUID($guid, $attributes = []) |
|
326 | |||
327 | /** |
||
328 | * Get a particular AD group's data given a DN. |
||
329 | * |
||
330 | * @param string $dn |
||
331 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
332 | * @return array |
||
333 | */ |
||
334 | View Code Duplication | public function getGroupByDN($dn, $attributes = []) |
|
344 | |||
345 | /** |
||
346 | * Return all AD users in configured search locations, including all users in nested groups. |
||
347 | * Uses users_search_locations if defined, otherwise falls back to NULL, which tells LDAPGateway |
||
348 | * to use the default baseDn defined in the connection. |
||
349 | * |
||
350 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
351 | * @return array |
||
352 | */ |
||
353 | public function getUsers($attributes = []) |
||
371 | |||
372 | /** |
||
373 | * Get a specific AD user's data given a GUID. |
||
374 | * |
||
375 | * @param string $guid |
||
376 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
377 | * @return array |
||
378 | */ |
||
379 | View Code Duplication | public function getUserByGUID($guid, $attributes = []) |
|
389 | |||
390 | /** |
||
391 | * Get a specific AD user's data given a DN. |
||
392 | * |
||
393 | * @param string $dn |
||
394 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
395 | * |
||
396 | * @return array |
||
397 | */ |
||
398 | View Code Duplication | public function getUserByDN($dn, $attributes = []) |
|
408 | |||
409 | /** |
||
410 | * Get a specific user's data given an email. |
||
411 | * |
||
412 | * @param string $email |
||
413 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
414 | * @return array |
||
415 | */ |
||
416 | View Code Duplication | public function getUserByEmail($email, $attributes = []) |
|
426 | |||
427 | /** |
||
428 | * Get a specific user's data given a username. |
||
429 | * |
||
430 | * @param string $username |
||
431 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
432 | * @return array |
||
433 | */ |
||
434 | View Code Duplication | public function getUserByUsername($username, $attributes = []) |
|
444 | |||
445 | /** |
||
446 | * Get a username for an email. |
||
447 | * |
||
448 | * @param string $email |
||
449 | * @return string|null |
||
450 | */ |
||
451 | public function getUsernameByEmail($email) |
||
460 | |||
461 | /** |
||
462 | * Given a group DN, get the group membership data in LDAP. |
||
463 | * |
||
464 | * @param string $dn |
||
465 | * @return array |
||
466 | */ |
||
467 | public function getLDAPGroupMembers($dn) |
||
480 | |||
481 | /** |
||
482 | * Update the current Member record with data from LDAP. |
||
483 | * |
||
484 | * It's allowed to pass an unwritten Member record here, because it's not always possible to satisfy |
||
485 | * field constraints without importing data from LDAP (for example if the application requires Username |
||
486 | * through a Validator). Even though unwritten, it still must have the GUID set. |
||
487 | * |
||
488 | * Constraints: |
||
489 | * - GUID of the member must have already been set, for integrity reasons we don't allow it to change here. |
||
490 | * |
||
491 | * @param Member |
||
492 | * @param array|null $data If passed, this is pre-existing AD attribute data to update the Member with. |
||
493 | * If not given, the data will be looked up by the user's GUID. |
||
494 | * @return bool |
||
495 | */ |
||
496 | public function updateMemberFromLDAP(Member $member, $data = null) |
||
670 | |||
671 | /** |
||
672 | * Sync a specific Group by updating it with LDAP data. |
||
673 | * |
||
674 | * @param Group $group An existing Group or a new Group object |
||
675 | * @param array $data LDAP group object data |
||
676 | * |
||
677 | * @return bool |
||
678 | */ |
||
679 | public function updateGroupFromLDAP(Group $group, $data) |
||
715 | |||
716 | /** |
||
717 | * Creates a new LDAP user from the passed Member record. |
||
718 | * Note that the Member record must have a non-empty Username field for this to work. |
||
719 | * |
||
720 | * @param Member $member |
||
721 | * @throws ValidationException |
||
722 | * @throws Exception |
||
723 | */ |
||
724 | public function createLDAPUser(Member $member) |
||
766 | |||
767 | /** |
||
768 | * Creates a new LDAP group from the passed Group record. |
||
769 | * |
||
770 | * @param Group $group |
||
771 | * @throws ValidationException |
||
772 | */ |
||
773 | public function createLDAPGroup(Group $group) |
||
816 | |||
817 | /** |
||
818 | * Update the Member data back to the corresponding LDAP user object. |
||
819 | * |
||
820 | * @param Member $member |
||
821 | * @throws ValidationException |
||
822 | */ |
||
823 | public function updateLDAPFromMember(Member $member) |
||
882 | |||
883 | /** |
||
884 | * Ensure the user belongs to the correct groups in LDAP from their membership |
||
885 | * to local LDAP mapped SilverStripe groups. |
||
886 | * |
||
887 | * This also removes them from LDAP groups if they've been taken out of one. |
||
888 | * It will not affect group membership of non-mapped groups, so it will |
||
889 | * not touch such internal AD groups like "Domain Users". |
||
890 | * |
||
891 | * @param Member $member |
||
892 | * @throws ValidationException |
||
893 | */ |
||
894 | public function updateLDAPGroupsForMember(Member $member) |
||
968 | |||
969 | /** |
||
970 | * Add LDAP user by DN to LDAP group. |
||
971 | * |
||
972 | * @param string $userDn |
||
973 | * @param string $groupDn |
||
974 | * @throws Exception |
||
975 | */ |
||
976 | public function addLDAPUserToGroup($userDn, $groupDn) |
||
993 | |||
994 | /** |
||
995 | * Change a members password on the AD. Works with ActiveDirectory compatible services that saves the |
||
996 | * password in the `unicodePwd` attribute. |
||
997 | * |
||
998 | * @todo Use the Zend\Ldap\Attribute::setPassword functionality to create a password in |
||
999 | * an abstract way, so it works on other LDAP directories, not just Active Directory. |
||
1000 | * |
||
1001 | * Ensure that the LDAP bind:ed user can change passwords and that the connection is secure. |
||
1002 | * |
||
1003 | * @param Member $member |
||
1004 | * @param string $password |
||
1005 | * @param string|null $oldPassword Supply old password to perform a password change (as opposed to password reset) |
||
1006 | * @return ValidationResult |
||
1007 | */ |
||
1008 | public function setPassword(Member $member, $password, $oldPassword = null) |
||
1051 | |||
1052 | /** |
||
1053 | * Delete an LDAP user mapped to the Member record |
||
1054 | * @param Member $member |
||
1055 | * @throws ValidationException |
||
1056 | */ |
||
1057 | public function deleteLDAPMember(Member $member) |
||
1076 | |||
1077 | /** |
||
1078 | * A simple proxy to LDAP update operation. |
||
1079 | * |
||
1080 | * @param string $dn Location to add the entry at. |
||
1081 | * @param array $attributes A simple associative array of attributes. |
||
1082 | */ |
||
1083 | public function update($dn, array $attributes) |
||
1087 | |||
1088 | /** |
||
1089 | * A simple proxy to LDAP delete operation. |
||
1090 | * |
||
1091 | * @param string $dn Location of object to delete |
||
1092 | * @param bool $recursively Recursively delete nested objects? |
||
1093 | */ |
||
1094 | public function delete($dn, $recursively = false) |
||
1098 | |||
1099 | /** |
||
1100 | * A simple proxy to LDAP copy/delete operation. |
||
1101 | * |
||
1102 | * @param string $fromDn |
||
1103 | * @param string $toDn |
||
1104 | * @param bool $recursively Recursively move nested objects? |
||
1105 | */ |
||
1106 | public function move($fromDn, $toDn, $recursively = false) |
||
1110 | |||
1111 | /** |
||
1112 | * A simple proxy to LDAP add operation. |
||
1113 | * |
||
1114 | * @param string $dn Location to add the entry at. |
||
1115 | * @param array $attributes A simple associative array of attributes. |
||
1116 | */ |
||
1117 | public function add($dn, array $attributes) |
||
1121 | |||
1122 | /** |
||
1123 | * @param string $dn Distinguished name of the user |
||
1124 | * @param string $password New password. |
||
1125 | * @throws Exception |
||
1126 | */ |
||
1127 | private function passwordHistoryWorkaround($dn, $password) |
||
1135 | |||
1136 | /** |
||
1137 | * Get a logger |
||
1138 | * |
||
1139 | * @return LoggerInterface |
||
1140 | */ |
||
1141 | public function getLogger() |
||
1145 | } |
||
1146 |
This check marks private properties in classes that are never used. Those properties can be removed.