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 |
||
| 38 | class LDAPService extends Object implements Flushable |
||
| 39 | { |
||
| 40 | /** |
||
| 41 | * @var array |
||
| 42 | */ |
||
| 43 | private static $dependencies = [ |
||
|
|
|||
| 44 | 'gateway' => '%$SilverStripe\\ActiveDirectory\\Model\\LDAPGateway' |
||
| 45 | ]; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * If configured, only user objects within these locations will be exposed to this service. |
||
| 49 | * |
||
| 50 | * @var array |
||
| 51 | * @config |
||
| 52 | */ |
||
| 53 | private static $users_search_locations = []; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * If configured, only group objects within these locations will be exposed to this service. |
||
| 57 | * @var array |
||
| 58 | * |
||
| 59 | * @config |
||
| 60 | */ |
||
| 61 | private static $groups_search_locations = []; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Location to create new users in (distinguished name). |
||
| 65 | * @var string |
||
| 66 | * |
||
| 67 | * @config |
||
| 68 | */ |
||
| 69 | private static $new_users_dn; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Location to create new groups in (distinguished name). |
||
| 73 | * @var string |
||
| 74 | * |
||
| 75 | * @config |
||
| 76 | */ |
||
| 77 | private static $new_groups_dn; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var array |
||
| 81 | */ |
||
| 82 | private static $_cache_nested_groups = []; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * If this is configured to a "Code" value of a {@link Group} in SilverStripe, the user will always |
||
| 86 | * be added to this group's membership when imported, regardless of any sort of group mappings. |
||
| 87 | * |
||
| 88 | * @var string |
||
| 89 | * @config |
||
| 90 | */ |
||
| 91 | private static $default_group; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * For samba4 directory, there is no way to enforce password history on password resets. |
||
| 95 | * This only happens with changePassword (which requires the old password). |
||
| 96 | * This works around it by making the old password up and setting it administratively. |
||
| 97 | * |
||
| 98 | * A cleaner fix would be to use the LDAP_SERVER_POLICY_HINTS_OID connection flag, |
||
| 99 | * but it's not implemented in samba https://bugzilla.samba.org/show_bug.cgi?id=12020 |
||
| 100 | * |
||
| 101 | * @var bool |
||
| 102 | */ |
||
| 103 | private static $password_history_workaround = false; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Get the cache object used for LDAP results. Note that the default lifetime set here |
||
| 107 | * is 8 hours, but you can change that by calling Cache::set_lifetime('ldap', <lifetime in seconds>) |
||
| 108 | * |
||
| 109 | * @return Zend_Cache_Frontend |
||
| 110 | */ |
||
| 111 | public static function get_cache() |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Flushes out the LDAP results cache when flush=1 is called. |
||
| 121 | */ |
||
| 122 | public static function flush() |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @var LDAPGateway |
||
| 130 | */ |
||
| 131 | public $gateway; |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Setter for gateway. Useful for overriding the gateway with a fake for testing. |
||
| 135 | * @var LDAPGateway |
||
| 136 | */ |
||
| 137 | public function setGateway($gateway) |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Checkes whether or not the service is enabled. |
||
| 144 | * |
||
| 145 | * @return bool |
||
| 146 | */ |
||
| 147 | public function enabled() |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Authenticate the given username and password with LDAP. |
||
| 155 | * |
||
| 156 | * @param string $username |
||
| 157 | * @param string $password |
||
| 158 | * |
||
| 159 | * @return array |
||
| 160 | */ |
||
| 161 | public function authenticate($username, $password) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Return all nodes (organizational units, containers, and domains) within the current base DN. |
||
| 200 | * |
||
| 201 | * @param boolean $cached Cache the results from AD, so that subsequent calls are faster. Enabled by default. |
||
| 202 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
| 203 | * @return array |
||
| 204 | */ |
||
| 205 | public function getNodes($cached = true, $attributes = []) |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Return all AD groups in configured search locations, including all nested groups. |
||
| 225 | * Uses groups_search_locations if defined, otherwise falls back to NULL, which tells LDAPGateway |
||
| 226 | * to use the default baseDn defined in the connection. |
||
| 227 | * |
||
| 228 | * @param boolean $cached Cache the results from AD, so that subsequent calls are faster. Enabled by default. |
||
| 229 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
| 230 | * @param string $indexBy Attribute to use as an index. |
||
| 231 | * @return array |
||
| 232 | */ |
||
| 233 | public function getGroups($cached = true, $attributes = [], $indexBy = 'dn') |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Return all member groups (and members of those, recursively) underneath a specific group DN. |
||
| 260 | * Note that these get cached in-memory per-request for performance to avoid re-querying for the same results. |
||
| 261 | * |
||
| 262 | * @param string $dn |
||
| 263 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
| 264 | * @return array |
||
| 265 | */ |
||
| 266 | public function getNestedGroups($dn, $attributes = []) |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Get a particular AD group's data given a GUID. |
||
| 287 | * |
||
| 288 | * @param string $guid |
||
| 289 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
| 290 | * @return array |
||
| 291 | */ |
||
| 292 | View Code Duplication | public function getGroupByGUID($guid, $attributes = []) |
|
| 302 | |||
| 303 | /** |
||
| 304 | * Get a particular AD group's data given a DN. |
||
| 305 | * |
||
| 306 | * @param string $dn |
||
| 307 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
| 308 | * @return array |
||
| 309 | */ |
||
| 310 | View Code Duplication | public function getGroupByDN($dn, $attributes = []) |
|
| 320 | |||
| 321 | /** |
||
| 322 | * Return all AD users in configured search locations, including all users in nested groups. |
||
| 323 | * Uses users_search_locations if defined, otherwise falls back to NULL, which tells LDAPGateway |
||
| 324 | * to use the default baseDn defined in the connection. |
||
| 325 | * |
||
| 326 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
| 327 | * @return array |
||
| 328 | */ |
||
| 329 | public function getUsers($attributes = []) |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Get a specific AD user's data given a GUID. |
||
| 350 | * |
||
| 351 | * @param string $guid |
||
| 352 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
| 353 | * @return array |
||
| 354 | */ |
||
| 355 | View Code Duplication | public function getUserByGUID($guid, $attributes = []) |
|
| 365 | |||
| 366 | /** |
||
| 367 | * Get a specific AD user's data given a DN. |
||
| 368 | * |
||
| 369 | * @param string $dn |
||
| 370 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
| 371 | * |
||
| 372 | * @return array |
||
| 373 | */ |
||
| 374 | View Code Duplication | public function getUserByDN($dn, $attributes = []) |
|
| 384 | |||
| 385 | /** |
||
| 386 | * Get a specific user's data given an email. |
||
| 387 | * |
||
| 388 | * @param string $email |
||
| 389 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
| 390 | * @return array |
||
| 391 | */ |
||
| 392 | View Code Duplication | public function getUserByEmail($email, $attributes = []) |
|
| 402 | |||
| 403 | /** |
||
| 404 | * Get a specific user's data given a username. |
||
| 405 | * |
||
| 406 | * @param string $username |
||
| 407 | * @param array $attributes List of specific AD attributes to return. Empty array means return everything. |
||
| 408 | * @return array |
||
| 409 | */ |
||
| 410 | View Code Duplication | public function getUserByUsername($username, $attributes = []) |
|
| 420 | |||
| 421 | /** |
||
| 422 | * Get a username for an email. |
||
| 423 | * |
||
| 424 | * @param string $email |
||
| 425 | * @return string|null |
||
| 426 | */ |
||
| 427 | public function getUsernameByEmail($email) |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Given a group DN, get the group membership data in LDAP. |
||
| 439 | * |
||
| 440 | * @param string $dn |
||
| 441 | * @return array |
||
| 442 | */ |
||
| 443 | public function getLDAPGroupMembers($dn) |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Update the current Member record with data from LDAP. |
||
| 459 | * |
||
| 460 | * Constraints: |
||
| 461 | * - Member *must* be in the database before calling this as it will need the ID to be mapped to a {@link Group}. |
||
| 462 | * - GUID of the member must have already been set, for integrity reasons we don't allow it to change here. |
||
| 463 | * |
||
| 464 | * @param Member |
||
| 465 | * @param array|null $data If passed, this is pre-existing AD attribute data to update the Member with. |
||
| 466 | * If not given, the data will be looked up by the user's GUID. |
||
| 467 | * @return bool |
||
| 468 | */ |
||
| 469 | public function updateMemberFromLDAP(Member $member, $data = null) |
||
| 640 | |||
| 641 | /** |
||
| 642 | * Sync a specific Group by updating it with LDAP data. |
||
| 643 | * |
||
| 644 | * @param Group $group An existing Group or a new Group object |
||
| 645 | * @param array $data LDAP group object data |
||
| 646 | * |
||
| 647 | * @return bool |
||
| 648 | */ |
||
| 649 | public function updateGroupFromLDAP(Group $group, $data) |
||
| 650 | { |
||
| 651 | if (!$this->enabled()) { |
||
| 652 | return false; |
||
| 653 | } |
||
| 654 | |||
| 655 | // Synchronise specific guaranteed fields. |
||
| 656 | $group->Code = $data['samaccountname']; |
||
| 657 | $group->Title = $data['samaccountname']; |
||
| 658 | if (!empty($data['description'])) { |
||
| 659 | $group->Description = $data['description']; |
||
| 660 | } |
||
| 661 | $group->DN = $data['dn']; |
||
| 662 | $group->LastSynced = (string)DBDatetime::now(); |
||
| 663 | $group->write(); |
||
| 664 | |||
| 665 | // Mappings on this group are automatically maintained to contain just the group's DN. |
||
| 666 | // First, scan through existing mappings and remove ones that are not matching (in case the group moved). |
||
| 667 | $hasCorrectMapping = false; |
||
| 668 | foreach ($group->LDAPGroupMappings() as $mapping) { |
||
| 669 | if ($mapping->DN === $data['dn']) { |
||
| 670 | // This is the correct mapping we want to retain. |
||
| 671 | $hasCorrectMapping = true; |
||
| 672 | } else { |
||
| 673 | $mapping->delete(); |
||
| 674 | } |
||
| 675 | } |
||
| 676 | |||
| 677 | // Second, if the main mapping was not found, add it in. |
||
| 678 | if (!$hasCorrectMapping) { |
||
| 679 | $mapping = new LDAPGroupMapping(); |
||
| 680 | $mapping->DN = $data['dn']; |
||
| 681 | $mapping->write(); |
||
| 682 | $group->LDAPGroupMappings()->add($mapping); |
||
| 683 | } |
||
| 684 | } |
||
| 685 | |||
| 686 | /** |
||
| 687 | * Creates a new LDAP user from the passed Member record. |
||
| 688 | * Note that the Member record must have a non-empty Username field for this to work. |
||
| 689 | * |
||
| 690 | * @param Member $member |
||
| 691 | * @throws ValidationException |
||
| 692 | * @throws Exception |
||
| 693 | */ |
||
| 694 | public function createLDAPUser(Member $member) |
||
| 695 | { |
||
| 696 | if (!$this->enabled()) { |
||
| 697 | return; |
||
| 698 | } |
||
| 699 | if (empty($member->Username)) { |
||
| 700 | throw new ValidationException('Member missing Username. Cannot create LDAP user'); |
||
| 701 | } |
||
| 702 | if (!$this->config()->new_users_dn) { |
||
| 703 | throw new Exception('LDAPService::new_users_dn must be configured to create LDAP users'); |
||
| 704 | } |
||
| 705 | |||
| 706 | // Normalise username to lowercase to ensure we don't have duplicates of different cases |
||
| 707 | $member->Username = strtolower($member->Username); |
||
| 708 | |||
| 709 | // Create user in LDAP using available information. |
||
| 710 | $dn = sprintf('CN=%s,%s', $member->Username, $this->config()->new_users_dn); |
||
| 711 | |||
| 712 | try { |
||
| 713 | $this->add($dn, [ |
||
| 714 | 'objectclass' => 'user', |
||
| 715 | 'cn' => $member->Username, |
||
| 716 | 'accountexpires' => '9223372036854775807', |
||
| 717 | 'useraccountcontrol' => '66048', |
||
| 718 | 'userprincipalname' => sprintf( |
||
| 719 | '%s@%s', |
||
| 720 | $member->Username, |
||
| 721 | $this->gateway->config()->options['accountDomainName'] |
||
| 722 | ), |
||
| 723 | ]); |
||
| 724 | } catch (Exception $e) { |
||
| 725 | throw new ValidationException('LDAP synchronisation failure: ' . $e->getMessage()); |
||
| 726 | } |
||
| 727 | |||
| 728 | $user = $this->getUserByUsername($member->Username); |
||
| 729 | if (empty($user['objectguid'])) { |
||
| 730 | throw new ValidationException('LDAP synchronisation failure: user missing GUID'); |
||
| 731 | } |
||
| 732 | |||
| 733 | // Creation was successful, mark the user as LDAP managed by setting the GUID. |
||
| 734 | $member->GUID = $user['objectguid']; |
||
| 735 | } |
||
| 736 | |||
| 737 | /** |
||
| 738 | * Creates a new LDAP group from the passed Group record. |
||
| 739 | * |
||
| 740 | * @param Group $group |
||
| 741 | * @throws ValidationException |
||
| 742 | */ |
||
| 743 | public function createLDAPGroup(Group $group) { |
||
| 744 | if (!$this->enabled()) { |
||
| 745 | return; |
||
| 746 | } |
||
| 747 | if (empty($group->Title)) { |
||
| 748 | throw new ValidationException('Group missing Title. Cannot create LDAP group'); |
||
| 749 | } |
||
| 750 | if (!$this->config()->new_groups_dn) { |
||
| 751 | throw new Exception('LDAPService::new_groups_dn must be configured to create LDAP groups'); |
||
| 752 | } |
||
| 753 | |||
| 754 | // LDAP isn't really meant to distinguish between a Title and Code. Squash them. |
||
| 755 | $group->Code = $group->Title; |
||
| 756 | |||
| 757 | $dn = sprintf('CN=%s,%s', $group->Title, $this->config()->new_groups_dn); |
||
| 758 | try { |
||
| 759 | $this->add($dn, [ |
||
| 760 | 'objectclass' => 'group', |
||
| 761 | 'cn' => $group->Title, |
||
| 762 | 'name' => $group->Title, |
||
| 763 | 'samaccountname' => $group->Title, |
||
| 764 | 'description' => $group->Description, |
||
| 765 | 'distinguishedname' => $dn |
||
| 766 | ]); |
||
| 767 | } catch (\Exception $e) { |
||
| 768 | throw new \ValidationException('LDAP group creation failure: ' . $e->getMessage()); |
||
| 769 | } |
||
| 770 | |||
| 771 | $data = $this->getGroupByDN($dn); |
||
| 772 | if (empty($data['objectguid'])) { |
||
| 773 | throw new \ValidationException( |
||
| 774 | new \ValidationResult( |
||
| 775 | false, |
||
| 776 | 'LDAP group creation failure: group might have been created in LDAP. GUID is missing.' |
||
| 777 | ) |
||
| 778 | ); |
||
| 779 | } |
||
| 780 | |||
| 781 | // Creation was successful, mark the group as LDAP managed by setting the GUID. |
||
| 782 | $group->GUID = $data['objectguid']; |
||
| 783 | $group->DN = $data['dn']; |
||
| 784 | } |
||
| 785 | |||
| 786 | /** |
||
| 787 | * Update the Member data back to the corresponding LDAP user object. |
||
| 788 | * |
||
| 789 | * @param Member $member |
||
| 790 | * @throws ValidationException |
||
| 791 | */ |
||
| 792 | public function updateLDAPFromMember(Member $member) |
||
| 851 | |||
| 852 | /** |
||
| 853 | * Ensure the user belongs to the correct groups in LDAP from their membership |
||
| 854 | * to local LDAP mapped SilverStripe groups. |
||
| 855 | * |
||
| 856 | * This also removes them from LDAP groups if they've been taken out of one. |
||
| 857 | * It will not affect group membership of non-mapped groups, so it will |
||
| 858 | * not touch such internal AD groups like "Domain Users". |
||
| 859 | * |
||
| 860 | * @param Member $member |
||
| 861 | * @throws ValidationException |
||
| 862 | */ |
||
| 863 | public function updateLDAPGroupsForMember(Member $member) |
||
| 937 | |||
| 938 | /** |
||
| 939 | * Add LDAP user by DN to LDAP group. |
||
| 940 | * |
||
| 941 | * @param string $userDn |
||
| 942 | * @param string $groupDn |
||
| 943 | * @throws \Exception |
||
| 944 | */ |
||
| 945 | public function addLDAPUserToGroup($userDn, $groupDn) { |
||
| 961 | |||
| 962 | /** |
||
| 963 | * Change a members password on the AD. Works with ActiveDirectory compatible services that saves the |
||
| 964 | * password in the `unicodePwd` attribute. |
||
| 965 | * |
||
| 966 | * @todo Use the Zend\Ldap\Attribute::setPassword functionality to create a password in |
||
| 967 | * an abstract way, so it works on other LDAP directories, not just Active Directory. |
||
| 968 | * |
||
| 969 | * Ensure that the LDAP bind:ed user can change passwords and that the connection is secure. |
||
| 970 | * |
||
| 971 | * @param Member $member |
||
| 972 | * @param string $password |
||
| 973 | * @param string|null $oldPassword Supply old password to perform a password change (as opposed to password reset) |
||
| 974 | * @return ValidationResult |
||
| 975 | */ |
||
| 976 | public function setPassword(Member $member, $password, $oldPassword = null) |
||
| 1019 | |||
| 1020 | /** |
||
| 1021 | * Delete an LDAP user mapped to the Member record |
||
| 1022 | * @param Member $member |
||
| 1023 | * @throws ValidationException |
||
| 1024 | */ |
||
| 1025 | public function deleteLDAPMember(Member $member) |
||
| 1044 | |||
| 1045 | /** |
||
| 1046 | * A simple proxy to LDAP update operation. |
||
| 1047 | * |
||
| 1048 | * @param string $dn Location to add the entry at. |
||
| 1049 | * @param array $attributes A simple associative array of attributes. |
||
| 1050 | */ |
||
| 1051 | public function update($dn, array $attributes) |
||
| 1055 | |||
| 1056 | /** |
||
| 1057 | * A simple proxy to LDAP delete operation. |
||
| 1058 | * |
||
| 1059 | * @param string $dn Location of object to delete |
||
| 1060 | * @param bool $recursively Recursively delete nested objects? |
||
| 1061 | */ |
||
| 1062 | public function delete($dn, $recursively = false) |
||
| 1066 | |||
| 1067 | /** |
||
| 1068 | * A simple proxy to LDAP copy/delete operation. |
||
| 1069 | * |
||
| 1070 | * @param string $fromDn |
||
| 1071 | * @param string $toDn |
||
| 1072 | * @param bool $recursively Recursively move nested objects? |
||
| 1073 | */ |
||
| 1074 | public function move($fromDn, $toDn, $recursively = false) |
||
| 1078 | |||
| 1079 | /** |
||
| 1080 | * A simple proxy to LDAP add operation. |
||
| 1081 | * |
||
| 1082 | * @param string $dn Location to add the entry at. |
||
| 1083 | * @param array $attributes A simple associative array of attributes. |
||
| 1084 | */ |
||
| 1085 | public function add($dn, array $attributes) |
||
| 1089 | |||
| 1090 | /** |
||
| 1091 | * @param string $dn Distinguished name of the user |
||
| 1092 | * @param string $password New password. |
||
| 1093 | * @throws Exception |
||
| 1094 | */ |
||
| 1095 | private function passwordHistoryWorkaround($dn, $password) |
||
| 1103 | |||
| 1104 | /** |
||
| 1105 | * Get a logger |
||
| 1106 | * |
||
| 1107 | * @return LoggerInterface |
||
| 1108 | */ |
||
| 1109 | public function getLogger() |
||
| 1113 | } |
||
| 1114 |
This check marks private properties in classes that are never used. Those properties can be removed.