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 | * @var array |
||
50 | */ |
||
51 | private static $_cache_nested_groups = []; |
||
52 | |||
53 | /** |
||
54 | * If this is configured to a "Code" value of a {@link Group} in SilverStripe, the user will always |
||
55 | * be added to this group's membership when imported, regardless of any sort of group mappings. |
||
56 | * |
||
57 | * @var string |
||
58 | * @config |
||
59 | */ |
||
60 | private static $default_group; |
||
61 | |||
62 | /** |
||
63 | * For samba4 directory, there is no way to enforce password history on password resets. |
||
64 | * This only happens with changePassword (which requires the old password). |
||
65 | * This works around it by making the old password up and setting it administratively. |
||
66 | * |
||
67 | * A cleaner fix would be to use the LDAP_SERVER_POLICY_HINTS_OID connection flag, |
||
68 | * but it's not implemented in samba https://bugzilla.samba.org/show_bug.cgi?id=12020 |
||
69 | * |
||
70 | * @var bool |
||
71 | */ |
||
72 | private static $password_history_workaround = false; |
||
73 | |||
74 | /** |
||
75 | * Get the cache objecgt used for LDAP results. Note that the default lifetime set here |
||
76 | * is 8 hours, but you can change that by calling SS_Cache::set_lifetime('ldap', <lifetime in seconds>) |
||
77 | * |
||
78 | * @return Zend_Cache_Frontend |
||
79 | */ |
||
80 | public static function get_cache() |
||
87 | |||
88 | /** |
||
89 | * Flushes out the LDAP results cache when flush=1 is called. |
||
90 | */ |
||
91 | public static function flush() |
||
96 | |||
97 | /** |
||
98 | * @var LDAPGateway |
||
99 | */ |
||
100 | public $gateway; |
||
101 | |||
102 | /** |
||
103 | * Setter for gateway. Useful for overriding the gateway with a fake for testing. |
||
104 | * @var LDAPGateway |
||
105 | */ |
||
106 | public function setGateway($gateway) |
||
110 | |||
111 | /** |
||
112 | * Checkes whether or not the service is enabled. |
||
113 | * |
||
114 | * @return bool |
||
115 | */ |
||
116 | public function enabled() |
||
121 | |||
122 | /** |
||
123 | * Authenticate the given username and password with LDAP. |
||
124 | * |
||
125 | * @param string $username |
||
126 | * @param string $password |
||
127 | * |
||
128 | * @return array |
||
129 | */ |
||
130 | public function authenticate($username, $password) |
||
166 | |||
167 | /** |
||
168 | * Return all nodes (organizational units, containers, and domains) within the current base DN. |
||
169 | * |
||
170 | * @param boolean $cached Cache the results from AD, so that subsequent calls are faster. Enabled by default. |
||
171 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
172 | * @return array |
||
173 | */ |
||
174 | public function getNodes($cached = true, $attributes = []) |
||
191 | |||
192 | /** |
||
193 | * Return all AD groups in configured search locations, including all nested groups. |
||
194 | * Uses groups_search_locations if defined, otherwise falls back to NULL, which tells LDAPGateway |
||
195 | * to use the default baseDn defined in the connection. |
||
196 | * |
||
197 | * @param boolean $cached Cache the results from AD, so that subsequent calls are faster. Enabled by default. |
||
198 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
199 | * @param string $indexBy Attribute to use as an index. |
||
200 | * @return array |
||
201 | */ |
||
202 | public function getGroups($cached = true, $attributes = [], $indexBy = 'dn') |
||
226 | |||
227 | /** |
||
228 | * Return all member groups (and members of those, recursively) underneath a specific group DN. |
||
229 | * Note that these get cached in-memory per-request for performance to avoid re-querying for the same results. |
||
230 | * |
||
231 | * @param string $dn |
||
232 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
233 | * @return array |
||
234 | */ |
||
235 | public function getNestedGroups($dn, $attributes = []) |
||
253 | |||
254 | /** |
||
255 | * Get a particular AD group's data given a GUID. |
||
256 | * |
||
257 | * @param string $guid |
||
258 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
259 | * @return array |
||
260 | */ |
||
261 | View Code Duplication | public function getGroupByGUID($guid, $attributes = []) |
|
271 | |||
272 | /** |
||
273 | * Get a particular AD group's data given a DN. |
||
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 | View Code Duplication | public function getGroupByDN($dn, $attributes = []) |
|
289 | |||
290 | /** |
||
291 | * Return all AD users in configured search locations, including all users in nested groups. |
||
292 | * Uses users_search_locations if defined, otherwise falls back to NULL, which tells LDAPGateway |
||
293 | * to use the default baseDn defined in the connection. |
||
294 | * |
||
295 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
296 | * @return array |
||
297 | */ |
||
298 | public function getUsers($attributes = []) |
||
316 | |||
317 | /** |
||
318 | * Get a specific AD user's data given a GUID. |
||
319 | * |
||
320 | * @param string $guid |
||
321 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
322 | * @return array |
||
323 | */ |
||
324 | View Code Duplication | public function getUserByGUID($guid, $attributes = []) |
|
334 | |||
335 | /** |
||
336 | * Get a specific AD user's data given a DN. |
||
337 | * |
||
338 | * @param string $dn |
||
339 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
340 | * |
||
341 | * @return array |
||
342 | */ |
||
343 | View Code Duplication | public function getUserByDN($dn, $attributes = []) |
|
353 | |||
354 | /** |
||
355 | * Get a specific user's data given an email. |
||
356 | * |
||
357 | * @param string $email |
||
358 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
359 | * @return array |
||
360 | */ |
||
361 | View Code Duplication | public function getUserByEmail($email, $attributes = []) |
|
371 | |||
372 | /** |
||
373 | * Get a specific user's data given a username. |
||
374 | * |
||
375 | * @param string $username |
||
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 getUserByUsername($username, $attributes = []) |
|
389 | |||
390 | /** |
||
391 | * Get a username for an email. |
||
392 | * |
||
393 | * @param string $email |
||
394 | * @return string|null |
||
395 | */ |
||
396 | public function getUsernameByEmail($email) |
||
405 | |||
406 | /** |
||
407 | * Given a group DN, get the group membership data in LDAP. |
||
408 | * |
||
409 | * @param string $dn |
||
410 | * @return array |
||
411 | */ |
||
412 | public function getLDAPGroupMembers($dn) |
||
425 | |||
426 | /** |
||
427 | * Update the current Member record with data from LDAP. |
||
428 | * |
||
429 | * Constraints: |
||
430 | * - Member *must* be in the database before calling this as it will need the ID to be mapped to a {@link Group}. |
||
431 | * - GUID of the member must have already been set, for integrity reasons we don't allow it to change here. |
||
432 | * |
||
433 | * @param Member |
||
434 | * @param array|null $data If passed, this is pre-existing AD attribute data to update the Member with. |
||
435 | * If not given, the data will be looked up by the user's GUID. |
||
436 | * @return bool |
||
437 | */ |
||
438 | public function updateMemberFromLDAP(Member $member, $data = null) |
||
599 | |||
600 | /** |
||
601 | * Sync a specific Group by updating it with LDAP data. |
||
602 | * |
||
603 | * @param Group $group An existing Group or a new Group object |
||
604 | * @param array $data LDAP group object data |
||
605 | * |
||
606 | * @return bool |
||
607 | */ |
||
608 | public function updateGroupFromLDAP(Group $group, $data) |
||
648 | |||
649 | /** |
||
650 | * Creates a new LDAP user from the passed Member record. |
||
651 | * Note that the Member record must have a non-empty Username field for this to work. |
||
652 | * |
||
653 | * @param Member $member |
||
654 | */ |
||
655 | public function createLDAPUser(Member $member) |
||
697 | |||
698 | /** |
||
699 | * Update the Member data back to the corresponding LDAP user object. |
||
700 | * |
||
701 | * @param Member $member |
||
702 | * @throws ValidationException |
||
703 | */ |
||
704 | public function updateLDAPFromMember(Member $member) |
||
763 | |||
764 | /** |
||
765 | * Ensure the user belongs to the correct groups in LDAP from their membership |
||
766 | * to local LDAP mapped SilverStripe groups. |
||
767 | * |
||
768 | * This also removes them from LDAP groups if they've been taken out of one. |
||
769 | * It will not affect group membership of non-mapped groups, so it will |
||
770 | * not touch such internal AD groups like "Domain Users". |
||
771 | * |
||
772 | * @param Member $member |
||
773 | */ |
||
774 | public function updateLDAPGroupsForMember(Member $member) |
||
861 | |||
862 | /** |
||
863 | * Change a members password on the AD. Works with ActiveDirectory compatible services that saves the |
||
864 | * password in the `unicodePwd` attribute. |
||
865 | * |
||
866 | * @todo Use the Zend\Ldap\Attribute::setPassword functionality to create a password in |
||
867 | * an abstract way, so it works on other LDAP directories, not just Active Directory. |
||
868 | * |
||
869 | * Ensure that the LDAP bind:ed user can change passwords and that the connection is secure. |
||
870 | * |
||
871 | * @param Member $member |
||
872 | * @param string $password |
||
873 | * @param string|null $oldPassword Supply old password to perform a password change (as opposed to password reset) |
||
874 | * @return ValidationResult |
||
875 | * @throws Exception |
||
876 | */ |
||
877 | public function setPassword(Member $member, $password, $oldPassword = null) |
||
910 | |||
911 | /** |
||
912 | * Delete an LDAP user mapped to the Member record |
||
913 | * @param Member $member |
||
914 | */ |
||
915 | public function deleteLDAPMember(Member $member) { |
||
933 | |||
934 | /** |
||
935 | * A simple proxy to LDAP update operation. |
||
936 | * |
||
937 | * @param string $dn Location to add the entry at. |
||
938 | * @param array $attributes A simple associative array of attributes. |
||
939 | */ |
||
940 | public function update($dn, array $attributes) |
||
944 | |||
945 | /** |
||
946 | * A simple proxy to LDAP delete operation. |
||
947 | * |
||
948 | * @param string $dn Location of object to delete |
||
949 | * @param bool $recursively Recursively delete nested objects? |
||
950 | */ |
||
951 | public function delete($dn, $recursively = false) |
||
955 | |||
956 | /** |
||
957 | * A simple proxy to LDAP copy/delete operation. |
||
958 | * |
||
959 | * @param string $fromDn |
||
960 | * @param string $toDn |
||
961 | * @param bool $recursively Recursively move nested objects? |
||
962 | */ |
||
963 | public function move($fromDn, $toDn, $recursively = false) |
||
967 | |||
968 | /** |
||
969 | * A simple proxy to LDAP add operation. |
||
970 | * |
||
971 | * @param string $dn Location to add the entry at. |
||
972 | * @param array $attributes A simple associative array of attributes. |
||
973 | */ |
||
974 | public function add($dn, array $attributes) |
||
978 | |||
979 | /** |
||
980 | * @param string $dn Distinguished name of the user |
||
981 | * @param string $password New password. |
||
982 | * @throws Exception |
||
983 | */ |
||
984 | private function passwordHistoryWorkaround($dn, $password) { |
||
991 | |||
992 | } |
||
993 |
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.