Modify IcingaObjectFilterHelper::filterByTemplate for director branches

Icinga objects must be filterable based on templates when a director branch is active.
This commit is contained in:
raviks789 2023-08-03 10:33:52 +02:00 committed by Thomas Gelf
parent e6f7d82c09
commit 3c7082cabc
3 changed files with 45 additions and 5 deletions

View File

@ -5,6 +5,7 @@ namespace Icinga\Module\Director\Db;
use Icinga\Module\Director\Objects\IcingaObject; use Icinga\Module\Director\Objects\IcingaObject;
use Icinga\Module\Director\Resolver\TemplateTree; use Icinga\Module\Director\Resolver\TemplateTree;
use InvalidArgumentException; use InvalidArgumentException;
use Ramsey\Uuid\UuidInterface;
use RuntimeException; use RuntimeException;
use Zend_Db_Select as ZfSelect; use Zend_Db_Select as ZfSelect;
@ -46,13 +47,42 @@ class IcingaObjectFilterHelper
ZfSelect $query, ZfSelect $query,
$template, $template,
$tableAlias = 'o', $tableAlias = 'o',
$inheritanceType = self::INHERIT_DIRECT $inheritanceType = self::INHERIT_DIRECT,
UuidInterface $branchuuid = null
) { ) {
$i = $tableAlias . 'i'; $i = $tableAlias . 'i';
$o = $tableAlias; $o = $tableAlias;
$type = $template->getShortTableName(); $type = $template->getShortTableName();
$db = $template->getDb(); $db = $template->getDb();
$id = static::wantId($template); $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( $sub = $db->select()->from(
array($i => "icinga_{$type}_inheritance"), array($i => "icinga_{$type}_inheritance"),
array('e' => '(1)') array('e' => '(1)')

View File

@ -25,6 +25,8 @@ abstract class TemplateController extends CompatController
{ {
use DirectorDb; use DirectorDb;
use BranchHelper;
/** @var IcingaObject */ /** @var IcingaObject */
protected $template; protected $template;
@ -43,6 +45,7 @@ abstract class TemplateController extends CompatController
ObjectsTable::create($this->getType(), $this->db()) ObjectsTable::create($this->getType(), $this->db())
->setAuth($this->Auth()) ->setAuth($this->Auth())
->setBranch($this->getBranch())
->setBaseObjectUrl($this->getBaseObjectUrl()) ->setBaseObjectUrl($this->getBaseObjectUrl())
->filterTemplate($template, $this->getInheritance()) ->filterTemplate($template, $this->getInheritance())
->renderTo($this); ->renderTo($this);

View File

@ -3,9 +3,11 @@
namespace Icinga\Module\Director\Web\Table; namespace Icinga\Module\Director\Web\Table;
use Icinga\Exception\ProgrammingError; use Icinga\Exception\ProgrammingError;
use Icinga\Module\Director\Db\Branch\Branch;
use Icinga\Module\Director\Objects\IcingaObject; use Icinga\Module\Director\Objects\IcingaObject;
use Icinga\Module\Director\Resolver\TemplateTree; use Icinga\Module\Director\Resolver\TemplateTree;
use gipfl\IcingaWeb2\Link; use gipfl\IcingaWeb2\Link;
use Icinga\Module\Director\Web\Controller\BranchHelper;
use ipl\Html\Table; use ipl\Html\Table;
use gipfl\Translation\TranslationHelper; use gipfl\Translation\TranslationHelper;
@ -13,10 +15,14 @@ class TemplateUsageTable extends Table
{ {
use TranslationHelper; use TranslationHelper;
use TableWithBranchSupport;
protected $defaultAttributes = ['class' => 'pivot']; protected $defaultAttributes = ['class' => 'pivot'];
protected $objectType; protected $objectType;
protected $searchColumns = [];
public function getTypes() public function getTypes()
{ {
return [ return [
@ -37,14 +43,14 @@ class TemplateUsageTable extends Table
* @param IcingaObject $template * @param IcingaObject $template
* @return TemplateUsageTable * @return TemplateUsageTable
*/ */
public static function forTemplate(IcingaObject $template) public static function forTemplate(IcingaObject $template, Branch $branch = null)
{ {
$type = ucfirst($template->getShortTableName()); $type = ucfirst($template->getShortTableName());
$class = __NAMESPACE__ . "\\{$type}TemplateUsageTable"; $class = __NAMESPACE__ . "\\{$type}TemplateUsageTable";
if (class_exists($class)) { if (class_exists($class)) {
return new $class($template); return new $class($template, $branch);
} else { } else {
return new static($template); return new static($template, $branch);
} }
} }
@ -58,7 +64,7 @@ class TemplateUsageTable extends Table
]; ];
} }
protected function __construct(IcingaObject $template) protected function __construct(IcingaObject $template, Branch $branch = null)
{ {
if ($template->get('object_type') !== 'template') { if ($template->get('object_type') !== 'template') {
@ -68,6 +74,7 @@ class TemplateUsageTable extends Table
); );
} }
$this->setBranch($branch);
$this->objectType = $objectType = $template->getShortTableName(); $this->objectType = $objectType = $template->getShortTableName();
$types = $this->getTypes(); $types = $this->getTypes();
$usage = $this->getUsageSummary($template); $usage = $this->getUsageSummary($template);