From 630b36e706b9b241554eb203f60d48ea55dfedc4 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Thu, 10 Jul 2014 11:15:46 +0200 Subject: [PATCH] Adjust Icinga\Form\Authentication\LoginForm to suit the new form builder refs #5525 --- .../controllers/AuthenticationController.php | 27 ++++---- .../forms/Authentication/LoginForm.php | 61 +++++++++++++------ 2 files changed, 55 insertions(+), 33 deletions(-) diff --git a/application/controllers/AuthenticationController.php b/application/controllers/AuthenticationController.php index cfbbafe4d..eaca4e97b 100644 --- a/application/controllers/AuthenticationController.php +++ b/application/controllers/AuthenticationController.php @@ -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(); diff --git a/application/forms/Authentication/LoginForm.php b/application/forms/Authentication/LoginForm.php index b9eb2f27d..c6e5b23da 100644 --- a/application/forms/Authentication/LoginForm.php +++ b/application/forms/Authentication/LoginForm.php @@ -1,4 +1,6 @@ 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; } }