summaryrefslogtreecommitdiffstats
path: root/application/forms/IcingaHostSelfServiceForm.php
blob: 1e05b963cf640e1ad908cbfe67d6429984e55018 (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php

namespace Icinga\Module\Director\Forms;

use Icinga\Exception\NotFoundError;
use Icinga\Module\Director\Db;
use Icinga\Module\Director\Objects\IcingaHost;
use Icinga\Module\Director\Web\Form\DirectorForm;
use Icinga\Security\SecurityException;

class IcingaHostSelfServiceForm extends DirectorForm
{
    /** @var string */
    private $hostApiKey;

    /** @var IcingaHost */
    private $template;

    private $hostName;

    public function setup()
    {
        if ($this->hostName === null) {
            $this->addElement('text', 'object_name', array(
                'label'    => $this->translate('Host name'),
                'required' => true,
                'value'    => $this->hostName,
            ));
        }
        $this->addElement('text', 'display_name', array(
            'label'    => $this->translate('Alias'),
        ));

        $this->addElement('text', 'address', array(
            'label' => $this->translate('Host address'),
            'description' => $this->translate(
                'Host address. Usually an IPv4 address, but may be any kind of address'
                . ' your check plugin is able to deal with'
            )
        ));

        $this->addElement('text', 'address6', array(
            'label' => $this->translate('IPv6 address'),
            'description' => $this->translate('Usually your hosts main IPv6 address')
        ));

        if ($this->template === null) {
            $this->addElement('text', 'key', array(
                'label'    => $this->translate('API Key'),
                'ignore'   => true,
                'required' => true,
            ));
        }

        $this->submitLabel = sprintf(
            $this->translate('Register')
        );
    }

    public function setHostName($name)
    {
        $this->hostName = $name;
        $this->removeElement('object_name');
        return $this;
    }

    public function loadTemplateWithApiKey($key)
    {
        $this->template = IcingaHost::loadWithApiKey($key, $this->getDb());
        if (! $this->template->isTemplate()) {
            throw new NotFoundError('Got invalid API key "%s"', $key);
        }

        if ($this->template->getResolvedProperty('has_agent') !== 'y') {
            throw new NotFoundError(
                'Got valid API key "%s", but template is not for Agents',
                $key
            );
        }

        $this->removeElement('key');

        return $this->template;
    }

    public function listMissingRequiredFields()
    {
        $result = [];
        foreach ($this->getElements() as $element) {
            if (in_array('isEmpty', $element->getErrors())) {
                $result[] = $element->getName();
            }
        }

        return $result;
    }

    public function isMissingRequiredFields()
    {
        return count($this->listMissingRequiredFields()) > 0;
    }

    public function onSuccess()
    {
        $db = $this->getDb();
        if ($this->template === null) {
            $this->loadTemplateWithApiKey($this->getValue('key'));
        }
        $name = $this->hostName ?: $this->getValue('object_name');
        if (IcingaHost::exists($name, $db)) {
            $host = IcingaHost::load($name, $db);
            if ($host->isTemplate()) {
                throw new SecurityException(
                    'You are not allowed to create "%s"',
                    $name
                );
            }

            if (null !== $host->getProperty('api_key')) {
                throw new SecurityException(
                    'The host "%s" has already been registered',
                    $name
                );
            }

            $propertyNames = ['display_name', 'address', 'address6'];
            foreach ($propertyNames as $property) {
                if (\strlen($value = $this->getValue($property)) > 0) {
                    $host->set($property, $value);
                }
            }
        } else {
            $host = IcingaHost::create(array_filter($this->getValues(), 'strlen'), $db);
            $host->set('object_name', $name);
            $host->set('object_type', 'object');
            $host->set('imports', [$this->template]);
        }

        $key = $host->generateApiKey();
        $host->store($db);
        $this->hostApiKey = $key;
    }

    /**
     * @return string|null
     */
    public function getHostApiKey()
    {
        return $this->hostApiKey;
    }

    public static function create(Db $db)
    {
        return static::load()->setDb($db);
    }
}