TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
PermissionController.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Beuser\Controller;
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 
33 {
37  const SESSION_PREFIX = 'tx_Beuser_';
38 
42  protected $id;
43 
47  protected $returnId;
48 
52  protected $depth;
53 
57  protected $lastEdited;
58 
64  protected $getLevels = 10;
65 
69  protected $pageInfo = [];
70 
76  protected $defaultViewObjectName = BackendTemplateView::class;
77 
83  protected $view;
84 
90  protected function initializeAction()
91  {
92  // determine id parameter
93  $this->id = (int)GeneralUtility::_GP('id');
94  if ($this->request->hasArgument('id')) {
95  $this->id = (int)$this->request->getArgument('id');
96  }
97 
98  // determine depth parameter
99  $this->depth = ((int)GeneralUtility::_GP('depth') > 0)
100  ? (int) GeneralUtility::_GP('depth')
101  : $this->getBackendUser()->getSessionData(self::SESSION_PREFIX . 'depth');
102  if ($this->request->hasArgument('depth')) {
103  $this->depth = (int)$this->request->getArgument('depth');
104  }
105  $this->getBackendUser()->setAndSaveSessionData(self::SESSION_PREFIX . 'depth', $this->depth);
106  $this->lastEdited = GeneralUtility::_GP('lastEdited');
107  $this->returnId = GeneralUtility::_GP('returnId');
108  $this->pageInfo = BackendUtility::readPageAccess($this->id, ' 1=1');
109  }
110 
117  protected function initializeView(ViewInterface $view)
118  {
119  parent::initializeView($view);
120  $view->assign(
121  'previewUrl',
123  (int)$this->pageInfo['uid'],
124  '',
125  BackendUtility::BEgetRootLine((int)$this->pageInfo['uid'])
126  )
127  );
128 
129  // the view of the update action has a different view class
130  if ($view instanceof BackendTemplateView) {
131  $view->getModuleTemplate()->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Beuser/Permissions');
132  $view->getModuleTemplate()->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Backend/Tooltip');
133  $view->getModuleTemplate()->addJavaScriptCode(
134  'jumpToUrl',
135  '
136  function jumpToUrl(URL) {
137  window.location.href = URL;
138  return false;
139  }
140  '
141  );
142  $this->registerDocHeaderButtons();
143  $this->view->getModuleTemplate()->setFlashMessageQueue($this->controllerContext->getFlashMessageQueue());
144  }
145  }
146 
153  protected function registerDocHeaderButtons()
154  {
156  $buttonBar = $this->view->getModuleTemplate()->getDocHeaderComponent()->getButtonBar();
157  $currentRequest = $this->request;
158  $moduleName = $currentRequest->getPluginName();
159  $getVars = $this->request->getArguments();
160  $lang = $this->getLanguageService();
161 
162  $extensionName = $currentRequest->getControllerExtensionName();
163  if (empty($getVars)) {
164  $modulePrefix = strtolower('tx_' . $extensionName . '_' . $moduleName);
165  $getVars = ['id', 'M', $modulePrefix];
166  }
167 
168  if ($currentRequest->getControllerActionName() === 'edit') {
169  // CLOSE button:
170  $closeUrl = $this->uriBuilder->reset()->setArguments([
171  'action' => 'index',
172  'id' => $this->id
173  ])->buildBackendUri();
174  $closeButton = $buttonBar->makeLinkButton()
175  ->setHref($closeUrl)
176  ->setTitle($lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:rm.closeDoc'))
177  ->setIcon($this->view->getModuleTemplate()->getIconFactory()->getIcon(
178  'actions-document-close',
180  ));
181  $buttonBar->addButton($closeButton);
182 
183  // SAVE button:
184  $saveButton = $buttonBar->makeInputButton()
185  ->setTitle($lang->sL('LLL:EXT:lang/Resources/Private/Language/locallang_core.xlf:rm.saveCloseDoc'))
186  ->setName('tx_beuser_system_beusertxpermission[submit]')
187  ->setValue('Save')
188  ->setForm('PermissionControllerEdit')
189  ->setIcon($this->view->getModuleTemplate()->getIconFactory()->getIcon(
190  'actions-document-save',
192  ))
193  ->setShowLabelText(true);
194 
195  $buttonBar->addButton($saveButton);
196  }
197 
198  // SHORTCUT botton:
199  $shortcutButton = $buttonBar->makeShortcutButton()
200  ->setModuleName($moduleName)
201  ->setGetVariables($getVars);
202  $buttonBar->addButton($shortcutButton);
203  }
204 
210  public function indexAction()
211  {
212  if (!$this->id) {
213  $this->pageInfo = ['title' => '[root-level]', 'uid' => 0, 'pid' => 0];
214  }
215 
216  if ($this->getBackendUser()->workspace != 0) {
217  // Adding section with the permission setting matrix:
218  $this->addFlashMessage(
219  LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf:WorkspaceWarningText', 'beuser'),
220  LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf:WorkspaceWarning', 'beuser'),
222  );
223  }
224 
225  // depth options
226  $depthOptions = [];
227  $url = $this->uriBuilder->reset()->setArguments([
228  'action' => 'index',
229  'depth' => '__DEPTH__',
230  'id' => $this->id
231  ])->buildBackendUri();
232  foreach ([1, 2, 3, 4, 10] as $depthLevel) {
233  $levelLabel = $depthLevel === 1 ? 'level' : 'levels';
234  $depthOptions[$depthLevel] = $depthLevel . ' ' . LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf:' . $levelLabel, 'beuser');
235  }
236  $this->view->assign('depthBaseUrl', $url);
237  $this->view->assign('depth', $this->depth);
238  $this->view->assign('depthOptions', $depthOptions);
239 
240  $beUserArray = BackendUtility::getUserNames();
241  $this->view->assign('beUsers', $beUserArray);
242  $beGroupArray = BackendUtility::getGroupNames();
243  $this->view->assign('beGroups', $beGroupArray);
244 
246  $tree = GeneralUtility::makeInstance(PageTreeView::class);
247  $tree->init();
248  $tree->addField('perms_user', true);
249  $tree->addField('perms_group', true);
250  $tree->addField('perms_everybody', true);
251  $tree->addField('perms_userid', true);
252  $tree->addField('perms_groupid', true);
253  $tree->addField('hidden');
254  $tree->addField('fe_group');
255  $tree->addField('starttime');
256  $tree->addField('endtime');
257  $tree->addField('editlock');
258 
259  // Create the tree from $this->id
260  if ($this->id) {
261  $tree->tree[] = ['row' => $this->pageInfo, 'HTML' => $tree->getIcon($this->id)];
262  } else {
263  $tree->tree[] = ['row' => $this->pageInfo, 'HTML' => $tree->getRootIcon($this->pageInfo)];
264  }
265  $tree->getTree($this->id, $this->depth);
266  $this->view->assign('viewTree', $tree->tree);
267 
268  // CSH for permissions setting
269  $this->view->assign('cshItem', BackendUtility::cshItem('xMOD_csh_corebe', 'perm_module', null, '<span class="btn btn-default btn-sm">|</span>'));
270  }
271 
277  public function editAction()
278  {
279  $this->view->assign('id', $this->id);
280  $this->view->assign('depth', $this->depth);
281 
282  if (!$this->id) {
283  $this->pageInfo = ['title' => '[root-level]', 'uid' => 0, 'pid' => 0];
284  }
285  if ($this->getBackendUser()->workspace != 0) {
286  // Adding FlashMessage with the permission setting matrix:
287  $this->addFlashMessage(
288  LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf:WorkspaceWarningText', 'beuser'),
289  LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf:WorkspaceWarning', 'beuser'),
291  );
292  }
293  // Get usernames and groupnames
294  $beGroupArray = BackendUtility::getListGroupNames('title,uid');
295  $beUserArray = BackendUtility::getUserNames();
296 
297  // Owner selector
298  $beUserDataArray = [0 => LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf:selectNone', 'beuser')];
299  foreach ($beUserArray as $uid => &$row) {
300  $beUserDataArray[$uid] = $row['username'];
301  }
302  $beUserDataArray[-1] = LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf:selectUnchanged', 'beuser');
303  $this->view->assign('currentBeUser', $this->pageInfo['perms_userid']);
304  $this->view->assign('beUserData', $beUserDataArray);
305 
306  // Group selector
307  $beGroupDataArray = [0 => LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf:selectNone', 'beuser')];
308  foreach ($beGroupArray as $uid => $row) {
309  $beGroupDataArray[$uid] = $row['title'];
310  }
311  $beGroupDataArray[-1] = LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf:selectUnchanged', 'beuser');
312  $this->view->assign('currentBeGroup', $this->pageInfo['perms_groupid']);
313  $this->view->assign('beGroupData', $beGroupDataArray);
314  $this->view->assign('pageInfo', $this->pageInfo);
315  $this->view->assign('returnId', $this->returnId);
316  $this->view->assign('recursiveSelectOptions', $this->getRecursiveSelectOptions());
317  }
318 
326  protected function updateAction(array $data, array $mirror)
327  {
328  if (!empty($data['pages'])) {
329  foreach ($data['pages'] as $pageUid => $properties) {
330  // if the owner and group field shouldn't be touched, unset the option
331  if ((int)$properties['perms_userid'] === -1) {
332  unset($properties['perms_userid']);
333  }
334  if ((int)$properties['perms_groupid'] === -1) {
335  unset($properties['perms_groupid']);
336  }
337  $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('pages');
338  $connection->update(
339  'pages',
340  $properties,
341  ['uid' => (int)$pageUid]
342  );
343 
344  if (!empty($mirror['pages'][$pageUid])) {
345  $mirrorPages = GeneralUtility::trimExplode(',', $mirror['pages'][$pageUid]);
346  foreach ($mirrorPages as $mirrorPageUid) {
347  $connection->update(
348  'pages',
349  $properties,
350  ['uid' => (int)$mirrorPageUid]
351  );
352  }
353  }
354  }
355  }
356  $this->redirect('index', null, null, ['id' => $this->returnId, 'depth' => $this->depth]);
357  }
358 
362  protected function getBackendUser()
363  {
364  return $GLOBALS['BE_USER'];
365  }
366 
372  protected function getRecursiveSelectOptions()
373  {
374  // Initialize tree object:
375  $tree = GeneralUtility::makeInstance(PageTreeView::class);
376  $tree->init();
377  $tree->addField('perms_userid', true);
378  $tree->makeHTML = 0;
379  $tree->setRecs = 1;
380  // Make tree:
381  $tree->getTree($this->id, $this->getLevels, '');
382  $options = [];
383  $options[''] = '';
384  // If there are a hierarchy of page ids, then...
385  if ($this->getBackendUser()->user['uid'] && !empty($tree->orig_ids_hierarchy)) {
386  // Init:
387  $labelRecursive = LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf:recursive', 'beuser');
388  $labelLevel = LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf:level', 'beuser');
389  $labelLevels = LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf:levels', 'beuser');
390  $labelPageAffected = LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf:page_affected', 'beuser');
391  $labelPagesAffected = LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf:pages_affected', 'beuser');
392  $theIdListArr = [];
393  // Traverse the number of levels we want to allow recursive
394  // setting of permissions for:
395  for ($a = $this->getLevels; $a > 0; $a--) {
396  if (is_array($tree->orig_ids_hierarchy[$a])) {
397  foreach ($tree->orig_ids_hierarchy[$a] as $theId) {
398  $theIdListArr[] = $theId;
399  }
400  $lKey = $this->getLevels - $a + 1;
401  $pagesCount = count($theIdListArr);
402  $options[implode(',', $theIdListArr)] = $labelRecursive . ' ' . $lKey . ' ' . ($lKey === 1 ? $labelLevel : $labelLevels) .
403  ' (' . $pagesCount . ' ' . ($pagesCount === 1 ? $labelPageAffected : $labelPagesAffected) . ')';
404  }
405  }
406  }
407  return $options;
408  }
409 
415  protected function getLanguageService()
416  {
417  return $GLOBALS['LANG'];
418  }
419 }
static trimExplode($delim, $string, $removeEmptyValues=false, $limit=0)
redirect($actionName, $controllerName=null, $extensionName=null, array $arguments=null, $pageUid=null, $delay=0, $statusCode=303)
static viewOnClick($pageUid, $backPath= '', $rootLine=null, $anchorSection= '', $alternativeUrl= '', $additionalGetVars= '', $switchFocus=true)
static BEgetRootLine($uid, $clause= '', $workspaceOL=false)
static translate($key, $extensionName, $arguments=null)
if(TYPO3_MODE=== 'BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']
static makeInstance($className,...$constructorArguments)
static getUserNames($fields= 'username, usergroup, usergroup_cached_list, uid', $where= '')
addFlashMessage($messageBody, $messageTitle= '', $severity=\TYPO3\CMS\Core\Messaging\AbstractMessage::OK, $storeInSession=true)