Completed
Pull Request — master (#4281)
by Craig
04:38
created

UsersModuleInstaller::migrateAttributes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 0
dl 0
loc 21
rs 10
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 - 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\UsersModule;
15
16
use DateTime;
17
use Zikula\ExtensionsModule\Api\VariableApi;
18
use Zikula\ExtensionsModule\Installer\AbstractExtensionInstaller;
19
use Zikula\UsersModule\Constant as UsersConstant;
20
use Zikula\UsersModule\Entity\UserAttributeEntity;
21
use Zikula\UsersModule\Entity\UserEntity;
22
use Zikula\UsersModule\Entity\UserSessionEntity;
23
24
class UsersModuleInstaller extends AbstractExtensionInstaller
25
{
26
    private $entities = [
27
        UserEntity::class,
28
        UserAttributeEntity::class,
29
        UserSessionEntity::class
30
    ];
31
32
    public function install(): bool
33
    {
34
        $this->schemaTool->create($this->entities);
35
36
        $this->createDefaultData();
37
        $this->setVars($this->getDefaultModvars());
38
        $this->getVariableApi()->set(VariableApi::CONFIG, 'authenticationMethodsStatus', ['native_uname' => true]);
39
40
        return true;
41
    }
42
43
    public function upgrade(string $oldVersion): bool
44
    {
45
        // 3.0.1 shipped with Core-1.4.3
46
        // 3.0.5 shipped with Core-2.0.15
47
        // version number reset to 3.0.0 at Core 3.0.0
48
        $connection = $this->entityManager->getConnection();
0 ignored issues
show
Bug introduced by
The method getConnection() does not exist on Doctrine\Persistence\ObjectManager. It seems like you code against a sub-type of Doctrine\Persistence\ObjectManager such as Doctrine\ORM\Decorator\EntityManagerDecorator or Doctrine\ORM\EntityManagerInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

48
        /** @scrutinizer ignore-call */ 
49
        $connection = $this->entityManager->getConnection();
Loading history...
49
        switch ($oldVersion) {
50
            case '2.9.9':
51
                $sql = '
52
                    ALTER TABLE users_attributes
53
                    ADD FOREIGN KEY (user_id)
54
                    REFERENCES users(uid)
55
                    ON DELETE CASCADE
56
                ';
57
                $stmt = $connection->prepare($sql);
58
                $stmt->execute();
59
                $this->delVar('password_reminder_enabled');
60
                $this->delVar('password_reminder_mandatory');
61
                $this->delVar('accountitemsperpage');
62
                $this->delVar('accountitemsperrow');
63
                $this->delVar('userimg');
64
                $this->setVar(UsersConstant::MODVAR_ALLOW_USER_SELF_DELETE, UsersConstant::DEFAULT_ALLOW_USER_SELF_DELETE);
65
                $this->schemaTool->update([UserEntity::class]);
66
        }
67
68
        return true;
69
    }
70
71
    public function uninstall(): bool
72
    {
73
        // Deletion not allowed
74
        return false;
75
    }
76
77
    /**
78
     * Build and return an array of all current module variables, with their default values.
79
     */
80
    private function getDefaultModvars(): array
81
    {
82
        return [
83
            UsersConstant::MODVAR_ACCOUNT_DISPLAY_GRAPHICS              => UsersConstant::DEFAULT_ACCOUNT_DISPLAY_GRAPHICS,
84
            UsersConstant::MODVAR_ANONYMOUS_DISPLAY_NAME                => $this->trans(/* Anonymous (guest) account display name */'Guest'),
85
            UsersConstant::MODVAR_ITEMS_PER_PAGE                        => UsersConstant::DEFAULT_ITEMS_PER_PAGE,
86
            UsersConstant::MODVAR_LOGIN_DISPLAY_APPROVAL_STATUS         => UsersConstant::DEFAULT_LOGIN_DISPLAY_APPROVAL_STATUS,
87
            UsersConstant::MODVAR_LOGIN_DISPLAY_DELETE_STATUS           => UsersConstant::DEFAULT_LOGIN_DISPLAY_DELETE_STATUS,
88
            UsersConstant::MODVAR_LOGIN_DISPLAY_INACTIVE_STATUS         => UsersConstant::DEFAULT_LOGIN_DISPLAY_INACTIVE_STATUS,
89
            UsersConstant::MODVAR_LOGIN_DISPLAY_VERIFY_STATUS           => UsersConstant::DEFAULT_LOGIN_DISPLAY_VERIFY_STATUS,
90
            UsersConstant::MODVAR_REGISTRATION_ADMIN_NOTIFICATION_EMAIL => '',
91
            UsersConstant::MODVAR_REGISTRATION_APPROVAL_REQUIRED        => UsersConstant::DEFAULT_REGISTRATION_APPROVAL_REQUIRED,
92
            UsersConstant::MODVAR_REGISTRATION_AUTO_LOGIN               => UsersConstant::DEFAULT_REGISTRATION_AUTO_LOGIN,
93
            UsersConstant::MODVAR_REGISTRATION_DISABLED_REASON          => $this->trans(/* registration disabled reason (default value, */'Sorry! New user registration is currently disabled.'),
94
            UsersConstant::MODVAR_REGISTRATION_ENABLED                  => UsersConstant::DEFAULT_REGISTRATION_ENABLED,
95
            UsersConstant::MODVAR_REGISTRATION_ILLEGAL_AGENTS           => '',
96
            UsersConstant::MODVAR_REGISTRATION_ILLEGAL_DOMAINS          => '',
97
            UsersConstant::MODVAR_REGISTRATION_ILLEGAL_UNAMES           => $this->trans(/* illegal username list */'root, webmaster, admin, administrator, nobody, anonymous, username'),
98
            UsersConstant::MODVAR_ALLOW_USER_SELF_DELETE                => UsersConstant::DEFAULT_ALLOW_USER_SELF_DELETE,
99
        ];
100
    }
101
102
    /**
103
     * Create the default data for the users module.
104
     *
105
     * This function is only ever called once during the lifetime of a particular
106
     * module instance.
107
     */
108
    private function createDefaultData(): void
109
    {
110
        $now = new DateTime();
111
        $then = new DateTime('1970-01-01 00:00:00');
112
113
        // Anonymous
114
        $record = [
115
            'uid'           => UsersConstant::USER_ID_ANONYMOUS,
116
            'uname'         => 'guest',
117
            'email'         => '',
118
            'activated'     => UsersConstant::ACTIVATED_ACTIVE,
119
            'approvedDate'  => $then,
120
            'approvedBy'    => UsersConstant::USER_ID_ADMIN,
121
            'registrationDate' => $then,
122
            'lastLogin'     => $then,
123
        ];
124
        $user = new UserEntity();
125
        $user->merge($record);
126
        $this->entityManager->persist($user);
127
128
        // Admin
129
        $record = [
130
            'uid'           => UsersConstant::USER_ID_ADMIN,
131
            'uname'         => 'admin',
132
            'email'         => '',
133
            'activated'     => UsersConstant::ACTIVATED_ACTIVE,
134
            'approvedDate'  => $now,
135
            'approvedBy'    => UsersConstant::USER_ID_ADMIN,
136
            'registrationDate' => $now,
137
            'lastLogin'     => $then,
138
        ];
139
        $user = new UserEntity();
140
        $user->merge($record);
141
        $this->entityManager->persist($user);
142
143
        $this->entityManager->flush();
144
    }
145
}
146