Add controller to handle resource configuration

Add the controller, forms and views to handle the resource configuration.

refs #4786
This commit is contained in:
Matthias Jentsch 2013-11-06 19:02:30 +01:00
parent bd38e998d7
commit 2cf154310b
8 changed files with 434 additions and 94 deletions

View File

@ -34,6 +34,7 @@ use \Icinga\Web\Url;
use \Icinga\Web\Hook\Configuration\ConfigurationTabBuilder;
use \Icinga\Application\Icinga;
use \Icinga\Application\Config as IcingaConfig;
use \Icinga\Data\ResourceFactory;
use \Icinga\Form\Config\GeneralForm;
use \Icinga\Form\Config\Authentication\ReorderForm;
use \Icinga\Form\Config\Authentication\LdapBackendForm;
@ -47,6 +48,12 @@ use \Icinga\Config\PreservingIniWriter;
*/
class ConfigController extends BaseConfigController
{
/**
* The resource types that are available.
*
* @var array
*/
private $resourceTypes = array('livestatus', 'ido', 'statusdat', 'ldap');
/**
* Create tabs for this configuration controller
@ -72,7 +79,13 @@ class ConfigController extends BaseConfigController
'url' => Url::fromPath('/config/authentication')
)
),
'resources' => new Tab(
array(
'name' => 'resource',
'title' => 'Resources',
'url' => Url::fromPath('/config/resource')
)
),
'logging' => new Tab(
array(
'name' => 'logging',
@ -210,40 +223,6 @@ class ConfigController extends BaseConfigController
$this->render('authentication');
}
/**
* Action for removing a backend from the authentication list.
*
* Redirects to the overview after removal is finished
*/
public function removeauthenticationbackendAction()
{
$configArray = IcingaConfig::app('authentication', true)->toArray();
$authBackend = $this->getParam('auth_backend');
if (!isset($configArray[$authBackend])) {
$this->view->errorMessage = 'Can\'t perform removal: Unknown Authentication Backend Provided';
$this->authenticationAction(true);
return;
}
$form = new ConfirmRemovalForm();
$form->setRequest($this->getRequest());
$form->setRemoveTarget('auth_backend', $authBackend);
if ($form->isSubmittedAndValid()) {
unset($configArray[$authBackend]);
if ($this->writeAuthenticationFile($configArray)) {
$this->view->successMessage = 'Authentication Backend "' . $authBackend . '" Removed';
$this->authenticationAction(true);
}
return;
}
$this->view->form = $form;
$this->view->name = $authBackend;
$this->render('authentication/remove');
}
/**
* Action for creating a new authentication backend
*/
@ -281,6 +260,7 @@ class ConfigController extends BaseConfigController
$this->render('authentication/create');
}
/**
* Form for editing backends
*
@ -296,11 +276,24 @@ class ConfigController extends BaseConfigController
$this->authenticationAction(true);
return;
}
if (!array_key_exists('resource', $configArray[$authBackend])) {
$this->view->errorMessage = 'Configuration error: Backend "' . $authBackend . '" has no Resource';
$this->authenticationAction(true);
return;
}
if ($configArray[$authBackend]['backend'] === 'ldap') {
$form = new LdapBackendForm();
} else {
$form = new DbBackendForm();
$type = ResourceFactory::getResourceConfig($configArray[$authBackend]['resource'])->type;
switch ($type) {
case 'ldap':
$form = new LdapBackendForm();
break;
case 'db':
$form = new DbBackendForm();
break;
default:
$this->view->errorMessage = 'Can\'t edit: backend type "' . $type . '" of given resource not supported.';
$this->authenticationAction(true);
return;
}
$form->setBackendName($this->getParam('auth_backend'));
@ -329,6 +322,82 @@ class ConfigController extends BaseConfigController
$this->render('authentication/modify');
}
/**
* Action for removing a backend from the authentication list.
*
* Redirects to the overview after removal is finished
*/
public function removeauthenticationbackendAction()
{
$configArray = IcingaConfig::app('authentication', true)->toArray();
$authBackend = $this->getParam('auth_backend');
if (!isset($configArray[$authBackend])) {
$this->view->errorMessage = 'Can\'t perform removal: Unknown Authentication Backend Provided';
$this->authenticationAction(true);
return;
}
if (!array_key_exists('resource', $configArray[$authBackend])) {
$this->view->errorMessage = 'Configuration error: Backend "' . $authBackend . '" has no Resource';
$this->authenticationAction(true);
return;
}
$form = new ConfirmRemovalForm();
$form->setRequest($this->getRequest());
$form->setRemoveTarget('auth_backend', $authBackend);
if ($form->isSubmittedAndValid()) {
unset($configArray[$authBackend]);
if ($this->writeAuthenticationFile($configArray)) {
$this->view->successMessage = 'Authentication Backend "' . $authBackend . '" Removed';
$this->authenticationAction(true);
}
return;
}
$this->view->form = $form;
$this->view->name = $authBackend;
$this->render('authentication/remove');
}
public function resourceAction($showOnly = false)
{
$this->view->resources = ResourceFactory::getResourceConfigs()->toArray();
$this->view->createActions = array();
$this->render('resource');
}
public function createresourceAction()
{
$this->view->resourceTypes = $this->resourceTypes;
$this->render('resource/create');
}
public function editresourceAction()
{
$resources = ResourceFactory::getResourceConfigs()->toArray();
$name = $resources[$this->getParam('resource')];
if (!isset($resources[$name])) {
$this->view->errorMessage = 'Can\'t edit: Unknown Resource Provided';
$this->resourceAction(true);
return;
}
$this->render('resource/modify');
}
public function removeresourceAction()
{
$resources = ResourceFactory::getResourceConfigs()->toArray();
$name = $resources[$this->getParam('resource')];
if (!isset($resources[$name])) {
$this->view->errorMessage = 'Can\'t remove: Unknown Resource Provided';
$this->resourceAction(true);
return;
}
$this->render('resource/remove');
}
/**
* Write changes to an authentication file
*

View File

@ -30,6 +30,7 @@
namespace Icinga\Form\Config\Authentication;
use \Exception;
use Icinga\Data\ResourceFactory;
use \Zend_Config;
use \Icinga\Web\Form;
use \Icinga\Authentication\Backend\LdapUserBackend;
@ -64,48 +65,15 @@ class LdapBackendForm extends BaseBackendForm
);
$this->addElement(
'text',
'backend_' . $name . '_hostname',
'select',
'backend_' . $name . '_resource',
array(
'label' => 'LDAP Server Host',
'label' => 'Database Connection',
'required' => true,
'allowEmpty' => false,
'value' => $backend->get('hostname', 'localhost'),
'helptext' => 'The hostname or address of the LDAP server to use for authentication',
'required' => true
)
);
$this->addElement(
'text',
'backend_' . $name . '_root_dn',
array(
'label' => 'LDAP Root DN',
'value' => $backend->get('root_dn', 'ou=people,dc=icinga,dc=org'),
'helptext' => 'The path where users can be found on the ldap server',
'required' => true
)
);
$this->addElement(
'text',
'backend_' . $name . '_bind_dn',
array(
'label' => 'LDAP Bind DN',
'value' => $backend->get('bind_dn', 'cn=admin,cn=config'),
'helptext' => 'The user dn to use for querying the ldap server',
'required' => true
)
);
$this->addElement(
'password',
'backend_' . $name . '_bind_pw',
array(
'label' => 'LDAP Bind Password',
'renderPassword' => true,
'value' => $backend->get('bind_pw', 'admin'),
'helptext' => 'The password to use for querying the ldap server',
'required' => true
'helptext' => 'The database connection to use for authenticating with this provider',
'value' => $this->getBackend()->get('resource'),
'multiOptions' => $this->getLdapResources()
)
);
@ -158,14 +126,16 @@ class LdapBackendForm extends BaseBackendForm
$section = $this->getValue($prefix . 'name');
$cfg = array(
'backend' => 'ldap',
'target' => 'user',
'target' => 'user',
'resource' => $this->getValue($prefix . 'resource'),
'user_class' => $this->getValue($prefix . 'user_class'),
'user_name_attribute' => $this->getValue($prefix . 'user_name_attribute')
/*
'hostname' => $this->getValue($prefix . 'hostname'),
'root_dn' => $this->getValue($prefix . 'root_dn'),
'bind_dn' => $this->getValue($prefix . 'bind_dn'),
'bind_pw' => $this->getValue($prefix . 'bind_pw'),
'user_class' => $this->getValue($prefix . 'user_class'),
'user_name_attribute' => $this->getValue($prefix . 'user_name_attribute')
*/
);
return array(
$section => $cfg
@ -184,21 +154,25 @@ class LdapBackendForm extends BaseBackendForm
$cfg = $this->getConfig();
$backendName = 'backend_' . $this->filterName($this->getBackendName()) . '_name';
$backendConfig = new Zend_Config($cfg[$this->getValue($backendName)]);
$testConn = new LdapUserBackend(
new LdapConnection($backendConfig), $backendConfig
);
$testConn = new LdapUserBackend($backendConfig);
if ($testConn->getUserCount() === 0) {
throw new Exception('No Users Found On Directory Server');
}
} catch (Exception $exc) {
$this->addErrorMessage(
'Connection Validation Failed:'.
$exc->getMessage()
'Connection Validation Failed:' . $exc->getMessage()
);
return false;
}
return true;
}
private function getLdapResources()
{
$res = ResourceFactory::getResourceConfigs('ldap')->toArray();
foreach ($res as $key => $value) {
$res[$key] = $key;
}
return $res;
}
}

