|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * --------------------------------------------------------------------- |
| 5 | + * |
| 6 | + * GLPI - Gestionnaire Libre de Parc Informatique |
| 7 | + * |
| 8 | + * http://glpi-project.org |
| 9 | + * |
| 10 | + * @copyright 2015-2026 Teclib' and contributors. |
| 11 | + * @licence https://www.gnu.org/licenses/gpl-3.0.html |
| 12 | + * |
| 13 | + * --------------------------------------------------------------------- |
| 14 | + * |
| 15 | + * LICENSE |
| 16 | + * |
| 17 | + * This file is part of GLPI. |
| 18 | + * |
| 19 | + * This program is free software: you can redistribute it and/or modify |
| 20 | + * it under the terms of the GNU General Public License as published by |
| 21 | + * the Free Software Foundation, either version 3 of the License, or |
| 22 | + * (at your option) any later version. |
| 23 | + * |
| 24 | + * This program is distributed in the hope that it will be useful, |
| 25 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 26 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 27 | + * GNU General Public License for more details. |
| 28 | + * |
| 29 | + * You should have received a copy of the GNU General Public License |
| 30 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 31 | + * |
| 32 | + * --------------------------------------------------------------------- |
| 33 | + */ |
| 34 | + |
| 35 | +/** |
| 36 | + * @var Migration $migration |
| 37 | + * @var DBmysql $DB |
| 38 | + */ |
| 39 | + |
| 40 | +use Ramsey\Uuid\Uuid; |
| 41 | + |
| 42 | +if (!$DB->tableExists('glpi_user_sessions')) { |
| 43 | + $DB->doQuery(<<<SQL |
| 44 | + CREATE TABLE `glpi_user_sessions` ( |
| 45 | + `id` int unsigned NOT NULL AUTO_INCREMENT, |
| 46 | + `users_id` int unsigned NOT NULL, |
| 47 | + `session_token_hash` varchar(64) NOT NULL, |
| 48 | + `session_file` varchar(261) NOT NULL COMMENT 'Session filename. PHP allows up to 256 characters for session IDs + the "sess_" prefix used by default.', |
| 49 | + `ip_address` varchar(45) NOT NULL, |
| 50 | + `user_agent` varchar(512) NOT NULL, |
| 51 | + `auth_type` tinyint NOT NULL, |
| 52 | + `created_at` timestamp NOT NULL, |
| 53 | + `last_activity_at` timestamp NOT NULL, |
| 54 | + PRIMARY KEY (`id`), |
| 55 | + UNIQUE KEY `session_token_hash` (`session_token_hash`), |
| 56 | + KEY `users_id` (`users_id`), |
| 57 | + KEY `last_activity_at` (`last_activity_at`) |
| 58 | + ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ROW_FORMAT=DYNAMIC; |
| 59 | +SQL); |
| 60 | +} |
| 61 | + |
| 62 | +if (!$DB->tableExists('glpi_user_session_history')) { |
| 63 | + $DB->doQuery(<<<SQL |
| 64 | + CREATE TABLE `glpi_user_session_history` ( |
| 65 | + `id` int unsigned NOT NULL AUTO_INCREMENT, |
| 66 | + `users_id` int unsigned NOT NULL, |
| 67 | + `session_token_hash` varchar(64) NOT NULL, |
| 68 | + `ip_address` varchar(45) NOT NULL, |
| 69 | + `user_agent` varchar(512) NOT NULL, |
| 70 | + `auth_type` tinyint NOT NULL, |
| 71 | + `logged_in_at` timestamp NOT NULL, |
| 72 | + `logged_out_at` timestamp NULL DEFAULT NULL, |
| 73 | + `logout_reason` enum('user', 'admin', 'expired') DEFAULT NULL, |
| 74 | + `users_id_revoked_by` int unsigned DEFAULT NULL, |
| 75 | + PRIMARY KEY (`id`), |
| 76 | + UNIQUE KEY `session_token_hash` (`session_token_hash`), |
| 77 | + KEY `users_id` (`users_id`, `logged_in_at` DESC), |
| 78 | + KEY `users_id_revoked_by` (`users_id_revoked_by`), |
| 79 | + KEY `logged_out_at` (`logged_out_at`) |
| 80 | + ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ROW_FORMAT=DYNAMIC; |
| 81 | +SQL); |
| 82 | +} |
| 83 | + |
| 84 | +// Add a uuid column to glpi_oauth_access_tokens without NOT NULL constraint, generate UUIDs for existing records, then add the NOT NULL constraint |
| 85 | +if (!$DB->fieldExists('glpi_oauth_access_tokens', 'uuid')) { |
| 86 | + $migration->addField('glpi_oauth_access_tokens', 'uuid', 'varchar(255)', ['null' => true, 'after' => 'identifier']); |
| 87 | + $migration->migrationOneTable('glpi_oauth_access_tokens'); |
| 88 | + // Generating UUIDs in PHP because MySQL only has UUID() which is v1 and typically expect v4. |
| 89 | + $count = countElementsInTable('glpi_oauth_access_tokens', ['uuid' => null]); |
| 90 | + $it = $DB->request([ |
| 91 | + 'SELECT' => ['identifier'], |
| 92 | + 'FROM' => 'glpi_oauth_access_tokens', |
| 93 | + 'WHERE' => ['uuid' => null], |
| 94 | + ]); |
| 95 | + foreach ($it as $row) { |
| 96 | + $DB->update('glpi_oauth_access_tokens', ['uuid' => Uuid::uuid4()->toString()], ['identifier' => $row['identifier']]); |
| 97 | + } |
| 98 | + $migration->changeField('glpi_oauth_access_tokens', 'uuid', 'uuid', 'varchar(255)', ['null' => false, 'after' => 'identifier']); |
| 99 | +} |
0 commit comments