‪TYPO3CMS  10.4
PermissionController.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 
29 
35 {
39  const ‪SESSION_PREFIX = 'tx_Beuser_';
40 
44  protected ‪$id;
45 
49  protected ‪$returnUrl = '';
50 
54  protected ‪$depth;
55 
61  protected ‪$getLevels = 10;
62 
66  protected ‪$pageInfo = [];
67 
73  protected ‪$defaultViewObjectName = BackendTemplateView::class;
74 
80  protected ‪$view;
81 
85  protected function ‪initializeAction()
86  {
87  // determine depth parameter
88  $this->depth = (int)GeneralUtility::_GP('depth') > 0
89  ? (int)GeneralUtility::_GP('depth')
90  : (int)$this->‪getBackendUser()->‪getSessionData(self::SESSION_PREFIX . 'depth');
91  if ($this->request->hasArgument('depth')) {
92  $this->depth = (int)$this->request->getArgument('depth');
93  }
94  $this->‪getBackendUser()->‪setAndSaveSessionData(self::SESSION_PREFIX . 'depth', $this->depth);
95 
96  // determine id parameter
97  $this->id = (int)GeneralUtility::_GP('id');
98  if ($this->request->hasArgument('id')) {
99  $this->id = (int)$this->request->getArgument('id');
100  }
101 
102  if (!‪BackendUtility::getRecord('pages', $this->id)) {
103  $this->id = 0;
104  }
105 
106  $this->returnUrl = GeneralUtility::_GP('returnUrl');
107  if ($this->request->hasArgument('returnUrl')) {
108  $this->returnUrl = $this->request->getArgument('returnUrl');
109  }
110 
111  $this->‪setPageInfo();
112  }
113 
119  protected function ‪initializeView(‪ViewInterface ‪$view)
120  {
121  parent::initializeView(‪$view);
122  $this->‪setPageInfo();
123 
124  // the view of the update action has a different view class
125  if ($view instanceof ‪BackendTemplateView) {
126  ‪$view->‪getModuleTemplate()->‪getPageRenderer()->‪loadRequireJsModule('TYPO3/CMS/Beuser/Permissions');
127  ‪$view->‪getModuleTemplate()->‪getPageRenderer()->‪loadRequireJsModule('TYPO3/CMS/Backend/Tooltip');
128 
130  $this->view->getModuleTemplate()->getDocHeaderComponent()->setMetaInformation($this->pageInfo);
131  $this->view->getModuleTemplate()->setFlashMessageQueue($this->controllerContext->getFlashMessageQueue());
132  }
133  }
134 
140  protected function ‪registerDocHeaderButtons()
141  {
143  $buttonBar = $this->view->getModuleTemplate()->getDocHeaderComponent()->getButtonBar();
144  $currentRequest = ‪$this->request;
145  $moduleName = $currentRequest->‪getPluginName();
146  $getVars = $this->request->getArguments();
147  $lang = $this->‪getLanguageService();
148 
149  $extensionName = $currentRequest->getControllerExtensionName();
150  if (empty($getVars)) {
151  $modulePrefix = strtolower('tx_' . $extensionName . '_' . $moduleName);
152  $getVars = ['id', 'route', $modulePrefix];
153  }
154 
155  if ($currentRequest->getControllerActionName() === 'edit') {
156  // CLOSE button:
157  if (!empty($this->returnUrl)) {
158  $closeButton = $buttonBar->makeLinkButton()
159  ->setHref($this->returnUrl)
160  ->setTitle($lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:rm.closeDoc'))
161  ->setIcon($this->view->getModuleTemplate()->getIconFactory()->getIcon(
162  'actions-close',
164  ));
165  $buttonBar->addButton($closeButton);
166  }
167 
168  // SAVE button:
169  $saveButton = $buttonBar->makeInputButton()
170  ->setTitle($lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:rm.saveCloseDoc'))
171  ->setName('tx_beuser_system_beusertxpermission[submit]')
172  ->setValue('Save')
173  ->setForm('PermissionControllerEdit')
174  ->setIcon($this->view->getModuleTemplate()->getIconFactory()->getIcon(
175  'actions-document-save',
177  ))
178  ->setShowLabelText(true);
179 
180  $buttonBar->addButton($saveButton);
181  }
182 
183  // SHORTCUT bottom:
184  $shortcutButton = $buttonBar->makeShortcutButton()
185  ->setModuleName($moduleName)
186  ->setGetVariables($getVars);
187  $buttonBar->addButton($shortcutButton);
188  }
189 
193  public function ‪indexAction()
194  {
195  if (!$this->id) {
196  $this->pageInfo = ['title' => ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'], 'uid' => 0, 'pid' => 0];
197  }
198 
199  if ($this->‪getBackendUser()->workspace != 0) {
200  // Adding section with the permission setting matrix:
201  $this->‪addFlashMessage(
202  ‪LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf:WorkspaceWarningText', 'beuser') ?? '',
203  ‪LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf:WorkspaceWarning', 'beuser') ?? '',
205  );
206  }
207 
208  // depth options
209  $depthOptions = [];
210  $url = $this->uriBuilder->reset()->setArguments([
211  'action' => 'index',
212  'depth' => '${value}',
213  'id' => $this->id
214  ])->buildBackendUri();
215  foreach ([1, 2, 3, 4, 10] as $depthLevel) {
216  $levelLabel = $depthLevel === 1 ? 'level' : 'levels';
217  $depthOptions[$depthLevel] = $depthLevel . ' ' . ‪LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf:' . $levelLabel, 'beuser');
218  }
219  $this->view->assign('currentId', $this->id);
220  $this->view->assign('depthBaseUrl', $url);
221  $this->view->assign('depth', $this->depth);
222  $this->view->assign('depthOptions', $depthOptions);
223 
224  $beUserArray = ‪BackendUtility::getUserNames();
225  $this->view->assign('beUsers', $beUserArray);
226  $beGroupArray = ‪BackendUtility::getGroupNames();
227  $this->view->assign('beGroups', $beGroupArray);
228 
230  $tree = GeneralUtility::makeInstance(PageTreeView::class);
231  $tree->init();
232  $tree->addField('perms_user', true);
233  $tree->addField('perms_group', true);
234  $tree->addField('perms_everybody', true);
235  $tree->addField('perms_userid', true);
236  $tree->addField('perms_groupid', true);
237  $tree->addField('hidden');
238  $tree->addField('fe_group');
239  $tree->addField('starttime');
240  $tree->addField('endtime');
241  $tree->addField('editlock');
242 
243  // Create the tree from $this->id
244  if ($this->id) {
245  $tree->tree[] = ['row' => ‪$this->pageInfo, 'HTML' => $tree->getIcon($this->id)];
246  } else {
247  $tree->tree[] = ['row' => ‪$this->pageInfo, 'HTML' => $tree->getRootIcon($this->pageInfo)];
248  }
249  $tree->getTree($this->id, $this->depth);
250  $this->view->assign('viewTree', $tree->tree);
251 
252  // CSH for permissions setting
253  $this->view->assign('cshItem', ‪BackendUtility::cshItem('xMOD_csh_corebe', 'perm_module', '', '<span class="btn btn-default btn-sm">|</span>'));
254  }
255 
259  public function ‪editAction()
260  {
261  $this->view->assign('id', $this->id);
262  $this->view->assign('depth', $this->depth);
263 
264  if (!$this->id) {
265  $this->pageInfo = ['title' => ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'], 'uid' => 0, 'pid' => 0];
266  }
267  if ($this->‪getBackendUser()->workspace != 0) {
268  // Adding FlashMessage with the permission setting matrix:
269  $this->‪addFlashMessage(
270  ‪LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf:WorkspaceWarningText', 'beuser') ?? '',
271  ‪LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf:WorkspaceWarning', 'beuser') ?? '',
273  );
274  }
275  // Get user names and group names
276  $beGroupArray = ‪BackendUtility::getGroupNames();
277  $beUserArray = ‪BackendUtility::getUserNames();
278 
279  // Owner selector
280  $beUserDataArray = [0 => ‪LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf:selectNone', 'beuser')];
281  foreach ($beUserArray as $uid => &$row) {
282  $beUserDataArray[$uid] = $row['username'];
283  }
284  $beUserDataArray[-1] = ‪LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf:selectUnchanged', 'beuser');
285  $this->view->assign('currentBeUser', $this->pageInfo['perms_userid']);
286  $this->view->assign('beUserData', $beUserDataArray);
287 
288  // Group selector
289  $beGroupDataArray = [0 => ‪LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf:selectNone', 'beuser')];
290  foreach ($beGroupArray as $uid => $row) {
291  $beGroupDataArray[$uid] = $row['title'];
292  }
293  $beGroupDataArray[-1] = ‪LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf:selectUnchanged', 'beuser');
294  $this->view->assign('currentBeGroup', $this->pageInfo['perms_groupid']);
295  $this->view->assign('beGroupData', $beGroupDataArray);
296  $this->view->assign('pageInfo', $this->pageInfo);
297  $this->view->assign('returnUrl', $this->returnUrl);
298  $this->view->assign('recursiveSelectOptions', $this->‪getRecursiveSelectOptions());
299  }
300 
307  protected function ‪updateAction(array $data, array $mirror)
308  {
309  $dataHandlerInput = [];
310  // Prepare the input data for data handler
311  if (!empty($data['pages'])) {
312  foreach ($data['pages'] as $pageUid => $properties) {
313  // if the owner and group field shouldn't be touched, unset the option
314  if ((int)$properties['perms_userid'] === -1) {
315  unset($properties['perms_userid']);
316  }
317  if ((int)$properties['perms_groupid'] === -1) {
318  unset($properties['perms_groupid']);
319  }
320  $dataHandlerInput[$pageUid] = $properties;
321  if (!empty($mirror['pages'][$pageUid])) {
322  $mirrorPages = ‪GeneralUtility::intExplode(',', $mirror['pages'][$pageUid]);
323  foreach ($mirrorPages as $mirrorPageUid) {
324  $dataHandlerInput[$mirrorPageUid] = $properties;
325  }
326  }
327  }
328  }
329 
330  $dataHandler = GeneralUtility::makeInstance(DataHandler::class);
331  $dataHandler->start(
332  [
333  'pages' => $dataHandlerInput
334  ],
335  []
336  );
337  $dataHandler->process_datamap();
338 
339  $this->‪redirectToUri($this->returnUrl);
340  }
341 
345  protected function ‪getBackendUser()
346  {
347  return ‪$GLOBALS['BE_USER'];
348  }
349 
355  protected function ‪getRecursiveSelectOptions()
356  {
357  // Initialize tree object:
358  $tree = GeneralUtility::makeInstance(PageTreeView::class);
359  $tree->init();
360  $tree->addField('perms_userid', true);
361  $tree->makeHTML = 0;
362  $tree->setRecs = 1;
363  // Make tree:
364  $tree->getTree($this->id, $this->getLevels, '');
365  $options = [];
366  $options[''] = '';
367  // If there are a hierarchy of page ids, then...
368  if ($this->‪getBackendUser()->user['uid'] && !empty($tree->orig_ids_hierarchy)) {
369  // Init:
370  $labelRecursive = ‪LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf:recursive', 'beuser');
371  $labelLevel = ‪LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf:level', 'beuser');
372  $labelLevels = ‪LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf:levels', 'beuser');
373  $labelPageAffected = ‪LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf:page_affected', 'beuser');
374  $labelPagesAffected = ‪LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf:pages_affected', 'beuser');
375  $theIdListArr = [];
376  // Traverse the number of levels we want to allow recursive
377  // setting of permissions for:
378  for ($a = $this->getLevels; $a > 0; $a--) {
379  if (is_array($tree->orig_ids_hierarchy[$a])) {
380  foreach ($tree->orig_ids_hierarchy[$a] as $theId) {
381  $theIdListArr[] = $theId;
382  }
383  $lKey = $this->getLevels - $a + 1;
384  $pagesCount = count($theIdListArr);
385  $options[implode(',', $theIdListArr)] = $labelRecursive . ' ' . $lKey . ' ' . ($lKey === 1 ? $labelLevel : $labelLevels) .
386  ' (' . $pagesCount . ' ' . ($pagesCount === 1 ? $labelPageAffected : $labelPagesAffected) . ')';
387  }
388  }
389  }
390  return $options;
391  }
392 
396  protected function ‪setPageInfo(): void
397  {
398  $this->pageInfo = ‪BackendUtility::readPageAccess(‪BackendUtility::getRecord('pages', $this->id) ? $this->id : 0, ' 1=1');
399  }
400 
406  protected function ‪getLanguageService()
407  {
408  return ‪$GLOBALS['LANG'];
409  }
410 }
‪TYPO3\CMS\Core\DataHandling\DataHandler
Definition: DataHandler.php:84
‪TYPO3\CMS\Core\Imaging\Icon\SIZE_SMALL
‪const SIZE_SMALL
Definition: Icon.php:30
‪TYPO3\CMS\Beuser\Controller\PermissionController
Definition: PermissionController.php:35
‪TYPO3\CMS\Beuser\Controller\PermissionController\editAction
‪editAction()
Definition: PermissionController.php:252
‪TYPO3\CMS\Beuser\Controller\PermissionController\$id
‪int $id
Definition: PermissionController.php:43
‪TYPO3\CMS\Backend\Template\Components\ButtonBar
Definition: ButtonBar.php:32
‪TYPO3\CMS\Extbase\Utility\LocalizationUtility
Definition: LocalizationUtility.php:33
‪TYPO3\CMS\Beuser\Controller\PermissionController\$getLevels
‪int $getLevels
Definition: PermissionController.php:57
‪TYPO3\CMS\Core\Imaging\Icon
Definition: Icon.php:26
‪TYPO3\CMS\Beuser\Controller\PermissionController\$pageInfo
‪array $pageInfo
Definition: PermissionController.php:61
‪TYPO3\CMS\Beuser\Controller\PermissionController\getBackendUser
‪TYPO3 CMS Core Authentication BackendUserAuthentication getBackendUser()
Definition: PermissionController.php:338
‪TYPO3\CMS\Extbase\Mvc\Request\getPluginName
‪string getPluginName()
Definition: Request.php:181
‪TYPO3\CMS\Beuser\Controller\PermissionController\setPageInfo
‪setPageInfo()
Definition: PermissionController.php:389
‪TYPO3\CMS\Backend\Template\ModuleTemplate\getPageRenderer
‪PageRenderer getPageRenderer()
Definition: ModuleTemplate.php:378
‪TYPO3\CMS\Beuser\Controller
Definition: BackendUserController.php:16
‪TYPO3\CMS\Backend\Utility\BackendUtility\cshItem
‪static string cshItem($table, $field, $_='', $wrap='')
Definition: BackendUtility.php:2306
‪TYPO3\CMS\Backend\Utility\BackendUtility\getUserNames
‪static array getUserNames($fields='username, usergroup, usergroup_cached_list, uid', $where='')
Definition: BackendUtility.php:818
‪TYPO3\CMS\Beuser\Controller\PermissionController\getLanguageService
‪TYPO3 CMS Core Localization LanguageService getLanguageService()
Definition: PermissionController.php:399
‪TYPO3\CMS\Extbase\Mvc\Controller\ActionController\addFlashMessage
‪addFlashMessage($messageBody, $messageTitle='', $severity=AbstractMessage::OK, $storeInSession=true)
Definition: ActionController.php:747
‪TYPO3\CMS\Backend\Tree\View\PageTreeView
Definition: PageTreeView.php:24
‪TYPO3\CMS\Core\Page\PageRenderer\loadRequireJsModule
‪loadRequireJsModule($mainModuleName, $callBackFunction=null)
Definition: PageRenderer.php:1493
‪TYPO3\CMS\Beuser\Controller\PermissionController\$returnUrl
‪string $returnUrl
Definition: PermissionController.php:47
‪TYPO3\CMS\Core\Messaging\AbstractMessage\WARNING
‪const WARNING
Definition: AbstractMessage.php:30
‪TYPO3\CMS\Beuser\Controller\PermissionController\registerDocHeaderButtons
‪registerDocHeaderButtons()
Definition: PermissionController.php:133
‪TYPO3\CMS\Beuser\Controller\PermissionController\indexAction
‪indexAction()
Definition: PermissionController.php:186
‪TYPO3\CMS\Beuser\Controller\PermissionController\SESSION_PREFIX
‪const SESSION_PREFIX
Definition: PermissionController.php:39
‪TYPO3\CMS\Beuser\Controller\PermissionController\$defaultViewObjectName
‪string $defaultViewObjectName
Definition: PermissionController.php:67
‪TYPO3\CMS\Beuser\Controller\PermissionController\$view
‪BackendTemplateView $view
Definition: PermissionController.php:73
‪TYPO3\CMS\Backend\Utility\BackendUtility\getGroupNames
‪static array getGroupNames($fields='title, uid', $where='')
Definition: BackendUtility.php:836
‪TYPO3\CMS\Extbase\Utility\LocalizationUtility\translate
‪static string null translate(string $key, ?string $extensionName=null, array $arguments=null, string $languageKey=null, array $alternativeLanguageKeys=null)
Definition: LocalizationUtility.php:67
‪TYPO3\CMS\Core\Authentication\AbstractUserAuthentication\getSessionData
‪mixed getSessionData($key)
Definition: AbstractUserAuthentication.php:1206
‪TYPO3\CMS\Core\Authentication\AbstractUserAuthentication\setAndSaveSessionData
‪setAndSaveSessionData($key, $data)
Definition: AbstractUserAuthentication.php:1233
‪TYPO3\CMS\Extbase\Mvc\View\ViewInterface
Definition: ViewInterface.php:24
‪TYPO3\CMS\Beuser\Controller\PermissionController\initializeView
‪initializeView(ViewInterface $view)
Definition: PermissionController.php:112
‪TYPO3\CMS\Backend\Utility\BackendUtility
Definition: BackendUtility.php:75
‪TYPO3\CMS\Backend\Utility\BackendUtility\getRecord
‪static array null getRecord($table, $uid, $fields=' *', $where='', $useDeleteClause=true)
Definition: BackendUtility.php:95
‪TYPO3\CMS\Backend\Utility\BackendUtility\readPageAccess
‪static array false readPageAccess($id, $perms_clause)
Definition: BackendUtility.php:597
‪TYPO3\CMS\Beuser\Controller\PermissionController\initializeAction
‪initializeAction()
Definition: PermissionController.php:78
‪TYPO3\CMS\Backend\View\BackendTemplateView
Definition: BackendTemplateView.php:28
‪TYPO3\CMS\Core\Messaging\FlashMessage
Definition: FlashMessage.php:24
‪TYPO3\CMS\Beuser\Controller\PermissionController\getRecursiveSelectOptions
‪array getRecursiveSelectOptions()
Definition: PermissionController.php:348
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Extbase\Mvc\Controller\ActionController\$request
‪TYPO3 CMS Extbase Mvc Request $request
Definition: ActionController.php:109
‪TYPO3\CMS\Beuser\Controller\PermissionController\$depth
‪int $depth
Definition: PermissionController.php:51
‪TYPO3\CMS\Extbase\Mvc\Controller\ActionController
Definition: ActionController.php:55
‪TYPO3\CMS\Beuser\Controller\PermissionController\updateAction
‪updateAction(array $data, array $mirror)
Definition: PermissionController.php:300
‪TYPO3\CMS\Core\Utility\GeneralUtility\intExplode
‪static int[] intExplode($delimiter, $string, $removeEmptyValues=false, $limit=0)
Definition: GeneralUtility.php:988
‪TYPO3\CMS\Backend\View\BackendTemplateView\getModuleTemplate
‪ModuleTemplate getModuleTemplate()
Definition: BackendTemplateView.php:56
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Extbase\Mvc\Controller\ActionController\redirectToUri
‪redirectToUri($uri, $delay=0, $statusCode=303)
Definition: ActionController.php:878