View File

@ -0,0 +1,182 @@
<?php
// {{{ICINGA_LICENSE_HEADER}}}
/**
* This file is part of Icinga Web 2.
*
* Icinga Web 2 - Head for multiple monitoring backends.
* 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>
* @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\Module\Monitoring\Form\Config\Backend;
use \Zend_Config;
use \Icinga\Web\Form;
use \Icinga\Application\Icinga;
use Icinga\Data\ResourceFactory;
/**
* Form for modifying a monitoring backend
*/
class EditResourceForm extends Form
{
/**
* The currently edited resource.
*
* @var Zend_Config
*/
private $resource;
private $name;
private function addDbForm()
{
}
private function addStatusdatForm()
{
$this->addElement(
'text',
'backend_statusdat_statusfile',
array (
'label' => 'Status.dat File',
'value' => $this->backend->status_file,
'required' => true,
'helptext' => 'Location of your icinga status.dat file'
)
);
$this->addElement(
'text',
'backend_statusdat_objectfile',
array (
'label' => 'Objects.cache File',
'value' => $this->backend->status_file,
'required' => true,
'helptext' => 'Location of your icinga objects.cache file'
)
);
}
private function addLivestatusForm()
{
$this->addElement(
'text',
'backend_livestatus_socket',
array(
'label' => 'Livestatus Socket Location',
'required' => true,
'helptext' => 'The path to your livestatus socket used for querying monitoring data',
'value' => $this->backend->socket,
)
);
}
private function addLdapForm()
{
$this->addElement(
'text',
'resource_' . $this->name . '_hostname',
array(
'label' => 'LDAP Server Host',
'allowEmpty' => false,
'value' => $this->resource->get('hostname', 'localhost'),
'helptext' => 'The hostname or address of the LDAP server to use for authentication',
'required' => true
)
);
$this->addElement(
'text',
'resource_' . $this->name . '_root_dn',
array(
'label' => 'LDAP Root DN',
'value' => $this->resource->get('root_dn', 'ou=people,dc=icinga,dc=org'),
'helptext' => 'The path where users can be found on the ldap server',
'required' => true
)
);
$this->addElement(
'text',
'resource_' . $this->name . '_bind_dn',
array(
'label' => 'LDAP Bind DN',
'value' => $this->resource->get('bind_dn', 'cn=admin,cn=config'),
'helptext' => 'The user dn to use for querying the ldap server',
'required' => true
)
);
$this->addElement(
'password',
'resource_' . $this->name . '_bind_pw',
array(
'label' => 'LDAP Bind Password',
'renderPassword' => true,
'value' => $this->resource->get('bind_pw', 'admin'),
'helptext' => 'The password to use for querying the ldap server',
'required' => true
)
);
}
/**
* Add a select box for choosing the type to use for this backend
*/
private function addTypeSelectionBox()
{
$this->addElement(
'select',
'resource_type',
array(
'label' => 'Resource Type',
'value' => $this->resource->type,
'required' => true,
'helptext' => 'Choose the type of resource you want to create.',
'multiOptions' => array(
'db' => 'SQL Database',
'ldap' => 'Ldap',
'statusdat' => 'Status.dat',
'livestatus' => 'Livestatus'
)
)
);
$this->enableAutoSubmit(array('resource_type'));
}
public function create()
{
$this->addTypeSelectionBox();
switch ($this->getRequest()->getParam('resource_type', $this->resource->type)) {
case 'db':
break;
case 'statusdat':
break;
case 'livestatus':
break;
case 'ldap':
break;
}
$this->setSubmitLabel('{{SAVE_ICON}} Save Changes');
}
}

