Merge remote-tracking branch 'origin/bugfix/rebuild-form-builder-5525' into bugfix/autologin-backend-form-6434

This commit is contained in:
Alexander Klimov 2014-07-25 10:57:08 +02:00
commit c06db7495b
5 changed files with 88 additions and 121 deletions

View File

@ -428,82 +428,91 @@ class ConfigController extends BaseConfigController
$this->render('resource/create'); $this->render('resource/create');
} }
/**
* Display a form to edit a existing resource
*/
public function editresourceAction() public function editresourceAction()
{ {
$this->view->messageBox = new AlertMessageBox(true); $this->view->messageBox = new AlertMessageBox(true);
$resources = ResourceFactory::getResourceConfigs(); // Fetch the resource to be edited
$name = $this->getParam('resource'); $resources = IcingaConfig::app('resources')->toArray();
if ($resources->get($name) === null) { $name = $this->getParam('resource');
$this->addErrorMessage('Can\'t edit: Unknown Resource Provided'); if (false === array_key_exists($name, $resources)) {
$this->render('resource/modify'); $this->addErrorMessage(sprintf($this->translate('Cannot edit "%s". Resource not found.'), $name));
return; $this->redirectNow('config/configurationerror');
} }
$form = new ResourceForm();
if ($this->_request->isPost() === false) { $form = new ResourceForm();
$form->setOldName($name); $request = $this->getRequest();
$form->setName($name); if ($request->isPost()) {
} if ($form->isValid($request->getPost())) {
$form->setRequest($this->_request); list($newName, $config) = $form->getResourceConfig();
$form->setResource($resources->get($name));
if ($form->isSubmittedAndValid()) { if ($newName !== $name) {
$oldName = $form->getOldName(); // Resource name has changed
$name = $form->getName(); unset($resources[$name]); // We can safely use unset as all values are part of the form
if ($oldName !== $name) { }
unset($resources->{$oldName});
} $resources[$newName] = $config;
$resources->{$name} = $form->getConfig(); if ($this->writeConfigFile($resources, 'resources')) {
if ($this->writeConfigFile($resources, 'resources')) { $this->addSuccessMessage(sprintf($this->translate('Resource "%s" successfully edited.'), $name));
$this->addSuccessMessage('Resource "' . $name . '" edited.'); $this->redirectNow('config/resource');
$this->redirectNow("config/resource"); }
} }
return; } else {
$form->setResourceConfig($name, $resources[$name]);
} }
$this->view->messageBox->addForm($form);
$this->view->form = $form; $this->view->form = $form;
$this->view->name = $name; $this->view->messageBox->addForm($form);
$this->render('resource/modify'); $this->render('resource/modify');
} }
/**
* Display a confirmation form to remove a resource
*/
public function removeresourceAction() public function removeresourceAction()
{ {
$this->view->messageBox = new AlertMessageBox(true); $this->view->messageBox = new AlertMessageBox(true);
$resources = ResourceFactory::getResourceConfigs()->toArray(); // Fetch the resource to be removed
$name = $this->getParam('resource'); $resources = IcingaConfig::app('resources')->toArray();
if (!isset($resources[$name])) { $name = $this->getParam('resource');
$this->addSuccessMessage('Can\'t remove: Unknown resource provided'); if (false === array_key_exists($name, $resources)) {
$this->render('resource/remove'); $this->addErrorMessage(sprintf($this->translate('Cannot remove "%s". Resource not found.'), $name));
return; $this->redirectNow('config/configurationerror');
}
// Check if selected resource is currently used for authentication
$authConfig = IcingaConfig::app('authentication')->toArray();
foreach ($authConfig as $backendName => $config) {
if (array_key_exists('resource', $config) && $config['resource'] === $name) {
$this->addWarningMessage(
sprintf(
$this->translate(
'The resource "%s" is currently in use by the authentication backend "%s". ' .
'Removing the resource can result in noone being able to log in any longer.'
),
$name,
$backendName
)
);
}
} }
$form = new ConfirmRemovalForm(); $form = new ConfirmRemovalForm();
$form->setRequest($this->getRequest()); $request = $this->getRequest();
$form->setRemoveTarget('resource', $name); if ($request->isPost() && $form->isValid($request->getPost())) {
// Check if selected resource is currently used for authentication
$authConfig = IcingaConfig::app('authentication', true)->toArray();
foreach ($authConfig as $backendName => $config) {
if (array_key_exists('resource', $config) && $config['resource'] === $name) {
$this->addErrorMessage(
'Warning: The resource "' . $name . '" is currently used for user authentication by "' . $backendName . '". ' .
' Deleting it could eventally make login impossible.'
);
}
}
if ($form->isSubmittedAndValid()) {
unset($resources[$name]); unset($resources[$name]);
if ($this->writeConfigFile($resources, 'resources')) { if ($this->writeConfigFile($resources, 'resources')) {
$this->addSuccessMessage('Resource "' . $name . '" removed.'); $this->addSuccessMessage(sprintf($this->translate('Resource "%s" successfully removed.'), $name));
$this->redirectNow('config/resource'); $this->redirectNow('config/resource');
} }
return;
} }
$this->view->name = $name;
$this->view->form = $form; $this->view->form = $form;
$this->view->messageBox->addForm($form);
$this->render('resource/remove'); $this->render('resource/remove');
} }

