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 Access 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 Access, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 54 | class Access extends LDAPUtility implements IUserTools { |
||
| 55 | /** |
||
| 56 | * @var \OCA\User_LDAP\Connection |
||
| 57 | */ |
||
| 58 | public $connection; |
||
| 59 | /** @var Manager */ |
||
| 60 | public $userManager; |
||
| 61 | //never ever check this var directly, always use getPagedSearchResultState |
||
| 62 | protected $pagedSearchedSuccessful; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var string[] $cookies an array of returned Paged Result cookies |
||
| 66 | */ |
||
| 67 | protected $cookies = array(); |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var string $lastCookie the last cookie returned from a Paged Results |
||
| 71 | * operation, defaults to an empty string |
||
| 72 | */ |
||
| 73 | protected $lastCookie = ''; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var AbstractMapping $userMapper |
||
| 77 | */ |
||
| 78 | protected $userMapper; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @var AbstractMapping $userMapper |
||
| 82 | */ |
||
| 83 | protected $groupMapper; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @var \OCA\User_LDAP\Helper |
||
| 87 | */ |
||
| 88 | private $helper; |
||
| 89 | |||
| 90 | public function __construct(Connection $connection, ILDAPWrapper $ldap, |
||
| 98 | |||
| 99 | /** |
||
| 100 | * sets the User Mapper |
||
| 101 | * @param AbstractMapping $mapper |
||
| 102 | */ |
||
| 103 | public function setUserMapper(AbstractMapping $mapper) { |
||
| 106 | |||
| 107 | /** |
||
| 108 | * returns the User Mapper |
||
| 109 | * @throws \Exception |
||
| 110 | * @return AbstractMapping |
||
| 111 | */ |
||
| 112 | public function getUserMapper() { |
||
| 118 | |||
| 119 | /** |
||
| 120 | * sets the Group Mapper |
||
| 121 | * @param AbstractMapping $mapper |
||
| 122 | */ |
||
| 123 | public function setGroupMapper(AbstractMapping $mapper) { |
||
| 126 | |||
| 127 | /** |
||
| 128 | * returns the Group Mapper |
||
| 129 | * @throws \Exception |
||
| 130 | * @return AbstractMapping |
||
| 131 | */ |
||
| 132 | public function getGroupMapper() { |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @return bool |
||
| 141 | */ |
||
| 142 | private function checkConnection() { |
||
| 145 | |||
| 146 | /** |
||
| 147 | * returns the Connection instance |
||
| 148 | * @return \OCA\User_LDAP\Connection |
||
| 149 | */ |
||
| 150 | public function getConnection() { |
||
| 153 | |||
| 154 | /** |
||
| 155 | * reads a given attribute for an LDAP record identified by a DN |
||
| 156 | * @param string $dn the record in question |
||
| 157 | * @param string $attr the attribute that shall be retrieved |
||
| 158 | * if empty, just check the record's existence |
||
| 159 | * @param string $filter |
||
| 160 | * @return array|false an array of values on success or an empty |
||
| 161 | * array if $attr is empty, false otherwise |
||
| 162 | */ |
||
| 163 | public function readAttribute($dn, $attr, $filter = 'objectClass=*') { |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Runs an read operation against LDAP |
||
| 234 | * |
||
| 235 | * @param resource $cr the LDAP connection |
||
| 236 | * @param string $dn |
||
| 237 | * @param string $attribute |
||
| 238 | * @param string $filter |
||
| 239 | * @param int $maxResults |
||
| 240 | * @return array|bool false if there was any error, true if an exists check |
||
| 241 | * was performed and the requested DN found, array with the |
||
| 242 | * returned data on a successful usual operation |
||
| 243 | */ |
||
| 244 | public function executeRead($cr, $dn, $attribute, $filter, $maxResults) { |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Normalizes a result grom getAttributes(), i.e. handles DNs and binary |
||
| 274 | * data if present. |
||
| 275 | * |
||
| 276 | * @param array $result from ILDAPWrapper::getAttributes() |
||
| 277 | * @param string $attribute the attribute name that was read |
||
| 278 | * @return string[] |
||
| 279 | */ |
||
| 280 | public function extractAttributeValuesFromResult($result, $attribute) { |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Attempts to find ranged data in a getAttribute results and extracts the |
||
| 299 | * returned values as well as information on the range and full attribute |
||
| 300 | * name for further processing. |
||
| 301 | * |
||
| 302 | * @param array $result from ILDAPWrapper::getAttributes() |
||
| 303 | * @param string $attribute the attribute name that was read. Without ";range=…" |
||
| 304 | * @return array If a range was detected with keys 'values', 'attributeName', |
||
| 305 | * 'attributeFull' and 'rangeHigh', otherwise empty. |
||
| 306 | */ |
||
| 307 | public function extractRangeData($result, $attribute) { |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Set password for an LDAP user identified by a DN |
||
| 329 | * |
||
| 330 | * @param string $userDN the user in question |
||
| 331 | * @param string $password the new password |
||
| 332 | * @return bool |
||
| 333 | * @throws HintException |
||
| 334 | * @throws \Exception |
||
| 335 | */ |
||
| 336 | public function setPassword($userDN, $password) { |
||
| 353 | |||
| 354 | /** |
||
| 355 | * checks whether the given attributes value is probably a DN |
||
| 356 | * @param string $attr the attribute in question |
||
| 357 | * @return boolean if so true, otherwise false |
||
| 358 | */ |
||
| 359 | private function resemblesDN($attr) { |
||
| 369 | |||
| 370 | /** |
||
| 371 | * checks whether the given string is probably a DN |
||
| 372 | * @param string $string |
||
| 373 | * @return boolean |
||
| 374 | */ |
||
| 375 | public function stringResemblesDN($string) { |
||
| 381 | |||
| 382 | /** |
||
| 383 | * returns a DN-string that is cleaned from not domain parts, e.g. |
||
| 384 | * cn=foo,cn=bar,dc=foobar,dc=server,dc=org |
||
| 385 | * becomes dc=foobar,dc=server,dc=org |
||
| 386 | * @param string $dn |
||
| 387 | * @return string |
||
| 388 | */ |
||
| 389 | public function getDomainDNFromDN($dn) { |
||
| 408 | |||
| 409 | /** |
||
| 410 | * returns the LDAP DN for the given internal ownCloud name of the group |
||
| 411 | * @param string $name the ownCloud name in question |
||
| 412 | * @return string|false LDAP DN on success, otherwise false |
||
| 413 | */ |
||
| 414 | public function groupname2dn($name) { |
||
| 417 | |||
| 418 | /** |
||
| 419 | * returns the LDAP DN for the given internal ownCloud name of the user |
||
| 420 | * @param string $name the ownCloud name in question |
||
| 421 | * @return string|false with the LDAP DN on success, otherwise false |
||
| 422 | */ |
||
| 423 | public function username2dn($name) { |
||
| 434 | |||
| 435 | /** |
||
| 436 | * returns the internal ownCloud name for the given LDAP DN of the group, false on DN outside of search DN or failure |
||
| 437 | * @param string $fdn the dn of the group object |
||
| 438 | * @param string $ldapName optional, the display name of the object |
||
| 439 | * @return string|false with the name to use in ownCloud, false on DN outside of search DN |
||
| 440 | */ |
||
| 441 | View Code Duplication | public function dn2groupname($fdn, $ldapName = null) { |
|
| 451 | |||
| 452 | /** |
||
| 453 | * accepts an array of group DNs and tests whether they match the user |
||
| 454 | * filter by doing read operations against the group entries. Returns an |
||
| 455 | * array of DNs that match the filter. |
||
| 456 | * |
||
| 457 | * @param string[] $groupDNs |
||
| 458 | * @return string[] |
||
| 459 | */ |
||
| 460 | public function groupsMatchFilter($groupDNs) { |
||
| 490 | |||
| 491 | /** |
||
| 492 | * returns the internal ownCloud name for the given LDAP DN of the user, false on DN outside of search DN or failure |
||
| 493 | * @param string $dn the dn of the user object |
||
| 494 | * @param string $ldapName optional, the display name of the object |
||
| 495 | * @return string|false with with the name to use in ownCloud |
||
| 496 | */ |
||
| 497 | View Code Duplication | public function dn2username($fdn, $ldapName = null) { |
|
| 507 | |||
| 508 | /** |
||
| 509 | * returns an internal ownCloud name for the given LDAP DN, false on DN outside of search DN |
||
| 510 | * @param string $dn the dn of the user object |
||
| 511 | * @param string $ldapName optional, the display name of the object |
||
| 512 | * @param bool $isUser optional, whether it is a user object (otherwise group assumed) |
||
| 513 | * @return string|false with with the name to use in ownCloud |
||
| 514 | */ |
||
| 515 | public function dn2ocname($fdn, $ldapName = null, $isUser = true) { |
||
| 590 | |||
| 591 | /** |
||
| 592 | * gives back the user names as they are used ownClod internally |
||
| 593 | * @param array $ldapUsers as returned by fetchList() |
||
| 594 | * @return array an array with the user names to use in ownCloud |
||
| 595 | * |
||
| 596 | * gives back the user names as they are used ownClod internally |
||
| 597 | */ |
||
| 598 | public function ownCloudUserNames($ldapUsers) { |
||
| 601 | |||
| 602 | /** |
||
| 603 | * gives back the group names as they are used ownClod internally |
||
| 604 | * @param array $ldapGroups as returned by fetchList() |
||
| 605 | * @return array an array with the group names to use in ownCloud |
||
| 606 | * |
||
| 607 | * gives back the group names as they are used ownClod internally |
||
| 608 | */ |
||
| 609 | public function ownCloudGroupNames($ldapGroups) { |
||
| 612 | |||
| 613 | /** |
||
| 614 | * @param array $ldapObjects as returned by fetchList() |
||
| 615 | * @param bool $isUsers |
||
| 616 | * @return array |
||
| 617 | */ |
||
| 618 | private function ldap2ownCloudNames($ldapObjects, $isUsers) { |
||
| 654 | |||
| 655 | /** |
||
| 656 | * caches the user display name |
||
| 657 | * @param string $ocName the internal ownCloud username |
||
| 658 | * @param string|false $home the home directory path |
||
| 659 | */ |
||
| 660 | public function cacheUserHome($ocName, $home) { |
||
| 664 | |||
| 665 | /** |
||
| 666 | * caches a user as existing |
||
| 667 | * @param string $ocName the internal ownCloud username |
||
| 668 | */ |
||
| 669 | public function cacheUserExists($ocName) { |
||
| 672 | |||
| 673 | /** |
||
| 674 | * caches the user display name |
||
| 675 | * @param string $ocName the internal ownCloud username |
||
| 676 | * @param string $displayName the display name |
||
| 677 | * @param string $displayName2 the second display name |
||
| 678 | */ |
||
| 679 | public function cacheUserDisplayName($ocName, $displayName, $displayName2 = '') { |
||
| 688 | |||
| 689 | /** |
||
| 690 | * creates a unique name for internal ownCloud use for users. Don't call it directly. |
||
| 691 | * @param string $name the display name of the object |
||
| 692 | * @return string|false with with the name to use in ownCloud or false if unsuccessful |
||
| 693 | * |
||
| 694 | * Instead of using this method directly, call |
||
| 695 | * createAltInternalOwnCloudName($name, true) |
||
| 696 | */ |
||
| 697 | private function _createAltInternalOwnCloudNameForUsers($name) { |
||
| 710 | |||
| 711 | /** |
||
| 712 | * creates a unique name for internal ownCloud use for groups. Don't call it directly. |
||
| 713 | * @param string $name the display name of the object |
||
| 714 | * @return string|false with with the name to use in ownCloud or false if unsuccessful. |
||
| 715 | * |
||
| 716 | * Instead of using this method directly, call |
||
| 717 | * createAltInternalOwnCloudName($name, false) |
||
| 718 | * |
||
| 719 | * Group names are also used as display names, so we do a sequential |
||
| 720 | * numbering, e.g. Developers_42 when there are 41 other groups called |
||
| 721 | * "Developers" |
||
| 722 | */ |
||
| 723 | private function _createAltInternalOwnCloudNameForGroups($name) { |
||
| 748 | |||
| 749 | /** |
||
| 750 | * creates a unique name for internal ownCloud use. |
||
| 751 | * @param string $name the display name of the object |
||
| 752 | * @param boolean $isUser whether name should be created for a user (true) or a group (false) |
||
| 753 | * @return string|false with with the name to use in ownCloud or false if unsuccessful |
||
| 754 | */ |
||
| 755 | private function createAltInternalOwnCloudName($name, $isUser) { |
||
| 767 | |||
| 768 | /** |
||
| 769 | * fetches a list of users according to a provided loginName and utilizing |
||
| 770 | * the login filter. |
||
| 771 | * |
||
| 772 | * @param string $loginName |
||
| 773 | * @param array $attributes optional, list of attributes to read |
||
| 774 | * @return array |
||
| 775 | */ |
||
| 776 | View Code Duplication | public function fetchUsersByLoginName($loginName, $attributes = array('dn')) { |
|
| 782 | |||
| 783 | /** |
||
| 784 | * counts the number of users according to a provided loginName and |
||
| 785 | * utilizing the login filter. |
||
| 786 | * |
||
| 787 | * @param string $loginName |
||
| 788 | * @return array |
||
| 789 | */ |
||
| 790 | View Code Duplication | public function countUsersByLoginName($loginName) { |
|
| 796 | |||
| 797 | /** |
||
| 798 | * @param string $filter |
||
| 799 | * @param string|string[] $attr |
||
| 800 | * @param int $limit |
||
| 801 | * @param int $offset |
||
| 802 | * @return array |
||
| 803 | */ |
||
| 804 | public function fetchListOfUsers($filter, $attr, $limit = null, $offset = null) { |
||
| 809 | |||
| 810 | /** |
||
| 811 | * provided with an array of LDAP user records the method will fetch the |
||
| 812 | * user object and requests it to process the freshly fetched attributes and |
||
| 813 | * and their values |
||
| 814 | * @param array $ldapRecords |
||
| 815 | */ |
||
| 816 | public function batchApplyUserAttributes(array $ldapRecords){ |
||
| 843 | |||
| 844 | /** |
||
| 845 | * @param string $filter |
||
| 846 | * @param string|string[] $attr |
||
| 847 | * @param int $limit |
||
| 848 | * @param int $offset |
||
| 849 | * @return array |
||
| 850 | */ |
||
| 851 | public function fetchListOfGroups($filter, $attr, $limit = null, $offset = null) { |
||
| 854 | |||
| 855 | /** |
||
| 856 | * @param array $list |
||
| 857 | * @param bool $manyAttributes |
||
| 858 | * @return array |
||
| 859 | */ |
||
| 860 | private function fetchList($list, $manyAttributes) { |
||
| 877 | |||
| 878 | /** |
||
| 879 | * executes an LDAP search, optimized for Users |
||
| 880 | * @param string $filter the LDAP filter for the search |
||
| 881 | * @param string|string[] $attr optional, when a certain attribute shall be filtered out |
||
| 882 | * @param integer $limit |
||
| 883 | * @param integer $offset |
||
| 884 | * @return array with the search result |
||
| 885 | * |
||
| 886 | * Executes an LDAP search |
||
| 887 | */ |
||
| 888 | public function searchUsers($filter, $attr = null, $limit = null, $offset = null) { |
||
| 891 | |||
| 892 | /** |
||
| 893 | * @param string $filter |
||
| 894 | * @param string|string[] $attr |
||
| 895 | * @param int $limit |
||
| 896 | * @param int $offset |
||
| 897 | * @return false|int |
||
| 898 | */ |
||
| 899 | public function countUsers($filter, $attr = array('dn'), $limit = null, $offset = null) { |
||
| 902 | |||
| 903 | /** |
||
| 904 | * executes an LDAP search, optimized for Groups |
||
| 905 | * @param string $filter the LDAP filter for the search |
||
| 906 | * @param string|string[] $attr optional, when a certain attribute shall be filtered out |
||
| 907 | * @param integer $limit |
||
| 908 | * @param integer $offset |
||
| 909 | * @return array with the search result |
||
| 910 | * |
||
| 911 | * Executes an LDAP search |
||
| 912 | */ |
||
| 913 | public function searchGroups($filter, $attr = null, $limit = null, $offset = null) { |
||
| 916 | |||
| 917 | /** |
||
| 918 | * returns the number of available groups |
||
| 919 | * @param string $filter the LDAP search filter |
||
| 920 | * @param string[] $attr optional |
||
| 921 | * @param int|null $limit |
||
| 922 | * @param int|null $offset |
||
| 923 | * @return int|bool |
||
| 924 | */ |
||
| 925 | public function countGroups($filter, $attr = array('dn'), $limit = null, $offset = null) { |
||
| 928 | |||
| 929 | /** |
||
| 930 | * returns the number of available objects on the base DN |
||
| 931 | * |
||
| 932 | * @param int|null $limit |
||
| 933 | * @param int|null $offset |
||
| 934 | * @return int|bool |
||
| 935 | */ |
||
| 936 | public function countObjects($limit = null, $offset = null) { |
||
| 939 | |||
| 940 | /** |
||
| 941 | * retrieved. Results will according to the order in the array. |
||
| 942 | * @param int $limit optional, maximum results to be counted |
||
| 943 | * @param int $offset optional, a starting point |
||
| 944 | * @return array|false array with the search result as first value and pagedSearchOK as |
||
| 945 | * second | false if not successful |
||
| 946 | */ |
||
| 947 | private function executeSearch($filter, $base, &$attr = null, $limit = null, $offset = null) { |
||
| 974 | |||
| 975 | /** |
||
| 976 | * processes an LDAP paged search operation |
||
| 977 | * @param array $sr the array containing the LDAP search resources |
||
| 978 | * @param string $filter the LDAP filter for the search |
||
| 979 | * @param array $base an array containing the LDAP subtree(s) that shall be searched |
||
| 980 | * @param int $iFoundItems number of results in the search operation |
||
| 981 | * @param int $limit maximum results to be counted |
||
| 982 | * @param int $offset a starting point |
||
| 983 | * @param bool $pagedSearchOK whether a paged search has been executed |
||
| 984 | * @param bool $skipHandling required for paged search when cookies to |
||
| 985 | * prior results need to be gained |
||
| 986 | * @return bool cookie validity, true if we have more pages, false otherwise. |
||
| 987 | */ |
||
| 988 | private function processPagedSearchStatus($sr, $filter, $base, $iFoundItems, $limit, $offset, $pagedSearchOK, $skipHandling) { |
||
| 1020 | |||
| 1021 | /** |
||
| 1022 | * executes an LDAP search, but counts the results only |
||
| 1023 | * @param string $filter the LDAP filter for the search |
||
| 1024 | * @param array $base an array containing the LDAP subtree(s) that shall be searched |
||
| 1025 | * @param string|string[] $attr optional, array, one or more attributes that shall be |
||
| 1026 | * retrieved. Results will according to the order in the array. |
||
| 1027 | * @param int $limit optional, maximum results to be counted |
||
| 1028 | * @param int $offset optional, a starting point |
||
| 1029 | * @param bool $skipHandling indicates whether the pages search operation is |
||
| 1030 | * completed |
||
| 1031 | * @return int|false Integer or false if the search could not be initialized |
||
| 1032 | * |
||
| 1033 | */ |
||
| 1034 | private function count($filter, $base, $attr = null, $limit = null, $offset = null, $skipHandling = false) { |
||
| 1072 | |||
| 1073 | /** |
||
| 1074 | * @param array $searchResults |
||
| 1075 | * @return int |
||
| 1076 | */ |
||
| 1077 | private function countEntriesInSearchResults($searchResults) { |
||
| 1088 | |||
| 1089 | /** |
||
| 1090 | * Executes an LDAP search |
||
| 1091 | * @param string $filter the LDAP filter for the search |
||
| 1092 | * @param array $base an array containing the LDAP subtree(s) that shall be searched |
||
| 1093 | * @param string|string[] $attr optional, array, one or more attributes that shall be |
||
| 1094 | * @param int $limit |
||
| 1095 | * @param int $offset |
||
| 1096 | * @param bool $skipHandling |
||
| 1097 | * @return array with the search result |
||
| 1098 | */ |
||
| 1099 | private function search($filter, $base, $attr = null, $limit = null, $offset = null, $skipHandling = false) { |
||
| 1191 | |||
| 1192 | /** |
||
| 1193 | * @param string $name |
||
| 1194 | * @return bool|mixed|string |
||
| 1195 | */ |
||
| 1196 | public function sanitizeUsername($name) { |
||
| 1213 | |||
| 1214 | /** |
||
| 1215 | * escapes (user provided) parts for LDAP filter |
||
| 1216 | * @param string $input, the provided value |
||
| 1217 | * @param bool $allowAsterisk whether in * at the beginning should be preserved |
||
| 1218 | * @return string the escaped string |
||
| 1219 | */ |
||
| 1220 | public function escapeFilterPart($input, $allowAsterisk = false) { |
||
| 1230 | |||
| 1231 | /** |
||
| 1232 | * combines the input filters with AND |
||
| 1233 | * @param string[] $filters the filters to connect |
||
| 1234 | * @return string the combined filter |
||
| 1235 | */ |
||
| 1236 | public function combineFilterWithAnd($filters) { |
||
| 1239 | |||
| 1240 | /** |
||
| 1241 | * combines the input filters with OR |
||
| 1242 | * @param string[] $filters the filters to connect |
||
| 1243 | * @return string the combined filter |
||
| 1244 | * Combines Filter arguments with OR |
||
| 1245 | */ |
||
| 1246 | public function combineFilterWithOr($filters) { |
||
| 1249 | |||
| 1250 | /** |
||
| 1251 | * combines the input filters with given operator |
||
| 1252 | * @param string[] $filters the filters to connect |
||
| 1253 | * @param string $operator either & or | |
||
| 1254 | * @return string the combined filter |
||
| 1255 | */ |
||
| 1256 | private function combineFilter($filters, $operator) { |
||
| 1267 | |||
| 1268 | /** |
||
| 1269 | * creates a filter part for to perform search for users |
||
| 1270 | * @param string $search the search term |
||
| 1271 | * @return string the final filter part to use in LDAP searches |
||
| 1272 | */ |
||
| 1273 | public function getFilterPartForUserSearch($search) { |
||
| 1278 | |||
| 1279 | /** |
||
| 1280 | * creates a filter part for to perform search for groups |
||
| 1281 | * @param string $search the search term |
||
| 1282 | * @return string the final filter part to use in LDAP searches |
||
| 1283 | */ |
||
| 1284 | public function getFilterPartForGroupSearch($search) { |
||
| 1289 | |||
| 1290 | /** |
||
| 1291 | * creates a filter part for searches by splitting up the given search |
||
| 1292 | * string into single words |
||
| 1293 | * @param string $search the search term |
||
| 1294 | * @param string[] $searchAttributes needs to have at least two attributes, |
||
| 1295 | * otherwise it does not make sense :) |
||
| 1296 | * @return string the final filter part to use in LDAP searches |
||
| 1297 | * @throws \Exception |
||
| 1298 | */ |
||
| 1299 | private function getAdvancedFilterPartForSearch($search, $searchAttributes) { |
||
| 1316 | |||
| 1317 | /** |
||
| 1318 | * creates a filter part for searches |
||
| 1319 | * @param string $search the search term |
||
| 1320 | * @param string[]|null $searchAttributes |
||
| 1321 | * @param string $fallbackAttribute a fallback attribute in case the user |
||
| 1322 | * did not define search attributes. Typically the display name attribute. |
||
| 1323 | * @return string the final filter part to use in LDAP searches |
||
| 1324 | */ |
||
| 1325 | private function getFilterPartForSearch($search, $searchAttributes, $fallbackAttribute) { |
||
| 1356 | |||
| 1357 | /** |
||
| 1358 | * returns the search term depending on whether we are allowed |
||
| 1359 | * list users found by ldap with the current input appended by |
||
| 1360 | * a * |
||
| 1361 | * @return string |
||
| 1362 | */ |
||
| 1363 | private function prepareSearchTerm($term) { |
||
| 1376 | |||
| 1377 | /** |
||
| 1378 | * returns the filter used for counting users |
||
| 1379 | * @return string |
||
| 1380 | */ |
||
| 1381 | public function getFilterForUserCount() { |
||
| 1389 | |||
| 1390 | /** |
||
| 1391 | * @param string $name |
||
| 1392 | * @param string $password |
||
| 1393 | * @return bool |
||
| 1394 | */ |
||
| 1395 | public function areCredentialsValid($name, $password) { |
||
| 1407 | |||
| 1408 | /** |
||
| 1409 | * reverse lookup of a DN given a known UUID |
||
| 1410 | * |
||
| 1411 | * @param string $uuid |
||
| 1412 | * @return string |
||
| 1413 | * @throws \Exception |
||
| 1414 | */ |
||
| 1415 | public function getUserDnByUuid($uuid) { |
||
| 1455 | |||
| 1456 | /** |
||
| 1457 | * auto-detects the directory's UUID attribute |
||
| 1458 | * @param string $dn a known DN used to check against |
||
| 1459 | * @param bool $isUser |
||
| 1460 | * @param bool $force the detection should be run, even if it is not set to auto |
||
| 1461 | * @return bool true on success, false otherwise |
||
| 1462 | */ |
||
| 1463 | private function detectUuidAttribute($dn, $isUser = true, $force = false) { |
||
| 1464 | View Code Duplication | if($isUser) { |
|
| 1465 | $uuidAttr = 'ldapUuidUserAttribute'; |
||
| 1466 | $uuidOverride = $this->connection->ldapExpertUUIDUserAttr; |
||
| 1467 | } else { |
||
| 1468 | $uuidAttr = 'ldapUuidGroupAttribute'; |
||
| 1469 | $uuidOverride = $this->connection->ldapExpertUUIDGroupAttr; |
||
| 1470 | } |
||
| 1471 | |||
| 1472 | if(($this->connection->$uuidAttr !== 'auto') && !$force) { |
||
| 1473 | return true; |
||
| 1474 | } |
||
| 1475 | |||
| 1476 | if (is_string($uuidOverride) && trim($uuidOverride) !== '' && !$force) { |
||
| 1477 | $this->connection->$uuidAttr = $uuidOverride; |
||
| 1478 | return true; |
||
| 1479 | } |
||
| 1480 | |||
| 1481 | // for now, supported attributes are entryUUID, nsuniqueid, objectGUID, ipaUniqueID |
||
| 1482 | $testAttributes = array('entryuuid', 'nsuniqueid', 'objectguid', 'guid', 'ipauniqueid'); |
||
| 1483 | |||
| 1484 | foreach($testAttributes as $attribute) { |
||
| 1485 | $value = $this->readAttribute($dn, $attribute); |
||
| 1486 | if(is_array($value) && isset($value[0]) && !empty($value[0])) { |
||
| 1487 | \OCP\Util::writeLog('user_ldap', |
||
| 1488 | 'Setting '.$attribute.' as '.$uuidAttr, |
||
| 1489 | \OCP\Util::DEBUG); |
||
| 1490 | $this->connection->$uuidAttr = $attribute; |
||
| 1491 | return true; |
||
| 1492 | } |
||
| 1493 | } |
||
| 1494 | \OCP\Util::writeLog('user_ldap', |
||
| 1495 | 'Could not autodetect the UUID attribute', |
||
| 1496 | \OCP\Util::ERROR); |
||
| 1497 | |||
| 1498 | return false; |
||
| 1499 | } |
||
| 1500 | |||
| 1501 | /** |
||
| 1502 | * @param string $dn |
||
| 1503 | * @param bool $isUser |
||
| 1504 | * @return string|bool |
||
| 1505 | */ |
||
| 1506 | public function getUUID($dn, $isUser = true) { |
||
| 1531 | |||
| 1532 | /** |
||
| 1533 | * converts a binary ObjectGUID into a string representation |
||
| 1534 | * @param string $oguid the ObjectGUID in it's binary form as retrieved from AD |
||
| 1535 | * @return string |
||
| 1536 | * @link http://www.php.net/manual/en/function.ldap-get-values-len.php#73198 |
||
| 1537 | */ |
||
| 1538 | private function convertObjectGUID2Str($oguid) { |
||
| 1557 | |||
| 1558 | /** |
||
| 1559 | * the first three blocks of the string-converted GUID happen to be in |
||
| 1560 | * reverse order. In order to use it in a filter, this needs to be |
||
| 1561 | * corrected. Furthermore the dashes need to be replaced and \\ preprended |
||
| 1562 | * to every two hax figures. |
||
| 1563 | * |
||
| 1564 | * If an invalid string is passed, it will be returned without change. |
||
| 1565 | * |
||
| 1566 | * @param string $guid |
||
| 1567 | * @return string |
||
| 1568 | */ |
||
| 1569 | public function formatGuid2ForFilterUser($guid) { |
||
| 1604 | |||
| 1605 | /** |
||
| 1606 | * gets a SID of the domain of the given dn |
||
| 1607 | * @param string $dn |
||
| 1608 | * @return string|bool |
||
| 1609 | */ |
||
| 1610 | public function getSID($dn) { |
||
| 1628 | |||
| 1629 | /** |
||
| 1630 | * converts a binary SID into a string representation |
||
| 1631 | * @param string $sid |
||
| 1632 | * @return string |
||
| 1633 | */ |
||
| 1634 | public function convertSID2Str($sid) { |
||
| 1666 | |||
| 1667 | /** |
||
| 1668 | * checks if the given DN is part of the given base DN(s) |
||
| 1669 | * @param string $dn the DN |
||
| 1670 | * @param string[] $bases array containing the allowed base DN or DNs |
||
| 1671 | * @return bool |
||
| 1672 | */ |
||
| 1673 | public function isDNPartOfBase($dn, $bases) { |
||
| 1688 | |||
| 1689 | /** |
||
| 1690 | * resets a running Paged Search operation |
||
| 1691 | */ |
||
| 1692 | private function abandonPagedSearch() { |
||
| 1701 | |||
| 1702 | /** |
||
| 1703 | * get a cookie for the next LDAP paged search |
||
| 1704 | * @param string $base a string with the base DN for the search |
||
| 1705 | * @param string $filter the search filter to identify the correct search |
||
| 1706 | * @param int $limit the limit (or 'pageSize'), to identify the correct search well |
||
| 1707 | * @param int $offset the offset for the new search to identify the correct search really good |
||
| 1708 | * @return string containing the key or empty if none is cached |
||
| 1709 | */ |
||
| 1710 | private function getPagedResultCookie($base, $filter, $limit, $offset) { |
||
| 1726 | |||
| 1727 | /** |
||
| 1728 | * checks whether an LDAP paged search operation has more pages that can be |
||
| 1729 | * retrieved, typically when offset and limit are provided. |
||
| 1730 | * |
||
| 1731 | * Be very careful to use it: the last cookie value, which is inspected, can |
||
| 1732 | * be reset by other operations. Best, call it immediately after a search(), |
||
| 1733 | * searchUsers() or searchGroups() call. count-methods are probably safe as |
||
| 1734 | * well. Don't rely on it with any fetchList-method. |
||
| 1735 | * @return bool |
||
| 1736 | */ |
||
| 1737 | public function hasMoreResults() { |
||
| 1750 | |||
| 1751 | /** |
||
| 1752 | * set a cookie for LDAP paged search run |
||
| 1753 | * @param string $base a string with the base DN for the search |
||
| 1754 | * @param string $filter the search filter to identify the correct search |
||
| 1755 | * @param int $limit the limit (or 'pageSize'), to identify the correct search well |
||
| 1756 | * @param int $offset the offset for the run search to identify the correct search really good |
||
| 1757 | * @param string $cookie string containing the cookie returned by ldap_control_paged_result_response |
||
| 1758 | * @return void |
||
| 1759 | */ |
||
| 1760 | private function setPagedResultCookie($base, $filter, $limit, $offset, $cookie) { |
||
| 1768 | |||
| 1769 | /** |
||
| 1770 | * Check whether the most recent paged search was successful. It flushed the state var. Use it always after a possible paged search. |
||
| 1771 | * @return boolean|null true on success, null or false otherwise |
||
| 1772 | */ |
||
| 1773 | public function getPagedSearchResultState() { |
||
| 1778 | |||
| 1779 | /** |
||
| 1780 | * Prepares a paged search, if possible |
||
| 1781 | * @param string $filter the LDAP filter for the search |
||
| 1782 | * @param string[] $bases an array containing the LDAP subtree(s) that shall be searched |
||
| 1783 | * @param string[] $attr optional, when a certain attribute shall be filtered outside |
||
| 1784 | * @param int $limit |
||
| 1785 | * @param int $offset |
||
| 1786 | * @return bool|true |
||
| 1787 | */ |
||
| 1788 | private function initPagedSearch($filter, $bases, $attr, $limit, $offset) { |
||
| 1856 | |||
| 1857 | } |
||
| 1858 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: