1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
|
<?php
namespace Icinga\Module\Director\Controllers;
use Exception;
use Icinga\Exception\NotFoundError;
use Icinga\Module\Director\Auth\Permission;
use Icinga\Module\Director\Data\Db\DbObjectTypeRegistry;
use Icinga\Module\Director\Db\Branch\UuidLookup;
use Icinga\Module\Director\Forms\IcingaServiceForm;
use Icinga\Module\Director\Objects\IcingaObject;
use Icinga\Module\Director\Web\Controller\ObjectController;
use Icinga\Module\Director\Objects\IcingaService;
use Icinga\Module\Director\Objects\IcingaHost;
use Icinga\Module\Director\Web\Form\DirectorObjectForm;
use Icinga\Module\Director\Web\Table\IcingaAppliedServiceTable;
use Icinga\Web\Widget\Tab;
use gipfl\IcingaWeb2\Link;
use gipfl\IcingaWeb2\Widget\Tabs;
class ServiceController extends ObjectController
{
/** @var IcingaHost */
protected $host;
protected $set;
protected $apply;
protected function checkDirectorPermissions()
{
if (
$this->host
&& $this->object
&& $this->backend()->canModifyService($this->host->getObjectName(), $this->object->getObjectName())
) {
return;
}
$this->assertPermission(Permission::HOSTS);
}
public function init()
{
// This happens in parent::init() too, but is required to take place before the next two lines
$this->enableStaticObjectLoader($this->getTableName());
// Hint: having Host and Set loaded first is important for UUID lookups with legacy URLs
$this->host = $this->getOptionalRelatedObjectFromParams('host', 'host');
$this->set = $this->getOptionalRelatedObjectFromParams('service_set', 'set');
parent::init();
$this->addOptionalHostTabs();
$this->addOptionalSetTabs();
}
protected function getOptionalRelatedObjectFromParams($type, $parameter)
{
if ($id = $this->params->get("{$parameter}_id")) {
$key = (int) $id;
} else {
$key = $this->params->get($parameter);
}
if ($key !== null) {
$table = DbObjectTypeRegistry::tableNameByType($type);
$key = UuidLookup::findUuidForKey($key, $table, $this->db(), $this->getBranch());
return $this->loadSpecificObject($table, $key);
}
return null;
}
protected function loadOptionalObject(): void
{
parent::loadOptionalObject();
if ($this->object) {
if ($this->host === null) {
$this->host = $this->loadOptionalRelatedObject($this->object, 'host');
}
if ($this->set === null) {
$this->set = $this->loadOptionalRelatedObject($this->object, 'service_set');
}
}
}
protected function loadOptionalRelatedObject(IcingaObject $object, $relation)
{
$key = $object->getUnresolvedRelated($relation);
if ($key === null) {
if ($key = $object->get("{$relation}_id")) {
$key = (int) $key;
} else {
$key = $object->get($relation);
// We reach this when accessing Service Template Fields
}
}
if ($key === null) {
return null;
}
$table = DbObjectTypeRegistry::tableNameByType($relation);
$uuid = UuidLookup::findUuidForKey($key, $table, $this->db(), $this->getBranch());
return $this->loadSpecificObject($table, $uuid);
}
protected function addParamToTabs($name, $value)
{
foreach ($this->tabs()->getTabs() as $tab) {
/** @var Tab $tab */
$tab->getUrl()->setParam($name, $value);
}
return $this;
}
public function addAction()
{
parent::addAction();
if ($this->host) {
// TODO: use setTitle. And figure out, where we use this old route.
$this->view->title = $this->host->object_name . ': ' . $this->view->title;
} elseif ($this->set) {
$this->view->title = sprintf(
$this->translate('Add a service to "%s"'),
$this->set->object_name
);
} elseif ($this->apply) {
$this->view->title = sprintf(
$this->translate('Apply "%s"'),
$this->apply->object_name
);
}
}
protected function onObjectFormLoaded(DirectorObjectForm $form)
{
if ($this->set) {
/** @var IcingaServiceForm$form */
$form->setServiceSet($this->set);
}
if ($this->object === null && $this->apply) {
$form->createApplyRuleFor($this->apply);
}
}
public function editAction()
{
$this->tabs()->activate('modify');
/** @var IcingaService $object */
$object = $this->object;
$this->addTitle($object->getObjectName());
if ($object->isTemplate() && $this->showNotInBranch($this->translate('Modifying Templates'))) {
return;
}
$form = IcingaServiceForm::load()->setDb($this->db());
$form->setBranch($this->getBranch());
if ($this->host) {
$this->actions()->add(Link::create(
$this->translate('back'),
'director/host/services',
['uuid' => $this->host->getUniqueId()->toString()],
['class' => 'icon-left-big']
));
$form->setHost($this->host);
}
if ($this->set) {
$form->setServiceSet($this->set);
}
if ($this->host && $object->usesVarOverrides()) {
$fake = IcingaService::create(array(
'object_type' => 'object',
'host_id' => $object->get('host_id'),
'imports' => $object,
'object_name' => $object->object_name,
'use_var_overrides' => 'y',
'vars' => $this->host->getOverriddenServiceVars($object->object_name),
), $this->db());
$form->setObject($fake);
} else {
$form->setObject($object);
}
$form->handleRequest();
$this->addActionClone();
if ($this->host) {
$this->view->subtitle = sprintf(
$this->translate('(on %s)'),
$this->host->object_name
);
}
try {
if (
$object->isTemplate()
&& $object->getResolvedProperty('check_command_id')
) {
$this->view->actionLinks .= ' ' . $this->view->qlink(
'Create apply-rule',
'director/service/add',
array('apply' => $object->object_name),
array('class' => 'icon-plus')
);
}
} catch (Exception $e) {
// ignore the error, show no apply link
}
$this->content()->add($form);
}
public function assignAction()
{
// TODO: figure out whether and where we link to this
/** @var IcingaService $service */
$service = $this->object;
$this->actions()->add(new Link(
$this->translate('back'),
$this->getRequest()->getUrl()->without('rule_id'),
null,
array('class' => 'icon-left-big')
));
$this->tabs()->activate('applied');
$this->addTitle(
$this->translate('Apply: %s'),
$service->getObjectName()
);
$table = (new IcingaAppliedServiceTable($this->db()))
->setService($service);
$table->getAttributes()->set('data-base-target', '_self');
$this->content()->add($table);
}
protected function getLegacyKey()
{
if ($key = $this->params->get('id')) {
$key = (int) $key;
} else {
$key = $this->params->get('name');
}
if ($key === null) {
throw new \InvalidArgumentException('uuid, name or id required');
}
return $key;
}
protected function loadObject()
{
if ($this->params->has('uuid')) {
parent::loadObject();
return;
}
$key = $this->getLegacyKey();
// Hint: not passing 'object' as type, we still have name-based links in previews and similar
$uuid = UuidLookup::findServiceUuid($this->db(), $this->getBranch(), null, $key, $this->host, $this->set);
if ($uuid === null) {
if (! $this->params->get('allowOverrides')) {
throw new NotFoundError('Not found');
}
} else {
$this->params->set('uuid', $uuid->toString());
parent::loadObject();
}
}
protected function addOptionalHostTabs()
{
if ($this->host === null) {
return;
}
$hostname = $this->host->getObjectName();
$tabs = new Tabs();
$urlParams = ['uuid' => $this->host->getUniqueId()->toString()];
$tabs->add('host', [
'url' => 'director/host',
'urlParams' => $urlParams,
'label' => $this->translate('Host'),
])->add('services', [
'url' => 'director/host/services',
'urlParams' => $urlParams,
'label' => $this->translate('Services'),
]);
$this->addParamToTabs('host', $hostname);
$this->controls()->prependTabs($tabs);
}
protected function addOptionalSetTabs()
{
if ($this->set === null) {
return;
}
$setName = $this->set->getObjectName();
$tabs = new Tabs();
$tabs->add('set', [
'url' => 'director/serviceset',
'urlParams' => ['name' => $setName],
'label' => $this->translate('ServiceSet'),
])->add('services', [
'url' => 'director/serviceset/services',
'urlParams' => ['name' => $setName],
'label' => $this->translate('Services'),
]);
$this->addParamToTabs('serviceset', $setName);
$this->controls()->prependTabs($tabs);
}
}
|