summaryrefslogtreecommitdiffstats
path: root/library/Director/DirectorObject/Lookup/ServiceFinder.php
blob: fb8d74cdc5259eaa3e52ced083985e16ad30396a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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,
        ]);
    }
}