BackendUserController::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 18
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 8

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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 Psr\Http\Message\ResponseInterface;
19
use TYPO3\CMS\Backend\Authentication\PasswordReset;
20
use TYPO3\CMS\Backend\Routing\UriBuilder as BackendUriBuilder;
21
use TYPO3\CMS\Backend\Template\Components\ButtonBar;
22
use TYPO3\CMS\Backend\Template\ModuleTemplate;
23
use TYPO3\CMS\Backend\Template\ModuleTemplateFactory;
24
use TYPO3\CMS\Beuser\Domain\Model\BackendUser;
25
use TYPO3\CMS\Beuser\Domain\Model\Demand;
26
use TYPO3\CMS\Beuser\Domain\Model\ModuleData;
27
use TYPO3\CMS\Beuser\Domain\Repository\BackendUserGroupRepository;
28
use TYPO3\CMS\Beuser\Domain\Repository\BackendUserRepository;
29
use TYPO3\CMS\Beuser\Domain\Repository\BackendUserSessionRepository;
30
use TYPO3\CMS\Beuser\Service\UserInformationService;
31
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
32
use TYPO3\CMS\Core\Context\Context;
33
use TYPO3\CMS\Core\Imaging\Icon;
34
use TYPO3\CMS\Core\Imaging\IconFactory;
35
use TYPO3\CMS\Core\Messaging\FlashMessage;
36
use TYPO3\CMS\Core\Page\PageRenderer;
37
use TYPO3\CMS\Core\Pagination\SimplePagination;
38
use TYPO3\CMS\Core\Utility\GeneralUtility;
39
use TYPO3\CMS\Extbase\Http\ForwardResponse;
40
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
41
use TYPO3\CMS\Extbase\Mvc\Exception\StopActionException;
42
use TYPO3\CMS\Extbase\Mvc\Request;
43
use TYPO3\CMS\Extbase\Mvc\RequestInterface;
44
use TYPO3\CMS\Extbase\Mvc\View\ViewInterface;
45
use TYPO3\CMS\Extbase\Pagination\QueryResultPaginator;
46
use TYPO3\CMS\Extbase\Persistence\QueryResultInterface;
47
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
48
49
/**
50
 * Backend module user and user group administration controller
51
 * @internal This class is a TYPO3 Backend implementation and is not considered part of the Public TYPO3 API.
52
 */
