| Conditions | 18 |
| Paths | 322 |
| Total Lines | 65 |
| Code Lines | 37 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 58 | public static function requireLogin($realm, $permissionCode = null, $tryUsingSessionLogin = true) { |
||
|
|
|||
| 59 | $isRunningTests = (class_exists('SapphireTest', false) && SapphireTest::is_running_test()); |
||
| 60 | if(!Security::database_is_ready() || (Director::is_cli() && !$isRunningTests)) return true; |
||
| 61 | |||
| 62 | /* |
||
| 63 | * Enable HTTP Basic authentication workaround for PHP running in CGI mode with Apache |
||
| 64 | * Depending on server configuration the auth header may be in HTTP_AUTHORIZATION or |
||
| 65 | * REDIRECT_HTTP_AUTHORIZATION |
||
| 66 | * |
||
| 67 | * The follow rewrite rule must be in the sites .htaccess file to enable this workaround |
||
| 68 | * RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] |
||
| 69 | */ |
||
| 70 | $authHeader = (isset($_SERVER['HTTP_AUTHORIZATION']) ? $_SERVER['HTTP_AUTHORIZATION'] : |
||
| 71 | (isset($_SERVER['REDIRECT_HTTP_AUTHORIZATION']) ? $_SERVER['REDIRECT_HTTP_AUTHORIZATION'] : null)); |
||
| 72 | $matches = array(); |
||
| 73 | if ($authHeader && |
||
| 74 | preg_match('/Basic\s+(.*)$/i', $authHeader, $matches)) { |
||
| 75 | list($name, $password) = explode(':', base64_decode($matches[1])); |
||
| 76 | $_SERVER['PHP_AUTH_USER'] = strip_tags($name); |
||
| 77 | $_SERVER['PHP_AUTH_PW'] = strip_tags($password); |
||
| 78 | } |
||
| 79 | |||
| 80 | $member = null; |
||
| 81 | if(isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW'])) { |
||
| 82 | $member = MemberAuthenticator::authenticate(array( |
||
| 83 | 'Email' => $_SERVER['PHP_AUTH_USER'], |
||
| 84 | 'Password' => $_SERVER['PHP_AUTH_PW'], |
||
| 85 | ), null); |
||
| 86 | } |
||
| 87 | |||
| 88 | if(!$member && $tryUsingSessionLogin) $member = Member::currentUser(); |
||
| 89 | |||
| 90 | // If we've failed the authentication mechanism, then show the login form |
||
| 91 | if(!$member) { |
||
| 92 | $response = new SS_HTTPResponse(null, 401); |
||
| 93 | $response->addHeader('WWW-Authenticate', "Basic realm=\"$realm\""); |
||
| 94 | |||
| 95 | if(isset($_SERVER['PHP_AUTH_USER'])) { |
||
| 96 | $response->setBody(_t('BasicAuth.ERRORNOTREC', "That username / password isn't recognised")); |
||
| 97 | } else { |
||
| 98 | $response->setBody(_t('BasicAuth.ENTERINFO', "Please enter a username and password.")); |
||
| 99 | } |
||
| 100 | |||
| 101 | // Exception is caught by RequestHandler->handleRequest() and will halt further execution |
||
| 102 | $e = new SS_HTTPResponse_Exception(null, 401); |
||
| 103 | $e->setResponse($response); |
||
| 104 | throw $e; |
||
| 105 | } |
||
| 106 | |||
| 107 | if($permissionCode && !Permission::checkMember($member->ID, $permissionCode)) { |
||
| 108 | $response = new SS_HTTPResponse(null, 401); |
||
| 109 | $response->addHeader('WWW-Authenticate', "Basic realm=\"$realm\""); |
||
| 110 | |||
| 111 | if(isset($_SERVER['PHP_AUTH_USER'])) { |
||
| 112 | $response->setBody(_t('BasicAuth.ERRORNOTADMIN', "That user is not an administrator.")); |
||
| 113 | } |
||
| 114 | |||
| 115 | // Exception is caught by RequestHandler->handleRequest() and will halt further execution |
||
| 116 | $e = new SS_HTTPResponse_Exception(null, 401); |
||
| 117 | $e->setResponse($response); |
||
| 118 | throw $e; |
||
| 119 | } |
||
| 120 | |||
| 121 | return $member; |
||
| 122 | } |
||
| 123 | |||
| 163 |
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: