chg: [AppController] move loading and initialisation of Auth plugins to reuseable method

For some authentication workflows it might be desireable to execute the
exact same code without having to call the entire beforeFilter method
from the base class. That way you do not have to work around all the
edge cases without having to reinvent the same code in multiple
locations.
pull/5187/head
Andreas Rammhold 2019-05-14 16:27:41 +02:00 committed by Chris Halls
parent 9edffd01ba
commit 3cbc36af4e
1 changed files with 19 additions and 15 deletions

View File

@ -287,21 +287,7 @@ class AppController extends Controller
throw new ForbiddenException('Authentication failed. Please make sure you pass the API key of an API enabled user along in the Authorization header.'); throw new ForbiddenException('Authentication failed. Please make sure you pass the API key of an API enabled user along in the Authorization header.');
} }
} elseif (!$this->Session->read(AuthComponent::$sessionKey)) { } elseif (!$this->Session->read(AuthComponent::$sessionKey)) {
// load authentication plugins from Configure::read('Security.auth') $this->_loadAuthenticationPlugins();
$auth = Configure::read('Security.auth');
if ($auth) {
$this->Auth->authenticate = array_merge($auth, $this->Auth->authenticate);
if ($this->Auth->startup($this)) {
$user = $this->Auth->user();
if ($user) {
// User found in the db, add the user info to the session
$this->Session->renew();
$this->Session->write(AuthComponent::$sessionKey, $user);
}
unset($user);
}
}
unset($auth);
} }
} }
$this->set('externalAuthUser', $userLoggedIn); $this->set('externalAuthUser', $userLoggedIn);
@ -1033,4 +1019,22 @@ class AppController extends Controller
$targetRoute['admin'] = false; $targetRoute['admin'] = false;
$this->redirect($targetRoute); $this->redirect($targetRoute);
} }
protected function _loadAuthenticationPlugins() {
// load authentication plugins from Configure::read('Security.auth')
$auth = Configure::read('Security.auth');
if (!$auth) return;
$this->Auth->authenticate = array_merge($auth, $this->Auth->authenticate);
if ($this->Auth->startup($this)) {
$user = $this->Auth->user();
if ($user) {
// User found in the db, add the user info to the session
$this->Session->renew();
$this->Session->write(AuthComponent::$sessionKey, $user);
}
}
}
} }