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 Security 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 Security, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 39 | class Security extends Controller implements TemplateGlobalProvider |
||
| 40 | { |
||
| 41 | |||
| 42 | private static $allowed_actions = array( |
||
|
|
|||
| 43 | 'index', |
||
| 44 | 'login', |
||
| 45 | 'logout', |
||
| 46 | 'basicauthlogin', |
||
| 47 | 'lostpassword', |
||
| 48 | 'passwordsent', |
||
| 49 | 'changepassword', |
||
| 50 | 'ping', |
||
| 51 | 'LoginForm', |
||
| 52 | 'ChangePasswordForm', |
||
| 53 | 'LostPasswordForm', |
||
| 54 | ); |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Default user name. Only used in dev-mode by {@link setDefaultAdmin()} |
||
| 58 | * |
||
| 59 | * @var string |
||
| 60 | * @see setDefaultAdmin() |
||
| 61 | */ |
||
| 62 | protected static $default_username; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Default password. Only used in dev-mode by {@link setDefaultAdmin()} |
||
| 66 | * |
||
| 67 | * @var string |
||
| 68 | * @see setDefaultAdmin() |
||
| 69 | */ |
||
| 70 | protected static $default_password; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * If set to TRUE to prevent sharing of the session across several sites |
||
| 74 | * in the domain. |
||
| 75 | * |
||
| 76 | * @config |
||
| 77 | * @var bool |
||
| 78 | */ |
||
| 79 | protected static $strict_path_checking = false; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * The password encryption algorithm to use by default. |
||
| 83 | * This is an arbitrary code registered through {@link PasswordEncryptor}. |
||
| 84 | * |
||
| 85 | * @config |
||
| 86 | * @var string |
||
| 87 | */ |
||
| 88 | private static $password_encryption_algorithm = 'blowfish'; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Showing "Remember me"-checkbox |
||
| 92 | * on loginform, and saving encrypted credentials to a cookie. |
||
| 93 | * |
||
| 94 | * @config |
||
| 95 | * @var bool |
||
| 96 | */ |
||
| 97 | private static $autologin_enabled = true; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Determine if login username may be remembered between login sessions |
||
| 101 | * If set to false this will disable autocomplete and prevent username persisting in the session |
||
| 102 | * |
||
| 103 | * @config |
||
| 104 | * @var bool |
||
| 105 | */ |
||
| 106 | private static $remember_username = true; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Location of word list to use for generating passwords |
||
| 110 | * |
||
| 111 | * @config |
||
| 112 | * @var string |
||
| 113 | */ |
||
| 114 | private static $word_list = './wordlist.txt'; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @config |
||
| 118 | * @var string |
||
| 119 | */ |
||
| 120 | private static $template = 'BlankPage'; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Template thats used to render the pages. |
||
| 124 | * |
||
| 125 | * @var string |
||
| 126 | * @config |
||
| 127 | */ |
||
| 128 | private static $template_main = 'Page'; |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Class to use for page rendering |
||
| 132 | * |
||
| 133 | * @var string |
||
| 134 | * @config |
||
| 135 | */ |
||
| 136 | private static $page_class = Page::class; |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Default message set used in permission failures. |
||
| 140 | * |
||
| 141 | * @config |
||
| 142 | * @var array|string |
||
| 143 | */ |
||
| 144 | private static $default_message_set; |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Random secure token, can be used as a crypto key internally. |
||
| 148 | * Generate one through 'sake dev/generatesecuretoken'. |
||
| 149 | * |
||
| 150 | * @config |
||
| 151 | * @var String |
||
| 152 | */ |
||
| 153 | private static $token; |
||
| 154 | |||
| 155 | /** |
||
| 156 | * The default login URL |
||
| 157 | * |
||
| 158 | * @config |
||
| 159 | * |
||
| 160 | * @var string |
||
| 161 | */ |
||
| 162 | private static $login_url = "Security/login"; |
||
| 163 | |||
| 164 | /** |
||
| 165 | * The default logout URL |
||
| 166 | * |
||
| 167 | * @config |
||
| 168 | * |
||
| 169 | * @var string |
||
| 170 | */ |
||
| 171 | private static $logout_url = "Security/logout"; |
||
| 172 | |||
| 173 | /** |
||
| 174 | * The default lost password URL |
||
| 175 | * |
||
| 176 | * @config |
||
| 177 | * |
||
| 178 | * @var string |
||
| 179 | */ |
||
| 180 | private static $lost_password_url = "Security/lostpassword"; |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Value of X-Frame-Options header |
||
| 184 | * |
||
| 185 | * @config |
||
| 186 | * @var string |
||
| 187 | */ |
||
| 188 | private static $frame_options = 'SAMEORIGIN'; |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Value of the X-Robots-Tag header (for the Security section) |
||
| 192 | * |
||
| 193 | * @config |
||
| 194 | * @var string |
||
| 195 | */ |
||
| 196 | private static $robots_tag = 'noindex, nofollow'; |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Enable or disable recording of login attempts |
||
| 200 | * through the {@link LoginRecord} object. |
||
| 201 | * |
||
| 202 | * @config |
||
| 203 | * @var boolean $login_recording |
||
| 204 | */ |
||
| 205 | private static $login_recording = false; |
||
| 206 | |||
| 207 | /** |
||
| 208 | * @var boolean If set to TRUE or FALSE, {@link database_is_ready()} |
||
| 209 | * will always return FALSE. Used for unit testing. |
||
| 210 | */ |
||
| 211 | static $force_database_is_ready = null; |
||
| 212 | |||
| 213 | /** |
||
| 214 | * When the database has once been verified as ready, it will not do the |
||
| 215 | * checks again. |
||
| 216 | * |
||
| 217 | * @var bool |
||
| 218 | */ |
||
| 219 | static $database_is_ready = false; |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Get all registered authenticators |
||
| 223 | * |
||
| 224 | * @return array Return an array of Authenticator objects |
||
| 225 | */ |
||
| 226 | public static function getAuthenticators() |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Check if a given authenticator is registered |
||
| 253 | * |
||
| 254 | * @param string $authenticator Name of the authenticator class to check |
||
| 255 | * @return bool Returns TRUE if the authenticator is registered, FALSE |
||
| 256 | * otherwise. |
||
| 257 | */ |
||
| 258 | public static function hasAuthenticator($authenticator) |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Register that we've had a permission failure trying to view the given page |
||
| 270 | * |
||
| 271 | * This will redirect to a login page. |
||
| 272 | * If you don't provide a messageSet, a default will be used. |
||
| 273 | * |
||
| 274 | * @param Controller $controller The controller that you were on to cause the permission |
||
| 275 | * failure. |
||
| 276 | * @param string|array $messageSet The message to show to the user. This |
||
| 277 | * can be a string, or a map of different |
||
| 278 | * messages for different contexts. |
||
| 279 | * If you pass an array, you can use the |
||
| 280 | * following keys: |
||
| 281 | * - default: The default message |
||
| 282 | * - alreadyLoggedIn: The message to |
||
| 283 | * show if the user |
||
| 284 | * is already logged |
||
| 285 | * in and lacks the |
||
| 286 | * permission to |
||
| 287 | * access the item. |
||
| 288 | * |
||
| 289 | * The alreadyLoggedIn value can contain a '%s' placeholder that will be replaced with a link |
||
| 290 | * to log in. |
||
| 291 | * @return HTTPResponse |
||
| 292 | */ |
||
| 293 | public static function permissionFailure($controller = null, $messageSet = null) |
||
| 387 | |||
| 388 | protected function init() |
||
| 404 | |||
| 405 | public function index() |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Get the selected authenticator for this request |
||
| 412 | * |
||
| 413 | * @return string Class name of Authenticator |
||
| 414 | * @throws LogicException |
||
| 415 | */ |
||
| 416 | protected function getAuthenticator() |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Get the login form to process according to the submitted data |
||
| 434 | * |
||
| 435 | * @return Form |
||
| 436 | * @throws Exception |
||
| 437 | */ |
||
| 438 | public function LoginForm() |
||
| 446 | |||
| 447 | /** |
||
| 448 | * Get the login forms for all available authentication methods |
||
| 449 | * |
||
| 450 | * @return array Returns an array of available login forms (array of Form |
||
| 451 | * objects). |
||
| 452 | * |
||
| 453 | * @todo Check how to activate/deactivate authentication methods |
||
| 454 | */ |
||
| 455 | public function getLoginForms() |
||
| 466 | |||
| 467 | |||
| 468 | /** |
||
| 469 | * Get a link to a security action |
||
| 470 | * |
||
| 471 | * @param string $action Name of the action |
||
| 472 | * @return string Returns the link to the given action |
||
| 473 | */ |
||
| 474 | public function Link($action = null) |
||
| 479 | |||
| 480 | /** |
||
| 481 | * This action is available as a keep alive, so user |
||
| 482 | * sessions don't timeout. A common use is in the admin. |
||
| 483 | */ |
||
| 484 | public function ping() |
||
| 488 | |||
| 489 | /** |
||
| 490 | * Log the currently logged in user out |
||
| 491 | * |
||
| 492 | * @param bool $redirect Redirect the user back to where they came. |
||
| 493 | * - If it's false, the code calling logout() is |
||
| 494 | * responsible for sending the user where-ever |
||
| 495 | * they should go. |
||
| 496 | * @return HTTPResponse|null |
||
| 497 | */ |
||
| 498 | public function logout($redirect = true) |
||
| 510 | |||
| 511 | /** |
||
| 512 | * Perform pre-login checking and prepare a response if available prior to login |
||
| 513 | * |
||
| 514 | * @return HTTPResponse Substitute response object if the login process should be curcumvented. |
||
| 515 | * Returns null if should proceed as normal. |
||
| 516 | */ |
||
| 517 | protected function preLogin() |
||
| 550 | |||
| 551 | /** |
||
| 552 | * Prepare the controller for handling the response to this request |
||
| 553 | * |
||
| 554 | * @param string $title Title to use |
||
| 555 | * @return Controller |
||
| 556 | */ |
||
| 557 | protected function getResponseController($title) |
||
| 581 | |||
| 582 | /** |
||
| 583 | * Combine the given forms into a formset with a tabbed interface |
||
| 584 | * |
||
| 585 | * @param array $forms List of LoginForm instances |
||
| 586 | * @return string |
||
| 587 | */ |
||
| 588 | protected function generateLoginFormSet($forms) |
||
| 597 | |||
| 598 | /** |
||
| 599 | * Get the HTML Content for the $Content area during login |
||
| 600 | * |
||
| 601 | * @param string &$messageType Type of message, if available, passed back to caller |
||
| 602 | * @return string Message in HTML format |
||
| 603 | */ |
||
| 604 | protected function getLoginMessage(&$messageType = null) |
||
| 619 | |||
| 620 | /** |
||
| 621 | * Set the next message to display for the security login page. Defaults to warning |
||
| 622 | * |
||
| 623 | * @param string $message Message |
||
| 624 | * @param string $messageType Message type. One of ValidationResult::TYPE_* |
||
| 625 | * @param string $messageCast Message cast. One of ValidationResult::CAST_* |
||
| 626 | */ |
||
| 627 | public static function setLoginMessage( |
||
| 636 | |||
| 637 | /** |
||
| 638 | * Clear login message |
||
| 639 | */ |
||
| 640 | public static function clearLoginMessage() |
||
| 644 | |||
| 645 | |||
| 646 | /** |
||
| 647 | * Show the "login" page |
||
| 648 | * |
||
| 649 | * For multiple authenticators, Security_MultiAuthenticatorLogin is used. |
||
| 650 | * See getTemplatesFor and getIncludeTemplate for how to override template logic |
||
| 651 | * |
||
| 652 | * @return string|HTTPResponse Returns the "login" page as HTML code. |
||
| 653 | */ |
||
| 654 | View Code Duplication | public function login($request) |
|
| 655 | { |
||
| 656 | // Check pre-login process |
||
| 657 | if ($response = $this->preLogin()) { |
||
| 658 | return $response; |
||
| 659 | } |
||
| 660 | |||
| 661 | // Create the login handler |
||
| 662 | $handler = $this->getAuthenticator()->getLoginHandler( |
||
| 663 | Controller::join_links($this->link(), 'login') |
||
| 664 | ); |
||
| 665 | |||
| 666 | return $this->delegateToHandler( |
||
| 667 | $handler, |
||
| 668 | _t('Security.LOGIN', 'Log in'), |
||
| 669 | $this->getTemplatesFor('login') |
||
| 670 | ); |
||
| 671 | } |
||
| 672 | |||
| 673 | /** |
||
| 674 | * Delegate to another RequestHandler, rendering any fragment arrays into an appropriate. |
||
| 675 | * controller. |
||
| 676 | * @param string $title The title of the form |
||
| 677 | * @param $template The template stack to render |
||
| 678 | */ |
||
| 679 | protected function delegateToHandler(RequestHandler $handler, $title, array $templates) |
||
| 736 | |||
| 737 | public function basicauthlogin() |
||
| 742 | |||
| 743 | /** |
||
| 744 | * Show the "lost password" page |
||
| 745 | * |
||
| 746 | * @return string Returns the "lost password" page as HTML code. |
||
| 747 | */ |
||
| 748 | View Code Duplication | public function lostpassword() |
|
| 760 | |||
| 761 | /** |
||
| 762 | * Create a link to the password reset form. |
||
| 763 | * |
||
| 764 | * GET parameters used: |
||
| 765 | * - m: member ID |
||
| 766 | * - t: plaintext token |
||
| 767 | * |
||
| 768 | * @param Member $member Member object associated with this link. |
||
| 769 | * @param string $autologinToken The auto login token. |
||
| 770 | * @return string |
||
| 771 | */ |
||
| 772 | public static function getPasswordResetLink($member, $autologinToken) |
||
| 780 | |||
| 781 | /** |
||
| 782 | * Show the "change password" page. |
||
| 783 | * This page can either be called directly by logged-in users |
||
| 784 | * (in which case they need to provide their old password), |
||
| 785 | * or through a link emailed through {@link lostpassword()}. |
||
| 786 | * In this case no old password is required, authentication is ensured |
||
| 787 | * through the Member.AutoLoginHash property. |
||
| 788 | * |
||
| 789 | * @see ChangePasswordForm |
||
| 790 | * |
||
| 791 | * @return string|HTTPRequest Returns the "change password" page as HTML code, or a redirect response |
||
| 792 | */ |
||
| 793 | public function changepassword() |
||
| 867 | |||
| 868 | /** |
||
| 869 | * Factory method for the lost password form |
||
| 870 | * |
||
| 871 | * @skipUpgrade |
||
| 872 | * @return ChangePasswordForm Returns the lost password form |
||
| 873 | */ |
||
| 874 | public function ChangePasswordForm() |
||
| 878 | |||
| 879 | /** |
||
| 880 | * Determine the list of templates to use for rendering the given action. |
||
| 881 | * |
||
| 882 | * @skipUpgrade |
||
| 883 | * @param string $action |
||
| 884 | * @return array Template list |
||
| 885 | */ |
||
| 886 | public function getTemplatesFor($action) |
||
| 899 | |||
| 900 | /** |
||
| 901 | * Return an existing member with administrator privileges, or create one of necessary. |
||
| 902 | * |
||
| 903 | * Will create a default 'Administrators' group if no group is found |
||
| 904 | * with an ADMIN permission. Will create a new 'Admin' member with administrative permissions |
||
| 905 | * if no existing Member with these permissions is found. |
||
| 906 | * |
||
| 907 | * Important: Any newly created administrator accounts will NOT have valid |
||
| 908 | * login credentials (Email/Password properties), which means they can't be used for login |
||
| 909 | * purposes outside of any default credentials set through {@link Security::setDefaultAdmin()}. |
||
| 910 | * |
||
| 911 | * @return Member |
||
| 912 | */ |
||
| 913 | public static function findAnAdministrator() |
||
| 964 | |||
| 965 | /** |
||
| 966 | * Flush the default admin credentials |
||
| 967 | */ |
||
| 968 | public static function clear_default_admin() |
||
| 973 | |||
| 974 | |||
| 975 | /** |
||
| 976 | * Set a default admin in dev-mode |
||
| 977 | * |
||
| 978 | * This will set a static default-admin which is not existing |
||
| 979 | * as a database-record. By this workaround we can test pages in dev-mode |
||
| 980 | * with a unified login. Submitted login-credentials are first checked |
||
| 981 | * against this static information in {@link Security::authenticate()}. |
||
| 982 | * |
||
| 983 | * @param string $username The user name |
||
| 984 | * @param string $password The password (in cleartext) |
||
| 985 | * @return bool True if successfully set |
||
| 986 | */ |
||
| 987 | public static function setDefaultAdmin($username, $password) |
||
| 998 | |||
| 999 | /** |
||
| 1000 | * Checks if the passed credentials are matching the default-admin. |
||
| 1001 | * Compares cleartext-password set through Security::setDefaultAdmin(). |
||
| 1002 | * |
||
| 1003 | * @param string $username |
||
| 1004 | * @param string $password |
||
| 1005 | * @return bool |
||
| 1006 | */ |
||
| 1007 | public static function check_default_admin($username, $password) |
||
| 1015 | |||
| 1016 | /** |
||
| 1017 | * Check that the default admin account has been set. |
||
| 1018 | */ |
||
| 1019 | public static function has_default_admin() |
||
| 1023 | |||
| 1024 | /** |
||
| 1025 | * Get default admin username |
||
| 1026 | * |
||
| 1027 | * @return string |
||
| 1028 | */ |
||
| 1029 | public static function default_admin_username() |
||
| 1033 | |||
| 1034 | /** |
||
| 1035 | * Get default admin password |
||
| 1036 | * |
||
| 1037 | * @return string |
||
| 1038 | */ |
||
| 1039 | public static function default_admin_password() |
||
| 1043 | |||
| 1044 | /** |
||
| 1045 | * Encrypt a password according to the current password encryption settings. |
||
| 1046 | * If the settings are so that passwords shouldn't be encrypted, the |
||
| 1047 | * result is simple the clear text password with an empty salt except when |
||
| 1048 | * a custom algorithm ($algorithm parameter) was passed. |
||
| 1049 | * |
||
| 1050 | * @param string $password The password to encrypt |
||
| 1051 | * @param string $salt Optional: The salt to use. If it is not passed, but |
||
| 1052 | * needed, the method will automatically create a |
||
| 1053 | * random salt that will then be returned as return value. |
||
| 1054 | * @param string $algorithm Optional: Use another algorithm to encrypt the |
||
| 1055 | * password (so that the encryption algorithm can be changed over the time). |
||
| 1056 | * @param Member $member Optional |
||
| 1057 | * @return mixed Returns an associative array containing the encrypted |
||
| 1058 | * password and the used salt in the form: |
||
| 1059 | * <code> |
||
| 1060 | * array( |
||
| 1061 | * 'password' => string, |
||
| 1062 | * 'salt' => string, |
||
| 1063 | * 'algorithm' => string, |
||
| 1064 | * 'encryptor' => PasswordEncryptor instance |
||
| 1065 | * ) |
||
| 1066 | * </code> |
||
| 1067 | * If the passed algorithm is invalid, FALSE will be returned. |
||
| 1068 | * |
||
| 1069 | * @see encrypt_passwords() |
||
| 1070 | */ |
||
| 1071 | public static function encrypt_password($password, $salt = null, $algorithm = null, $member = null) |
||
| 1090 | |||
| 1091 | /** |
||
| 1092 | * Checks the database is in a state to perform security checks. |
||
| 1093 | * See {@link DatabaseAdmin->init()} for more information. |
||
| 1094 | * |
||
| 1095 | * @return bool |
||
| 1096 | */ |
||
| 1097 | public static function database_is_ready() |
||
| 1145 | |||
| 1146 | /** |
||
| 1147 | * Enable or disable recording of login attempts |
||
| 1148 | * through the {@link LoginRecord} object. |
||
| 1149 | * |
||
| 1150 | * @deprecated 4.0 Use the "Security.login_recording" config setting instead |
||
| 1151 | * @param boolean $bool |
||
| 1152 | */ |
||
| 1153 | public static function set_login_recording($bool) |
||
| 1158 | |||
| 1159 | /** |
||
| 1160 | * @deprecated 4.0 Use the "Security.login_recording" config setting instead |
||
| 1161 | * @return boolean |
||
| 1162 | */ |
||
| 1163 | public static function login_recording() |
||
| 1168 | |||
| 1169 | /** |
||
| 1170 | * @config |
||
| 1171 | * @var string Set the default login dest |
||
| 1172 | * This is the URL that users will be redirected to after they log in, |
||
| 1173 | * if they haven't logged in en route to access a secured page. |
||
| 1174 | * By default, this is set to the homepage. |
||
| 1175 | */ |
||
| 1176 | private static $default_login_dest = ""; |
||
| 1177 | |||
| 1178 | protected static $ignore_disallowed_actions = false; |
||
| 1179 | |||
| 1180 | /** |
||
| 1181 | * Set to true to ignore access to disallowed actions, rather than returning permission failure |
||
| 1182 | * Note that this is just a flag that other code needs to check with Security::ignore_disallowed_actions() |
||
| 1183 | * @param $flag True or false |
||
| 1184 | */ |
||
| 1185 | public static function set_ignore_disallowed_actions($flag) |
||
| 1189 | |||
| 1190 | public static function ignore_disallowed_actions() |
||
| 1194 | |||
| 1195 | /** |
||
| 1196 | * Get the URL of the log-in page. |
||
| 1197 | * |
||
| 1198 | * To update the login url use the "Security.login_url" config setting. |
||
| 1199 | * |
||
| 1200 | * @return string |
||
| 1201 | */ |
||
| 1202 | public static function login_url() |
||
| 1206 | |||
| 1207 | |||
| 1208 | /** |
||
| 1209 | * Get the URL of the logout page. |
||
| 1210 | * |
||
| 1211 | * To update the logout url use the "Security.logout_url" config setting. |
||
| 1212 | * |
||
| 1213 | * @return string |
||
| 1214 | */ |
||
| 1215 | public static function logout_url() |
||
| 1219 | |||
| 1220 | /** |
||
| 1221 | * Get the URL of the logout page. |
||
| 1222 | * |
||
| 1223 | * To update the logout url use the "Security.logout_url" config setting. |
||
| 1224 | * |
||
| 1225 | * @return string |
||
| 1226 | */ |
||
| 1227 | public static function lost_password_url() |
||
| 1231 | |||
| 1232 | /** |
||
| 1233 | * Defines global accessible templates variables. |
||
| 1234 | * |
||
| 1235 | * @return array |
||
| 1236 | */ |
||
| 1237 | public static function get_template_global_variables() |
||
| 1245 | } |
||
| 1246 |