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 |
||
40 | class Security extends Controller implements TemplateGlobalProvider |
||
41 | { |
||
42 | |||
43 | private static $allowed_actions = array( |
||
|
|||
44 | 'index', |
||
45 | 'login', |
||
46 | 'logout', |
||
47 | 'basicauthlogin', |
||
48 | 'lostpassword', |
||
49 | 'passwordsent', |
||
50 | 'changepassword', |
||
51 | 'ping', |
||
52 | 'LoginForm', |
||
53 | 'ChangePasswordForm', |
||
54 | 'LostPasswordForm', |
||
55 | ); |
||
56 | |||
57 | /** |
||
58 | * Default user name. Only used in dev-mode by {@link setDefaultAdmin()} |
||
59 | * |
||
60 | * @var string |
||
61 | * @see setDefaultAdmin() |
||
62 | */ |
||
63 | protected static $default_username; |
||
64 | |||
65 | /** |
||
66 | * Default password. Only used in dev-mode by {@link setDefaultAdmin()} |
||
67 | * |
||
68 | * @var string |
||
69 | * @see setDefaultAdmin() |
||
70 | */ |
||
71 | protected static $default_password; |
||
72 | |||
73 | /** |
||
74 | * If set to TRUE to prevent sharing of the session across several sites |
||
75 | * in the domain. |
||
76 | * |
||
77 | * @config |
||
78 | * @var bool |
||
79 | */ |
||
80 | protected static $strict_path_checking = false; |
||
81 | |||
82 | /** |
||
83 | * The password encryption algorithm to use by default. |
||
84 | * This is an arbitrary code registered through {@link PasswordEncryptor}. |
||
85 | * |
||
86 | * @config |
||
87 | * @var string |
||
88 | */ |
||
89 | private static $password_encryption_algorithm = 'blowfish'; |
||
90 | |||
91 | /** |
||
92 | * Showing "Remember me"-checkbox |
||
93 | * on loginform, and saving encrypted credentials to a cookie. |
||
94 | * |
||
95 | * @config |
||
96 | * @var bool |
||
97 | */ |
||
98 | private static $autologin_enabled = true; |
||
99 | |||
100 | /** |
||
101 | * Determine if login username may be remembered between login sessions |
||
102 | * If set to false this will disable autocomplete and prevent username persisting in the session |
||
103 | * |
||
104 | * @config |
||
105 | * @var bool |
||
106 | */ |
||
107 | private static $remember_username = true; |
||
108 | |||
109 | /** |
||
110 | * Location of word list to use for generating passwords |
||
111 | * |
||
112 | * @config |
||
113 | * @var string |
||
114 | */ |
||
115 | private static $word_list = './wordlist.txt'; |
||
116 | |||
117 | /** |
||
118 | * @config |
||
119 | * @var string |
||
120 | */ |
||
121 | private static $template = 'BlankPage'; |
||
122 | |||
123 | /** |
||
124 | * Template thats used to render the pages. |
||
125 | * |
||
126 | * @var string |
||
127 | * @config |
||
128 | */ |
||
129 | private static $template_main = 'Page'; |
||
130 | |||
131 | /** |
||
132 | * Class to use for page rendering |
||
133 | * |
||
134 | * @var string |
||
135 | * @config |
||
136 | */ |
||
137 | private static $page_class = Page::class; |
||
138 | |||
139 | /** |
||
140 | * Default message set used in permission failures. |
||
141 | * |
||
142 | * @config |
||
143 | * @var array|string |
||
144 | */ |
||
145 | private static $default_message_set; |
||
146 | |||
147 | /** |
||
148 | * Random secure token, can be used as a crypto key internally. |
||
149 | * Generate one through 'sake dev/generatesecuretoken'. |
||
150 | * |
||
151 | * @config |
||
152 | * @var String |
||
153 | */ |
||
154 | private static $token; |
||
155 | |||
156 | /** |
||
157 | * The default login URL |
||
158 | * |
||
159 | * @config |
||
160 | * |
||
161 | * @var string |
||
162 | */ |
||
163 | private static $login_url = "Security/login"; |
||
164 | |||
165 | /** |
||
166 | * The default logout URL |
||
167 | * |
||
168 | * @config |
||
169 | * |
||
170 | * @var string |
||
171 | */ |
||
172 | private static $logout_url = "Security/logout"; |
||
173 | |||
174 | /** |
||
175 | * The default lost password URL |
||
176 | * |
||
177 | * @config |
||
178 | * |
||
179 | * @var string |
||
180 | */ |
||
181 | private static $lost_password_url = "Security/lostpassword"; |
||
182 | |||
183 | /** |
||
184 | * Value of X-Frame-Options header |
||
185 | * |
||
186 | * @config |
||
187 | * @var string |
||
188 | */ |
||
189 | private static $frame_options = 'SAMEORIGIN'; |
||
190 | |||
191 | /** |
||
192 | * Value of the X-Robots-Tag header (for the Security section) |
||
193 | * |
||
194 | * @config |
||
195 | * @var string |
||
196 | */ |
||
197 | private static $robots_tag = 'noindex, nofollow'; |
||
198 | |||
199 | /** |
||
200 | * Enable or disable recording of login attempts |
||
201 | * through the {@link LoginRecord} object. |
||
202 | * |
||
203 | * @config |
||
204 | * @var boolean $login_recording |
||
205 | */ |
||
206 | private static $login_recording = false; |
||
207 | |||
208 | /** |
||
209 | * @var boolean If set to TRUE or FALSE, {@link database_is_ready()} |
||
210 | * will always return FALSE. Used for unit testing. |
||
211 | */ |
||
212 | protected static $force_database_is_ready = null; |
||
213 | |||
214 | /** |
||
215 | * When the database has once been verified as ready, it will not do the |
||
216 | * checks again. |
||
217 | * |
||
218 | * @var bool |
||
219 | */ |
||
220 | protected static $database_is_ready = false; |
||
221 | |||
222 | protected static $authenticators = []; |
||
223 | |||
224 | protected static $default_authenticator = MemberAuthenticator\Authenticator::class; |
||
225 | |||
226 | /** |
||
227 | * Get all registered authenticators |
||
228 | * |
||
229 | * @return array Return an array of Authenticator objects |
||
230 | */ |
||
231 | public static function getAuthenticators() |
||
255 | |||
256 | /** |
||
257 | * Check if a given authenticator is registered |
||
258 | * |
||
259 | * @param string $authenticator Name of the authenticator class to check |
||
260 | * @return bool Returns TRUE if the authenticator is registered, FALSE |
||
261 | * otherwise. |
||
262 | */ |
||
263 | public static function hasAuthenticator($authenticator) |
||
272 | |||
273 | /** |
||
274 | * Register that we've had a permission failure trying to view the given page |
||
275 | * |
||
276 | * This will redirect to a login page. |
||
277 | * If you don't provide a messageSet, a default will be used. |
||
278 | * |
||
279 | * @param Controller $controller The controller that you were on to cause the permission |
||
280 | * failure. |
||
281 | * @param string|array $messageSet The message to show to the user. This |
||
282 | * can be a string, or a map of different |
||
283 | * messages for different contexts. |
||
284 | * If you pass an array, you can use the |
||
285 | * following keys: |
||
286 | * - default: The default message |
||
287 | * - alreadyLoggedIn: The message to |
||
288 | * show if the user |
||
289 | * is already logged |
||
290 | * in and lacks the |
||
291 | * permission to |
||
292 | * access the item. |
||
293 | * |
||
294 | * The alreadyLoggedIn value can contain a '%s' placeholder that will be replaced with a link |
||
295 | * to log in. |
||
296 | * @return HTTPResponse |
||
297 | */ |
||
298 | public static function permissionFailure($controller = null, $messageSet = null) |
||
392 | |||
393 | protected function init() |
||
409 | |||
410 | public function index() |
||
414 | |||
415 | /** |
||
416 | * Get the selected authenticator for this request |
||
417 | * |
||
418 | * @return string Class name of Authenticator |
||
419 | * @throws LogicException |
||
420 | */ |
||
421 | protected function getAuthenticator() |
||
436 | |||
437 | /** |
||
438 | * Get the login form to process according to the submitted data |
||
439 | * |
||
440 | * @return Form |
||
441 | * @throws Exception |
||
442 | */ |
||
443 | public function LoginForm() |
||
452 | |||
453 | /** |
||
454 | * Get the login forms for all available authentication methods |
||
455 | * |
||
456 | * @return array Returns an array of available login forms (array of Form |
||
457 | * objects). |
||
458 | * |
||
459 | * @todo Check how to activate/deactivate authentication methods |
||
460 | */ |
||
461 | public function getLoginForms() |
||
470 | |||
471 | |||
472 | /** |
||
473 | * Get a link to a security action |
||
474 | * |
||
475 | * @param string $action Name of the action |
||
476 | * @return string Returns the link to the given action |
||
477 | */ |
||
478 | public function Link($action = null) |
||
483 | |||
484 | /** |
||
485 | * This action is available as a keep alive, so user |
||
486 | * sessions don't timeout. A common use is in the admin. |
||
487 | */ |
||
488 | public function ping() |
||
492 | |||
493 | /** |
||
494 | * Log the currently logged in user out |
||
495 | * |
||
496 | * @param bool $redirect Redirect the user back to where they came. |
||
497 | * - If it's false, the code calling logout() is |
||
498 | * responsible for sending the user where-ever |
||
499 | * they should go. |
||
500 | * @return HTTPResponse|null |
||
501 | */ |
||
502 | public function logout($redirect = true) |
||
514 | |||
515 | /** |
||
516 | * Perform pre-login checking and prepare a response if available prior to login |
||
517 | * |
||
518 | * @return HTTPResponse Substitute response object if the login process should be curcumvented. |
||
519 | * Returns null if should proceed as normal. |
||
520 | */ |
||
521 | protected function preLogin() |
||
554 | |||
555 | /** |
||
556 | * Prepare the controller for handling the response to this request |
||
557 | * |
||
558 | * @param string $title Title to use |
||
559 | * @return Controller |
||
560 | */ |
||
561 | protected function getResponseController($title) |
||
585 | |||
586 | /** |
||
587 | * Combine the given forms into a formset with a tabbed interface |
||
588 | * |
||
589 | * @param array $authenticators List of Authenticator instances |
||
590 | * @return string |
||
591 | */ |
||
592 | protected function generateLoginFormSet($forms) |
||
601 | |||
602 | /** |
||
603 | * Get the HTML Content for the $Content area during login |
||
604 | * |
||
605 | * @param string &$messageType Type of message, if available, passed back to caller |
||
606 | * @return string Message in HTML format |
||
607 | */ |
||
608 | protected function getLoginMessage(&$messageType = null) |
||
623 | |||
624 | /** |
||
625 | * Set the next message to display for the security login page. Defaults to warning |
||
626 | * |
||
627 | * @param string $message Message |
||
628 | * @param string $messageType Message type. One of ValidationResult::TYPE_* |
||
629 | * @param string $messageCast Message cast. One of ValidationResult::CAST_* |
||
630 | */ |
||
631 | public static function setLoginMessage( |
||
640 | |||
641 | /** |
||
642 | * Clear login message |
||
643 | */ |
||
644 | public static function clearLoginMessage() |
||
648 | |||
649 | |||
650 | /** |
||
651 | * Show the "login" page |
||
652 | * |
||
653 | * For multiple authenticators, Security_MultiAuthenticatorLogin is used. |
||
654 | * See getTemplatesFor and getIncludeTemplate for how to override template logic |
||
655 | * |
||
656 | * @return string|HTTPResponse Returns the "login" page as HTML code. |
||
657 | */ |
||
658 | public function login($request) |
||
670 | |||
671 | /** |
||
672 | * Delegate to another RequestHandler, rendering any fragment arrays into an appropriate. |
||
673 | * controller. |
||
674 | * @param string $title The title of the form |
||
675 | * @param array $templates |
||
676 | * @return array|HTTPResponse|RequestHandler|\SilverStripe\ORM\FieldType\DBHTMLText|string |
||
677 | */ |
||
678 | protected function delegateToHandlers($title, array $templates) |
||
725 | |||
726 | public function basicauthlogin() |
||
731 | |||
732 | /** |
||
733 | * Show the "lost password" page |
||
734 | * |
||
735 | * @return string Returns the "lost password" page as HTML code. |
||
736 | */ |
||
737 | public function lostpassword() |
||
749 | |||
750 | /** |
||
751 | * Create a link to the password reset form. |
||
752 | * |
||
753 | * GET parameters used: |
||
754 | * - m: member ID |
||
755 | * - t: plaintext token |
||
756 | * |
||
757 | * @param Member $member Member object associated with this link. |
||
758 | * @param string $autologinToken The auto login token. |
||
759 | * @return string |
||
760 | */ |
||
761 | public static function getPasswordResetLink($member, $autologinToken) |
||
769 | |||
770 | /** |
||
771 | * Show the "change password" page. |
||
772 | * This page can either be called directly by logged-in users |
||
773 | * (in which case they need to provide their old password), |
||
774 | * or through a link emailed through {@link lostpassword()}. |
||
775 | * In this case no old password is required, authentication is ensured |
||
776 | * through the Member.AutoLoginHash property. |
||
777 | * |
||
778 | * @see ChangePasswordForm |
||
779 | * |
||
780 | * @return string|HTTPRequest Returns the "change password" page as HTML code, or a redirect response |
||
781 | */ |
||
782 | public function changepassword() |
||
856 | |||
857 | /** |
||
858 | * Factory method for the lost password form |
||
859 | * |
||
860 | * @skipUpgrade |
||
861 | * @return ChangePasswordForm Returns the lost password form |
||
862 | */ |
||
863 | public function ChangePasswordForm() |
||
867 | |||
868 | /** |
||
869 | * Determine the list of templates to use for rendering the given action. |
||
870 | * |
||
871 | * @skipUpgrade |
||
872 | * @param string $action |
||
873 | * @return array Template list |
||
874 | */ |
||
875 | public function getTemplatesFor($action) |
||
888 | |||
889 | /** |
||
890 | * Return an existing member with administrator privileges, or create one of necessary. |
||
891 | * |
||
892 | * Will create a default 'Administrators' group if no group is found |
||
893 | * with an ADMIN permission. Will create a new 'Admin' member with administrative permissions |
||
894 | * if no existing Member with these permissions is found. |
||
895 | * |
||
896 | * Important: Any newly created administrator accounts will NOT have valid |
||
897 | * login credentials (Email/Password properties), which means they can't be used for login |
||
898 | * purposes outside of any default credentials set through {@link Security::setDefaultAdmin()}. |
||
899 | * |
||
900 | * @return Member |
||
901 | */ |
||
902 | public static function findAnAdministrator() |
||
953 | |||
954 | /** |
||
955 | * Flush the default admin credentials |
||
956 | */ |
||
957 | public static function clear_default_admin() |
||
962 | |||
963 | |||
964 | /** |
||
965 | * Set a default admin in dev-mode |
||
966 | * |
||
967 | * This will set a static default-admin which is not existing |
||
968 | * as a database-record. By this workaround we can test pages in dev-mode |
||
969 | * with a unified login. Submitted login-credentials are first checked |
||
970 | * against this static information in {@link Security::authenticate()}. |
||
971 | * |
||
972 | * @param string $username The user name |
||
973 | * @param string $password The password (in cleartext) |
||
974 | * @return bool True if successfully set |
||
975 | */ |
||
976 | public static function setDefaultAdmin($username, $password) |
||
987 | |||
988 | /** |
||
989 | * Checks if the passed credentials are matching the default-admin. |
||
990 | * Compares cleartext-password set through Security::setDefaultAdmin(). |
||
991 | * |
||
992 | * @param string $username |
||
993 | * @param string $password |
||
994 | * @return bool |
||
995 | */ |
||
996 | public static function check_default_admin($username, $password) |
||
1004 | |||
1005 | /** |
||
1006 | * Check that the default admin account has been set. |
||
1007 | */ |
||
1008 | public static function has_default_admin() |
||
1012 | |||
1013 | /** |
||
1014 | * Get default admin username |
||
1015 | * |
||
1016 | * @return string |
||
1017 | */ |
||
1018 | public static function default_admin_username() |
||
1022 | |||
1023 | /** |
||
1024 | * Get default admin password |
||
1025 | * |
||
1026 | * @return string |
||
1027 | */ |
||
1028 | public static function default_admin_password() |
||
1032 | |||
1033 | /** |
||
1034 | * Encrypt a password according to the current password encryption settings. |
||
1035 | * If the settings are so that passwords shouldn't be encrypted, the |
||
1036 | * result is simple the clear text password with an empty salt except when |
||
1037 | * a custom algorithm ($algorithm parameter) was passed. |
||
1038 | * |
||
1039 | * @param string $password The password to encrypt |
||
1040 | * @param string $salt Optional: The salt to use. If it is not passed, but |
||
1041 | * needed, the method will automatically create a |
||
1042 | * random salt that will then be returned as return value. |
||
1043 | * @param string $algorithm Optional: Use another algorithm to encrypt the |
||
1044 | * password (so that the encryption algorithm can be changed over the time). |
||
1045 | * @param Member $member Optional |
||
1046 | * @return mixed Returns an associative array containing the encrypted |
||
1047 | * password and the used salt in the form: |
||
1048 | * <code> |
||
1049 | * array( |
||
1050 | * 'password' => string, |
||
1051 | * 'salt' => string, |
||
1052 | * 'algorithm' => string, |
||
1053 | * 'encryptor' => PasswordEncryptor instance |
||
1054 | * ) |
||
1055 | * </code> |
||
1056 | * If the passed algorithm is invalid, FALSE will be returned. |
||
1057 | * |
||
1058 | * @see encrypt_passwords() |
||
1059 | */ |
||
1060 | public static function encrypt_password($password, $salt = null, $algorithm = null, $member = null) |
||
1079 | |||
1080 | /** |
||
1081 | * Checks the database is in a state to perform security checks. |
||
1082 | * See {@link DatabaseAdmin->init()} for more information. |
||
1083 | * |
||
1084 | * @return bool |
||
1085 | */ |
||
1086 | public static function database_is_ready() |
||
1134 | |||
1135 | /** |
||
1136 | * Enable or disable recording of login attempts |
||
1137 | * through the {@link LoginRecord} object. |
||
1138 | * |
||
1139 | * @deprecated 4.0 Use the "Security.login_recording" config setting instead |
||
1140 | * @param boolean $bool |
||
1141 | */ |
||
1142 | public static function set_login_recording($bool) |
||
1147 | |||
1148 | /** |
||
1149 | * @deprecated 4.0 Use the "Security.login_recording" config setting instead |
||
1150 | * @return boolean |
||
1151 | */ |
||
1152 | public static function login_recording() |
||
1157 | |||
1158 | /** |
||
1159 | * @config |
||
1160 | * @var string Set the default login dest |
||
1161 | * This is the URL that users will be redirected to after they log in, |
||
1162 | * if they haven't logged in en route to access a secured page. |
||
1163 | * By default, this is set to the homepage. |
||
1164 | */ |
||
1165 | private static $default_login_dest = ""; |
||
1166 | |||
1167 | protected static $ignore_disallowed_actions = false; |
||
1168 | |||
1169 | /** |
||
1170 | * Set to true to ignore access to disallowed actions, rather than returning permission failure |
||
1171 | * Note that this is just a flag that other code needs to check with Security::ignore_disallowed_actions() |
||
1172 | * @param $flag True or false |
||
1173 | */ |
||
1174 | public static function set_ignore_disallowed_actions($flag) |
||
1178 | |||
1179 | public static function ignore_disallowed_actions() |
||
1183 | |||
1184 | /** |
||
1185 | * Get the URL of the log-in page. |
||
1186 | * |
||
1187 | * To update the login url use the "Security.login_url" config setting. |
||
1188 | * |
||
1189 | * @return string |
||
1190 | */ |
||
1191 | public static function login_url() |
||
1195 | |||
1196 | |||
1197 | /** |
||
1198 | * Get the URL of the logout page. |
||
1199 | * |
||
1200 | * To update the logout url use the "Security.logout_url" config setting. |
||
1201 | * |
||
1202 | * @return string |
||
1203 | */ |
||
1204 | public static function logout_url() |
||
1208 | |||
1209 | /** |
||
1210 | * Get the URL of the logout page. |
||
1211 | * |
||
1212 | * To update the logout url use the "Security.logout_url" config setting. |
||
1213 | * |
||
1214 | * @return string |
||
1215 | */ |
||
1216 | public static function lost_password_url() |
||
1220 | |||
1221 | /** |
||
1222 | * Defines global accessible templates variables. |
||
1223 | * |
||
1224 | * @return array |
||
1225 | */ |
||
1226 | public static function get_template_global_variables() |
||
1234 | } |
||
1235 |