summaryrefslogtreecommitdiffstats
path: root/library/Director/Restriction/FilterByNameRestriction.php
blob: 8c3b25696f4cbc0adab2f8f5064509b5dcc6af35 (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
<?php

namespace Icinga\Module\Director\Restriction;

use gipfl\IcingaWeb2\Zf1\Db\FilterRenderer;
use Icinga\Authentication\Auth;
use Icinga\Data\Filter\Filter;
use Icinga\Module\Director\Db;
use Icinga\Module\Director\Objects\IcingaObject;
use Zend_Db_Select as ZfSelect;

class FilterByNameRestriction extends ObjectRestriction
{
    protected $type;

    /** @var Filter */
    protected $filter;

    public function __construct(Db $connection, Auth $auth, $type)
    {
        parent::__construct($connection, $auth);
        $this->setType($type);
    }

    protected function setType($type)
    {
        $this->type = $type;
        $this->setNameForType($type);
    }

    protected function setNameForType($type)
    {
        $this->name = "director/${type}/filter-by-name";
    }

    public function allows(IcingaObject $object)
    {
        if (! $this->isRestricted()) {
            return true;
        }

        return $this->getFilter()->matches([
            (object) ['object_name' => $object->getObjectName()]
        ]);
    }

    public function getFilter()
    {
        if ($this->filter === null) {
            $this->filter = MatchingFilter::forUser(
                $this->auth->getUser(),
                $this->name,
                'object_name'
            );
        }

        return $this->filter;
    }

    protected function filterQuery(ZfSelect $query, $tableAlias = 'o')
    {
        FilterRenderer::applyToQuery($this->getFilter(), $query);
    }
}