TYPO3 CMS  TYPO3_8-7
CommandLineBackendUserRemovalUpdate.php
Go to the documentation of this file.
1 <?php
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 
20 
25 {
29  protected $title = 'Remove unneeded CLI backend users';
30 
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 
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 
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 
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 }
static makeInstance($className,... $constructorArguments)