summaryrefslogtreecommitdiffstats
path: root/library/Director/Web/Controller/Extension/DirectorDb.php
blob: c36e3584d25c64bc66005453e5db4954c55d1b2b (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
<?php

namespace Icinga\Module\Director\Web\Controller\Extension;

use Icinga\Module\Director\Auth\Restriction;
use Icinga\Module\Director\Db;
use Icinga\Module\Director\Web\Controller\ActionController;
use Icinga\Module\Director\Web\Window;
use RuntimeException;

trait DirectorDb
{
    /** @var Db */
    private $db;

    protected function getDbResourceName()
    {
        if ($name = $this->getDbResourceNameFromRequest()) {
            return $name;
        } elseif ($name = $this->getPreferredDbResourceName()) {
            return $name;
        } else {
            return $this->getFirstDbResourceName();
        }
    }

    protected function getDbResourceNameFromRequest()
    {
        $param = 'dbResourceName';
        // We shouldn't access _POST and _GET. However, this trait is used
        // in various places - and our Request is going to be replaced anyways.
        // So, let's not over-engineer things, this is quick & dirty:
        if (isset($_POST[$param])) {
            $name = $_POST[$param];
        } elseif (isset($_GET[$param])) {
            $name = $_GET[$param];
        } else {
            return null;
        }

        if (in_array($name, $this->listAllowedDbResourceNames())) {
            return $name;
        } else {
            return null;
        }
    }

    protected function getPreferredDbResourceName()
    {
        return $this->getWindowSessionValue('db_resource');
    }

    protected function getFirstDbResourceName()
    {
        $names = $this->listAllowedDbResourceNames();
        if (empty($names)) {
            return null;
        } else {
            return array_shift($names);
        }
    }

    protected function listAllowedDbResourceNames()
    {
        /** @var \Icinga\Authentication\Auth $auth */
        $auth = $this->Auth();

        $available = $this->listAvailableDbResourceNames();
        if ($resourceNames = $auth->getRestrictions(Restriction::DB_RESOURCE)) {
            $names = [];
            foreach ($resourceNames as $rNames) {
                foreach ($this->splitList($rNames) as $name) {
                    if (array_key_exists($name, $available)) {
                        $names[] = $name;
                    }
                }
            }

            return $names;
        } else {
            return $available;
        }
    }

    /**
     * @param string $string
     * @return array
     */
    protected function splitList($string)
    {
        return preg_split('/\s*,\s*/', $string, -1, PREG_SPLIT_NO_EMPTY);
    }

    protected function isMultiDbSetup()
    {
        return count($this->listAvailableDbResourceNames()) > 1;
    }

    /**
     * @return array
     */
    protected function listAvailableDbResourceNames()
    {
        /** @var \Icinga\Application\Config $config */
        $config = $this->Config();
        $resources = $config->get('db', 'resources');
        if ($resources === null) {
            $resource = $config->get('db', 'resource');
            if ($resource === null) {
                return [];
            } else {
                return [$resource => $resource];
            }
        } else {
            $resources = $this->splitList($resources);
            $resources = array_combine($resources, $resources);
            // natsort doesn't work!?
            ksort($resources, SORT_NATURAL);
            if ($resource = $config->get('db', 'resource')) {
                unset($resources[$resource]);
                $resources = [$resource => $resource] + $resources;
            }

            return $resources;
        }
    }

    protected function getWindowSessionValue($value, $default = null)
    {
        /** @var Window $window */
        $window = $this->Window();
        /** @var \Icinga\Web\Session\SessionNamespace $session */
        $session = $window->getSessionNamespace('director');

        return $session->get($value, $default);
    }

    /**
     *
     * @return Db
     */
    public function db()
    {
        if ($this->db === null) {
            $resourceName = $this->getDbResourceName();
            if ($resourceName) {
                $this->db = Db::fromResourceName($resourceName);
            } elseif ($this instanceof ActionController) {
                if ($this->getRequest()->isApiRequest()) {
                    throw new RuntimeException('Icinga Director is not correctly configured');
                } else {
                    $this->redirectNow('director');
                }
            } else {
                throw new RuntimeException('Icinga Director is not correctly configured');
            }
        }

        return $this->db;
    }
}