Passed
Branch develop (9d5aa6)
by Nikolay
04:57
created

AclProvider::clearCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 0
1
<?php
2
/*
3
 * MikoPBX - free phone system for small business
4
 * Copyright © 2017-2023 Alexey Portnov and Nikolay Beketov
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License along with this program.
17
 * If not, see <https://www.gnu.org/licenses/>.
18
 */
19
20
declare(strict_types=1);
21
22
namespace MikoPBX\Common\Providers;
23
24
use MikoPBX\Modules\Config\WebUIConfigInterface;
25
use Phalcon\Acl\Adapter\Memory as AclList;
26
use Phalcon\Acl\Component;
27
use Phalcon\Acl\Enum as AclEnum;
28
use Phalcon\Acl\Role as AclRole;
29
use Phalcon\Di;
30
use Phalcon\Di\DiInterface;
31
use Phalcon\Di\ServiceProviderInterface;
32
33
/**
34
 * Makes the Access Control List (ACL).
35
 *
36
 * This method creates a new AclList object and sets the default action to AclEnum::DENY. It then adds two roles,
37
 * admins and guest, to the ACL, and sets the default permissions such that admins are allowed to perform any
38
 * action and guest is denied access to any action.
39
 *
40
 * Finally, it uses the PBXConfModulesProvider class to allow modules to modify the ACL, and returns the modified ACL.
41
 *
42
 * @return AclList The Access Control List.
43
 *
44
 * @package MikoPBX\Common\Providers
45
 */
46
class AclProvider implements ServiceProviderInterface
47
{
48
    public const SERVICE_NAME = 'ACL';
49
50
    public const CACHE_KEY = 'ACLCache';
51
52
    public const ROLE_ADMINS = 'admins';
53
    public const ROLE_GUESTS = 'guests';
54
55
56
    /**
57
     * Register ACL service provider
58
     *
59
     * @param \Phalcon\Di\DiInterface $di The DI container.
60
     */
61
    public function register(DiInterface $di): void
62
    {
63
        $di->setShared(
64
            self::SERVICE_NAME,
65
            function () use ($di){
66
67
                $cache = $di->getShared(ManagedCacheProvider::SERVICE_NAME);
68
                $acl = $cache->get(self::CACHE_KEY);
69
70
                if (!$acl) {
71
                    $acl = new AclList();
72
                    $acl->setDefaultAction(AclEnum::DENY);
73
74
                    // Register roles
75
                    $acl->addRole(new AclRole(AclProvider::ROLE_ADMINS, 'Admins'));
76
                    $acl->addRole(new AclRole(AclProvider::ROLE_GUESTS, 'Guests'));
77
78
                    // Default permissions
79
                    $acl->allow(AclProvider::ROLE_ADMINS, '*', '*');
80
                    $acl->deny(AclProvider::ROLE_GUESTS, '*', '*');
81
82
                    // Modules HOOK
83
                    PBXConfModulesProvider::hookModulesMethod(WebUIConfigInterface::ON_AFTER_ACL_LIST_PREPARED, [&$acl]);
84
85
                    // Allow to show ERROR controllers to everybody
86
                    $acl->addComponent(new Component('Errors'), ['show401', 'show404', 'show500']);
87
                    $acl->allow('*', 'Errors', ['show401', 'show404', 'show500']);
88
89
                    // Allow to show session controllers actions to everybody
90
                    $acl->addComponent(new Component('Session'), ['index', 'start', 'changeLanguage', 'end']);
91
                    $acl->allow('*', 'Session', ['index', 'start', 'changeLanguage', 'end']);
92
93
                    $cache->set(self::CACHE_KEY, $acl, 86400);
94
                }
95
96
                return $acl;
97
            }
98
        );
99
    }
100
101
    /**
102
     *  Clear ACL cache
103
     * @return void
104
     */
105
    public static function clearCache():void {
106
        $di = Di::getDefault();
107
        $cache = $di->get(ManagedCacheProvider::SERVICE_NAME);
108
        $cache->delete(self::CACHE_KEY);
109
    }
110
111
}