summaryrefslogtreecommitdiffstats
path: root/library/Icingadb/Hook/IcingadbSupportHook.php
blob: 96cdd19d2fc2aaf952c3fc156951d378ffefe835 (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
<?php

/* Icinga DB Web | (c) 2021 Icinga GmbH | GPLv2 */

namespace Icinga\Module\Icingadb\Hook;

use Icinga\Application\Icinga;
use Icinga\Authentication\Auth;
use Icinga\Module\Icingadb\Hook\Common\HookUtils;
use Icinga\Web\Session;

abstract class IcingadbSupportHook
{
    use HookUtils;

    /** @var string key name of preference */
    const PREFERENCE_NAME = 'icingadb.as_backend';

    /**
     * Return whether your module supports IcingaDB or not
     *
     * @return bool
     */
    public function supportsIcingaDb(): bool
    {
        return true;
    }

    /**
     * Whether icingadb is set as the preferred backend in preferences
     *
     * @return bool Return true if icingadb is set as backend, false otherwise
     */
    final public static function isIcingaDbSetAsPreferredBackend(): bool
    {
        return (bool) Session::getSession()
            ->getNamespace('icingadb')
            ->get(self::PREFERENCE_NAME, false);
    }

    /**
     * Whether to use icingadb as the backend
     *
     * @return bool Returns true if monitoring module is accessible or icingadb is selected as backend, false otherwise.
     */
    final public static function useIcingaDbAsBackend(): bool
    {
        return ! Icinga::app()->getModuleManager()->hasEnabled('monitoring')
            || ! Auth::getInstance()->hasPermission('module/monitoring')
            || self::isIcingaDbSetAsPreferredBackend();
    }
}