4da08fe92898e67b5f270734087d84d6e82bb4ff
[Packages/TYPO3.CMS.git] / typo3 / sysext / install / Classes / Updates / FilesReplacePermissionUpdate.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 /**
18 * Upgrade wizard which goes through all users and groups and set the "replaceFile" permission if "writeFile" is set
19 */
20 class FilesReplacePermissionUpdate extends AbstractUpdate
21 {
22 /**
23 * @var string
24 */
25 protected $title = 'Set the "Files:replace" permission for all BE user/groups with "Files:write" set';
26
27 /**
28 * Checks whether updates are required.
29 *
30 * @param string &$description The description for the update
31 * @return bool Whether an update is required (TRUE) or not (FALSE)
32 */
33 public function checkForUpdate(&$description)
34 {
35 if ($this->isWizardDone()) {
36 return false;
37 }
38 $description = 'A new file permission was introduced regarding replacing files.' .
39 ' This update sets "Files:replace" for all BE users/groups with the permission "Files:write".';
40 $updateNeeded = false;
41 $db = $this->getDatabaseConnection();
42
43 // Fetch user records where the writeFile is set and replaceFile is not
44 $notMigratedRowsCount = $db->exec_SELECTcountRows(
45 'uid',
46 'be_users',
47 $this->getWhereClause()
48 );
49 if ($notMigratedRowsCount > 0) {
50 $updateNeeded = true;
51 }
52
53 if (!$updateNeeded) {
54 // Fetch group records where the writeFile is set and replaceFile is not
55 $notMigratedRowsCount = $db->exec_SELECTcountRows(
56 'uid',
57 'be_groups',
58 $this->getWhereClause()
59 );
60 if ($notMigratedRowsCount > 0) {
61 $updateNeeded = true;
62 }
63 }
64 return $updateNeeded;
65 }
66
67 /**
68 * Performs the accordant updates.
69 *
70 * @param array &$dbQueries Queries done in this update
71 * @param mixed &$customMessages Custom messages
72 * @return bool Whether everything went smoothly or not
73 */
74 public function performUpdate(array &$dbQueries, &$customMessages)
75 {
76 $db = $this->getDatabaseConnection();
77
78 // Iterate over users and groups table to perform permission updates
79 $tablesToProcess = ['be_groups', 'be_users'];
80 foreach ($tablesToProcess as $table) {
81 $records = $this->getRecordsFromTable($table);
82 foreach ($records as $singleRecord) {
83 $updateArray = [
84 'file_permissions' => $singleRecord['file_permissions'] . ',replaceFile'
85 ];
86 $db->exec_UPDATEquery($table, 'uid=' . (int)$singleRecord['uid'], $updateArray);
87 // Get last executed query
88 $dbQueries[] = str_replace(chr(10), ' ', $db->debug_lastBuiltQuery);
89 // Check for errors
90 if ($db->sql_error()) {
91 $customMessages = 'SQL-ERROR: ' . htmlspecialchars($db->sql_error());
92 return false;
93 }
94 }
95 }
96 $this->markWizardAsDone();
97 return true;
98 }
99
100 /**
101 * Retrieve every record which needs to be processed
102 *
103 * @param string $table
104 * @return array
105 */
106 protected function getRecordsFromTable($table)
107 {
108 $fields = implode(',', array('uid', 'file_permissions'));
109 $records = $this->getDatabaseConnection()->exec_SELECTgetRows($fields, $table, $this->getWhereClause());
110 return $records;
111 }
112
113 /**
114 * Returns the where clause for database requests
115 *
116 * @return string
117 */
118 protected function getWhereClause()
119 {
120 return 'file_permissions LIKE \'%writeFile%\' AND file_permissions LIKE \'%replaceFile%\'';
121 }
122 }