From ebdd0a8108e2a5fe0a2edaf53573aed263da67b4 Mon Sep 17 00:00:00 2001 From: Sukhwinder Dhillon Date: Tue, 26 Nov 2024 14:25:48 +0100 Subject: [PATCH 1/3] ObjectSuggestions: Add methods to restrict $customVarSources for customVar fetch --- .../Control/SearchBar/ObjectSuggestions.php | 41 ++++++++++++++++++- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/library/Icingadb/Web/Control/SearchBar/ObjectSuggestions.php b/library/Icingadb/Web/Control/SearchBar/ObjectSuggestions.php index 7630e0162..468846132 100644 --- a/library/Icingadb/Web/Control/SearchBar/ObjectSuggestions.php +++ b/library/Icingadb/Web/Control/SearchBar/ObjectSuggestions.php @@ -43,6 +43,9 @@ class ObjectSuggestions extends Suggestions /** @var array */ protected $customVarSources; + /** @var string[] Reduce {@see $customVarSources} to only given relations to fetch customVar for */ + protected $onlyWithCustomVarSources = []; + public function __construct() { $this->customVarSources = [ @@ -241,7 +244,7 @@ protected function fetchColumnSuggestions($searchTerm) $search = $name . '[*]'; } - foreach ($this->customVarSources as $relation => $label) { + foreach ($this->getCustomVarSources() as $relation => $label) { if (isset($customVar->$relation)) { if (! $titleAdded) { $titleAdded = true; @@ -284,8 +287,9 @@ protected function queryCustomvarConfig(string $searchTerm): Select $scalarQueries = []; $aggregates = ['flatname']; + $customVarSources = $this->getCustomVarSources(); foreach ($resolver->getRelations($customVars->getModel()) as $name => $relation) { - if (isset($this->customVarSources[$name]) && $relation instanceof BelongsToMany) { + if (isset($customVarSources[$name]) && $relation instanceof BelongsToMany) { $query = $customVars->createSubQuery( $relation->getTarget(), $resolver->qualifyPath($name, $tableName) @@ -419,4 +423,37 @@ protected static function collectRelations(Resolver $resolver, Model $subject, a } } } + + /** + * Get the custom variable sources to fetch customVar for + * + * If {@see $onlyWithCustomVarSources} is set, only return the sources that are in the list + * + * @return array + */ + protected function getCustomVarSources(): array + { + if (! empty($this->onlyWithCustomVarSources)) { + $this->customVarSources = array_intersect_key( + $this->customVarSources, + array_flip($this->onlyWithCustomVarSources) + ); + } + + return $this->customVarSources; + } + + /** + * Reduce {@see $customVarSources} to only given relations to fetch customVar for + * + * @param string[] $relations + * + * @return $this + */ + public function onlyWithCustomVarSources(array $relations): self + { + $this->onlyWithCustomVarSources = $relations; + + return $this; + } } From 8d045889788f8dcd15c68c3a05d3a3fa01465da1 Mon Sep 17 00:00:00 2001 From: Sukhwinder Dhillon Date: Tue, 26 Nov 2024 14:31:48 +0100 Subject: [PATCH 2/3] RedundancygroupController: Restrict customVar suggestions - Only use fetchable relations. - Restrict to `host`, `service`, `hostgroup` and `servicegroup` --- application/controllers/RedundancygroupController.php | 1 + 1 file changed, 1 insertion(+) diff --git a/application/controllers/RedundancygroupController.php b/application/controllers/RedundancygroupController.php index 9e0c65fb0..3c43ac856 100644 --- a/application/controllers/RedundancygroupController.php +++ b/application/controllers/RedundancygroupController.php @@ -228,6 +228,7 @@ public function completeAction(): void $suggestions = (new ObjectSuggestions()) ->setModel(DependencyNode::class) ->setBaseFilter(Filter::equal("$column.redundancy_group.id", $this->groupId)) + ->onlyWithCustomVarSources(['host', 'service', 'hostgroup', 'servicegroup']) ->forRequest($this->getServerRequest()); $this->getDocument()->add($suggestions); From 5adb2827a5495816ae1b24c89712db587ff42e3f Mon Sep 17 00:00:00 2001 From: Sukhwinder Dhillon Date: Tue, 26 Nov 2024 14:35:39 +0100 Subject: [PATCH 3/3] Models: Add missing `child` relation --- library/Icingadb/Model/Hostgroup.php | 3 ++- library/Icingadb/Model/Servicegroup.php | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/library/Icingadb/Model/Hostgroup.php b/library/Icingadb/Model/Hostgroup.php index 1ff6f0fba..0c28ee494 100644 --- a/library/Icingadb/Model/Hostgroup.php +++ b/library/Icingadb/Model/Hostgroup.php @@ -71,7 +71,8 @@ public function getDefaultSort() public function createBehaviors(Behaviors $behaviors) { $behaviors->add(new ReRoute([ - 'servicegroup' => 'service.servicegroup' + 'servicegroup' => 'service.servicegroup', + 'child' => 'host.to.from' ])); $behaviors->add(new Binary([ diff --git a/library/Icingadb/Model/Servicegroup.php b/library/Icingadb/Model/Servicegroup.php index 2e4b847b5..2db297a62 100644 --- a/library/Icingadb/Model/Servicegroup.php +++ b/library/Icingadb/Model/Servicegroup.php @@ -72,7 +72,8 @@ public function createBehaviors(Behaviors $behaviors) { $behaviors->add(new ReRoute([ 'host' => 'service.host', - 'hostgroup' => 'service.hostgroup' + 'hostgroup' => 'service.hostgroup', + 'child' => 'service.to.from' ])); $behaviors->add(new Binary([