2013-08-16 16:24:12 +02:00
|
|
|
<?php
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
/**
|
|
|
|
* This file is part of Icinga 2 Web.
|
2013-08-21 11:02:53 +02:00
|
|
|
*
|
2013-08-16 16:24:12 +02:00
|
|
|
* Icinga 2 Web - Head for multiple monitoring backends.
|
|
|
|
* Copyright (C) 2013 Icinga Development Team
|
2013-08-21 11:02:53 +02:00
|
|
|
*
|
2013-08-16 16:24:12 +02:00
|
|
|
* 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.
|
2013-08-21 11:02:53 +02:00
|
|
|
*
|
2013-08-16 16:24:12 +02:00
|
|
|
* 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.
|
2013-08-21 11:02:53 +02:00
|
|
|
*
|
2013-08-16 16:24:12 +02:00
|
|
|
* 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.
|
2013-08-21 11:02:53 +02:00
|
|
|
*
|
2013-08-16 16:24:12 +02:00
|
|
|
* @copyright 2013 Icinga Development Team <info@icinga.org>
|
|
|
|
* @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2
|
|
|
|
* @author Icinga Development Team <info@icinga.org>
|
|
|
|
*/
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
|
|
|
|
namespace Icinga\Form\Config\Authentication;
|
|
|
|
|
2013-08-21 11:02:53 +02:00
|
|
|
use \Zend_Config;
|
2013-08-26 17:23:31 +02:00
|
|
|
use \Icinga\Web\Form\Decorator\HelpText;
|
2013-08-16 16:24:12 +02:00
|
|
|
use \Icinga\Application\DbAdapterFactory;
|
|
|
|
use \Icinga\Web\Form;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Base form for authentication backend forms
|
|
|
|
*/
|
|
|
|
abstract class BaseBackendForm extends Form
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* The name of the backend currently displayed in this form
|
|
|
|
*
|
|
|
|
* Will be the section in the authentication.ini file
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2013-08-21 11:02:53 +02:00
|
|
|
private $backendName = '';
|
2013-08-16 16:24:12 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The backend configuration as a Zend_Config object
|
|
|
|
*
|
|
|
|
* @var Zend_Config
|
|
|
|
*/
|
2013-08-21 11:02:53 +02:00
|
|
|
private $backend;
|
2013-08-16 16:24:12 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The resources to use instead of the factory provided ones (use for testing)
|
|
|
|
*
|
|
|
|
* @var Zend_Config
|
|
|
|
*/
|
2013-08-21 11:02:53 +02:00
|
|
|
private $resources;
|
2013-08-16 16:24:12 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the name of the currently displayed backend
|
|
|
|
*
|
2013-08-21 11:02:53 +02:00
|
|
|
* @param string $name The name to be stored as the section when persisting
|
2013-08-16 16:24:12 +02:00
|
|
|
*/
|
|
|
|
public function setBackendName($name)
|
|
|
|
{
|
|
|
|
$this->backendName = $name;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the backend name of this form
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getBackendName()
|
|
|
|
{
|
|
|
|
return $this->backendName;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the backend configuration or a empty Zend_Config object if none is given
|
|
|
|
*
|
|
|
|
* @return Zend_Config
|
|
|
|
*/
|
|
|
|
public function getBackend()
|
|
|
|
{
|
|
|
|
return ($this->backend !== null) ? $this->backend : new Zend_Config(array());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the backend configuration for initial population
|
|
|
|
*
|
2013-08-21 11:02:53 +02:00
|
|
|
* @param Zend_Config $backend The backend to display in this form
|
2013-08-16 16:24:12 +02:00
|
|
|
*/
|
|
|
|
public function setBackend(Zend_Config $backend)
|
|
|
|
{
|
|
|
|
$this->backend = $backend;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set an alternative array of resources that should be used instead of the DBFactory resource set
|
|
|
|
* (used for testing)
|
|
|
|
*
|
2013-08-21 11:02:53 +02:00
|
|
|
* @param array $resources The resources to use for populating the db selection field
|
2013-08-16 16:24:12 +02:00
|
|
|
*/
|
|
|
|
public function setResources(array $resources)
|
|
|
|
{
|
|
|
|
$this->resources = $resources;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return content of the resources.ini or previously set resources for displaying in the database selection field
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getResources()
|
|
|
|
{
|
|
|
|
if ($this->resources === null) {
|
|
|
|
return DbAdapterFactory::getResources();
|
|
|
|
} else {
|
|
|
|
return $this->resources;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-26 16:56:23 +02:00
|
|
|
/**
|
2013-08-27 14:37:22 +02:00
|
|
|
* Add checkbox at the beginning of the form which allows to skip logic connection validation
|
2013-08-26 16:56:23 +02:00
|
|
|
*/
|
2013-08-27 14:37:22 +02:00
|
|
|
private function addForceCreationCheckbox()
|
|
|
|
{
|
2013-08-26 16:56:23 +02:00
|
|
|
$checkbox = new \Zend_Form_Element_Checkbox(
|
|
|
|
array(
|
|
|
|
'name' => 'backend_force_creation',
|
|
|
|
'label' => 'Force Changes',
|
|
|
|
'helptext' => 'Check this box to enforce changes without connectivity validation',
|
|
|
|
'order' => 0
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$checkbox->addDecorator(new HelpText());
|
|
|
|
$this->addElement($checkbox);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-08-27 14:37:22 +02:00
|
|
|
* Validate this form with the Zend validation mechanism and perform a logic validation of the connection.
|
2013-08-26 16:56:23 +02:00
|
|
|
*
|
|
|
|
* If logic validation fails, the 'backend_force_creation' checkbox is prepended to the form to allow users to
|
|
|
|
* skip the logic connection validation.
|
|
|
|
*
|
|
|
|
* @param array $data The form input to validate
|
|
|
|
*
|
|
|
|
* @return bool True when validation succeeded, false if not
|
|
|
|
*/
|
|
|
|
public function isValid($data)
|
|
|
|
{
|
|
|
|
if (!parent::isValid($data)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ($this->getRequest()->getPost('backend_force_creation')) {
|
|
|
|
return true;
|
|
|
|
}
|
2013-08-27 14:37:22 +02:00
|
|
|
if (!$this->isValidAuthenticationBackend()) {
|
2013-08-26 16:56:23 +02:00
|
|
|
$this->addForceCreationCheckbox();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-08-16 16:24:12 +02:00
|
|
|
/**
|
|
|
|
* Return an array containing all sections defined by this form as the key and all settings
|
2013-08-21 11:02:53 +02:00
|
|
|
* as an key-value sub-array
|
2013-08-16 16:24:12 +02:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
abstract public function getConfig();
|
2013-08-26 16:56:23 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate the configuration state of this backend with the concrete authentication backend.
|
|
|
|
*
|
|
|
|
* An implementation should not throw any exception, but use the add/setErrorMessages method of
|
|
|
|
* Zend_Form. If the 'backend_force_creation' checkbox is set, this method won't be called.
|
|
|
|
*
|
|
|
|
* @return bool True when validation succeeded, otherwise false
|
|
|
|
*/
|
2013-08-27 14:37:22 +02:00
|
|
|
abstract public function isValidAuthenticationBackend();
|
2013-08-16 16:24:12 +02:00
|
|
|
}
|