| Conditions | 15 |
| Paths | 44 |
| Total Lines | 83 |
| Code Lines | 45 |
| 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 |
||
| 85 | public static function requireLogin( |
||
| 86 | HTTPRequest $request, |
||
| 87 | $realm, |
||
| 88 | $permissionCode = null, |
||
| 89 | $tryUsingSessionLogin = true |
||
| 90 | ) { |
||
| 91 | if ((Director::is_cli() && static::config()->get('ignore_cli'))) { |
||
| 92 | return true; |
||
| 93 | } |
||
| 94 | |||
| 95 | $member = null; |
||
| 96 | |||
| 97 | try { |
||
| 98 | if ($request->getHeader('PHP_AUTH_USER') && $request->getHeader('PHP_AUTH_PW')) { |
||
| 99 | /** @var MemberAuthenticator $authenticator */ |
||
| 100 | $authenticators = Security::singleton()->getApplicableAuthenticators(Authenticator::LOGIN); |
||
| 101 | |||
| 102 | foreach ($authenticators as $name => $authenticator) { |
||
| 103 | $member = $authenticator->authenticate([ |
||
| 104 | 'Email' => $request->getHeader('PHP_AUTH_USER'), |
||
| 105 | 'Password' => $request->getHeader('PHP_AUTH_PW'), |
||
| 106 | ], $request); |
||
| 107 | if ($member instanceof Member) { |
||
| 108 | break; |
||
| 109 | } |
||
| 110 | } |
||
| 111 | } |
||
| 112 | } catch (DatabaseException $e) { |
||
| 113 | // Database isn't ready, let people in |
||
| 114 | return true; |
||
| 115 | } |
||
| 116 | |||
| 117 | if (!$member && $tryUsingSessionLogin) { |
||
| 118 | $member = Security::getCurrentUser(); |
||
| 119 | } |
||
| 120 | |||
| 121 | // If we've failed the authentication mechanism, then show the login form |
||
| 122 | if (!$member) { |
||
| 123 | $response = new HTTPResponse(null, 401); |
||
| 124 | $response->addHeader('WWW-Authenticate', "Basic realm=\"$realm\""); |
||
| 125 | |||
| 126 | if ($request->getHeader('PHP_AUTH_USER')) { |
||
| 127 | $response->setBody( |
||
| 128 | _t( |
||
| 129 | 'SilverStripe\\Security\\BasicAuth.ERRORNOTREC', |
||
| 130 | "That username / password isn't recognised" |
||
| 131 | ) |
||
| 132 | ); |
||
| 133 | } else { |
||
| 134 | $response->setBody( |
||
| 135 | _t( |
||
| 136 | 'SilverStripe\\Security\\BasicAuth.ENTERINFO', |
||
| 137 | 'Please enter a username and password.' |
||
| 138 | ) |
||
| 139 | ); |
||
| 140 | } |
||
| 141 | |||
| 142 | // Exception is caught by RequestHandler->handleRequest() and will halt further execution |
||
| 143 | $e = new HTTPResponse_Exception(null, 401); |
||
| 144 | $e->setResponse($response); |
||
| 145 | throw $e; |
||
| 146 | } |
||
| 147 | |||
| 148 | if ($permissionCode && !Permission::checkMember($member->ID, $permissionCode)) { |
||
| 149 | $response = new HTTPResponse(null, 401); |
||
| 150 | $response->addHeader('WWW-Authenticate', "Basic realm=\"$realm\""); |
||
| 151 | |||
| 152 | if ($request->getHeader('PHP_AUTH_USER')) { |
||
| 153 | $response->setBody( |
||
| 154 | _t( |
||
| 155 | 'SilverStripe\\Security\\BasicAuth.ERRORNOTADMIN', |
||
| 156 | 'That user is not an administrator.' |
||
| 157 | ) |
||
| 158 | ); |
||
| 159 | } |
||
| 160 | |||
| 161 | // Exception is caught by RequestHandler->handleRequest() and will halt further execution |
||
| 162 | $e = new HTTPResponse_Exception(null, 401); |
||
| 163 | $e->setResponse($response); |
||
| 164 | throw $e; |
||
| 165 | } |
||
| 166 | |||
| 167 | return $member; |
||
| 168 | } |
||
| 243 |