[BUGFIX] Respect casing in CommandLineBackendUserRemovalUpdate
[Packages/TYPO3.CMS.git] / typo3 / sysext / install / Classes / Updates / CommandLineBackendUserRemovalUpdate.php
1 <?php
2 namespace TYPO3\CMS\Install\Updates;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Database\ConnectionPool;
18 use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20
21 /**
22 * Remove all backend users starting with _cli_
23 */
24 class CommandLineBackendUserRemovalUpdate extends AbstractUpdate
25 {
26 /**
27 * @var string
28 */
29 protected $title = 'Remove unneeded CLI backend users';
30
31 /**
32 * Checks if an update is needed
33 *
34 * @param string &$description The description for the update
35 * @return bool Whether an update is needed (TRUE) or not (FALSE)
36 */
37 public function checkForUpdate(&$description)
38 {
39 if ($this->isWizardDone()) {
40 return false;
41 }
42 $needsExecution = false;
43 $usersFound = $this->getUnneededCommandLineUsers();
44 if (!empty($usersFound)) {
45 $needsExecution = true;
46 $description = 'The command line interface does not need to have custom _cli_* backend users anymore. They can safely be deleted.';
47 }
48 return $needsExecution;
49 }
50
51 /**
52 * Shows information on the next step of the page
53 * @param string $formFieldNamePrefix
54 * @return string
55 */
56 public function getUserInput($formFieldNamePrefix)
57 {
58 $usersFound = $this->getUnneededCommandLineUsers();
59 return '<p>The following backend users will be deleted:</p><ul><li>' . implode('</li><li>', $usersFound) . '</li></ul>';
60 }
61
62 /**
63 * Performs the database update to set all be_users starting with _CLI_* to deleted
64 *
65 * @param array &$databaseQueries Queries done in this update
66 * @param string &$customMessage Custom message
67 * @return bool
68 */
69 public function performUpdate(array &$databaseQueries, &$customMessage)
70 {
71 $usersFound = $this->getUnneededCommandLineUsers();
72 foreach ($usersFound as $userUid => $username) {
73 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('be_users');
74 $queryBuilder->update('be_users')
75 ->where(
76 $queryBuilder->expr()->eq(
77 'uid',
78 $queryBuilder->createNamedParameter($userUid, \PDO::PARAM_INT)
79 )
80 )
81 // "false" is set as third parameter to have the final
82 // value in $databaseQueries and not a statement placeholder
83 ->set('deleted', 1, false)
84 ->execute();
85 $databaseQueries[] = $queryBuilder->getSQL();
86 }
87 $customMessage = '<p>The following backend users have been deleted:</p><ul><li>' . implode('</li><li>', $usersFound) . '</li></ul>';
88 $this->markWizardAsDone();
89 return true;
90 }
91
92 /**
93 * Find all backend users starting with _CLI_ that are not deleted yet.
94 *
95 * @return array a list of uids
96 */
97 protected function getUnneededCommandLineUsers()
98 {
99 $commandLineUsers = [];
100
101 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
102 ->getQueryBuilderForTable('be_users');
103 $queryBuilder->getRestrictions()
104 ->removeAll()
105 ->add(GeneralUtility::makeInstance(DeletedRestriction::class));
106
107 $result = $queryBuilder
108 ->select('uid', 'username')
109 ->from('be_users')
110 ->where(
111 // Using query builder is complicated in this case. Get it straight, no user input is involved.
112 'LOWER(username) LIKE \'_cli_%\'',
113 $queryBuilder->expr()->neq(
114 'username',
115 $queryBuilder->createNamedParameter('_cli_', \PDO::PARAM_STR)
116 )
117 )
118 ->execute();
119
120 while ($row = $result->fetch()) {
121 $commandLineUsers[$row['uid']] = $row['username'];
122 }
123
124 return $commandLineUsers;
125 }
126 }