2015-06-30 11:19:31 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Icinga\Module\Director\Web\Controller;
|
|
|
|
|
2020-10-26 18:50:13 +01:00
|
|
|
use gipfl\Web\Widget\Hint;
|
2017-10-20 10:23:23 +02:00
|
|
|
use Icinga\Exception\IcingaException;
|
2016-03-06 14:20:49 +01:00
|
|
|
use Icinga\Exception\InvalidPropertyException;
|
2016-02-17 19:22:36 +01:00
|
|
|
use Icinga\Exception\NotFoundError;
|
2021-08-16 07:12:06 +02:00
|
|
|
use Icinga\Exception\ProgrammingError;
|
2021-10-05 23:10:50 +02:00
|
|
|
use Icinga\Module\Director\Data\Db\DbObjectTypeRegistry;
|
2021-08-16 11:43:09 +02:00
|
|
|
use Icinga\Module\Director\Db\Branch\Branch;
|
2021-10-05 23:10:50 +02:00
|
|
|
use Icinga\Module\Director\Db\Branch\BranchedObject;
|
|
|
|
use Icinga\Module\Director\Db\Branch\UuidLookup;
|
2017-07-20 08:31:53 +02:00
|
|
|
use Icinga\Module\Director\Deployment\DeploymentInfo;
|
2018-10-15 14:49:47 +02:00
|
|
|
use Icinga\Module\Director\DirectorObject\Automation\ExportInterface;
|
2019-02-13 21:41:46 +01:00
|
|
|
use Icinga\Module\Director\Exception\NestingError;
|
2017-07-20 08:31:53 +02:00
|
|
|
use Icinga\Module\Director\Forms\DeploymentLinkForm;
|
2017-07-25 10:16:15 +02:00
|
|
|
use Icinga\Module\Director\Forms\IcingaCloneObjectForm;
|
2017-07-06 09:04:32 +02:00
|
|
|
use Icinga\Module\Director\Forms\IcingaObjectFieldForm;
|
2019-05-27 17:27:18 +02:00
|
|
|
use Icinga\Module\Director\Objects\IcingaCommand;
|
2015-08-02 15:01:07 +02:00
|
|
|
use Icinga\Module\Director\Objects\IcingaObject;
|
2017-07-20 08:31:53 +02:00
|
|
|
use Icinga\Module\Director\Objects\IcingaObjectGroup;
|
2019-03-19 15:29:08 +01:00
|
|
|
use Icinga\Module\Director\Objects\IcingaService;
|
2018-10-25 08:38:24 +02:00
|
|
|
use Icinga\Module\Director\Objects\IcingaServiceSet;
|
2017-08-16 23:53:37 +02:00
|
|
|
use Icinga\Module\Director\RestApi\IcingaObjectHandler;
|
2017-06-22 00:44:31 +02:00
|
|
|
use Icinga\Module\Director\Web\Controller\Extension\ObjectRestrictions;
|
2016-11-16 16:02:28 +01:00
|
|
|
use Icinga\Module\Director\Web\Form\DirectorObjectForm;
|
2017-08-16 23:53:37 +02:00
|
|
|
use Icinga\Module\Director\Web\ObjectPreview;
|
2017-07-20 08:31:53 +02:00
|
|
|
use Icinga\Module\Director\Web\Table\ActivityLogTable;
|
2021-10-05 23:10:50 +02:00
|
|
|
use Icinga\Module\Director\Web\Table\BranchActivityTable;
|
2017-07-11 08:33:03 +02:00
|
|
|
use Icinga\Module\Director\Web\Table\GroupMemberTable;
|
2017-08-16 23:53:37 +02:00
|
|
|
use Icinga\Module\Director\Web\Table\IcingaObjectDatafieldTable;
|
2017-07-03 07:04:31 +02:00
|
|
|
use Icinga\Module\Director\Web\Tabs\ObjectTabs;
|
2021-10-05 19:13:57 +02:00
|
|
|
use Icinga\Module\Director\Web\Widget\BranchedObjectHint;
|
2019-05-02 13:23:06 +02:00
|
|
|
use gipfl\IcingaWeb2\Link;
|
2021-10-05 23:10:50 +02:00
|
|
|
use ipl\Html\Html;
|
|
|
|
use Ramsey\Uuid\Uuid;
|
|
|
|
use Ramsey\Uuid\UuidInterface;
|
2015-06-30 11:19:31 +02:00
|
|
|
|
|
|
|
abstract class ObjectController extends ActionController
|
|
|
|
{
|
2017-06-22 00:44:31 +02:00
|
|
|
use ObjectRestrictions;
|
2021-08-16 11:43:09 +02:00
|
|
|
use BranchHelper;
|
2017-06-22 00:44:31 +02:00
|
|
|
|
2017-02-08 12:38:14 +01:00
|
|
|
/** @var IcingaObject */
|
2015-06-30 11:19:31 +02:00
|
|
|
protected $object;
|
|
|
|
|
2017-08-16 08:08:08 +02:00
|
|
|
/** @var bool This controller handles REST API requests */
|
2015-12-10 13:11:21 +01:00
|
|
|
protected $isApified = true;
|
|
|
|
|
2017-08-16 08:08:08 +02:00
|
|
|
/** @var array Allowed object types we are allowed to edit anyways */
|
2016-03-22 18:19:13 +01:00
|
|
|
protected $allowedExternals = array(
|
|
|
|
'apiuser',
|
|
|
|
'endpoint'
|
|
|
|
);
|
|
|
|
|
2017-08-16 23:53:37 +02:00
|
|
|
protected $type;
|
|
|
|
|
2020-11-30 10:17:44 +01:00
|
|
|
/** @var string|null */
|
|
|
|
protected $objectBaseUrl;
|
|
|
|
|
2015-06-30 11:19:31 +02:00
|
|
|
public function init()
|
|
|
|
{
|
2015-12-10 13:11:21 +01:00
|
|
|
parent::init();
|
2021-12-17 11:46:55 +01:00
|
|
|
$this->enableStaticObjectLoader($this->getTableName());
|
2015-12-10 13:11:21 +01:00
|
|
|
|
2016-12-16 12:07:01 +01:00
|
|
|
if ($this->getRequest()->isApiRequest()) {
|
2017-08-16 08:08:08 +02:00
|
|
|
$handler = new IcingaObjectHandler($this->getRequest(), $this->getResponse(), $this->db());
|
2017-08-22 16:47:57 +02:00
|
|
|
try {
|
2021-10-05 23:10:50 +02:00
|
|
|
$this->loadOptionalObject();
|
2017-08-22 16:47:57 +02:00
|
|
|
} catch (NotFoundError $e) {
|
|
|
|
// Silently ignore the error, the handler will complain
|
|
|
|
$handler->sendJsonError($e, 404);
|
|
|
|
// TODO: nice shutdown
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2017-08-16 08:08:08 +02:00
|
|
|
$handler->setApi($this->api());
|
|
|
|
if ($this->object) {
|
|
|
|
$handler->setObject($this->object);
|
2016-12-16 12:07:01 +01:00
|
|
|
}
|
2017-08-16 08:08:08 +02:00
|
|
|
$handler->dispatch();
|
2018-09-05 22:27:07 +02:00
|
|
|
// Hint: also here, hard exit. There is too much magic going on.
|
|
|
|
// Letting this bubble up smoothly would be "correct", but proved
|
|
|
|
// to be too fragile. Web 2, all kinds of pre/postDispatch magic,
|
|
|
|
// different view renderers - hard exit is the only safe bet right
|
|
|
|
// now.
|
|
|
|
exit;
|
2017-08-16 08:08:08 +02:00
|
|
|
} else {
|
2021-10-05 23:10:50 +02:00
|
|
|
$this->loadOptionalObject();
|
2017-08-21 13:20:47 +02:00
|
|
|
if ($this->getRequest()->getActionName() === 'add') {
|
|
|
|
$this->addSingleTab(
|
|
|
|
sprintf($this->translate('Add %s'), ucfirst($this->getType())),
|
|
|
|
null,
|
|
|
|
'add'
|
|
|
|
);
|
|
|
|
} else {
|
2018-06-15 02:08:06 +02:00
|
|
|
$this->tabs(new ObjectTabs(
|
|
|
|
$this->getRequest()->getControllerName(),
|
|
|
|
$this->getAuth(),
|
|
|
|
$this->object
|
|
|
|
));
|
2017-08-21 13:20:47 +02:00
|
|
|
}
|
2021-08-16 11:43:09 +02:00
|
|
|
if ($this->object !== null) {
|
|
|
|
$this->addDeploymentLink();
|
|
|
|
}
|
2016-12-16 12:07:01 +01:00
|
|
|
}
|
2015-06-30 11:19:31 +02:00
|
|
|
}
|
|
|
|
|
2018-10-15 14:49:47 +02:00
|
|
|
/**
|
|
|
|
* @throws NotFoundError
|
|
|
|
*/
|
2015-06-30 11:19:31 +02:00
|
|
|
public function indexAction()
|
2015-07-30 10:04:12 +02:00
|
|
|
{
|
2017-08-16 08:08:08 +02:00
|
|
|
if (! $this->getRequest()->isApiRequest()) {
|
|
|
|
$this->redirectToPreviewForExternals()
|
|
|
|
->editAction();
|
2016-03-18 14:58:46 +01:00
|
|
|
}
|
2015-06-30 11:19:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function addAction()
|
|
|
|
{
|
2017-07-03 07:04:31 +02:00
|
|
|
$this->tabs()->activate('add');
|
2017-08-16 23:53:37 +02:00
|
|
|
$url = sprintf('director/%ss', $this->getPluralType());
|
2015-06-30 11:19:31 +02:00
|
|
|
|
2017-08-21 09:07:20 +02:00
|
|
|
$imports = $this->params->get('imports');
|
2017-08-16 23:53:37 +02:00
|
|
|
$form = $this->loadObjectForm()
|
2017-07-28 18:46:11 +02:00
|
|
|
->presetImports($imports)
|
2015-06-30 11:19:31 +02:00
|
|
|
->setSuccessUrl($url);
|
|
|
|
|
2017-08-21 09:07:20 +02:00
|
|
|
if ($oType = $this->params->get('type', 'object')) {
|
2017-07-03 07:04:31 +02:00
|
|
|
$form->setPreferredObjectType($oType);
|
2016-10-13 13:53:01 +02:00
|
|
|
}
|
2021-12-17 08:06:20 +01:00
|
|
|
if ($this->getTableName() === 'icinga_service_set'
|
|
|
|
&& $this->showNotInBranch($this->translate('Creating Service Sets'))
|
|
|
|
) {
|
|
|
|
$this->addTitle($this->translate('Create a new Service Set'));
|
|
|
|
return;
|
|
|
|
}
|
2016-10-13 13:53:01 +02:00
|
|
|
|
2017-07-03 07:04:31 +02:00
|
|
|
if ($oType === 'template') {
|
2021-12-14 10:46:52 +01:00
|
|
|
if ($this->showNotInBranch($this->translate('Creating Templates'))) {
|
|
|
|
$this->addTitle($this->translate('Create a new Template'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-08-16 23:53:37 +02:00
|
|
|
$this->addTemplate();
|
2016-10-13 13:53:01 +02:00
|
|
|
} else {
|
2017-08-16 23:53:37 +02:00
|
|
|
$this->addObject();
|
2016-10-13 13:53:01 +02:00
|
|
|
}
|
2015-07-30 09:26:02 +02:00
|
|
|
|
|
|
|
$form->handleRequest();
|
2017-07-03 07:04:31 +02:00
|
|
|
$this->content()->add($form);
|
2015-12-10 13:19:16 +01:00
|
|
|
}
|
|
|
|
|
2018-10-15 14:49:47 +02:00
|
|
|
/**
|
|
|
|
* @throws NotFoundError
|
|
|
|
*/
|
2017-08-16 23:53:37 +02:00
|
|
|
public function editAction()
|
2016-03-29 19:32:50 +02:00
|
|
|
{
|
2017-08-16 23:53:37 +02:00
|
|
|
$object = $this->requireObject();
|
|
|
|
$this->tabs()->activate('modify');
|
2021-12-14 10:46:52 +01:00
|
|
|
$this->addObjectTitle();
|
|
|
|
if ($object->isTemplate() && $this->showNotInBranch($this->translate('Modifying Templates'))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ($object->isApplyRule() && $this->showNotInBranch($this->translate('Modifying Apply Rules'))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->addObjectForm($object)
|
2017-08-16 23:53:37 +02:00
|
|
|
->addActionClone()
|
2018-10-15 14:49:47 +02:00
|
|
|
->addActionUsage()
|
|
|
|
->addActionBasket();
|
2016-03-29 19:32:50 +02:00
|
|
|
}
|
|
|
|
|
2018-10-15 14:49:47 +02:00
|
|
|
/**
|
|
|
|
* @throws NotFoundError
|
|
|
|
* @throws \Icinga\Security\SecurityException
|
|
|
|
*/
|
2017-08-16 23:53:37 +02:00
|
|
|
public function renderAction()
|
2017-07-25 10:18:07 +02:00
|
|
|
{
|
2017-10-12 16:26:50 +02:00
|
|
|
$this->assertTypePermission()
|
|
|
|
->assertPermission('director/showconfig');
|
2017-08-16 23:53:37 +02:00
|
|
|
$this->tabs()->activate('render');
|
|
|
|
$preview = new ObjectPreview($this->requireObject(), $this->getRequest());
|
2017-08-18 16:13:09 +02:00
|
|
|
if ($this->object->isExternal()) {
|
|
|
|
$this->addActionClone();
|
|
|
|
}
|
2019-05-27 17:27:18 +02:00
|
|
|
$this->addActionBasket();
|
2017-08-16 23:53:37 +02:00
|
|
|
$preview->renderTo($this);
|
2017-07-25 10:18:07 +02:00
|
|
|
}
|
|
|
|
|
2018-10-15 14:49:47 +02:00
|
|
|
/**
|
|
|
|
* @throws NotFoundError
|
|
|
|
*/
|
2015-12-10 13:19:16 +01:00
|
|
|
public function cloneAction()
|
|
|
|
{
|
2017-10-12 16:26:50 +02:00
|
|
|
$this->assertTypePermission();
|
2017-08-16 23:53:37 +02:00
|
|
|
$object = $this->requireObject();
|
2021-12-14 10:46:52 +01:00
|
|
|
$this->addTitle($this->translate('Clone: %s'), $object->getObjectName())
|
|
|
|
->addBackToObjectLink();
|
|
|
|
|
|
|
|
if ($object->isTemplate() && $this->showNotInBranch($this->translate('Cloning Templates'))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($object->isTemplate() && $this->showNotInBranch($this->translate('Cloning Apply Rules'))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-07-25 10:16:15 +02:00
|
|
|
$form = IcingaCloneObjectForm::load()
|
2021-08-25 11:28:51 +02:00
|
|
|
->setBranch($this->getBranch())
|
2017-08-16 23:53:37 +02:00
|
|
|
->setObject($object)
|
2020-11-30 10:17:44 +01:00
|
|
|
->setObjectBaseUrl($this->getObjectBaseUrl())
|
2017-07-25 10:16:15 +02:00
|
|
|
->handleRequest();
|
2017-08-16 23:53:37 +02:00
|
|
|
|
2017-08-18 16:13:09 +02:00
|
|
|
if ($object->isExternal()) {
|
|
|
|
$this->tabs()->activate('render');
|
|
|
|
} else {
|
|
|
|
$this->tabs()->activate('modify');
|
|
|
|
}
|
2021-12-14 10:46:52 +01:00
|
|
|
$this->content()->add($form);
|
2015-06-30 11:19:31 +02:00
|
|
|
}
|
|
|
|
|
2018-10-15 14:49:47 +02:00
|
|
|
/**
|
|
|
|
* @throws NotFoundError
|
|
|
|
* @throws \Icinga\Security\SecurityException
|
|
|
|
*/
|
2015-08-02 15:01:47 +02:00
|
|
|
public function fieldsAction()
|
|
|
|
{
|
2017-07-11 08:33:03 +02:00
|
|
|
$this->assertPermission('director/admin');
|
2017-07-29 00:12:34 +02:00
|
|
|
$object = $this->requireObject();
|
2015-08-02 15:01:47 +02:00
|
|
|
$type = $this->getType();
|
|
|
|
|
2017-07-06 09:04:32 +02:00
|
|
|
$this->addTitle(
|
2016-05-03 09:09:01 +02:00
|
|
|
$this->translate('Custom fields: %s'),
|
2017-07-29 00:12:34 +02:00
|
|
|
$object->getObjectName()
|
2015-08-02 15:01:47 +02:00
|
|
|
);
|
2017-07-29 00:12:34 +02:00
|
|
|
$this->tabs()->activate('fields');
|
2021-12-14 10:46:52 +01:00
|
|
|
if ($this->showNotInBranch($this->translate('Managing Fields'))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-13 21:41:46 +01:00
|
|
|
try {
|
|
|
|
$this->addFieldsFormAndTable($object, $type);
|
|
|
|
} catch (NestingError $e) {
|
2020-10-26 18:50:13 +01:00
|
|
|
$this->content()->add(Hint::error($e->getMessage()));
|
2019-02-13 21:41:46 +01:00
|
|
|
}
|
|
|
|
}
|
2015-08-02 15:01:47 +02:00
|
|
|
|
2019-02-13 21:41:46 +01:00
|
|
|
protected function addFieldsFormAndTable($object, $type)
|
|
|
|
{
|
2017-07-06 09:04:32 +02:00
|
|
|
$form = IcingaObjectFieldForm::load()
|
2017-06-22 00:44:31 +02:00
|
|
|
->setDb($this->db())
|
2015-09-14 16:54:43 +02:00
|
|
|
->setIcingaObject($object);
|
|
|
|
|
|
|
|
if ($id = $this->params->get('field_id')) {
|
2017-08-16 23:53:37 +02:00
|
|
|
$form->loadObject([
|
|
|
|
"${type}_id" => $object->id,
|
2015-10-13 17:58:50 +02:00
|
|
|
'datafield_id' => $id
|
2017-08-16 23:53:37 +02:00
|
|
|
]);
|
2016-02-28 10:58:42 +01:00
|
|
|
|
2017-07-06 09:04:32 +02:00
|
|
|
$this->actions()->add(Link::create(
|
2016-02-28 10:58:42 +01:00
|
|
|
$this->translate('back'),
|
2017-07-06 09:04:32 +02:00
|
|
|
$this->url()->without('field_id'),
|
2016-02-28 10:58:42 +01:00
|
|
|
null,
|
2017-07-06 09:04:32 +02:00
|
|
|
['class' => 'icon-left-big']
|
|
|
|
));
|
2015-09-14 16:54:43 +02:00
|
|
|
}
|
|
|
|
$form->handleRequest();
|
2017-08-16 23:53:37 +02:00
|
|
|
$this->content()->add($form);
|
|
|
|
$table = new IcingaObjectDatafieldTable($object);
|
2018-05-05 00:24:49 +02:00
|
|
|
$table->getAttributes()->set('data-base-target', '_self');
|
2017-08-16 23:53:37 +02:00
|
|
|
$table->renderTo($this);
|
2015-08-02 15:01:47 +02:00
|
|
|
}
|
|
|
|
|
2018-10-15 14:49:47 +02:00
|
|
|
/**
|
|
|
|
* @throws NotFoundError
|
|
|
|
* @throws \Icinga\Security\SecurityException
|
|
|
|
*/
|
2015-06-30 11:19:31 +02:00
|
|
|
public function historyAction()
|
|
|
|
{
|
2017-10-12 16:26:50 +02:00
|
|
|
$this
|
|
|
|
->assertTypePermission()
|
|
|
|
->assertPermission('director/audit')
|
2017-07-29 00:12:34 +02:00
|
|
|
->setAutorefreshInterval(10)
|
|
|
|
->tabs()->activate('history');
|
|
|
|
|
|
|
|
$name = $this->requireObject()->getObjectName();
|
|
|
|
$this->addTitle($this->translate('Activity Log: %s'), $name);
|
|
|
|
|
2016-03-16 22:50:35 +01:00
|
|
|
$db = $this->db();
|
2019-04-10 10:39:05 +02:00
|
|
|
$objectTable = $this->object->getTableName();
|
2019-01-11 12:24:29 +01:00
|
|
|
$table = (new ActivityLogTable($db))
|
2017-07-29 00:12:34 +02:00
|
|
|
->setLastDeployedId($db->getLastDeploymentActivityLogId())
|
2019-04-10 10:39:05 +02:00
|
|
|
->filterObject($objectTable, $name);
|
2019-01-11 12:24:29 +01:00
|
|
|
if ($host = $this->params->get('host')) {
|
|
|
|
$table->filterHost($host);
|
|
|
|
}
|
2021-10-05 23:10:50 +02:00
|
|
|
$this->showOptionalBranchActivity($table);
|
2019-01-11 12:24:29 +01:00
|
|
|
$table->renderTo($this);
|
2015-06-30 11:19:31 +02:00
|
|
|
}
|
|
|
|
|
2018-10-15 14:49:47 +02:00
|
|
|
/**
|
|
|
|
* @throws NotFoundError
|
|
|
|
*/
|
2017-07-11 08:33:03 +02:00
|
|
|
public function membershipAction()
|
|
|
|
{
|
2017-07-29 00:12:34 +02:00
|
|
|
$object = $this->requireObject();
|
|
|
|
if (! $object instanceof IcingaObjectGroup) {
|
2017-07-11 08:33:03 +02:00
|
|
|
throw new NotFoundError('Not Found');
|
|
|
|
}
|
|
|
|
|
2017-07-29 00:12:34 +02:00
|
|
|
$this
|
|
|
|
->addTitle($this->translate('Group membership: %s'), $object->getObjectName())
|
|
|
|
->setAutorefreshInterval(15)
|
|
|
|
->tabs()->activate('membership');
|
2017-07-11 08:33:03 +02:00
|
|
|
|
2017-07-29 00:12:34 +02:00
|
|
|
$type = substr($this->getType(), 0, -5);
|
|
|
|
GroupMemberTable::create($type, $this->db())
|
|
|
|
->setGroup($object)
|
|
|
|
->renderTo($this);
|
2017-07-11 08:33:03 +02:00
|
|
|
}
|
|
|
|
|
2018-10-15 14:49:47 +02:00
|
|
|
/**
|
|
|
|
* @return $this
|
|
|
|
* @throws NotFoundError
|
|
|
|
*/
|
2017-08-16 23:53:37 +02:00
|
|
|
protected function addObjectTitle()
|
|
|
|
{
|
|
|
|
$object = $this->requireObject();
|
|
|
|
$name = $object->getObjectName();
|
|
|
|
if ($object->isTemplate()) {
|
|
|
|
$this->addTitle($this->translate('Template: %s'), $name);
|
|
|
|
} else {
|
|
|
|
$this->addTitle($name);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2018-10-15 14:52:03 +02:00
|
|
|
/**
|
|
|
|
* @return $this
|
|
|
|
* @throws NotFoundError
|
|
|
|
*/
|
2017-08-16 23:53:37 +02:00
|
|
|
protected function addActionUsage()
|
|
|
|
{
|
|
|
|
$type = $this->getType();
|
|
|
|
$object = $this->requireObject();
|
2018-10-15 14:52:03 +02:00
|
|
|
if ($object->isTemplate() && $type !== 'serviceSet') {
|
2017-08-16 23:53:37 +02:00
|
|
|
$this->actions()->add([
|
|
|
|
Link::create(
|
|
|
|
$this->translate('Usage'),
|
|
|
|
"director/${type}template/usage",
|
|
|
|
['name' => $object->getObjectName()],
|
|
|
|
['class' => 'icon-sitemap']
|
|
|
|
)
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function addActionClone()
|
|
|
|
{
|
|
|
|
$this->actions()->add(Link::create(
|
|
|
|
$this->translate('Clone'),
|
2020-11-30 10:17:44 +01:00
|
|
|
$this->getObjectBaseUrl() . '/clone',
|
2017-08-16 23:53:37 +02:00
|
|
|
$this->object->getUrlParams(),
|
|
|
|
array('class' => 'icon-paste')
|
|
|
|
));
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2018-10-15 14:49:47 +02:00
|
|
|
/**
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
protected function addActionBasket()
|
|
|
|
{
|
|
|
|
if ($this->hasBasketSupport()) {
|
|
|
|
$object = $this->object;
|
|
|
|
if ($object instanceof ExportInterface) {
|
2019-05-27 17:27:18 +02:00
|
|
|
if ($object instanceof IcingaCommand) {
|
|
|
|
if ($object->isExternal()) {
|
|
|
|
$type = 'ExternalCommand';
|
|
|
|
} elseif ($object->isTemplate()) {
|
|
|
|
$type = 'CommandTemplate';
|
|
|
|
} else {
|
|
|
|
$type = 'Command';
|
|
|
|
}
|
|
|
|
} elseif ($object instanceof IcingaServiceSet) {
|
2018-10-25 08:38:24 +02:00
|
|
|
$type = 'ServiceSet';
|
|
|
|
} elseif ($object->isTemplate()) {
|
2018-10-30 16:41:49 +01:00
|
|
|
$type = ucfirst($this->getType()) . 'Template';
|
2018-10-15 14:49:47 +02:00
|
|
|
} elseif ($object->isGroup()) {
|
|
|
|
$type = ucfirst($this->getType());
|
|
|
|
} else {
|
|
|
|
// Command? Sure?
|
|
|
|
$type = ucfirst($this->getType());
|
|
|
|
}
|
|
|
|
$this->actions()->add(Link::create(
|
|
|
|
$this->translate('Add to Basket'),
|
|
|
|
'director/basket/add',
|
|
|
|
[
|
|
|
|
'type' => $type,
|
|
|
|
'names' => $object->getUniqueIdentifier()
|
|
|
|
],
|
|
|
|
['class' => 'icon-tag']
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2017-08-16 23:53:37 +02:00
|
|
|
protected function addTemplate()
|
2015-06-30 11:19:31 +02:00
|
|
|
{
|
2017-08-16 23:53:37 +02:00
|
|
|
$this->assertPermission('director/admin');
|
|
|
|
$this->addTitle(
|
|
|
|
$this->translate('Add new Icinga %s template'),
|
|
|
|
$this->getTranslatedType()
|
2015-06-30 11:19:31 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-08-16 23:53:37 +02:00
|
|
|
protected function addObject()
|
|
|
|
{
|
2017-10-12 16:26:50 +02:00
|
|
|
$this->assertTypePermission();
|
2017-08-16 23:53:37 +02:00
|
|
|
$imports = $this->params->get('imports');
|
|
|
|
if (is_string($imports) && strlen($imports)) {
|
|
|
|
$this->addTitle(
|
|
|
|
$this->translate('Add %s: %s'),
|
|
|
|
$this->getTranslatedType(),
|
|
|
|
$imports
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$this->addTitle(
|
|
|
|
$this->translate('Add new Icinga %s'),
|
|
|
|
$this->getTranslatedType()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function redirectToPreviewForExternals()
|
|
|
|
{
|
|
|
|
if ($this->object
|
|
|
|
&& $this->object->isExternal()
|
|
|
|
&& ! in_array($this->object->getShortTableName(), $this->allowedExternals)
|
|
|
|
) {
|
|
|
|
$this->redirectNow(
|
|
|
|
$this->getRequest()->getUrl()->setPath(sprintf('director/%s/render', $this->getType()))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getType()
|
|
|
|
{
|
|
|
|
if ($this->type === null) {
|
|
|
|
// Strip final 's' and upcase an eventual 'group'
|
|
|
|
$this->type = preg_replace(
|
|
|
|
array('/group$/', '/period$/', '/argument$/', '/apiuser$/', '/set$/'),
|
|
|
|
array('Group', 'Period', 'Argument', 'ApiUser', 'Set'),
|
|
|
|
$this->getRequest()->getControllerName()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->type;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getPluralType()
|
|
|
|
{
|
|
|
|
return $this->getType() . 's';
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getTranslatedType()
|
|
|
|
{
|
|
|
|
return $this->translate(ucfirst($this->getType()));
|
|
|
|
}
|
|
|
|
|
2017-10-12 16:26:50 +02:00
|
|
|
protected function assertTypePermission()
|
|
|
|
{
|
2021-04-23 12:33:42 +02:00
|
|
|
$type = strtolower($this->getPluralType());
|
|
|
|
// TODO: Check getPluralType usage, fix it there.
|
|
|
|
if ($type === 'scheduleddowntimes') {
|
|
|
|
$type = 'scheduled-downtimes';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->assertPermission("director/$type");
|
2017-10-12 16:26:50 +02:00
|
|
|
}
|
|
|
|
|
2021-10-05 23:10:50 +02:00
|
|
|
protected function loadOptionalObject()
|
2017-08-16 08:08:08 +02:00
|
|
|
{
|
2021-10-05 23:10:50 +02:00
|
|
|
if ($this->params->get('uuid') || null !== $this->params->get('name') || $this->params->get('id')) {
|
2017-08-16 08:08:08 +02:00
|
|
|
$this->loadObject();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-05 23:10:50 +02:00
|
|
|
/**
|
|
|
|
* @return ?UuidInterface
|
|
|
|
* @throws InvalidPropertyException
|
|
|
|
* @throws NotFoundError
|
|
|
|
*/
|
|
|
|
protected function getUuidFromUrl()
|
2015-06-30 11:19:31 +02:00
|
|
|
{
|
2021-10-05 23:10:50 +02:00
|
|
|
$key = null;
|
|
|
|
if ($uuid = $this->params->get('uuid')) {
|
|
|
|
$key = Uuid::fromString($uuid);
|
|
|
|
} elseif ($id = $this->params->get('id')) {
|
2021-08-16 11:43:09 +02:00
|
|
|
$key = (int) $id;
|
|
|
|
} elseif (null !== ($name = $this->params->get('name'))) {
|
|
|
|
$key = $name;
|
|
|
|
}
|
|
|
|
if ($key === null) {
|
2021-10-05 23:10:50 +02:00
|
|
|
$request = $this->getRequest();
|
|
|
|
if ($request->isApiRequest() && $request->isGet()) {
|
2021-08-16 11:43:09 +02:00
|
|
|
$this->getResponse()->setHttpResponseCode(422);
|
2016-03-06 20:24:47 +01:00
|
|
|
|
2021-08-16 11:43:09 +02:00
|
|
|
throw new InvalidPropertyException(
|
|
|
|
'Cannot load object, missing parameters'
|
|
|
|
);
|
2016-03-06 14:20:49 +01:00
|
|
|
}
|
2016-02-27 12:23:00 +01:00
|
|
|
|
2021-10-05 23:10:50 +02:00
|
|
|
return null;
|
2021-08-16 11:43:09 +02:00
|
|
|
}
|
2021-10-05 23:10:50 +02:00
|
|
|
|
|
|
|
return $this->requireUuid($key);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function loadObject()
|
|
|
|
{
|
|
|
|
if ($this->object) {
|
|
|
|
throw new ProgrammingError('Loading an object twice is not very efficient');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->object = $this->loadSpecificObject($this->getTableName(), $this->getUuidFromUrl(), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function loadSpecificObject($tableName, $key, $showHint = false)
|
|
|
|
{
|
2021-08-16 11:43:09 +02:00
|
|
|
$branch = $this->getBranch();
|
2021-10-05 23:10:50 +02:00
|
|
|
$branchedObject = BranchedObject::load($this->db(), $tableName, $key, $branch);
|
|
|
|
$object = $branchedObject->getBranchedDbObject($this->db());
|
|
|
|
assert($object instanceof IcingaObject);
|
|
|
|
$object->setBeingLoadedFromDb();
|
2021-08-16 11:43:09 +02:00
|
|
|
if (! $this->allowsObject($object)) {
|
|
|
|
throw new NotFoundError('No such object available');
|
|
|
|
}
|
2021-12-14 10:46:52 +01:00
|
|
|
if ($showHint && $branch->isBranch() && $object->isObject() && ! $this->getRequest()->isApiRequest()) {
|
2021-10-05 23:10:50 +02:00
|
|
|
$this->content()->add(new BranchedObjectHint($branch, $this->Auth(), $branchedObject));
|
2015-06-30 11:19:31 +02:00
|
|
|
}
|
|
|
|
|
2021-10-05 23:10:50 +02:00
|
|
|
return $object;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function requireUuid($key)
|
|
|
|
{
|
|
|
|
if (! $key instanceof UuidInterface) {
|
|
|
|
$key = UuidLookup::findUuidForKey($key, $this->getTableName(), $this->db(), $this->getBranch());
|
|
|
|
if ($key === null) {
|
|
|
|
throw new NotFoundError('No such object available');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $key;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getTableName()
|
|
|
|
{
|
|
|
|
return DbObjectTypeRegistry::tableNameByType($this->getType());
|
2015-06-30 11:19:31 +02:00
|
|
|
}
|
2016-02-03 00:54:00 +01:00
|
|
|
|
2017-08-16 23:53:37 +02:00
|
|
|
protected function addDeploymentLink()
|
2017-07-31 10:34:57 +02:00
|
|
|
{
|
2017-10-20 10:23:23 +02:00
|
|
|
try {
|
|
|
|
$info = new DeploymentInfo($this->db());
|
|
|
|
$info->setObject($this->object);
|
|
|
|
|
|
|
|
if (! $this->getRequest()->isApiRequest()) {
|
2021-08-16 11:43:09 +02:00
|
|
|
if ($this->getBranch()->isBranch()) {
|
|
|
|
$this->actions()->add($this->linkToMergeBranch($this->getBranch()));
|
|
|
|
} else {
|
|
|
|
$this->actions()->add(
|
|
|
|
DeploymentLinkForm::create(
|
|
|
|
$this->db(),
|
|
|
|
$info,
|
|
|
|
$this->Auth(),
|
|
|
|
$this->api()
|
|
|
|
)->handleRequest()
|
|
|
|
);
|
|
|
|
}
|
2017-10-20 10:23:23 +02:00
|
|
|
}
|
|
|
|
} catch (IcingaException $e) {
|
|
|
|
// pass (deployment may not be set up yet)
|
2017-07-31 10:34:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-16 11:43:09 +02:00
|
|
|
protected function linkToMergeBranch(Branch $branch)
|
|
|
|
{
|
2021-08-23 08:55:30 +02:00
|
|
|
$link = Branch::requireHook()->linkToBranch($branch, $this->Auth(), $this->translate('Merge'));
|
2021-08-16 11:43:09 +02:00
|
|
|
if ($link instanceof Link) {
|
|
|
|
$link->addAttributes(['class' => 'icon-flapping']);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $link;
|
|
|
|
}
|
|
|
|
|
2017-08-16 23:53:37 +02:00
|
|
|
protected function addBackToObjectLink()
|
2016-02-03 00:54:00 +01:00
|
|
|
{
|
2019-03-19 15:29:08 +01:00
|
|
|
$params = [
|
2021-10-18 07:27:28 +02:00
|
|
|
'uuid' => $this->object->getUniqueId()->toString(),
|
2019-03-19 15:29:08 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
if ($this->object instanceof IcingaService) {
|
|
|
|
if (($host = $this->object->get('host')) !== null) {
|
|
|
|
$params['host'] = $host;
|
2019-03-19 15:43:20 +01:00
|
|
|
} elseif (($set = $this->object->get('service_set')) !== null) {
|
2019-03-19 15:29:08 +01:00
|
|
|
$params['set'] = $set;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-16 23:53:37 +02:00
|
|
|
$this->actions()->add(Link::create(
|
|
|
|
$this->translate('back'),
|
2020-11-30 10:17:44 +01:00
|
|
|
$this->getObjectBaseUrl(),
|
2019-03-19 15:29:08 +01:00
|
|
|
$params,
|
2017-08-16 23:53:37 +02:00
|
|
|
['class' => 'icon-left-big']
|
|
|
|
));
|
2016-02-03 00:54:00 +01:00
|
|
|
|
2017-08-16 23:53:37 +02:00
|
|
|
return $this;
|
|
|
|
}
|
2016-02-03 00:54:00 +01:00
|
|
|
|
2017-08-16 23:53:37 +02:00
|
|
|
protected function addObjectForm(IcingaObject $object = null)
|
|
|
|
{
|
|
|
|
$form = $this->loadObjectForm($object);
|
|
|
|
$this->content()->add($form);
|
|
|
|
$form->handleRequest();
|
|
|
|
return $this;
|
|
|
|
}
|
2016-02-03 00:54:00 +01:00
|
|
|
|
2017-08-16 23:53:37 +02:00
|
|
|
protected function loadObjectForm(IcingaObject $object = null)
|
|
|
|
{
|
|
|
|
/** @var DirectorObjectForm $class */
|
|
|
|
$class = sprintf(
|
|
|
|
'Icinga\\Module\\Director\\Forms\\Icinga%sForm',
|
|
|
|
ucfirst($this->getType())
|
|
|
|
);
|
|
|
|
|
|
|
|
$form = $class::load()
|
|
|
|
->setDb($this->db())
|
|
|
|
->setAuth($this->Auth());
|
2016-02-03 00:54:00 +01:00
|
|
|
|
2017-08-16 23:53:37 +02:00
|
|
|
if ($object !== null) {
|
|
|
|
$form->setObject($object);
|
2016-02-03 00:54:00 +01:00
|
|
|
}
|
2021-08-16 11:43:09 +02:00
|
|
|
if (true || $form->supportsBranches()) {
|
2021-08-25 13:00:56 +02:00
|
|
|
$form->setBranch($this->getBranch());
|
2021-08-16 11:43:09 +02:00
|
|
|
}
|
2017-08-16 23:53:37 +02:00
|
|
|
|
|
|
|
$this->onObjectFormLoaded($form);
|
|
|
|
|
|
|
|
return $form;
|
2016-02-03 00:54:00 +01:00
|
|
|
}
|
2016-02-17 19:22:36 +01:00
|
|
|
|
2020-11-30 10:17:44 +01:00
|
|
|
protected function getObjectBaseUrl()
|
|
|
|
{
|
|
|
|
return $this->objectBaseUrl ?: 'director/' . strtolower($this->getType());
|
|
|
|
}
|
|
|
|
|
2018-10-15 14:49:47 +02:00
|
|
|
protected function hasBasketSupport()
|
|
|
|
{
|
|
|
|
return $this->object->isTemplate() || $this->object->isGroup();
|
|
|
|
}
|
|
|
|
|
2017-08-16 23:53:37 +02:00
|
|
|
protected function onObjectFormLoaded(DirectorObjectForm $form)
|
2016-02-27 12:23:00 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-10-15 14:49:47 +02:00
|
|
|
/**
|
|
|
|
* @return IcingaObject
|
|
|
|
* @throws NotFoundError
|
|
|
|
*/
|
2016-02-17 19:22:36 +01:00
|
|
|
protected function requireObject()
|
|
|
|
{
|
|
|
|
if (! $this->object) {
|
|
|
|
$this->getResponse()->setHttpResponseCode(404);
|
2017-07-31 10:34:57 +02:00
|
|
|
if (null === $this->params->get('name')) {
|
2016-02-17 19:22:36 +01:00
|
|
|
throw new NotFoundError('You need to pass a "name" parameter to access a specific object');
|
|
|
|
} else {
|
|
|
|
throw new NotFoundError('No such object available');
|
|
|
|
}
|
|
|
|
}
|
2016-05-25 08:11:53 +02:00
|
|
|
|
2017-07-29 00:12:34 +02:00
|
|
|
return $this->object;
|
2016-03-18 11:44:48 +01:00
|
|
|
}
|
2021-10-05 23:10:50 +02:00
|
|
|
|
|
|
|
protected function showOptionalBranchActivity($activityTable)
|
|
|
|
{
|
|
|
|
$branch = $this->getBranch();
|
|
|
|
if ($branch->isBranch() && (int) $this->params->get('page', '1') === 1) {
|
|
|
|
$table = new BranchActivityTable($branch->getUuid(), $this->db(), $this->object->getUniqueId());
|
|
|
|
if (count($table) > 0) {
|
|
|
|
$this->content()->add(Hint::info(Html::sprintf($this->translate(
|
|
|
|
'The following modifications are visible in this %s only...'
|
|
|
|
), Branch::requireHook()->linkToBranch(
|
|
|
|
$branch,
|
|
|
|
$this->Auth(),
|
|
|
|
$this->translate('configuration branch')
|
|
|
|
))));
|
|
|
|
$this->content()->add($table);
|
|
|
|
if (count($activityTable) === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$this->content()->add(Html::tag('br'));
|
|
|
|
$this->content()->add(Hint::ok($this->translate(
|
|
|
|
'...and the modifications below are already in the main branch:'
|
|
|
|
)));
|
|
|
|
$this->content()->add(Html::tag('br'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-06-30 11:19:31 +02:00
|
|
|
}
|