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) |
||
369 | |||
370 | protected function init() |
||
386 | |||
387 | public function index() |
||
391 | |||
392 | /** |
||
393 | * Get the selected authenticator for this request |
||
394 | * |
||
395 | * @param $name string The identifier of the authenticator in your config |
||
396 | * @return string Class name of Authenticator |
||
397 | * @throws LogicException |
||
398 | */ |
||
399 | protected function getAuthenticator($name) |
||
411 | |||
412 | /** |
||
413 | * Get the login form to process according to the submitted data |
||
414 | * |
||
415 | * @return Form |
||
416 | * @throws Exception |
||
417 | */ |
||
418 | public function LoginForm() |
||
427 | |||
428 | /** |
||
429 | * Get the login forms for all available authentication methods |
||
430 | * |
||
431 | * @return array Returns an array of available login forms (array of Form |
||
432 | * objects). |
||
433 | * |
||
434 | * @todo Check how to activate/deactivate authentication methods |
||
435 | */ |
||
436 | public function getLoginForms() |
||
445 | |||
446 | |||
447 | /** |
||
448 | * Get a link to a security action |
||
449 | * |
||
450 | * @param string $action Name of the action |
||
451 | * @return string Returns the link to the given action |
||
452 | */ |
||
453 | public function Link($action = null) |
||
458 | |||
459 | /** |
||
460 | * This action is available as a keep alive, so user |
||
461 | * sessions don't timeout. A common use is in the admin. |
||
462 | */ |
||
463 | public function ping() |
||
467 | |||
468 | /** |
||
469 | * Log the currently logged in user out |
||
470 | * |
||
471 | * @param bool $redirect Redirect the user back to where they came. |
||
472 | * - If it's false, the code calling logout() is |
||
473 | * responsible for sending the user where-ever |
||
474 | * they should go. |
||
475 | * @return HTTPResponse|null |
||
476 | */ |
||
477 | public function logout($redirect = true) |
||
489 | |||
490 | /** |
||
491 | * Perform pre-login checking and prepare a response if available prior to login |
||
492 | * |
||
493 | * @return HTTPResponse Substitute response object if the login process should be curcumvented. |
||
494 | * Returns null if should proceed as normal. |
||
495 | */ |
||
496 | protected function preLogin() |
||
529 | |||
530 | /** |
||
531 | * Prepare the controller for handling the response to this request |
||
532 | * |
||
533 | * @param string $title Title to use |
||
534 | * @return Controller |
||
535 | */ |
||
536 | protected function getResponseController($title) |
||
560 | |||
561 | /** |
||
562 | * Combine the given forms into a formset with a tabbed interface |
||
563 | * |
||
564 | * @param array $authenticators List of Authenticator instances |
||
565 | * @return string |
||
566 | */ |
||
567 | protected function generateLoginFormSet($forms) |
||
576 | |||
577 | /** |
||
578 | * Get the HTML Content for the $Content area during login |
||
579 | * |
||
580 | * @param string &$messageType Type of message, if available, passed back to caller |
||
581 | * @return string Message in HTML format |
||
582 | */ |
||
583 | protected function getLoginMessage(&$messageType = null) |
||
598 | |||
599 | /** |
||
600 | * Set the next message to display for the security login page. Defaults to warning |
||
601 | * |
||
602 | * @param string $message Message |
||
603 | * @param string $messageType Message type. One of ValidationResult::TYPE_* |
||
604 | * @param string $messageCast Message cast. One of ValidationResult::CAST_* |
||
605 | */ |
||
606 | public static function setLoginMessage( |
||
615 | |||
616 | /** |
||
617 | * Clear login message |
||
618 | */ |
||
619 | public static function clearLoginMessage() |
||
623 | |||
624 | |||
625 | /** |
||
626 | * Show the "login" page |
||
627 | * |
||
628 | * For multiple authenticators, Security_MultiAuthenticatorLogin is used. |
||
629 | * See getTemplatesFor and getIncludeTemplate for how to override template logic |
||
630 | * |
||
631 | * @return string|HTTPResponse Returns the "login" page as HTML code. |
||
632 | */ |
||
633 | public function login($request) |
||
672 | |||
673 | /** |
||
674 | * Delegate to an number of handlers, extracting their forms and rendering a tabbed form-set. |
||
675 | * This is used to built the log-in page where there are multiple authenticators active. |
||
676 | * |
||
677 | * If a single handler is passed, delegateToHandler() will be called instead |
||
678 | * |
||
679 | * @param string $title The title of the form |
||
680 | * @param array $templates |
||
681 | * @return array|HTTPResponse|RequestHandler|\SilverStripe\ORM\FieldType\DBHTMLText|string |
||
682 | */ |
||
683 | protected function delegateToMultipleHandlers(array $handlers, $title, array $templates) |
||
724 | |||
725 | /** |
||
726 | * Delegate to another RequestHandler, rendering any fragment arrays into an appropriate. |
||
727 | * controller. |
||
728 | * |
||
729 | * @param string $title The title of the form |
||
730 | * @param array $templates |
||
731 | * @return array|HTTPResponse|RequestHandler|\SilverStripe\ORM\FieldType\DBHTMLText|string |
||
732 | */ |
||
733 | protected function delegateToHandler(RequestHandler $handler, $title, array $templates) |
||
745 | |||
746 | /** |
||
747 | * Render the given fragments into a security page controller with the given title. |
||
748 | * @param $title string The title to give the security page |
||
749 | * @param $fragments A map of objects to render into the page, e.g. "Form" |
||
750 | * @param $templates An array of templates to use for the render |
||
751 | */ |
||
752 | protected function renderWrappedController($title, array $fragments, array $templates) |
||
779 | |||
780 | public function basicauthlogin() |
||
785 | |||
786 | /** |
||
787 | * Show the "lost password" page |
||
788 | * |
||
789 | * @return string Returns the "lost password" page as HTML code. |
||
790 | */ |
||
791 | public function lostpassword() |
||
803 | |||
804 | /** |
||
805 | * Create a link to the password reset form. |
||
806 | * |
||
807 | * GET parameters used: |
||
808 | * - m: member ID |
||
809 | * - t: plaintext token |
||
810 | * |
||
811 | * @param Member $member Member object associated with this link. |
||
812 | * @param string $autologinToken The auto login token. |
||
813 | * @return string |
||
814 | */ |
||
815 | public static function getPasswordResetLink($member, $autologinToken) |
||
823 | |||
824 | /** |
||
825 | * Show the "change password" page. |
||
826 | * This page can either be called directly by logged-in users |
||
827 | * (in which case they need to provide their old password), |
||
828 | * or through a link emailed through {@link lostpassword()}. |
||
829 | * In this case no old password is required, authentication is ensured |
||
830 | * through the Member.AutoLoginHash property. |
||
831 | * |
||
832 | * @see ChangePasswordForm |
||
833 | * |
||
834 | * @return string|HTTPRequest Returns the "change password" page as HTML code, or a redirect response |
||
835 | */ |
||
836 | public function changepassword() |
||
910 | |||
911 | /** |
||
912 | * Factory method for the lost password form |
||
913 | * |
||
914 | * @skipUpgrade |
||
915 | * @return ChangePasswordForm Returns the lost password form |
||
916 | */ |
||
917 | public function ChangePasswordForm() |
||
921 | |||
922 | /** |
||
923 | * Determine the list of templates to use for rendering the given action. |
||
924 | * |
||
925 | * @skipUpgrade |
||
926 | * @param string $action |
||
927 | * @return array Template list |
||
928 | */ |
||
929 | public function getTemplatesFor($action) |
||
942 | |||
943 | /** |
||
944 | * Return an existing member with administrator privileges, or create one of necessary. |
||
945 | * |
||
946 | * Will create a default 'Administrators' group if no group is found |
||
947 | * with an ADMIN permission. Will create a new 'Admin' member with administrative permissions |
||
948 | * if no existing Member with these permissions is found. |
||
949 | * |
||
950 | * Important: Any newly created administrator accounts will NOT have valid |
||
951 | * login credentials (Email/Password properties), which means they can't be used for login |
||
952 | * purposes outside of any default credentials set through {@link Security::setDefaultAdmin()}. |
||
953 | * |
||
954 | * @return Member |
||
955 | */ |
||
956 | public static function findAnAdministrator() |
||
1007 | |||
1008 | /** |
||
1009 | * Flush the default admin credentials |
||
1010 | */ |
||
1011 | public static function clear_default_admin() |
||
1016 | |||
1017 | |||
1018 | /** |
||
1019 | * Set a default admin in dev-mode |
||
1020 | * |
||
1021 | * This will set a static default-admin which is not existing |
||
1022 | * as a database-record. By this workaround we can test pages in dev-mode |
||
1023 | * with a unified login. Submitted login-credentials are first checked |
||
1024 | * against this static information in {@link Security::authenticate()}. |
||
1025 | * |
||
1026 | * @param string $username The user name |
||
1027 | * @param string $password The password (in cleartext) |
||
1028 | * @return bool True if successfully set |
||
1029 | */ |
||
1030 | public static function setDefaultAdmin($username, $password) |
||
1041 | |||
1042 | /** |
||
1043 | * Checks if the passed credentials are matching the default-admin. |
||
1044 | * Compares cleartext-password set through Security::setDefaultAdmin(). |
||
1045 | * |
||
1046 | * @param string $username |
||
1047 | * @param string $password |
||
1048 | * @return bool |
||
1049 | */ |
||
1050 | public static function check_default_admin($username, $password) |
||
1058 | |||
1059 | /** |
||
1060 | * Check that the default admin account has been set. |
||
1061 | */ |
||
1062 | public static function has_default_admin() |
||
1066 | |||
1067 | /** |
||
1068 | * Get default admin username |
||
1069 | * |
||
1070 | * @return string |
||
1071 | */ |
||
1072 | public static function default_admin_username() |
||
1076 | |||
1077 | /** |
||
1078 | * Get default admin password |
||
1079 | * |
||
1080 | * @return string |
||
1081 | */ |
||
1082 | public static function default_admin_password() |
||
1086 | |||
1087 | /** |
||
1088 | * Encrypt a password according to the current password encryption settings. |
||
1089 | * If the settings are so that passwords shouldn't be encrypted, the |
||
1090 | * result is simple the clear text password with an empty salt except when |
||
1091 | * a custom algorithm ($algorithm parameter) was passed. |
||
1092 | * |
||
1093 | * @param string $password The password to encrypt |
||
1094 | * @param string $salt Optional: The salt to use. If it is not passed, but |
||
1095 | * needed, the method will automatically create a |
||
1096 | * random salt that will then be returned as return value. |
||
1097 | * @param string $algorithm Optional: Use another algorithm to encrypt the |
||
1098 | * password (so that the encryption algorithm can be changed over the time). |
||
1099 | * @param Member $member Optional |
||
1100 | * @return mixed Returns an associative array containing the encrypted |
||
1101 | * password and the used salt in the form: |
||
1102 | * <code> |
||
1103 | * array( |
||
1104 | * 'password' => string, |
||
1105 | * 'salt' => string, |
||
1106 | * 'algorithm' => string, |
||
1107 | * 'encryptor' => PasswordEncryptor instance |
||
1108 | * ) |
||
1109 | * </code> |
||
1110 | * If the passed algorithm is invalid, FALSE will be returned. |
||
1111 | * |
||
1112 | * @see encrypt_passwords() |
||
1113 | */ |
||
1114 | public static function encrypt_password($password, $salt = null, $algorithm = null, $member = null) |
||
1133 | |||
1134 | /** |
||
1135 | * Checks the database is in a state to perform security checks. |
||
1136 | * See {@link DatabaseAdmin->init()} for more information. |
||
1137 | * |
||
1138 | * @return bool |
||
1139 | */ |
||
1140 | public static function database_is_ready() |
||
1188 | |||
1189 | /** |
||
1190 | * Resets the database_is_ready cache |
||
1191 | */ |
||
1192 | public static function clear_database_is_ready() |
||
1197 | |||
1198 | /** |
||
1199 | * For the database_is_ready call to return a certain value - used for testing |
||
1200 | */ |
||
1201 | public static function force_database_is_ready($isReady) |
||
1205 | |||
1206 | /** |
||
1207 | * Enable or disable recording of login attempts |
||
1208 | * through the {@link LoginRecord} object. |
||
1209 | * |
||
1210 | * @deprecated 4.0 Use the "Security.login_recording" config setting instead |
||
1211 | * @param boolean $bool |
||
1212 | */ |
||
1213 | public static function set_login_recording($bool) |
||
1218 | |||
1219 | /** |
||
1220 | * @deprecated 4.0 Use the "Security.login_recording" config setting instead |
||
1221 | * @return boolean |
||
1222 | */ |
||
1223 | public static function login_recording() |
||
1228 | |||
1229 | /** |
||
1230 | * @config |
||
1231 | * @var string Set the default login dest |
||
1232 | * This is the URL that users will be redirected to after they log in, |
||
1233 | * if they haven't logged in en route to access a secured page. |
||
1234 | * By default, this is set to the homepage. |
||
1235 | */ |
||
1236 | private static $default_login_dest = ""; |
||
1237 | |||
1238 | protected static $ignore_disallowed_actions = false; |
||
1239 | |||
1240 | /** |
||
1241 | * Set to true to ignore access to disallowed actions, rather than returning permission failure |
||
1242 | * Note that this is just a flag that other code needs to check with Security::ignore_disallowed_actions() |
||
1243 | * @param $flag True or false |
||
1244 | */ |
||
1245 | public static function set_ignore_disallowed_actions($flag) |
||
1249 | |||
1250 | public static function ignore_disallowed_actions() |
||
1254 | |||
1255 | /** |
||
1256 | * Get the URL of the log-in page. |
||
1257 | * |
||
1258 | * To update the login url use the "Security.login_url" config setting. |
||
1259 | * |
||
1260 | * @return string |
||
1261 | */ |
||
1262 | public static function login_url() |
||
1266 | |||
1267 | |||
1268 | /** |
||
1269 | * Get the URL of the logout page. |
||
1270 | * |
||
1271 | * To update the logout url use the "Security.logout_url" config setting. |
||
1272 | * |
||
1273 | * @return string |
||
1274 | */ |
||
1275 | public static function logout_url() |
||
1279 | |||
1280 | /** |
||
1281 | * Get the URL of the logout page. |
||
1282 | * |
||
1283 | * To update the logout url use the "Security.logout_url" config setting. |
||
1284 | * |
||
1285 | * @return string |
||
1286 | */ |
||
1287 | public static function lost_password_url() |
||
1291 | |||
1292 | /** |
||
1293 | * Defines global accessible templates variables. |
||
1294 | * |
||
1295 | * @return array |
||
1296 | */ |
||
1297 | public static function get_template_global_variables() |
||
1305 | } |
||
1306 |