diff options
Diffstat (limited to '')
-rw-r--r-- | library/Director/DirectorObject/Lookup/ServiceFinder.php | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/library/Director/DirectorObject/Lookup/ServiceFinder.php b/library/Director/DirectorObject/Lookup/ServiceFinder.php new file mode 100644 index 0000000..fb8d74c --- /dev/null +++ b/library/Director/DirectorObject/Lookup/ServiceFinder.php @@ -0,0 +1,79 @@ +<?php + +namespace Icinga\Module\Director\DirectorObject\Lookup; + +use gipfl\IcingaWeb2\Url; +use Icinga\Authentication\Auth; +use Icinga\Module\Director\Objects\HostApplyMatches; +use Icinga\Module\Director\Objects\IcingaHost; +use RuntimeException; + +class ServiceFinder +{ + /** @var IcingaHost */ + protected $host; + + /** @var ?Auth */ + protected $auth; + + /** @var IcingaHost[] */ + protected $parents; + + /** @var HostApplyMatches */ + protected $applyMatcher; + + /** @var \Icinga\Module\Director\Db */ + protected $db; + + public function __construct(IcingaHost $host, Auth $auth = null) + { + $this->host = $host; + $this->auth = $auth; + $this->db = $host->getConnection(); + } + + public static function find(IcingaHost $host, $serviceName) + { + foreach ([ + SingleServiceInfo::class, + InheritedServiceInfo::class, + ServiceSetServiceInfo::class, + AppliedServiceInfo::class, + AppliedServiceSetServiceInfo::class, + ] as $class) { + /** @var ServiceInfo $class */ + if ($info = $class::find($host, $serviceName)) { + return $info; + } + } + + return false; + } + + /** + * @param $serviceName + * @return Url + */ + public function getRedirectionUrl($serviceName) + { + if ($this->auth === null) { + throw new RuntimeException('Auth is required for ServiceFinder when dealing when asking for URLs'); + } + if ($this->auth->hasPermission('director/host')) { + if ($info = $this::find($this->host, $serviceName)) { + return $info->getUrl(); + } + } + if ($this->auth->hasPermission('director/monitoring/services-ro')) { + return Url::fromPath('director/host/servicesro', [ + 'name' => $this->host->getObjectName(), + 'service' => $serviceName + ]); + } + + return Url::fromPath('director/host/invalidservice', [ + 'name' => $this->host->getObjectName(), + 'service' => $serviceName, + ]); + } +} |