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(); $auth = $this->Auth();
$this->view->form = new LoginForm(); $this->view->form = new LoginForm();
$this->view->form->setRequest($this->_request);
$this->view->title = $this->translate('Icingaweb Login'); $this->view->title = $this->translate('Icingaweb Login');
try { try {
@ -85,18 +84,7 @@ class AuthenticationController extends ActionController
} }
$chain = new AuthChain($config); $chain = new AuthChain($config);
if ($this->getRequest()->isGet()) { if ($this->view->form->isSubmittedAndValid($this->_request->getParams())) {
$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()) {
$user = new User($this->view->form->getValue('username')); $user = new User($this->view->form->getValue('username'));
$password = $this->view->form->getValue('password'); $password = $this->view->form->getValue('password');
$backendsTried = 0; $backendsTried = 0;
@ -136,7 +124,7 @@ class AuthenticationController extends ActionController
); );
} }
if ($backendsWithError) { if ($backendsWithError) {
$this->view->form->addNote( $this->view->form->getElement('username')->addError(
$this->translate( $this->translate(
'Note that not all authentication backends are available for authentication because they' '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' . ' 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')); $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) { } catch (Exception $e) {
$this->view->errorInfo = $e->getMessage(); $this->view->errorInfo = $e->getMessage();

View File

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