[TASK] Use DocHeader buttons in PermissionController
[Packages/TYPO3.CMS.git] / typo3 / sysext / beuser / Classes / Controller / PermissionController.php
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
17 use TYPO3\CMS\Backend\Template\Components\ButtonBar;
18 use TYPO3\CMS\Backend\Tree\View\PageTreeView;
19 use TYPO3\CMS\Backend\Utility\BackendUtility;
20 use TYPO3\CMS\Backend\View\BackendTemplateView;
21 use TYPO3\CMS\Core\Database\DatabaseConnection;
22 use TYPO3\CMS\Core\Imaging\Icon;
23 use TYPO3\CMS\Core\Messaging\FlashMessage;
24 use TYPO3\CMS\Core\Utility\GeneralUtility;
25 use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
26 use TYPO3\CMS\Extbase\Mvc\View\ViewInterface;
27 use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
28
29 /**
30 * Backend module page permissions
31 */
32 class PermissionController extends ActionController
33 {
34 /**
35 * @var string prefix for session
36 */
37 const SESSION_PREFIX = 'tx_Beuser_';
38
39 /**
40 * @var int the current page id
41 */
42 protected $id;
43
44 /**
45 * @var int
46 */
47 protected $returnId;
48
49 /**
50 * @var int
51 */
52 protected $depth;
53
54 /**
55 * @var int
56 */
57 protected $lastEdited;
58
59 /**
60 * Number of levels to enable recursive settings for
61 *
62 * @var int
63 */
64 protected $getLevels = 10;
65
66 /**
67 * @var array
68 */
69 protected $pageInfo = array();
70
71 /**
72 * Backend Template Container
73 *
74 * @var string
75 */
76 protected $defaultViewObjectName = BackendTemplateView::class;
77
78 /**
79 * BackendTemplateContainer
80 *
81 * @var BackendTemplateView
82 */
83 protected $view;
84
85 /**
86 * Initialize action
87 *
88 * @return void
89 */
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
111 /**
112 * Initializes view
113 *
114 * @param ViewInterface $view The view to be initialized
115 * @return void
116 */
117 protected function initializeView(ViewInterface $view)
118 {
119 parent::initializeView($view);
120 $view->assign(
121 'previewUrl',
122 BackendUtility::viewonclick(
123 $this->pageInfo['uid'], '',
124 BackendUtility::BEgetRootLine($this->pageInfo['uid'])
125 )
126 );
127
128 // the view of the update action has a different view class
129 if ($view instanceof BackendTemplateView) {
130 $view->getModuleTemplate()->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Beuser/Permissions');
131 $view->getModuleTemplate()->addJavaScriptCode(
132 'jumpToUrl',
133 '
134 function jumpToUrl(URL) {
135 window.location.href = URL;
136 return false;
137 }
138 '
139 );
140 $this->registerDocHeaderButtons();
141 $this->view->getModuleTemplate()->setFlashMessageQueue($this->controllerContext->getFlashMessageQueue());
142 }
143 }
144
145 /**
146 * Registers the Icons into the docheader
147 *
148 * @return void
149 * @throws \InvalidArgumentException
150 */
151 protected function registerDocHeaderButtons()
152 {
153 /** @var ButtonBar $buttonBar */
154 $buttonBar = $this->view->getModuleTemplate()->getDocHeaderComponent()->getButtonBar();
155 $currentRequest = $this->request;
156 $moduleName = $currentRequest->getPluginName();
157 $getVars = $this->request->getArguments();
158 $lang = $this->getLanguageService();
159
160 $extensionName = $currentRequest->getControllerExtensionName();
161 if (empty($getVars)) {
162 $modulePrefix = strtolower('tx_' . $extensionName . '_' . $moduleName);
163 $getVars = array('id', 'M', $modulePrefix);
164 }
165
166 if ($currentRequest->getControllerActionName() === 'edit') {
167 // CLOSE button:
168 $closeUrl = $this->uriBuilder->reset()->setArguments(array(
169 'action' => 'index',
170 'id' => $this->id
171 ))->buildBackendUri();
172 $closeButton = $buttonBar->makeLinkButton()
173 ->setHref($closeUrl)
174 ->setTitle($lang->sL('LLL:EXT:lang/locallang_core.xlf:rm.closeDoc'))
175 ->setIcon($this->view->getModuleTemplate()->getIconFactory()->getIcon(
176 'actions-document-close',
177 Icon::SIZE_SMALL
178 ));
179 $buttonBar->addButton($closeButton);
180
181 // SAVE button:
182 $saveButton = $buttonBar->makeInputButton()
183 ->setTitle($lang->sL('LLL:EXT:lang/locallang_core.xlf:rm.saveCloseDoc'))
184 ->setName('tx_beuser_system_beusertxpermission[submit]')
185 ->setValue('Save')
186 ->setForm('PermissionControllerEdit')
187 ->setIcon($this->view->getModuleTemplate()->getIconFactory()->getIcon(
188 'actions-document-save',
189 Icon::SIZE_SMALL
190 ))
191 ->setShowLabelText(true);
192
193 $buttonBar->addButton($saveButton);
194 }
195
196 // SHORTCUT botton:
197 $shortcutButton = $buttonBar->makeShortcutButton()
198 ->setModuleName($moduleName)
199 ->setGetVariables($getVars);
200 $buttonBar->addButton($shortcutButton);
201
202 if ($this->id > 0) {
203 $iconFactory = $this->view->getModuleTemplate()->getIconFactory();
204 $viewButton = $buttonBar->makeLinkButton()
205 ->setOnClick(BackendUtility::viewOnClick($this->pageInfo['uid'], '',
206 BackendUtility::BEgetRootLine($this->pageInfo['uid'])))
207 ->setTitle($this->getLanguageService()->sL('LLL:EXT:lang/locallang_core.xlf:labels.showPage'))
208 ->setIcon($iconFactory->getIcon('actions-document-view', Icon::SIZE_SMALL))
209 ->setHref('#');
210
211 $buttonBar->addButton($viewButton, ButtonBar::BUTTON_POSITION_LEFT, 3);
212 }
213 }
214
215 /**
216 * Index action
217 *
218 * @return void
219 */
220 public function indexAction()
221 {
222 if (!$this->id) {
223 $this->pageInfo = array('title' => '[root-level]', 'uid' => 0, 'pid' => 0);
224 }
225
226 if ($this->getBackendUser()->workspace != 0) {
227 // Adding section with the permission setting matrix:
228 $this->addFlashMessage(
229 LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf:WorkspaceWarningText', 'beuser'),
230 LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf:WorkspaceWarning', 'beuser'),
231 FlashMessage::WARNING
232 );
233 }
234
235 // depth options
236 $depthOptions = array();
237 $url = $this->uriBuilder->reset()->setArguments(array(
238 'action' => 'index',
239 'depth' => '__DEPTH__',
240 'id' => $this->id
241 ))->buildBackendUri();
242 foreach (array(1, 2, 3, 4, 10) as $depthLevel) {
243 $depthOptions[$depthLevel] = $depthLevel . ' ' . LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf:levels', 'beuser');
244 }
245 $this->view->assign('depthBaseUrl', $url);
246 $this->view->assign('depth', $this->depth);
247 $this->view->assign('depthOptions', $depthOptions);
248
249 $beUserArray = BackendUtility::getUserNames();
250 $this->view->assign('beUsers', $beUserArray);
251 $beGroupArray = BackendUtility::getGroupNames();
252 $this->view->assign('beGroups', $beGroupArray);
253
254 /** @var $tree PageTreeView */
255 $tree = GeneralUtility::makeInstance(PageTreeView::class);
256 $tree->init();
257 $tree->addField('perms_user', true);
258 $tree->addField('perms_group', true);
259 $tree->addField('perms_everybody', true);
260 $tree->addField('perms_userid', true);
261 $tree->addField('perms_groupid', true);
262 $tree->addField('hidden');
263 $tree->addField('fe_group');
264 $tree->addField('starttime');
265 $tree->addField('endtime');
266 $tree->addField('editlock');
267
268 // Create the tree from $this->id
269 if ($this->id) {
270 $tree->tree[] = array('row' => $this->pageInfo, 'HTML' => $tree->getIcon($this->id));
271 } else {
272 $tree->tree[] = array('row' => $this->pageInfo, 'HTML' => $tree->getRootIcon($this->pageInfo));
273 }
274 $tree->getTree($this->id, $this->depth);
275 $this->view->assign('viewTree', $tree->tree);
276
277 // CSH for permissions setting
278 $this->view->assign('cshItem', BackendUtility::cshItem('xMOD_csh_corebe', 'perm_module', null, '<span class="btn btn-default btn-sm">|</span>'));
279 }
280
281 /**
282 * Edit action
283 *
284 * @return void
285 */
286 public function editAction()
287 {
288 $this->view->assign('id', $this->id);
289 $this->view->assign('depth', $this->depth);
290
291 if (!$this->id) {
292 $this->pageInfo = array('title' => '[root-level]', 'uid' => 0, 'pid' => 0);
293 }
294 if ($this->getBackendUser()->workspace != 0) {
295 // Adding FlashMessage with the permission setting matrix:
296 $this->addFlashMessage(
297 LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf:WorkspaceWarningText', 'beuser'),
298 LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf:WorkspaceWarning', 'beuser'),
299 FlashMessage::WARNING
300 );
301 }
302 // Get usernames and groupnames
303 $beGroupArray = BackendUtility::getListGroupNames('title,uid');
304 $beUserArray = BackendUtility::getUserNames();
305
306 // Owner selector
307 $beUserDataArray = array(0 => LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf:selectNone', 'beuser'));
308 foreach ($beUserArray as $uid => &$row) {
309 $beUserDataArray[$uid] = $row['username'];
310 }
311 $beUserDataArray[-1] = LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf:selectUnchanged', 'beuser');
312 $this->view->assign('currentBeUser', $this->pageInfo['perms_userid']);
313 $this->view->assign('beUserData', $beUserDataArray);
314
315 // Group selector
316 $beGroupDataArray = array(0 => LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf:selectNone', 'beuser'));
317 foreach ($beGroupArray as $uid => $row) {
318 $beGroupDataArray[$uid] = $row['title'];
319 }
320 $beGroupDataArray[-1] = LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf:selectUnchanged', 'beuser');
321 $this->view->assign('currentBeGroup', $this->pageInfo['perms_groupid']);
322 $this->view->assign('beGroupData', $beGroupDataArray);
323 $this->view->assign('pageInfo', $this->pageInfo);
324 $this->view->assign('returnId', $this->returnId);
325 $this->view->assign('recursiveSelectOptions', $this->getRecursiveSelectOptions());
326 }
327
328 /**
329 * Update action
330 *
331 * @param array $data
332 * @param array $mirror
333 * @return void
334 */
335 protected function updateAction(array $data, array $mirror)
336 {
337 if (!empty($data['pages'])) {
338 foreach ($data['pages'] as $pageUid => $properties) {
339 // if the owner and group field shouldn't be touched, unset the option
340 if ((int)$properties['perms_userid'] === -1) {
341 unset($properties['perms_userid']);
342 }
343 if ((int)$properties['perms_groupid'] === -1) {
344 unset($properties['perms_groupid']);
345 }
346 $this->getDatabaseConnection()->exec_UPDATEquery(
347 'pages',
348 'uid = ' . (int)$pageUid,
349 $properties
350 );
351 if (!empty($mirror['pages'][$pageUid])) {
352 $mirrorPages = GeneralUtility::trimExplode(',', $mirror['pages'][$pageUid]);
353 foreach ($mirrorPages as $mirrorPageUid) {
354 $this->getDatabaseConnection()->exec_UPDATEquery(
355 'pages',
356 'uid = ' . (int)$mirrorPageUid,
357 $properties
358 );
359 }
360 }
361 }
362 }
363 $this->redirect('index', null, null, array('id' => $this->returnId, 'depth' => $this->depth));
364 }
365
366 /**
367 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
368 */
369 protected function getBackendUser()
370 {
371 return $GLOBALS['BE_USER'];
372 }
373
374 /**
375 * @return DatabaseConnection
376 */
377 protected function getDatabaseConnection()
378 {
379 return $GLOBALS['TYPO3_DB'];
380 }
381
382 /**
383 * Finding tree and offer setting of values recursively.
384 *
385 * @return array
386 */
387 protected function getRecursiveSelectOptions()
388 {
389 // Initialize tree object:
390 $tree = GeneralUtility::makeInstance(PageTreeView::class);
391 $tree->init();
392 $tree->addField('perms_userid', true);
393 $tree->makeHTML = 0;
394 $tree->setRecs = 1;
395 // Make tree:
396 $tree->getTree($this->id, $this->getLevels, '');
397 $options = array();
398 $options[''] = '';
399 // If there are a hierarchy of page ids, then...
400 if ($this->getBackendUser()->user['uid'] && !empty($tree->orig_ids_hierarchy)) {
401 // Init:
402 $labelRecursive = LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf:recursive', 'beuser');
403 $labelLevels = LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf:levels', 'beuser');
404 $labelPagesAffected = LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf:pages_affected', 'beuser');
405 $theIdListArr = array();
406 // Traverse the number of levels we want to allow recursive
407 // setting of permissions for:
408 for ($a = $this->getLevels; $a > 0; $a--) {
409 if (is_array($tree->orig_ids_hierarchy[$a])) {
410 foreach ($tree->orig_ids_hierarchy[$a] as $theId) {
411 $theIdListArr[] = $theId;
412 }
413 $lKey = $this->getLevels - $a + 1;
414 $options[implode(',', $theIdListArr)] = $labelRecursive . ' ' . $lKey . ' ' . $labelLevels .
415 ' (' . count($theIdListArr) . ' ' . $labelPagesAffected . ')';
416 }
417 }
418 }
419 return $options;
420 }
421
422 /**
423 * Returns LanguageService
424 *
425 * @return \TYPO3\CMS\Lang\LanguageService
426 */
427 protected function getLanguageService()
428 {
429 return $GLOBALS['LANG'];
430 }
431 }