Host/Service/ObjectController: use more ipl

This commit is contained in:
Thomas Gelf 2017-07-03 07:04:31 +02:00
parent 20310a0c9e
commit 0cf6676c3c
6 changed files with 198 additions and 141 deletions

View File

@ -15,6 +15,7 @@ use Icinga\Module\Director\Restriction\HostgroupRestriction;
use Icinga\Module\Director\Util;
use Icinga\Module\Director\Web\Controller\ObjectController;
use Icinga\Web\Url;
use ipl\Html\Link;
class HostController extends ObjectController
{
@ -22,7 +23,7 @@ class HostController extends ObjectController
{
parent::init();
if ($this->object) {
$tabs = $this->getTabs();
$tabs = $this->tabs();
$tabs->add('services', array(
'url' => 'director/host/services',
'urlParams' => array('name' => $this->object->object_name),
@ -77,7 +78,7 @@ class HostController extends ObjectController
$host = $this->object;
$mon = $this->monitoring();
if ($host->isObject() && $mon->isAvailable() && $mon->hasHost($host->object_name)) {
$this->view->actionLinks .= ' ' . $this->view->qlink(
$this->actions()->add(Link::create(
$this->translate('Show'),
'monitoring/host/show',
array('host' => $host->object_name),
@ -85,7 +86,7 @@ class HostController extends ObjectController
'class' => 'icon-globe critical',
'data-base-target' => '_next'
)
);
));
}
}

View File

@ -113,7 +113,7 @@ class ServiceController extends ObjectController
public function editAction()
{
$this->getTabs()->activate('modify');
$this->tabs()->activate('modify');
/** @var IcingaService $object */
$object = $this->object;
@ -127,7 +127,7 @@ class ServiceController extends ObjectController
);
}
$this->view->form = $form = $this
$form = $this
->loadForm('icingaService')
->setDb($this->db());
@ -146,7 +146,7 @@ class ServiceController extends ObjectController
$form->setObject($object);
}
$this->view->form->handleRequest();
$form->handleRequest();
$this->view->actionLinks .= $this->createCloneLink();
$this->view->title = $object->object_name;
@ -172,7 +172,8 @@ class ServiceController extends ObjectController
// ignore the error, show no apply link
}
$this->setViewScript('object/form');
$this->content()->add($form);
// $this->setViewScript('object/form');
}
public function assignAction()

View File

@ -1,29 +0,0 @@
<div class="controls">
<?= $this->tabs ?>
<h1><?= $this->escape($this->title) ?></h1>
<span class="action-links">
<?= $this->actionLinks ?>
</span>
</div>
<div class="content">
<?php if ($this->object->isDisabled()): ?>
<p class="error"><?= $this->translate('This object will not be deployed as it has been disabled') ?></p>
<?php endif ?>
<?php if ($this->isExternal): ?>
<p><?= $this->translate(
'This is an external object. It has been imported from Icinga 2 throught the'
. ' Core API and cannot be managed with the Icinga Director. It is however'
. ' perfectly valid to create objects using this or referring to this object.'
. ' You might also want to define related Fields to make work based on this'
. ' object more enjoyable'
) ?></p>
<?php endif ?>
<?php foreach ($this->config->getFiles() as $filename => $file): ?>
<?php if (! $this->isExternal): ?><h2><?= $this->escape($filename) ?></h2><?php endif ?>
<pre<?php if ($this->object->isDisabled()): ?> class="disabled"<?php elseif ($this->isExternal): ?> class="logfile"<?php endif ?>>
<?= $this->escape($file->getContent()) ?>
</pre>
<?php endforeach ?>
</div>

View File

