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 |
||
41 | class Security extends Controller implements TemplateGlobalProvider |
||
42 | { |
||
43 | |||
44 | private static $allowed_actions = array( |
||
|
|||
45 | 'index', |
||
46 | 'login', |
||
47 | 'logout', |
||
48 | 'basicauthlogin', |
||
49 | 'lostpassword', |
||
50 | 'passwordsent', |
||
51 | 'changepassword', |
||
52 | 'ping', |
||
53 | 'LoginForm', |
||
54 | 'ChangePasswordForm', |
||
55 | 'LostPasswordForm', |
||
56 | ); |
||
57 | |||
58 | /** |
||
59 | * Default user name. Only used in dev-mode by {@link setDefaultAdmin()} |
||
60 | * |
||
61 | * @var string |
||
62 | * @see setDefaultAdmin() |
||
63 | */ |
||
64 | protected static $default_username; |
||
65 | |||
66 | /** |
||
67 | * Default password. Only used in dev-mode by {@link setDefaultAdmin()} |
||
68 | * |
||
69 | * @var string |
||
70 | * @see setDefaultAdmin() |
||
71 | */ |
||
72 | protected static $default_password; |
||
73 | |||
74 | /** |
||
75 | * If set to TRUE to prevent sharing of the session across several sites |
||
76 | * in the domain. |
||
77 | * |
||
78 | * @config |
||
79 | * @var bool |
||
80 | */ |
||
81 | protected static $strict_path_checking = false; |
||
82 | |||
83 | /** |
||
84 | * The password encryption algorithm to use by default. |
||
85 | * This is an arbitrary code registered through {@link PasswordEncryptor}. |
||
86 | * |
||
87 | * @config |
||
88 | * @var string |
||
89 | */ |
||
90 | private static $password_encryption_algorithm = 'blowfish'; |
||
91 | |||
92 | /** |
||
93 | * Showing "Remember me"-checkbox |
||
94 | * on loginform, and saving encrypted credentials to a cookie. |
||
95 | * |
||
96 | * @config |
||
97 | * @var bool |
||
98 | */ |
||
99 | private static $autologin_enabled = true; |
||
100 | |||
101 | /** |
||
102 | * Determine if login username may be remembered between login sessions |
||
103 | * If set to false this will disable autocomplete and prevent username persisting in the session |
||
104 | * |
||
105 | * @config |
||
106 | * @var bool |
||
107 | */ |
||
108 | private static $remember_username = true; |
||
109 | |||
110 | /** |
||
111 | * Location of word list to use for generating passwords |
||
112 | * |
||
113 | * @config |
||
114 | * @var string |
||
115 | */ |
||
116 | private static $word_list = './wordlist.txt'; |
||
117 | |||
118 | /** |
||
119 | * @config |
||
120 | * @var string |
||
121 | */ |
||
122 | private static $template = 'BlankPage'; |
||
123 | |||
124 | /** |
||
125 | * Template thats used to render the pages. |
||
126 | * |
||
127 | * @var string |
||
128 | * @config |
||
129 | */ |
||
130 | private static $template_main = 'Page'; |
||
131 | |||
132 | /** |
||
133 | * Class to use for page rendering |
||
134 | * |
||
135 | * @var string |
||
136 | * @config |
||
137 | */ |
||
138 | private static $page_class = Page::class; |
||
139 | |||
140 | /** |
||
141 | * Default message set used in permission failures. |
||
142 | * |
||
143 | * @config |
||
144 | * @var array|string |
||
145 | */ |
||
146 | private static $default_message_set; |
||
147 | |||
148 | /** |
||
149 | * Random secure token, can be used as a crypto key internally. |
||
150 | * Generate one through 'sake dev/generatesecuretoken'. |
||
151 | * |
||
152 | * @config |
||
153 | * @var String |
||
154 | */ |
||
155 | private static $token; |
||
156 | |||
157 | /** |
||
158 | * The default login URL |
||
159 | * |
||
160 | * @config |
||
161 | * |
||
162 | * @var string |
||
163 | */ |
||
164 | private static $login_url = "Security/login"; |
||
165 | |||
166 | /** |
||
167 | * The default logout URL |
||
168 | * |
||
169 | * @config |
||
170 | * |
||
171 | * @var string |
||
172 | */ |
||
173 | private static $logout_url = "Security/logout"; |
||
174 | |||
175 | /** |
||
176 | * The default lost password URL |
||
177 | * |
||
178 | * @config |
||
179 | * |
||
180 | * @var string |
||
181 | */ |
||
182 | private static $lost_password_url = "Security/lostpassword"; |
||
183 | |||
184 | /** |
||
185 | * Value of X-Frame-Options header |
||
186 | * |
||
187 | * @config |
||
188 | * @var string |
||
189 | */ |
||
190 | private static $frame_options = 'SAMEORIGIN'; |
||
191 | |||
192 | /** |
||
193 | * Value of the X-Robots-Tag header (for the Security section) |
||
194 | * |
||
195 | * @config |
||
196 | * @var string |
||
197 | */ |
||
198 | private static $robots_tag = 'noindex, nofollow'; |
||
199 | |||
200 | /** |
||
201 | * Enable or disable recording of login attempts |
||
202 | * through the {@link LoginRecord} object. |
||
203 | * |
||
204 | * @config |
||
205 | * @var boolean $login_recording |
||
206 | */ |
||
207 | private static $login_recording = false; |
||
208 | |||
209 | /** |
||
210 | * @var boolean If set to TRUE or FALSE, {@link database_is_ready()} |
||
211 | * will always return FALSE. Used for unit testing. |
||
212 | */ |
||
213 | protected static $force_database_is_ready = null; |
||
214 | |||
215 | /** |
||
216 | * When the database has once been verified as ready, it will not do the |
||
217 | * checks again. |
||
218 | * |
||
219 | * @var bool |
||
220 | */ |
||
221 | protected static $database_is_ready = false; |
||
222 | |||
223 | protected static $authenticators = []; |
||
224 | |||
225 | protected static $default_authenticator = MemberAuthenticator\Authenticator::class; |
||
226 | |||
227 | /** |
||
228 | * Get all registered authenticators |
||
229 | * |
||
230 | * @return array Return an array of Authenticator objects |
||
231 | */ |
||
232 | public static function getAuthenticators() |
||
240 | |||
241 | /** |
||
242 | * Check if a given authenticator is registered |
||
243 | * |
||
244 | * @param string $authenticator The configured identifier of the authenicator |
||
245 | * @return bool Returns TRUE if the authenticator is registered, FALSE |
||
246 | * otherwise. |
||
247 | */ |
||
248 | public static function hasAuthenticator($authenticator) |
||
253 | |||
254 | /** |
||
255 | * Register that we've had a permission failure trying to view the given page |
||
256 | * |
||
257 | * This will redirect to a login page. |
||
258 | * If you don't provide a messageSet, a default will be used. |
||
259 | * |
||
260 | * @param Controller $controller The controller that you were on to cause the permission |
||
261 | * failure. |
||
262 | * @param string|array $messageSet The message to show to the user. This |
||
263 | * can be a string, or a map of different |
||
264 | * messages for different contexts. |
||
265 | * If you pass an array, you can use the |
||
266 | * following keys: |
||
267 | * - default: The default message |
||
268 | * - alreadyLoggedIn: The message to |
||
269 | * show if the user |
||
270 | * is already logged |
||
271 | * in and lacks the |
||
272 | * permission to |
||
273 | * access the item. |
||
274 | * |
||
275 | * The alreadyLoggedIn value can contain a '%s' placeholder that will be replaced with a link |
||
276 | * to log in. |
||
277 | * @return HTTPResponse |
||
278 | */ |
||
279 | public static function permissionFailure($controller = null, $messageSet = null) |
||
373 | |||
374 | protected function init() |
||
390 | |||
391 | public function index() |
||
395 | |||
396 | /** |
||
397 | * Get the selected authenticator for this request |
||
398 | * |
||
399 | * @param $name string The identifier of the authenticator in your config |
||
400 | * @return string Class name of Authenticator |
||
401 | * @throws LogicException |
||
402 | */ |
||
403 | protected function getAuthenticator($name) |
||
415 | |||
416 | /** |
||
417 | * Get the login form to process according to the submitted data |
||
418 | * |
||
419 | * @return Form |
||
420 | * @throws Exception |
||
421 | */ |
||
422 | public function LoginForm() |
||
431 | |||
432 | /** |
||
433 | * Get the login forms for all available authentication methods |
||
434 | * |
||
435 | * @return array Returns an array of available login forms (array of Form |
||
436 | * objects). |
||
437 | * |
||
438 | * @todo Check how to activate/deactivate authentication methods |
||
439 | */ |
||
440 | public function getLoginForms() |
||
449 | |||
450 | |||
451 | /** |
||
452 | * Get a link to a security action |
||
453 | * |
||
454 | * @param string $action Name of the action |
||
455 | * @return string Returns the link to the given action |
||
456 | */ |
||
457 | public function Link($action = null) |
||
462 | |||
463 | /** |
||
464 | * This action is available as a keep alive, so user |
||
465 | * sessions don't timeout. A common use is in the admin. |
||
466 | */ |
||
467 | public function ping() |
||
471 | |||
472 | /** |
||
473 | * Log the currently logged in user out |
||
474 | * |
||
475 | * @param bool $redirect Redirect the user back to where they came. |
||
476 | * - If it's false, the code calling logout() is |
||
477 | * responsible for sending the user where-ever |
||
478 | * they should go. |
||
479 | * @return HTTPResponse|null |
||
480 | */ |
||
481 | public function logout($redirect = true) |
||
493 | |||
494 | /** |
||
495 | * Perform pre-login checking and prepare a response if available prior to login |
||
496 | * |
||
497 | * @return HTTPResponse Substitute response object if the login process should be curcumvented. |
||
498 | * Returns null if should proceed as normal. |
||
499 | */ |
||
500 | protected function preLogin() |
||
533 | |||
534 | /** |
||
535 | * Prepare the controller for handling the response to this request |
||
536 | * |
||
537 | * @param string $title Title to use |
||
538 | * @return Controller |
||
539 | */ |
||
540 | protected function getResponseController($title) |
||
564 | |||
565 | /** |
||
566 | * Combine the given forms into a formset with a tabbed interface |
||
567 | * |
||
568 | * @param array $authenticators List of Authenticator instances |
||
569 | * @return string |
||
570 | */ |
||
571 | protected function generateLoginFormSet($forms) |
||
580 | |||
581 | /** |
||
582 | * Get the HTML Content for the $Content area during login |
||
583 | * |
||
584 | * @param string &$messageType Type of message, if available, passed back to caller |
||
585 | * @return string Message in HTML format |
||
586 | */ |
||
587 | protected function getLoginMessage(&$messageType = null) |
||
602 | |||
603 | /** |
||
604 | * Set the next message to display for the security login page. Defaults to warning |
||
605 | * |
||
606 | * @param string $message Message |
||
607 | * @param string $messageType Message type. One of ValidationResult::TYPE_* |
||
608 | * @param string $messageCast Message cast. One of ValidationResult::CAST_* |
||
609 | */ |
||
610 | public static function setLoginMessage( |
||
619 | |||
620 | /** |
||
621 | * Clear login message |
||
622 | */ |
||
623 | public static function clearLoginMessage() |
||
627 | |||
628 | |||
629 | /** |
||
630 | * Show the "login" page |
||
631 | * |
||
632 | * For multiple authenticators, Security_MultiAuthenticatorLogin is used. |
||
633 | * See getTemplatesFor and getIncludeTemplate for how to override template logic |
||
634 | * |
||
635 | * @return string|HTTPResponse Returns the "login" page as HTML code. |
||
636 | */ |
||
637 | public function login($request) |
||
690 | |||
691 | /** |
||
692 | * Delegate to an number of handlers, extracting their forms and rendering a tabbed form-set. |
||
693 | * This is used to built the log-in page where there are multiple authenticators active. |
||
694 | * |
||
695 | * @param string $title The title of the form |
||
696 | * @param array $templates |
||
697 | * @return array|HTTPResponse|RequestHandler|\SilverStripe\ORM\FieldType\DBHTMLText|string |
||
698 | */ |
||
699 | protected function delegateToFormSet(array $handlers, $title, array $templates) |
||
735 | |||
736 | /** |
||
737 | * Delegate to another RequestHandler, rendering any fragment arrays into an appropriate. |
||
738 | * controller. |
||
739 | * |
||
740 | * @param string $title The title of the form |
||
741 | * @param array $templates |
||
742 | * @return array|HTTPResponse|RequestHandler|\SilverStripe\ORM\FieldType\DBHTMLText|string |
||
743 | */ |
||
744 | protected function delegateToHandler(RequestHandler $handler, $title, array $templates) |
||
756 | |||
757 | /** |
||
758 | * Render the given fragments into a security page controller with the given title. |
||
759 | * @param $title string The title to give the security page |
||
760 | * @param $fragments A map of objects to render into the page, e.g. "Form" |
||
761 | * @param $templates An array of templates to use for the render |
||
762 | */ |
||
763 | protected function renderWrappedController($title, array $fragments, array $templates) |
||
790 | |||
791 | public function basicauthlogin() |
||
796 | |||
797 | /** |
||
798 | * Show the "lost password" page |
||
799 | * |
||
800 | * @return string Returns the "lost password" page as HTML code. |
||
801 | */ |
||
802 | public function lostpassword() |
||
814 | |||
815 | /** |
||
816 | * Create a link to the password reset form. |
||
817 | * |
||
818 | * GET parameters used: |
||
819 | * - m: member ID |
||
820 | * - t: plaintext token |
||
821 | * |
||
822 | * @param Member $member Member object associated with this link. |
||
823 | * @param string $autologinToken The auto login token. |
||
824 | * @return string |
||
825 | */ |
||
826 | public static function getPasswordResetLink($member, $autologinToken) |
||
834 | |||
835 | /** |
||
836 | * Show the "change password" page. |
||
837 | * This page can either be called directly by logged-in users |
||
838 | * (in which case they need to provide their old password), |
||
839 | * or through a link emailed through {@link lostpassword()}. |
||
840 | * In this case no old password is required, authentication is ensured |
||
841 | * through the Member.AutoLoginHash property. |
||
842 | * |
||
843 | * @see ChangePasswordForm |
||
844 | * |
||
845 | * @return string|HTTPRequest Returns the "change password" page as HTML code, or a redirect response |
||
846 | */ |
||
847 | public function changepassword() |
||
921 | |||
922 | /** |
||
923 | * Factory method for the lost password form |
||
924 | * |
||
925 | * @skipUpgrade |
||
926 | * @return ChangePasswordForm Returns the lost password form |
||
927 | */ |
||
928 | public function ChangePasswordForm() |
||
932 | |||
933 | /** |
||
934 | * Determine the list of templates to use for rendering the given action. |
||
935 | * |
||
936 | * @skipUpgrade |
||
937 | * @param string $action |
||
938 | * @return array Template list |
||
939 | */ |
||
940 | public function getTemplatesFor($action) |
||
953 | |||
954 | /** |
||
955 | * Return an existing member with administrator privileges, or create one of necessary. |
||
956 | * |
||
957 | * Will create a default 'Administrators' group if no group is found |
||
958 | * with an ADMIN permission. Will create a new 'Admin' member with administrative permissions |
||
959 | * if no existing Member with these permissions is found. |
||
960 | * |
||
961 | * Important: Any newly created administrator accounts will NOT have valid |
||
962 | * login credentials (Email/Password properties), which means they can't be used for login |
||
963 | * purposes outside of any default credentials set through {@link Security::setDefaultAdmin()}. |
||
964 | * |
||
965 | * @return Member |
||
966 | */ |
||
967 | public static function findAnAdministrator() |
||
1018 | |||
1019 | /** |
||
1020 | * Flush the default admin credentials |
||
1021 | */ |
||
1022 | public static function clear_default_admin() |
||
1027 | |||
1028 | |||
1029 | /** |
||
1030 | * Set a default admin in dev-mode |
||
1031 | * |
||
1032 | * This will set a static default-admin which is not existing |
||
1033 | * as a database-record. By this workaround we can test pages in dev-mode |
||
1034 | * with a unified login. Submitted login-credentials are first checked |
||
1035 | * against this static information in {@link Security::authenticate()}. |
||
1036 | * |
||
1037 | * @param string $username The user name |
||
1038 | * @param string $password The password (in cleartext) |
||
1039 | * @return bool True if successfully set |
||
1040 | */ |
||
1041 | public static function setDefaultAdmin($username, $password) |
||
1052 | |||
1053 | /** |
||
1054 | * Checks if the passed credentials are matching the default-admin. |
||
1055 | * Compares cleartext-password set through Security::setDefaultAdmin(). |
||
1056 | * |
||
1057 | * @param string $username |
||
1058 | * @param string $password |
||
1059 | * @return bool |
||
1060 | */ |
||
1061 | public static function check_default_admin($username, $password) |
||
1069 | |||
1070 | /** |
||
1071 | * Check that the default admin account has been set. |
||
1072 | */ |
||
1073 | public static function has_default_admin() |
||
1077 | |||
1078 | /** |
||
1079 | * Get default admin username |
||
1080 | * |
||
1081 | * @return string |
||
1082 | */ |
||
1083 | public static function default_admin_username() |
||
1087 | |||
1088 | /** |
||
1089 | * Get default admin password |
||
1090 | * |
||
1091 | * @return string |
||
1092 | */ |
||
1093 | public static function default_admin_password() |
||
1097 | |||
1098 | /** |
||
1099 | * Encrypt a password according to the current password encryption settings. |
||
1100 | * If the settings are so that passwords shouldn't be encrypted, the |
||
1101 | * result is simple the clear text password with an empty salt except when |
||
1102 | * a custom algorithm ($algorithm parameter) was passed. |
||
1103 | * |
||
1104 | * @param string $password The password to encrypt |
||
1105 | * @param string $salt Optional: The salt to use. If it is not passed, but |
||
1106 | * needed, the method will automatically create a |
||
1107 | * random salt that will then be returned as return value. |
||
1108 | * @param string $algorithm Optional: Use another algorithm to encrypt the |
||
1109 | * password (so that the encryption algorithm can be changed over the time). |
||
1110 | * @param Member $member Optional |
||
1111 | * @return mixed Returns an associative array containing the encrypted |
||
1112 | * password and the used salt in the form: |
||
1113 | * <code> |
||
1114 | * array( |
||
1115 | * 'password' => string, |
||
1116 | * 'salt' => string, |
||
1117 | * 'algorithm' => string, |
||
1118 | * 'encryptor' => PasswordEncryptor instance |
||
1119 | * ) |
||
1120 | * </code> |
||
1121 | * If the passed algorithm is invalid, FALSE will be returned. |
||
1122 | * |
||
1123 | * @see encrypt_passwords() |
||
1124 | */ |
||
1125 | public static function encrypt_password($password, $salt = null, $algorithm = null, $member = null) |
||
1144 | |||
1145 | /** |
||
1146 | * Checks the database is in a state to perform security checks. |
||
1147 | * See {@link DatabaseAdmin->init()} for more information. |
||
1148 | * |
||
1149 | * @return bool |
||
1150 | */ |
||
1151 | public static function database_is_ready() |
||
1199 | |||
1200 | /** |
||
1201 | * Enable or disable recording of login attempts |
||
1202 | * through the {@link LoginRecord} object. |
||
1203 | * |
||
1204 | * @deprecated 4.0 Use the "Security.login_recording" config setting instead |
||
1205 | * @param boolean $bool |
||
1206 | */ |
||
1207 | public static function set_login_recording($bool) |
||
1212 | |||
1213 | /** |
||
1214 | * @deprecated 4.0 Use the "Security.login_recording" config setting instead |
||
1215 | * @return boolean |
||
1216 | */ |
||
1217 | public static function login_recording() |
||
1222 | |||
1223 | /** |
||
1224 | * @config |
||
1225 | * @var string Set the default login dest |
||
1226 | * This is the URL that users will be redirected to after they log in, |
||
1227 | * if they haven't logged in en route to access a secured page. |
||
1228 | * By default, this is set to the homepage. |
||
1229 | */ |
||
1230 | private static $default_login_dest = ""; |
||
1231 | |||
1232 | protected static $ignore_disallowed_actions = false; |
||
1233 | |||
1234 | /** |
||
1235 | * Set to true to ignore access to disallowed actions, rather than returning permission failure |
||
1236 | * Note that this is just a flag that other code needs to check with Security::ignore_disallowed_actions() |
||
1237 | * @param $flag True or false |
||
1238 | */ |
||
1239 | public static function set_ignore_disallowed_actions($flag) |
||
1243 | |||
1244 | public static function ignore_disallowed_actions() |
||
1248 | |||
1249 | /** |
||
1250 | * Get the URL of the log-in page. |
||
1251 | * |
||
1252 | * To update the login url use the "Security.login_url" config setting. |
||
1253 | * |
||
1254 | * @return string |
||
1255 | */ |
||
1256 | public static function login_url() |
||
1260 | |||
1261 | |||
1262 | /** |
||
1263 | * Get the URL of the logout page. |
||
1264 | * |
||
1265 | * To update the logout url use the "Security.logout_url" config setting. |
||
1266 | * |
||
1267 | * @return string |
||
1268 | */ |
||
1269 | public static function logout_url() |
||
1273 | |||
1274 | /** |
||
1275 | * Get the URL of the logout page. |
||
1276 | * |
||
1277 | * To update the logout url use the "Security.logout_url" config setting. |
||
1278 | * |
||
1279 | * @return string |
||
1280 | */ |
||
1281 | public static function lost_password_url() |
||
1285 | |||
1286 | /** |
||
1287 | * Defines global accessible templates variables. |
||
1288 | * |
||
1289 | * @return array |
||
1290 | */ |
||
1291 | public static function get_template_global_variables() |
||
1299 | } |
||
1300 |