Conditions | 18 |
Paths | 163 |
Total Lines | 133 |
Code Lines | 90 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
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 | } |
||
182 |