Complex classes like auth_plugin_authmysql 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 auth_plugin_authmysql, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class auth_plugin_authmysql extends DokuWiki_Auth_Plugin { |
||
| 15 | /** @var resource holds the database connection */ |
||
| 16 | protected $dbcon = 0; |
||
| 17 | /** @var int database version*/ |
||
| 18 | protected $dbver = 0; |
||
| 19 | /** @var int database revision */ |
||
| 20 | protected $dbrev = 0; |
||
| 21 | /** @var int database subrevision */ |
||
| 22 | protected $dbsub = 0; |
||
| 23 | |||
| 24 | /** @var array cache to avoid re-reading user info data */ |
||
| 25 | protected $cacheUserInfo = array(); |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Constructor |
||
| 29 | * |
||
| 30 | * checks if the mysql interface is available, otherwise it will |
||
| 31 | * set the variable $success of the basis class to false |
||
| 32 | * |
||
| 33 | * @author Matthias Grimm <[email protected]> |
||
| 34 | */ |
||
| 35 | public function __construct() { |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Check if the given config strings are set |
||
| 116 | * |
||
| 117 | * @author Matthias Grimm <[email protected]> |
||
| 118 | * |
||
| 119 | * @param string[] $keys |
||
| 120 | * @param bool $wop is this a check for a write operation? |
||
| 121 | * @return bool |
||
| 122 | */ |
||
| 123 | protected function _chkcnf($keys, $wop = false) { |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Checks if the given user exists and the given plaintext password |
||
| 140 | * is correct. Furtheron it might be checked wether the user is |
||
| 141 | * member of the right group |
||
| 142 | * |
||
| 143 | * Depending on which SQL string is defined in the config, password |
||
| 144 | * checking is done here (getpass) or by the database (passcheck) |
||
| 145 | * |
||
| 146 | * @param string $user user who would like access |
||
| 147 | * @param string $pass user's clear text password to check |
||
| 148 | * @return bool |
||
| 149 | * |
||
| 150 | * @author Andreas Gohr <[email protected]> |
||
| 151 | * @author Matthias Grimm <[email protected]> |
||
| 152 | */ |
||
| 153 | public function checkPass($user, $pass) { |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Return user info |
||
| 177 | * |
||
| 178 | * @author Andreas Gohr <[email protected]> |
||
| 179 | * @author Matthias Grimm <[email protected]> |
||
| 180 | * |
||
| 181 | * @param string $user user login to get data for |
||
| 182 | * @param bool $requireGroups when true, group membership information should be included in the returned array; |
||
| 183 | * when false, it maybe included, but is not required by the caller |
||
| 184 | * @return array|bool |
||
| 185 | */ |
||
| 186 | public function getUserData($user, $requireGroups=true) { |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Create a new User. Returns false if the user already exists, |
||
| 204 | * null when an error occurred and true if everything went well. |
||
| 205 | * |
||
| 206 | * The new user will be added to the default group by this |
||
| 207 | * function if grps are not specified (default behaviour). |
||
| 208 | * |
||
| 209 | * @author Andreas Gohr <[email protected]> |
||
| 210 | * @author Chris Smith <[email protected]> |
||
| 211 | * @author Matthias Grimm <[email protected]> |
||
| 212 | * |
||
| 213 | * @param string $user nick of the user |
||
| 214 | * @param string $pwd clear text password |
||
| 215 | * @param string $name full name of the user |
||
| 216 | * @param string $mail email address |
||
| 217 | * @param array $grps array of groups the user should become member of |
||
| 218 | * @return bool|null |
||
| 219 | */ |
||
| 220 | public function createUser($user, $pwd, $name, $mail, $grps = null) { |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Modify user data |
||
| 252 | * |
||
| 253 | * An existing user dataset will be modified. Changes are given in an array. |
||
| 254 | * |
||
| 255 | * The dataset update will be rejected if the user name should be changed |
||
| 256 | * to an already existing one. |
||
| 257 | * |
||
| 258 | * The password must be provided unencrypted. Pasword encryption is done |
||
| 259 | * automatically if configured. |
||
| 260 | * |
||
| 261 | * If one or more groups can't be updated, an error will be set. In |
||
| 262 | * this case the dataset might already be changed and we can't rollback |
||
| 263 | * the changes. Transactions would be really useful here. |
||
| 264 | * |
||
| 265 | * modifyUser() may be called without SQL statements defined that are |
||
| 266 | * needed to change group membership (for example if only the user profile |
||
| 267 | * should be modified). In this case we assure that we don't touch groups |
||
| 268 | * even when $changes['grps'] is set by mistake. |
||
| 269 | * |
||
| 270 | * @author Chris Smith <[email protected]> |
||
| 271 | * @author Matthias Grimm <[email protected]> |
||
| 272 | * |
||
| 273 | * @param string $user nick of the user to be changed |
||
| 274 | * @param array $changes array of field/value pairs to be changed (password will be clear text) |
||
| 275 | * @return bool true on success, false on error |
||
| 276 | */ |
||
| 277 | public function modifyUser($user, $changes) { |
||
| 318 | |||
| 319 | /** |
||
| 320 | * [public function] |
||
| 321 | * |
||
| 322 | * Remove one or more users from the list of registered users |
||
| 323 | * |
||
| 324 | * @param array $users array of users to be deleted |
||
| 325 | * @return int the number of users deleted |
||
| 326 | * |
||
| 327 | * @author Christopher Smith <[email protected]> |
||
| 328 | * @author Matthias Grimm <[email protected]> |
||
| 329 | */ |
||
| 330 | function deleteUsers($users) { |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Counts users which meet certain $filter criteria. |
||
| 352 | * |
||
| 353 | * @author Matthias Grimm <[email protected]> |
||
| 354 | * |
||
| 355 | * @param array $filter filter criteria in item/pattern pairs |
||
| 356 | * @return int count of found users |
||
| 357 | */ |
||
| 358 | public function getUserCount($filter = array()) { |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Bulk retrieval of user data |
||
| 380 | * |
||
| 381 | * @author Matthias Grimm <[email protected]> |
||
| 382 | * |
||
| 383 | * @param int $first index of first user to be returned |
||
| 384 | * @param int $limit max number of users to be returned |
||
| 385 | * @param array $filter array of field/pattern pairs |
||
| 386 | * @return array userinfo (refer getUserData for internal userinfo details) |
||
| 387 | */ |
||
| 388 | public function retrieveUsers($first = 0, $limit = 0, $filter = array()) { |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Give user membership of a group |
||
| 418 | * |
||
| 419 | * @author Matthias Grimm <[email protected]> |
||
| 420 | * |
||
| 421 | * @param string $user |
||
| 422 | * @param string $group |
||
| 423 | * @return bool true on success, false on error |
||
| 424 | */ |
||
| 425 | protected function joinGroup($user, $group) { |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Remove user from a group |
||
| 439 | * |
||
| 440 | * @author Matthias Grimm <[email protected]> |
||
| 441 | * |
||
| 442 | * @param string $user user that leaves a group |
||
| 443 | * @param string $group group to leave |
||
| 444 | * @return bool |
||
| 445 | */ |
||
| 446 | protected function leaveGroup($user, $group) { |
||
| 457 | |||
| 458 | /** |
||
| 459 | * MySQL is case-insensitive |
||
| 460 | */ |
||
| 461 | public function isCaseSensitive() { |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Adds a user to a group. |
||
| 467 | * |
||
| 468 | * If $force is set to true non existing groups would be created. |
||
| 469 | * |
||
| 470 | * The database connection must already be established. Otherwise |
||
| 471 | * this function does nothing and returns 'false'. It is strongly |
||
| 472 | * recommended to call this function only after all participating |
||
| 473 | * tables (group and usergroup) have been locked. |
||
| 474 | * |
||
| 475 | * @author Matthias Grimm <[email protected]> |
||
| 476 | * |
||
| 477 | * @param string $user user to add to a group |
||
| 478 | * @param string $group name of the group |
||
| 479 | * @param bool $force create missing groups |
||
| 480 | * @return bool true on success, false on error |
||
| 481 | */ |
||
| 482 | protected function _addUserToGroup($user, $group, $force = false) { |
||
| 517 | |||
| 518 | /** |
||
| 519 | * Remove user from a group |
||
| 520 | * |
||
| 521 | * @author Matthias Grimm <[email protected]> |
||
| 522 | * |
||
| 523 | * @param string $user user that leaves a group |
||
| 524 | * @param string $group group to leave |
||
| 525 | * @return bool true on success, false on error |
||
| 526 | */ |
||
| 527 | protected function _delUserFromGroup($user, $group) { |
||
| 550 | |||
| 551 | /** |
||
| 552 | * Retrieves a list of groups the user is a member off. |
||
| 553 | * |
||
| 554 | * The database connection must already be established |
||
| 555 | * for this function to work. Otherwise it will return |
||
| 556 | * false. |
||
| 557 | * |
||
| 558 | * @author Matthias Grimm <[email protected]> |
||
| 559 | * |
||
| 560 | * @param string $user user whose groups should be listed |
||
| 561 | * @return bool|array false on error, all groups on success |
||
| 562 | */ |
||
| 563 | protected function _getGroups($user) { |
||
| 579 | |||
| 580 | /** |
||
| 581 | * Retrieves the user id of a given user name |
||
| 582 | * |
||
| 583 | * The database connection must already be established |
||
| 584 | * for this function to work. Otherwise it will return |
||
| 585 | * false. |
||
| 586 | * |
||
| 587 | * @author Matthias Grimm <[email protected]> |
||
| 588 | * |
||
| 589 | * @param string $user user whose id is desired |
||
| 590 | * @return mixed user id |
||
| 591 | */ |
||
| 592 | protected function _getUserID($user) { |
||
| 600 | |||
| 601 | /** |
||
| 602 | * Adds a new User to the database. |
||
| 603 | * |
||
| 604 | * The database connection must already be established |
||
| 605 | * for this function to work. Otherwise it will return |
||
| 606 | * false. |
||
| 607 | * |
||
| 608 | * @author Andreas Gohr <[email protected]> |
||
| 609 | * @author Chris Smith <[email protected]> |
||
| 610 | * @author Matthias Grimm <[email protected]> |
||
| 611 | * |
||
| 612 | * @param string $user login of the user |
||
| 613 | * @param string $pwd encrypted password |
||
| 614 | * @param string $name full name of the user |
||
| 615 | * @param string $mail email address |
||
| 616 | * @param array $grps array of groups the user should become member of |
||
| 617 | * @return bool |
||
| 618 | */ |
||
| 619 | protected function _addUser($user, $pwd, $name, $mail, $grps) { |
||
| 651 | |||
| 652 | /** |
||
| 653 | * Deletes a given user and all his group references. |
||
| 654 | * |
||
| 655 | * The database connection must already be established |
||
| 656 | * for this function to work. Otherwise it will return |
||
| 657 | * false. |
||
| 658 | * |
||
| 659 | * @author Matthias Grimm <[email protected]> |
||
| 660 | * |
||
| 661 | * @param string $user username of the user to be deleted |
||
| 662 | * @return bool |
||
| 663 | */ |
||
| 664 | protected function _delUser($user) { |
||
| 679 | |||
| 680 | /** |
||
| 681 | * Flush cached user information |
||
| 682 | * |
||
| 683 | * @author Christopher Smith <[email protected]> |
||
| 684 | * |
||
| 685 | * @param string $user username of the user whose data is to be removed from the cache |
||
| 686 | * if null, empty the whole cache |
||
| 687 | */ |
||
| 688 | protected function _flushUserInfoCache($user=null) { |
||
| 695 | |||
| 696 | /** |
||
| 697 | * Quick lookup to see if a user's information has been cached |
||
| 698 | * |
||
| 699 | * This test does not need a database connection or read lock |
||
| 700 | * |
||
| 701 | * @author Christopher Smith <[email protected]> |
||
| 702 | * |
||
| 703 | * @param string $user username to be looked up in the cache |
||
| 704 | * @param bool $requireGroups true, if cached info should include group memberships |
||
| 705 | * |
||
| 706 | * @return bool existence of required user information in the cache |
||
| 707 | */ |
||
| 708 | protected function _cacheExists($user, $requireGroups=true) { |
||
| 721 | |||
| 722 | /** |
||
| 723 | * Get a user's information |
||
| 724 | * |
||
| 725 | * The database connection must already be established for this function to work. |
||
| 726 | * |
||
| 727 | * @author Christopher Smith <[email protected]> |
||
| 728 | * |
||
| 729 | * @param string $user username of the user whose information is being reterieved |
||
| 730 | * @param bool $requireGroups true if group memberships should be included |
||
| 731 | * @param bool $useCache true if ok to return cached data & to cache returned data |
||
| 732 | * |
||
| 733 | * @return mixed false|array false if the user doesn't exist |
||
| 734 | * array containing user information if user does exist |
||
| 735 | */ |
||
| 736 | protected function _getUserInfo($user, $requireGroups=true, $useCache=true) { |
||
| 757 | |||
| 758 | /** |
||
| 759 | * retrieveUserInfo |
||
| 760 | * |
||
| 761 | * Gets the data for a specific user. The database connection |
||
| 762 | * must already be established for this function to work. |
||
| 763 | * Otherwise it will return 'false'. |
||
| 764 | * |
||
| 765 | * @author Matthias Grimm <[email protected]> |
||
| 766 | * |
||
| 767 | * @param string $user user's nick to get data for |
||
| 768 | * @return false|array false on error, user info on success |
||
| 769 | */ |
||
| 770 | protected function _retrieveUserInfo($user) { |
||
| 779 | |||
| 780 | /** |
||
| 781 | * Updates the user info in the database |
||
| 782 | * |
||
| 783 | * Update a user data structure in the database according changes |
||
| 784 | * given in an array. The user name can only be changes if it didn't |
||
| 785 | * exists already. If the new user name exists the update procedure |
||
| 786 | * will be aborted. The database keeps unchanged. |
||
| 787 | * |
||
| 788 | * The database connection has already to be established for this |
||
| 789 | * function to work. Otherwise it will return 'false'. |
||
| 790 | * |
||
| 791 | * The password will be encrypted if necessary. |
||
| 792 | * |
||
| 793 | * @param string $user user's nick being updated |
||
| 794 | * @param array $changes array of items to change as pairs of item and value |
||
| 795 | * @return bool true on success or false on error |
||
| 796 | * |
||
| 797 | * @author Matthias Grimm <[email protected]> |
||
| 798 | */ |
||
| 799 | protected function _updateUserInfo($user, $changes) { |
||
| 844 | |||
| 845 | /** |
||
| 846 | * Retrieves the group id of a given group name |
||
| 847 | * |
||
| 848 | * The database connection must already be established |
||
| 849 | * for this function to work. Otherwise it will return |
||
| 850 | * false. |
||
| 851 | * |
||
| 852 | * @author Matthias Grimm <[email protected]> |
||
| 853 | * |
||
| 854 | * @param string $group group name which id is desired |
||
| 855 | * @return false|string group id |
||
| 856 | */ |
||
| 857 | protected function _getGroupID($group) { |
||
| 865 | |||
| 866 | /** |
||
| 867 | * Opens a connection to a database and saves the handle for further |
||
| 868 | * usage in the object. The successful call to this functions is |
||
| 869 | * essential for most functions in this object. |
||
| 870 | * |
||
| 871 | * @author Matthias Grimm <[email protected]> |
||
| 872 | * |
||
| 873 | * @return bool |
||
| 874 | */ |
||
| 875 | protected function _openDB() { |
||
| 905 | |||
| 906 | /** |
||
| 907 | * Closes a database connection. |
||
| 908 | * |
||
| 909 | * @author Matthias Grimm <[email protected]> |
||
| 910 | */ |
||
| 911 | protected function _closeDB() { |
||
| 917 | |||
| 918 | /** |
||
| 919 | * Sends a SQL query to the database and transforms the result into |
||
| 920 | * an associative array. |
||
| 921 | * |
||
| 922 | * This function is only able to handle queries that returns a |
||
| 923 | * table such as SELECT. |
||
| 924 | * |
||
| 925 | * @author Matthias Grimm <[email protected]> |
||
| 926 | * |
||
| 927 | * @param string $query SQL string that contains the query |
||
| 928 | * @return array|false with the result table |
||
| 929 | */ |
||
| 930 | protected function _queryDB($query) { |
||
| 948 | |||
| 949 | /** |
||
| 950 | * Sends a SQL query to the database |
||
| 951 | * |
||
| 952 | * This function is only able to handle queries that returns |
||
| 953 | * either nothing or an id value such as INPUT, DELETE, UPDATE, etc. |
||
| 954 | * |
||
| 955 | * @author Matthias Grimm <[email protected]> |
||
| 956 | * |
||
| 957 | * @param string $query SQL string that contains the query |
||
| 958 | * @return int|bool insert id or 0, false on error |
||
| 959 | */ |
||
| 960 | protected function _modifyDB($query) { |
||
| 975 | |||
| 976 | /** |
||
| 977 | * Locked a list of tables for exclusive access so that modifications |
||
| 978 | * to the database can't be disturbed by other threads. The list |
||
| 979 | * could be set with $conf['plugin']['authmysql']['TablesToLock'] = array() |
||
| 980 | * |
||
| 981 | * If aliases for tables are used in SQL statements, also this aliases |
||
| 982 | * must be locked. For eg. you use a table 'user' and the alias 'u' in |
||
| 983 | * some sql queries, the array must looks like this (order is important): |
||
| 984 | * array("user", "user AS u"); |
||
| 985 | * |
||
| 986 | * MySQL V3 is not able to handle transactions with COMMIT/ROLLBACK |
||
| 987 | * so that this functionality is simulated by this function. Nevertheless |
||
| 988 | * it is not as powerful as transactions, it is a good compromise in safty. |
||
| 989 | * |
||
| 990 | * @author Matthias Grimm <[email protected]> |
||
| 991 | * |
||
| 992 | * @param string $mode could be 'READ' or 'WRITE' |
||
| 993 | * @return bool |
||
| 994 | */ |
||
| 995 | protected function _lockTables($mode) { |
||
| 1013 | |||
| 1014 | /** |
||
| 1015 | * Unlock locked tables. All existing locks of this thread will be |
||
| 1016 | * abrogated. |
||
| 1017 | * |
||
| 1018 | * @author Matthias Grimm <[email protected]> |
||
| 1019 | * |
||
| 1020 | * @return bool |
||
| 1021 | */ |
||
| 1022 | protected function _unlockTables() { |
||
| 1029 | |||
| 1030 | /** |
||
| 1031 | * Transforms the filter settings in an filter string for a SQL database |
||
| 1032 | * The database connection must already be established, otherwise the |
||
| 1033 | * original SQL string without filter criteria will be returned. |
||
| 1034 | * |
||
| 1035 | * @author Matthias Grimm <[email protected]> |
||
| 1036 | * |
||
| 1037 | * @param string $sql SQL string to which the $filter criteria should be added |
||
| 1038 | * @param array $filter array of filter criteria as pairs of item and pattern |
||
| 1039 | * @return string SQL string with attached $filter criteria on success, original SQL string on error |
||
| 1040 | */ |
||
| 1041 | protected function _createSQLFilter($sql, $filter) { |
||
| 1075 | |||
| 1076 | /** |
||
| 1077 | * Escape a string for insertion into the database |
||
| 1078 | * |
||
| 1079 | * @author Andreas Gohr <[email protected]> |
||
| 1080 | * |
||
| 1081 | * @param string $string The string to escape |
||
| 1082 | * @param boolean $like Escape wildcard chars as well? |
||
| 1083 | * @return string |
||
| 1084 | */ |
||
| 1085 | protected function _escape($string, $like = false) { |
||
| 1096 | |||
| 1097 | /** |
||
| 1098 | * Wrapper around msg() but outputs only when debug is enabled |
||
| 1099 | * |
||
| 1100 | * @param string $message |
||
| 1101 | * @param int $err |
||
| 1102 | * @param int $line |
||
| 1103 | * @param string $file |
||
| 1104 | * @return void |
||
| 1105 | */ |
||
| 1106 | protected function _debug($message, $err, $line, $file) { |
||
| 1110 | } |
||
| 1111 |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.