View File

@ -12,57 +12,23 @@ use Icinga\Web\Form;
class ConfirmRemovalForm extends Form class ConfirmRemovalForm extends Form
{ {
/** /**
* The value of the target to remove * Initalize this form
*
* @var string
*/ */
private $removeTarget; public function init()
/**
* The name of the target parameter to remove
*
* @var string
*/
private $targetName;
/**
* Set the remove target in this field to be a hidden field with $name and value $target
*
* @param string $name The name to be set in the hidden field
* @param string $target The value to be set in the hidden field
*/
public function setRemoveTarget($name, $target)
{ {
$this->targetName = $name; $this->setName('form_confirm_removal');
$this->removeTarget = $target;
} }
/** /**
* Create the confirmation form * @see Form::addSubmitButton()
*
* @see Form::create()
*/ */
public function create() public function addSubmitButton()
{ {
$this->setName('form_confirm_removal');
$this->addElement( $this->addElement(
'hidden', 'submit',
$this->targetName,
array(
'value' => $this->removeTarget,
'required' => true
)
);
$this->addElement(
'button',
'btn_submit', 'btn_submit',
array( array(
'type' => 'submit', 'label' => t('Confirm Removal')
'escape' => false,
'value' => '1',
'class' => 'btn btn-cta btn-common',
'label' => '<i class="icinga-icon-remove"></i> Confirm Removal'
) )
); );
} }

View File

@ -1,18 +1,3 @@
<h4> <h4><?= $this->translate('Edit Existing Resource'); ?></h4>
<i class="icinga-icon-edit"></i> <?= $messageBox; ?>
Edit Resource "<?= $this->escape($this->name); ?>" <?= $form; ?>
</h4>
<?php if (isset($this->messageBox)): ?>
<?= $this->messageBox->render() ?>
<?php endif ?>
<?php if ($this->form->getErrorMessages()): ?>
<div>
<?php foreach ($this->form->getErrorMessages() as $error): ?>
<?= $this->escape($error); ?><br/>
<?php endforeach; ?>
</div>
<?php endif ?>
<?= $this->form ?>

View File

@ -1,10 +1,3 @@
<h4> <h4><?= $this->translate('Remove Existing Resource'); ?></h4>
<i class="icinga-icon-remove"></i> <?= $messageBox; ?>
Remove Resource "<?= $this->escape($this->name); ?>" <?= $form; ?>
</h4>
<?php if (isset($this->messageBox)): ?>
<?= $this->messageBox->render() ?>
<?php endif ?>
<?= $this->form ?>

View File

@ -47,6 +47,20 @@ class BaseConfigController extends ActionController
Session::getSession()->write(); Session::getSession()->write();
} }
/**
* Send a message with the logging level Zend_Log::WARN to the current user and
* commit the changes to the underlying session.
*
* @param $msg The message content
*/
protected function addWarningMessage($msg)
{
AuthenticationManager::getInstance()->getUser()->addMessage(
new Message($msg, Zend_Log::WARN)
);
Session::getSession()->write();
}
/* /*
* Return an array of tabs provided by this configuration controller. * Return an array of tabs provided by this configuration controller.
* *