‪TYPO3CMS  ‪main
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  $schemaManager = $this->‪createSchemaManager();
59  $schemaCurrent = $schemaManager->introspectSchema();
60  $schemaNew = $schemaManager->introspectSchema();
61  $schemaDiff = $schemaManager->createComparator()->compareSchemas($schemaCurrent, $schemaNew);
62  $schemaNew
63  ->getTable($this->testTableName)
64  ->addColumn('index_test', 'integer', ['unsigned' => true]);
65  $platform = $connection->getDatabasePlatform();
66  try {
67  foreach ($platform->getAlterSchemaSQL($schemaDiff) as $query) {
68  $connection->executeQuery($query);
69  }
70  } catch (\‪Exception) {
71  $this->messages[] = 'The database user needs ALTER permission';
72  }
73  $this->‪checkDropTable($this->testTableName);
74  return $this;
75  }
76 
77  public function ‪checkIndex(): self
78  {
79  if ($this->‪checkCreateTable($this->testTableName)) {
80  $connection = $this->‪getConnection();
81  $schemaManager = $this->‪createSchemaManager();
82  $schemaCurrent = $schemaManager->introspectSchema();
83  $schemaNew = $schemaManager->introspectSchema();
84  $testTable = $schemaNew->getTable($this->testTableName);
85  $testTable->addColumn('index_test', 'integer', ['unsigned' => true]);
86  $testTable->addIndex(['index_test'], 'test_index');
87  $schemaDiff = $schemaManager->createComparator()->compareSchemas($schemaCurrent, $schemaNew);
88  $platform = $connection->getDatabasePlatform();
89  try {
90  $statements = $platform->getAlterSchemaSQL($schemaDiff);
91  foreach ($statements as $query) {
92  $connection->executeQuery($query);
93  }
94  } catch (\‪Exception) {
95  $this->messages[] = 'The database user needs INDEX permission';
96  }
97  $this->‪checkDropTable($this->testTableName);
98  }
99  return $this;
100  }
101 
102  public function ‪checkCreateTemporaryTable(): self
103  {
104  $this->‪checkCreateTable($this->testTableName);
105  $connection = $this->‪getConnection();
106  try {
107  $sql = 'CREATE TEMPORARY TABLE %s AS (SELECT id FROM %s )';
108  $connection->executeStatement(sprintf($sql, $this->testTableName . '_tmp', $this->testTableName));
109  } catch (\‪Exception) {
110  $this->messages[] = 'The database user needs CREATE TEMPORARY TABLE permission';
111  }
112  $this->‪checkDropTable($this->testTableName);
113  return $this;
114  }
115 
116  public function ‪checkSelect(): self
117  {
118  $this->‪checkCreateTable($this->testTableName);
119  $connection = $this->‪getConnection();
120  try {
121  $connection->insert($this->testTableName, ['id' => 1]);
122  $connection->select(['id'], $this->testTableName);
123  } catch (\‪Exception) {
124  $this->messages[] = 'The database user needs SELECT permission';
125  }
126  $this->‪checkDropTable($this->testTableName);
127  return $this;
128  }
129 
130  public function ‪checkInsert(): self
131  {
132  $this->‪checkCreateTable($this->testTableName);
133  $connection = $this->‪getConnection();
134  try {
135  $connection->insert($this->testTableName, ['id' => 1]);
136  } catch (\‪Exception) {
137  $this->messages[] = 'The database user needs INSERT permission';
138  }
139  $this->‪checkDropTable($this->testTableName);
140  return $this;
141  }
142 
143  public function ‪checkUpdate(): self
144  {
145  $this->‪checkCreateTable($this->testTableName);
146  $connection = $this->‪getConnection();
147  try {
148  $connection->insert($this->testTableName, ['id' => 1]);
149  $connection->update($this->testTableName, ['id' => 2], ['id' => 1]);
150  } catch (\‪Exception) {
151  $this->messages[] = 'The database user needs UPDATE permission';
152  }
153  $this->‪checkDropTable($this->testTableName);
154  return $this;
155  }
156 
157  public function ‪checkDelete(): self
158  {
159  $this->‪checkCreateTable($this->testTableName);
160  $connection = $this->‪getConnection();
161  try {
162  $connection->insert($this->testTableName, ['id' => 1]);
163  $connection->delete($this->testTableName, ['id' => 1]);
164  } catch (\‪Exception) {
165  $this->messages[] = 'The database user needs DELETE permission';
166  }
167  $this->‪checkDropTable($this->testTableName);
168  return $this;
169  }
170 
171  public function ‪getMessages(): array
172  {
173  return ‪$this->messages;
174  }
175 
176  private function ‪checkCreateTable(string $tablename): bool
177  {
178  $connection = $this->‪getConnection();
179  $schema = $connection->createSchemaManager()->introspectSchema();
180  $testTable = $schema->createTable($tablename);
181  $testTable->addColumn('id', 'integer', ['unsigned' => true]);
182  $testTable->setPrimaryKey(['id']);
183  $platform = $connection->getDatabasePlatform();
184  try {
185  foreach ($schema->toSql($platform) as $query) {
186  $connection->executeQuery($query);
187  }
188  } catch (\‪Exception) {
189  return false;
190  }
191  return true;
192  }
193 
194  private function ‪checkDropTable(string $tablename): bool
195  {
196  $connection = $this->‪getConnection();
197  try {
198  $schemaManager = $connection->createSchemaManager();
199  $schemaCurrent = $schemaManager->introspectSchema();
200  $schemaNew = $schemaManager->introspectSchema();
201 
202  $schemaNew->dropTable($tablename);
203  $schemaDiff = $schemaManager->createComparator()->compareSchemas($schemaCurrent, $schemaNew);
204  $platform = $connection->getDatabasePlatform();
205  foreach ($platform->getAlterSchemaSQL($schemaDiff) as $query) {
206  $connection->executeQuery($query);
207  }
208  } catch (\‪Exception) {
209  return false;
210  }
211  return true;
212  }
213 
214  private function ‪getConnection(): ‪Connection
215  {
216  return GeneralUtility::makeInstance(ConnectionPool::class)
217  ->getConnectionByName(‪ConnectionPool::DEFAULT_CONNECTION_NAME);
218  }
219 
220  private function ‪createSchemaManager(): AbstractSchemaManager
221  {
222  return $this->‪getConnection()->createSchemaManager();
223  }
224 }
‪TYPO3\CMS\Install\Database\PermissionsCheck\checkSelect
‪checkSelect()
Definition: PermissionsCheck.php:116
‪TYPO3\CMS\Install\Database\PermissionsCheck\checkUpdate
‪checkUpdate()
Definition: PermissionsCheck.php:143
‪TYPO3\CMS\Install\Database\PermissionsCheck\$messages
‪$messages
Definition: PermissionsCheck.php:33
‪TYPO3\CMS\Install\Configuration\Exception
Definition: Exception.php:21
‪TYPO3\CMS\Install\Database\PermissionsCheck\createSchemaManager
‪createSchemaManager()
Definition: PermissionsCheck.php:220
‪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\getConnection
‪getConnection()
Definition: PermissionsCheck.php:214
‪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:176
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:41
‪TYPO3\CMS\Install\Database\PermissionsCheck\$testTableName
‪$testTableName
Definition: PermissionsCheck.php:32
‪TYPO3\CMS\Install\Database\PermissionsCheck\checkInsert
‪checkInsert()
Definition: PermissionsCheck.php:130
‪TYPO3\CMS\Install\Database\PermissionsCheck\checkDropTable
‪checkDropTable(string $tablename)
Definition: PermissionsCheck.php:194
‪TYPO3\CMS\Install\Database\PermissionsCheck\checkDelete
‪checkDelete()
Definition: PermissionsCheck.php:157
‪TYPO3\CMS\Install\Database\PermissionsCheck\checkIndex
‪checkIndex()
Definition: PermissionsCheck.php:77
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Install\Database\PermissionsCheck\getMessages
‪getMessages()
Definition: PermissionsCheck.php:171
‪TYPO3\CMS\Install\Database\PermissionsCheck\checkCreateTemporaryTable
‪checkCreateTemporaryTable()
Definition: PermissionsCheck.php:102