1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* For licensing terms, see /license.txt */ |
4
|
|
|
|
5
|
|
|
declare(strict_types=1); |
6
|
|
|
|
7
|
|
|
namespace Chamilo\CoreBundle\Migrations\Schema\V200; |
8
|
|
|
|
9
|
|
|
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo; |
10
|
|
|
use Doctrine\DBAL\Schema\Schema; |
11
|
|
|
|
12
|
|
|
final class Version20201212114910 extends AbstractMigrationChamilo |
13
|
|
|
{ |
14
|
|
|
public function getDescription(): string |
15
|
|
|
{ |
16
|
|
|
return 'Add password_rotation_days setting and user.password_updated_at column; migrate existing extra-field data and remove old extra-field'; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function up(Schema $schema): void |
20
|
|
|
{ |
21
|
|
|
// 1. Add new column to user table |
22
|
|
|
$this->addSql('ALTER TABLE `user` ADD COLUMN `password_updated_at` DATETIME DEFAULT NULL;'); |
23
|
|
|
|
24
|
|
|
// 2. Insert or update the new setting in settings table |
25
|
|
|
$setting = [ |
26
|
|
|
'variable' => 'password_rotation_days', |
27
|
|
|
'selected_value' => '0', |
28
|
|
|
'title' => 'Password rotation interval (days)', |
29
|
|
|
'comment' => 'Number of days before users must rotate their password (0 = disabled).', |
30
|
|
|
'category' => 'security', |
31
|
|
|
]; |
32
|
|
|
|
33
|
|
|
$sqlCheck = \sprintf( |
34
|
|
|
"SELECT COUNT(*) AS count |
35
|
|
|
FROM settings |
36
|
|
|
WHERE variable = '%s' |
37
|
|
|
AND subkey IS NULL |
38
|
|
|
AND access_url = 1", |
39
|
|
|
addslashes($setting['variable']) |
40
|
|
|
); |
41
|
|
|
$result = $this->connection->fetchAssociative($sqlCheck); |
42
|
|
|
|
43
|
|
|
if ($result && (int) $result['count'] > 0) { |
44
|
|
|
// UPDATE existing setting |
45
|
|
|
$this->addSql(\sprintf( |
46
|
|
|
"UPDATE settings |
47
|
|
|
SET selected_value = '%s', |
48
|
|
|
title = '%s', |
49
|
|
|
comment = '%s', |
50
|
|
|
category = '%s' |
51
|
|
|
WHERE variable = '%s' |
52
|
|
|
AND subkey IS NULL |
53
|
|
|
AND access_url = 1", |
54
|
|
|
addslashes($setting['selected_value']), |
55
|
|
|
addslashes($setting['title']), |
56
|
|
|
addslashes($setting['comment']), |
57
|
|
|
addslashes($setting['category']), |
58
|
|
|
addslashes($setting['variable']) |
59
|
|
|
)); |
60
|
|
|
} else { |
61
|
|
|
// INSERT new setting |
62
|
|
|
$this->addSql(\sprintf( |
63
|
|
|
"INSERT INTO settings |
64
|
|
|
(variable, subkey, type, category, selected_value, title, comment, access_url_changeable, access_url_locked, access_url) |
65
|
|
|
VALUES |
66
|
|
|
('%s', NULL, NULL, '%s', '%s', '%s', '%s', 1, 0, 1)", |
67
|
|
|
addslashes($setting['variable']), |
68
|
|
|
addslashes($setting['category']), |
69
|
|
|
addslashes($setting['selected_value']), |
70
|
|
|
addslashes($setting['title']), |
71
|
|
|
addslashes($setting['comment']) |
72
|
|
|
)); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
// 3. Migrate existing extra-field data into the new column (if tables exist) |
76
|
|
|
if ($schema->hasTable('extra_field_values') && $schema->hasTable('extra_field')) { |
77
|
|
|
// Copy field_value into user.password_updated_at for variable 'password_updated_at' |
78
|
|
|
$this->addSql(" |
79
|
|
|
UPDATE `user` AS u |
80
|
|
|
INNER JOIN `extra_field_values` AS efv |
81
|
|
|
ON efv.item_id = u.id |
82
|
|
|
INNER JOIN `extra_field` AS ef |
83
|
|
|
ON ef.id = efv.field_id |
84
|
|
|
AND ef.variable = 'password_updated_at' |
85
|
|
|
SET u.password_updated_at = efv.field_value |
86
|
|
|
"); |
87
|
|
|
|
88
|
|
|
// Delete the old extra_field_values entries |
89
|
|
|
$this->addSql(" |
90
|
|
|
DELETE efv |
91
|
|
|
FROM `extra_field_values` AS efv |
92
|
|
|
INNER JOIN `extra_field` AS ef |
93
|
|
|
ON ef.id = efv.field_id |
94
|
|
|
AND ef.variable = 'password_updated_at' |
95
|
|
|
"); |
96
|
|
|
|
97
|
|
|
// Delete the old extra_field definition |
98
|
|
|
$this->addSql(" |
99
|
|
|
DELETE ef |
100
|
|
|
FROM `extra_field` AS ef |
101
|
|
|
WHERE ef.variable = 'password_updated_at' |
102
|
|
|
AND ef.item_type = 1 |
103
|
|
|
"); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function down(Schema $schema): void |
108
|
|
|
{ |
109
|
|
|
// 1. Remove the setting |
110
|
|
|
$this->addSql(" |
111
|
|
|
DELETE FROM settings |
112
|
|
|
WHERE variable = 'password_rotation_days' |
113
|
|
|
AND subkey IS NULL |
114
|
|
|
AND access_url = 1 |
115
|
|
|
"); |
116
|
|
|
|
117
|
|
|
// 2. Drop the column from user table |
118
|
|
|
$this->addSql('ALTER TABLE `user` DROP COLUMN `password_updated_at`;'); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|