Adjust Icinga\Form\Authentication\LoginForm to suit the new form builder

refs #5525
This commit is contained in:
Johannes Meyer 2014-07-10 11:15:46 +02:00
parent 0dce204651
commit 630b36e706
2 changed files with 55 additions and 33 deletions

View File

@ -60,7 +60,6 @@ class AuthenticationController extends ActionController
{
$auth = $this->Auth();
$this->view->form = new LoginForm();
$this->view->form->setRequest($this->_request);
$this->view->title = $this->translate('Icingaweb Login');
try {
@ -85,18 +84,7 @@ class AuthenticationController extends ActionController
}
$chain = new AuthChain($config);
if ($this->getRequest()->isGet()) {
$user = new User('');
foreach ($chain as $backend) {
if ($backend instanceof AutoLoginBackend) {
$authenticated = $backend->authenticate($user);
if ($authenticated === true) {
$auth->setAuthenticated($user);
$this->rerenderLayout()->redirectNow($redirectUrl);
}
}
}
} elseif ($this->view->form->isSubmittedAndValid()) {
if ($this->view->form->isSubmittedAndValid($this->_request->getParams())) {
$user = new User($this->view->form->getValue('username'));
$password = $this->view->form->getValue('password');
$backendsTried = 0;
@ -136,7 +124,7 @@ class AuthenticationController extends ActionController
);
}
if ($backendsWithError) {
$this->view->form->addNote(
$this->view->form->getElement('username')->addError(
$this->translate(
'Note that not all authentication backends are available for authentication because they'
. ' have errors. Please check the system log or Icinga Web 2 log for more information'
@ -144,6 +132,17 @@ class AuthenticationController extends ActionController
);
}
$this->view->form->getElement('password')->addError($this->translate('Incorrect username or password'));
} elseif (false === $this->view->form->isSubmitted()) {
$user = new User('');
foreach ($chain as $backend) {
if ($backend instanceof AutoLoginBackend) {
$authenticated = $backend->authenticate($user);
if ($authenticated === true) {
$auth->setAuthenticated($user);
$this->rerenderLayout()->redirectNow($redirectUrl);
}
}
}
}
} catch (Exception $e) {
$this->view->errorInfo = $e->getMessage();

View File

@ -1,4 +1,6 @@
<?php
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
namespace Icinga\Form\Authentication;
@ -10,34 +12,55 @@ use Icinga\Web\Form;
class LoginForm extends Form
{
/**
* Disable CSRF protection
* @var bool
* Initialize this login form
*/
protected $tokenDisabled = true;
public function init()
{
$this->setTokenDisabled();
$this->setName('form_login');
$this->setSubmitLabel('Login');
}
/**
* Interface how the form should be created
* @see Form::createElements()
*/
protected function create()
public function createElements()
{
$this->setName('form_login');
$this->addElement('text', 'username', array(
'label' => t('Username'),
'placeholder' => t('Please enter your username...'),
'required' => true,
));
return array(
$this->createElement(
'text',
'username',
array(
'label' => t('Username'),
'placeholder' => t('Please enter your username...'),
'required' => true
)
),
$this->createElement(
'password',
'password',
array(
'label' => t('Password'),
'placeholder' => t('...and your password'),
'required' => true
)
)
);
}
$this->addElement('password', 'password', array(
'label' => t('Password'),
'placeholder' => t('...and your password'),
'required' => true
));
// TODO: We need a place to intercept filled forms before rendering
if (isset($_POST['username'])) {
/**
* @see Form::applyValues()
*/
public function applyValues(array $values)
{
parent::applyValues($values);
if (isset($values['username'])) {
$this->getElement('password')->setAttrib('class', 'autofocus');
} else {
$this->getElement('username')->setAttrib('class', 'autofocus');
}
$this->setSubmitLabel('Login');
return $this;
}
}