summaryrefslogtreecommitdiffstats
path: root/library/Director/Web/Tabs/ObjectsTabs.php
blob: 9d2a7376e1892fb46c05486fdf17d6ecb62143b2 (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
80
81
82
83
84
85
86
87
<?php

namespace Icinga\Module\Director\Web\Tabs;

use Icinga\Authentication\Auth;
use Icinga\Module\Director\Auth\Permission;
use Icinga\Module\Director\Objects\IcingaObject;
use gipfl\Translation\TranslationHelper;
use gipfl\IcingaWeb2\Widget\Tabs;

class ObjectsTabs extends Tabs
{
    use TranslationHelper;

    public function __construct($type, Auth $auth, $typeUrl)
    {
        $object = IcingaObject::createByType($type);
        if ($object->isGroup()) {
            $object = IcingaObject::createByType(substr($typeUrl, 0, -5));
        }
        $shortName = $object->getShortTableName();

        $plType = strtolower(preg_replace('/cys$/', 'cies', $shortName . 's'));
        $plType = str_replace('_', '-', $plType);
        if ($auth->hasPermission("director/{$plType}")) {
            $this->add('index', [
                'url'   => sprintf('director/%s', $plType),
                'label' => $this->translate(ucfirst($plType)),
            ]);
        }

        if ($object->getShortTableName() === 'command') {
            $this->add('external', [
                'url'       => sprintf('director/%s', strtolower($plType)),
                'urlParams' => ['type' => 'external_object'],
                'label'     => $this->translate('External'),
            ]);
        }

        if ($auth->hasPermission(Permission::ADMIN)
            || (
            $object->getShortTableName() === 'notification'
            && $auth->hasPermission(Permission::NOTIFICATIONS)
        ) || (
            $object->getShortTableName() === 'scheduled_downtime'
            && $auth->hasPermission(Permission::SCHEDULED_DOWNTIMES)
        )) {
            if ($object->supportsApplyRules()) {
                $this->add('applyrules', [
                    'url'   => sprintf('director/%s/applyrules', $plType),
                    'label' => $this->translate('Apply')
                ]);
            }
        }

        if ($auth->hasPermission(Permission::ADMIN) && $type !== 'zone') {
            if ($object->supportsImports()) {
                $this->add('templates', [
                    'url'   => sprintf('director/%s/templates', $plType),
                    'label' => $this->translate('Templates'),
                ]);
            }

            if ($object->supportsGroups()) {
                $this->add('groups', [
                    'url'   => sprintf('director/%sgroups', $typeUrl),
                    'label' => $this->translate('Groups')
                ]);
            }
        }

        if ($auth->hasPermission(Permission::ADMIN)) {
            if ($object->supportsChoices()) {
                $this->add('choices', [
                    'url'   => sprintf('director/templatechoices/%s', $shortName),
                    'label' => $this->translate('Choices')
                ]);
            }
        }
        if ($object->supportsSets() && $auth->hasPermission("director/{$typeUrl}sets")) {
            $this->add('sets', [
                'url'   => sprintf('director/%s/sets', $plType),
                'label' => $this->translate('Sets')
            ]);
        }
    }
}