Completed
Push — master ( ca49fa...f9ecf7 )
by Axel
25:36 queued 19:04
created

PermissionsModuleInstaller::upgrade()   A

Complexity

Conditions 6
Paths 10

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 9
nc 10
nop 1
dl 0
loc 16
rs 9.2222
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula Foundation - https://ziku.la/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Zikula\PermissionsModule;
15
16
use Exception;
17
use Zikula\ExtensionsModule\Installer\AbstractExtensionInstaller;
18
use Zikula\PermissionsModule\Entity\PermissionEntity;
19
20
/**
21
 * Installation and upgrade routines for the permissions module.
22
 */
23
class PermissionsModuleInstaller extends AbstractExtensionInstaller
24
{
25
    public function install(): bool
26
    {
27
        // create the table
28
        try {
29
            $this->schemaTool->create([
30
                PermissionEntity::class
31
            ]);
32
        } catch (Exception $exception) {
33
            return false;
34
        }
35
36
        $this->createDefaultData();
37
38
        // Initialisation successful
39
        return true;
40
    }
41
42
    public function upgrade(string $oldVersion): bool
43
    {
44
        // Upgrade dependent on old version number
45
        switch ($oldVersion) {
46
            case '1.1.1':
47
            case '1.1.2':
48
            case '1.2.0':
49
            case '1.2.1':
50
                $this->delVar('rowview');
51
                $this->delVar('rowedit');
52
            case '1.2.2':
53
            // future upgrade routines
54
        }
55
56
        // Update successful
57
        return true;
58
    }
59
60
    public function uninstall(): bool
61
    {
62
        // Deletion not allowed
63
        return false;
64
    }
65
66
    /**
67
     * Create the default data for the Permissions module.
68
     */
69
    public function createDefaultData(): void
70
    {
71
        // give administrator group full access to everything as top priority
72
        $record = new PermissionEntity();
73
        $record['gid']       = 2;
74
        $record['sequence']  = 1;
75
        $record['component'] = '.*';
76
        $record['instance']  = '.*';
77
        $record['level']     = ACCESS_ADMIN; // 800
78
        $this->entityManager->persist($record);
79
80
        // give user group comment access to everything as second priority
81
        $record = new PermissionEntity();
82
        $record['gid']       = 1;
83
        $record['sequence']  = 2;
84
        $record['component'] = '.*';
85
        $record['instance']  = '.*';
86
        $record['level']     = ACCESS_COMMENT; // 300
87
        $this->entityManager->persist($record);
88
89
        // allow unregistered users only read access to everything as lowest priority
90
        $record = new PermissionEntity();
91
        $record['gid']       = 0;
92
        $record['sequence']  = 3;
93
        $record['component'] = '.*';
94
        $record['instance']  = '.*';
95
        $record['level']     = ACCESS_READ; // 200
96
        $this->entityManager->persist($record);
97
98
        $this->entityManager->flush();
99
100
        $this->setVar('lockadmin', 1);
101
        $this->setVar('adminid', 1);
102
        $this->setVar('filter', 1);
103
    }
104
}
105