53
class BackendUserController extends ActionController
54
{
55
    protected ?ModuleData $moduleData = null;
56
    protected ?ModuleTemplate $moduleTemplate = null;
57
    protected BackendUserRepository $backendUserRepository;
58
    protected BackendUserGroupRepository $backendUserGroupRepository;
59
    protected BackendUserSessionRepository $backendUserSessionRepository;
60
    protected UserInformationService $userInformationService;
61
    protected ModuleTemplateFactory $moduleTemplateFactory;
62
    protected BackendUriBuilder $backendUriBuilder;
63
    protected IconFactory $iconFactory;
64
    protected PageRenderer $pageRenderer;
65
66
    public function __construct(
67
        BackendUserRepository $backendUserRepository,
68
        BackendUserGroupRepository $backendUserGroupRepository,
69
        BackendUserSessionRepository $backendUserSessionRepository,
70
        UserInformationService $userInformationService,
71
        ModuleTemplateFactory $moduleTemplateFactory,
72
        BackendUriBuilder $backendUriBuilder,
73
        IconFactory $iconFactory,
74
        PageRenderer $pageRenderer
75
    ) {
76
        $this->backendUserRepository = $backendUserRepository;
77
        $this->backendUserGroupRepository = $backendUserGroupRepository;
78
        $this->backendUserSessionRepository = $backendUserSessionRepository;
79
        $this->userInformationService = $userInformationService;
80
        $this->moduleTemplateFactory = $moduleTemplateFactory;
81
        $this->backendUriBuilder = $backendUriBuilder;
82
        $this->iconFactory = $iconFactory;
83
        $this->pageRenderer = $pageRenderer;
84
    }
85
86
    /**
87
     * Override the default action if found in user uc
88
     *
89
     * @param RequestInterface $request
90
     * @return ResponseInterface
91
     */
92
    public function processRequest(RequestInterface $request): ResponseInterface
93
    {
94
        $arguments = $request->getArguments();
95
        $backendUser = $this->getBackendUser();
96
        if (is_array($arguments) && isset($arguments['action']) && in_array((string)$arguments['action'], ['index', 'groups', 'online'])
97
            && (string)($backendUser->uc['beuser']['defaultAction'] ?? '') !== (string)$arguments['action']
98
        ) {
99
            $backendUser->uc['beuser']['defaultAction'] = (string)$arguments['action'];
100
            $backendUser->writeUC();
101
        } elseif (!isset($arguments['action']) && isset($backendUser->uc['beuser']['defaultAction'])
102
            && in_array((string)$backendUser->uc['beuser']['defaultAction'], ['index', 'groups', 'online'])
103
        ) {
104
            if ($request instanceof Request) {
105
                $request->setControllerActionName((string)$backendUser->uc['beuser']['defaultAction']);
106
            }
107
        }
108
        return parent::processRequest($request);
109
    }
110
111
    /**
112
     * Init module state.
113
     * This isn't done within __construct() since the controller
114
     * object is only created once in extbase when multiple actions are called in
115
     * one call. When those change module state, the second action would see old state.
116
     */
117
    public function initializeAction(): void
118
    {
119
        $this->moduleData = ModuleData::fromUc((array)($this->getBackendUser()->getModuleData('tx_beuser')));
120
        $this->moduleTemplate = $this->moduleTemplateFactory->create($this->request);
121
        $this->moduleTemplate->setTitle(LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang_mod.xlf:mlang_tabs_tab'));
122
    }
123
124
    /**
125
     * Assign default variables to view
126
     * @param ViewInterface $view
127
     */
128
    protected function initializeView(ViewInterface $view): void
129
    {
130
        $view->assignMultiple([
131
            'dateFormat' => $GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'],
132
            'timeFormat' => $GLOBALS['TYPO3_CONF_VARS']['SYS']['hhmm'],
133
        ]);
134
135
        // Load requireJS modules
136
        $this->pageRenderer->loadRequireJsModule('TYPO3/CMS/Backend/ContextMenu');
137
        $this->pageRenderer->loadRequireJsModule('TYPO3/CMS/Backend/Modal');
138
        $this->pageRenderer->loadRequireJsModule('TYPO3/CMS/Beuser/BackendUserListing');
139
    }
140
141
    /**
142
     * Displays all BackendUsers
143
     *
144
     * @param Demand|null $demand
145
     * @param int $currentPage
146
     * @param string $operation
147
     * @return ResponseInterface
148
     */
149
    public function indexAction(Demand $demand = null, int $currentPage = 1, string $operation = ''): ResponseInterface
150
    {
151
        $backendUser = $this->getBackendUser();
152
153
        if ($operation === 'reset-filters') {
154
            // Reset the module data demand object
155
            $this->moduleData->setDemand(new Demand());
0 ignored issues
show
Bug introduced by
The method setDemand() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

155
            $this->moduleData->/** @scrutinizer ignore-call */ 
156
                               setDemand(new Demand());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
156
            $demand = null;
157
        }
158
        if ($demand === null) {
159
            $demand = $this->moduleData->getDemand();
160
        } else {
161
            $this->moduleData->setDemand($demand);
162
        }
163
        $backendUser->pushModuleData('tx_beuser', $this->moduleData->forUc());
164
165
        $compareUserList = $this->moduleData->getCompareUserList();
166
        $backendUsers = $this->backendUserRepository->findDemanded($demand);
167
        $paginator = new QueryResultPaginator($backendUsers, $currentPage, 50);
168
        $pagination = new SimplePagination($paginator);
169
170
        $this->view->assignMultiple([
171
            'onlineBackendUsers' => $this->getOnlineBackendUsers(),
172
            'demand' => $demand,
173
            'paginator' => $paginator,
174
            'pagination' => $pagination,
175
            'totalAmountOfBackendUsers' => $backendUsers->count(),
176
            'backendUserGroups' => array_merge([''], $this->backendUserGroupRepository->findAll()->toArray()),
177
            'compareUserUidList' => array_combine($compareUserList, $compareUserList),
178
            'currentUserUid' => $backendUser->user['uid'],
179
            'compareUserList' => !empty($compareUserList) ? $this->backendUserRepository->findByUidList($compareUserList) : '',
180
        ]);
181
182
        $this->addMainMenu('index');
183
        $buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar();
0 ignored issues
show
Bug introduced by
The method getDocHeaderComponent() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

183
        $buttonBar = $this->moduleTemplate->/** @scrutinizer ignore-call */ getDocHeaderComponent()->getButtonBar();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
184
        $addUserButton = $buttonBar->makeLinkButton()
185
            ->setIcon($this->iconFactory->getIcon('actions-add', Icon::SIZE_SMALL))
186
            ->setTitle(LocalizationUtility::translate('LLL:EXT:backend/Resources/Private/Language/locallang_layout.xlf:newRecordGeneral'))
187
            ->setHref($this->backendUriBuilder->buildUriFromRoute('record_edit', [
188
                'edit' => ['be_users' => [0 => 'new']],
189
                'returnUrl' => $this->request->getAttribute('normalizedParams')->getRequestUri()
190
            ]));
191
        $buttonBar->addButton($addUserButton);
192
        $shortcutButton = $buttonBar->makeShortcutButton()
193
            ->setRouteIdentifier('system_BeuserTxBeuser')
194
            ->setArguments(['tx_beuser_system_beusertxbeuser' => ['action' => 'index']])
195
            ->setDisplayName(LocalizationUtility::translate('backendUsers', 'beuser'));
196
        $buttonBar->addButton($shortcutButton, ButtonBar::BUTTON_POSITION_RIGHT);
197
198
        $this->pageRenderer->loadRequireJsModule('TYPO3/CMS/Backend/SwitchUser');
199
200
        $this->moduleTemplate->setContent($this->view->render());
201
        return $this->htmlResponse($this->moduleTemplate->renderContent());
202
    }
203
204
    /**
205
     * Views all currently logged in BackendUsers and their sessions
206
     */
207
    public function onlineAction(): ResponseInterface
208
    {
209
        $onlineUsersAndSessions = [];
210
        $onlineUsers = $this->backendUserRepository->findOnline();
211
        foreach ($onlineUsers as $onlineUser) {
212
            $onlineUsersAndSessions[] = [
213
                'backendUser' => $onlineUser,
214
                'sessions' => $this->backendUserSessionRepository->findByBackendUser($onlineUser)
215
            ];
216
        }
217
218
        $currentSessionId = $this->backendUserSessionRepository->getPersistedSessionIdentifier($this->getBackendUser());
219
220
        $this->view->assignMultiple([
221
            'onlineUsersAndSessions' => $onlineUsersAndSessions,
222
            'currentSessionId' => $currentSessionId,
223
        ]);
224
225
        $this->addMainMenu('online');
226
        $buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar();
227
        $shortcutButton = $buttonBar->makeShortcutButton()
228
            ->setRouteIdentifier('system_BeuserTxBeuser')
229
            ->setArguments(['tx_beuser_system_beusertxbeuser' => ['action' => 'online']])
230
            ->setDisplayName(LocalizationUtility::translate('onlineUsers', 'beuser'));
231
        $buttonBar->addButton($shortcutButton, ButtonBar::BUTTON_POSITION_RIGHT);
232
233
        $this->moduleTemplate->setContent($this->view->render());
234
        return $this->htmlResponse($this->moduleTemplate->renderContent());
235
    }
236
237
    public function showAction(int $uid = 0): ResponseInterface
238
    {
239
        $data = $this->userInformationService->getUserInformation($uid);
240
        $this->view->assign('data', $data);
241
242
        $this->addMainMenu('show');
243
        $buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar();
244
        $backButton = $buttonBar->makeLinkButton()
245
            ->setIcon($this->iconFactory->getIcon('actions-view-go-back', Icon::SIZE_SMALL))
246
            ->setTitle(LocalizationUtility::translate('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.goBack'))
247
            ->setHref($this->backendUriBuilder->buildUriFromRoute('system_BeuserTxBeuser'));
248
        $buttonBar->addButton($backButton);
249
        $editButton = $buttonBar->makeLinkButton()
250
            ->setIcon($this->iconFactory->getIcon('actions-open', Icon::SIZE_SMALL))
251
            ->setTitle(LocalizationUtility::translate('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.goBack'))
252
            ->setHref($this->backendUriBuilder->buildUriFromRoute('record_edit', [
253
                'edit' => ['be_users' => [$uid => 'edit']],
254
                'returnUrl' => $this->request->getAttribute('normalizedParams')->getRequestUri()
255
            ]));
256
        $buttonBar->addButton($editButton);
257
        $addUserButton = $buttonBar->makeLinkButton()
258
            ->setIcon($this->iconFactory->getIcon('actions-add', Icon::SIZE_SMALL))
259
            ->setTitle(LocalizationUtility::translate('LLL:EXT:backend/Resources/Private/Language/locallang_layout.xlf:newRecordGeneral'))
260
            ->setHref($this->backendUriBuilder->buildUriFromRoute('record_edit', [
261
                'edit' => ['be_users' => [0 => 'new']],
262
                'returnUrl' => $this->request->getAttribute('normalizedParams')->getRequestUri()
263
            ]));
264
        $buttonBar->addButton($addUserButton);
265
        $shortcutButton = $buttonBar->makeShortcutButton()
266
            ->setRouteIdentifier('system_BeuserTxBeuser')
267
            ->setArguments(['tx_beuser_system_beusertxbeuser' => ['action' => 'show', 'uid' => $uid]])
268
            ->setDisplayName(LocalizationUtility::translate('backendUser', 'beuser') . ': ' . (string)$data['user']['username']);
269
        $buttonBar->addButton($shortcutButton, ButtonBar::BUTTON_POSITION_RIGHT);
270
271
        $this->moduleTemplate->setContent($this->view->render());
272
        return $this->htmlResponse($this->moduleTemplate->renderContent());
273
    }
274
275
    /**
276
     * Compare backend users from demand
277
     */
278
    public function compareAction(): ResponseInterface
279
    {
280
        $compareUserList = $this->moduleData->getCompareUserList();
281
        if (empty($compareUserList)) {
282
            $this->redirect('index');
283
        }
284
285
        $compareData = [];
286
        foreach ($compareUserList as $uid) {
287
            if ($compareInformation = $this->userInformationService->getUserInformation($uid)) {
288
                $compareData[] = $compareInformation;
289
            }
290
        }
291
292
        $this->view->assignMultiple([
293
            'compareUserList' => $compareData,
294
            'onlineBackendUsers' => $this->getOnlineBackendUsers()
295
        ]);
296
297
        $this->addMainMenu('compare');
298
        $buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar();
299
        $backButton = $buttonBar->makeLinkButton()
300
            ->setIcon($this->iconFactory->getIcon('actions-view-go-back', Icon::SIZE_SMALL))
301
            ->setTitle(LocalizationUtility::translate('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.goBack'))
302
            ->setHref($this->backendUriBuilder->buildUriFromRoute('system_BeuserTxBeuser'));
303
        $buttonBar->addButton($backButton);
304
        $shortcutButton = $buttonBar->makeShortcutButton()
305
            ->setRouteIdentifier('system_BeuserTxBeuser')
306
            ->setArguments(['tx_beuser_system_beusertxbeuser' => ['action' => 'compare']])
307
            ->setDisplayName(LocalizationUtility::translate('compareUsers', 'beuser'));
308
        $buttonBar->addButton($shortcutButton, ButtonBar::BUTTON_POSITION_RIGHT);
309
310
        $this->moduleTemplate->setContent($this->view->render());
311
        return $this->htmlResponse($this->moduleTemplate->renderContent());
312
    }
313
314
    /**
315
     * Starts the password reset process for a selected user.
316
     *
317
     * @param int $user
318
     */
319
    public function initiatePasswordResetAction(int $user): ResponseInterface
320
    {
321
        $context = GeneralUtility::makeInstance(Context::class);
322
        /** @var BackendUser $user */
323
        $user = $this->backendUserRepository->findByUid($user);
0 ignored issues
show
Bug introduced by
$user of type TYPO3\CMS\Beuser\Domain\Model\BackendUser is incompatible with the type integer expected by parameter $uid of TYPO3\CMS\Extbase\Persis...Repository::findByUid(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

323
        $user = $this->backendUserRepository->findByUid(/** @scrutinizer ignore-type */ $user);
Loading history...
324
        if (!$user || !$user->isPasswordResetEnabled() || !$context->getAspect('backend.user')->isAdmin()) {
0 ignored issues
show
introduced by
$user is of type TYPO3\CMS\Beuser\Domain\Model\BackendUser, thus it always evaluated to true.
Loading history...
325
            // Add an error message
326
            $this->addFlashMessage(
327
                LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang.xlf:flashMessage.resetPassword.error.text', 'beuser') ?? '',
328
                LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang.xlf:flashMessage.resetPassword.error.title', 'beuser') ?? '',
329
                FlashMessage::ERROR
330
            );
331
        } else {
332
            GeneralUtility::makeInstance(PasswordReset::class)->initiateReset(
333
                $this->request,
334
                $context,
335
                $user->getEmail()
336
            );
337
            $this->addFlashMessage(
338
                LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang.xlf:flashMessage.resetPassword.success.text', 'beuser', [$user->getEmail()]) ?? '',
339
                LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang.xlf:flashMessage.resetPassword.success.title', 'beuser') ?? '',
340
                FlashMessage::OK
341
            );
342
        }
343
        return new ForwardResponse('index');
344
    }
345
346
    /**
347
     * Attaches one backend user to the compare list
348
     *
349
     * @param int $uid
350
     */
351
    public function addToCompareListAction($uid): ResponseInterface
352
    {
353
        $this->moduleData->attachUidCompareUser($uid);
354
        $this->getBackendUser()->pushModuleData('tx_beuser', $this->moduleData->forUc());
355
        return new ForwardResponse('index');
356
    }
357
358
    /**
359
     * Removes given backend user to the compare list
360
     *
361
     * @param int $uid
362
     * @param int $redirectToCompare
363
     */
364
    public function removeFromCompareListAction($uid, int $redirectToCompare = 0): void
365
    {
366
        $this->moduleData->detachUidCompareUser($uid);
367
        $this->getBackendUser()->pushModuleData('tx_beuser', $this->moduleData->forUc());
368
        if ($redirectToCompare) {
369
            $this->redirect('compare');
370
        } else {
371
            $this->redirect('index');
372
        }
373
    }
374
375
    /**
376
     * Removes all backend users from the compare list
377
     * @throws StopActionException
378
     */
379
    public function removeAllFromCompareListAction(): void
380
    {
381
        $this->moduleData->resetCompareUserList();
382
        $this->getBackendUser()->pushModuleData('tx_beuser', $this->moduleData->forUc());
383
        $this->redirect('index');
384
    }
385
386
    /**
387
     * Terminate BackendUser session and logout corresponding client
388
     * Redirects to onlineAction with message
389
     *
390
     * @param string $sessionId
391
     */
392
    protected function terminateBackendUserSessionAction($sessionId): ResponseInterface
393
    {
394
        // terminating value of persisted session ID
395
        $success = $this->backendUserSessionRepository->terminateSessionByIdentifier($sessionId);
396
        if ($success) {
397
            $this->addFlashMessage(LocalizationUtility::translate('LLL:EXT:beuser/Resources/Private/Language/locallang.xlf:terminateSessionSuccess', 'beuser') ?? '');
398
        }
399
        return new ForwardResponse('online');
400
    }
401
402
    /**
403
     * Displays all BackendUserGroups
404
     *
405
     * @param int $currentPage
406
     * @return ResponseInterface
407
     */
408
    public function groupsAction(int $currentPage = 1): ResponseInterface
409
    {
410
        /** @var QueryResultInterface $backendUsers */
411
        $backendUsers = $this->backendUserGroupRepository->findAll();
412
        $paginator = new QueryResultPaginator($backendUsers, $currentPage, 50);
413
        $pagination = new SimplePagination($paginator);
414
        $compareGroupUidList = array_keys($this->getBackendUser()->uc['beuser']['compareGroupUidList'] ?? []);
415
        $this->view->assignMultiple(
416
            [
417
                'paginator' => $paginator,
418
                'pagination' => $pagination,
419
                'totalAmountOfBackendUserGroups' => $backendUsers->count(),
420
                'compareGroupUidList' => array_map(static function ($value) { // uid as key and force value to 1
421
                    return 1;
422
                }, array_flip($compareGroupUidList)),
423
                'compareGroupList' => !empty($compareGroupUidList) ? $this->backendUserGroupRepository->findByUidList($compareGroupUidList) : [],
424
            ]
425
        );
426
427
        $this->addMainMenu('groups');
428
        $buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar();
429
        $addGroupButton = $buttonBar->makeLinkButton()
430
            ->setIcon($this->iconFactory->getIcon('actions-add', Icon::SIZE_SMALL))
431
            ->setTitle(LocalizationUtility::translate('LLL:EXT:backend/Resources/Private/Language/locallang_layout.xlf:newRecordGeneral'))
432
            ->setHref($this->backendUriBuilder->buildUriFromRoute('record_edit', [
433
                'edit' => ['be_groups' => [0 => 'new']],
434
                'returnUrl' => $this->request->getAttribute('normalizedParams')->getRequestUri()
435
            ]));
436
        $buttonBar->addButton($addGroupButton);
437
        $shortcutButton = $buttonBar->makeShortcutButton()
438
            ->setRouteIdentifier('system_BeuserTxBeuser')
439
            ->setArguments(['tx_beuser_system_beusertxbeuser' => ['action' => 'groups']])
440
            ->setDisplayName(LocalizationUtility::translate('backendUserGroupsMenu', 'beuser'));
441
        $buttonBar->addButton($shortcutButton, ButtonBar::BUTTON_POSITION_RIGHT);
442
443
        $this->moduleTemplate->setContent($this->view->render());
444
        return $this->htmlResponse($this->moduleTemplate->renderContent());
445
    }
446
447
    public function compareGroupsAction(): ResponseInterface
448
    {
449
        $compareGroupUidList = array_keys($this->getBackendUser()->uc['beuser']['compareGroupUidList'] ?? []);
450
451
        $compareData = [];
452
        foreach ($compareGroupUidList as $uid) {
453
            if ($compareInformation = $this->userInformationService->getGroupInformation($uid)) {
454
                $compareData[] = $compareInformation;
455
            }
456
        }
457
        if (empty($compareData)) {
458
            $this->redirect('groups');
459
        }
460
461
        $this->view->assign('compareGroupList', $compareData);
462
463
        $this->addMainMenu('compareGroups');
464
        $buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar();
465
        $backButton = $buttonBar->makeLinkButton()
466
            ->setIcon($this->iconFactory->getIcon('actions-view-go-back', Icon::SIZE_SMALL))
467
            ->setTitle(LocalizationUtility::translate('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.goBack'))
468
            ->setHref($this->uriBuilder->uriFor('groups'));
469
        $buttonBar->addButton($backButton);
470
        $shortcutButton = $buttonBar->makeShortcutButton()
471
            ->setRouteIdentifier('system_BeuserTxBeuser')
472
            ->setArguments(['tx_beuser_system_beusertxbeuser' => ['action' => 'compareGroups']])
473
            ->setDisplayName(LocalizationUtility::translate('compareBackendUsersGroups', 'beuser'));
474
        $buttonBar->addButton($shortcutButton, ButtonBar::BUTTON_POSITION_RIGHT);
475
476
        $this->moduleTemplate->setContent($this->view->render());
477
        return $this->htmlResponse($this->moduleTemplate->renderContent());
478
    }
479
480
    /**
481
     * Attaches one backend user group to the compare list
482
     *
483
     * @param int $uid
484
     */
485
    public function addGroupToCompareListAction(int $uid): void
486
    {
487
        $backendUser = $this->getBackendUser();
488
        $list = $backendUser->uc['beuser']['compareGroupUidList'] ?? [];
489
        $list[$uid] = true;
490
        $backendUser->uc['beuser']['compareGroupUidList'] = $list;
491
        $backendUser->writeUC();
492
        $this->redirect('groups');
493
    }
494
495
    /**
496
     * Removes given backend user group to the compare list
497
     *
498
     * @param int $uid
499
     * @param int $redirectToCompare
500
     */
501
    public function removeGroupFromCompareListAction(int $uid, int $redirectToCompare = 0): void
502
    {
503
        $backendUser = $this->getBackendUser();
504
        $list = $backendUser->uc['beuser']['compareGroupUidList'] ?? [];
505
        unset($list[$uid]);
506
        $backendUser->uc['beuser']['compareGroupUidList'] = $list;
507
        $backendUser->writeUC();
508
509
        if ($redirectToCompare) {
510
            $this->redirect('compareGroups');
511
        } else {
512
            $this->redirect('groups');
513
        }
514
    }
515
516
    /**
517
     * Removes all backend user groups from the compare list
518
     */
519
    public function removeAllGroupsFromCompareListAction(): void
520
    {
521
        $backendUser = $this->getBackendUser();
522
        $backendUser->uc['beuser']['compareGroupUidList'] = [];
523
        $backendUser->writeUC();
524
        $this->redirect('groups');
525
    }
526
527
    /**
528
     * Create an array with the uids of online users as the keys
529
     * [
530
     *   1 => true,
531
     *   5 => true
532
     * ]
533
     * @return array
534
     */
535
    protected function getOnlineBackendUsers(): array
536
    {
537
        $onlineUsers = $this->backendUserSessionRepository->findAllActive();
538
        $onlineBackendUsers = [];
539
        foreach ($onlineUsers as $onlineUser) {
540
            $onlineBackendUsers[$onlineUser['ses_userid']] = true;
541
        }
542
        return $onlineBackendUsers;
543
    }
544
545
    /**
546
     * Doc header main drop down
547
     *
548
     * @param string $currentAction
549
     */
550
    protected function addMainMenu(string $currentAction): void
551
    {
552
        $this->uriBuilder->setRequest($this->request);
553
        $menu = $this->moduleTemplate->getDocHeaderComponent()->getMenuRegistry()->makeMenu();
554
        $menu->setIdentifier('BackendUserModuleMenu');
555
        $menu->addMenuItem(
556
            $menu->makeMenuItem()
557
            ->setTitle(LocalizationUtility::translate('backendUsers', 'beuser'))
558
            ->setHref($this->uriBuilder->uriFor('index'))
559
            ->setActive($currentAction === 'index')
560
        );
561
        if ($currentAction === 'show') {
562
            $menu->addMenuItem(
563
                $menu->makeMenuItem()
564
                ->setTitle(LocalizationUtility::translate('backendUserDetails', 'beuser'))
565
                ->setHref($this->uriBuilder->uriFor('show'))
566
                ->setActive(true)
567
            );
568
        }
569
        if ($currentAction === 'compare') {
570
            $menu->addMenuItem(
571
                $menu->makeMenuItem()
572
                ->setTitle(LocalizationUtility::translate('compareBackendUsers', 'beuser'))
573
                ->setHref($this->uriBuilder->uriFor('index'))
574
                ->setActive(true)
575
            );
576
        }
577
        $menu->addMenuItem(
578
            $menu->makeMenuItem()
579
            ->setTitle(LocalizationUtility::translate('backendUserGroupsMenu', 'beuser'))
580
            ->setHref($this->uriBuilder->uriFor('groups'))
581
            ->setActive($currentAction === 'groups')
582
        );
583
        if ($currentAction === 'compareGroups') {
584
            $menu->addMenuItem(
585
                $menu->makeMenuItem()
586
                ->setTitle(LocalizationUtility::translate('compareBackendUsersGroups', 'beuser'))
587
                ->setHref($this->uriBuilder->uriFor('compareGroups'))
588
                ->setActive(true)
589
            );
590
        }
591
        $menu->addMenuItem(
592
            $menu->makeMenuItem()
593
            ->setTitle(LocalizationUtility::translate('onlineUsers', 'beuser'))
594
            ->setHref($this->uriBuilder->uriFor('online'))
595
            ->setActive($currentAction === 'online')
596
        );
597
        $this->moduleTemplate->getDocHeaderComponent()->getMenuRegistry()->addMenu($menu);
598
    }
599
600
    /**
601
     * @return BackendUserAuthentication
602
     */
603
    protected function getBackendUser(): BackendUserAuthentication
604
    {
605
        return $GLOBALS['BE_USER'];
606
    }
607
}
608