[!!!][FEATURE] Replace file feature for FAL file list
[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 $description = 'A new file permission was introduced regarding replacing files.' .
35 ' This update sets "Files:replace" for all BE users/groups with the permission "Files:write".';
36 $updateNeeded = FALSE;
37 $db = $this->getDatabaseConnection();
38
39 // Fetch user records where the writeFile is set and replaceFile is not
40 $notMigratedRowsCount = $db->exec_SELECTcountRows(
41 'uid',
42 'be_users',
43 $this->getWhereClause()
44 );
45 if ($notMigratedRowsCount > 0) {
46 $updateNeeded = TRUE;
47 }
48
49 if (!$updateNeeded) {
50 // Fetch group records where the writeFile is set and replaceFile is not
51 $notMigratedRowsCount = $db->exec_SELECTcountRows(
52 'uid',
53 'be_groups',
54 $this->getWhereClause()
55 );
56 if ($notMigratedRowsCount > 0) {
57 $updateNeeded = TRUE;
58 }
59 }
60 return $updateNeeded;
61 }
62
63 /**
64 * Performs the accordant updates.
65 *
66 * @param array &$dbQueries Queries done in this update
67 * @param mixed &$customMessages Custom messages
68 * @return bool Whether everything went smoothly or not
69 */
70 public function performUpdate(array &$dbQueries, &$customMessages) {
71 $db = $this->getDatabaseConnection();
72
73 // Iterate over users and groups table to perform permission updates
74 $tablesToProcess = ['be_groups', 'be_users'];
75 foreach ($tablesToProcess as $table) {
76 $records = $this->getRecordsFromTable($table);
77 foreach ($records as $singleRecord) {
78 $updateArray = [
79 'file_permissions' => $singleRecord['file_permissions'] . ',replaceFile'
80 ];
81 $db->exec_UPDATEquery($table, 'uid=' . (int)$singleRecord['uid'], $updateArray);
82 // Get last executed query
83 $dbQueries[] = str_replace(chr(10), ' ', $db->debug_lastBuiltQuery);
84 // Check for errors
85 if ($db->sql_error()) {
86 $customMessages = 'SQL-ERROR: ' . htmlspecialchars($db->sql_error());
87 return FALSE;
88 }
89 }
90 }
91 return TRUE;
92 }
93
94 /**
95 * Retrieve every record which needs to be processed
96 *
97 * @param string $table
98 * @return array
99 */
100 protected function getRecordsFromTable($table) {
101 $fields = implode(',', array('uid', 'file_permissions'));
102 $records = $this->getDatabaseConnection()->exec_SELECTgetRows($fields, $table, $this->getWhereClause());
103 return $records;
104 }
105
106 /**
107 * Returns the where clause for database requests
108 *
109 * @return string
110 */
111 protected function getWhereClause() {
112 return 'file_permissions LIKE "%writeFile%" AND file_permissions LIKE "%replaceFile%"';
113 }
114 }