summaryrefslogtreecommitdiffstats
path: root/application/controllers/UsageController.php
blob: 079d24a68bd187ca876e2b50831011b97302c1b8 (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
<?php

// Icinga Web 2 X.509 Module | (c) 2018 Icinga GmbH | GPLv2

namespace Icinga\Module\X509\Controllers;

use Icinga\Exception\ConfigurationError;
use Icinga\Module\X509\Common\Database;
use Icinga\Module\X509\Controller;
use Icinga\Module\X509\Model\X509Certificate;
use Icinga\Module\X509\UsageTable;
use Icinga\Module\X509\Web\Control\SearchBar\ObjectSuggestions;
use ipl\Orm\Query;
use ipl\Sql\Expression;
use ipl\Web\Control\LimitControl;
use ipl\Web\Control\SortControl;

class UsageController extends Controller
{
    public function indexAction()
    {
        $this->addTitleTab($this->translate('Certificate Usage'));
        $this->getTabs()->enableDataExports();

        try {
            $conn = Database::get();
        } catch (ConfigurationError $_) {
            $this->render('missing-resource', null, true);
            return;
        }

        $targets = X509Certificate::on($conn)
            ->with(['chain', 'chain.target'])
            ->withColumns([
                'chain.id',
                'chain.valid',
                'chain.target.ip',
                'chain.target.port',
                'chain.target.hostname',
            ]);

        $targets
            ->getSelectBase()
            ->where(new Expression('certificate_link.order = 0'));

        $sortColumns = [
            'chain.target.hostname' => $this->translate('Hostname'),
            'chain.target.ip'       => $this->translate('IP'),
            'chain.target.port'     => $this->translate('Port'),
            'subject'               => $this->translate('Certificate'),
            'issuer'                => $this->translate('Issuer'),
            'version'               => $this->translate('Version'),
            'self_signed'           => $this->translate('Is Self-Signed'),
            'ca'                    => $this->translate('Is Certificate Authority'),
            'trusted'               => $this->translate('Is Trusted'),
            'pubkey_algo'           => $this->translate('Public Key Algorithm'),
            'pubkey_bits'           => $this->translate('Public Key Strength'),
            'signature_algo'        => $this->translate('Signature Algorithm'),
            'signature_hash_algo'   => $this->translate('Signature Hash Algorithm'),
            'valid_from'            => $this->translate('Valid From'),
            'valid_to'              => $this->translate('Valid To'),
            'chain.valid'           => $this->translate('Chain Is Valid'),
            'duration'              => $this->translate('Duration')
        ];

        $limitControl = $this->createLimitControl();
        $paginator = $this->createPaginationControl($targets);
        $sortControl = $this->createSortControl($targets, $sortColumns);

        $searchBar = $this->createSearchBar($targets, [
            $limitControl->getLimitParam(),
            $sortControl->getSortParam()
        ]);

        if ($searchBar->hasBeenSent() && ! $searchBar->isValid()) {
            if ($searchBar->hasBeenSubmitted()) {
                $filter = $this->getFilter();
            } else {
                $this->addControl($searchBar);
                $this->sendMultipartUpdate();

                return;
            }
        } else {
            $filter = $searchBar->getFilter();
        }

        $targets->peekAhead($this->view->compact);

        $targets->filter($filter);

        $this->addControl($paginator);
        $this->addControl($sortControl);
        $this->addControl($limitControl);
        $this->addControl($searchBar);

        $this->handleFormatRequest($targets, function (Query $targets) {
            /** @var X509Certificate $usage */
            foreach ($targets as $usage) {
                $usage->valid_from = $usage->valid_from->format('l F jS, Y H:i:s e');
                $usage->valid_to = $usage->valid_to->format('l F jS, Y H:i:s e');

                $usage->ip = $usage->chain->target->ip;
                $usage->hostname = $usage->chain->target->hostname;
                $usage->port = $usage->chain->target->port;
                $usage->valid = $usage->chain->valid;

                yield array_intersect_key(
                    iterator_to_array($usage),
                    array_flip(array_merge(['valid', 'hostname', 'ip', 'port'], $usage->getExportableColumns()))
                );
            }
        });

        $this->addContent((new UsageTable())->setData($targets));

        if (! $searchBar->hasBeenSubmitted() && $searchBar->hasBeenSent()) {
            $this->sendMultipartUpdate(); // Updates the browser search bar
        }
    }

    public function completeAction()
    {
        $this->getDocument()->add(
            (new ObjectSuggestions())
                ->setModel(X509Certificate::class)
                ->forRequest($this->getServerRequest())
        );
    }

    public function searchEditorAction()
    {
        $editor = $this->createSearchEditor(X509Certificate::on(Database::get()), [
            LimitControl::DEFAULT_LIMIT_PARAM,
            SortControl::DEFAULT_SORT_PARAM
        ]);

        $this->getDocument()->add($editor);
        $this->setTitle(t('Adjust Filter'));
    }
}