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