2017-06-15 09:50:12 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Icinga\Module\Director\Web\Table;
|
|
|
|
|
2017-07-13 11:48:16 +02:00
|
|
|
use Icinga\Authentication\Auth;
|
2017-06-15 09:50:12 +02:00
|
|
|
use Icinga\Data\Filter\Filter;
|
|
|
|
use Icinga\Exception\IcingaException;
|
2017-07-28 23:07:48 +02:00
|
|
|
use Icinga\Module\Director\Db;
|
2017-06-28 08:15:48 +02:00
|
|
|
use Icinga\Module\Director\Db\IcingaObjectFilterHelper;
|
2017-06-15 09:50:12 +02:00
|
|
|
use Icinga\Module\Director\IcingaConfig\AssignRenderer;
|
2017-06-28 08:15:48 +02:00
|
|
|
use Icinga\Module\Director\Objects\IcingaObject;
|
2019-05-02 13:23:06 +02:00
|
|
|
use gipfl\IcingaWeb2\Icon;
|
|
|
|
use gipfl\IcingaWeb2\Link;
|
|
|
|
use gipfl\IcingaWeb2\Table\ZfQueryBasedTable;
|
|
|
|
use gipfl\IcingaWeb2\Url;
|
|
|
|
use gipfl\IcingaWeb2\Zf1\Db\FilterRenderer;
|
2017-07-13 11:48:16 +02:00
|
|
|
use Zend_Db_Select as ZfSelect;
|
2017-06-15 09:50:12 +02:00
|
|
|
|
2017-06-22 09:46:12 +02:00
|
|
|
class ApplyRulesTable extends ZfQueryBasedTable
|
2017-06-15 09:50:12 +02:00
|
|
|
{
|
|
|
|
protected $searchColumns = [
|
|
|
|
'o.object_name',
|
|
|
|
'o.assign_filter',
|
|
|
|
];
|
|
|
|
|
|
|
|
private $type;
|
|
|
|
|
2018-06-15 02:08:06 +02:00
|
|
|
/** @var IcingaObject */
|
|
|
|
protected $dummyObject;
|
|
|
|
|
|
|
|
protected $baseObjectUrl;
|
|
|
|
|
2019-04-10 17:19:46 +02:00
|
|
|
protected $linkWithName = false;
|
|
|
|
|
2017-07-28 23:07:48 +02:00
|
|
|
public static function create($type, Db $db)
|
|
|
|
{
|
|
|
|
$table = new static($db);
|
|
|
|
$table->setType($type);
|
|
|
|
return $table;
|
|
|
|
}
|
|
|
|
|
2017-06-15 09:50:12 +02:00
|
|
|
public function setType($type)
|
|
|
|
{
|
|
|
|
$this->type = $type;
|
2018-06-15 02:08:06 +02:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setBaseObjectUrl($url)
|
|
|
|
{
|
|
|
|
$this->baseObjectUrl = $url;
|
|
|
|
|
2017-06-15 09:50:12 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2019-04-10 17:19:46 +02:00
|
|
|
public function createLinksWithNames($linksWithName = true)
|
|
|
|
{
|
|
|
|
$this->linkWithName = (bool) $linksWithName;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2019-02-15 01:29:43 +01:00
|
|
|
public function getType()
|
|
|
|
{
|
|
|
|
return $this->type;
|
|
|
|
}
|
|
|
|
|
2017-06-15 09:50:12 +02:00
|
|
|
public function getColumnsToBeRendered()
|
|
|
|
{
|
2017-06-28 08:15:48 +02:00
|
|
|
return ['Name', 'assign where'/*, 'Actions'*/];
|
2017-06-15 09:50:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function renderRow($row)
|
|
|
|
{
|
2019-04-10 17:19:46 +02:00
|
|
|
if ($this->linkWithName) {
|
|
|
|
$params = ['name' => $row->object_name];
|
|
|
|
} else {
|
|
|
|
$params = ['id' => $row->id];
|
|
|
|
}
|
|
|
|
$url = Url::fromPath("director/{$this->baseObjectUrl}/edit", $params);
|
2017-06-15 09:50:12 +02:00
|
|
|
|
2019-05-06 14:38:17 +02:00
|
|
|
$assignWhere = $this->renderApplyFilter($row->assign_filter);
|
|
|
|
|
|
|
|
if (! empty($row->apply_for)) {
|
|
|
|
$assignWhere = sprintf('apply for %s / %s', $row->apply_for, $assignWhere);
|
|
|
|
}
|
|
|
|
|
2017-07-13 12:45:52 +02:00
|
|
|
$tr = static::tr([
|
2017-06-15 09:50:12 +02:00
|
|
|
static::td(Link::create($row->object_name, $url)),
|
2019-05-06 14:38:17 +02:00
|
|
|
static::td($assignWhere),
|
2017-06-28 08:15:48 +02:00
|
|
|
// NOT (YET) static::td($this->createActionLinks($row))->setSeparator(' ')
|
2017-06-15 09:50:12 +02:00
|
|
|
]);
|
2017-07-13 12:45:52 +02:00
|
|
|
|
|
|
|
if ($row->disabled === 'y') {
|
2018-05-05 00:24:49 +02:00
|
|
|
$tr->getAttributes()->add('class', 'disabled');
|
2017-07-13 12:45:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $tr;
|
2017-06-15 09:50:12 +02:00
|
|
|
}
|
|
|
|
|
2019-04-10 17:19:46 +02:00
|
|
|
/**
|
|
|
|
* Should be triggered from renderRow, still unused.
|
|
|
|
*
|
|
|
|
* @param IcingaObject $template
|
|
|
|
* @param string $inheritance
|
|
|
|
* @return $this
|
|
|
|
* @throws \Icinga\Exception\ProgrammingError
|
|
|
|
*/
|
2017-06-28 08:15:48 +02:00
|
|
|
public function filterTemplate(
|
|
|
|
IcingaObject $template,
|
|
|
|
$inheritance = IcingaObjectFilterHelper::INHERIT_DIRECT
|
|
|
|
) {
|
|
|
|
IcingaObjectFilterHelper::filterByTemplate(
|
|
|
|
$this->getQuery(),
|
|
|
|
$template,
|
|
|
|
'o',
|
|
|
|
$inheritance
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2017-06-15 09:50:12 +02:00
|
|
|
protected function renderApplyFilter($assignFilter)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$string = AssignRenderer::forFilter(
|
|
|
|
Filter::fromQueryString($assignFilter)
|
|
|
|
)->renderAssign();
|
|
|
|
// Do not prefix it
|
|
|
|
$string = preg_replace('/^assign where /', '', $string);
|
|
|
|
} catch (IcingaException $e) {
|
|
|
|
// ignore errors in filter rendering
|
|
|
|
$string = 'Error in Filter rendering: ' . $e->getMessage();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $string;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function createActionLinks($row)
|
|
|
|
{
|
2018-06-15 02:08:06 +02:00
|
|
|
$baseUrl = 'director/' . $this->baseObjectUrl;
|
2017-06-15 09:50:12 +02:00
|
|
|
$links = [];
|
|
|
|
$links[] = Link::create(
|
|
|
|
Icon::create('sitemap'),
|
2018-06-15 02:08:06 +02:00
|
|
|
"${baseUrl}template/applytargets",
|
2017-06-15 09:50:12 +02:00
|
|
|
['id' => $row->id],
|
|
|
|
['title' => $this->translate('Show affected Objects')]
|
|
|
|
);
|
|
|
|
|
|
|
|
$links[] = Link::create(
|
|
|
|
Icon::create('edit'),
|
2018-06-15 02:08:06 +02:00
|
|
|
"$baseUrl/edit",
|
2017-06-15 09:50:12 +02:00
|
|
|
['id' => $row->id],
|
|
|
|
['title' => $this->translate('Modify this Apply Rule')]
|
|
|
|
);
|
|
|
|
|
|
|
|
$links[] = Link::create(
|
|
|
|
Icon::create('doc-text'),
|
2018-06-15 02:08:06 +02:00
|
|
|
"$baseUrl/render",
|
2017-06-15 09:50:12 +02:00
|
|
|
['id' => $row->id],
|
|
|
|
['title' => $this->translate('Apply Rule rendering preview')]
|
|
|
|
);
|
|
|
|
|
|
|
|
$links[] = Link::create(
|
|
|
|
Icon::create('history'),
|
2018-06-15 02:08:06 +02:00
|
|
|
"$baseUrl/history",
|
2017-06-15 09:50:12 +02:00
|
|
|
['id' => $row->id],
|
|
|
|
['title' => $this->translate('Apply rule history')]
|
|
|
|
);
|
|
|
|
|
|
|
|
return $links;
|
|
|
|
}
|
|
|
|
|
2017-07-13 11:48:16 +02:00
|
|
|
protected function applyRestrictions(ZfSelect $query)
|
|
|
|
{
|
|
|
|
$auth = Auth::getInstance();
|
|
|
|
$type = $this->type;
|
2017-07-14 11:40:22 +02:00
|
|
|
$restrictions = $auth->getRestrictions("director/$type/apply/filter-by-name");
|
2017-07-13 11:48:16 +02:00
|
|
|
if (empty($restrictions)) {
|
|
|
|
return $query;
|
|
|
|
}
|
|
|
|
|
|
|
|
$filter = Filter::matchAny();
|
|
|
|
foreach ($restrictions as $restriction) {
|
|
|
|
$filter->addFilter(Filter::where('o.object_name', $restriction));
|
|
|
|
}
|
|
|
|
|
|
|
|
return FilterRenderer::applyToQuery($filter, $query);
|
|
|
|
}
|
|
|
|
|
2018-06-15 02:08:06 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return IcingaObject
|
|
|
|
*/
|
|
|
|
protected function getDummyObject()
|
|
|
|
{
|
|
|
|
if ($this->dummyObject === null) {
|
|
|
|
$type = $this->type;
|
|
|
|
$this->dummyObject = IcingaObject::createByType($type);
|
|
|
|
}
|
|
|
|
return $this->dummyObject;
|
|
|
|
}
|
|
|
|
|
2017-06-15 09:50:12 +02:00
|
|
|
public function prepareQuery()
|
|
|
|
{
|
2018-06-15 02:08:06 +02:00
|
|
|
$table = $this->getDummyObject()->getTableName();
|
2017-06-15 09:50:12 +02:00
|
|
|
$columns = [
|
|
|
|
'id' => 'o.id',
|
|
|
|
'object_name' => 'o.object_name',
|
2017-07-13 12:45:52 +02:00
|
|
|
'disabled' => 'o.disabled',
|
2017-06-15 09:50:12 +02:00
|
|
|
'assign_filter' => 'o.assign_filter',
|
2019-05-06 21:21:13 +02:00
|
|
|
'apply_for' => '(NULL)',
|
2017-06-15 09:50:12 +02:00
|
|
|
];
|
2019-05-06 21:21:13 +02:00
|
|
|
|
|
|
|
if ($table === 'icinga_service') {
|
|
|
|
$columns['apply_for'] = 'o.apply_for';
|
|
|
|
}
|
2017-06-15 09:50:12 +02:00
|
|
|
$query = $this->db()->select()->from(
|
2018-06-15 02:08:06 +02:00
|
|
|
['o' => $table],
|
2017-06-15 09:50:12 +02:00
|
|
|
$columns
|
|
|
|
)->where(
|
|
|
|
"object_type = 'apply'"
|
|
|
|
)->order('o.object_name');
|
|
|
|
|
2018-06-15 02:08:06 +02:00
|
|
|
if ($this->type === 'service') {
|
2017-06-15 09:50:12 +02:00
|
|
|
$query->where('service_set_id IS NULL');
|
|
|
|
}
|
|
|
|
|
2017-07-13 11:48:16 +02:00
|
|
|
return $this->applyRestrictions($query);
|
2017-06-15 09:50:12 +02:00
|
|
|
}
|
|
|
|
}
|