2013-08-16 16:24:12 +02:00
|
|
|
<?php
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
/**
|
2013-10-23 15:10:33 +02:00
|
|
|
* This file is part of Icinga Web 2.
|
2013-08-21 11:02:53 +02:00
|
|
|
*
|
2013-10-23 15:10:33 +02:00
|
|
|
* Icinga Web 2 - Head for multiple monitoring backends.
|
2013-08-16 16:24:12 +02:00
|
|
|
* 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-10-23 15:10:33 +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>
|
|
|
|
*
|
2013-08-16 16:24:12 +02:00
|
|
|
*/
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
|
|
|
|
namespace Icinga\Form\Config\Authentication;
|
|
|
|
|
2013-08-26 17:23:31 +02:00
|
|
|
use \Icinga\Authentication\Backend\DbUserBackend;
|
2013-08-30 16:28:13 +02:00
|
|
|
use \Zend_Config;
|
2013-08-16 16:24:12 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Form class for adding/modifying database authentication backends
|
|
|
|
*/
|
|
|
|
class DbBackendForm extends BaseBackendForm
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Return a list of all database resource ready to be used as the multiOptions
|
|
|
|
* attribute in a Zend_Form_Element_Select object
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function getDatabaseResources()
|
|
|
|
{
|
|
|
|
$backends = array();
|
|
|
|
foreach ($this->getResources() as $resname => $resource) {
|
|
|
|
if ($resource['type'] !== 'db') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$backends[$resname] = $resname;
|
|
|
|
}
|
|
|
|
return $backends;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-08-21 11:02:53 +02:00
|
|
|
* Create this form and add all required elements
|
|
|
|
*
|
|
|
|
* @see Form::create()
|
|
|
|
*/
|
2013-08-16 16:24:12 +02:00
|
|
|
public function create()
|
|
|
|
{
|
2013-08-28 10:12:27 +02:00
|
|
|
$this->setName('form_modify_backend');
|
2013-08-16 16:24:12 +02:00
|
|
|
$name = $this->filterName($this->getBackendName());
|
|
|
|
|
|
|
|
$this->addElement(
|
|
|
|
'text',
|
|
|
|
'backend_' . $name . '_name',
|
|
|
|
array(
|
2013-08-21 11:02:53 +02:00
|
|
|
'required' => true,
|
|
|
|
'allowEmpty' => false,
|
|
|
|
'label' => 'Backend Name',
|
|
|
|
'helptext' => 'The name of this authentication provider',
|
|
|
|
'value' => $this->getBackendName()
|
2013-08-16 16:24:12 +02:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->addElement(
|
|
|
|
'select',
|
|
|
|
'backend_' . $name . '_resource',
|
|
|
|
array(
|
2013-08-20 17:30:28 +02:00
|
|
|
'label' => 'Database Connection',
|
2013-08-16 16:24:12 +02:00
|
|
|
'required' => true,
|
|
|
|
'allowEmpty' => false,
|
2013-08-19 18:25:20 +02:00
|
|
|
'helptext' => 'The database connection to use for authenticating with this provider',
|
2013-08-16 16:24:12 +02:00
|
|
|
'value' => $this->getBackend()->get('resource'),
|
|
|
|
'multiOptions' => $this->getDatabaseResources()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2013-10-23 12:25:51 +02:00
|
|
|
$this->addElement(
|
|
|
|
'button',
|
|
|
|
'btn_submit',
|
|
|
|
array(
|
|
|
|
'type' => 'submit',
|
|
|
|
'value' => '1',
|
|
|
|
'escape' => false,
|
|
|
|
'class' => 'btn btn-cta btn-wide',
|
|
|
|
'label' => '<i class="icinga-icon-save"></i> Save Backend'
|
|
|
|
)
|
|
|
|
);
|
2013-08-16 16:24:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the datatbase authentication backend configuration for this form
|
|
|
|
*
|
2013-08-21 11:02:53 +02:00
|
|
|
* @return array
|
|
|
|
*
|
|
|
|
* @see BaseBackendForm::getConfig()
|
2013-08-16 16:24:12 +02:00
|
|
|
*/
|
|
|
|
public function getConfig()
|
|
|
|
{
|
|
|
|
$name = $this->getBackendName();
|
|
|
|
$prefix = 'backend_' . $this->filterName($name) . '_';
|
|
|
|
|
|
|
|
$section = $this->getValue($prefix . 'name');
|
|
|
|
$cfg = array(
|
2013-08-21 11:02:53 +02:00
|
|
|
'backend' => 'db',
|
|
|
|
'target' => 'user',
|
|
|
|
'resource' => $this->getValue($prefix . 'resource'),
|
2013-08-16 16:24:12 +02:00
|
|
|
);
|
|
|
|
return array(
|
|
|
|
$section => $cfg
|
|
|
|
);
|
|
|
|
}
|
2013-08-26 16:56:23 +02:00
|
|
|
|
2013-08-27 14:37:22 +02:00
|
|
|
/**
|
|
|
|
* Validate the current configuration by creating a backend and requesting the user count
|
|
|
|
*
|
|
|
|
* @return bool True when the backend is valid, false otherwise
|
|
|
|
* @see BaseBackendForm::isValidAuthenticationBackend
|
|
|
|
*/
|
|
|
|
public function isValidAuthenticationBackend()
|
2013-08-26 16:56:23 +02:00
|
|
|
{
|
2014-03-05 13:38:38 +01:00
|
|
|
// @TODO fix validation of authentication backends (AK #5712)
|
2014-03-05 16:10:44 +01:00
|
|
|
return true;
|
2013-08-26 17:23:31 +02:00
|
|
|
try {
|
|
|
|
$name = $this->getBackendName();
|
2013-09-02 17:33:12 +02:00
|
|
|
$dbBackend = new DbUserBackend(
|
|
|
|
new Zend_Config(
|
|
|
|
array(
|
|
|
|
'backend' => 'db',
|
|
|
|
'target' => 'user',
|
|
|
|
'resource' => $this->getValue('backend_' . $this->filterName($name) . '_resource'),
|
|
|
|
)
|
2013-08-30 16:28:13 +02:00
|
|
|
)
|
2013-09-02 17:33:12 +02:00
|
|
|
);
|
2014-01-22 11:38:47 +01:00
|
|
|
$dbBackend->connect();
|
2013-08-26 17:23:31 +02:00
|
|
|
if ($dbBackend->getUserCount() < 1) {
|
|
|
|
$this->addErrorMessage("No users found under the specified database backend");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$this->addErrorMessage("Using the specified backend failed: " . $e->getMessage());
|
|
|
|
return false;
|
|
|
|
} catch (\Zend_Db_Statement_Exception $e) {
|
|
|
|
$this->addErrorMessage("Using the specified backend failed: " . $e->getMessage());
|
|
|
|
return false;
|
|
|
|
}
|
2013-08-26 16:56:23 +02:00
|
|
|
return true;
|
|
|
|
}
|
2013-08-16 16:24:12 +02:00
|
|
|
}
|