mirror of
https://github.com/Icinga/icingaweb2.git
synced 2025-07-23 22:04:25 +02:00
Forms: New form system
Move new form parts arround and add new LoginForm. refs #4355
This commit is contained in:
parent
dc878de275
commit
d5b8a850ea
@ -27,40 +27,9 @@
|
|||||||
# namespace Icinga\Application\Controllers;
|
# namespace Icinga\Application\Controllers;
|
||||||
|
|
||||||
use Icinga\Web\ActionController;
|
use Icinga\Web\ActionController;
|
||||||
use Icinga\Authentication\Credentials as Credentials;
|
use Icinga\Authentication\Credentials;
|
||||||
use Icinga\Authentication\Manager as AuthManager;
|
use Icinga\Authentication\Manager as AuthManager;
|
||||||
use Icinga\Form\Form;
|
use Icinga\Form\Authentication\LoginForm;
|
||||||
|
|
||||||
|
|
||||||
// @TODO: I (jom) suppose this is not the best place, but
|
|
||||||
// finding a "bedder" one is your part mr. hein :)
|
|
||||||
class Auth_Form extends Form
|
|
||||||
{
|
|
||||||
public function create()
|
|
||||||
{
|
|
||||||
$this->addElement('text', 'username', array(
|
|
||||||
'label' => t('Username'),
|
|
||||||
'required' => true
|
|
||||||
)
|
|
||||||
);
|
|
||||||
$this->addElement('password', 'password', array(
|
|
||||||
'label' => t('Password'),
|
|
||||||
'required' => true
|
|
||||||
)
|
|
||||||
);
|
|
||||||
$this->addElement('submit', 'submit', array(
|
|
||||||
'label' => t('Login'),
|
|
||||||
'class' => 'pull-right'
|
|
||||||
)
|
|
||||||
);
|
|
||||||
$this->disableCsrfToken();
|
|
||||||
}
|
|
||||||
|
|
||||||
public function isSubmitted()
|
|
||||||
{
|
|
||||||
return parent::isSubmitted('submit');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -70,41 +39,45 @@ class Auth_Form extends Form
|
|||||||
class AuthenticationController extends ActionController
|
class AuthenticationController extends ActionController
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
|
* Flag indicates authentication handling
|
||||||
* @var bool
|
* @var bool
|
||||||
*/
|
*/
|
||||||
protected $handlesAuthentication = true;
|
protected $handlesAuthentication = true;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Flag indicates session modification
|
||||||
* @var bool
|
* @var bool
|
||||||
*/
|
*/
|
||||||
protected $modifiesSession = true;
|
protected $modifiesSession = true;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* Action to handle login
|
||||||
*/
|
*/
|
||||||
public function loginAction()
|
public function loginAction()
|
||||||
{
|
{
|
||||||
$this->replaceLayout = true;
|
$this->replaceLayout = true;
|
||||||
$credentials = new Credentials();
|
$credentials = new Credentials();
|
||||||
$this->view->form = new Auth_Form();
|
$this->view->form = new LoginForm();
|
||||||
$this->view->form->setRequest($this->_request);
|
$this->view->form->setRequest($this->_request);
|
||||||
$this->view->form->bindToModel($credentials);
|
|
||||||
try {
|
try {
|
||||||
$auth = AuthManager::getInstance(null, array(
|
$auth = AuthManager::getInstance(null, array(
|
||||||
"writeSession" => true
|
"writeSession" => true
|
||||||
));
|
));
|
||||||
|
|
||||||
if ($auth->isAuthenticated()) {
|
if ($auth->isAuthenticated()) {
|
||||||
$this->redirectNow('index?_render=body');
|
$this->redirectNow('index?_render=body');
|
||||||
}
|
}
|
||||||
if ($this->getRequest()->isPost() && $this->view->form->isSubmitted()) {
|
|
||||||
$this->view->form->repopulate();
|
if ($this->getRequest()->isPost() && $this->view->form->isValid($this->getRequest())) {
|
||||||
// @TODO: Re-enable this once the CSRF validation works
|
|
||||||
if (true) { //($this->view->form->isValid($this->getRequest())) {
|
$credentials->setUsername($this->view->form->getValue('username'));
|
||||||
if (!$auth->authenticate($credentials)) {
|
$credentials->setPassword($this->view->form->getValue('password'));
|
||||||
$this->view->form->getElement('password')->addError(t('Please provide a valid username and password'));
|
|
||||||
} else {
|
if (!$auth->authenticate($credentials)) {
|
||||||
$this->redirectNow('index?_render=body');
|
$this->view->form->getElement('password')->addError(t('Please provide a valid username and password'));
|
||||||
}
|
} else {
|
||||||
|
$this->redirectNow('index?_render=body');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (\Icinga\Exception\ConfigurationError $configError) {
|
} catch (\Icinga\Exception\ConfigurationError $configError) {
|
||||||
@ -113,7 +86,7 @@ class AuthenticationController extends ActionController
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* Action handle logout
|
||||||
*/
|
*/
|
||||||
public function logoutAction()
|
public function logoutAction()
|
||||||
{
|
{
|
||||||
|
66
application/forms/Authentication/LoginForm.php
Normal file
66
application/forms/Authentication/LoginForm.php
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
<?php
|
||||||
|
// {{{ICINGA_LICENSE_HEADER}}}
|
||||||
|
/**
|
||||||
|
* Icinga 2 Web - Head for multiple monitoring frontends
|
||||||
|
* Copyright (C) 2013 Icinga Development Team
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or
|
||||||
|
* modify it under the terms of the GNU General Public License
|
||||||
|
* as published by the Free Software Foundation; either version 2
|
||||||
|
* of the License, or (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||||
|
*
|
||||||
|
* @copyright 2013 Icinga Development Team <info@icinga.org>
|
||||||
|
* @author Icinga Development Team <info@icinga.org>
|
||||||
|
*/
|
||||||
|
// {{{ICINGA_LICENSE_HEADER}}}
|
||||||
|
|
||||||
|
namespace Icinga\Form\Authentication;
|
||||||
|
|
||||||
|
use Icinga\Web\Form;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class LoginForm
|
||||||
|
*/
|
||||||
|
class LoginForm extends Form
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Interface how the form should be created
|
||||||
|
*/
|
||||||
|
protected function create()
|
||||||
|
{
|
||||||
|
$this->addElement(
|
||||||
|
'text',
|
||||||
|
'username',
|
||||||
|
array(
|
||||||
|
'label' => t('Username'),
|
||||||
|
'required' => true
|
||||||
|
)
|
||||||
|
);
|
||||||
|
$this->addElement(
|
||||||
|
'password',
|
||||||
|
'password',
|
||||||
|
array(
|
||||||
|
'label' => t('Password'),
|
||||||
|
'required' => true
|
||||||
|
)
|
||||||
|
);
|
||||||
|
$this->addElement(
|
||||||
|
'submit',
|
||||||
|
'submit',
|
||||||
|
array(
|
||||||
|
'label' => t('Login'),
|
||||||
|
'class' => 'pull-right'
|
||||||
|
)
|
||||||
|
);
|
||||||
|
$this->disableCsrfToken();
|
||||||
|
}
|
||||||
|
}
|
@ -1,97 +0,0 @@
|
|||||||
<?php
|
|
||||||
// {{{ICINGA_LICENSE_HEADER}}}
|
|
||||||
/**
|
|
||||||
* Icinga 2 Web - Head for multiple monitoring frontends
|
|
||||||
* Copyright (C) 2013 Icinga Development Team
|
|
||||||
*
|
|
||||||
* This program is free software; you can redistribute it and/or
|
|
||||||
* modify it under the terms of the GNU General Public License
|
|
||||||
* as published by the Free Software Foundation; either version 2
|
|
||||||
* of the License, or (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with this program; if not, write to the Free Software
|
|
||||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
||||||
*
|
|
||||||
* @copyright 2013 Icinga Development Team <info@icinga.org>
|
|
||||||
* @author Icinga Development Team <info@icinga.org>
|
|
||||||
*/
|
|
||||||
// {{{ICINGA_LICENSE_HEADER}}}
|
|
||||||
|
|
||||||
namespace Icinga\Web\Form;
|
|
||||||
|
|
||||||
use Icinga\Web\Form;
|
|
||||||
use Icinga\Web\Notification;
|
|
||||||
use Icinga\Application\Config;
|
|
||||||
use Icinga\Authentication\Backend as AuthBackend;
|
|
||||||
use Icinga\Authentication\Auth;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Class LoginForm
|
|
||||||
* @package Icinga\Web\Form
|
|
||||||
*/
|
|
||||||
class LoginForm extends Form
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public function onSuccess()
|
|
||||||
{
|
|
||||||
$backend = new AuthBackend(Config::getInstance()->authentication);
|
|
||||||
$values = $this->getValues();
|
|
||||||
$username = $values['username'];
|
|
||||||
$password = $values['password'];
|
|
||||||
if ($backend->hasUsername($username)) {
|
|
||||||
if ($user = $backend->authenticate($username, $password)) {
|
|
||||||
// \Zend_Session::regenerateId();
|
|
||||||
Auth::getInstance()->setAuthenticatedUser($user);
|
|
||||||
Notification::success('Login succeeded');
|
|
||||||
$this->redirectNow('index?_render=body');
|
|
||||||
} else {
|
|
||||||
// TODO: Log "auth failed"
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// TODO: Log "User does not exist"
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->getElement('password')->addError(
|
|
||||||
t(
|
|
||||||
'Authentication failed, please check username and password'
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function elements()
|
|
||||||
{
|
|
||||||
return array(
|
|
||||||
'username' => array(
|
|
||||||
'text',
|
|
||||||
array(
|
|
||||||
'label' => t('Username'),
|
|
||||||
'required' => true,
|
|
||||||
)
|
|
||||||
),
|
|
||||||
'password' => array(
|
|
||||||
'password',
|
|
||||||
array(
|
|
||||||
'label' => t('Password'),
|
|
||||||
'required' => true,
|
|
||||||
)
|
|
||||||
),
|
|
||||||
'submit' => array(
|
|
||||||
'submit',
|
|
||||||
array(
|
|
||||||
'label' => t('Login')
|
|
||||||
)
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
@ -27,6 +27,11 @@ namespace Icinga\Web;
|
|||||||
|
|
||||||
use Icinga\Exception\ProgrammingError;
|
use Icinga\Exception\ProgrammingError;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class Form
|
||||||
|
*
|
||||||
|
* How forms are used in Icinga 2 Web
|
||||||
|
*/
|
||||||
abstract class Form extends \Zend_Form
|
abstract class Form extends \Zend_Form
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
@ -69,18 +74,49 @@ abstract class Form extends \Zend_Form
|
|||||||
/**
|
/**
|
||||||
* Add elements to this form (used by extending classes)
|
* Add elements to this form (used by extending classes)
|
||||||
*/
|
*/
|
||||||
abstract public function create();
|
abstract protected function create();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Apply a request object wherewith the form can work
|
* Setter for request
|
||||||
*
|
* @param \Zend_Controller_Request_Abstract $request The request object of a session
|
||||||
* @param $request The request object of a session
|
|
||||||
*/
|
*/
|
||||||
public function setRequest($request)
|
public function setRequest(\Zend_Controller_Request_Abstract $request)
|
||||||
{
|
{
|
||||||
$this->request = $request;
|
$this->request = $request;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Getter for request
|
||||||
|
* @return \Zend_Controller_Request_Abstract
|
||||||
|
*/
|
||||||
|
public function getRequest()
|
||||||
|
{
|
||||||
|
return $this->request;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test if data from array or request is valid
|
||||||
|
*
|
||||||
|
* If $data is null, internal request is selected to test validity
|
||||||
|
*
|
||||||
|
* @param null|\Zend_Controller_Request_Abstract|array $data
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function isValid($data)
|
||||||
|
{
|
||||||
|
$check = null;
|
||||||
|
|
||||||
|
if ($data === null) {
|
||||||
|
$data = $this->getRequest()->getParams();
|
||||||
|
} elseif ($data instanceof \Zend_Controller_Request_Abstract) {
|
||||||
|
$check = $data->getParams();
|
||||||
|
} else {
|
||||||
|
$check = $data;
|
||||||
|
}
|
||||||
|
|
||||||
|
return parent::isValid($check);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Enable CSRF counter measure
|
* Enable CSRF counter measure
|
||||||
*/
|
*/
|
||||||
@ -108,9 +144,12 @@ abstract class Form extends \Zend_Form
|
|||||||
}
|
}
|
||||||
list($seed, $token) = $this->generateCsrfToken($this->tokenTimeout);
|
list($seed, $token) = $this->generateCsrfToken($this->tokenTimeout);
|
||||||
|
|
||||||
$this->addElement('hidden', $this->tokenElementName, array(
|
$this->addElement(
|
||||||
'value' => sprintf('%s\|/%s', $seed, $token),
|
'hidden',
|
||||||
'decorators' => array('ViewHelper')
|
$this->tokenElementName,
|
||||||
|
array(
|
||||||
|
'value' => sprintf('%s\|/%s', $seed, $token),
|
||||||
|
'decorators' => array('ViewHelper')
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user