summaryrefslogtreecommitdiffstats
path: root/library/Businessprocess/MonitoringRestrictions.php
blob: c7d2cef90b1fe434afb2d11cf55b04e4e34f6972 (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
<?php

namespace Icinga\Module\Businessprocess;

use Icinga\Authentication\Auth;
use Icinga\Data\Filter\Filter;
use Icinga\Exception\ConfigurationError;
use Icinga\Exception\QueryException;

class MonitoringRestrictions
{
    /**
     * Return a filter for the given restriction
     *
     * @param   string $name        Name of the restriction
     *
     * @return  Filter|null         Filter object or null if the authenticated user is not restricted
     * @throws  ConfigurationError  If the restriction contains invalid filter columns
     */
    public static function getRestriction($name)
    {
        // Borrowed from Icinga\Module\Monitoring\Controller
        $restriction = Filter::matchAny();
        $restriction->setAllowedFilterColumns(array(
            'host_name',
            'hostgroup_name',
            'instance_name',
            'service_description',
            'servicegroup_name',
            function ($c) {
                return preg_match('/^_(?:host|service)_/i', $c);
            }
        ));

        foreach (Auth::getInstance()->getRestrictions($name) as $filter) {
            if ($filter === '*') {
                return Filter::matchAny();
            }

            try {
                $restriction->addFilter(Filter::fromQueryString($filter));
            } catch (QueryException $e) {
                throw new ConfigurationError(
                    mt(
                        'monitoring',
                        'Cannot apply restriction %s using the filter %s. You can only use the following columns: %s'
                    ),
                    $name,
                    $filter,
                    implode(', ', array(
                        'instance_name',
                        'host_name',
                        'hostgroup_name',
                        'service_description',
                        'servicegroup_name',
                        '_(host|service)_<customvar-name>'
                    )),
                    $e
                );
            }
        }

        return $restriction;
    }
}