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 |
||
36 | class Security extends Controller implements TemplateGlobalProvider |
||
37 | { |
||
38 | |||
39 | private static $allowed_actions = array( |
||
40 | 'index', |
||
41 | 'login', |
||
42 | 'logout', |
||
43 | 'basicauthlogin', |
||
44 | 'lostpassword', |
||
45 | 'passwordsent', |
||
46 | 'changepassword', |
||
47 | 'ping', |
||
48 | ); |
||
49 | |||
50 | /** |
||
51 | * If set to TRUE to prevent sharing of the session across several sites |
||
52 | * in the domain. |
||
53 | * |
||
54 | * @config |
||
55 | * @var bool |
||
56 | */ |
||
57 | private static $strict_path_checking = false; |
||
58 | |||
59 | /** |
||
60 | * The password encryption algorithm to use by default. |
||
61 | * This is an arbitrary code registered through {@link PasswordEncryptor}. |
||
62 | * |
||
63 | * @config |
||
64 | * @var string |
||
65 | */ |
||
66 | private static $password_encryption_algorithm = 'blowfish'; |
||
67 | |||
68 | /** |
||
69 | * Showing "Remember me"-checkbox |
||
70 | * on loginform, and saving encrypted credentials to a cookie. |
||
71 | * |
||
72 | * @config |
||
73 | * @var bool |
||
74 | */ |
||
75 | private static $autologin_enabled = true; |
||
76 | |||
77 | /** |
||
78 | * Determine if login username may be remembered between login sessions |
||
79 | * If set to false this will disable auto-complete and prevent username persisting in the session |
||
80 | * |
||
81 | * @config |
||
82 | * @var bool |
||
83 | */ |
||
84 | private static $remember_username = true; |
||
85 | |||
86 | /** |
||
87 | * Location of word list to use for generating passwords |
||
88 | * |
||
89 | * @config |
||
90 | * @var string |
||
91 | */ |
||
92 | private static $word_list = './wordlist.txt'; |
||
93 | |||
94 | /** |
||
95 | * @config |
||
96 | * @var string |
||
97 | */ |
||
98 | private static $template = 'BlankPage'; |
||
99 | |||
100 | /** |
||
101 | * Template that is used to render the pages. |
||
102 | * |
||
103 | * @var string |
||
104 | * @config |
||
105 | */ |
||
106 | private static $template_main = 'Page'; |
||
107 | |||
108 | /** |
||
109 | * Class to use for page rendering |
||
110 | * |
||
111 | * @var string |
||
112 | * @config |
||
113 | */ |
||
114 | private static $page_class = Page::class; |
||
115 | |||
116 | /** |
||
117 | * Default message set used in permission failures. |
||
118 | * |
||
119 | * @config |
||
120 | * @var array|string |
||
121 | */ |
||
122 | private static $default_message_set; |
||
123 | |||
124 | /** |
||
125 | * Random secure token, can be used as a crypto key internally. |
||
126 | * Generate one through 'sake dev/generatesecuretoken'. |
||
127 | * |
||
128 | * @config |
||
129 | * @var String |
||
130 | */ |
||
131 | private static $token; |
||
132 | |||
133 | /** |
||
134 | * The default login URL |
||
135 | * |
||
136 | * @config |
||
137 | * |
||
138 | * @var string |
||
139 | */ |
||
140 | private static $login_url = 'Security/login'; |
||
141 | |||
142 | /** |
||
143 | * The default logout URL |
||
144 | * |
||
145 | * @config |
||
146 | * |
||
147 | * @var string |
||
148 | */ |
||
149 | private static $logout_url = 'Security/logout'; |
||
150 | |||
151 | /** |
||
152 | * The default lost password URL |
||
153 | * |
||
154 | * @config |
||
155 | * |
||
156 | * @var string |
||
157 | */ |
||
158 | private static $lost_password_url = 'Security/lostpassword'; |
||
159 | |||
160 | /** |
||
161 | * Value of X-Frame-Options header |
||
162 | * |
||
163 | * @config |
||
164 | * @var string |
||
165 | */ |
||
166 | private static $frame_options = 'SAMEORIGIN'; |
||
167 | |||
168 | /** |
||
169 | * Value of the X-Robots-Tag header (for the Security section) |
||
170 | * |
||
171 | * @config |
||
172 | * @var string |
||
173 | */ |
||
174 | private static $robots_tag = 'noindex, nofollow'; |
||
175 | |||
176 | /** |
||
177 | * Enable or disable recording of login attempts |
||
178 | * through the {@link LoginRecord} object. |
||
179 | * |
||
180 | * @config |
||
181 | * @var boolean $login_recording |
||
182 | */ |
||
183 | private static $login_recording = false; |
||
184 | |||
185 | /** |
||
186 | * @var boolean If set to TRUE or FALSE, {@link database_is_ready()} |
||
187 | * will always return FALSE. Used for unit testing. |
||
188 | */ |
||
189 | protected static $force_database_is_ready; |
||
190 | |||
191 | /** |
||
192 | * When the database has once been verified as ready, it will not do the |
||
193 | * checks again. |
||
194 | * |
||
195 | * @var bool |
||
196 | */ |
||
197 | protected static $database_is_ready = false; |
||
198 | |||
199 | /** |
||
200 | * @var Authenticator[] available authenticators |
||
201 | */ |
||
202 | private $authenticators = []; |
||
203 | |||
204 | /** |
||
205 | * @var Member Currently logged in user (if available) |
||
206 | */ |
||
207 | protected static $currentUser; |
||
208 | |||
209 | /** |
||
210 | * @return Authenticator[] |
||
211 | */ |
||
212 | public function getAuthenticators() |
||
216 | |||
217 | /** |
||
218 | * @param Authenticator[] $authenticators |
||
219 | */ |
||
220 | public function setAuthenticators(array $authenticators) |
||
224 | |||
225 | protected function init() |
||
241 | |||
242 | public function index() |
||
246 | |||
247 | /** |
||
248 | * Get the selected authenticator for this request |
||
249 | * |
||
250 | * @param string $name The identifier of the authenticator in your config |
||
251 | * @return Authenticator Class name of Authenticator |
||
252 | * @throws LogicException |
||
253 | */ |
||
254 | protected function getAuthenticator($name = 'default') |
||
264 | |||
265 | /** |
||
266 | * Get all registered authenticators |
||
267 | * |
||
268 | * @param int $service The type of service that is requested |
||
269 | * @return Authenticator[] Return an array of Authenticator objects |
||
270 | */ |
||
271 | public function getApplicableAuthenticators($service = Authenticator::LOGIN) |
||
288 | |||
289 | /** |
||
290 | * Check if a given authenticator is registered |
||
291 | * |
||
292 | * @param string $authenticator The configured identifier of the authenicator |
||
293 | * @return bool Returns TRUE if the authenticator is registered, FALSE |
||
294 | * otherwise. |
||
295 | */ |
||
296 | public function hasAuthenticator($authenticator) |
||
302 | |||
303 | /** |
||
304 | * Register that we've had a permission failure trying to view the given page |
||
305 | * |
||
306 | * This will redirect to a login page. |
||
307 | * If you don't provide a messageSet, a default will be used. |
||
308 | * |
||
309 | * @param Controller $controller The controller that you were on to cause the permission |
||
310 | * failure. |
||
311 | * @param string|array $messageSet The message to show to the user. This |
||
312 | * can be a string, or a map of different |
||
313 | * messages for different contexts. |
||
314 | * If you pass an array, you can use the |
||
315 | * following keys: |
||
316 | * - default: The default message |
||
317 | * - alreadyLoggedIn: The message to |
||
318 | * show if the user |
||
319 | * is already logged |
||
320 | * in and lacks the |
||
321 | * permission to |
||
322 | * access the item. |
||
323 | * |
||
324 | * The alreadyLoggedIn value can contain a '%s' placeholder that will be replaced with a link |
||
325 | * to log in. |
||
326 | * @return HTTPResponse |
||
327 | */ |
||
328 | public static function permissionFailure($controller = null, $messageSet = null) |
||
423 | |||
424 | /** |
||
425 | * @param null|Member $currentUser |
||
426 | */ |
||
427 | public static function setCurrentUser($currentUser = null) |
||
431 | |||
432 | /** |
||
433 | * @return null|Member |
||
434 | */ |
||
435 | public static function getCurrentUser() |
||
439 | |||
440 | /** |
||
441 | * Get the login forms for all available authentication methods |
||
442 | * |
||
443 | * @deprecated 5.0.0 Now handled by {@link static::delegateToMultipleHandlers} |
||
444 | * |
||
445 | * @return array Returns an array of available login forms (array of Form |
||
446 | * objects). |
||
447 | * |
||
448 | */ |
||
449 | public function getLoginForms() |
||
462 | |||
463 | |||
464 | /** |
||
465 | * Get a link to a security action |
||
466 | * |
||
467 | * @param string $action Name of the action |
||
468 | * @return string Returns the link to the given action |
||
469 | */ |
||
470 | public function Link($action = null) |
||
475 | |||
476 | /** |
||
477 | * This action is available as a keep alive, so user |
||
478 | * sessions don't timeout. A common use is in the admin. |
||
479 | */ |
||
480 | public function ping() |
||
484 | |||
485 | /** |
||
486 | * Log the currently logged in user out |
||
487 | * |
||
488 | * Logging out without ID-parameter in the URL, will log the user out of all applicable Authenticators. |
||
489 | * |
||
490 | * Adding an ID will only log the user out of that Authentication method. |
||
491 | * |
||
492 | * Logging out of Default will <i>always</i> completely log out the user. |
||
493 | * |
||
494 | * @param bool $redirect Redirect the user back to where they came. |
||
495 | * - If it's false, the code calling logout() is |
||
496 | * responsible for sending the user where-ever |
||
497 | * they should go. |
||
498 | * @return HTTPResponse|null |
||
499 | */ |
||
500 | public function logout($redirect = true) |
||
532 | |||
533 | /** |
||
534 | * Perform pre-login checking and prepare a response if available prior to login |
||
535 | * |
||
536 | * @return HTTPResponse Substitute response object if the login process should be curcumvented. |
||
537 | * Returns null if should proceed as normal. |
||
538 | */ |
||
539 | protected function preLogin() |
||
572 | |||
573 | /** |
||
574 | * Prepare the controller for handling the response to this request |
||
575 | * |
||
576 | * @param string $title Title to use |
||
577 | * @return Controller |
||
578 | */ |
||
579 | protected function getResponseController($title) |
||
602 | |||
603 | /** |
||
604 | * Combine the given forms into a formset with a tabbed interface |
||
605 | * |
||
606 | * @param array|Form[] $forms |
||
607 | * @return string |
||
608 | */ |
||
609 | protected function generateLoginFormSet($forms) |
||
619 | |||
620 | /** |
||
621 | * Get the HTML Content for the $Content area during login |
||
622 | * |
||
623 | * @param string &$messageType Type of message, if available, passed back to caller |
||
624 | * @return string Message in HTML format |
||
625 | */ |
||
626 | protected function getLoginMessage(&$messageType = null) |
||
642 | |||
643 | /** |
||
644 | * Set the next message to display for the security login page. Defaults to warning |
||
645 | * |
||
646 | * @param string $message Message |
||
647 | * @param string $messageType Message type. One of ValidationResult::TYPE_* |
||
648 | * @param string $messageCast Message cast. One of ValidationResult::CAST_* |
||
649 | */ |
||
650 | public function setLoginMessage( |
||
659 | |||
660 | /** |
||
661 | * Clear login message |
||
662 | */ |
||
663 | public static function clearLoginMessage() |
||
667 | |||
668 | |||
669 | /** |
||
670 | * Show the "login" page |
||
671 | * |
||
672 | * For multiple authenticators, Security_MultiAuthenticatorLogin is used. |
||
673 | * See getTemplatesFor and getIncludeTemplate for how to override template logic |
||
674 | * |
||
675 | * @param null|HTTPRequest $request |
||
676 | * @param int $service |
||
677 | * @return HTTPResponse|string Returns the "login" page as HTML code. |
||
678 | * @throws HTTPResponse_Exception |
||
679 | */ |
||
680 | public function login($request = null, $service = Authenticator::LOGIN) |
||
729 | |||
730 | /** |
||
731 | * Delegate to an number of handlers, extracting their forms and rendering a tabbed form-set. |
||
732 | * This is used to built the log-in page where there are multiple authenticators active. |
||
733 | * |
||
734 | * If a single handler is passed, delegateToHandler() will be called instead |
||
735 | * |
||
736 | * @param array|RequestHandler[] $handlers |
||
737 | * @param string $title The title of the form |
||
738 | * @param array $templates |
||
739 | * @return array|HTTPResponse|RequestHandler|DBHTMLText|string |
||
740 | */ |
||
741 | protected function delegateToMultipleHandlers(array $handlers, $title, array $templates) |
||
781 | |||
782 | /** |
||
783 | * Delegate to another RequestHandler, rendering any fragment arrays into an appropriate. |
||
784 | * controller. |
||
785 | * |
||
786 | * @param RequestHandler $handler |
||
787 | * @param string $title The title of the form |
||
788 | * @param array $templates |
||
789 | * @return array|HTTPResponse|RequestHandler|DBHTMLText|string |
||
790 | */ |
||
791 | protected function delegateToHandler(RequestHandler $handler, $title, array $templates = []) |
||
803 | |||
804 | /** |
||
805 | * Render the given fragments into a security page controller with the given title. |
||
806 | * @param string $title string The title to give the security page |
||
807 | * @param array $fragments A map of objects to render into the page, e.g. "Form" |
||
808 | * @param array $templates An array of templates to use for the render |
||
809 | * @return HTTPResponse|DBHTMLText |
||
810 | */ |
||
811 | protected function renderWrappedController($title, array $fragments, array $templates) |
||
838 | |||
839 | public function basicauthlogin() |
||
844 | |||
845 | /** |
||
846 | * Show the "lost password" page |
||
847 | * |
||
848 | * @return string Returns the "lost password" page as HTML code. |
||
849 | */ |
||
850 | public function lostpassword() |
||
867 | |||
868 | /** |
||
869 | * Show the "change password" page. |
||
870 | * This page can either be called directly by logged-in users |
||
871 | * (in which case they need to provide their old password), |
||
872 | * or through a link emailed through {@link lostpassword()}. |
||
873 | * In this case no old password is required, authentication is ensured |
||
874 | * through the Member.AutoLoginHash property. |
||
875 | * |
||
876 | * @see ChangePasswordForm |
||
877 | * |
||
878 | * @return string|HTTPRequest Returns the "change password" page as HTML code, or a redirect response |
||
879 | */ |
||
880 | public function changepassword() |
||
895 | |||
896 | /** |
||
897 | * Create a link to the password reset form. |
||
898 | * |
||
899 | * GET parameters used: |
||
900 | * - m: member ID |
||
901 | * - t: plaintext token |
||
902 | * |
||
903 | * @param Member $member Member object associated with this link. |
||
904 | * @param string $autologinToken The auto login token. |
||
905 | * @return string |
||
906 | */ |
||
907 | public static function getPasswordResetLink($member, $autologinToken) |
||
913 | |||
914 | /** |
||
915 | * Determine the list of templates to use for rendering the given action. |
||
916 | * |
||
917 | * @skipUpgrade |
||
918 | * @param string $action |
||
919 | * @return array Template list |
||
920 | */ |
||
921 | public function getTemplatesFor($action) |
||
935 | |||
936 | /** |
||
937 | * Return an existing member with administrator privileges, or create one of necessary. |
||
938 | * |
||
939 | * Will create a default 'Administrators' group if no group is found |
||
940 | * with an ADMIN permission. Will create a new 'Admin' member with administrative permissions |
||
941 | * if no existing Member with these permissions is found. |
||
942 | * |
||
943 | * Important: Any newly created administrator accounts will NOT have valid |
||
944 | * login credentials (Email/Password properties), which means they can't be used for login |
||
945 | * purposes outside of any default credentials set through {@link Security::setDefaultAdmin()}. |
||
946 | * |
||
947 | * @return Member |
||
948 | * |
||
949 | * @deprecated 4.0.0..5.0.0 Please use DefaultAdminService::findOrCreateDefaultAdmin() |
||
950 | */ |
||
951 | public static function findAnAdministrator() |
||
958 | |||
959 | /** |
||
960 | * Flush the default admin credentials |
||
961 | * |
||
962 | * @deprecated 4.0.0..5.0.0 Please use DefaultAdminService::clearDefaultAdmin() |
||
963 | */ |
||
964 | public static function clear_default_admin() |
||
970 | |||
971 | |||
972 | /** |
||
973 | * Set a default admin in dev-mode |
||
974 | * |
||
975 | * This will set a static default-admin which is not existing |
||
976 | * as a database-record. By this workaround we can test pages in dev-mode |
||
977 | * with a unified login. Submitted login-credentials are first checked |
||
978 | * against this static information in {@link Security::authenticate()}. |
||
979 | * |
||
980 | * @param string $username The user name |
||
981 | * @param string $password The password (in cleartext) |
||
982 | * @return bool True if successfully set |
||
983 | * |
||
984 | * @deprecated 4.0.0..5.0.0 Please use DefaultAdminService::setDefaultAdmin($username, $password) |
||
985 | */ |
||
986 | public static function setDefaultAdmin($username, $password) |
||
993 | |||
994 | /** |
||
995 | * Checks if the passed credentials are matching the default-admin. |
||
996 | * Compares cleartext-password set through Security::setDefaultAdmin(). |
||
997 | * |
||
998 | * @param string $username |
||
999 | * @param string $password |
||
1000 | * @return bool |
||
1001 | * |
||
1002 | * @deprecated 4.0.0..5.0.0 Use DefaultAdminService::isDefaultAdminCredentials() instead |
||
1003 | */ |
||
1004 | public static function check_default_admin($username, $password) |
||
1011 | |||
1012 | /** |
||
1013 | * Check that the default admin account has been set. |
||
1014 | * |
||
1015 | * @deprecated 4.0.0..5.0.0 Use DefaultAdminService::hasDefaultAdmin() instead |
||
1016 | */ |
||
1017 | public static function has_default_admin() |
||
1023 | |||
1024 | /** |
||
1025 | * Get default admin username |
||
1026 | * |
||
1027 | * @deprecated 4.0.0..5.0.0 Use DefaultAdminService::getDefaultAdminUsername() |
||
1028 | * @return string |
||
1029 | */ |
||
1030 | public static function default_admin_username() |
||
1036 | |||
1037 | /** |
||
1038 | * Get default admin password |
||
1039 | * |
||
1040 | * @deprecated 4.0.0..5.0.0 Use DefaultAdminService::getDefaultAdminPassword() |
||
1041 | * @return string |
||
1042 | */ |
||
1043 | public static function default_admin_password() |
||
1049 | |||
1050 | /** |
||
1051 | * Encrypt a password according to the current password encryption settings. |
||
1052 | * If the settings are so that passwords shouldn't be encrypted, the |
||
1053 | * result is simple the clear text password with an empty salt except when |
||
1054 | * a custom algorithm ($algorithm parameter) was passed. |
||
1055 | * |
||
1056 | * @param string $password The password to encrypt |
||
1057 | * @param string $salt Optional: The salt to use. If it is not passed, but |
||
1058 | * needed, the method will automatically create a |
||
1059 | * random salt that will then be returned as return value. |
||
1060 | * @param string $algorithm Optional: Use another algorithm to encrypt the |
||
1061 | * password (so that the encryption algorithm can be changed over the time). |
||
1062 | * @param Member $member Optional |
||
1063 | * @return mixed Returns an associative array containing the encrypted |
||
1064 | * password and the used salt in the form: |
||
1065 | * <code> |
||
1066 | * array( |
||
1067 | * 'password' => string, |
||
1068 | * 'salt' => string, |
||
1069 | * 'algorithm' => string, |
||
1070 | * 'encryptor' => PasswordEncryptor instance |
||
1071 | * ) |
||
1072 | * </code> |
||
1073 | * If the passed algorithm is invalid, FALSE will be returned. |
||
1074 | * |
||
1075 | * @see encrypt_passwords() |
||
1076 | */ |
||
1077 | public static function encrypt_password($password, $salt = null, $algorithm = null, $member = null) |
||
1096 | |||
1097 | /** |
||
1098 | * Checks the database is in a state to perform security checks. |
||
1099 | * See {@link DatabaseAdmin->init()} for more information. |
||
1100 | * |
||
1101 | * @return bool |
||
1102 | */ |
||
1103 | public static function database_is_ready() |
||
1151 | |||
1152 | /** |
||
1153 | * Resets the database_is_ready cache |
||
1154 | */ |
||
1155 | public static function clear_database_is_ready() |
||
1160 | |||
1161 | /** |
||
1162 | * For the database_is_ready call to return a certain value - used for testing |
||
1163 | * |
||
1164 | * @param bool $isReady |
||
1165 | */ |
||
1166 | public static function force_database_is_ready($isReady) |
||
1170 | |||
1171 | /** |
||
1172 | * @config |
||
1173 | * @var string Set the default login dest |
||
1174 | * This is the URL that users will be redirected to after they log in, |
||
1175 | * if they haven't logged in en route to access a secured page. |
||
1176 | * By default, this is set to the homepage. |
||
1177 | */ |
||
1178 | private static $default_login_dest = ""; |
||
1179 | |||
1180 | protected static $ignore_disallowed_actions = false; |
||
1181 | |||
1182 | /** |
||
1183 | * Set to true to ignore access to disallowed actions, rather than returning permission failure |
||
1184 | * Note that this is just a flag that other code needs to check with Security::ignore_disallowed_actions() |
||
1185 | * @param bool $flag True or false |
||
1186 | */ |
||
1187 | public static function set_ignore_disallowed_actions($flag) |
||
1191 | |||
1192 | public static function ignore_disallowed_actions() |
||
1196 | |||
1197 | /** |
||
1198 | * Get the URL of the log-in page. |
||
1199 | * |
||
1200 | * To update the login url use the "Security.login_url" config setting. |
||
1201 | * |
||
1202 | * @return string |
||
1203 | */ |
||
1204 | public static function login_url() |
||
1208 | |||
1209 | |||
1210 | /** |
||
1211 | * Get the URL of the logout page. |
||
1212 | * |
||
1213 | * To update the logout url use the "Security.logout_url" config setting. |
||
1214 | * |
||
1215 | * @return string |
||
1216 | */ |
||
1217 | public static function logout_url() |
||
1221 | |||
1222 | /** |
||
1223 | * Get the URL of the logout page. |
||
1224 | * |
||
1225 | * To update the logout url use the "Security.logout_url" config setting. |
||
1226 | * |
||
1227 | * @return string |
||
1228 | */ |
||
1229 | public static function lost_password_url() |
||
1233 | |||
1234 | /** |
||
1235 | * Defines global accessible templates variables. |
||
1236 | * |
||
1237 | * @return array |
||
1238 | */ |
||
1239 | public static function get_template_global_variables() |
||
1249 | } |
||
1250 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: