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 | * Perform pre-login checking and prepare a response if available prior to login |
||
487 | * |
||
488 | * @return HTTPResponse Substitute response object if the login process should be curcumvented. |
||
489 | * Returns null if should proceed as normal. |
||
490 | */ |
||
491 | protected function preLogin() |
||
524 | |||
525 | /** |
||
526 | * Prepare the controller for handling the response to this request |
||
527 | * |
||
528 | * @param string $title Title to use |
||
529 | * @return Controller |
||
530 | */ |
||
531 | protected function getResponseController($title) |
||
554 | |||
555 | /** |
||
556 | * Combine the given forms into a formset with a tabbed interface |
||
557 | * |
||
558 | * @param array|Form[] $forms |
||
559 | * @return string |
||
560 | */ |
||
561 | protected function generateTabbedFormSet($forms) |
||
575 | |||
576 | /** |
||
577 | * Get the HTML Content for the $Content area during login |
||
578 | * |
||
579 | * @param string &$messageType Type of message, if available, passed back to caller |
||
580 | * @return string Message in HTML format |
||
581 | */ |
||
582 | protected function getSessionMessage(&$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 function setSessionMessage( |
||
615 | |||
616 | /** |
||
617 | * Clear login message |
||
618 | */ |
||
619 | public static function clearSessionMessage() |
||
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 | * @param null|HTTPRequest $request |
||
632 | * @param int $service |
||
633 | * @return HTTPResponse|string Returns the "login" page as HTML code. |
||
634 | */ |
||
635 | public function login($request = null, $service = Authenticator::LOGIN) |
||
664 | |||
665 | /** |
||
666 | * Log the currently logged in user out |
||
667 | * |
||
668 | * Logging out without ID-parameter in the URL, will log the user out of all applicable Authenticators. |
||
669 | * |
||
670 | * Adding an ID will only log the user out of that Authentication method. |
||
671 | * |
||
672 | * @param null|HTTPRequest $request |
||
673 | * @param int $service |
||
674 | * @return HTTPResponse|string |
||
675 | */ |
||
676 | public function logout($request = null, $service = Authenticator::LOGOUT) |
||
701 | |||
702 | /** |
||
703 | * Get authenticators for the given service, optionally filtered by the ID parameter |
||
704 | * of the current request |
||
705 | * |
||
706 | * @param int $service |
||
707 | * @param HTTPRequest $request |
||
708 | * @throws HTTPResponse_Exception |
||
709 | */ |
||
710 | protected function getServiceAuthenticatorsFromRequest($service, HTTPRequest $request) |
||
748 | |||
749 | /** |
||
750 | * Aggregate tabbed forms from each handler to fragments ready to be rendered. |
||
751 | * |
||
752 | * @param array $results |
||
753 | * @return array |
||
754 | */ |
||
755 | protected function aggregateTabbedForms(array $results) |
||
777 | |||
778 | /** |
||
779 | * Delegate to a number of handlers and aggregate the results. This is used, for example, to |
||
780 | * build the log-in page where there are multiple authenticators active. |
||
781 | * |
||
782 | * If a single handler is passed, delegateToHandler() will be called instead |
||
783 | * |
||
784 | * @param array|RequestHandler[] $handlers |
||
785 | * @param string $title The title of the form |
||
786 | * @param array $templates |
||
787 | * @param callable $aggregator |
||
788 | * @return array|HTTPResponse|RequestHandler|DBHTMLText|string |
||
789 | */ |
||
790 | protected function delegateToMultipleHandlers(array $handlers, $title, array $templates, callable $aggregator) |
||
809 | |||
810 | /** |
||
811 | * Delegate to another RequestHandler, rendering any fragment arrays into an appropriate. |
||
812 | * controller. |
||
813 | * |
||
814 | * @param RequestHandler $handler |
||
815 | * @param string $title The title of the form |
||
816 | * @param array $templates |
||
817 | * @return array|HTTPResponse|RequestHandler|DBHTMLText|string |
||
818 | */ |
||
819 | protected function delegateToHandler(RequestHandler $handler, $title, array $templates = []) |
||
830 | |||
831 | /** |
||
832 | * Render the given fragments into a security page controller with the given title. |
||
833 | * |
||
834 | * @param string $title string The title to give the security page |
||
835 | * @param array $fragments A map of objects to render into the page, e.g. "Form" |
||
836 | * @param array $templates An array of templates to use for the render |
||
837 | * @return HTTPResponse|DBHTMLText |
||
838 | */ |
||
839 | protected function renderWrappedController($title, array $fragments, array $templates) |
||
866 | |||
867 | public function basicauthlogin() |
||
872 | |||
873 | /** |
||
874 | * Show the "lost password" page |
||
875 | * |
||
876 | * @return string Returns the "lost password" page as HTML code. |
||
877 | */ |
||
878 | public function lostpassword() |
||
896 | |||
897 | /** |
||
898 | * Show the "change password" page. |
||
899 | * This page can either be called directly by logged-in users |
||
900 | * (in which case they need to provide their old password), |
||
901 | * or through a link emailed through {@link lostpassword()}. |
||
902 | * In this case no old password is required, authentication is ensured |
||
903 | * through the Member.AutoLoginHash property. |
||
904 | * |
||
905 | * @see ChangePasswordForm |
||
906 | * |
||
907 | * @return string|HTTPRequest Returns the "change password" page as HTML code, or a redirect response |
||
908 | */ |
||
909 | public function changepassword() |
||
925 | |||
926 | /** |
||
927 | * Create a link to the password reset form. |
||
928 | * |
||
929 | * GET parameters used: |
||
930 | * - m: member ID |
||
931 | * - t: plaintext token |
||
932 | * |
||
933 | * @param Member $member Member object associated with this link. |
||
934 | * @param string $autologinToken The auto login token. |
||
935 | * @return string |
||
936 | */ |
||
937 | public static function getPasswordResetLink($member, $autologinToken) |
||
943 | |||
944 | /** |
||
945 | * Determine the list of templates to use for rendering the given action. |
||
946 | * |
||
947 | * @skipUpgrade |
||
948 | * @param string $action |
||
949 | * @return array Template list |
||
950 | */ |
||
951 | public function getTemplatesFor($action) |
||
965 | |||
966 | /** |
||
967 | * Return an existing member with administrator privileges, or create one of necessary. |
||
968 | * |
||
969 | * Will create a default 'Administrators' group if no group is found |
||
970 | * with an ADMIN permission. Will create a new 'Admin' member with administrative permissions |
||
971 | * if no existing Member with these permissions is found. |
||
972 | * |
||
973 | * Important: Any newly created administrator accounts will NOT have valid |
||
974 | * login credentials (Email/Password properties), which means they can't be used for login |
||
975 | * purposes outside of any default credentials set through {@link Security::setDefaultAdmin()}. |
||
976 | * |
||
977 | * @return Member |
||
978 | * |
||
979 | * @deprecated 4.0.0..5.0.0 Please use DefaultAdminService::findOrCreateDefaultAdmin() |
||
980 | */ |
||
981 | public static function findAnAdministrator() |
||
988 | |||
989 | /** |
||
990 | * Flush the default admin credentials |
||
991 | * |
||
992 | * @deprecated 4.0.0..5.0.0 Please use DefaultAdminService::clearDefaultAdmin() |
||
993 | */ |
||
994 | public static function clear_default_admin() |
||
1000 | |||
1001 | |||
1002 | /** |
||
1003 | * Set a default admin in dev-mode |
||
1004 | * |
||
1005 | * This will set a static default-admin which is not existing |
||
1006 | * as a database-record. By this workaround we can test pages in dev-mode |
||
1007 | * with a unified login. Submitted login-credentials are first checked |
||
1008 | * against this static information in {@link Security::authenticate()}. |
||
1009 | * |
||
1010 | * @param string $username The user name |
||
1011 | * @param string $password The password (in cleartext) |
||
1012 | * @return bool True if successfully set |
||
1013 | * |
||
1014 | * @deprecated 4.0.0..5.0.0 Please use DefaultAdminService::setDefaultAdmin($username, $password) |
||
1015 | */ |
||
1016 | public static function setDefaultAdmin($username, $password) |
||
1023 | |||
1024 | /** |
||
1025 | * Checks if the passed credentials are matching the default-admin. |
||
1026 | * Compares cleartext-password set through Security::setDefaultAdmin(). |
||
1027 | * |
||
1028 | * @param string $username |
||
1029 | * @param string $password |
||
1030 | * @return bool |
||
1031 | * |
||
1032 | * @deprecated 4.0.0..5.0.0 Use DefaultAdminService::isDefaultAdminCredentials() instead |
||
1033 | */ |
||
1034 | public static function check_default_admin($username, $password) |
||
1041 | |||
1042 | /** |
||
1043 | * Check that the default admin account has been set. |
||
1044 | * |
||
1045 | * @deprecated 4.0.0..5.0.0 Use DefaultAdminService::hasDefaultAdmin() instead |
||
1046 | */ |
||
1047 | public static function has_default_admin() |
||
1053 | |||
1054 | /** |
||
1055 | * Get default admin username |
||
1056 | * |
||
1057 | * @deprecated 4.0.0..5.0.0 Use DefaultAdminService::getDefaultAdminUsername() |
||
1058 | * @return string |
||
1059 | */ |
||
1060 | public static function default_admin_username() |
||
1066 | |||
1067 | /** |
||
1068 | * Get default admin password |
||
1069 | * |
||
1070 | * @deprecated 4.0.0..5.0.0 Use DefaultAdminService::getDefaultAdminPassword() |
||
1071 | * @return string |
||
1072 | */ |
||
1073 | public static function default_admin_password() |
||
1079 | |||
1080 | /** |
||
1081 | * Encrypt a password according to the current password encryption settings. |
||
1082 | * If the settings are so that passwords shouldn't be encrypted, the |
||
1083 | * result is simple the clear text password with an empty salt except when |
||
1084 | * a custom algorithm ($algorithm parameter) was passed. |
||
1085 | * |
||
1086 | * @param string $password The password to encrypt |
||
1087 | * @param string $salt Optional: The salt to use. If it is not passed, but |
||
1088 | * needed, the method will automatically create a |
||
1089 | * random salt that will then be returned as return value. |
||
1090 | * @param string $algorithm Optional: Use another algorithm to encrypt the |
||
1091 | * password (so that the encryption algorithm can be changed over the time). |
||
1092 | * @param Member $member Optional |
||
1093 | * @return mixed Returns an associative array containing the encrypted |
||
1094 | * password and the used salt in the form: |
||
1095 | * <code> |
||
1096 | * array( |
||
1097 | * 'password' => string, |
||
1098 | * 'salt' => string, |
||
1099 | * 'algorithm' => string, |
||
1100 | * 'encryptor' => PasswordEncryptor instance |
||
1101 | * ) |
||
1102 | * </code> |
||
1103 | * If the passed algorithm is invalid, FALSE will be returned. |
||
1104 | * |
||
1105 | * @see encrypt_passwords() |
||
1106 | */ |
||
1107 | public static function encrypt_password($password, $salt = null, $algorithm = null, $member = null) |
||
1126 | |||
1127 | /** |
||
1128 | * Checks the database is in a state to perform security checks. |
||
1129 | * See {@link DatabaseAdmin->init()} for more information. |
||
1130 | * |
||
1131 | * @return bool |
||
1132 | */ |
||
1133 | public static function database_is_ready() |
||
1181 | |||
1182 | /** |
||
1183 | * Resets the database_is_ready cache |
||
1184 | */ |
||
1185 | public static function clear_database_is_ready() |
||
1190 | |||
1191 | /** |
||
1192 | * For the database_is_ready call to return a certain value - used for testing |
||
1193 | * |
||
1194 | * @param bool $isReady |
||
1195 | */ |
||
1196 | public static function force_database_is_ready($isReady) |
||
1200 | |||
1201 | /** |
||
1202 | * @config |
||
1203 | * @var string Set the default login dest |
||
1204 | * This is the URL that users will be redirected to after they log in, |
||
1205 | * if they haven't logged in en route to access a secured page. |
||
1206 | * By default, this is set to the homepage. |
||
1207 | */ |
||
1208 | private static $default_login_dest = ""; |
||
1209 | |||
1210 | protected static $ignore_disallowed_actions = false; |
||
1211 | |||
1212 | /** |
||
1213 | * Set to true to ignore access to disallowed actions, rather than returning permission failure |
||
1214 | * Note that this is just a flag that other code needs to check with Security::ignore_disallowed_actions() |
||
1215 | * @param bool $flag True or false |
||
1216 | */ |
||
1217 | public static function set_ignore_disallowed_actions($flag) |
||
1221 | |||
1222 | public static function ignore_disallowed_actions() |
||
1226 | |||
1227 | /** |
||
1228 | * Get the URL of the log-in page. |
||
1229 | * |
||
1230 | * To update the login url use the "Security.login_url" config setting. |
||
1231 | * |
||
1232 | * @return string |
||
1233 | */ |
||
1234 | public static function login_url() |
||
1238 | |||
1239 | |||
1240 | /** |
||
1241 | * Get the URL of the logout page. |
||
1242 | * |
||
1243 | * To update the logout url use the "Security.logout_url" config setting. |
||
1244 | * |
||
1245 | * @return string |
||
1246 | */ |
||
1247 | public static function logout_url() |
||
1252 | |||
1253 | /** |
||
1254 | * Get the URL of the logout page. |
||
1255 | * |
||
1256 | * To update the logout url use the "Security.logout_url" config setting. |
||
1257 | * |
||
1258 | * @return string |
||
1259 | */ |
||
1260 | public static function lost_password_url() |
||
1264 | |||
1265 | /** |
||
1266 | * Defines global accessible templates variables. |
||
1267 | * |
||
1268 | * @return array |
||
1269 | */ |
||
1270 | public static function get_template_global_variables() |
||
1280 | } |
||
1281 |
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: