Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/bugfix/rebuild-form-builder-5525…
Browse files Browse the repository at this point in the history
…' into bugfix/autologin-backend-form-6434
  • Loading branch information
Al2Klimov committed Jul 25, 2014
2 parents 50de21e + 26a78dc commit c06db74
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 115 deletions.
101 changes: 55 additions & 46 deletions application/controllers/ConfigController.php
Expand Up @@ -428,82 +428,91 @@ public function createresourceAction()
$this->render('resource/create');
}

/**
* Display a form to edit a existing resource
*/
public function editresourceAction()
{
$this->view->messageBox = new AlertMessageBox(true);

$resources = ResourceFactory::getResourceConfigs();
$name = $this->getParam('resource');
if ($resources->get($name) === null) {
$this->addErrorMessage('Can\'t edit: Unknown Resource Provided');
$this->render('resource/modify');
return;
// Fetch the resource to be edited
$resources = IcingaConfig::app('resources')->toArray();
$name = $this->getParam('resource');
if (false === array_key_exists($name, $resources)) {
$this->addErrorMessage(sprintf($this->translate('Cannot edit "%s". Resource not found.'), $name));
$this->redirectNow('config/configurationerror');
}

$form = new ResourceForm();
if ($this->_request->isPost() === false) {
$form->setOldName($name);
$form->setName($name);
}
$form->setRequest($this->_request);
$form->setResource($resources->get($name));
if ($form->isSubmittedAndValid()) {
$oldName = $form->getOldName();
$name = $form->getName();
if ($oldName !== $name) {
unset($resources->{$oldName});
}
$resources->{$name} = $form->getConfig();
if ($this->writeConfigFile($resources, 'resources')) {
$this->addSuccessMessage('Resource "' . $name . '" edited.');
$this->redirectNow("config/resource");
$request = $this->getRequest();
if ($request->isPost()) {
if ($form->isValid($request->getPost())) {
list($newName, $config) = $form->getResourceConfig();

if ($newName !== $name) {
// Resource name has changed
unset($resources[$name]); // We can safely use unset as all values are part of the form
}

$resources[$newName] = $config;
if ($this->writeConfigFile($resources, 'resources')) {
$this->addSuccessMessage(sprintf($this->translate('Resource "%s" successfully edited.'), $name));
$this->redirectNow('config/resource');
}
}
return;
} else {
$form->setResourceConfig($name, $resources[$name]);
}

$this->view->messageBox->addForm($form);
$this->view->form = $form;
$this->view->name = $name;
$this->view->messageBox->addForm($form);
$this->render('resource/modify');
}

/**
* Display a confirmation form to remove a resource
*/
public function removeresourceAction()
{
$this->view->messageBox = new AlertMessageBox(true);

$resources = ResourceFactory::getResourceConfigs()->toArray();
$name = $this->getParam('resource');
if (!isset($resources[$name])) {
$this->addSuccessMessage('Can\'t remove: Unknown resource provided');
$this->render('resource/remove');
return;
// Fetch the resource to be removed
$resources = IcingaConfig::app('resources')->toArray();
$name = $this->getParam('resource');
if (false === array_key_exists($name, $resources)) {
$this->addErrorMessage(sprintf($this->translate('Cannot remove "%s". Resource not found.'), $name));
$this->redirectNow('config/configurationerror');
}

$form = new ConfirmRemovalForm();
$form->setRequest($this->getRequest());
$form->setRemoveTarget('resource', $name);

// Check if selected resource is currently used for authentication
$authConfig = IcingaConfig::app('authentication', true)->toArray();
$authConfig = IcingaConfig::app('authentication')->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 (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
)
);
}
}

if ($form->isSubmittedAndValid()) {
$form = new ConfirmRemovalForm();
$request = $this->getRequest();
if ($request->isPost() && $form->isValid($request->getPost())) {
unset($resources[$name]);
if ($this->writeConfigFile($resources, 'resources')) {
$this->addSuccessMessage('Resource "' . $name . '" removed.');
$this->addSuccessMessage(sprintf($this->translate('Resource "%s" successfully removed.'), $name));
$this->redirectNow('config/resource');
}
return;
}

$this->view->name = $name;
$this->view->form = $form;
$this->view->messageBox->addForm($form);
$this->render('resource/remove');
}

Expand Down
48 changes: 7 additions & 41 deletions application/forms/Config/ConfirmRemovalForm.php
Expand Up @@ -12,57 +12,23 @@
class ConfirmRemovalForm extends Form
{
/**
* The value of the target to remove
*
* @var string
* Initalize this form
*/
private $removeTarget;

/**
* 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)
public function init()
{
$this->targetName = $name;
$this->removeTarget = $target;
$this->setName('form_confirm_removal');
}

/**
* Create the confirmation form
*
* @see Form::create()
* @see Form::addSubmitButton()
*/
public function create()
public function addSubmitButton()
{
$this->setName('form_confirm_removal');
$this->addElement(
'hidden',
$this->targetName,
array(
'value' => $this->removeTarget,
'required' => true
)
);

$this->addElement(
'button',
'submit',
'btn_submit',
array(
'type' => 'submit',
'escape' => false,
'value' => '1',
'class' => 'btn btn-cta btn-common',
'label' => '<i class="icinga-icon-remove"></i> Confirm Removal'
'label' => t('Confirm Removal')
)
);
}
Expand Down
21 changes: 3 additions & 18 deletions application/views/scripts/config/resource/modify.phtml
@@ -1,18 +1,3 @@
<h4>
<i class="icinga-icon-edit"></i>
Edit Resource "<?= $this->escape($this->name); ?>"
</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 ?>
<h4><?= $this->translate('Edit Existing Resource'); ?></h4>
<?= $messageBox; ?>
<?= $form; ?>
13 changes: 3 additions & 10 deletions application/views/scripts/config/resource/remove.phtml
@@ -1,10 +1,3 @@
<h4>
<i class="icinga-icon-remove"></i>
Remove Resource "<?= $this->escape($this->name); ?>"
</h4>

<?php if (isset($this->messageBox)): ?>
<?= $this->messageBox->render() ?>
<?php endif ?>

<?= $this->form ?>
<h4><?= $this->translate('Remove Existing Resource'); ?></h4>
<?= $messageBox; ?>
<?= $form; ?>
14 changes: 14 additions & 0 deletions library/Icinga/Web/Controller/BaseConfigController.php
Expand Up @@ -47,6 +47,20 @@ protected function addErrorMessage($msg)
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.
*
Expand Down

0 comments on commit c06db74

Please sign in to comment.