[!!!][FEATURE] Flash message view helper add deferred rendering
[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\Tree\View\PageTreeView;
18 use TYPO3\CMS\Backend\Utility\IconUtility;
19 use TYPO3\CMS\Core\Database\DatabaseConnection;
20 use TYPO3\CMS\Core\Messaging\FlashMessage;
21 use TYPO3\CMS\Core\Utility\GeneralUtility;
22 use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
23 use TYPO3\CMS\Backend\Utility\BackendUtility;
24 use TYPO3\CMS\Extbase\Mvc\View\ViewInterface;
25 use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
26
27 /**
28 * Backend module page permissions
29 */
30 class PermissionController extends ActionController {
31
32 /**
33 * @var string prefix for session
34 */
35 const SESSION_PREFIX = 'tx_Beuser_';
36
37 /**
38 * @var int the current page id
39 */
40 protected $id;
41
42 /**
43 * @var int
44 */
45 protected $returnId;
46
47 /**
48 * @var int
49 */
50 protected $depth;
51
52 /**
53 * @var int
54 */
55 protected $lastEdited;
56
57 /**
58 * Number of levels to enable recursive settings for
59 *
60 * @var int
61 */
62 protected $getLevels = 10;
63
64 /**
65 * @var array
66 */
67 protected $pageInfo = array();
68
69 /**
70 * Initialize action
71 *
72 * @return void
73 */
74 protected function initializeAction() {
75 // determine id parameter
76 $this->id = (int)GeneralUtility::_GP('id');
77 if ($this->request->hasArgument('id')) {
78 $this->id = (int)$this->request->getArgument('id');
79 }
80
81 // determine depth parameter
82 $this->depth = ((int)GeneralUtility::_GP('depth') > 0)
83 ? (int) GeneralUtility::_GP('depth')
84 : $this->getBackendUser()->getSessionData(self::SESSION_PREFIX . 'depth');
85 if ($this->request->hasArgument('depth')) {
86 $this->depth = (int)$this->request->getArgument('depth');
87 }
88 $this->getBackendUser()->setAndSaveSessionData(self::SESSION_PREFIX . 'depth', $this->depth);
89 $this->lastEdited = GeneralUtility::_GP('lastEdited');
90 $this->returnId = GeneralUtility::_GP('returnId');
91 $this->pageInfo = BackendUtility::readPageAccess($this->id, ' 1=1');
92 }
93
94 /**
95 * Initializes view
96 *
97 * @param ViewInterface $view The view to be initialized
98 * @return void
99 */
100 protected function initializeView(ViewInterface $view) {
101 $view->assign(
102 'previewUrl',
103 BackendUtility::viewonclick(
104 $this->pageInfo['uid'], $GLOBALS['BACK_PATH'],
105 BackendUtility::BEgetRootLine($this->pageInfo['uid'])
106 )
107 );
108 }
109
110 /**
111 * Index action
112 *
113 * @return void
114 */
115 public function indexAction() {
116 if (!$this->id) {
117 $this->pageInfo = array('title' => '[root-level]', 'uid' => 0, 'pid' => 0);
118 }
119
120 if ($this->getBackendUser()->workspace != 0) {
121 // Adding section with the permission setting matrix:
122 $this->addFlashMessage(
123 'LLL:EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf:WorkspaceWarningText',
124 'LLL:EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf:WorkspaceWarning',
125 FlashMessage::WARNING
126 );
127 }
128
129 // depth options
130 $depthOptions = array();
131 $url = $this->uriBuilder->reset()->setArguments(array(
132 'action' => 'index',
133 'depth' => '__DEPTH__',
134 'id' => $this->id
135 ))->buildBackendUri();
136 foreach (array(1, 2, 3, 4, 10) as $depthLevel) {
137 $depthOptions[$depthLevel] = $depthLevel . ' ' . LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf:levels', 'beuser');
138 }
139 $this->view->assign('depthBaseUrl', $url);
140 $this->view->assign('depth', $this->depth);
141 $this->view->assign('depthOptions', $depthOptions);
142
143 $beUserArray = BackendUtility::getUserNames();
144 $this->view->assign('beUsers', $beUserArray);
145 $beGroupArray = BackendUtility::getGroupNames();
146 $this->view->assign('beGroups', $beGroupArray);
147
148 /** @var PageTreeView */
149 $tree = GeneralUtility::makeInstance(PageTreeView::class);
150 $tree->init();
151 $tree->addField('perms_user', TRUE);
152 $tree->addField('perms_group', TRUE);
153 $tree->addField('perms_everybody', TRUE);
154 $tree->addField('perms_userid', TRUE);
155 $tree->addField('perms_groupid', TRUE);
156 $tree->addField('hidden');
157 $tree->addField('fe_group');
158 $tree->addField('starttime');
159 $tree->addField('endtime');
160 $tree->addField('editlock');
161
162 // Creating top icon; the current page
163 $html = IconUtility::getSpriteIconForRecord('pages', $this->pageInfo);
164 $tree->tree[] = array('row' => $this->pageInfo, 'HTML' => $html);
165
166 // Create the tree from $this->id
167 $tree->getTree($this->id, $this->depth, '');
168 $this->view->assign('viewTree', $tree->tree);
169
170 // CSH for permissions setting
171 $this->view->assign('cshItem', BackendUtility::cshItem('xMOD_csh_corebe', 'perm_module', $GLOBALS['BACK_PATH']));
172 }
173
174 /**
175 * Edit action
176 *
177 * @return void
178 */
179 public function editAction() {
180 $this->view->assign('id', $this->id);
181 $this->view->assign('depth', $this->depth);
182
183 if (!$this->id) {
184 $this->pageInfo = array('title' => '[root-level]', 'uid' => 0, 'pid' => 0);
185 }
186 if ($this->getBackendUser()->workspace != 0) {
187 // Adding FlashMessage with the permission setting matrix:
188 $this->addFlashMessage(
189 LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf:WorkspaceWarningText', 'beuser'),
190 LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf:WorkspaceWarning', 'beuser'),
191 FlashMessage::WARNING
192 );
193 }
194 // Get usernames and groupnames
195 $beGroupArray = BackendUtility::getListGroupNames('title,uid');
196 $beUserArray = BackendUtility::getUserNames();
197
198 // Owner selector
199 $beUserDataArray = array(0 => LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf:selectNone', 'beuser'));
200 foreach ($beUserArray as $uid => &$row) {
201 $beUserDataArray[$uid] = $row['username'];
202 }
203 $beUserDataArray[-1] = LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf:selectUnchanged', 'beuser');
204 $this->view->assign('currentBeUser', $this->pageInfo['perms_userid']);
205 $this->view->assign('beUserData', $beUserDataArray);
206
207 // Group selector
208 $beGroupDataArray = array(0 => LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf:selectNone', 'beuser'));
209 foreach ($beGroupArray as $uid => $row) {
210 $beGroupDataArray[$uid] = $row['title'];
211 }
212 $beGroupDataArray[-1] = LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf:selectUnchanged', 'beuser');
213 $this->view->assign('currentBeGroup', $this->pageInfo['perms_groupid']);
214 $this->view->assign('beGroupData', $beGroupDataArray);
215 $this->view->assign('pageInfo', $this->pageInfo);
216 $this->view->assign('returnId', $this->returnId);
217 $this->view->assign('recursiveSelectOptions', $this->getRecursiveSelectOptions());
218 }
219
220 /**
221 * Update action
222 *
223 * @param array $data
224 * @param array $mirror
225 * @return void
226 */
227 protected function updateAction(array $data, array $mirror) {
228 if (!empty($data['pages'])) {
229 foreach ($data['pages'] as $pageUid => $properties) {
230 // if the owner and group field shouldn't be touched, unset the option
231 if ((int)$properties['perms_userid'] === -1) {
232 unset($properties['perms_userid']);
233 }
234 if ((int)$properties['perms_groupid'] === -1) {
235 unset($properties['perms_groupid']);
236 }
237 $this->getDatabaseConnection()->exec_UPDATEquery(
238 'pages',
239 'uid = ' . (int)$pageUid,
240 $properties
241 );
242 if (!empty($mirror['pages'][$pageUid])) {
243 $mirrorPages = GeneralUtility::trimExplode(',', $mirror['pages'][$pageUid]);
244 foreach ($mirrorPages as $mirrorPageUid) {
245 $this->getDatabaseConnection()->exec_UPDATEquery(
246 'pages',
247 'uid = ' . (int)$mirrorPageUid,
248 $properties
249 );
250 }
251 }
252 }
253 }
254 $this->redirect('index', NULL, NULL, array('id' => $this->returnId, 'depth' => $this->depth));
255 }
256
257 /**
258 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
259 */
260 protected function getBackendUser() {
261 return $GLOBALS['BE_USER'];
262 }
263
264 /**
265 * @return DatabaseConnection
266 */
267 protected function getDatabaseConnection() {
268 return $GLOBALS['TYPO3_DB'];
269 }
270
271 /**
272 * Finding tree and offer setting of values recursively.
273 *
274 * @return array
275 */
276 protected function getRecursiveSelectOptions() {
277 // Initialize tree object:
278 $tree = GeneralUtility::makeInstance(PageTreeView::class);
279 $tree->init();
280 $tree->addField('perms_userid', TRUE);
281 $tree->makeHTML = 0;
282 $tree->setRecs = 1;
283 // Make tree:
284 $tree->getTree($this->id, $this->getLevels, '');
285 $options = array();
286 $options[''] = '';
287 // If there are a hierarchy of page ids, then...
288 if ($this->getBackendUser()->user['uid'] && count($tree->orig_ids_hierarchy)) {
289 // Init:
290 $labelRecursive = LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf:recursive', 'beuser');
291 $labelLevels = LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf:levels', 'beuser');
292 $labelPagesAffected = LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf:pages_affected', 'beuser');
293 $theIdListArr = array();
294 // Traverse the number of levels we want to allow recursive
295 // setting of permissions for:
296 for ($a = $this->getLevels; $a > 0; $a--) {
297 if (is_array($tree->orig_ids_hierarchy[$a])) {
298 foreach ($tree->orig_ids_hierarchy[$a] as $theId) {
299 $theIdListArr[] = $theId;
300 }
301 $lKey = $this->getLevels - $a + 1;
302 $options[implode(',', $theIdListArr)] = $labelRecursive . ' ' . $lKey . ' ' . $labelLevels .
303 ' (' . count($theIdListArr) . ' ' . $labelPagesAffected . ')';
304 }
305 }
306 }
307 return $options;
308 }
309
310 }