Passed
Pull Request — master (#6800)
by
unknown
08:35
created

Version20250923224100::getDescription()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 1
1
<?php
2
3
4
declare(strict_types=1);
5
6
namespace Chamilo\CoreBundle\Migrations\Schema\V200;
7
8
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
9
use Doctrine\DBAL\Schema\Schema;
10
11
final class Version20250923224100 extends AbstractMigrationChamilo
12
{
13
    public function getDescription(): string
14
    {
15
        return 'Insert or update platform setting title/comment for security 2FA global toggle (2fa_enable).';
16
    }
17
18
    public function up(Schema $schema): void
19
    {
20
        $settings = [
21
            [
22
                'name' => '2fa_enable',
23
                'title' => 'Enable 2FA',
24
                'comment' => "Add fields in the password update page to enable 2FA using a TOTP authenticator app. When disabled globally, users won't see 2FA fields and won't be prompted for 2FA at login, even if they had enabled it previously.",
25
                'category' => 'security',
26
                'default' => 'false',
27
            ],
28
        ];
29
30
        foreach ($settings as $setting) {
31
            $variable = addslashes($setting['name']);
32
            $title = addslashes($setting['title']);
33
            $comment = addslashes($setting['comment']);
34
            $category = addslashes($setting['category']);
35
            $default = addslashes($setting['default']);
36
37
            $sqlCheck = \sprintf(
38
                "SELECT COUNT(*) AS count
39
                 FROM settings
40
                 WHERE variable = '%s'
41
                   AND subkey IS NULL
42
                   AND access_url = 1",
43
                $variable
44
            );
45
46
            $stmt = $this->connection->executeQuery($sqlCheck);
47
            $result = $stmt->fetchAssociative();
48
49
            if ($result && (int) $result['count'] > 0) {
50
                $this->addSql(\sprintf(
51
                    "UPDATE settings
52
                     SET title = '%s',
53
                         comment = '%s',
54
                         category = '%s'
55
                     WHERE variable = '%s'
56
                       AND subkey IS NULL
57
                       AND access_url = 1",
58
                    $title,
59
                    $comment,
60
                    $category,
61
                    $variable
62
                ));
63
                $this->write(\sprintf('Updated setting: %s', $setting['name']));
64
            } else {
65
                $this->addSql(\sprintf(
66
                    "INSERT INTO settings
67
                        (variable, subkey, type, category, selected_value, title, comment, access_url_changeable, access_url_locked, access_url)
68
                     VALUES
69
                        ('%s', NULL, NULL, '%s', '%s', '%s', '%s', 1, 0, 1)",
70
                    $variable,
71
                    $category,
72
                    $default,
73
                    $title,
74
                    $comment
75
                ));
76
                $this->write(\sprintf('Inserted setting: %s', $setting['name']));
77
            }
78
        }
79
    }
80
81
    public function down(Schema $schema): void
82
    {
83
        $variables = ['2fa_enable'];
84
85
        foreach ($variables as $variable) {
86
            $this->addSql(\sprintf(
87
                "DELETE FROM settings
88
                 WHERE variable = '%s'
89
                   AND subkey IS NULL
90
                   AND access_url = 1",
91
                addslashes($variable)
92
            ));
93
            $this->write(\sprintf('Removed setting: %s.', $variable));
94
        }
95
    }
96
}
97