@ -12,7 +12,7 @@ use Icinga\Module\Director\Web\Controller\Extension\CoreApi;
use Icinga\Module\Director\Web\Controller\Extension\DirectorDb;
use Icinga\Module\Director\Web\Controller\Extension\RestApi;
use Icinga\Module\Director\Web\Form\FormLoader;
use Icinga\Module\Director\Web\Form\QuickBaseForm;
use Icinga\Module\Director\Web\Form\QuickForm;
use Icinga\Module\Director\Web\Table\QuickTable;
use Icinga\Module\Director\Web\Table\TableLoader;
use Icinga\Security\SecurityException;
@ -99,7 +99,7 @@ abstract class ActionController extends Controller implements ControlsAndContent
/**
* @param string $name
*
* @return QuickBaseForm
* @return QuickForm
*/
public function loadForm($name)
{

View File

@ -11,6 +11,9 @@ use Icinga\Module\Director\Exception\NestingError;
use Icinga\Module\Director\Objects\IcingaObject;
use Icinga\Module\Director\Web\Controller\Extension\ObjectRestrictions;
use Icinga\Module\Director\Web\Form\DirectorObjectForm;
use Icinga\Module\Director\Web\Tabs\ObjectTabs;
use ipl\Html\Html;
use ipl\Html\Link;
abstract class ObjectController extends ActionController
{
@ -51,54 +54,10 @@ abstract class ObjectController extends ActionController
}
$type = strtolower($this->getType());
if ($object = $this->loadObject()) {
$this->beforeTabs();
$params = $object->getUrlParams();
if ($object->isExternal()
&& ! in_array($object->getShortTableName(), $this->allowedExternals)
) {
$tabs = $this->getTabs();
} else {
$tabs = $this->getTabs()->add('modify', array(
'url' => sprintf('director/%s', $type),
'urlParams' => $params,
'label' => $this->translate(ucfirst($type))
));
}
if ($this->hasPermission('director/showconfig')) {
$tabs->add('render', array(
'url' => sprintf('director/%s/render', $type),
'urlParams' => $params,
'label' => $this->translate('Preview'),
));
}
if ($this->hasPermission('director/audit')) {
$tabs->add('history', array(
'url' => sprintf('director/%s/history', $type),
'urlParams' => $params,
'label' => $this->translate('History')
));
}
if ($this->hasPermission('director/admin') && $this->hasFields()) {
$tabs->add('fields', array(
'url' => sprintf('director/%s/fields', $type),
'urlParams' => $params,
'label' => $this->translate('Fields')
));
}
} else {
$this->beforeTabs();
$this->getTabs()->add('add', array(
'url' => sprintf('director/%s/add', $type),
'label' => sprintf($this->translate('Add %s'), ucfirst($type)),
));
if ($name = $this->params->get('name')) {
$this->loadObject();
}
$this->tabs(new ObjectTabs($type, $this->getAuth(), $this->object));
}
public function indexAction()
@ -122,10 +81,12 @@ abstract class ObjectController extends ActionController
public function renderAction()
{
$this->assertPermission('director/showconfig');
$type = $this->getType();
$this->getTabs()->activate('render');
$this->tabs()->activate('render');
$object = $this->object;
$this->view->isExternal = $object->isExternal();
$this->addTitle(
$this->translate('Config preview: %s'),
$object->object_name
);
if ($this->params->shift('resolved')) {
$object = $object::fromPlainObject(
@ -133,62 +94,84 @@ abstract class ObjectController extends ActionController
$object->getConnection()
);
$this->view->actionLinks = $this->view->qlink(
$this->actions()->add(Link::create(
$this->translate('Show normal'),
$this->getRequest()->getUrl()->without('resolved'),
null,
array('class' => 'icon-resize-small state-warning')
);
['class' => 'icon-resize-small state-warning']
));
} else {
try {
if ($object->supportsImports() && $object->imports()->count() > 0) {
$this->view->actionLinks = $this->view->qlink(
$this->actions()->add(Link::create(
$this->translate('Show resolved'),
$this->getRequest()->getUrl()->with('resolved', true),
null,
array('class' => 'icon-resize-full')
);
['class' => 'icon-resize-full']
));
}
} catch (NestingError $e) {
// No resolve link with nesting errors
}
}
$this->view->object = $object;
$this->view->config = $object->toSingleIcingaConfig();
$content = $this->content();
if ($object->isDisabled()) {
$content->add(Html::p(
['class' => 'error'],
$this->translate('This object will not be deployed as it has been disabled')
));
}
if ($object->isExternal()) {
$content->add(Html::p($this->translate((
'This is an external object. It has been imported from Icinga 2 throught the'
. ' Core API and cannot be managed with the Icinga Director. It is however'
. ' perfectly valid to create objects using this or referring to this object.'
. ' You might also want to define related Fields to make work based on this'
. ' object more enjoyable'
))));
}
$config = $object->toSingleIcingaConfig();
$this->view->title = sprintf(
$this->translate('Config preview: %s'),
$object->object_name
);
$this->setViewScript('object/show');
foreach ($config->getFiles() as $filename => $file) {
if (! $object->isExternal()) {
$content->add(Html::h2($filename));
}
$classes = array();
if ($object->isDisabled()) {
$classes[] = 'disabled';
} elseif ($object->isExternal()) {
$classes[] = 'logfile';
}
$content->add(Html::pre(['class' => $classes], $file->getContent()));
}
}
public function editAction()
{
$object = $this->object;
$this->getTabs()->activate('modify');
$ltype = $this->getType();
$type = ucfirst($ltype);
$this->addTitle($object->object_name);
$this->tabs()->activate('modify');
$formName = 'icinga' . $type;
$this->view->form = $form = $this->loadForm($formName)
$formName = 'icinga' . ucfirst($this->getType());
$this->content()->add($form = $this->loadForm($formName)
->setDb($this->db())
->setApi($this->getApiIfAvailable());
/** @var DirectorObjectForm */
$form->setObject($object);
$form->setAuth($this->Auth());
->setAuth($this->Auth())
->setApi($this->getApiIfAvailable())
->setObject($object)
->setAuth($this->Auth())
->handleRequest()
$this->view->title = $object->object_name;
$this->view->form->handleRequest();
);
$this->view->actionLinks = $this->createCloneLink();
$this->setViewScript('object/form');
$this->actions()->add($this->createCloneLink());
}
protected function createCloneLink()
{
return $this->view->qlink(
return Link::create(
$this->translate('Clone'),
'director/' . $this->getType() .'/clone',
$this->object->getUrlParams(),
@ -198,7 +181,7 @@ abstract class ObjectController extends ActionController
public function addAction()
{
$this->getTabs()->activate('add');
$this->tabs()->activate('add');
$type = $this->getType();
$ltype = strtolower($type);
@ -206,30 +189,32 @@ abstract class ObjectController extends ActionController
/** @var DirectorObjectForm $form */
$form = $this->view->form = $this->loadForm('icinga' . ucfirst($type))
->setDb($this->db())
->setAuth($this->Auth())
->presetImports($this->params->shift('imports'))
->setApi($this->getApiIfAvailable())
->setSuccessUrl($url);
if ($type = $this->params->shift('type')) {
$form->setPreferredObjectType($type);
if ($oType = $this->params->shift('type')) {
$form->setPreferredObjectType($oType);
}
if ($type === 'template') {
$this->view->title = sprintf(
if ($oType === 'template') {
$this->assertPermission('director/admin');
$this->addTitle(
$this->translate('Add new Icinga %s template'),
ucfirst($ltype)
);
} else {
$this->view->title = sprintf(
$this->assertPermission('director/' . $ltype);
$this->addTitle(
$this->translate('Add new Icinga %s'),
ucfirst($ltype)
);
}
$this->beforeHandlingAddRequest($form);
$form->handleRequest();
$this->setViewScript('object/form');
$this->content()->add($form);
}
protected function beforeHandlingAddRequest($form)
@ -240,26 +225,18 @@ abstract class ObjectController extends ActionController
{
$type = $this->getType();
$ltype = strtolower($type);
$this->getTabs()->activate('modify');
$this->view->form = $form = $this->loadForm(
'icingaCloneObject'
)->setObject($this->object);
$this->view->title = sprintf(
$this->translate('Clone Icinga %s'),
ucfirst($type)
);
$this->view->form->handleRequest();
$this->view->actionLinks = $this->view->qlink(
$this->assertPermission('director/' . $ltype);
$this->tabs()->activate('modify');
$this->addTitle($this->translate('Clone Icinga %s'), ucfirst($type));
$form = $this->loadForm('icingaCloneObject')->setObject($this->object);
$form->handleRequest();
$this->content()->add($form);
$this->actions()->add(Link::create(
$this->translate('back'),
'director/' . $ltype,
array('name' => $this->object->object_name),
array('class' => 'icon-left-big')
);
$this->setViewScript('object/form');
));
}
public function fieldsAction()
@ -268,7 +245,7 @@ abstract class ObjectController extends ActionController
$object = $this->object;
$type = $this->getType();
$this->getTabs()->activate('fields');
$this->tabs()->activate('fields');
$this->view->title = sprintf(
$this->translate('Custom fields: %s'),
@ -309,7 +286,7 @@ abstract class ObjectController extends ActionController
$this->setAutorefreshInterval(10);
$db = $this->db();
$type = $this->getType();
$this->getTabs()->activate('history');
$this->tabs()->activate('history');
$this->view->title = sprintf(
$this->translate('Activity Log: %s'),
$this->object->object_name

View File

@ -0,0 +1,107 @@
<?php
namespace Icinga\Module\Director\Web\Tabs;
use Icinga\Authentication\Auth;
use Icinga\Module\Director\Objects\IcingaObject;
use ipl\Translation\TranslationHelper;
use ipl\Web\Component\Tabs;
class ObjectTabs extends Tabs
{
use TranslationHelper;
/** @var string */
private $type;
/** @var Auth */
private $auth;
/** @var IcingaObject $object */
private $object;
private $allowedExternals = array(
'apiuser',
'endpoint'
);
public function __construct($type, Auth $auth, IcingaObject $object = null)
{
$this->type = $type;
$this->auth = $auth;
$this->object = $object;
// We are not a BaseElement, not yet
$this->assemble();
}
protected function assemble()
{
if (null === $this->object) {
$this->addTabsForNewObject();
} else {
$this->addTabsForExistingObject();
}
}
protected function addTabsForNewObject()
{
$type = $this->type;
$this->add('add', array(
'url' => sprintf('director/%s/add', $type),
'label' => sprintf($this->translate('Add %s'), ucfirst($type)),
));
}
protected function addTabsForExistingObject()
{
$type = $this->type;
$auth = $this->auth;
$object = $this->object;
$params = $object->getUrlParams();
if (! $object->isExternal()
|| in_array($object->getShortTableName(), $this->allowedExternals)
) {
$this->add('modify', array(
'url' => sprintf('director/%s', $type),
'urlParams' => $params,
'label' => $this->translate(ucfirst($type))
));
}
if ($auth->hasPermission('director/showconfig')) {
$this->add('render', array(
'url' => sprintf('director/%s/render', $type),
'urlParams' => $params,
'label' => $this->translate('Preview'),
));
}
if ($auth->hasPermission('director/audit')) {
$this->add('history', array(
'url' => sprintf('director/%s/history', $type),
'urlParams' => $params,
'label' => $this->translate('History')
));
}
if ($auth->hasPermission('director/admin') && $this->hasFields()) {
$this->add('fields', array(
'url' => sprintf('director/%s/fields', $type),
'urlParams' => $params,
'label' => $this->translate('Fields')
));
}
}
protected function hasFields()
{
if (! ($object = $this->object)) {
return false;
}
return $object->hasBeenLoadedFromDb()
&& $object->supportsFields()
&& ($object->isTemplate() || $this->type === 'command');
}
}