From 956cce84cb58b20d8484bae1d8d621069b45b512 Mon Sep 17 00:00:00 2001 From: Sukhwinder Dhillon Date: Fri, 17 Nov 2023 13:43:08 +0100 Subject: [PATCH] Cleanup code - Remove superfluous methods/usages - Simplify the code --- .../DirectorObject/Lookup/ServiceFinder.php | 2 +- .../Integration/Icingadb/IcingadbBackend.php | 27 ++++----------- .../MonitoringModule/Monitoring.php | 33 ++++--------------- .../Web/Controller/ActionController.php | 2 +- 4 files changed, 15 insertions(+), 49 deletions(-) diff --git a/library/Director/DirectorObject/Lookup/ServiceFinder.php b/library/Director/DirectorObject/Lookup/ServiceFinder.php index fba6b65e..45b586c8 100644 --- a/library/Director/DirectorObject/Lookup/ServiceFinder.php +++ b/library/Director/DirectorObject/Lookup/ServiceFinder.php @@ -68,7 +68,7 @@ class ServiceFinder } if ($this->auth->hasPermission(Permission::MONITORING_HOSTS)) { if ($info = $this::find($this->host, $serviceName)) { - if ((new Monitoring($this->auth))->canModifyService($this->host->getObjectName(), $serviceName)) { + if ((new Monitoring())->canModifyService($this->host->getObjectName(), $serviceName)) { return $info->getUrl(); } } diff --git a/library/Director/Integration/Icingadb/IcingadbBackend.php b/library/Director/Integration/Icingadb/IcingadbBackend.php index fcdfeacf..5279652c 100644 --- a/library/Director/Integration/Icingadb/IcingadbBackend.php +++ b/library/Director/Integration/Icingadb/IcingadbBackend.php @@ -2,16 +2,12 @@ namespace Icinga\Module\Director\Integration\Icingadb; -use gipfl\IcingaWeb2\Link; -use Icinga\Application\Icinga; -use Icinga\Data\Filter\Filter as DataFilter; +use Icinga\Application\Modules\Module; use Icinga\Module\Director\Integration\BackendInterface; -use Icinga\Module\Director\Objects\IcingaHost; use Icinga\Module\Icingadb\Common\Auth; use Icinga\Module\Icingadb\Common\Database; use Icinga\Module\Icingadb\Model\Host; use Icinga\Module\Icingadb\Model\Service; -use Icinga\Module\Icingadb\Redis\VolatileStateResults; use Icinga\Web\Url; use ipl\Stdlib\Filter; @@ -22,28 +18,22 @@ class IcingadbBackend implements BackendInterface public function isAvailable(): bool { - $app = Icinga::app(); - $modules = $app->getModuleManager(); - return $modules->hasLoaded('icingadb'); + return Module::exists('icingadb'); } public function hasHost($hostname): bool { - $query = Host::on($this->getDb()); - $query->filter(Filter::equal('host.name', $hostname)); + $query = Host::on($this->getDb()) + ->filter(Filter::equal('host.name', $hostname)); $this->applyRestrictions($query); - /** @var Host $host */ - $host = $query->first(); - - return ($host !== null); + return $query->first() !== null; } public function hasService($hostname, $service): bool { - $query = Service::on($this->getDb()); - $query + $query = Service::on($this->getDb()) ->filter(Filter::all( Filter::equal('service.name', $service), Filter::equal('host.name', $hostname) @@ -51,10 +41,7 @@ class IcingadbBackend implements BackendInterface $this->applyRestrictions($query); - /** @var Service $service */ - $service = $query->first(); - - return ($service !== null); + return $query->first() !== null; } public function getHostUrl(string $hostname): Url diff --git a/library/Director/Integration/MonitoringModule/Monitoring.php b/library/Director/Integration/MonitoringModule/Monitoring.php index ccfa60ae..6d9fa201 100644 --- a/library/Director/Integration/MonitoringModule/Monitoring.php +++ b/library/Director/Integration/MonitoringModule/Monitoring.php @@ -39,11 +39,6 @@ class Monitoring implements BackendInterface } public function hasHost($hostname): bool - { - return $this->hasHostByName($hostname); - } - - public function hasHostByName($hostname): bool { if (! $this->isAvailable()) { return false; @@ -56,13 +51,7 @@ class Monitoring implements BackendInterface } } - public function hasService($hostname, $service): bool - { - return $this->hasServiceByName($hostname, $service); - } - - public function hasServiceByName($hostname, $service): bool { if (! $this->isAvailable()) { return false; @@ -77,23 +66,18 @@ class Monitoring implements BackendInterface public function canModifyService(string $hostName, string $serviceName): bool { - return $this->canModifyServiceByName($hostName, $serviceName); - } - - public function canModifyServiceByName($hostname, $service): bool - { - if (! $this->isAvailable() || $hostname === null || $service === null) { + if (! $this->isAvailable() || $hostName === null || $serviceName === null) { return false; } if ($this->auth->hasPermission(Permission::MONITORING_SERVICES)) { $restriction = null; foreach ($this->auth->getRestrictions(Restriction::MONITORING_RW_OBJECT_FILTER) as $restriction) { - if ($this->hasServiceWithFilter($hostname, $service, Filter::fromQueryString($restriction))) { + if ($this->hasServiceWithFilter($hostName, $serviceName, Filter::fromQueryString($restriction))) { return true; } } if ($restriction === null) { - return $this->hasServiceByName($hostname, $service); + return $this->hasService($hostName, $serviceName); } } @@ -101,21 +85,16 @@ class Monitoring implements BackendInterface } public function canModifyHost(string $hostName): bool - { - return $this->canModifyHostByName($hostName); - } - - public function canModifyHostByName($hostname): bool { if ($this->isAvailable() && $this->auth->hasPermission(Permission::MONITORING_HOSTS)) { $restriction = null; foreach ($this->auth->getRestrictions(Restriction::MONITORING_RW_OBJECT_FILTER) as $restriction) { - if ($this->hasHostWithFilter($hostname, Filter::fromQueryString($restriction))) { + if ($this->hasHostWithFilter($hostName, Filter::fromQueryString($restriction))) { return true; } } if ($restriction === null) { - return $this->hasHostByName($hostname); + return $this->hasHost($hostName); } } @@ -131,7 +110,7 @@ class Monitoring implements BackendInterface } } - public function hasServiceWithFilter($hostname, $service, Filter $filter): bool + protected function hasServiceWithFilter($hostname, $service, Filter $filter): bool { try { return $this->rowIsService( diff --git a/library/Director/Web/Controller/ActionController.php b/library/Director/Web/Controller/ActionController.php index 72750b30..bbff3a30 100644 --- a/library/Director/Web/Controller/ActionController.php +++ b/library/Director/Web/Controller/ActionController.php @@ -251,7 +251,7 @@ abstract class ActionController extends Controller implements ControlsAndContent if (Module::exists('icingadb')) { $this->backend = new IcingadbBackend(); } else { - $this->backend = new Monitoring($this->Auth()); + $this->backend = new Monitoring($this->getAuth()); } }