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 |
||
7 | class Security extends Controller implements TemplateGlobalProvider { |
||
8 | |||
9 | private static $allowed_actions = array( |
||
|
|||
10 | 'index', |
||
11 | 'login', |
||
12 | 'logout', |
||
13 | 'basicauthlogin', |
||
14 | 'lostpassword', |
||
15 | 'passwordsent', |
||
16 | 'changepassword', |
||
17 | 'ping', |
||
18 | 'LoginForm', |
||
19 | 'ChangePasswordForm', |
||
20 | 'LostPasswordForm', |
||
21 | ); |
||
22 | |||
23 | /** |
||
24 | * Default user name. Only used in dev-mode by {@link setDefaultAdmin()} |
||
25 | * |
||
26 | * @var string |
||
27 | * @see setDefaultAdmin() |
||
28 | */ |
||
29 | protected static $default_username; |
||
30 | |||
31 | /** |
||
32 | * Default password. Only used in dev-mode by {@link setDefaultAdmin()} |
||
33 | * |
||
34 | * @var string |
||
35 | * @see setDefaultAdmin() |
||
36 | */ |
||
37 | protected static $default_password; |
||
38 | |||
39 | /** |
||
40 | * If set to TRUE to prevent sharing of the session across several sites |
||
41 | * in the domain. |
||
42 | * |
||
43 | * @config |
||
44 | * @var bool |
||
45 | */ |
||
46 | protected static $strict_path_checking = false; |
||
47 | |||
48 | /** |
||
49 | * The password encryption algorithm to use by default. |
||
50 | * This is an arbitrary code registered through {@link PasswordEncryptor}. |
||
51 | * |
||
52 | * @config |
||
53 | * @var string |
||
54 | */ |
||
55 | private static $password_encryption_algorithm = 'blowfish'; |
||
56 | |||
57 | /** |
||
58 | * Showing "Remember me"-checkbox |
||
59 | * on loginform, and saving encrypted credentials to a cookie. |
||
60 | * |
||
61 | * @config |
||
62 | * @var bool |
||
63 | */ |
||
64 | private static $autologin_enabled = true; |
||
65 | |||
66 | /** |
||
67 | * Determine if login username may be remembered between login sessions |
||
68 | * If set to false this will disable autocomplete and prevent username persisting in the session |
||
69 | * |
||
70 | * @config |
||
71 | * @var bool |
||
72 | */ |
||
73 | private static $remember_username = true; |
||
74 | |||
75 | /** |
||
76 | * Location of word list to use for generating passwords |
||
77 | * |
||
78 | * @config |
||
79 | * @var string |
||
80 | */ |
||
81 | private static $word_list = './wordlist.txt'; |
||
82 | |||
83 | /** |
||
84 | * @config |
||
85 | * @var string |
||
86 | */ |
||
87 | private static $template = 'BlankPage'; |
||
88 | |||
89 | /** |
||
90 | * Template thats used to render the pages. |
||
91 | * |
||
92 | * @var string |
||
93 | * @config |
||
94 | */ |
||
95 | private static $template_main = 'Page'; |
||
96 | |||
97 | /** |
||
98 | * Default message set used in permission failures. |
||
99 | * |
||
100 | * @config |
||
101 | * @var array|string |
||
102 | */ |
||
103 | private static $default_message_set; |
||
104 | |||
105 | /** |
||
106 | * Random secure token, can be used as a crypto key internally. |
||
107 | * Generate one through 'sake dev/generatesecuretoken'. |
||
108 | * |
||
109 | * @config |
||
110 | * @var String |
||
111 | */ |
||
112 | private static $token; |
||
113 | |||
114 | /** |
||
115 | * The default login URL |
||
116 | * |
||
117 | * @config |
||
118 | * |
||
119 | * @var string |
||
120 | */ |
||
121 | private static $login_url = "Security/login"; |
||
122 | |||
123 | /** |
||
124 | * The default logout URL |
||
125 | * |
||
126 | * @config |
||
127 | * |
||
128 | * @var string |
||
129 | */ |
||
130 | private static $logout_url = "Security/logout"; |
||
131 | |||
132 | /** |
||
133 | * Get location of word list file |
||
134 | * |
||
135 | * @deprecated 4.0 Use the "Security.word_list" config setting instead |
||
136 | */ |
||
137 | public static function get_word_list() { |
||
138 | Deprecation::notice('4.0', 'Use the "Security.word_list" config setting instead'); |
||
139 | return self::config()->word_list; |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * Enable or disable recording of login attempts |
||
144 | * through the {@link LoginRecord} object. |
||
145 | * |
||
146 | * @config |
||
147 | * @var boolean $login_recording |
||
148 | */ |
||
149 | private static $login_recording = false; |
||
150 | |||
151 | /** |
||
152 | * @var boolean If set to TRUE or FALSE, {@link database_is_ready()} |
||
153 | * will always return FALSE. Used for unit testing. |
||
154 | */ |
||
155 | static $force_database_is_ready = null; |
||
156 | |||
157 | /** |
||
158 | * When the database has once been verified as ready, it will not do the |
||
159 | * checks again. |
||
160 | * |
||
161 | * @var bool |
||
162 | */ |
||
163 | static $database_is_ready = false; |
||
164 | |||
165 | /** |
||
166 | * Set location of word list file |
||
167 | * |
||
168 | * @deprecated 4.0 Use the "Security.word_list" config setting instead |
||
169 | * @param string $wordListFile Location of word list file |
||
170 | */ |
||
171 | public static function set_word_list($wordListFile) { |
||
172 | Deprecation::notice('4.0', 'Use the "Security.word_list" config setting instead'); |
||
173 | self::config()->word_list = $wordListFile; |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * Set the default message set used in permissions failures. |
||
178 | * |
||
179 | * @deprecated 4.0 Use the "Security.default_message_set" config setting instead |
||
180 | * @param string|array $messageSet |
||
181 | */ |
||
182 | public static function set_default_message_set($messageSet) { |
||
183 | Deprecation::notice('4.0', 'Use the "Security.default_message_set" config setting instead'); |
||
184 | self::config()->default_message_set = $messageSet; |
||
185 | } |
||
186 | |||
187 | |||
188 | /** |
||
189 | * Register that we've had a permission failure trying to view the given page |
||
190 | * |
||
191 | * This will redirect to a login page. |
||
192 | * If you don't provide a messageSet, a default will be used. |
||
193 | * |
||
194 | * @param Controller $controller The controller that you were on to cause the permission |
||
195 | * failure. |
||
196 | * @param string|array $messageSet The message to show to the user. This |
||
197 | * can be a string, or a map of different |
||
198 | * messages for different contexts. |
||
199 | * If you pass an array, you can use the |
||
200 | * following keys: |
||
201 | * - default: The default message |
||
202 | * - alreadyLoggedIn: The message to |
||
203 | * show if the user |
||
204 | * is already logged |
||
205 | * in and lacks the |
||
206 | * permission to |
||
207 | * access the item. |
||
208 | * |
||
209 | * The alreadyLoggedIn value can contain a '%s' placeholder that will be replaced with a link |
||
210 | * to log in. |
||
211 | * @return SS_HTTPResponse |
||
212 | */ |
||
213 | public static function permissionFailure($controller = null, $messageSet = null) { |
||
306 | |||
307 | public function init() { |
||
308 | parent::init(); |
||
309 | |||
313 | |||
314 | public function index() { |
||
317 | |||
318 | /** |
||
319 | * Get the selected authenticator for this request |
||
320 | * |
||
321 | * @return string Class name of Authenticator |
||
322 | */ |
||
323 | protected function getAuthenticator() { |
||
335 | |||
336 | /** |
||
337 | * Get the login form to process according to the submitted data |
||
338 | * |
||
339 | * @return Form |
||
340 | */ |
||
341 | public function LoginForm() { |
||
346 | |||
347 | /** |
||
348 | * Get the login forms for all available authentication methods |
||
349 | * |
||
350 | * @return array Returns an array of available login forms (array of Form |
||
351 | * objects). |
||
352 | * |
||
353 | * @todo Check how to activate/deactivate authentication methods |
||
354 | */ |
||
355 | public function GetLoginForms() { |
||
365 | |||
366 | |||
367 | /** |
||
368 | * Get a link to a security action |
||
369 | * |
||
370 | * @param string $action Name of the action |
||
371 | * @return string Returns the link to the given action |
||
372 | */ |
||
373 | public function Link($action = null) { |
||
376 | |||
377 | /** |
||
378 | * This action is available as a keep alive, so user |
||
379 | * sessions don't timeout. A common use is in the admin. |
||
380 | */ |
||
381 | public function ping() { |
||
384 | |||
385 | /** |
||
386 | * Log the currently logged in user out |
||
387 | * |
||
388 | * @param bool $redirect Redirect the user back to where they came. |
||
389 | * - If it's false, the code calling logout() is |
||
390 | * responsible for sending the user where-ever |
||
391 | * they should go. |
||
392 | */ |
||
393 | public function logout($redirect = true) { |
||
401 | |||
402 | /** |
||
403 | * Perform pre-login checking and prepare a response if available prior to login |
||
404 | * |
||
405 | * @return SS_HTTPResponse Substitute response object if the login process should be curcumvented. |
||
406 | * Returns null if should proceed as normal. |
||
407 | */ |
||
408 | protected function preLogin() { |
||
434 | |||
435 | /** |
||
436 | * Prepare the controller for handling the response to this request |
||
437 | * |
||
438 | * @param string $title Title to use |
||
439 | * @return Controller |
||
440 | */ |
||
441 | protected function getResponseController($title) { |
||
456 | |||
457 | /** |
||
458 | * Determine the list of templates to use for rendering the given action |
||
459 | * |
||
460 | * @param string $action |
||
461 | * @return array Template list |
||
462 | */ |
||
463 | public function getTemplatesFor($action) { |
||
466 | |||
467 | /** |
||
468 | * Combine the given forms into a formset with a tabbed interface |
||
469 | * |
||
470 | * @param array $forms List of LoginForm instances |
||
471 | * @return string |
||
472 | */ |
||
473 | protected function generateLoginFormSet($forms) { |
||
481 | |||
482 | /** |
||
483 | * Get the HTML Content for the $Content area during login |
||
484 | * |
||
485 | * @param string &$messageType Type of message, if available, passed back to caller |
||
486 | * @return string Message in HTML format |
||
487 | */ |
||
488 | protected function getLoginMessage(&$messageType = null) { |
||
500 | |||
501 | |||
502 | /** |
||
503 | * Show the "login" page |
||
504 | * |
||
505 | * For multiple authenticators, Security_MultiAuthenticatorLogin is used. |
||
506 | * See getTemplatesFor and getIncludeTemplate for how to override template logic |
||
507 | * |
||
508 | * @return string|SS_HTTPResponse Returns the "login" page as HTML code. |
||
509 | */ |
||
510 | public function login() { |
||
554 | |||
555 | public function basicauthlogin() { |
||
559 | |||
560 | /** |
||
561 | * Show the "lost password" page |
||
562 | * |
||
563 | * @return string Returns the "lost password" page as HTML code. |
||
564 | */ |
||
565 | public function lostpassword() { |
||
585 | |||
586 | |||
587 | /** |
||
588 | * Factory method for the lost password form |
||
589 | * |
||
590 | * @return Form Returns the lost password form |
||
591 | */ |
||
592 | public function LostPasswordForm() { |
||
608 | |||
609 | |||
610 | /** |
||
611 | * Show the "password sent" page, after a user has requested |
||
612 | * to reset their password. |
||
613 | * |
||
614 | * @param SS_HTTPRequest $request The SS_HTTPRequest for this action. |
||
615 | * @return string Returns the "password sent" page as HTML code. |
||
616 | */ |
||
617 | public function passwordsent($request) { |
||
641 | |||
642 | |||
643 | /** |
||
644 | * Create a link to the password reset form. |
||
645 | * |
||
646 | * GET parameters used: |
||
647 | * - m: member ID |
||
648 | * - t: plaintext token |
||
649 | * |
||
650 | * @param Member $member Member object associated with this link. |
||
651 | * @param string $autoLoginHash The auto login token. |
||
652 | */ |
||
653 | public static function getPasswordResetLink($member, $autologinToken) { |
||
659 | |||
660 | /** |
||
661 | * Show the "change password" page. |
||
662 | * This page can either be called directly by logged-in users |
||
663 | * (in which case they need to provide their old password), |
||
664 | * or through a link emailed through {@link lostpassword()}. |
||
665 | * In this case no old password is required, authentication is ensured |
||
666 | * through the Member.AutoLoginHash property. |
||
667 | * |
||
668 | * @see ChangePasswordForm |
||
669 | * |
||
670 | * @return string Returns the "change password" page as HTML code. |
||
671 | */ |
||
672 | public function changepassword() { |
||
738 | |||
739 | /** |
||
740 | * Factory method for the lost password form |
||
741 | * |
||
742 | * @return Form Returns the lost password form |
||
743 | */ |
||
744 | public function ChangePasswordForm() { |
||
747 | |||
748 | /** |
||
749 | * Gets the template for an include used for security. |
||
750 | * For use in any subclass. |
||
751 | * |
||
752 | * @return string|array Returns the template(s) for rendering |
||
753 | */ |
||
754 | public function getIncludeTemplate($name) { |
||
757 | |||
758 | /** |
||
759 | * Return an existing member with administrator privileges, or create one of necessary. |
||
760 | * |
||
761 | * Will create a default 'Administrators' group if no group is found |
||
762 | * with an ADMIN permission. Will create a new 'Admin' member with administrative permissions |
||
763 | * if no existing Member with these permissions is found. |
||
764 | * |
||
765 | * Important: Any newly created administrator accounts will NOT have valid |
||
766 | * login credentials (Email/Password properties), which means they can't be used for login |
||
767 | * purposes outside of any default credentials set through {@link Security::setDefaultAdmin()}. |
||
768 | * |
||
769 | * @return Member |
||
770 | */ |
||
771 | public static function findAnAdministrator() { |
||
820 | |||
821 | /** |
||
822 | * Flush the default admin credentials |
||
823 | */ |
||
824 | public static function clear_default_admin() { |
||
828 | |||
829 | |||
830 | /** |
||
831 | * Set a default admin in dev-mode |
||
832 | * |
||
833 | * This will set a static default-admin which is not existing |
||
834 | * as a database-record. By this workaround we can test pages in dev-mode |
||
835 | * with a unified login. Submitted login-credentials are first checked |
||
836 | * against this static information in {@link Security::authenticate()}. |
||
837 | * |
||
838 | * @param string $username The user name |
||
839 | * @param string $password The password (in cleartext) |
||
840 | */ |
||
841 | public static function setDefaultAdmin($username, $password) { |
||
850 | |||
851 | /** |
||
852 | * Checks if the passed credentials are matching the default-admin. |
||
853 | * Compares cleartext-password set through Security::setDefaultAdmin(). |
||
854 | * |
||
855 | * @param string $username |
||
856 | * @param string $password |
||
857 | * @return bool |
||
858 | */ |
||
859 | public static function check_default_admin($username, $password) { |
||
866 | |||
867 | /** |
||
868 | * Check that the default admin account has been set. |
||
869 | */ |
||
870 | public static function has_default_admin() { |
||
873 | |||
874 | /** |
||
875 | * Get default admin username |
||
876 | * |
||
877 | * @return string |
||
878 | */ |
||
879 | public static function default_admin_username() { |
||
882 | |||
883 | /** |
||
884 | * Get default admin password |
||
885 | * |
||
886 | * @return string |
||
887 | */ |
||
888 | public static function default_admin_password() { |
||
891 | |||
892 | /** |
||
893 | * Set strict path checking |
||
894 | * |
||
895 | * This prevents sharing of the session across several sites in the |
||
896 | * domain. |
||
897 | * |
||
898 | * @deprecated 4.0 Use the "Security.strict_path_checking" config setting instead |
||
899 | * @param boolean $strictPathChecking To enable or disable strict patch |
||
900 | * checking. |
||
901 | */ |
||
902 | public static function setStrictPathChecking($strictPathChecking) { |
||
906 | |||
907 | |||
908 | /** |
||
909 | * Get strict path checking |
||
910 | * |
||
911 | * @deprecated 4.0 Use the "Security.strict_path_checking" config setting instead |
||
912 | * @return boolean Status of strict path checking |
||
913 | */ |
||
914 | public static function getStrictPathChecking() { |
||
918 | |||
919 | |||
920 | /** |
||
921 | * Set the password encryption algorithm |
||
922 | * |
||
923 | * @deprecated 4.0 Use the "Security.password_encryption_algorithm" config setting instead |
||
924 | * @param string $algorithm One of the available password encryption |
||
925 | * algorithms determined by {@link Security::get_encryption_algorithms()} |
||
926 | * @return bool Returns TRUE if the passed algorithm was valid, otherwise FALSE. |
||
927 | */ |
||
928 | public static function set_password_encryption_algorithm($algorithm) { |
||
933 | |||
934 | /** |
||
935 | * @deprecated 4.0 Use the "Security.password_encryption_algorithm" config setting instead |
||
936 | * @return String |
||
937 | */ |
||
938 | public static function get_password_encryption_algorithm() { |
||
942 | |||
943 | /** |
||
944 | * Encrypt a password according to the current password encryption settings. |
||
945 | * If the settings are so that passwords shouldn't be encrypted, the |
||
946 | * result is simple the clear text password with an empty salt except when |
||
947 | * a custom algorithm ($algorithm parameter) was passed. |
||
948 | * |
||
949 | * @param string $password The password to encrypt |
||
950 | * @param string $salt Optional: The salt to use. If it is not passed, but |
||
951 | * needed, the method will automatically create a |
||
952 | * random salt that will then be returned as return value. |
||
953 | * @param string $algorithm Optional: Use another algorithm to encrypt the |
||
954 | * password (so that the encryption algorithm can be changed over the time). |
||
955 | * @param Member $member Optional |
||
956 | * @return mixed Returns an associative array containing the encrypted |
||
957 | * password and the used salt in the form: |
||
958 | * <code> |
||
959 | * array( |
||
960 | * 'password' => string, |
||
961 | * 'salt' => string, |
||
962 | * 'algorithm' => string, |
||
963 | * 'encryptor' => PasswordEncryptor instance |
||
964 | * ) |
||
965 | * </code> |
||
966 | * If the passed algorithm is invalid, FALSE will be returned. |
||
967 | * |
||
968 | * @see encrypt_passwords() |
||
969 | */ |
||
970 | public static function encrypt_password($password, $salt = null, $algorithm = null, $member = null) { |
||
986 | |||
987 | /** |
||
988 | * Checks the database is in a state to perform security checks. |
||
989 | * See {@link DatabaseAdmin->init()} for more information. |
||
990 | * |
||
991 | * @return bool |
||
992 | */ |
||
993 | public static function database_is_ready() { |
||
1027 | |||
1028 | /** |
||
1029 | * Enable or disable recording of login attempts |
||
1030 | * through the {@link LoginRecord} object. |
||
1031 | * |
||
1032 | * @deprecated 4.0 Use the "Security.login_recording" config setting instead |
||
1033 | * @param boolean $bool |
||
1034 | */ |
||
1035 | public static function set_login_recording($bool) { |
||
1039 | |||
1040 | /** |
||
1041 | * @deprecated 4.0 Use the "Security.login_recording" config setting instead |
||
1042 | * @return boolean |
||
1043 | */ |
||
1044 | public static function login_recording() { |
||
1048 | |||
1049 | /** |
||
1050 | * @config |
||
1051 | * @var string Set the default login dest |
||
1052 | * This is the URL that users will be redirected to after they log in, |
||
1053 | * if they haven't logged in en route to access a secured page. |
||
1054 | * By default, this is set to the homepage. |
||
1055 | */ |
||
1056 | private static $default_login_dest = ""; |
||
1057 | |||
1058 | /** |
||
1059 | * @deprecated 4.0 Use the "Security.default_login_dest" config setting instead |
||
1060 | */ |
||
1061 | public static function set_default_login_dest($dest) { |
||
1065 | |||
1066 | /** |
||
1067 | * Get the default login dest. |
||
1068 | * |
||
1069 | * @deprecated 4.0 Use the "Security.default_login_dest" config setting instead |
||
1070 | */ |
||
1071 | public static function default_login_dest() { |
||
1075 | |||
1076 | protected static $ignore_disallowed_actions = false; |
||
1077 | |||
1078 | /** |
||
1079 | * Set to true to ignore access to disallowed actions, rather than returning permission failure |
||
1080 | * Note that this is just a flag that other code needs to check with Security::ignore_disallowed_actions() |
||
1081 | * @param $flag True or false |
||
1082 | */ |
||
1083 | public static function set_ignore_disallowed_actions($flag) { |
||
1086 | |||
1087 | public static function ignore_disallowed_actions() { |
||
1090 | |||
1091 | |||
1092 | /** |
||
1093 | * Set a custom log-in URL if you have built your own log-in page. |
||
1094 | * |
||
1095 | * @deprecated 4.0 Use the "Security.login_url" config setting instead. |
||
1096 | */ |
||
1097 | public static function set_login_url($loginUrl) { |
||
1101 | |||
1102 | |||
1103 | /** |
||
1104 | * Get the URL of the log-in page. |
||
1105 | * |
||
1106 | * To update the login url use the "Security.login_url" config setting. |
||
1107 | * |
||
1108 | * @return string |
||
1109 | */ |
||
1110 | public static function login_url() { |
||
1113 | |||
1114 | |||
1115 | /** |
||
1116 | * Get the URL of the logout page. |
||
1117 | * |
||
1118 | * To update the logout url use the "Security.logout_url" config setting. |
||
1119 | * |
||
1120 | * @return string |
||
1121 | */ |
||
1122 | public static function logout_url() { |
||
1125 | |||
1126 | |||
1127 | /** |
||
1128 | * Defines global accessible templates variables. |
||
1129 | * |
||
1130 | * @return array |
||
1131 | */ |
||
1132 | public static function get_template_global_variables() { |
||
1138 | |||
1139 | } |
||
1140 |