View File

@ -0,0 +1,56 @@
<?php
use Icinga\Web\Url;
$createResource = $this->href('/config/createresource');
?>
<?= $this->tabs->render($this); ?>
<?php if ($this->errorMessage): ?>
<div class="alert alert-danger">
<i class="icinga-icon-error"></i>
<strong><?= $this->escape($this->errorMessage); ?></strong>
</div>
<?php endif; ?>
<?php if ($this->successMessage): ?>
<div class="alert alert-success">
<i class="icinga-icon-success"></i>
<strong><?= $this->escape($this->successMessage); ?></strong>
</div>
<?php endif; ?>
<div class="panel panel-default">
<div class="panel-heading panel-title">
Create Resource
</div>
<div class="panel-body">
<a href="<?= $createResource ?>"><i class="icinga-icon-create"></i> Create A New Resource</a><br/>
</div>
</div>
<?php foreach ($this->resources as $name => $resource): ?>
<div class="panel panel-default">
<div class="panel-heading panel-title">
<b>Resource: </b> <?= $this->escape($name); ?>
<br/>
</div>
<div class="panel-body">
<a href="<?= $this->href('config/editresource', array('resource' => $name));?>">
<i class="icinga-icon-edit"></i> Edit This Resource
</a>
<br/>
<?php if (count($this->resources) > 1): ?>
<a href="<?= $this->href('config/removeresource', array('resource' => $name));?>">
<i class="icinga-icon-remove"></i> Remove This Resource
</a>
<br/>
<?php endif; ?>
</div>
</div>
<?php endforeach; ?>

