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 |
||
33 | class Security extends Controller implements TemplateGlobalProvider { |
||
34 | |||
35 | private static $allowed_actions = array( |
||
36 | 'index', |
||
37 | 'login', |
||
38 | 'logout', |
||
39 | 'basicauthlogin', |
||
40 | 'lostpassword', |
||
41 | 'passwordsent', |
||
42 | 'changepassword', |
||
43 | 'ping', |
||
44 | 'LoginForm', |
||
45 | 'ChangePasswordForm', |
||
46 | 'LostPasswordForm', |
||
47 | ); |
||
48 | |||
49 | /** |
||
50 | * Default user name. Only used in dev-mode by {@link setDefaultAdmin()} |
||
51 | * |
||
52 | * @var string |
||
53 | * @see setDefaultAdmin() |
||
54 | */ |
||
55 | protected static $default_username; |
||
56 | |||
57 | /** |
||
58 | * Default password. Only used in dev-mode by {@link setDefaultAdmin()} |
||
59 | * |
||
60 | * @var string |
||
61 | * @see setDefaultAdmin() |
||
62 | */ |
||
63 | protected static $default_password; |
||
64 | |||
65 | /** |
||
66 | * If set to TRUE to prevent sharing of the session across several sites |
||
67 | * in the domain. |
||
68 | * |
||
69 | * @config |
||
70 | * @var bool |
||
71 | */ |
||
72 | protected static $strict_path_checking = false; |
||
73 | |||
74 | /** |
||
75 | * The password encryption algorithm to use by default. |
||
76 | * This is an arbitrary code registered through {@link PasswordEncryptor}. |
||
77 | * |
||
78 | * @config |
||
79 | * @var string |
||
80 | */ |
||
81 | private static $password_encryption_algorithm = 'blowfish'; |
||
82 | |||
83 | /** |
||
84 | * Showing "Remember me"-checkbox |
||
85 | * on loginform, and saving encrypted credentials to a cookie. |
||
86 | * |
||
87 | * @config |
||
88 | * @var bool |
||
89 | */ |
||
90 | private static $autologin_enabled = true; |
||
91 | |||
92 | /** |
||
93 | * Determine if login username may be remembered between login sessions |
||
94 | * If set to false this will disable autocomplete and prevent username persisting in the session |
||
95 | * |
||
96 | * @config |
||
97 | * @var bool |
||
98 | */ |
||
99 | private static $remember_username = true; |
||
100 | |||
101 | /** |
||
102 | * Location of word list to use for generating passwords |
||
103 | * |
||
104 | * @config |
||
105 | * @var string |
||
106 | */ |
||
107 | private static $word_list = './wordlist.txt'; |
||
108 | |||
109 | /** |
||
110 | * @config |
||
111 | * @var string |
||
112 | */ |
||
113 | private static $template = 'BlankPage'; |
||
114 | |||
115 | /** |
||
116 | * Template thats used to render the pages. |
||
117 | * |
||
118 | * @var string |
||
119 | * @config |
||
120 | */ |
||
121 | private static $template_main = 'Page'; |
||
122 | |||
123 | /** |
||
124 | * Default message set used in permission failures. |
||
125 | * |
||
126 | * @config |
||
127 | * @var array|string |
||
128 | */ |
||
129 | private static $default_message_set; |
||
130 | |||
131 | /** |
||
132 | * Random secure token, can be used as a crypto key internally. |
||
133 | * Generate one through 'sake dev/generatesecuretoken'. |
||
134 | * |
||
135 | * @config |
||
136 | * @var String |
||
137 | */ |
||
138 | private static $token; |
||
139 | |||
140 | /** |
||
141 | * The default login URL |
||
142 | * |
||
143 | * @config |
||
144 | * |
||
145 | * @var string |
||
146 | */ |
||
147 | private static $login_url = "Security/login"; |
||
148 | |||
149 | /** |
||
150 | * The default logout URL |
||
151 | * |
||
152 | * @config |
||
153 | * |
||
154 | * @var string |
||
155 | */ |
||
156 | private static $logout_url = "Security/logout"; |
||
157 | |||
158 | /** |
||
159 | * Get location of word list file |
||
160 | * |
||
161 | * @deprecated 4.0 Use the "Security.word_list" config setting instead |
||
162 | */ |
||
163 | public static function get_word_list() { |
||
167 | |||
168 | /** |
||
169 | * Enable or disable recording of login attempts |
||
170 | * through the {@link LoginRecord} object. |
||
171 | * |
||
172 | * @config |
||
173 | * @var boolean $login_recording |
||
174 | */ |
||
175 | private static $login_recording = false; |
||
176 | |||
177 | /** |
||
178 | * @var boolean If set to TRUE or FALSE, {@link database_is_ready()} |
||
179 | * will always return FALSE. Used for unit testing. |
||
180 | */ |
||
181 | static $force_database_is_ready = null; |
||
|
|||
182 | |||
183 | /** |
||
184 | * When the database has once been verified as ready, it will not do the |
||
185 | * checks again. |
||
186 | * |
||
187 | * @var bool |
||
188 | */ |
||
189 | static $database_is_ready = false; |
||
190 | |||
191 | /** |
||
192 | * Set location of word list file |
||
193 | * |
||
194 | * @deprecated 4.0 Use the "Security.word_list" config setting instead |
||
195 | * @param string $wordListFile Location of word list file |
||
196 | */ |
||
197 | public static function set_word_list($wordListFile) { |
||
201 | |||
202 | /** |
||
203 | * Set the default message set used in permissions failures. |
||
204 | * |
||
205 | * @deprecated 4.0 Use the "Security.default_message_set" config setting instead |
||
206 | * @param string|array $messageSet |
||
207 | */ |
||
208 | public static function set_default_message_set($messageSet) { |
||
212 | |||
213 | |||
214 | /** |
||
215 | * Register that we've had a permission failure trying to view the given page |
||
216 | * |
||
217 | * This will redirect to a login page. |
||
218 | * If you don't provide a messageSet, a default will be used. |
||
219 | * |
||
220 | * @param Controller $controller The controller that you were on to cause the permission |
||
221 | * failure. |
||
222 | * @param string|array $messageSet The message to show to the user. This |
||
223 | * can be a string, or a map of different |
||
224 | * messages for different contexts. |
||
225 | * If you pass an array, you can use the |
||
226 | * following keys: |
||
227 | * - default: The default message |
||
228 | * - alreadyLoggedIn: The message to |
||
229 | * show if the user |
||
230 | * is already logged |
||
231 | * in and lacks the |
||
232 | * permission to |
||
233 | * access the item. |
||
234 | * |
||
235 | * The alreadyLoggedIn value can contain a '%s' placeholder that will be replaced with a link |
||
236 | * to log in. |
||
237 | * @return SS_HTTPResponse |
||
238 | */ |
||
239 | public static function permissionFailure($controller = null, $messageSet = null) { |
||
332 | |||
333 | protected function init() { |
||
339 | |||
340 | public function index() { |
||
343 | |||
344 | /** |
||
345 | * Get the selected authenticator for this request |
||
346 | * |
||
347 | * @return string Class name of Authenticator |
||
348 | */ |
||
349 | protected function getAuthenticator() { |
||
361 | |||
362 | /** |
||
363 | * Get the login form to process according to the submitted data |
||
364 | * |
||
365 | * @return Form |
||
366 | * @throws Exception |
||
367 | */ |
||
368 | public function LoginForm() { |
||
373 | |||
374 | /** |
||
375 | * Get the login forms for all available authentication methods |
||
376 | * |
||
377 | * @return array Returns an array of available login forms (array of Form |
||
378 | * objects). |
||
379 | * |
||
380 | * @todo Check how to activate/deactivate authentication methods |
||
381 | */ |
||
382 | public function GetLoginForms() { |
||
392 | |||
393 | |||
394 | /** |
||
395 | * Get a link to a security action |
||
396 | * |
||
397 | * @param string $action Name of the action |
||
398 | * @return string Returns the link to the given action |
||
399 | */ |
||
400 | public function Link($action = null) { |
||
404 | |||
405 | /** |
||
406 | * This action is available as a keep alive, so user |
||
407 | * sessions don't timeout. A common use is in the admin. |
||
408 | */ |
||
409 | public function ping() { |
||
412 | |||
413 | /** |
||
414 | * Log the currently logged in user out |
||
415 | * |
||
416 | * @param bool $redirect Redirect the user back to where they came. |
||
417 | * - If it's false, the code calling logout() is |
||
418 | * responsible for sending the user where-ever |
||
419 | * they should go. |
||
420 | */ |
||
421 | public function logout($redirect = true) { |
||
429 | |||
430 | /** |
||
431 | * Perform pre-login checking and prepare a response if available prior to login |
||
432 | * |
||
433 | * @return SS_HTTPResponse Substitute response object if the login process should be curcumvented. |
||
434 | * Returns null if should proceed as normal. |
||
435 | */ |
||
436 | protected function preLogin() { |
||
462 | |||
463 | /** |
||
464 | * Prepare the controller for handling the response to this request |
||
465 | * |
||
466 | * @param string $title Title to use |
||
467 | * @return Controller |
||
468 | */ |
||
469 | protected function getResponseController($title) { |
||
485 | |||
486 | /** |
||
487 | * Determine the list of templates to use for rendering the given action |
||
488 | * |
||
489 | * @param string $action |
||
490 | * @return array Template list |
||
491 | */ |
||
492 | public function getTemplatesFor($action) { |
||
496 | |||
497 | /** |
||
498 | * Combine the given forms into a formset with a tabbed interface |
||
499 | * |
||
500 | * @param array $forms List of LoginForm instances |
||
501 | * @return string |
||
502 | */ |
||
503 | protected function generateLoginFormSet($forms) { |
||
511 | |||
512 | /** |
||
513 | * Get the HTML Content for the $Content area during login |
||
514 | * |
||
515 | * @param string &$messageType Type of message, if available, passed back to caller |
||
516 | * @return string Message in HTML format |
||
517 | */ |
||
518 | protected function getLoginMessage(&$messageType = null) { |
||
530 | |||
531 | |||
532 | /** |
||
533 | * Show the "login" page |
||
534 | * |
||
535 | * For multiple authenticators, Security_MultiAuthenticatorLogin is used. |
||
536 | * See getTemplatesFor and getIncludeTemplate for how to override template logic |
||
537 | * |
||
538 | * @return string|SS_HTTPResponse Returns the "login" page as HTML code. |
||
539 | */ |
||
540 | public function login() { |
||
584 | |||
585 | public function basicauthlogin() { |
||
589 | |||
590 | /** |
||
591 | * Show the "lost password" page |
||
592 | * |
||
593 | * @return string Returns the "lost password" page as HTML code. |
||
594 | */ |
||
595 | public function lostpassword() { |
||
615 | |||
616 | |||
617 | /** |
||
618 | * Factory method for the lost password form |
||
619 | * |
||
620 | * @return Form Returns the lost password form |
||
621 | */ |
||
622 | public function LostPasswordForm() { |
||
638 | |||
639 | |||
640 | /** |
||
641 | * Show the "password sent" page, after a user has requested |
||
642 | * to reset their password. |
||
643 | * |
||
644 | * @param SS_HTTPRequest $request The SS_HTTPRequest for this action. |
||
645 | * @return string Returns the "password sent" page as HTML code. |
||
646 | */ |
||
647 | public function passwordsent($request) { |
||
671 | |||
672 | |||
673 | /** |
||
674 | * Create a link to the password reset form. |
||
675 | * |
||
676 | * GET parameters used: |
||
677 | * - m: member ID |
||
678 | * - t: plaintext token |
||
679 | * |
||
680 | * @param Member $member Member object associated with this link. |
||
681 | * @param string $autologinToken The auto login token. |
||
682 | * @return string |
||
683 | */ |
||
684 | public static function getPasswordResetLink($member, $autologinToken) { |
||
690 | |||
691 | /** |
||
692 | * Show the "change password" page. |
||
693 | * This page can either be called directly by logged-in users |
||
694 | * (in which case they need to provide their old password), |
||
695 | * or through a link emailed through {@link lostpassword()}. |
||
696 | * In this case no old password is required, authentication is ensured |
||
697 | * through the Member.AutoLoginHash property. |
||
698 | * |
||
699 | * @see ChangePasswordForm |
||
700 | * |
||
701 | * @return string Returns the "change password" page as HTML code. |
||
702 | */ |
||
703 | public function changepassword() { |
||
769 | |||
770 | /** |
||
771 | * Factory method for the lost password form |
||
772 | * |
||
773 | * @return ChangePasswordForm Returns the lost password form |
||
774 | */ |
||
775 | public function ChangePasswordForm() { |
||
783 | |||
784 | /** |
||
785 | * Gets the template for an include used for security. |
||
786 | * For use in any subclass. |
||
787 | * |
||
788 | * @param string $name |
||
789 | * @return array Returns the template(s) for rendering |
||
790 | */ |
||
791 | public function getIncludeTemplate($name) { |
||
794 | |||
795 | /** |
||
796 | * Return an existing member with administrator privileges, or create one of necessary. |
||
797 | * |
||
798 | * Will create a default 'Administrators' group if no group is found |
||
799 | * with an ADMIN permission. Will create a new 'Admin' member with administrative permissions |
||
800 | * if no existing Member with these permissions is found. |
||
801 | * |
||
802 | * Important: Any newly created administrator accounts will NOT have valid |
||
803 | * login credentials (Email/Password properties), which means they can't be used for login |
||
804 | * purposes outside of any default credentials set through {@link Security::setDefaultAdmin()}. |
||
805 | * |
||
806 | * @return Member |
||
807 | */ |
||
808 | public static function findAnAdministrator() { |
||
857 | |||
858 | /** |
||
859 | * Flush the default admin credentials |
||
860 | */ |
||
861 | public static function clear_default_admin() { |
||
865 | |||
866 | |||
867 | /** |
||
868 | * Set a default admin in dev-mode |
||
869 | * |
||
870 | * This will set a static default-admin which is not existing |
||
871 | * as a database-record. By this workaround we can test pages in dev-mode |
||
872 | * with a unified login. Submitted login-credentials are first checked |
||
873 | * against this static information in {@link Security::authenticate()}. |
||
874 | * |
||
875 | * @param string $username The user name |
||
876 | * @param string $password The password (in cleartext) |
||
877 | * @return bool |
||
878 | */ |
||
879 | public static function setDefaultAdmin($username, $password) { |
||
888 | |||
889 | /** |
||
890 | * Checks if the passed credentials are matching the default-admin. |
||
891 | * Compares cleartext-password set through Security::setDefaultAdmin(). |
||
892 | * |
||
893 | * @param string $username |
||
894 | * @param string $password |
||
895 | * @return bool |
||
896 | */ |
||
897 | public static function check_default_admin($username, $password) { |
||
904 | |||
905 | /** |
||
906 | * Check that the default admin account has been set. |
||
907 | */ |
||
908 | public static function has_default_admin() { |
||
911 | |||
912 | /** |
||
913 | * Get default admin username |
||
914 | * |
||
915 | * @return string |
||
916 | */ |
||
917 | public static function default_admin_username() { |
||
920 | |||
921 | /** |
||
922 | * Get default admin password |
||
923 | * |
||
924 | * @return string |
||
925 | */ |
||
926 | public static function default_admin_password() { |
||
929 | |||
930 | /** |
||
931 | * Set strict path checking |
||
932 | * |
||
933 | * This prevents sharing of the session across several sites in the |
||
934 | * domain. |
||
935 | * |
||
936 | * @deprecated 4.0 Use the "Security.strict_path_checking" config setting instead |
||
937 | * @param boolean $strictPathChecking To enable or disable strict patch |
||
938 | * checking. |
||
939 | */ |
||
940 | public static function setStrictPathChecking($strictPathChecking) { |
||
944 | |||
945 | |||
946 | /** |
||
947 | * Get strict path checking |
||
948 | * |
||
949 | * @deprecated 4.0 Use the "Security.strict_path_checking" config setting instead |
||
950 | * @return boolean Status of strict path checking |
||
951 | */ |
||
952 | public static function getStrictPathChecking() { |
||
956 | |||
957 | |||
958 | /** |
||
959 | * Set the password encryption algorithm |
||
960 | * |
||
961 | * @deprecated 4.0 Use the "Security.password_encryption_algorithm" config setting instead |
||
962 | * @param string $algorithm One of the available password encryption |
||
963 | * algorithms determined by {@link Security::get_encryption_algorithms()} |
||
964 | * @return bool Returns TRUE if the passed algorithm was valid, otherwise FALSE. |
||
965 | */ |
||
966 | public static function set_password_encryption_algorithm($algorithm) { |
||
971 | |||
972 | /** |
||
973 | * @deprecated 4.0 Use the "Security.password_encryption_algorithm" config setting instead |
||
974 | * @return String |
||
975 | */ |
||
976 | public static function get_password_encryption_algorithm() { |
||
980 | |||
981 | /** |
||
982 | * Encrypt a password according to the current password encryption settings. |
||
983 | * If the settings are so that passwords shouldn't be encrypted, the |
||
984 | * result is simple the clear text password with an empty salt except when |
||
985 | * a custom algorithm ($algorithm parameter) was passed. |
||
986 | * |
||
987 | * @param string $password The password to encrypt |
||
988 | * @param string $salt Optional: The salt to use. If it is not passed, but |
||
989 | * needed, the method will automatically create a |
||
990 | * random salt that will then be returned as return value. |
||
991 | * @param string $algorithm Optional: Use another algorithm to encrypt the |
||
992 | * password (so that the encryption algorithm can be changed over the time). |
||
993 | * @param Member $member Optional |
||
994 | * @return mixed Returns an associative array containing the encrypted |
||
995 | * password and the used salt in the form: |
||
996 | * <code> |
||
997 | * array( |
||
998 | * 'password' => string, |
||
999 | * 'salt' => string, |
||
1000 | * 'algorithm' => string, |
||
1001 | * 'encryptor' => PasswordEncryptor instance |
||
1002 | * ) |
||
1003 | * </code> |
||
1004 | * If the passed algorithm is invalid, FALSE will be returned. |
||
1005 | * |
||
1006 | * @see encrypt_passwords() |
||
1007 | */ |
||
1008 | public static function encrypt_password($password, $salt = null, $algorithm = null, $member = null) { |
||
1024 | |||
1025 | /** |
||
1026 | * Checks the database is in a state to perform security checks. |
||
1027 | * See {@link DatabaseAdmin->init()} for more information. |
||
1028 | * |
||
1029 | * @return bool |
||
1030 | */ |
||
1031 | public static function database_is_ready() { |
||
1078 | |||
1079 | /** |
||
1080 | * Enable or disable recording of login attempts |
||
1081 | * through the {@link LoginRecord} object. |
||
1082 | * |
||
1083 | * @deprecated 4.0 Use the "Security.login_recording" config setting instead |
||
1084 | * @param boolean $bool |
||
1085 | */ |
||
1086 | public static function set_login_recording($bool) { |
||
1090 | |||
1091 | /** |
||
1092 | * @deprecated 4.0 Use the "Security.login_recording" config setting instead |
||
1093 | * @return boolean |
||
1094 | */ |
||
1095 | public static function login_recording() { |
||
1099 | |||
1100 | /** |
||
1101 | * @config |
||
1102 | * @var string Set the default login dest |
||
1103 | * This is the URL that users will be redirected to after they log in, |
||
1104 | * if they haven't logged in en route to access a secured page. |
||
1105 | * By default, this is set to the homepage. |
||
1106 | */ |
||
1107 | private static $default_login_dest = ""; |
||
1108 | |||
1109 | /** |
||
1110 | * @deprecated 4.0 Use the "Security.default_login_dest" config setting instead |
||
1111 | */ |
||
1112 | public static function set_default_login_dest($dest) { |
||
1116 | |||
1117 | /** |
||
1118 | * Get the default login dest. |
||
1119 | * |
||
1120 | * @deprecated 4.0 Use the "Security.default_login_dest" config setting instead |
||
1121 | */ |
||
1122 | public static function default_login_dest() { |
||
1126 | |||
1127 | protected static $ignore_disallowed_actions = false; |
||
1128 | |||
1129 | /** |
||
1130 | * Set to true to ignore access to disallowed actions, rather than returning permission failure |
||
1131 | * Note that this is just a flag that other code needs to check with Security::ignore_disallowed_actions() |
||
1132 | * @param $flag True or false |
||
1133 | */ |
||
1134 | public static function set_ignore_disallowed_actions($flag) { |
||
1137 | |||
1138 | public static function ignore_disallowed_actions() { |
||
1141 | |||
1142 | |||
1143 | /** |
||
1144 | * Set a custom log-in URL if you have built your own log-in page. |
||
1145 | * |
||
1146 | * @deprecated 4.0 Use the "Security.login_url" config setting instead. |
||
1147 | */ |
||
1148 | public static function set_login_url($loginUrl) { |
||
1152 | |||
1153 | |||
1154 | /** |
||
1155 | * Get the URL of the log-in page. |
||
1156 | * |
||
1157 | * To update the login url use the "Security.login_url" config setting. |
||
1158 | * |
||
1159 | * @return string |
||
1160 | */ |
||
1161 | public static function login_url() { |
||
1164 | |||
1165 | |||
1166 | /** |
||
1167 | * Get the URL of the logout page. |
||
1168 | * |
||
1169 | * To update the logout url use the "Security.logout_url" config setting. |
||
1170 | * |
||
1171 | * @return string |
||
1172 | */ |
||
1173 | public static function logout_url() { |
||
1176 | |||
1177 | |||
1178 | /** |
||
1179 | * Defines global accessible templates variables. |
||
1180 | * |
||
1181 | * @return array |
||
1182 | */ |
||
1183 | public static function get_template_global_variables() { |
||
1189 | |||
1190 | } |
||
1191 |
The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using
the property is implicitly global.
To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.