parent
ae4b7144cd
commit
db78f810ff
|
@ -3,16 +3,24 @@
|
|||
|
||||
namespace Icinga\Forms\Authentication;
|
||||
|
||||
use Icinga\Authentication\Auth;
|
||||
use Icinga\Authentication\User\ExternalBackend;
|
||||
use Icinga\User;
|
||||
use Icinga\Web\Form;
|
||||
use Icinga\Web\Url;
|
||||
|
||||
/**
|
||||
* Class LoginForm
|
||||
* Form for user authentication
|
||||
*/
|
||||
class LoginForm extends Form
|
||||
{
|
||||
/**
|
||||
* Initialize this login form
|
||||
* Redirect URL
|
||||
*/
|
||||
const REDIRECT_URL = 'dashboard';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
|
@ -22,7 +30,7 @@ class LoginForm extends Form
|
|||
}
|
||||
|
||||
/**
|
||||
* @see Form::createElements()
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function createElements(array $formData)
|
||||
{
|
||||
|
@ -54,4 +62,86 @@ class LoginForm extends Form
|
|||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getRedirectUrl()
|
||||
{
|
||||
$redirect = null;
|
||||
if ($this->created) {
|
||||
$redirect = $this->getElement('redirect')->getValue();
|
||||
}
|
||||
if (empty($redirect)) {
|
||||
$redirect = static::REDIRECT_URL;
|
||||
}
|
||||
return Url::fromPath($redirect);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function onSuccess()
|
||||
{
|
||||
$auth = Auth::getInstance();
|
||||
$authChain = $auth->getAuthChain();
|
||||
$authChain->setIteratorMode($authChain::IT_MODE_NOT_EXTERNAL);
|
||||
$user = new User($this->getElement('username')->getValue());
|
||||
$password = $this->getElement('password')->getValue();
|
||||
$authenticated = $authChain->authenticate($user, $password);
|
||||
if ($authenticated) {
|
||||
$auth->setAuthenticated($user);
|
||||
return true;
|
||||
}
|
||||
switch ($authChain->getError()) {
|
||||
case $authChain::EEMPTY:
|
||||
$this->addError($this->translate(
|
||||
'No authentication methods available.'
|
||||
. ' Did you create authentication.ini when setting up Icinga Web 2?'
|
||||
));
|
||||
break;
|
||||
case $authChain::EFAIL:
|
||||
$this->addError($this->translate(
|
||||
'All configured authentication methods failed.'
|
||||
. ' Please check the system log or Icinga Web 2 log for more information.'
|
||||
));
|
||||
break;
|
||||
case $authChain::ENOTALL:
|
||||
$this->addError($this->translate(
|
||||
'Please note that not all authentication methods were available.'
|
||||
. ' Check the system log or Icinga Web 2 log for more information.'
|
||||
));
|
||||
// Move to default
|
||||
default:
|
||||
$this->getElement('password')->addError($this->translate('Incorrect username or password'));
|
||||
break;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function onRequest()
|
||||
{
|
||||
$auth = Auth::getInstance();
|
||||
$onlyExternal = true;
|
||||
$user = new User('');
|
||||
foreach ($auth->getAuthChain() as $backend) {
|
||||
if ($backend instanceof ExternalBackend) {
|
||||
if ($backend->authenticate($user)) {
|
||||
$auth->setAuthenticated($user);
|
||||
$this->getResponse()->redirectAndExit($this->getRedirectUrl());
|
||||
}
|
||||
} else {
|
||||
$onlyExternal = false;
|
||||
}
|
||||
}
|
||||
if ($onlyExternal) {
|
||||
$this->addError($this->translate(
|
||||
'You\'re currently not authenticated using any of the web server\'s authentication mechanisms.'
|
||||
. ' Make sure you\'ll configure such, otherwise you\'ll not be able to login.'
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue