‪TYPO3CMS  9.5
CommandLineBackendUserRemovalUpdate.php
Go to the documentation of this file.
1 <?php
2 
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 
18 use Symfony\Component\Console\Output\OutputInterface;
22 
28 {
32  protected ‪$output;
33 
37  protected ‪$confirmation;
38 
39  public function ‪__construct()
40  {
41  $this->confirmation = new ‪Confirmation(
42  'Are you sure?',
43  'The following backend users will be removed: ' . implode(', ', $this->‪getUnneededCommandLineUsers()),
44  true
45  );
46  }
47 
51  public function ‪getIdentifier(): string
52  {
53  return 'commandLineBackendUserRemovalUpdate';
54  }
55 
59  public function ‪getTitle(): string
60  {
61  return 'Remove unneeded CLI backend users';
62  }
63 
67  public function ‪getDescription(): string
68  {
69  return 'The command line interface does not need to have custom _cli_* backend users anymore.'
70  . ' They can safely be deleted.';
71  }
72 
78  public function ‪updateNecessary(): bool
79  {
80  $needsExecution = false;
81  $usersFound = $this->‪getUnneededCommandLineUsers();
82  if (!empty($usersFound)) {
83  $needsExecution = true;
84  }
85  return $needsExecution;
86  }
87 
91  public function ‪setOutput(OutputInterface ‪$output): void
92  {
93  $this->output = ‪$output;
94  }
95 
99  public function ‪getPrerequisites(): array
100  {
101  return [
102  DatabaseUpdatedPrerequisite::class,
103  ];
104  }
105 
111  public function ‪executeUpdate(): bool
112  {
113  $usersFound = $this->‪getUnneededCommandLineUsers();
114  foreach ($usersFound as $userUid => $username) {
115  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('be_users');
116  $queryBuilder->update('be_users')
117  ->where(
118  $queryBuilder->expr()->eq(
119  'uid',
120  $queryBuilder->createNamedParameter($userUid, \PDO::PARAM_INT)
121  )
122  )
123  // "false" is set as third parameter to have the final
124  // value in $databaseQueries and not a statement placeholder
125  ->set('deleted', 1, false)
126  ->execute();
127  }
128  $this->output->writeln('The following backend users have been deleted:');
129  foreach ($usersFound as $user) {
130  $this->output->writeln('* ' . $user);
131  }
132  return true;
133  }
134 
140  protected function ‪getUnneededCommandLineUsers(): array
141  {
142  $commandLineUsers = [];
143 
144  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
145  ->getQueryBuilderForTable('be_users');
146  $queryBuilder->getRestrictions()
147  ->removeAll()
148  ->add(GeneralUtility::makeInstance(DeletedRestriction::class));
149 
150  $result = $queryBuilder
151  ->select('uid', 'username')
152  ->from('be_users')
153  ->where(
154  // Using query builder is complicated in this case. Get it straight, no user input is involved.
155  'LOWER(username) LIKE \'_cli_%\'',
156  $queryBuilder->expr()->neq(
157  'username',
158  $queryBuilder->createNamedParameter('_cli_', \PDO::PARAM_STR)
159  )
160  )
161  ->execute();
162 
163  while ($row = $result->fetch()) {
164  $commandLineUsers[$row['uid']] = $row['username'];
165  }
166 
167  return $commandLineUsers;
168  }
169 
175  public function ‪getConfirmation(): Confirmation
176  {
177  return ‪$this->confirmation;
178  }
179 }
‪TYPO3\CMS\Install\Updates\CommandLineBackendUserRemovalUpdate
Definition: CommandLineBackendUserRemovalUpdate.php:28
‪TYPO3\CMS\Install\Updates\CommandLineBackendUserRemovalUpdate\getConfirmation
‪Confirmation getConfirmation()
Definition: CommandLineBackendUserRemovalUpdate.php:173
‪TYPO3\CMS\Install\Updates\CommandLineBackendUserRemovalUpdate\$confirmation
‪Confirmation $confirmation
Definition: CommandLineBackendUserRemovalUpdate.php:35
‪TYPO3\CMS\Install\Updates\RepeatableInterface
Definition: RepeatableInterface.php:25
‪TYPO3\CMS\Install\Updates\CommandLineBackendUserRemovalUpdate\setOutput
‪setOutput(OutputInterface $output)
Definition: CommandLineBackendUserRemovalUpdate.php:89
‪TYPO3\CMS\Install\Updates\ChattyInterface
Definition: ChattyInterface.php:25
‪TYPO3\CMS\Install\Updates\CommandLineBackendUserRemovalUpdate\getTitle
‪string getTitle()
Definition: CommandLineBackendUserRemovalUpdate.php:57
‪TYPO3\CMS\Install\Updates
Definition: AbstractDownloadExtensionUpdate.php:3
‪TYPO3\CMS\Install\Updates\CommandLineBackendUserRemovalUpdate\$output
‪OutputInterface $output
Definition: CommandLineBackendUserRemovalUpdate.php:31
‪TYPO3\CMS\Install\Updates\CommandLineBackendUserRemovalUpdate\getDescription
‪string getDescription()
Definition: CommandLineBackendUserRemovalUpdate.php:65
‪TYPO3\CMS\Install\Updates\CommandLineBackendUserRemovalUpdate\getUnneededCommandLineUsers
‪array getUnneededCommandLineUsers()
Definition: CommandLineBackendUserRemovalUpdate.php:138
‪TYPO3\CMS\Install\Updates\Confirmation
Definition: Confirmation.php:20
‪TYPO3\CMS\Install\Updates\CommandLineBackendUserRemovalUpdate\updateNecessary
‪bool updateNecessary()
Definition: CommandLineBackendUserRemovalUpdate.php:76
‪TYPO3\CMS\Install\Updates\CommandLineBackendUserRemovalUpdate\getIdentifier
‪string getIdentifier()
Definition: CommandLineBackendUserRemovalUpdate.php:49
‪TYPO3\CMS\Install\Updates\CommandLineBackendUserRemovalUpdate\executeUpdate
‪bool executeUpdate()
Definition: CommandLineBackendUserRemovalUpdate.php:109
‪TYPO3\CMS\Install\Updates\CommandLineBackendUserRemovalUpdate\getPrerequisites
‪string[] getPrerequisites()
Definition: CommandLineBackendUserRemovalUpdate.php:97
‪TYPO3\CMS\Install\Updates\UpgradeWizardInterface
Definition: UpgradeWizardInterface.php:22
‪TYPO3\CMS\Install\Updates\CommandLineBackendUserRemovalUpdate\__construct
‪__construct()
Definition: CommandLineBackendUserRemovalUpdate.php:37
‪TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction
Definition: DeletedRestriction.php:26
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:44
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Install\Updates\ConfirmableInterface
Definition: ConfirmableInterface.php:23