summaryrefslogtreecommitdiffstats
path: root/library/Director/Web/Table/ApplyRulesTable.php
blob: a861bace041818c7c6ae08c2e57b2e013e64f68f (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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<?php

namespace Icinga\Module\Director\Web\Table;

use Icinga\Authentication\Auth;
use Icinga\Data\Filter\Filter;
use Icinga\Exception\IcingaException;
use Icinga\Module\Director\Db;
use Icinga\Module\Director\Db\DbUtil;
use Icinga\Module\Director\Db\IcingaObjectFilterHelper;
use Icinga\Module\Director\IcingaConfig\AssignRenderer;
use Icinga\Module\Director\Objects\IcingaObject;
use gipfl\IcingaWeb2\Icon;
use gipfl\IcingaWeb2\Link;
use gipfl\IcingaWeb2\Table\ZfQueryBasedTable;
use gipfl\IcingaWeb2\Url;
use gipfl\IcingaWeb2\Zf1\Db\FilterRenderer;
use Ramsey\Uuid\Uuid;
use Zend_Db_Select as ZfSelect;

class ApplyRulesTable extends ZfQueryBasedTable
{
    protected $searchColumns = [
        'o.object_name',
        'o.assign_filter',
    ];

    private $type;

    /** @var IcingaObject */
    protected $dummyObject;

    protected $baseObjectUrl;

    protected $linkWithName = false;

    public static function create($type, Db $db)
    {
        $table = new static($db);
        $table->setType($type);
        return $table;
    }

    public function setType($type)
    {
        $this->type = $type;

        return $this;
    }

    public function setBaseObjectUrl($url)
    {
        $this->baseObjectUrl = $url;

        return $this;
    }

    public function createLinksWithNames($linksWithName = true)
    {
        $this->linkWithName = (bool) $linksWithName;

        return $this;
    }

    public function getType()
    {
        return $this->type;
    }

    public function getColumnsToBeRendered()
    {
        return ['Name', 'assign where'/*, 'Actions'*/];
    }

    public function renderRow($row)
    {
        $row->uuid = DbUtil::binaryResult($row->uuid);
        if ($this->linkWithName) {
            $params = ['name' => $row->object_name];
        } else {
            $params = ['uuid' => Uuid::fromBytes($row->uuid)->toString()];
        }
        $url = Url::fromPath("director/{$this->baseObjectUrl}/edit", $params);

        $assignWhere = $this->renderApplyFilter($row->assign_filter);

        if (! empty($row->apply_for)) {
            $assignWhere = sprintf('apply for %s / %s', $row->apply_for, $assignWhere);
        }

        $tr = static::tr([
            static::td(Link::create($row->object_name, $url)),
            static::td($assignWhere),
            // NOT (YET) static::td($this->createActionLinks($row))->setSeparator(' ')
        ]);

        if ($row->disabled === 'y') {
            $tr->getAttributes()->add('class', 'disabled');
        }

        return $tr;
    }

    /**
     * Should be triggered from renderRow, still unused.
     *
     * @param IcingaObject $template
     * @param string $inheritance
     * @return $this
     * @throws \Icinga\Exception\ProgrammingError
     */
    public function filterTemplate(
        IcingaObject $template,
        $inheritance = IcingaObjectFilterHelper::INHERIT_DIRECT
    ) {
        IcingaObjectFilterHelper::filterByTemplate(
            $this->getQuery(),
            $template,
            'o',
            $inheritance
        );

        return $this;
    }

    protected function renderApplyFilter($assignFilter)
    {
        try {
            $string = AssignRenderer::forFilter(
                Filter::fromQueryString($assignFilter)
            )->renderAssign();
            // Do not prefix it
            $string = preg_replace('/^assign where /', '', $string);
        } catch (IcingaException $e) {
            // ignore errors in filter rendering
            $string = 'Error in Filter rendering: ' . $e->getMessage();
        }

        return $string;
    }

    public function createActionLinks($row)
    {
        $params = ['uuid' => Uuid::fromBytes($row->uuid)->toString()];
        $baseUrl = 'director/' . $this->baseObjectUrl;
        $links = [];
        $links[] = Link::create(
            Icon::create('sitemap'),
            "${baseUrl}template/applytargets",
            ['id' => $row->id],
            ['title' => $this->translate('Show affected Objects')]
        );

        $links[] = Link::create(
            Icon::create('edit'),
            "$baseUrl/edit",
            $params,
            ['title' => $this->translate('Modify this Apply Rule')]
        );

        $links[] = Link::create(
            Icon::create('doc-text'),
            "$baseUrl/render",
            $params,
            ['title' => $this->translate('Apply Rule rendering preview')]
        );

        $links[] = Link::create(
            Icon::create('history'),
            "$baseUrl/history",
            $params,
            ['title' => $this->translate('Apply rule history')]
        );

        return $links;
    }

    protected function applyRestrictions(ZfSelect $query)
    {
        $auth = Auth::getInstance();
        $type = $this->type;
        // TODO: Centralize this logic
        if ($type === 'scheduledDowntime') {
            $type = 'scheduled-downtime';
        }
        $restrictions = $auth->getRestrictions("director/$type/apply/filter-by-name");
        if (empty($restrictions)) {
            return $query;
        }

        $filter = Filter::matchAny();
        foreach ($restrictions as $restriction) {
            $filter->addFilter(Filter::where('o.object_name', $restriction));
        }

        return FilterRenderer::applyToQuery($filter, $query);
    }


    /**
     * @return IcingaObject
     */
    protected function getDummyObject()
    {
        if ($this->dummyObject === null) {
            $type = $this->type;
            $this->dummyObject = IcingaObject::createByType($type);
        }
        return $this->dummyObject;
    }

    public function prepareQuery()
    {
        $table = $this->getDummyObject()->getTableName();
        $columns = [
            'id'            => 'o.id',
            'uuid'          => 'o.uuid',
            'object_name'   => 'o.object_name',
            'disabled'      => 'o.disabled',
            'assign_filter' => 'o.assign_filter',
            'apply_for'     => '(NULL)',
        ];

        if ($table === 'icinga_service') {
            $columns['apply_for'] = 'o.apply_for';
        }
        $query = $this->db()->select()->from(
            ['o' => $table],
            $columns
        )->where(
            "object_type = 'apply'"
        )->order('o.object_name');

        if ($this->type === 'service') {
            $query->where('service_set_id IS NULL');
        }

        return $this->applyRestrictions($query);
    }
}