View File

@ -0,0 +1,18 @@
<?= $this->tabs->render($this); ?>
<h4>
<i class="icinga-icon-create"></i>
Create New Resource
</h4>
<?php if ($this->errorMessage): ?>
<div class="alert alert-danger">
<?= $this->escape($this->errorMessage); ?>
</div>
<?php endif; ?>
<p>
Create a new backend for authenticating your users. This backend will be added at the end of your authentication order.
</p>
<?= $this->form ?>

View File

@ -0,0 +1,17 @@
<?= $this->tabs->render($this); ?>
<h4>
<i class="icinga-icon-edit"></i>
Edit Resource "<?= $this->escape($this->name); ?>"
</h4>
<?php if ($this->errorMessage || $this->form->getErrorMessages()): ?>
<div class="alert alert-danger">
<?= $this->escape($this->errorMessage); ?>
<?php foreach ($this->form->getErrorMessages() as $error): ?>
<?= $this->escape($error); ?><br/>
<?php endforeach; ?>
</div>
<?php endif; ?>
<?= $this->form ?>

View File

@ -0,0 +1,7 @@
<?= $this->tabs->render($this); ?>
<h4>
<i class="icinga-icon-remove"></i>
Remove Resource "<?= $this->escape($this->name); ?>"
</h4>
<?= $this->form ?>

View File

@ -68,14 +68,26 @@ class ResourceFactory implements ConfigAwareFactory
}
/**
* Return the configuration of all existing resources.
* Return the configuration of all existing resources, or get all resources of a given type.
*
* @return Zend_Config The configuration containing all resources
* @param String|null $type Fetch only resources that have the given type.
*
* @return Zend_Config The configuration containing all resources
*/
public static function getResourceConfigs()
public static function getResourceConfigs($type = null)
{
self::assertResourcesExist();
return self::$resources;
if (!isset($type)) {
return self::$resources;
} else {
$resources = array();
foreach (self::$resources as $name => $resource) {
if (strtolower($resource->type) === $type) {
$resources[$name] = $resource;
}
}
return new Zend_Config($resources);
}
}
/**
@ -124,4 +136,9 @@ class ResourceFactory implements ConfigAwareFactory
}
return $resource;
}
public static function getBackendType($resource)
{
}
}