Passed
Push — master ( cc6329...bce4af )
by
unknown
166:34 queued 142:45
created

PermissionController::getBackendUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
16
namespace TYPO3\CMS\Beuser\Controller;
17
18
use TYPO3\CMS\Backend\Template\Components\ButtonBar;
19
use TYPO3\CMS\Backend\Tree\View\PageTreeView;
20
use TYPO3\CMS\Backend\Utility\BackendUtility;
21
use TYPO3\CMS\Backend\View\BackendTemplateView;
22
use TYPO3\CMS\Core\DataHandling\DataHandler;
23
use TYPO3\CMS\Core\Imaging\Icon;
24
use TYPO3\CMS\Core\Messaging\FlashMessage;
25
use TYPO3\CMS\Core\Utility\GeneralUtility;
26
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
27
use TYPO3\CMS\Extbase\Mvc\View\ViewInterface;
28
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
29
30
/**
31
 * Backend module page permissions
32
 * @internal This class is a TYPO3 Backend implementation and is not considered part of the Public TYPO3 API.
33
 */
34
class PermissionController extends ActionController
35
{
36
    /**
37
     * @var string prefix for session
38
     */
39
    const SESSION_PREFIX = 'tx_Beuser_';
40
41
    /**
42
     * @var int the current page id
43
     */
44
    protected $id;
45
46
    /**
47
     * @var string
48
     */
49
    protected $returnUrl = '';
50
51
    /**
52
     * @var int
53
     */
54
    protected $depth;
55
56
    /**
57
     * Number of levels to enable recursive settings for
58
     *
59
     * @var int
60
     */
61
    protected $getLevels = 10;
62
63
    /**
64
     * @var array
65
     */
66
    protected $pageInfo = [];
67
68
    /**
69
     * Backend Template Container
70
     *
71
     * @var string
72
     */
73
    protected $defaultViewObjectName = BackendTemplateView::class;
74
75
    /**
76
     * BackendTemplateContainer
77
     *
78
     * @var BackendTemplateView
79
     */
80
    protected $view;
81
82
    /**
83
     * Initialize action
84
     */
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 = (string)$this->request->getArgument('returnUrl');
109
        }
110
111
        $this->setPageInfo();
112
    }
113
114
    /**
115
     * Initializes view
116
     *
117
     * @param ViewInterface $view The view to be initialized
118
     */
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
129
            $this->registerDocHeaderButtons();
130
            $this->view->getModuleTemplate()->getDocHeaderComponent()->setMetaInformation($this->pageInfo);
131
            $this->view->getModuleTemplate()->setFlashMessageQueue($this->getFlashMessageQueue());
132
        }
133
    }
134
135
    /**
136
     * Registers the Icons into the docheader
137
     *
138
     * @throws \InvalidArgumentException
139
     */
140
    protected function registerDocHeaderButtons()
