Passed
Push — master ( 25a926...39145a )
by
unknown
20:30
created

MfaInfoElement::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the TYPO3 CMS project.
7
 *
8
 * It is free software; you can redistribute it and/or modify it under
9
 * the terms of the GNU General Public License, either version 2
10
 * of the License, or any later version.
11
 *
12
 * For the full copyright and license information, please read the
13
 * LICENSE.txt file that was distributed with this source code.
14
 *
15
 * The TYPO3 project - inspiring people to share!
16
 */
17
18
namespace TYPO3\CMS\Backend\Form\Element;
19
20
use TYPO3\CMS\Backend\Form\NodeFactory;
21
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
22
use TYPO3\CMS\Core\Authentication\Mfa\MfaProviderPropertyManager;
23
use TYPO3\CMS\Core\Authentication\Mfa\MfaProviderRegistry;
24
use TYPO3\CMS\Core\Imaging\Icon;
25
use TYPO3\CMS\Core\Utility\GeneralUtility;
26
use TYPO3\CMS\Core\Utility\StringUtility;
27
use TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication;
28
29
/**
30
 * Renders an element, displaying MFA related information and providing
31
 * interactions like deactivation of active providers and MFA in general.
32
 *
33
 * @internal
34
 */
35
class MfaInfoElement extends AbstractFormElement
36
{
37
    private const ALLOWED_TABLES = ['be_users', 'fe_users'];
38
39
    protected MfaProviderRegistry $mfaProviderRegistry;
40
41
    public function __construct(NodeFactory $nodeFactory, array $data)
42
    {
43
        parent::__construct($nodeFactory, $data);
44
        $this->mfaProviderRegistry = GeneralUtility::makeInstance(MfaProviderRegistry::class);
45
    }
46
47
    public function render(): array
48
    {
49
        $resultArray = $this->initializeResultArray();
50
        $currentBackendUser = $this->getBackendUser();
51
        $tableName = $this->data['tableName'];
52
53
        // This renderType only works for user tables: be_users, fe_users
54
        if (!in_array($tableName, self::ALLOWED_TABLES, true)) {
55
            return $resultArray;
56
        }
57
58
        // Initialize a user based on the current table name
59
        $targetUser = $tableName === 'be_users'
60
            ? GeneralUtility::makeInstance(BackendUserAuthentication::class)
61
            : GeneralUtility::makeInstance(FrontendUserAuthentication::class);
62
63
        $userId = (int)($this->data['databaseRow'][$targetUser->userid_column] ?? 0);
64
        $targetUser->enablecolumns = ['deleted' => true];
65
        $targetUser->setBeUserByUid($userId);
66
67
        $isDeactivationAllowed = true;
68
        // System maintainer checks are only required for backend users
69
        if ($targetUser instanceof BackendUserAuthentication) {
70
            $systemMaintainer = array_map('intval', $GLOBALS['TYPO3_CONF_VARS']['SYS']['systemMaintainers'] ?? []);
71
            $isCurrentBackendUserSystemMaintainer = in_array((int)$currentBackendUser->user[$currentBackendUser->userid_column], $systemMaintainer, true);
72
            $isTargetUserSystemMaintainer = in_array((int)$targetUser->user[$targetUser->userid_column], $systemMaintainer, true);
73
            // Providers from system maintainers can only be deactivated by system maintainers
74
            if ($isTargetUserSystemMaintainer && !$isCurrentBackendUserSystemMaintainer) {
75
                $isDeactivationAllowed = false;
76
            }
77
        }
78
79
        // Fetch providers from the mfa field
80
        $mfaProviders = json_decode($this->data['parameterArray']['itemFormElValue'] ?? '', true) ?? [];
81
82
        // Initialize variables
83
        $html = $childHtml = $activeProviders = $lockedProviders = [];
84
        $lang = $this->getLanguageService();
85
        $enabledLabel = htmlspecialchars($lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.mfa.enabled'));
86
        $disabledLabel = htmlspecialchars($lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.mfa.disabled'));
87
        $status = '<span class="label label-danger label-space-right t3js-mfa-status-label" data-alternative-label="' . $enabledLabel . '">' . $disabledLabel . '</span>';
88
89
        // Unset invalid providers
90
        foreach ($mfaProviders as $identifier => $providerSettings) {
91
            if (!$this->mfaProviderRegistry->hasProvider($identifier)) {
92
                unset($mfaProviders[$identifier]);
93
            }
94
        }
95
96
        if ($mfaProviders !== []) {
97
            // Check if remaining providers are active and/or locked for the user
98
            foreach ($mfaProviders as $identifier => $providerSettings) {
99
                $provider = $this->mfaProviderRegistry->getProvider($identifier);
100
                $propertyManager = MfaProviderPropertyManager::create($provider, $targetUser);
101
                if (!$provider->isActive($propertyManager)) {
102
                    continue;
103
                }
104
                $activeProviders[$identifier] = $provider;
105
                if ($provider->isLocked($propertyManager)) {
106
                    $lockedProviders[] = $identifier;
107
                }
108
            }
109
110
            if ($activeProviders !== []) {
111
                // Change status label to MFA being enabled
112
                $status = '<span class="label label-success label-space-right t3js-mfa-status-label"' . ' data-alternative-label="' . $disabledLabel . '">' . $enabledLabel . '</span>';
113
114
                // Add providers list
115
                $childHtml[] = '<ul class="list-group t3js-mfa-active-providers-list">';
116
                foreach ($activeProviders as $identifier => $activeProvider) {
117
                    $childHtml[] = '<li class="list-group-item" id="provider-' . htmlspecialchars($identifier) . '" style="line-height: 2.1em;">';
118
                    $childHtml[] =  $this->iconFactory->getIcon($activeProvider->getIconIdentifier(), Icon::SIZE_SMALL);
119
                    $childHtml[] =  htmlspecialchars($lang->sL($activeProvider->getTitle()));
120
                    if (in_array($identifier, $lockedProviders, true)) {
121
                        $childHtml[] = '<span class="label label-danger">' . htmlspecialchars($lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.locked')) . '</span>';
122
                    } else {
123
                        $childHtml[] = '<span class="label label-success">' . htmlspecialchars($lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.active')) . '</span>';
124
                    }
125
                    if ($isDeactivationAllowed) {
126
                        $childHtml[] = '<button type="button"';
127
                        $childHtml[] =  ' class="btn btn-default btn-sm pull-right t3js-deactivate-provider-button"';
128
                        $childHtml[] =  ' data-confirmation-title="' . htmlspecialchars(sprintf($lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:buttons.deactivateMfaProvider'), $lang->sL($activeProvider->getTitle()))) . '"';
129
                        $childHtml[] =  ' data-confirmation-content="' . htmlspecialchars(sprintf($lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:buttons.deactivateMfaProvider.confirmation.text'), $lang->sL($activeProvider->getTitle()))) . '"';
130
                        $childHtml[] =  ' data-confirmation-cancel-text="' . htmlspecialchars($lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.cancel')) . '"';
131
                        $childHtml[] =  ' data-confirmation-deactivate-text="' . htmlspecialchars($lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.deactivate')) . '"';
132
                        $childHtml[] =  ' data-provider="' . htmlspecialchars($identifier) . '"';
133
                        $childHtml[] =  ' title="' . htmlspecialchars(sprintf($lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:buttons.deactivateMfaProvider'), $lang->sL($activeProvider->getTitle()))) . '"';
134
                        $childHtml[] =  '>';
135
                        $childHtml[] =      $this->iconFactory->getIcon('actions-delete', Icon::SIZE_SMALL)->render('inline');
136
                        $childHtml[] = '</button>';
137
                    }
138
                    $childHtml[] = '</li>';
139
                }
140
                $childHtml[] = '</ul>';
141
            }
142
        }
143
144
        $fieldId = 't3js-form-field-mfa-id' . StringUtility::getUniqueId('-');
145
146
        $html[] = '<div class="formengine-field-item t3js-formengine-field-item" id="' . htmlspecialchars($fieldId) . '">';
147
        $html[] =   '<div class="form-control-wrap" style="max-width: ' . (int)$this->formMaxWidth($this->defaultInputWidth) . 'px">';
148
        $html[] =       '<div class="form-wizards-wrap">';
149
        $html[] =           '<div class="form-wizards-element">';
150
        $html[] =               implode(PHP_EOL, $childHtml);
151
        if ($isDeactivationAllowed) {
152
            $html[] = '<div class="form-wizards-items-bottom">';
153
            $html[] =   '<div class="help-block">';
154
            $html[] =       '<button type="button"';
155
            $html[] =           ' class="t3js-deactivate-mfa-button btn btn-danger ' . ($activeProviders === [] ? 'disabled" disabled="disabled' : '') . '"';
156
            $html[] =           ' data-confirmation-title="' . htmlspecialchars($lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:buttons.deactivateMfa')) . '"';
157
            $html[] =           ' data-confirmation-content="' . htmlspecialchars($lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:buttons.deactivateMfa.confirmation.text')) . '"';
158
            $html[] =           ' data-confirmation-cancel-text="' . htmlspecialchars($lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.cancel')) . '"';
159
            $html[] =           ' data-confirmation-deactivate-text="' . htmlspecialchars($lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.deactivate')) . '"';
160
            $html[] =       '>';
161
            $html[] =           $this->iconFactory->getIcon('actions-toggle-off', Icon::SIZE_SMALL)->render('inline');
162
            $html[] =           htmlspecialchars($lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:buttons.deactivateMfa'));
163
            $html[] =       '</button>';
164
            $html[] =   '</div>';
165
            $html[] = '</div>';
166
        }
167
        $html[] =           '</div>';
168
        $html[] =       '</div>';
169
        $html[] =   '</div>';
170
        $html[] = '</div>';
171
172
        $resultArray['requireJsModules'][] = ['TYPO3/CMS/Backend/FormEngine/Element/MfaInfoElement' => '
173
            function(MfaInfoElement) {
174
                new MfaInfoElement(' . GeneralUtility::quoteJSvalue('#' . $fieldId) . ', ' . json_encode(['userId' => $userId, 'tableName' => $tableName]) . ');
175
            }'
176
        ];
177
178
        $resultArray['html'] = $status . implode(PHP_EOL, $html);
179
        return $resultArray;
180
    }
181
}
182