summaryrefslogtreecommitdiffstats
path: root/library/Director/ProvidedHook/Icingadb/HostActions.php
blob: d7332eae9a47a25491c723e9401b9ad32068491e (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
<?php

namespace Icinga\Module\Director\ProvidedHook\Icingadb;

use Exception;
use Icinga\Application\Config;
use Icinga\Module\Director\Auth\Permission;
use Icinga\Module\Director\Db;
use Icinga\Module\Director\Integration\Icingadb\IcingadbBackend;
use Icinga\Module\Director\Objects\IcingaHost;
use Icinga\Module\Director\Util;
use Icinga\Module\Icingadb\Hook\HostActionsHook;
use Icinga\Module\Icingadb\Model\Host;
use ipl\Web\Url;
use ipl\Web\Widget\Link;

class HostActions extends HostActionsHook
{
    public function getActionsForObject(Host $host): array
    {
        try {
            return $this->getThem($host);
        } catch (Exception $e) {
            return [];
        }
    }

    protected function getThem(Host $host): array
    {
        $actions = [];
        $db = $this->db();
        if (! $db) {
            return $actions;
        }
        $hostname = $host->name;
        if (Util::hasPermission(Permission::INSPECT)) {
            $actions[] = new Link(
                mt('director', 'Inspect'),
                Url::fromPath(
                    'director/inspect/object',
                    ['type' => 'host', 'plural' => 'hosts', 'name' => $hostname]
                )
            );
        }

        $allowEdit = false;
        if (Util::hasPermission(Permission::HOSTS) && IcingaHost::exists($hostname, $db)) {
            $allowEdit = true;
        }
        if (Util::hasPermission(Permission::ICINGADB_HOSTS)) {
            if ((new IcingadbBackend())->canModifyHost($hostname)) {
                $allowEdit = IcingaHost::exists($hostname, $db);
            }
        }

        if ($allowEdit) {
            $label = mt('director', 'Modify');
            $actions[] = new Link(
                $label,
                Url::fromPath('director/host/edit', [
                    'name'    => $hostname
                ])
            );
        }

        return $actions;
    }

    protected function db()
    {
        $resourceName = Config::module('director')->get('db', 'resource');
        if (! $resourceName) {
            return false;
        }

        return Db::fromResourceName($resourceName);
    }
}