Merge pull request #7311 from sminnee/reduce-is-ready-usage

FIX: Removed unnecessary database_is_ready call.
This commit is contained in:
Damian Mooyman 2017-08-25 14:45:42 +12:00 committed by GitHub
commit 2a0805dda7
3 changed files with 30 additions and 24 deletions

View File

@ -7,6 +7,7 @@ use SilverStripe\Control\HTTPResponse;
use SilverStripe\Control\Middleware\HTTPMiddleware;
use SilverStripe\Core\Config\Configurable;
use SilverStripe\ORM\ValidationException;
use SilverStripe\ORM\Connect\DatabaseException;
class AuthenticationMiddleware implements HTTPMiddleware
{
@ -44,17 +45,17 @@ class AuthenticationMiddleware implements HTTPMiddleware
*/
public function process(HTTPRequest $request, callable $delegate)
{
if (Security::database_is_ready()) {
try {
$this
->getAuthenticationHandler()
->authenticateRequest($request);
} catch (ValidationException $e) {
return new HTTPResponse(
"Bad log-in details: " . $e->getMessage(),
400
);
}
try {
$this
->getAuthenticationHandler()
->authenticateRequest($request);
} catch (ValidationException $e) {
return new HTTPResponse(
"Bad log-in details: " . $e->getMessage(),
400
);
} catch (DatabaseException $e) {
// Database isn't ready, carry on.
}
return $delegate($request);

View File

@ -10,6 +10,7 @@ use SilverStripe\Control\HTTPResponse_Exception;
use SilverStripe\Core\Config\Configurable;
use SilverStripe\Dev\Debug;
use SilverStripe\Security\MemberAuthenticator\MemberAuthenticator;
use SilverStripe\ORM\Connect\DatabaseException;
/**
* Provides an interface to HTTP basic authentication.
@ -72,7 +73,7 @@ class BasicAuth
$permissionCode = null,
$tryUsingSessionLogin = true
) {
if (!Security::database_is_ready() || (Director::is_cli() && static::config()->get('ignore_cli'))) {
if ((Director::is_cli() && static::config()->get('ignore_cli'))) {
return true;
}
@ -94,19 +95,24 @@ class BasicAuth
$member = null;
if ($request->getHeader('PHP_AUTH_USER') && $request->getHeader('PHP_AUTH_PW')) {
/** @var MemberAuthenticator $authenticator */
$authenticators = Security::singleton()->getApplicableAuthenticators(Authenticator::LOGIN);
try {
if ($request->getHeader('PHP_AUTH_USER') && $request->getHeader('PHP_AUTH_PW')) {
/** @var MemberAuthenticator $authenticator */
$authenticators = Security::singleton()->getApplicableAuthenticators(Authenticator::LOGIN);
foreach ($authenticators as $name => $authenticator) {
$member = $authenticator->authenticate([
'Email' => $request->getHeader('PHP_AUTH_USER'),
'Password' => $request->getHeader('PHP_AUTH_PW'),
], $request);
if ($member instanceof Member) {
break;
foreach ($authenticators as $name => $authenticator) {
$member = $authenticator->authenticate([
'Email' => $request->getHeader('PHP_AUTH_USER'),
'Password' => $request->getHeader('PHP_AUTH_PW'),
], $request);
if ($member instanceof Member) {
break;
}
}
}
} catch (DatabaseException $e) {
// Database isn't ready, let people in
return true;
}
if ($member instanceof Member) {

View File

@ -107,8 +107,7 @@ class CookieAuthenticationHandler implements AuthenticationHandler
$uidAndToken = Cookie::get($this->getTokenCookieName());
$deviceID = Cookie::get($this->getDeviceCookieName());
// @todo Consider better placement of database_is_ready test
if ($deviceID === null || strpos($uidAndToken, ':') === false || !Security::database_is_ready()) {
if ($deviceID === null || strpos($uidAndToken, ':') === false) {
return null;
}