‪TYPO3CMS  ‪main
FileMetadataPermissionsAspect.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
19 use TYPO3\CMS\Backend\Utility\BackendUtility;
26 
33 {
43  public function ‪checkRecordUpdateAccess($table, $id, $fileMetadataRecord, $otherHookGrantedAccess, ‪DataHandler $dataHandler)
44  {
45  $accessAllowed = $otherHookGrantedAccess;
46  if ($table === 'sys_file_metadata' && $accessAllowed !== 0) {
47  $existingFileMetadataRecord = BackendUtility::getRecord('sys_file_metadata', $id);
48  if ($existingFileMetadataRecord === null || (empty($existingFileMetadataRecord['file']) && !empty($fileMetadataRecord['file']))) {
49  $existingFileMetadataRecord = $fileMetadataRecord;
50  }
51  $accessAllowed = $this->‪checkFileWriteAccessForFileMetaData($existingFileMetadataRecord) ? 1 : 0;
52  }
53 
54  return $accessAllowed;
55  }
56 
67  public function ‪checkModifyAccessList(&$accessAllowed, $table, ‪DataHandler $parent)
68  {
69  if ($table === 'sys_file_metadata') {
70  foreach (($parent->cmdmap['sys_file_metadata'] ?? []) as $id => $command) {
71  $fileMetadataRecord = (array)BackendUtility::getRecord('sys_file_metadata', (int)$id);
72  $accessAllowed = $this->‪checkFileWriteAccessForFileMetaData($fileMetadataRecord);
73  if (!$accessAllowed) {
74  // If for any item in the array, access is not allowed, we deny the whole operation
75  break;
76  }
77  }
78 
79  if (isset($parent->datamap[$table])) {
80  foreach ($parent->datamap[$table] as $id => $data) {
81  $recordAccessAllowed = false;
82 
83  if (!str_contains((string)$id, 'NEW')) {
84  $fileMetadataRecord = BackendUtility::getRecord('sys_file_metadata', (int)$id);
85  if ($fileMetadataRecord !== null) {
86  if ($parent->isImporting && empty($fileMetadataRecord['file'])) {
87  // When importing the record was added with an empty file relation as first step
88  $recordAccessAllowed = true;
89  } else {
90  $recordAccessAllowed = $this->‪checkFileWriteAccessForFileMetaData($fileMetadataRecord);
91  }
92  }
93  } else {
94  // For new records record access is allowed
95  $recordAccessAllowed = true;
96  }
97 
98  if (isset($data['file'])) {
99  if ($parent->isImporting && empty($data['file'])) {
100  // When importing the record will be created with an empty file relation as first step
101  $dataAccessAllowed = true;
102  } elseif (empty($data['file'])) {
103  $dataAccessAllowed = false;
104  } else {
105  $dataAccessAllowed = $this->‪checkFileWriteAccessForFileMetaData($data);
106  }
107  } else {
108  $dataAccessAllowed = true;
109  }
110 
111  if (!$recordAccessAllowed || !$dataAccessAllowed) {
112  // If for any item in the array, access is not allowed, we deny the whole operation
113  $accessAllowed = false;
114  break;
115  }
116  }
117  }
118  }
119  }
120 
124  #[AsEventListener('evaluate-file-meta-data-edit-form-access')]
126  {
127  if (!$event->‪doesUserHaveAccess()
128  || $event->‪getTableName() !== 'sys_file_metadata'
129  || $event->‪getCommand() !== 'edit'
130  ) {
131  return;
132  }
133 
135  (array)BackendUtility::getRecord('sys_file_metadata', (int)($event->‪getDatabaseRow()['uid'] ?? 0))
136  ) ? $event->‪allowUserAccess() : $event->‪denyUserAccess();
137  }
138 
145  protected function ‪checkFileWriteAccessForFileMetaData($fileMetadataRecord)
146  {
147  $accessAllowed = false;
148  if (is_array($fileMetadataRecord) && !empty($fileMetadataRecord['file'])) {
149  $file = $fileMetadataRecord['file'];
150  // The file relation could be written as sys_file_[uid], strip this off before checking the rights
151  if (str_contains($file, 'sys_file_')) {
152  $file = substr($file, strlen('sys_file_'));
153  }
154  $fileObject = GeneralUtility::makeInstance(ResourceFactory::class)->getFileObject((int)$file);
155  $accessAllowed = $fileObject->checkActionPermission('editMeta');
156  }
157  return $accessAllowed;
158  }
159 }
‪TYPO3\CMS\Core\DataHandling\DataHandler
Definition: DataHandler.php:94
‪TYPO3\CMS\Backend\Form\Event\ModifyEditFormUserAccessEvent\denyUserAccess
‪denyUserAccess()
Definition: ModifyEditFormUserAccessEvent.php:53
‪TYPO3\CMS\Backend\Form\Event\ModifyEditFormUserAccessEvent\getCommand
‪getCommand()
Definition: ModifyEditFormUserAccessEvent.php:87
‪TYPO3\CMS\Core\Attribute\AsEventListener
Definition: AsEventListener.php:25
‪TYPO3\CMS\Core\Resource\Security
Definition: FileMetadataPermissionsAspect.php:16
‪TYPO3\CMS\Core\Resource\Security\FileMetadataPermissionsAspect\checkRecordUpdateAccess
‪int null checkRecordUpdateAccess($table, $id, $fileMetadataRecord, $otherHookGrantedAccess, DataHandler $dataHandler)
Definition: FileMetadataPermissionsAspect.php:43
‪TYPO3\CMS\Backend\Form\Event\ModifyEditFormUserAccessEvent\getDatabaseRow
‪getDatabaseRow()
Definition: ModifyEditFormUserAccessEvent.php:95
‪TYPO3\CMS\Backend\Form\Event\ModifyEditFormUserAccessEvent\getTableName
‪getTableName()
Definition: ModifyEditFormUserAccessEvent.php:78
‪TYPO3\CMS\Backend\Form\Event\ModifyEditFormUserAccessEvent\doesUserHaveAccess
‪doesUserHaveAccess()
Definition: ModifyEditFormUserAccessEvent.php:61
‪TYPO3\CMS\Backend\Form\Event\ModifyEditFormUserAccessEvent
Definition: ModifyEditFormUserAccessEvent.php:27
‪TYPO3\CMS\Core\Resource\ResourceFactory
Definition: ResourceFactory.php:42
‪TYPO3\CMS\Backend\Form\Event\ModifyEditFormUserAccessEvent\allowUserAccess
‪allowUserAccess()
Definition: ModifyEditFormUserAccessEvent.php:45
‪TYPO3\CMS\Core\Resource\Security\FileMetadataPermissionsAspect\isAllowedToShowEditForm
‪isAllowedToShowEditForm(ModifyEditFormUserAccessEvent $event)
Definition: FileMetadataPermissionsAspect.php:125
‪TYPO3\CMS\Core\Resource\Security\FileMetadataPermissionsAspect\checkModifyAccessList
‪checkModifyAccessList(&$accessAllowed, $table, DataHandler $parent)
Definition: FileMetadataPermissionsAspect.php:67
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:22
‪TYPO3\CMS\Core\Resource\Security\FileMetadataPermissionsAspect\checkFileWriteAccessForFileMetaData
‪bool checkFileWriteAccessForFileMetaData($fileMetadataRecord)
Definition: FileMetadataPermissionsAspect.php:145
‪TYPO3\CMS\Core\DataHandling\DataHandlerCheckModifyAccessListHookInterface
Definition: DataHandlerCheckModifyAccessListHookInterface.php:22
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Resource\Security\FileMetadataPermissionsAspect
Definition: FileMetadataPermissionsAspect.php:33