Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions library/Director/RestApi/IcingaObjectHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,21 @@ protected function handleApiRequest()
], $data);
$object->replaceWith(IcingaObject::createByType($type, $data, $db));
}

if (in_array((int) $object->get('id'), $object->listAncestorIds())) {
throw new RuntimeException(
'Import loop detected for the object '
. $object->getObjectName() . ' -> Imports: '
. implode(', ', $object->getImports())
);
}

if (in_array($object->get('object_name'), $data['imports'])) {
throw new RuntimeException(
'You can not import the same object into itself: ' . $object->getObjectName()
);
}

$this->persistChanges($object);
$this->sendJson($object->toPlainObject(false, true));
} elseif ($allowsOverrides && $type === 'service') {
Expand Down
37 changes: 36 additions & 1 deletion library/Director/Web/Form/DirectorObjectForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@
use Icinga\Module\Director\Objects\IcingaTemplateChoice;
use Icinga\Module\Director\Objects\IcingaCommand;
use Icinga\Module\Director\Objects\IcingaObject;
use Icinga\Module\Director\Resolver\TemplateTree;
use Icinga\Module\Director\Util;
use Icinga\Module\Director\Web\Form\Element\ExtensibleSet;
use Icinga\Module\Director\Web\Form\Validate\NamePattern;
use Zend_Form_Element as ZfElement;
use Zend_Form_Element_Select as ZfSelect;
use Zend_Validate_Callback;

abstract class DirectorObjectForm extends DirectorForm
{
Expand Down Expand Up @@ -1283,7 +1285,40 @@ protected function addImportsElement($required = null)
// 'multiOptions' => $this->optionallyAddFromEnum($enum),
'sorted' => true,
'value' => $this->presetImports,
'class' => 'autosubmit'
'class' => 'autosubmit',
'validators' => [
new Zend_Validate_Callback(function ($value) {
$templateTree = new TemplateTree($this->object->getShortTableName(), $this->getDb());
$importsElement = $this->getElement('imports');
$objectName = $this->object->getObjectName();
if (in_array($objectName, $value, true)) {
$importsElement->addErrorMessage(
$this->translate('You can not import the same object into itself, please remove it first')
);

return false;
}

if ($this->object->isTemplate()) {
$descendents = $templateTree->getDescendantsFor($this->object);
$loopedImports = array_intersect($templateTree->getDescendantsFor($this->object), $value);
if (! empty($loopedImports)) {
$loopedImport = array_slice($loopedImports, 0, 1);
array_push($loopedImport, ...$descendents);
$importsElement->addErrorMessage(
sprintf(
$this->translate('Loop detected for imports: %s'),
implode('->', $loopedImports)
)
);

return false;
}
}

return true;
})
]
));

return $this;
Expand Down
Loading