‪TYPO3CMS  ‪main
BackendModulePermissionMigration.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
22 use TYPO3\CMS\Core\Database\Query\QueryBuilder;
25 
30 #[UpgradeWizard('backendModulePermission')]
32 {
33  protected array ‪$aliases = [];
34 
35  public function ‪__construct()
36  {
37  $this->aliases = GeneralUtility::makeInstance(ModuleRegistry::class)->getModuleAliases();
38  }
39 
40  public function ‪getTitle(): string
41  {
42  return 'Migrate backend user and groups to new module names.';
43  }
44 
45  public function ‪getDescription(): string
46  {
47  return 'Update backend user and groups to migrate to possible new module names.';
48  }
49 
50  public function ‪getPrerequisites(): array
51  {
52  return [];
53  }
54 
55  public function ‪updateNecessary(): bool
56  {
57  return $this->aliases !== []
58  && (
59  $this->‪hasRecordsToUpdate('be_groups', 'groupMods')
60  || $this->‪hasRecordsToUpdate('be_users', 'userMods')
61  );
62  }
63 
64  public function ‪executeUpdate(): bool
65  {
66  $this->‪updateRecords('be_groups', 'groupMods');
67  $this->‪updateRecords('be_users', 'userMods');
68  return true;
69  }
70 
71  protected function ‪hasRecordsToUpdate(string $table, string $field): bool
72  {
73  $queryBuilder = $this->‪getPreparedQueryBuilder($table, $field);
74  $statement = $queryBuilder->select($field)->executeQuery();
75  $aliasIdentifiers = array_keys($this->aliases);
76  while (‪$record = $statement->fetchAssociative()) {
77  $selectedModules = ‪GeneralUtility::trimExplode(',', ‪$record[$field], true);
78  if (array_intersect($selectedModules, $aliasIdentifiers) !== []) {
79  return true;
80  }
81  }
82  return false;
83  }
84 
85  protected function ‪updateRecords(string $table, string $field): void
86  {
87  $connection = $this->‪getConnectionPool()->getConnectionForTable($table);
88  $statement = $this->‪getPreparedQueryBuilder($table, $field)->select('uid', $field)->executeQuery();
89  $aliasIdentifiers = array_keys($this->aliases);
90  while (‪$record = $statement->fetchAssociative()) {
91  $selectedModules = ‪GeneralUtility::trimExplode(',', ‪$record[$field], true);
92  if (array_intersect($selectedModules, $aliasIdentifiers) === []) {
93  continue;
94  }
95  $newModules = [];
96  foreach ($selectedModules as $moduleIdentifier) {
97  $newModules[] = $this->aliases[$moduleIdentifier] ?? $moduleIdentifier;
98  }
99  $connection->update($table, [$field => implode(',', $newModules)], ['uid' => ‪$record['uid']]);
100  }
101  }
102 
103  protected function ‪getPreparedQueryBuilder(string $table, string $field): QueryBuilder
104  {
105  $queryBuilder = $this->‪getConnectionPool()->getQueryBuilderForTable($table);
106  $queryBuilder->getRestrictions()->removeAll();
107  $queryBuilder
108  ->from($table)
109  ->where(
110  $queryBuilder->expr()->isNotNull($field),
111  $queryBuilder->expr()->neq($field, $queryBuilder->createNamedParameter('')),
112  );
113 
114  return $queryBuilder;
115  }
116 
118  {
119  return GeneralUtility::makeInstance(ConnectionPool::class);
120  }
121 }
‪TYPO3\CMS\Install\Updates\BackendModulePermissionMigration
Definition: BackendModulePermissionMigration.php:32
‪TYPO3\CMS\Backend\Module\ModuleRegistry
Definition: ModuleRegistry.php:28
‪TYPO3\CMS\Install\Updates\BackendModulePermissionMigration\hasRecordsToUpdate
‪hasRecordsToUpdate(string $table, string $field)
Definition: BackendModulePermissionMigration.php:71
‪TYPO3\CMS\Install\Updates\BackendModulePermissionMigration\getDescription
‪getDescription()
Definition: BackendModulePermissionMigration.php:45
‪TYPO3\CMS\Install\Attribute\UpgradeWizard
Definition: UpgradeWizard.php:25
‪TYPO3\CMS\Install\Updates\BackendModulePermissionMigration\getPreparedQueryBuilder
‪getPreparedQueryBuilder(string $table, string $field)
Definition: BackendModulePermissionMigration.php:103
‪TYPO3\CMS\Install\Updates\BackendModulePermissionMigration\updateRecords
‪updateRecords(string $table, string $field)
Definition: BackendModulePermissionMigration.php:85
‪TYPO3\CMS\Install\Updates
Definition: LegacyClassesForIde.php:22
‪TYPO3\CMS\Install\Updates\BackendModulePermissionMigration\getPrerequisites
‪getPrerequisites()
Definition: BackendModulePermissionMigration.php:50
‪TYPO3\CMS\Install\Updates\BackendModulePermissionMigration\$aliases
‪array $aliases
Definition: BackendModulePermissionMigration.php:33
‪TYPO3\CMS\Install\Updates\BackendModulePermissionMigration\getTitle
‪getTitle()
Definition: BackendModulePermissionMigration.php:40
‪TYPO3\CMS\Webhooks\Message\$record
‪identifier readonly int readonly array $record
Definition: PageModificationMessage.php:36
‪TYPO3\CMS\Install\Updates\BackendModulePermissionMigration\executeUpdate
‪executeUpdate()
Definition: BackendModulePermissionMigration.php:64
‪TYPO3\CMS\Install\Updates\BackendModulePermissionMigration\__construct
‪__construct()
Definition: BackendModulePermissionMigration.php:35
‪TYPO3\CMS\Install\Updates\UpgradeWizardInterface
Definition: UpgradeWizardInterface.php:24
‪TYPO3\CMS\Install\Updates\BackendModulePermissionMigration\getConnectionPool
‪getConnectionPool()
Definition: BackendModulePermissionMigration.php:117
‪TYPO3\CMS\Install\Updates\BackendModulePermissionMigration\updateNecessary
‪updateNecessary()
Definition: BackendModulePermissionMigration.php:55
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Utility\GeneralUtility\trimExplode
‪static list< string > trimExplode(string $delim, string $string, bool $removeEmptyValues=false, int $limit=0)
Definition: GeneralUtility.php:817