Skip to content

Commit

Permalink
Modify IcingaObjectFilterHelper::filterByTemplate for director bran…
Browse files Browse the repository at this point in the history
…ches

Icinga objects must be filterable based on templates when a director branch is active.
  • Loading branch information
raviks789 authored and Thomas-Gelf committed Sep 21, 2023
1 parent e6f7d82 commit 3c7082c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 5 deletions.
32 changes: 31 additions & 1 deletion library/Director/Db/IcingaObjectFilterHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Icinga\Module\Director\Objects\IcingaObject;
use Icinga\Module\Director\Resolver\TemplateTree;
use InvalidArgumentException;
use Ramsey\Uuid\UuidInterface;
use RuntimeException;
use Zend_Db_Select as ZfSelect;

Expand Down Expand Up @@ -46,13 +47,42 @@ public static function filterByTemplate(
ZfSelect $query,
$template,
$tableAlias = 'o',
$inheritanceType = self::INHERIT_DIRECT
$inheritanceType = self::INHERIT_DIRECT,
UuidInterface $branchuuid = null
) {
$i = $tableAlias . 'i';
$o = $tableAlias;
$type = $template->getShortTableName();
$db = $template->getDb();
$id = static::wantId($template);

if ($branchuuid) {
if ($inheritanceType === self::INHERIT_DIRECT) {
return $query->where('imports LIKE \'%"' . $template->getObjectName() . '"%\'');
} elseif ($inheritanceType === self::INHERIT_INDIRECT
|| $inheritanceType === self::INHERIT_DIRECT_OR_INDIRECT
) {
$tree = new TemplateTree($type, $template->getConnection());
$templateNames = $tree->getDescendantsFor($template);

if ($inheritanceType === self::INHERIT_DIRECT_OR_INDIRECT) {
$templateNames[] = $template->getObjectName();
}

if (empty($templateNames)) {
$condition = '(1 = 0)';
} else {
$condition = 'imports LIKE \'%"' . array_pop($templateNames) . '"%\'';

foreach ($templateNames as $templateName) {
$condition .= " OR imports LIKE '%\"$templateName\"%'";
}
}

return $query->where($condition);
}
}

$sub = $db->select()->from(
array($i => "icinga_{$type}_inheritance"),
array('e' => '(1)')
Expand Down
3 changes: 3 additions & 0 deletions library/Director/Web/Controller/TemplateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ abstract class TemplateController extends CompatController
{
use DirectorDb;

use BranchHelper;

/** @var IcingaObject */
protected $template;

Expand All @@ -43,6 +45,7 @@ public function objectsAction()

ObjectsTable::create($this->getType(), $this->db())
->setAuth($this->Auth())
->setBranch($this->getBranch())
->setBaseObjectUrl($this->getBaseObjectUrl())
->filterTemplate($template, $this->getInheritance())
->renderTo($this);
Expand Down
15 changes: 11 additions & 4 deletions library/Director/Web/Table/TemplateUsageTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,26 @@
namespace Icinga\Module\Director\Web\Table;

use Icinga\Exception\ProgrammingError;
use Icinga\Module\Director\Db\Branch\Branch;
use Icinga\Module\Director\Objects\IcingaObject;
use Icinga\Module\Director\Resolver\TemplateTree;
use gipfl\IcingaWeb2\Link;
use Icinga\Module\Director\Web\Controller\BranchHelper;
use ipl\Html\Table;
use gipfl\Translation\TranslationHelper;

class TemplateUsageTable extends Table
{
use TranslationHelper;

use TableWithBranchSupport;

protected $defaultAttributes = ['class' => 'pivot'];

protected $objectType;

protected $searchColumns = [];

public function getTypes()
{
return [
Expand All @@ -37,14 +43,14 @@ protected function getTypeSummaryDefinitions()
* @param IcingaObject $template
* @return TemplateUsageTable
*/
public static function forTemplate(IcingaObject $template)
public static function forTemplate(IcingaObject $template, Branch $branch = null)
{
$type = ucfirst($template->getShortTableName());
$class = __NAMESPACE__ . "\\{$type}TemplateUsageTable";
if (class_exists($class)) {
return new $class($template);
return new $class($template, $branch);
} else {
return new static($template);
return new static($template, $branch);
}
}

Expand All @@ -58,7 +64,7 @@ public function getColumnsToBeRendered()
];
}

protected function __construct(IcingaObject $template)
protected function __construct(IcingaObject $template, Branch $branch = null)
{

if ($template->get('object_type') !== 'template') {
Expand All @@ -68,6 +74,7 @@ protected function __construct(IcingaObject $template)
);
}

$this->setBranch($branch);
$this->objectType = $objectType = $template->getShortTableName();
$types = $this->getTypes();
$usage = $this->getUsageSummary($template);
Expand Down

0 comments on commit 3c7082c

Please sign in to comment.