| Conditions | 13 |
| Paths | 96 |
| Total Lines | 59 |
| Code Lines | 32 |
| 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 |
||
| 104 | protected function triggerBasicAuthProtection() |
||
| 105 | { |
||
| 106 | $allowWithoutAuth = false; |
||
| 107 | |||
| 108 | // Allow whitelisting IPs for bypassing the basic auth. |
||
| 109 | if (Environment::getEnv('CWP_IP_BYPASS_BASICAUTH')) { |
||
| 110 | $remote = $_SERVER['REMOTE_ADDR']; |
||
| 111 | $bypass = explode(',', Environment::getEnv('CWP_IP_BYPASS_BASICAUTH')); |
||
| 112 | |||
| 113 | if (in_array($remote, $bypass)) { |
||
| 114 | $allowWithoutAuth = true; |
||
| 115 | } |
||
| 116 | } |
||
| 117 | |||
| 118 | /** @var HTTPRequest|null $request */ |
||
| 119 | $request = $this->getRequest(); |
||
| 120 | |||
| 121 | // First, see if we can get a member to act on, either from a changepassword token or the session |
||
| 122 | if (isset($_REQUEST['m']) && isset($_REQUEST['t'])) { |
||
| 123 | /** @var Member $member */ |
||
| 124 | $member = Member::get()->filter('ID', (int) $_REQUEST['m'])->first(); |
||
| 125 | |||
| 126 | if (!$member->validateAutoLoginToken($_REQUEST['t'])) { |
||
| 127 | $member = null; |
||
| 128 | } |
||
| 129 | } elseif ($request && $request->getSession()->get('AutoLoginHash')) { |
||
| 130 | $member = Member::member_from_autologinhash( |
||
| 131 | $request->getSession()->get('AutoLoginHash') |
||
| 132 | ); |
||
| 133 | } else { |
||
| 134 | $member = Security::getCurrentUser(); |
||
| 135 | } |
||
| 136 | |||
| 137 | // Then, if they have the right permissions, check the allowed URLs |
||
| 138 | $existingMemberCanAccessUAT = $member && $this->callWithSubsitesDisabled(function () use ($member) { |
||
| 139 | return Permission::checkMember($member, 'ACCESS_UAT_SERVER'); |
||
| 140 | }); |
||
| 141 | |||
| 142 | if ($existingMemberCanAccessUAT) { |
||
| 143 | $allowed = [ |
||
| 144 | '/^Security\/changepassword/', |
||
| 145 | '/^Security\/ChangePasswordForm/', |
||
| 146 | ]; |
||
| 147 | |||
| 148 | $relativeURL = Director::makeRelative(Director::absoluteURL($_SERVER['REQUEST_URI'])); |
||
| 149 | |||
| 150 | foreach ($allowed as $pattern) { |
||
| 151 | $allowWithoutAuth = $allowWithoutAuth || preg_match($pattern, $relativeURL); |
||
| 152 | } |
||
| 153 | } |
||
| 154 | |||
| 155 | // Finally if they weren't allowed to bypass Basic Auth, trigger it |
||
| 156 | if (!$allowWithoutAuth) { |
||
| 157 | $this->callWithSubsitesDisabled(function () use ($request) { |
||
| 158 | BasicAuth::requireLogin( |
||
| 159 | $request, |
||
| 160 | _t(__CLASS__ . '.LoginPrompt', "Please log in with your CMS credentials"), |
||
| 161 | 'ACCESS_UAT_SERVER', |
||
| 162 | true |
||
| 163 | ); |
||
| 231 |