‪TYPO3CMS  11.5
NoteController.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use TYPO3\CMS\Backend\Utility\BackendUtility;
26 
33 {
37  protected ‪$notesRepository;
38 
39  protected array ‪$pagePermissionCache = [];
40 
41  public function ‪__construct()
42  {
43  $this->notesRepository = GeneralUtility::makeInstance(SysNoteRepository::class);
44  }
45 
53  public function ‪listAction($pids, int $position = null): string
54  {
55  $backendUser = $this->‪getBackendUser();
56  if (empty($pids)
57  || empty($backendUser->user[$backendUser->userid_column])
58  || !$backendUser->check('tables_select', 'sys_note')
59  ) {
60  return '';
61  }
62 
63  $notes = $this->notesRepository->findByPidsAndAuthorId($pids, (int)$backendUser->user[$backendUser->userid_column], $position);
64  if (!$notes) {
65  return '';
66  }
67  $view = GeneralUtility::makeInstance(StandaloneView::class);
68  $view->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName(
69  'EXT:sys_note/Resources/Private/Templates/Note/List.html'
70  ));
71  $view->setLayoutRootPaths(['EXT:sys_note/Resources/Private/Layouts']);
72  $view->getRequest()->setControllerExtensionName('SysNote');
73  $view->assign('notes', $this->‪enrichWithEditPermissions($notes));
74  return $view->render();
75  }
76 
77  protected function ‪enrichWithEditPermissions(array $notes): array
78  {
79  $backendUser = $this->‪getBackendUser();
80  $hasEditAccess = $backendUser->isAdmin() || $backendUser->check('tables_modify', 'sys_note');
81 
82  foreach ($notes as &$note) {
83  if (!$hasEditAccess) {
84  // If no edit access, disable edit and delete options for all notes
85  $note['canBeEdited'] = false;
86  $note['canBeDeleted'] = false;
87  continue;
88  }
89  // Check content edit permissions for the note
90  $pid = (int)($note['pid'] ?? 0);
91  if (!isset($this->pagePermissionCache[$pid])) {
92  // Calculate and cache the content edit permissions for this $pid
93  $permissionClause = $backendUser->getPagePermsClause(‪Permission::PAGE_SHOW);
94  $pageRow = BackendUtility::readPageAccess($pid, $permissionClause) ?: [];
95  $this->pagePermissionCache[$pid] = $backendUser->doesUserHaveAccess($pageRow, ‪Permission::CONTENT_EDIT);
96  }
97  $note['canBeEdited'] = $this->pagePermissionCache[$pid];
98  // For delete, also take user TSconfig into account
99  $note['canBeDeleted'] = $this->pagePermissionCache[$pid]
100  && !(bool)trim($backendUser->getTSConfig()['options.']['disableDelete.']['sys_note'] ?? $backendUser->getTSConfig()['options.']['disableDelete'] ?? '');
101  }
102 
103  return $notes;
104  }
105 
106  protected function ‪getBackendUser(): ‪BackendUserAuthentication
107  {
108  return ‪$GLOBALS['BE_USER'];
109  }
110 }
‪TYPO3\CMS\SysNote\Controller\NoteController
Definition: NoteController.php:33
‪TYPO3\CMS\SysNote\Controller\NoteController\listAction
‪string listAction($pids, int $position=null)
Definition: NoteController.php:52
‪TYPO3\CMS\SysNote\Controller\NoteController\$pagePermissionCache
‪array $pagePermissionCache
Definition: NoteController.php:38
‪TYPO3\CMS\SysNote\Controller\NoteController\$notesRepository
‪SysNoteRepository $notesRepository
Definition: NoteController.php:36
‪TYPO3\CMS\Core\Type\Bitmask\Permission
Definition: Permission.php:26
‪TYPO3\CMS\SysNote\Domain\Repository\SysNoteRepository
Definition: SysNoteRepository.php:30
‪TYPO3\CMS\SysNote\Controller\NoteController\__construct
‪__construct()
Definition: NoteController.php:40
‪TYPO3\CMS\SysNote\Controller\NoteController\enrichWithEditPermissions
‪enrichWithEditPermissions(array $notes)
Definition: NoteController.php:76
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:62
‪TYPO3\CMS\Core\Type\Bitmask\Permission\PAGE_SHOW
‪const PAGE_SHOW
Definition: Permission.php:35
‪TYPO3\CMS\SysNote\Controller
Definition: NoteController.php:18
‪TYPO3\CMS\Fluid\View\StandaloneView
Definition: StandaloneView.php:31
‪TYPO3\CMS\Core\Type\Bitmask\Permission\CONTENT_EDIT
‪const CONTENT_EDIT
Definition: Permission.php:55
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\SysNote\Controller\NoteController\getBackendUser
‪getBackendUser()
Definition: NoteController.php:105
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50