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 |
||
41 | class LDAPService implements Flushable |
||
42 | { |
||
43 | use Injectable; |
||
44 | use Extensible; |
||
45 | |||
46 | /** |
||
47 | * @var array |
||
48 | */ |
||
49 | private static $dependencies = [ |
||
|
|||
50 | 'gateway' => '%$' . LDAPGateway::class |
||
51 | ]; |
||
52 | |||
53 | /** |
||
54 | * If configured, only user objects within these locations will be exposed to this service. |
||
55 | * |
||
56 | * @var array |
||
57 | * @config |
||
58 | */ |
||
59 | private static $users_search_locations = []; |
||
60 | |||
61 | /** |
||
62 | * If configured, only group objects within these locations will be exposed to this service. |
||
63 | * @var array |
||
64 | * |
||
65 | * @config |
||
66 | */ |
||
67 | private static $groups_search_locations = []; |
||
68 | |||
69 | /** |
||
70 | * Location to create new users in (distinguished name). |
||
71 | * @var string |
||
72 | * |
||
73 | * @config |
||
74 | */ |
||
75 | private static $new_users_dn; |
||
76 | |||
77 | /** |
||
78 | * Location to create new groups in (distinguished name). |
||
79 | * @var string |
||
80 | * |
||
81 | * @config |
||
82 | */ |
||
83 | private static $new_groups_dn; |
||
84 | |||
85 | /** |
||
86 | * @var array |
||
87 | */ |
||
88 | private static $_cache_nested_groups = []; |
||
89 | |||
90 | /** |
||
91 | * If this is configured to a "Code" value of a {@link Group} in SilverStripe, the user will always |
||
92 | * be added to this group's membership when imported, regardless of any sort of group mappings. |
||
93 | * |
||
94 | * @var string |
||
95 | * @config |
||
96 | */ |
||
97 | private static $default_group; |
||
98 | |||
99 | /** |
||
100 | * For samba4 directory, there is no way to enforce password history on password resets. |
||
101 | * This only happens with changePassword (which requires the old password). |
||
102 | * This works around it by making the old password up and setting it administratively. |
||
103 | * |
||
104 | * A cleaner fix would be to use the LDAP_SERVER_POLICY_HINTS_OID connection flag, |
||
105 | * but it's not implemented in samba https://bugzilla.samba.org/show_bug.cgi?id=12020 |
||
106 | * |
||
107 | * @var bool |
||
108 | */ |
||
109 | private static $password_history_workaround = false; |
||
110 | |||
111 | /** |
||
112 | * Get the cache object used for LDAP results. Note that the default lifetime set here |
||
113 | * is 8 hours, but you can change that by adding configuration: |
||
114 | * |
||
115 | * <code> |
||
116 | * SilverStripe\Core\Injector\Injector: |
||
117 | * Psr\SimpleCache\CacheInterface.ldap: |
||
118 | * constructor: |
||
119 | * defaultLifetime: 3600 # time in seconds |
||
120 | * </code> |
||
121 | * |
||
122 | * @return Psr\SimpleCache\CacheInterface |
||
123 | */ |
||
124 | public static function get_cache() |
||
128 | |||
129 | /** |
||
130 | * Flushes out the LDAP results cache when flush=1 is called. |
||
131 | */ |
||
132 | public static function flush() |
||
138 | |||
139 | /** |
||
140 | * @var LDAPGateway |
||
141 | */ |
||
142 | public $gateway; |
||
143 | |||
144 | public function __construct() |
||
148 | |||
149 | /** |
||
150 | * Setter for gateway. Useful for overriding the gateway with a fake for testing. |
||
151 | * @var LDAPGateway |
||
152 | */ |
||
153 | public function setGateway($gateway) |
||
157 | |||
158 | /** |
||
159 | * Checkes whether or not the service is enabled. |
||
160 | * |
||
161 | * @return bool |
||
162 | */ |
||
163 | public function enabled() |
||
168 | |||
169 | /** |
||
170 | * Authenticate the given username and password with LDAP. |
||
171 | * |
||
172 | * @param string $username |
||
173 | * @param string $password |
||
174 | * |
||
175 | * @return array |
||
176 | */ |
||
177 | public function authenticate($username, $password) |
||
213 | |||
214 | /** |
||
215 | * Return all nodes (organizational units, containers, and domains) within the current base DN. |
||
216 | * |
||
217 | * @param boolean $cached Cache the results from AD, so that subsequent calls are faster. Enabled by default. |
||
218 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
219 | * @return array |
||
220 | */ |
||
221 | public function getNodes($cached = true, $attributes = []) |
||
239 | |||
240 | /** |
||
241 | * Return all AD groups in configured search locations, including all nested groups. |
||
242 | * Uses groups_search_locations if defined, otherwise falls back to NULL, which tells LDAPGateway |
||
243 | * to use the default baseDn defined in the connection. |
||
244 | * |
||
245 | * @param boolean $cached Cache the results from AD, so that subsequent calls are faster. Enabled by default. |
||
246 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
247 | * @param string $indexBy Attribute to use as an index. |
||
248 | * @return array |
||
249 | */ |
||
250 | public function getGroups($cached = true, $attributes = [], $indexBy = 'dn') |
||
279 | |||
280 | /** |
||
281 | * Return all member groups (and members of those, recursively) underneath a specific group DN. |
||
282 | * Note that these get cached in-memory per-request for performance to avoid re-querying for the same results. |
||
283 | * |
||
284 | * @param string $dn |
||
285 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
286 | * @return array |
||
287 | */ |
||
288 | public function getNestedGroups($dn, $attributes = []) |
||
306 | |||
307 | /** |
||
308 | * Get a particular AD group's data given a GUID. |
||
309 | * |
||
310 | * @param string $guid |
||
311 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
312 | * @return array |
||
313 | */ |
||
314 | View Code Duplication | public function getGroupByGUID($guid, $attributes = []) |
|
324 | |||
325 | /** |
||
326 | * Get a particular AD group's data given a DN. |
||
327 | * |
||
328 | * @param string $dn |
||
329 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
330 | * @return array |
||
331 | */ |
||
332 | View Code Duplication | public function getGroupByDN($dn, $attributes = []) |
|
342 | |||
343 | /** |
||
344 | * Return all AD users in configured search locations, including all users in nested groups. |
||
345 | * Uses users_search_locations if defined, otherwise falls back to NULL, which tells LDAPGateway |
||
346 | * to use the default baseDn defined in the connection. |
||
347 | * |
||
348 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
349 | * @return array |
||
350 | */ |
||
351 | public function getUsers($attributes = []) |
||
369 | |||
370 | /** |
||
371 | * Get a specific AD user's data given a GUID. |
||
372 | * |
||
373 | * @param string $guid |
||
374 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
375 | * @return array |
||
376 | */ |
||
377 | View Code Duplication | public function getUserByGUID($guid, $attributes = []) |
|
387 | |||
388 | /** |
||
389 | * Get a specific AD user's data given a DN. |
||
390 | * |
||
391 | * @param string $dn |
||
392 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
393 | * |
||
394 | * @return array |
||
395 | */ |
||
396 | View Code Duplication | public function getUserByDN($dn, $attributes = []) |
|
406 | |||
407 | /** |
||
408 | * Get a specific user's data given an email. |
||
409 | * |
||
410 | * @param string $email |
||
411 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
412 | * @return array |
||
413 | */ |
||
414 | View Code Duplication | public function getUserByEmail($email, $attributes = []) |
|
424 | |||
425 | /** |
||
426 | * Get a specific user's data given a username. |
||
427 | * |
||
428 | * @param string $username |
||
429 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
430 | * @return array |
||
431 | */ |
||
432 | View Code Duplication | public function getUserByUsername($username, $attributes = []) |
|
442 | |||
443 | /** |
||
444 | * Get a username for an email. |
||
445 | * |
||
446 | * @param string $email |
||
447 | * @return string|null |
||
448 | */ |
||
449 | public function getUsernameByEmail($email) |
||
458 | |||
459 | /** |
||
460 | * Given a group DN, get the group membership data in LDAP. |
||
461 | * |
||
462 | * @param string $dn |
||
463 | * @return array |
||
464 | */ |
||
465 | public function getLDAPGroupMembers($dn) |
||
478 | |||
479 | /** |
||
480 | * Update the current Member record with data from LDAP. |
||
481 | * |
||
482 | * Constraints: |
||
483 | * - Member *must* be in the database before calling this as it will need the ID to be mapped to a {@link Group}. |
||
484 | * - GUID of the member must have already been set, for integrity reasons we don't allow it to change here. |
||
485 | * |
||
486 | * @param Member |
||
487 | * @param array|null $data If passed, this is pre-existing AD attribute data to update the Member with. |
||
488 | * If not given, the data will be looked up by the user's GUID. |
||
489 | * @return bool |
||
490 | */ |
||
491 | public function updateMemberFromLDAP(Member $member, $data = null) |
||
662 | |||
663 | /** |
||
664 | * Sync a specific Group by updating it with LDAP data. |
||
665 | * |
||
666 | * @param Group $group An existing Group or a new Group object |
||
667 | * @param array $data LDAP group object data |
||
668 | * |
||
669 | * @return bool |
||
670 | */ |
||
671 | public function updateGroupFromLDAP(Group $group, $data) |
||
707 | |||
708 | /** |
||
709 | * Creates a new LDAP user from the passed Member record. |
||
710 | * Note that the Member record must have a non-empty Username field for this to work. |
||
711 | * |
||
712 | * @param Member $member |
||
713 | * @throws ValidationException |
||
714 | * @throws Exception |
||
715 | */ |
||
716 | public function createLDAPUser(Member $member) |
||
758 | |||
759 | /** |
||
760 | * Creates a new LDAP group from the passed Group record. |
||
761 | * |
||
762 | * @param Group $group |
||
763 | * @throws ValidationException |
||
764 | */ |
||
765 | public function createLDAPGroup(Group $group) |
||
808 | |||
809 | /** |
||
810 | * Update the Member data back to the corresponding LDAP user object. |
||
811 | * |
||
812 | * @param Member $member |
||
813 | * @throws ValidationException |
||
814 | */ |
||
815 | public function updateLDAPFromMember(Member $member) |
||
874 | |||
875 | /** |
||
876 | * Ensure the user belongs to the correct groups in LDAP from their membership |
||
877 | * to local LDAP mapped SilverStripe groups. |
||
878 | * |
||
879 | * This also removes them from LDAP groups if they've been taken out of one. |
||
880 | * It will not affect group membership of non-mapped groups, so it will |
||
881 | * not touch such internal AD groups like "Domain Users". |
||
882 | * |
||
883 | * @param Member $member |
||
884 | * @throws ValidationException |
||
885 | */ |
||
886 | public function updateLDAPGroupsForMember(Member $member) |
||
960 | |||
961 | /** |
||
962 | * Add LDAP user by DN to LDAP group. |
||
963 | * |
||
964 | * @param string $userDn |
||
965 | * @param string $groupDn |
||
966 | * @throws Exception |
||
967 | */ |
||
968 | public function addLDAPUserToGroup($userDn, $groupDn) |
||
985 | |||
986 | /** |
||
987 | * Change a members password on the AD. Works with ActiveDirectory compatible services that saves the |
||
988 | * password in the `unicodePwd` attribute. |
||
989 | * |
||
990 | * @todo Use the Zend\Ldap\Attribute::setPassword functionality to create a password in |
||
991 | * an abstract way, so it works on other LDAP directories, not just Active Directory. |
||
992 | * |
||
993 | * Ensure that the LDAP bind:ed user can change passwords and that the connection is secure. |
||
994 | * |
||
995 | * @param Member $member |
||
996 | * @param string $password |
||
997 | * @param string|null $oldPassword Supply old password to perform a password change (as opposed to password reset) |
||
998 | * @return ValidationResult |
||
999 | */ |
||
1000 | public function setPassword(Member $member, $password, $oldPassword = null) |
||
1043 | |||
1044 | /** |
||
1045 | * Delete an LDAP user mapped to the Member record |
||
1046 | * @param Member $member |
||
1047 | * @throws ValidationException |
||
1048 | */ |
||
1049 | public function deleteLDAPMember(Member $member) |
||
1068 | |||
1069 | /** |
||
1070 | * A simple proxy to LDAP update operation. |
||
1071 | * |
||
1072 | * @param string $dn Location to add the entry at. |
||
1073 | * @param array $attributes A simple associative array of attributes. |
||
1074 | */ |
||
1075 | public function update($dn, array $attributes) |
||
1079 | |||
1080 | /** |
||
1081 | * A simple proxy to LDAP delete operation. |
||
1082 | * |
||
1083 | * @param string $dn Location of object to delete |
||
1084 | * @param bool $recursively Recursively delete nested objects? |
||
1085 | */ |
||
1086 | public function delete($dn, $recursively = false) |
||
1090 | |||
1091 | /** |
||
1092 | * A simple proxy to LDAP copy/delete operation. |
||
1093 | * |
||
1094 | * @param string $fromDn |
||
1095 | * @param string $toDn |
||
1096 | * @param bool $recursively Recursively move nested objects? |
||
1097 | */ |
||
1098 | public function move($fromDn, $toDn, $recursively = false) |
||
1102 | |||
1103 | /** |
||
1104 | * A simple proxy to LDAP add operation. |
||
1105 | * |
||
1106 | * @param string $dn Location to add the entry at. |
||
1107 | * @param array $attributes A simple associative array of attributes. |
||
1108 | */ |
||
1109 | public function add($dn, array $attributes) |
||
1113 | |||
1114 | /** |
||
1115 | * @param string $dn Distinguished name of the user |
||
1116 | * @param string $password New password. |
||
1117 | * @throws Exception |
||
1118 | */ |
||
1119 | private function passwordHistoryWorkaround($dn, $password) |
||
1127 | |||
1128 | /** |
||
1129 | * Get a logger |
||
1130 | * |
||
1131 | * @return LoggerInterface |
||
1132 | */ |
||
1133 | public function getLogger() |
||
1137 | } |
||
1138 |
This check marks private properties in classes that are never used. Those properties can be removed.