Passed
Push — master ( da9c6d...70f855 )
by
unknown
17:46
created

getBackendUserAuthentication()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 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\ViewHelpers;
17
18
use TYPO3\CMS\Beuser\Domain\Model\BackendUser;
19
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
20
use TYPO3\CMS\Core\Imaging\Icon;
21
use TYPO3\CMS\Core\Imaging\IconFactory;
22
use TYPO3\CMS\Core\Utility\GeneralUtility;
23
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
24
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
25
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
26
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
27
28
/**
29
 * Displays 'SwitchUser' button to change current backend user to target backend user
30
 * @internal
31
 */
32
class SwitchUserViewHelper extends AbstractViewHelper
33
{
34
    use CompileWithRenderStatic;
35
36
    /**
37
     * As this ViewHelper renders HTML, the output must not be escaped.
38
     *
39
     * @var bool
40
     */
41
    protected $escapeOutput = false;
42
43
    /**
44
     * Initializes the arguments
45
     */
46
    public function initializeArguments()
47
    {
48
        $this->registerArgument('backendUser', BackendUser::class, 'Target backendUser to switch active session to', true);
49
    }
50
51
    /**
52
     * Render link with sprite icon to change current backend user to target
53
     *
54
     * @param array $arguments
55
     * @param \Closure $renderChildrenClosure
56
     * @param RenderingContextInterface $renderingContext
57
     *
58
     * @return string
59
     */
60
    public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
61
    {
62
        $targetUser = $arguments['backendUser'];
63
        $currentUser = self::getBackendUserAuthentication();
64
        $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
65
66
        if ((int)$targetUser->getUid() === (int)($currentUser->user[$currentUser->userid_column] ?? 0)
67
            || !$targetUser->isActive()
68
            || !$currentUser->isAdmin()
69
            || $currentUser->getOriginalUserIdWhenInSwitchUserMode() !== null
70
        ) {
71
            return '<span class="btn btn-default disabled">' . $iconFactory->getIcon('empty-empty', Icon::SIZE_SMALL)->render() . '</span>';
72
        }
73
74
        return '
75
            <typo3-backend-switch-user targetUser="' . htmlspecialchars((string)$targetUser->getUid()) . '">
76
                <button type="button" class="btn btn-default" title="' . htmlspecialchars(LocalizationUtility::translate('switchBackMode', 'beuser') ?? '') . '">'
77
                    . $iconFactory->getIcon('actions-system-backend-user-switch', Icon::SIZE_SMALL)->render() .
78
                '</button>
79
            </typo3-switch-user-button>';
80
    }
81
82
    protected static function getBackendUserAuthentication(): BackendUserAuthentication
83
    {
84
        return $GLOBALS['BE_USER'];
85
    }
86
}
87