summaryrefslogtreecommitdiffstats
path: root/modules/monitoring/library/Monitoring/SecurityStep.php
blob: 94053b320271448fa208718c6a65dbb5f51185ae (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
<?php
/* Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */

namespace Icinga\Module\Monitoring;

use Exception;
use Icinga\Module\Setup\Step;
use Icinga\Application\Config;
use Icinga\Exception\IcingaException;

class SecurityStep extends Step
{
    protected $data;

    protected $error;

    public function __construct(array $data)
    {
        $this->data = $data;
    }

    public function apply()
    {
        $config = array();
        $config['security'] = $this->data['securityConfig'];

        try {
            Config::fromArray($config)
                ->setConfigFile(Config::resolvePath('modules/monitoring/config.ini'))
                ->saveIni();
        } catch (Exception $e) {
            $this->error = $e;
            return false;
        }

        $this->error = false;
        return true;
    }

    public function getSummary()
    {
        $pageTitle = '<h2>' . mt('monitoring', 'Monitoring Security', 'setup.page.title') . '</h2>';
        $pageDescription = '<p>' . mt(
            'monitoring',
            'Icinga Web 2 will protect your monitoring environment against'
            . ' prying eyes using the configuration specified below:'
        ) . '</p>';

        $pageHtml = ''
            . '<table>'
            . '<tbody>'
            . '<tr>'
            . '<td><strong>' . mt('monitoring', 'Protected Custom Variables') . '</strong></td>'
            . '<td>' . ($this->data['securityConfig']['protected_customvars'] ? (
                $this->data['securityConfig']['protected_customvars']
            ) : mt('monitoring', 'None', 'monitoring.protected_customvars')) . '</td>'
            . '</tr>'
            . '</tbody>'
            . '</table>';

        return $pageTitle . '<div class="topic">' . $pageDescription . $pageHtml . '</div>';
    }

    public function getReport()
    {
        if ($this->error === false) {
            return array(sprintf(
                mt('monitoring', 'Monitoring security configuration has been successfully created: %s'),
                Config::resolvePath('modules/monitoring/config.ini')
            ));
        } elseif ($this->error !== null) {
            return array(
                sprintf(
                    mt(
                        'monitoring',
                        'Monitoring security configuration could not be written to: %s. An error occured:'
                    ),
                    Config::resolvePath('modules/monitoring/config.ini')
                ),
                sprintf(mt('setup', 'ERROR: %s'), IcingaException::describe($this->error))
            );
        }
    }
}