141
    {
142
        /** @var ButtonBar $buttonBar */
143
        $buttonBar = $this->view->getModuleTemplate()->getDocHeaderComponent()->getButtonBar();
144
        $currentRequest = $this->request;
145
        $lang = $this->getLanguageService();
146
147
        if ($currentRequest->getControllerActionName() === 'edit') {
148
            // CLOSE button:
149
            if (!empty($this->returnUrl)) {
150
                $closeButton = $buttonBar->makeLinkButton()
151
                    ->setHref($this->returnUrl)
152
                    ->setTitle($lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:rm.closeDoc'))
153
                    ->setIcon($this->view->getModuleTemplate()->getIconFactory()->getIcon(
154
                        'actions-close',
155
                        Icon::SIZE_SMALL
156
                    ));
157
                $buttonBar->addButton($closeButton);
158
            }
159
160
            // SAVE button:
161
            $saveButton = $buttonBar->makeInputButton()
162
                ->setTitle($lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:rm.saveCloseDoc'))
163
                ->setName('tx_beuser_system_beusertxpermission[submit]')
164
                ->setValue('Save')
165
                ->setForm('PermissionControllerEdit')
166
                ->setIcon($this->view->getModuleTemplate()->getIconFactory()->getIcon(
167
                    'actions-document-save',
168
                    Icon::SIZE_SMALL
169
                ))
170
                ->setShowLabelText(true);
171
172
            $buttonBar->addButton($saveButton);
173
        }
174
175
        $shortcutButton = $buttonBar->makeShortcutButton()
176
            ->setRouteIdentifier('system_BeuserTxPermission')
177
            ->setDisplayName($this->getShortcutTitle())
178
            ->setArguments(['id' => (int)$this->id]);
179
        $buttonBar->addButton($shortcutButton);
180
181
        $helpButton = $buttonBar->makeHelpButton()
182
            ->setModuleName('xMOD_csh_corebe')
183
            ->setFieldName('perm_module');
184
185
        $buttonBar->addButton($helpButton);
186
    }
187
188
    /**
189
     * Index action
190
     */
191
    public function indexAction()
192
    {
193
        if (!$this->id) {
194
            $this->pageInfo = ['title' => $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'], 'uid' => 0, 'pid' => 0];
195
        }
196
197
        if ($this->getBackendUser()->workspace != 0) {
198
            // Adding section with the permission setting matrix:
199
            $this->addFlashMessage(
200
                LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf:WorkspaceWarningText', 'beuser') ?? '',
201
                LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf:WorkspaceWarning', 'beuser') ?? '',
202
                FlashMessage::WARNING
203
            );
204
        }
205
206
        // depth options
207
        $depthOptions = [];
208
        $url = $this->uriBuilder->reset()->setArguments([
209
            'action' => 'index',
210
            'depth' => '${value}',
211
            'id' => $this->id
212
        ])->buildBackendUri();
213
        foreach ([1, 2, 3, 4, 10] as $depthLevel) {
214
            $levelLabel = $depthLevel === 1 ? 'level' : 'levels';
215
            $depthOptions[$depthLevel] = $depthLevel . ' ' . LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf:' . $levelLabel, 'beuser');
216
        }
217
        $this->view->assign('currentId', $this->id);
218
        $this->view->assign('depthBaseUrl', $url);
219
        $this->view->assign('depth', $this->depth);
220
        $this->view->assign('depthOptions', $depthOptions);
221
222
        $beUserArray = BackendUtility::getUserNames();
223
        $this->view->assign('beUsers', $beUserArray);
224
        $beGroupArray = BackendUtility::getGroupNames();
225
        $this->view->assign('beGroups', $beGroupArray);
226
227
        /** @var PageTreeView $tree */
228
        $tree = GeneralUtility::makeInstance(PageTreeView::class);
229
        $tree->init();
230
        $tree->addField('perms_user', true);
231
        $tree->addField('perms_group', true);
232
        $tree->addField('perms_everybody', true);
233
        $tree->addField('perms_userid', true);
234
        $tree->addField('perms_groupid', true);
235
        $tree->addField('hidden');
236
        $tree->addField('fe_group');
237
        $tree->addField('starttime');
238
        $tree->addField('endtime');
239
        $tree->addField('editlock');
240
241
        // Create the tree from $this->id
242
        if ($this->id) {
243
            $tree->tree[] = ['row' => $this->pageInfo, 'HTML' => $tree->getIcon($this->pageInfo)];
244
        } else {
245
            $tree->tree[] = ['row' => $this->pageInfo, 'HTML' => $tree->getRootIcon($this->pageInfo)];
246
        }
247
        $tree->getTree($this->id, $this->depth);
248
        $this->view->assign('viewTree', $tree->tree);
249
    }
250
251
    /**
252
     * Edit action
253
     */
254
    public function editAction()
255
    {
256
        $this->view->assign('id', $this->id);
257
        $this->view->assign('depth', $this->depth);
258
259
        if (!$this->id) {
260
            $this->pageInfo = ['title' => $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'], 'uid' => 0, 'pid' => 0];
261
        }
262
        if ($this->getBackendUser()->workspace != 0) {
263
            // Adding FlashMessage with the permission setting matrix:
264
            $this->addFlashMessage(
265
                LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf:WorkspaceWarningText', 'beuser') ?? '',
266
                LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf:WorkspaceWarning', 'beuser') ?? '',
267
                FlashMessage::WARNING
268
            );
269
        }
270
        // Get user names and group names
271
        $beGroupArray = BackendUtility::getGroupNames();
272
        $beUserArray  = BackendUtility::getUserNames();
273
274
        // Owner selector
275
        $beUserDataArray = [0 => LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf:selectNone', 'beuser')];
276
        foreach ($beUserArray as $uid => &$row) {
277
            $beUserDataArray[$uid] = $row['username'];
278
        }
279
        $beUserDataArray[-1] = LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf:selectUnchanged', 'beuser');
280
        $this->view->assign('currentBeUser', $this->pageInfo['perms_userid']);
281
        $this->view->assign('beUserData', $beUserDataArray);
282
283
        // Group selector
284
        $beGroupDataArray = [0 => LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf:selectNone', 'beuser')];
285
        foreach ($beGroupArray as $uid => $row) {
286
            $beGroupDataArray[$uid] = $row['title'];
287
        }
288
        $beGroupDataArray[-1] = LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf:selectUnchanged', 'beuser');
289
        $this->view->assign('currentBeGroup', $this->pageInfo['perms_groupid']);
290
        $this->view->assign('beGroupData', $beGroupDataArray);
291
        $this->view->assign('pageInfo', $this->pageInfo);
292
        $this->view->assign('returnUrl', $this->returnUrl);
293
        $this->view->assign('recursiveSelectOptions', $this->getRecursiveSelectOptions());
294
    }
295
296
    /**
297
     * Update action
298
     *
299
     * @param array $data
300
     * @param array $mirror
301
     */
302
    protected function updateAction(array $data, array $mirror)
303
    {
304
        $dataHandlerInput = [];
305
        // Prepare the input data for data handler
306
        if (!empty($data['pages'])) {
307
            foreach ($data['pages'] as $pageUid => $properties) {
308
                // if the owner and group field shouldn't be touched, unset the option
309
                if ((int)$properties['perms_userid'] === -1) {
310
                    unset($properties['perms_userid']);
311
                }
312
                if ((int)$properties['perms_groupid'] === -1) {
313
                    unset($properties['perms_groupid']);
314
                }
315
                $dataHandlerInput[$pageUid] = $properties;
316
                if (!empty($mirror['pages'][$pageUid])) {
317
                    $mirrorPages = GeneralUtility::intExplode(',', $mirror['pages'][$pageUid]);
318
                    foreach ($mirrorPages as $mirrorPageUid) {
319
                        $dataHandlerInput[$mirrorPageUid] = $properties;
320
                    }
321
                }
322
            }
323
        }
324
325
        $dataHandler = GeneralUtility::makeInstance(DataHandler::class);
326
        $dataHandler->start(
327
            [
328
                'pages' => $dataHandlerInput
329
            ],
330
            []
331
        );
332
        $dataHandler->process_datamap();
333
334
        $this->redirectToUri($this->returnUrl);
335
    }
336
337
    /**
338
     * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
339
     */
340
    protected function getBackendUser()
341
    {
342
        return $GLOBALS['BE_USER'];
343
    }
344
345
    /**
346
     * Finding tree and offer setting of values recursively.
347
     *
348
     * @return array
349
     */
350
    protected function getRecursiveSelectOptions()
351
    {
352
        // Initialize tree object:
353
        $tree = GeneralUtility::makeInstance(PageTreeView::class);
354
        $tree->init();
355
        $tree->addField('perms_userid', true);
356
        $tree->makeHTML = 0;
357
        // Make tree:
358
        $tree->getTree($this->id, $this->getLevels);
359
        $options = [];
360
        $options[''] = '';
361
        // If there are a hierarchy of page ids, then...
362
        if ($this->getBackendUser()->user['uid'] && !empty($tree->orig_ids_hierarchy)) {
363
            // Init:
364
            $labelRecursive = LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf:recursive', 'beuser');
365
            $labelLevel = LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf:level', 'beuser');
366
            $labelLevels = LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf:levels', 'beuser');
367
            $labelPageAffected = LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf:page_affected', 'beuser');
368
            $labelPagesAffected = LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang_mod_permission.xlf:pages_affected', 'beuser');
369
            $theIdListArr = [];
370
            // Traverse the number of levels we want to allow recursive
371
            // setting of permissions for:
372
            for ($a = $this->getLevels; $a > 0; $a--) {
373
                if (is_array($tree->orig_ids_hierarchy[$a])) {
374
                    foreach ($tree->orig_ids_hierarchy[$a] as $theId) {
375
                        $theIdListArr[] = $theId;
376
                    }
377
                    $lKey = $this->getLevels - $a + 1;
378
                    $pagesCount = count($theIdListArr);
379
                    $options[implode(',', $theIdListArr)] = $labelRecursive . ' ' . $lKey . ' ' . ($lKey === 1 ? $labelLevel : $labelLevels) .
380
                        ' (' . $pagesCount . ' ' . ($pagesCount === 1 ? $labelPageAffected : $labelPagesAffected) . ')';
381
                }
382
            }
383
        }
384
        return $options;
385
    }
386
387
    /**
388
     * Check if page record exists and set pageInfo
389
     */
390
    protected function setPageInfo(): void
391
    {
392
        $this->pageInfo = BackendUtility::readPageAccess(BackendUtility::getRecord('pages', $this->id) ? $this->id : 0, ' 1=1') ?: [];
393
    }
394
395
    /**
396
     * Returns LanguageService
397
     *
398
     * @return \TYPO3\CMS\Core\Localization\LanguageService
399
     */
400
    protected function getLanguageService()
401
    {
402
        return $GLOBALS['LANG'];
403
    }
404
405
    /**
406
     * Returns the shortcut title for the current page
407
     *
408
     * @return string
409
     */
410
    protected function getShortcutTitle(): string
411
    {
412
        return sprintf(
413
            '%s: %s [%d]',
414
            $this->getLanguageService()->sL('LLL:EXT:beuser/Resources/Private/Language/locallang_mod.xlf:mlang_tabs_tab'),
415
            BackendUtility::getRecordTitle('pages', $this->pageInfo),
416
            $this->id
417
        );
418
    }
419
}
420