‪TYPO3CMS  ‪main
BackendGroupsExplicitAllowDenyMigration.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 
20 use Symfony\Component\Console\Output\OutputInterface;
23 use TYPO3\CMS\Core\Database\Query\QueryBuilder;
26 
31 #[UpgradeWizard('backendGroupsExplicitAllowDenyMigration')]
33 {
34  private const ‪TABLE_NAME = 'be_groups';
35 
36  private OutputInterface ‪$output;
37 
38  public function ‪getTitle(): string
39  {
40  return 'Migrate backend groups "explicit_allowdeny" field to simplified format.';
41  }
42 
43  public function ‪getDescription(): string
44  {
45  return 'Backend groups field "explicit_allowdeny" storage format has been simplified. This updates all be_groups records.';
46  }
47 
48  public function ‪getPrerequisites(): array
49  {
50  return [
51  DatabaseUpdatedPrerequisite::class,
52  ];
53  }
54 
55  public function ‪updateNecessary(): bool
56  {
57  $needsUpdate = false;
58  $queryBuilder = $this->‪getPreparedQueryBuilder();
59  $result = $queryBuilder->select('uid', 'explicit_allowdeny')->from(self::TABLE_NAME)->executeQuery();
60  while ($row = $result->fetchAssociative()) {
61  // Target is a comma separated list of colon seperated "tableName:fieldName:valueName"
62  // If there are four colon fields, the update should remove the last one.
63  $tuples = ‪GeneralUtility::trimExplode(',', (string)$row['explicit_allowdeny'], true);
64  foreach ($tuples as $tuple) {
65  if (count(‪GeneralUtility::trimExplode(':', $tuple, true)) > 3) {
66  $needsUpdate = true;
67  $result->free();
68  break 2;
69  }
70  }
71  }
72  return $needsUpdate;
73  }
74 
75  public function ‪executeUpdate(): bool
76  {
77  $connection = $this->‪getConnectionPool()->getConnectionForTable(self::TABLE_NAME);
78  $queryBuilder = $this->‪getPreparedQueryBuilder();
79  $result = $queryBuilder->select('uid', 'explicit_allowdeny')->from(self::TABLE_NAME)->executeQuery();
80  while ($row = $result->fetchAssociative()) {
81  $tuples = ‪GeneralUtility::trimExplode(',', (string)$row['explicit_allowdeny'], true);
82  $newTuples = [];
83  $updateNeeded = false;
84  foreach ($tuples as $tuple) {
85  $explodedTuples = ‪GeneralUtility::trimExplode(':', $tuple, true);
86  if (count($explodedTuples) > 3) {
87  $updateNeeded = true;
88  $newTupleString = implode(':', array_chunk($explodedTuples, 3, true)[0]);
89  if ($explodedTuples[3] === 'DENY') {
90  // We can't migrate 'explicitDeny' values. Fully remove that tuple and add an output note.
91  $this->output->writeln(
92  '<error>Access rights setup "Explicitly allow field values" of be_groups row uid "' . $row['uid'] . '"'
93  . ' had explicit DENY set for the table/field/value combination "' . $newTupleString . '".'
94  . ' This is not allowed anymore. This be_groups row needs a manual update to fix access rights.</error>'
95  );
96  continue;
97  }
98  $newTuples[] = $newTupleString;
99  } else {
100  $newTuples[] = $tuple;
101  }
102  }
103  if ($updateNeeded) {
104  $connection->update(
105  self::TABLE_NAME,
106  ['explicit_allowdeny' => implode(',', $newTuples)],
107  ['uid' => (int)$row['uid']],
108  ['explicit_allowdeny' => ‪Connection::PARAM_STR]
109  );
110  }
111  }
112  return true;
113  }
114 
115  public function ‪setOutput(OutputInterface ‪$output): void
116  {
117  $this->output = ‪$output;
118  }
119 
120  private function ‪getPreparedQueryBuilder(): QueryBuilder
121  {
122  $queryBuilder = $this->‪getConnectionPool()->getQueryBuilderForTable(self::TABLE_NAME);
123  $queryBuilder->getRestrictions()->removeAll();
124  return $queryBuilder->from(self::TABLE_NAME);
125  }
126 
128  {
129  return GeneralUtility::makeInstance(ConnectionPool::class);
130  }
131 }
‪TYPO3\CMS\Install\Updates\BackendGroupsExplicitAllowDenyMigration\getPrerequisites
‪getPrerequisites()
Definition: BackendGroupsExplicitAllowDenyMigration.php:48
‪TYPO3\CMS\Install\Updates\BackendGroupsExplicitAllowDenyMigration
Definition: BackendGroupsExplicitAllowDenyMigration.php:33
‪TYPO3\CMS\Install\Updates\ChattyInterface
Definition: ChattyInterface.php:26
‪TYPO3\CMS\Install\Attribute\UpgradeWizard
Definition: UpgradeWizard.php:25
‪TYPO3\CMS\Install\Updates\BackendGroupsExplicitAllowDenyMigration\TABLE_NAME
‪const TABLE_NAME
Definition: BackendGroupsExplicitAllowDenyMigration.php:34
‪TYPO3\CMS\Core\Database\Connection\PARAM_STR
‪const PARAM_STR
Definition: Connection.php:57
‪TYPO3\CMS\Install\Updates\BackendGroupsExplicitAllowDenyMigration\updateNecessary
‪updateNecessary()
Definition: BackendGroupsExplicitAllowDenyMigration.php:55
‪TYPO3\CMS\Install\Updates
Definition: LegacyClassesForIde.php:22
‪TYPO3\CMS\Install\Updates\BackendGroupsExplicitAllowDenyMigration\$output
‪OutputInterface $output
Definition: BackendGroupsExplicitAllowDenyMigration.php:36
‪TYPO3\CMS\Install\Updates\BackendGroupsExplicitAllowDenyMigration\getPreparedQueryBuilder
‪getPreparedQueryBuilder()
Definition: BackendGroupsExplicitAllowDenyMigration.php:120
‪TYPO3\CMS\Install\Updates\BackendGroupsExplicitAllowDenyMigration\getConnectionPool
‪getConnectionPool()
Definition: BackendGroupsExplicitAllowDenyMigration.php:127
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:41
‪TYPO3\CMS\Install\Updates\UpgradeWizardInterface
Definition: UpgradeWizardInterface.php:24
‪TYPO3\CMS\Install\Updates\BackendGroupsExplicitAllowDenyMigration\getDescription
‪getDescription()
Definition: BackendGroupsExplicitAllowDenyMigration.php:43
‪TYPO3\CMS\Install\Updates\BackendGroupsExplicitAllowDenyMigration\setOutput
‪setOutput(OutputInterface $output)
Definition: BackendGroupsExplicitAllowDenyMigration.php:115
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Install\Updates\BackendGroupsExplicitAllowDenyMigration\getTitle
‪getTitle()
Definition: BackendGroupsExplicitAllowDenyMigration.php:38
‪TYPO3\CMS\Install\Updates\BackendGroupsExplicitAllowDenyMigration\executeUpdate
‪executeUpdate()
Definition: BackendGroupsExplicitAllowDenyMigration.php:75
‪TYPO3\CMS\Core\Utility\GeneralUtility\trimExplode
‪static list< string > trimExplode(string $delim, string $string, bool $removeEmptyValues=false, int $limit=0)
Definition: GeneralUtility.php:822