‪TYPO3CMS  10.4
PermissionsCheck.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 Doctrine\DBAL\Schema\AbstractSchemaManager;
25 
31 {
32  private ‪$testTableName = 't3install_test_table';
33  private ‪$messages = [];
34 
35  public function ‪checkCreateAndDrop(): self
36  {
37  $tableCreated = $this->‪checkCreateTable($this->testTableName);
38  if (!$tableCreated) {
39  $this->messages[] = 'The database user needs CREATE permissions.';
40  }
41  $tableDropped = $this->‪checkDropTable($this->testTableName);
42  if (!$tableDropped) {
43  $this->messages[] = 'The database user needs DROP permissions.';
44  }
45  if ($tableCreated && !$tableDropped) {
46  $this->messages[] = sprintf('Attention: A test table with name "%s" was created but could not be deleted, please remove the table manually!', $this->testTableName);
47  }
48  if (!$tableCreated || !$tableDropped) {
49  throw new ‪Exception('A test table could not be created or dropped, skipping all further checks now', 1590850369);
50  }
51  return $this;
52  }
53 
54  public function ‪checkAlter(): self
55  {
56  $this->‪checkCreateTable($this->testTableName);
57  $connection = $this->‪getConnection();
58  $schemaCurrent = $this->‪getSchemaManager()->createSchema();
59  $schemaNew = $this->‪getSchemaManager()->createSchema();
60  $schemaCurrent
61  ->getTable($this->testTableName)
62  ->addColumn('index_test', 'integer', ['unsigned' => true]);
63  $platform = $connection->getDatabasePlatform();
64  try {
65  foreach ($schemaNew->getMigrateToSql($schemaCurrent, $platform) as $query) {
66  $connection->executeQuery($query);
67  }
68  } catch (\‪Exception $e) {
69  $this->messages[] = 'The database user needs ALTER permission';
70  }
71  $this->‪checkDropTable($this->testTableName);
72  return $this;
73  }
74 
75  public function ‪checkIndex(): self
76  {
77  if ($this->‪checkCreateTable($this->testTableName)) {
78  $connection = $this->‪getConnection();
79  $schemaCurrent = $this->‪getSchemaManager()->createSchema();
80  $schemaNew = $this->‪getSchemaManager()->createSchema();
81  $testTable = $schemaCurrent->getTable($this->testTableName);
82  $testTable->addColumn('index_test', 'integer', ['unsigned' => true]);
83  $testTable->addIndex(['index_test'], 'test_index');
84  $platform = $connection->getDatabasePlatform();
85  try {
86  foreach ($schemaNew->getMigrateToSql($schemaCurrent, $platform) as $query) {
87  $connection->executeQuery($query);
88  }
89  } catch (\‪Exception $e) {
90  $this->‪checkDropTable($this->testTableName);
91  $this->messages[] = 'The database user needs INDEX permission';
92  }
93  $this->‪checkDropTable($this->testTableName);
94  }
95  return $this;
96  }
97 
98  public function ‪checkCreateTemporaryTable(): self
99  {
100  $this->‪checkCreateTable($this->testTableName);
101  $connection = $this->‪getConnection();
102  try {
103  $sql = 'CREATE TEMPORARY TABLE %s AS (SELECT id FROM %s )';
104  $connection->exec(sprintf($sql, $this->testTableName . '_tmp', $this->testTableName));
105  } catch (\‪Exception $e) {
106  $this->messages[] = 'The database user needs CREATE TEMPORARY TABLE permission';
107  }
108  $this->‪checkDropTable($this->testTableName);
109  return $this;
110  }
111 
112  public function ‪checkSelect(): self
113  {
114  $this->‪checkCreateTable($this->testTableName);
115  $connection = $this->‪getConnection();
116  try {
117  $connection->insert($this->testTableName, ['id' => 1]);
118  $connection->select(['id'], $this->testTableName);
119  } catch (\‪Exception $e) {
120  $this->messages[] = 'The database user needs SELECT permission';
121  }
122  $this->‪checkDropTable($this->testTableName);
123  return $this;
124  }
125 
126  public function ‪checkInsert(): self
127  {
128  $this->‪checkCreateTable($this->testTableName);
129  $connection = $this->‪getConnection();
130  try {
131  $connection->insert($this->testTableName, ['id' => 1]);
132  } catch (\‪Exception $e) {
133  $this->messages[] = 'The database user needs INSERT permission';
134  }
135  $this->‪checkDropTable($this->testTableName);
136  return $this;
137  }
138 
139  public function ‪checkUpdate(): self
140  {
141  $this->‪checkCreateTable($this->testTableName);
142  $connection = $this->‪getConnection();
143  try {
144  $connection->insert($this->testTableName, ['id' => 1]);
145  $connection->update($this->testTableName, ['id' => 2], ['id' => 1]);
146  } catch (\‪Exception $e) {
147  $this->messages[] = 'The database user needs UPDATE permission';
148  }
149  $this->‪checkDropTable($this->testTableName);
150  return $this;
151  }
152 
153  public function ‪checkDelete(): self
154  {
155  $this->‪checkCreateTable($this->testTableName);
156  $connection = $this->‪getConnection();
157  try {
158  $connection->insert($this->testTableName, ['id' => 1]);
159  $connection->delete($this->testTableName, ['id' => 1]);
160  } catch (\‪Exception $e) {
161  $this->messages[] = 'The database user needs DELETE permission';
162  }
163  $this->‪checkDropTable($this->testTableName);
164  return $this;
165  }
166 
167  public function ‪getMessages(): array
168  {
169  return ‪$this->messages;
170  }
171 
172  private function ‪checkCreateTable(string $tablename): bool
173  {
174  $connection = $this->‪getConnection();
175  $schema = $connection->getSchemaManager()->createSchema();
176  $testTable = $schema->createTable($tablename);
177  $testTable->addColumn('id', 'integer', ['unsigned' => true]);
178  $testTable->setPrimaryKey(['id']);
179  $platform = $connection->getDatabasePlatform();
180  try {
181  foreach ($schema->toSql($platform) as $query) {
182  $connection->executeQuery($query);
183  }
184  } catch (\‪Exception $e) {
185  return false;
186  }
187  return true;
188  }
189 
190  private function ‪checkDropTable(string $tablename): bool
191  {
192  $connection = $this->‪getConnection();
193  try {
194  $schemaCurrent = $connection->getSchemaManager()->createSchema();
195  $schemaNew = $connection->getSchemaManager()->createSchema();
196 
197  $schemaNew->dropTable($tablename);
198  $platform = $connection->getDatabasePlatform();
199  foreach ($schemaCurrent->getMigrateToSql($schemaNew, $platform) as $query) {
200  $connection->executeQuery($query);
201  }
202  } catch (\‪Exception $e) {
203  return false;
204  }
205  return true;
206  }
207 
208  private function ‪getConnection(): ‪Connection
209  {
210  return GeneralUtility::makeInstance(ConnectionPool::class)
211  ->getConnectionByName(‪ConnectionPool::DEFAULT_CONNECTION_NAME);
212  }
213 
214  private function ‪getSchemaManager(): AbstractSchemaManager
215  {
216  return $this->‪getConnection()->getSchemaManager();
217  }
218 }
‪TYPO3\CMS\Install\Database\PermissionsCheck\checkSelect
‪checkSelect()
Definition: PermissionsCheck.php:112
‪TYPO3\CMS\Install\Database\PermissionsCheck\checkUpdate
‪checkUpdate()
Definition: PermissionsCheck.php:139
‪TYPO3\CMS\Install\Database\PermissionsCheck\$messages
‪$messages
Definition: PermissionsCheck.php:33
‪TYPO3\CMS\Install\Configuration\Exception
Definition: Exception.php:22
‪TYPO3\CMS\Core\Database\ConnectionPool\DEFAULT_CONNECTION_NAME
‪const DEFAULT_CONNECTION_NAME
Definition: ConnectionPool.php:50
‪TYPO3\CMS\Install\Database\PermissionsCheck\checkCreateAndDrop
‪checkCreateAndDrop()
Definition: PermissionsCheck.php:35
‪TYPO3\CMS\Install\Database\PermissionsCheck
Definition: PermissionsCheck.php:31
‪TYPO3\CMS\Install\Database\PermissionsCheck\getSchemaManager
‪getSchemaManager()
Definition: PermissionsCheck.php:214
‪TYPO3\CMS\Install\Database\PermissionsCheck\getConnection
‪getConnection()
Definition: PermissionsCheck.php:208
‪TYPO3\CMS\Install\Database\PermissionsCheck\checkAlter
‪checkAlter()
Definition: PermissionsCheck.php:54
‪TYPO3\CMS\Install\Database
Definition: PermissionsCheck.php:18
‪TYPO3\CMS\Install\Database\PermissionsCheck\checkCreateTable
‪checkCreateTable(string $tablename)
Definition: PermissionsCheck.php:172
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:36
‪TYPO3\CMS\Install\Database\PermissionsCheck\$testTableName
‪$testTableName
Definition: PermissionsCheck.php:32
‪TYPO3\CMS\Install\Database\PermissionsCheck\checkInsert
‪checkInsert()
Definition: PermissionsCheck.php:126
‪TYPO3\CMS\Install\Database\PermissionsCheck\checkDropTable
‪checkDropTable(string $tablename)
Definition: PermissionsCheck.php:190
‪TYPO3\CMS\Install\Database\PermissionsCheck\checkDelete
‪checkDelete()
Definition: PermissionsCheck.php:153
‪TYPO3\CMS\Install\Database\PermissionsCheck\checkIndex
‪checkIndex()
Definition: PermissionsCheck.php:75
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Install\Database\PermissionsCheck\getMessages
‪getMessages()
Definition: PermissionsCheck.php:167
‪TYPO3\CMS\Install\Database\PermissionsCheck\checkCreateTemporaryTable
‪checkCreateTemporaryTable()
Definition: PermissionsCheck.php:98