Passed
Push — master ( 7aad9f...2388af )
by
unknown
17:10
created

SetupModuleController::getJavaScript()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 8
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\Setup\Controller;
17
18
use Psr\EventDispatcher\EventDispatcherInterface;
19
use Psr\Http\Message\ResponseInterface;
20
use Psr\Http\Message\ServerRequestInterface;
21
use TYPO3\CMS\Backend\Backend\Avatar\DefaultAvatarProvider;
22
use TYPO3\CMS\Backend\Module\ModuleLoader;
23
use TYPO3\CMS\Backend\Routing\UriBuilder;
24
use TYPO3\CMS\Backend\Template\ModuleTemplate;
25
use TYPO3\CMS\Backend\Utility\BackendUtility;
26
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
27
use TYPO3\CMS\Core\Authentication\Mfa\MfaProviderRegistry;
28
use TYPO3\CMS\Core\Core\Environment;
29
use TYPO3\CMS\Core\Crypto\PasswordHashing\InvalidPasswordHashException;
30
use TYPO3\CMS\Core\Crypto\PasswordHashing\PasswordHashFactory;
31
use TYPO3\CMS\Core\Database\ConnectionPool;
32
use TYPO3\CMS\Core\DataHandling\DataHandler;
33
use TYPO3\CMS\Core\FormProtection\FormProtectionFactory;
34
use TYPO3\CMS\Core\Http\HtmlResponse;
35
use TYPO3\CMS\Core\Imaging\Icon;
36
use TYPO3\CMS\Core\Imaging\IconFactory;
37
use TYPO3\CMS\Core\Localization\LanguageService;
38
use TYPO3\CMS\Core\Messaging\FlashMessage;
39
use TYPO3\CMS\Core\Messaging\FlashMessageService;
40
use TYPO3\CMS\Core\Page\PageRenderer;
41
use TYPO3\CMS\Core\Resource\Exception\FileDoesNotExistException;
42
use TYPO3\CMS\Core\Resource\ResourceFactory;
43
use TYPO3\CMS\Core\SysLog\Action\Setting as SystemLogSettingAction;
44
use TYPO3\CMS\Core\SysLog\Error as SystemLogErrorClassification;
45
use TYPO3\CMS\Core\SysLog\Type as SystemLogType;
46
use TYPO3\CMS\Core\Utility\GeneralUtility;
47
use TYPO3\CMS\Setup\Event\AddJavaScriptModulesEvent;
48
49
/**
50
 * Script class for the Setup module
51
 *
52
 * @internal This is a specific Backend Controller implementation and is not considered part of the Public TYPO3 API.
53
 */
54
class SetupModuleController
55
{
56
57
    /**
58
     * Flag if password has not been updated
59
     */
60
    const PASSWORD_NOT_UPDATED = 0;
61
62
    /**
63
     * Flag if password has been updated
64
     */
65
    const PASSWORD_UPDATED = 1;
66
67
    /**
68
     * Flag if both new passwords do not match
69
     */
70
    const PASSWORD_NOT_THE_SAME = 2;
71
72
    /**
73
     * Flag if the current password given was not identical to the real
74
     * current password
75
     */
76
    const PASSWORD_OLD_WRONG = 3;
77
78
    /**
79
     * @var string
80
     */
81
    protected $content;
82
83
    /**
84
     * @var array
85
     */
86
    protected $overrideConf;
87
88
    /**
89
     * @var bool
90
     */
91
    protected $languageUpdate;
92
93
    /**
94
     * @var bool
95
     */
96
    protected $pagetreeNeedsRefresh = false;
97
98
    /**
99
     * @var array
100
     */
101
    protected $tsFieldConf;
102
103
    /**
104
     * @var bool
105
     */
106
    protected $saveData = false;
107
108
    /**
109
     * @var int
110
     */
111
    protected $passwordIsUpdated = self::PASSWORD_NOT_UPDATED;
112
113
    /**
114
     * @var bool
115
     */
116
    protected $passwordIsSubmitted = false;
117
118
    /**
119
     * @var bool
120
     */
121
    protected $setupIsUpdated = false;
122
123
    /**
124
     * @var bool
125
     */
126
    protected $settingsAreResetToDefault = false;
127
128
    /**
129
     * Form protection instance
130
     *
131
     * @var \TYPO3\CMS\Core\FormProtection\BackendFormProtection
132
     */
133
    protected $formProtection;
134
135
    /**
136
     * The name of the module
137
     *
138
     * @var string
139
     */
140
    protected $moduleName = 'user_setup';
141
142
    /**
143
     * ModuleTemplate object
144
     *
145
     * @var ModuleTemplate
146
     */
147
    protected $moduleTemplate;
148
149
    /**
150
     * @var EventDispatcherInterface
151
     */
152
    protected $eventDispatcher;
153
154
    protected MfaProviderRegistry $mfaProviderRegistry;
155
156
    /**
157
     * Instantiate the form protection before a simulated user is initialized.
158
     *
159
     * @param EventDispatcherInterface $eventDispatcher
160
     */
161
    public function __construct(EventDispatcherInterface $eventDispatcher, MfaProviderRegistry $mfaProviderRegistry)
162
    {
163
        $this->eventDispatcher = $eventDispatcher;
164
        $this->mfaProviderRegistry = $mfaProviderRegistry;
165
        $this->moduleTemplate = GeneralUtility::makeInstance(ModuleTemplate::class);
166
        $this->formProtection = FormProtectionFactory::get();
167
        $pageRenderer = $this->moduleTemplate->getPageRenderer();
168
        $pageRenderer->loadRequireJsModule('TYPO3/CMS/Backend/Modal');
169
        $pageRenderer->loadRequireJsModule('TYPO3/CMS/Backend/FormEngine');
170
        $pageRenderer->loadRequireJsModule('TYPO3/CMS/Setup/SetupModule');
171
        $this->processAdditionalJavaScriptModules($pageRenderer);
172
        $pageRenderer->addInlineSetting('FormEngine', 'formName', 'editform');
173
        $pageRenderer->addInlineLanguageLabelArray([
174
            'FormEngine.remainingCharacters' => $this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.remainingCharacters'),
175
        ]);
176
    }
177
178
    protected function processAdditionalJavaScriptModules(PageRenderer $pageRenderer): void
179
    {
180
        $event = new AddJavaScriptModulesEvent();
181
        /** @var AddJavaScriptModulesEvent $event */
182
        $event = $this->eventDispatcher->dispatch($event);
183
        foreach ($event->getModules() as $moduleName) {
184
            $pageRenderer->loadRequireJsModule($moduleName);
185
        }
186
    }
187
188
    /**
189
     * Initializes the module for display of the settings form.
190
     */
191
    protected function initialize()
192
    {
193
        $this->getLanguageService()->includeLLFile('EXT:setup/Resources/Private/Language/locallang.xlf');
194
        $this->moduleTemplate->setTitle($this->getLanguageService()->getLL('UserSettings'));
195
        // Getting the 'override' values as set might be set in User TSconfig
196
        $this->overrideConf = $this->getBackendUser()->getTSConfig()['setup.']['override.'] ?? null;
197
        // Getting the disabled fields might be set in User TSconfig (eg setup.fields.password.disabled=1)
198
        $this->tsFieldConf = $this->getBackendUser()->getTSConfig()['setup.']['fields.'] ?? null;
199
        // id password is disabled, disable repeat of password too (password2)
200
        if ($this->tsFieldConf['password.']['disabled'] ?? false) {
201
            $this->tsFieldConf['password2.']['disabled'] = 1;
202
            $this->tsFieldConf['passwordCurrent.']['disabled'] = 1;
203
        }
204
    }
205
206
    /**
207
     * If settings are submitted to _POST[DATA], store them
208
     * NOTICE: This method is called before the \TYPO3\CMS\Backend\Template\ModuleTemplate
209
     * is included. See bottom of document.
210
     *
211
     * @param array $postData parsed body of the request
212
     */
213
    protected function storeIncomingData(array $postData)
214
    {
215
        // First check if something is submitted in the data-array from POST vars
216
        $d = $postData['data'] ?? null;
217
        $columns = $GLOBALS['TYPO3_USER_SETTINGS']['columns'];
218
        $backendUser = $this->getBackendUser();
219
        $beUserId = $backendUser->user['uid'];
220
        $storeRec = [];
221
        $fieldList = $this->getFieldsFromShowItem();
222
        if (is_array($d) && $this->formProtection->validateToken((string)($postData['formToken'] ?? ''), 'BE user setup', 'edit')) {
223
            // UC hashed before applying changes
224
            $save_before = md5(serialize($backendUser->uc));
225
            // PUT SETTINGS into the ->uc array:
226
            // Reload left frame when switching BE language
227
            if (isset($d['be_users']['lang']) && $d['be_users']['lang'] !== $backendUser->user['lang']) {
228
                $this->languageUpdate = true;
229
            }
230
            // Reload pagetree if the title length is changed
231
            if (isset($d['titleLen']) && $d['titleLen'] !== $backendUser->uc['titleLen']) {
232
                $this->pagetreeNeedsRefresh = true;
233
            }
234
            if ($d['setValuesToDefault']) {
235
                // If every value should be default
236
                $backendUser->resetUC();
237
                $this->settingsAreResetToDefault = true;
238
            } elseif ($d['save']) {
239
                // Save all submitted values if they are no array (arrays are with table=be_users) and exists in $GLOBALS['TYPO3_USER_SETTINGS'][columns]
240
                foreach ($columns as $field => $config) {
241
                    if (!in_array($field, $fieldList, true)) {
242
                        continue;
243
                    }
244
                    if ($config['table']) {
245
                        if ($config['table'] === 'be_users' && !in_array($field, ['password', 'password2', 'passwordCurrent', 'email', 'realName', 'admin', 'avatar'], true)) {
246
                            if (!isset($config['access']) || $this->checkAccess($config) && $backendUser->user[$field] !== $d['be_users'][$field]) {
247
                                if ($config['type'] === 'check') {
248
                                    $fieldValue = isset($d['be_users'][$field]) ? 1 : 0;
249
                                } else {
250
                                    $fieldValue = $d['be_users'][$field];
251
                                }
252
                                $storeRec['be_users'][$beUserId][$field] = $fieldValue;
253
                                $backendUser->user[$field] = $fieldValue;
254
                            }
255
                        }
256
                    }
257
                    if ($config['type'] === 'check') {
258
                        $backendUser->uc[$field] = isset($d[$field]) ? 1 : 0;
259
                    } else {
260
                        $backendUser->uc[$field] = htmlspecialchars($d[$field]);
261
                    }
262
                }
263
                // Personal data for the users be_user-record (email, name, password...)
264
                // If email and name is changed, set it in the users record:
265
                $be_user_data = $d['be_users'];
266
                // Possibility to modify the transmitted values. Useful to do transformations, like RSA password decryption
267
                foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/setup/mod/index.php']['modifyUserDataBeforeSave'] ?? [] as $function) {
268
                    $params = ['be_user_data' => &$be_user_data];
269
                    GeneralUtility::callUserFunction($function, $params, $this);
270
                }
271
                $this->passwordIsSubmitted = (string)$be_user_data['password'] !== '';
272
                $passwordIsConfirmed = $this->passwordIsSubmitted && $be_user_data['password'] === $be_user_data['password2'];
273
                // Update the real name:
274
                if ($be_user_data['realName'] !== $backendUser->user['realName']) {
275
                    $backendUser->user['realName'] = ($storeRec['be_users'][$beUserId]['realName'] = substr($be_user_data['realName'], 0, 80));
276
                }
277
                // Update the email address:
278
                if ($be_user_data['email'] !== $backendUser->user['email']) {
279
                    $backendUser->user['email'] = ($storeRec['be_users'][$beUserId]['email'] = substr($be_user_data['email'], 0, 80));
280
                }
281
                // Update the password:
282
                if ($passwordIsConfirmed) {
283
                    if ($backendUser->isAdmin()) {
284
                        $passwordOk = true;
285
                    } else {
286
                        $currentPasswordHashed = $backendUser->user['password'];
287
                        $passwordOk = false;
288
                        $saltFactory = GeneralUtility::makeInstance(PasswordHashFactory::class);
289
                        try {
290
                            $hashInstance = $saltFactory->get($currentPasswordHashed, 'BE');
291
                            $passwordOk = $hashInstance->checkPassword($be_user_data['passwordCurrent'], $currentPasswordHashed);
292
                        } catch (InvalidPasswordHashException $e) {
293
                            // Could not find hash class responsible for existing password. This is a
294
                            // misconfiguration and user can not change its password.
295
                        }
296
                    }
297
                    if ($passwordOk) {
298
                        $this->passwordIsUpdated = self::PASSWORD_UPDATED;
299
                        $storeRec['be_users'][$beUserId]['password'] = $be_user_data['password'];
300
                    } else {
301
                        $this->passwordIsUpdated = self::PASSWORD_OLD_WRONG;
302
                    }
303
                } else {
304
                    $this->passwordIsUpdated = self::PASSWORD_NOT_THE_SAME;
305
                }
306
307
                $this->setAvatarFileUid($beUserId, $be_user_data['avatar'], $storeRec);
0 ignored issues
show
Bug introduced by
The method setAvatarFileUid() 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

307
                $this->/** @scrutinizer ignore-call */ 
308
                       setAvatarFileUid($beUserId, $be_user_data['avatar'], $storeRec);

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...
308
309
                $this->saveData = true;
310
            }
311
            // Inserts the overriding values.
312
            $backendUser->overrideUC();
313
            $save_after = md5(serialize($backendUser->uc));
314
            // If something in the uc-array of the user has changed, we save the array...
315
            if ($save_before != $save_after) {
316
                $backendUser->writeUC($backendUser->uc);
317
                $backendUser->writelog(SystemLogType::SETTING, SystemLogSettingAction::CHANGE, SystemLogErrorClassification::MESSAGE, 1, 'Personal settings changed', []);
318
                $this->setupIsUpdated = true;
319
            }
320
            // Persist data if something has changed:
321
            if (!empty($storeRec) && $this->saveData) {
322
                // Set user to admin to circumvent DataHandler restrictions.
323
                // Not using isAdmin() to fetch the original value, just in case it has been boolean casted.
324
                $savedUserAdminState = $backendUser->user['admin'];
325
                $backendUser->user['admin'] = true;
326
                // Make dedicated instance of TCE for storing the changes.
327
                $dataHandler = GeneralUtility::makeInstance(DataHandler::class);
328
                $dataHandler->start($storeRec, [], $backendUser);
329
                // This is to make sure that the users record can be updated even if in another workspace. This is tolerated.
330
                $dataHandler->bypassWorkspaceRestrictions = true;
331
                $dataHandler->process_datamap();
332
                // reset the user record admin flag to previous value, just in case it gets used any further.
333
                $backendUser->user['admin'] = $savedUserAdminState;
334
                if ($this->passwordIsUpdated === self::PASSWORD_NOT_UPDATED || count($storeRec['be_users'][$beUserId]) > 1) {
335
                    $this->setupIsUpdated = true;
336
                }
337
                BackendUtility::setUpdateSignal('updateTopbar');
338
            }
339
        }
340
    }
341
342
    /**
343
     * Injects the request object, checks if data should be saved, and prepares a HTML page
344
     *
345
     * @param ServerRequestInterface $request the current request
346
     * @return ResponseInterface the response with the content
347
     */
348
    public function mainAction(ServerRequestInterface $request): ResponseInterface
349
    {
350
        $this->initialize();
351
        if ($request->getMethod() === 'POST') {
352
            $postData = $request->getParsedBody();
353
            if (is_array($postData) && !empty($postData)) {
354
                $this->storeIncomingData($postData);
355
            }
356
        }
357
        if ($this->languageUpdate) {
358
            $this->content .= $this->buildInstructionDataTag('TYPO3.ModuleMenu.App.refreshMenu');
359
            $this->content .= $this->buildInstructionDataTag('TYPO3.Backend.Topbar.refresh');
360
        }
361
        if ($this->pagetreeNeedsRefresh) {
362
            BackendUtility::setUpdateSignal('updatePageTree');
363
        }
364
365
        $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
366
        $this->content .= '<form action="' . $uriBuilder->buildUriFromRoute('user_setup') . '" method="post" id="SetupModuleController" name="usersetup" enctype="multipart/form-data">';
367
        $this->content .= '<div id="user-setup-wrapper">';
368
        $this->content .= $this->moduleTemplate->header($this->getLanguageService()->getLL('UserSettings'));
369
        $this->addFlashMessages();
370
371
        $formToken = $this->formProtection->generateToken('BE user setup', 'edit');
372
373
        // Render the menu items
374
        $menuItems = $this->renderUserSetup();
375
        $this->content .= $this->moduleTemplate->getDynamicTabMenu($menuItems, 'user-setup', 1, false, false);
376
        $this->content .= '<div>';
377
        $this->content .= '<input type="hidden" name="formToken" value="' . htmlspecialchars($formToken) . '" />
378
            <input type="hidden" value="1" name="data[save]" />
379
            <input type="hidden" name="data[setValuesToDefault]" value="0" id="setValuesToDefault" />';
380
        $this->content .= '</div>';
381
        // End of wrapper div
382
        $this->content .= '</div>';
383
        // Setting up the buttons and markers for docheader
384
        $this->getButtons();
385
        // Build the <body> for the module
386
        // Renders the module page
387
        $this->moduleTemplate->setContent($this->content);
388
        $this->content .= '</form>';
389
        return new HtmlResponse($this->moduleTemplate->renderContent());
390
    }
391
392
    protected function buildInstructionDataTag(string $dispatchAction): string
393
    {
394
        return sprintf(
395
            '<typo3-immediate-action action="%s"></typo3-immediate-action>' . "\n",
396
            htmlspecialchars($dispatchAction)
397
        );
398
    }
399
400
    /**
401
     * Create the panel of buttons for submitting the form or otherwise perform operations.
402
     */
403
    protected function getButtons(): void
404
    {
405
        $buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar();
406
        $cshButton = $buttonBar->makeHelpButton()
407
            ->setModuleName('_MOD_user_setup')
408
            ->setFieldName('');
409
        $buttonBar->addButton($cshButton);
410
411
        $saveButton = $buttonBar->makeInputButton()
412
            ->setName('data[save]')
413
            ->setTitle($this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:rm.saveDoc'))
414
            ->setValue('1')
415
            ->setForm('SetupModuleController')
416
            ->setShowLabelText(true)
417
            ->setIcon($this->moduleTemplate->getIconFactory()->getIcon('actions-document-save', Icon::SIZE_SMALL));
418
419
        $buttonBar->addButton($saveButton);
420
        $shortcutButton = $buttonBar->makeShortcutButton()
421
            ->setRouteIdentifier($this->moduleName)
422
            ->setDisplayName($this->getLanguageService()->sL('LLL:EXT:setup/Resources/Private/Language/locallang_mod.xlf:mlang_labels_tablabel'));
423
        $buttonBar->addButton($shortcutButton);
424
    }
425
426
    /******************************
427
     *
428
     * Render module
429
     *
430
     ******************************/
431
432
    /**
433
     * renders the data for all tabs in the user setup and returns
434
     * everything that is needed with tabs and dyntab menu
435
     *
436
     * @return array Ready to use for the dyntabmenu itemarray
437
     */
438
    protected function renderUserSetup()
439
    {
440
        $backendUser = $this->getBackendUser();
441
        $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
442
        $html = '';
443
        $result = [];
444
        $firstTabLabel = '';
445
        $code = [];
446
        $fieldArray = $this->getFieldsFromShowItem();
447
        $tabLabel = '';
448
        foreach ($fieldArray as $fieldName) {
449
            $config = $GLOBALS['TYPO3_USER_SETTINGS']['columns'][$fieldName];
450
            if (isset($config['access']) && !$this->checkAccess($config)) {
451
                continue;
452
            }
453
454
            if (strpos($fieldName, '--div--;') === 0) {
455
                if ($firstTabLabel === '') {
456
                    // First tab
457
                    $tabLabel = $this->getLabel(substr($fieldName, 8), '', false);
458
                    $firstTabLabel = $tabLabel;
459
                } else {
460
                    $result[] = [
461
                        'label' => $tabLabel,
462
                        'content' => count($code) ? implode(LF, $code) : ''
463
                    ];
464
                    $tabLabel = $this->getLabel(substr($fieldName, 8), '', false);
465
                    $code = [];
466
                }
467
                continue;
468
            }
469
            $label = $this->getLabel($config['label'], $fieldName);
470
            $label = $this->getCSH($config['csh'] ?: $fieldName, $label, $fieldName);
471
            $type = $config['type'];
472
            $class = $config['class'];
473
            if ($type !== 'check' && $type !== 'select') {
474
                $class .= ' form-control';
475
            }
476
            if ($type === 'select') {
477
                $class .= ' form-select';
478
            }
479
            $more = '';
480
            if ($class) {
481
                $more .= ' class="' . htmlspecialchars($class) . '"';
482
            }
483
            $style = $config['style'];
484
            if ($style) {
485
                $more .= ' style="' . htmlspecialchars($style) . '"';
486
            }
487
            if (isset($this->overrideConf[$fieldName])) {
488
                $more .= ' disabled="disabled"';
489
            }
490
            $value = $config['table'] === 'be_users' ? $backendUser->user[$fieldName] : $backendUser->uc[$fieldName];
491
            if (!$value && isset($config['default'])) {
492
                $value = $config['default'];
493
            }
494
            $dataAdd = '';
495
            if ($config['table'] === 'be_users') {
496
                $dataAdd = '[be_users]';
497
            }
498
499
            switch ($type) {
500
                case 'text':
501
                case 'number':
502
                case 'email':
503
                case 'password':
504
                    $noAutocomplete = '';
505
506
                    $maxLength = $config['max'] ?? 0;
507
                    if ((int)$maxLength > 0) {
508
                        $more .= ' maxlength="' . (int)$maxLength . '"';
509
                    }
510
511
                    if ($type === 'password') {
512
                        $value = '';
513
                        $noAutocomplete = 'autocomplete="new-password" ';
514
                        $more .= ' data-rsa-encryption=""';
515
                    }
516
                    $html = '<input aria-labelledby="label_' . htmlspecialchars($fieldName) . '" id="field_' . htmlspecialchars($fieldName) . '"
517
                        type="' . htmlspecialchars($type) . '"
518
                        name="data' . $dataAdd . '[' . htmlspecialchars($fieldName) . ']" ' .
519
                        $noAutocomplete .
520
                        'value="' . htmlspecialchars($value) . '" ' .
521
                        $more .
522
                        ' />';
523
                    break;
524
                case 'check':
525
                    $html = $label . '<div class="form-check form-switch"><input id="field_' . htmlspecialchars($fieldName) . '"
526
                        type="checkbox"
527
                        class="form-check-input"
528
                        aria-labelledby="label_' . htmlspecialchars($fieldName) . '"
529
                        name="data' . $dataAdd . '[' . htmlspecialchars($fieldName) . ']"' .
530
                        ($value ? ' checked="checked"' : '') .
531
                        $more .
532
                        ' /></div>';
533
                    $label = '';
534
                    break;
535
                case 'language':
536
                    $html = $this->renderLanguageSelect();
537
                    break;
538
                case 'select':
539
                    if ($config['itemsProcFunc']) {
540
                        $html = GeneralUtility::callUserFunction($config['itemsProcFunc'], $config, $this);
541
                    } else {
542
                        $html = '<select id="field_' . htmlspecialchars($fieldName) . '"
543
                            aria-labelledby="label_' . htmlspecialchars($fieldName) . '"
544
                            name="data' . $dataAdd . '[' . htmlspecialchars($fieldName) . ']"' .
545
                            $more . '>' . LF;
546
                        foreach ($config['items'] as $key => $optionLabel) {
547
                            $html .= '<option value="' . htmlspecialchars($key) . '"' . ($value == $key ? ' selected="selected"' : '') . '>' . $this->getLabel($optionLabel, '', false) . '</option>' . LF;
548
                        }
549
                        $html .= '</select>';
550
                    }
551
                    break;
552
                case 'user':
553
                    $html = GeneralUtility::callUserFunction($config['userFunc'], $config, $this);
554
                    break;
555
                case 'button':
556
                    if (!empty($config['clickData'])) {
557
                        $clickData = $config['clickData'];
558
                        $buttonAttributes = [
559
                            'type' => 'button',
560
                            'class' => 'btn btn-default',
561
                            'aria-labelledby' => 'label_' . htmlspecialchars($fieldName),
562
                            'value' => $this->getLabel($config['buttonlabel'], '', false),
563
                        ];
564
                        if (isset($clickData['eventName'])) {
565
                            $buttonAttributes['data-event'] = 'click';
566
                            $buttonAttributes['data-event-name'] = htmlspecialchars($clickData['eventName']);
567
                            $buttonAttributes['data-event-payload'] = htmlspecialchars($fieldName);
568
                        }
569
                        $html = '<br><input '
570
                            . GeneralUtility::implodeAttributes($buttonAttributes, false) . ' />';
571
                    } elseif (!empty($config['onClick'])) {
572
                        /**
573
                         * @deprecated Will be removed in TYPO3 v12.0
574
                         */
575
                        $onClick = $config['onClick'];
576
                        if ($config['onClickLabels']) {
577
                            foreach ($config['onClickLabels'] as $key => $labelclick) {
578
                                $config['onClickLabels'][$key] = $this->getLabel($labelclick, '', false);
579
                            }
580
                            $onClick = vsprintf($onClick, $config['onClickLabels']);
581
                        }
582
                        $html = '<br><input class="btn btn-default" type="button"
583
                            aria-labelledby="label_' . htmlspecialchars($fieldName) . '"
584
                            value="' . $this->getLabel($config['buttonlabel'], '', false) . '"
585
                            onclick="' . $onClick . '" />';
586
                    }
587
                    if (!empty($config['confirm'])) {
588
                        $confirmData = $config['confirmData'];
589
                        // cave: values must be processed by `htmlspecialchars()`
590
                        $buttonAttributes = [
591
                            'type' => 'button',
592
                            'class' => 'btn btn-default t3js-modal-trigger',
593
                            'data-severity' => 'warning',
594
                            'data-title' => $this->getLabel($config['label'], '', false),
595
                            'data-bs-content' => $this->getLabel($confirmData['message'], '', false),
596
                            'value' => htmlspecialchars($this->getLabel($config['buttonlabel'], '', false)),
597
                        ];
598
                        if (isset($confirmData['eventName'])) {
599
                            $buttonAttributes['data-event'] = 'confirm';
600
                            $buttonAttributes['data-event-name'] = htmlspecialchars($confirmData['eventName']);
601
                            $buttonAttributes['data-event-payload'] = htmlspecialchars($fieldName);
602
                        }
603
                        if (isset($confirmData['jsCodeAfterOk'])) {
604
                            /**
605
                             * @deprecated Will be removed in TYPO3 v12.0
606
                             */
607
                            $buttonAttributes['data-href'] = 'javascript:' . htmlspecialchars($confirmData['jsCodeAfterOk']);
608
                        }
609
                        $html = '<br><input '
610
                            . GeneralUtility::implodeAttributes($buttonAttributes, false) . ' />';
611
                    }
612
                    break;
613
                case 'avatar':
614
                    // Get current avatar image
615
                    $html = '<br>';
616
                    $avatarFileUid = $this->getAvatarFileUid($backendUser->user['uid']);
617
618
                    if ($avatarFileUid) {
619
                        $defaultAvatarProvider = GeneralUtility::makeInstance(DefaultAvatarProvider::class);
620
                        $avatarImage = $defaultAvatarProvider->getImage($backendUser->user, 32);
621
                        if ($avatarImage) {
622
                            $icon = '<span class="avatar"><span class="avatar-image">' .
623
                                '<img alt="" src="' . htmlspecialchars($avatarImage->getUrl(true)) . '"' .
624
                                ' width="' . (int)$avatarImage->getWidth() . '" ' .
625
                                'height="' . (int)$avatarImage->getHeight() . '" />' .
626
                                '</span></span>';
627
                            $html .= '<span class="pull-left" style="padding-right: 10px" id="image_' . htmlspecialchars($fieldName) . '">' . $icon . ' </span>';
628
                        }
629
                    }
630
                    $html .= '<input id="field_' . htmlspecialchars($fieldName) . '" type="hidden" ' .
631
                            'name="data' . $dataAdd . '[' . htmlspecialchars($fieldName) . ']"' . $more .
632
                            ' value="' . $avatarFileUid . '" data-setup-avatar-field="' . htmlspecialchars($fieldName) . '" />';
633
634
                    $html .= '<div class="btn-group">';
635
                    $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
636
                    if ($avatarFileUid) {
637
                        $html .=
638
                            '<button type="button" id="clear_button_' . htmlspecialchars($fieldName) . '" aria-label="' . htmlspecialchars($this->getLanguageService()->getLL('avatar.clear')) . '" '
639
                                . ' class="btn btn-default">'
640
                                . $iconFactory->getIcon('actions-delete', Icon::SIZE_SMALL)
641
                            . '</button>';
642
                    }
643
                    $html .=
644
                        '<button type="button" id="add_button_' . htmlspecialchars($fieldName) . '" class="btn btn-default btn-add-avatar"'
645
                            . ' aria-label="' . htmlspecialchars($this->getLanguageService()->getLL('avatar.openFileBrowser')) . '"'
646
                            . ' data-setup-avatar-url="' . htmlspecialchars((string)$uriBuilder->buildUriFromRoute('wizard_element_browser', ['mode' => 'file', 'bparams' => '||||__IDENTIFIER__'])) . '"'
647
                            . '>' . $iconFactory->getIcon('actions-insert-record', Icon::SIZE_SMALL)
648
                            . '</button></div>';
649
                    break;
650
                case 'mfa':
651
                    $html = '';
652
                    $lang = $this->getLanguageService();
653
                    $hasActiveProviders = $this->mfaProviderRegistry->hasActiveProviders($backendUser);
654
                    if ($hasActiveProviders) {
655
                        if ($this->mfaProviderRegistry->hasLockedProviders($backendUser)) {
656
                            $html .= ' <span class="badge badge-danger">' . htmlspecialchars($lang->getLL('mfaProviders.lockedMfaProviders')) . '</span>';
657
                        } else {
658
                            $html .= ' <span class="badge badge-success">' . htmlspecialchars($lang->getLL('mfaProviders.enabled')) . '</span>';
659
                        }
660
                    }
661
                    $html .= '<p class="text-muted">' . nl2br(htmlspecialchars($lang->getLL('mfaProviders.description'))) . '</p>';
662
                    if (!$this->mfaProviderRegistry->hasProviders()) {
663
                        $html .= '<span class="badge badge-danger">' . htmlspecialchars($lang->getLL('mfaProviders.notAvailable')) . '</span>';
664
                        break;
665
                    }
666
                    $html .= '<a href="' . htmlspecialchars((string)$uriBuilder->buildUriFromRoute('mfa')) . '" class="btn btn-' . ($hasActiveProviders ? 'default' : 'success') . '">';
667
                    $html .=    GeneralUtility::makeInstance(IconFactory::class)->getIcon($hasActiveProviders ? 'actions-cog' : 'actions-add', Icon::SIZE_SMALL);
668
                    $html .=    ' <span>' . htmlspecialchars($lang->getLL('mfaProviders.' . ($hasActiveProviders ? 'manageLinkTitle' : 'setupLinkTitle'))) . '</span>';
669
                    $html .= '</a>';
670
                    break;
671
                default:
672
                    $html = '';
673
            }
674
675
            $code[] = '<div class="form-section"><div class="row"><div class="form-group t3js-formengine-field-item col-md-12">' .
676
                $label .
677
                $html .
678
                '</div></div></div>';
679
        }
680
681
        $result[] = [
682
            'label' => $tabLabel,
683
            'content' => count($code) ? implode(LF, $code) : ''
684
        ];
685
        return $result;
686
    }
687
688
    /**
689
     * Return a select with available languages.
690
     * This method is called from the setup module fake TCA userFunc.
691
     *
692
     * @return string Complete select as HTML string or warning box if something went wrong.
693
     */
694
    protected function renderLanguageSelect()
695
    {
696
        $tcaConfig = $GLOBALS['TCA']['be_users']['columns']['lang']['config'];
697
        $items = $tcaConfig['items'];
698
        $itemsProcFunc = [
699
            'items' => &$items
700
        ];
701
        GeneralUtility::callUserFunction($tcaConfig['itemsProcFunc'], $itemsProcFunc);
702
        $backendUser = $this->getBackendUser();
703
        $currentSelectedLanguage = (string)($backendUser->user['lang'] ?? 'default');
704
        $languageService = $this->getLanguageService();
705
        $content = '';
706
        // get all labels in default language as well
707
        $defaultLanguageLabelService = LanguageService::create('default');
708
        $defaultLanguageLabelService->includeLLFile('EXT:setup/Resources/Private/Language/locallang.xlf');
709
        foreach ($items as $item) {
710
            $languageCode = $item[1];
711
            $name = $item[0];
712
            $available = in_array($languageCode, $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['lang']['availableLanguages'] ?? [], true) || is_dir(Environment::getLabelsPath() . '/' . $languageCode);
713
            if ($available || $languageCode === 'default') {
714
                $localizedName = htmlspecialchars($languageService->getLL('lang_' . $languageCode) ?: $name);
715
                $defaultName = $defaultLanguageLabelService->getLL('lang_' . $languageCode);
716
                if ($defaultName === $localizedName || $defaultName === '') {
717
                    $defaultName = $languageCode;
718
                }
719
                if ($defaultName !== $languageCode) {
720
                    $defaultName .= ' - ' . $languageCode;
721
                }
722
                $localLabel = ' [' . htmlspecialchars($defaultName) . ']';
723
                $content .= '<option value="' . $languageCode . '"' . ($currentSelectedLanguage === $languageCode ? ' selected="selected"' : '') . '>' . $localizedName . $localLabel . '</option>';
724
            }
725
        }
726
        $content = '<select aria-labelledby="label_lang" id="field_lang" name="data[be_users][lang]" class="form-select">' . $content . '</select>';
727
        if ($currentSelectedLanguage !== 'default' && !@is_dir(Environment::getLabelsPath() . '/' . $currentSelectedLanguage)) {
728
            $languageUnavailableWarning = htmlspecialchars(sprintf($languageService->getLL('languageUnavailable'), $languageService->getLL('lang_' . $currentSelectedLanguage))) . '&nbsp;&nbsp;<br />&nbsp;&nbsp;' . htmlspecialchars($languageService->getLL('languageUnavailable.' . ($backendUser->isAdmin() ? 'admin' : 'user')));
729
            $content = '<br /><span class="label label-danger">' . $languageUnavailableWarning . '</span><br /><br />' . $content;
730
        }
731
        return $content;
732
    }
733
734
    /**
735
     * Returns a select with all modules for startup.
736
     * This method is called from the setup module fake TCA userFunc.
737
     *
738
     * @return string Complete select as HTML string
739
     */
740
    public function renderStartModuleSelect()
741
    {
742
        // Load available backend modules
743
        $loadModules = GeneralUtility::makeInstance(ModuleLoader::class);
744
        $loadModules->observeWorkspaces = true;
745
        $loadModules->load($GLOBALS['TBE_MODULES']);
746
        $startModuleSelect = '<option value="">' . htmlspecialchars($this->getLanguageService()->getLL('startModule.firstInMenu')) . '</option>';
747
        foreach ($loadModules->modules as $mainMod => $modData) {
748
            $hasSubmodules = !empty($modData['sub']) && is_array($modData['sub']);
749
            $isStandalone = $modData['standalone'] ?? false;
750
            if ($hasSubmodules || $isStandalone) {
751
                $modules = '';
752
                if (($hasSubmodules)) {
753
                    foreach ($modData['sub'] as $subData) {
754
                        $modName = $subData['name'];
755
                        $modules .= '<option value="' . htmlspecialchars($modName) . '"';
756
                        $modules .= $this->getBackendUser()->uc['startModule'] === $modName ? ' selected="selected"' : '';
757
                        $modules .= '>' . htmlspecialchars($this->getLanguageService()->sL($loadModules->getLabelsForModule($modName)['title'])) . '</option>';
758
                    }
759
                } elseif ($isStandalone) {
760
                    $modName = $modData['name'];
761
                    $modules .= '<option value="' . htmlspecialchars($modName) . '"';
762
                    $modules .= $this->getBackendUser()->uc['startModule'] === $modName ? ' selected="selected"' : '';
763
                    $modules .= '>' . htmlspecialchars($this->getLanguageService()->sL($loadModules->getLabelsForModule($modName)['title'])) . '</option>';
764
                }
765
                $groupLabel = htmlspecialchars($this->getLanguageService()->sL($loadModules->getLabelsForModule($mainMod)['title']));
766
                $startModuleSelect .= '<optgroup label="' . htmlspecialchars($groupLabel) . '">' . $modules . '</optgroup>';
767
            }
768
        }
769
        return '<select id="field_startModule" aria-labelledby="label_startModule" name="data[startModule]" class="form-select">' . $startModuleSelect . '</select>';
770
    }
771
772
    /**
773
     * Returns access check (currently only "admin" is supported)
774
     *
775
     * @param array $config Configuration of the field, access mode is defined in key 'access'
776
     * @return bool Whether it is allowed to modify the given field
777
     */
778
    protected function checkAccess(array $config)
779
    {
780
        $access = $config['access'];
781
        if (isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['setup']['accessLevelCheck'][$access])) {
782
            if (class_exists($access)) {
783
                $accessObject = GeneralUtility::makeInstance($access);
784
                if (method_exists($accessObject, 'accessLevelCheck')) {
785
                    // Initialize vars. If method fails, $set will be set to FALSE
786
                    return $accessObject->accessLevelCheck($config);
787
                }
788
            }
789
        } elseif ($access === 'admin') {
790
            return $this->getBackendUser()->isAdmin();
791
        }
792
793
        return false;
794
    }
795
796
    /**
797
     * Returns the label $str from getLL() and grays out the value if the $str/$key is found in $this->overrideConf array
798
     *
799
     * @param string $str Locallang key
800
     * @param string $key Alternative override-config key
801
     * @param bool $addLabelTag Defines whether the string should be wrapped in a <label> tag.
802
     * @return string HTML output.
803
     */
804
    protected function getLabel($str, $key = '', $addLabelTag = true)
805
    {
806
        if (strpos($str, 'LLL:') === 0) {
807
            $out = htmlspecialchars($this->getLanguageService()->sL($str));
808
        } else {
809
            $out = htmlspecialchars($str);
810
        }
811
        if (isset($this->overrideConf[$key ?: $str])) {
812
            $out = '<span style="color:#999999">' . $out . '</span>';
813
        }
814
        if ($addLabelTag) {
815
            $out = '<label>' . $out . '</label>';
816
        }
817
        return $out;
818
    }
819
820
    /**
821
     * Returns the CSH Icon for given string
822
     *
823
     * @param string $str Locallang key
824
     * @param string $label The label to be used, that should be wrapped in help
825
     * @param string $fieldName field name
826
     * @return string HTML output.
827
     */
828
    protected function getCSH($str, $label, $fieldName)
829
    {
830
        $context = '_MOD_user_setup';
831
        $field = $str;
832
        $strParts = explode(':', $str);
833
        if (count($strParts) > 1) {
834
            // Setting comes from another extension
835
            $context = $strParts[0];
836
            $field = $strParts[1];
837
        } elseif ($str !== 'language' && $str !== 'reset') {
838
            $field = 'option_' . $str;
839
        }
840
        return '<span id="label_' . htmlspecialchars($fieldName) . '">' . BackendUtility::wrapInHelp($context, $field, $label) . '</span>';
841
    }
842
843
    /**
844
     * Returns array with fields defined in $GLOBALS['TYPO3_USER_SETTINGS']['showitem']
845
     * Remove fields which are disabled by user TSconfig
846
     *
847
     * @return string[] Array with field names visible in form
848
     */
849
    protected function getFieldsFromShowItem()
850
    {
851
        $allowedFields = GeneralUtility::trimExplode(',', $GLOBALS['TYPO3_USER_SETTINGS']['showitem'], true);
852
        if ($this->getBackendUser()->isAdmin()) {
853
            // Do not ask for current password if admin (unknown for other users and no security gain)
854
            $key = array_search('passwordCurrent', $allowedFields);
855
            if ($key !== false) {
856
                unset($allowedFields[$key]);
857
            }
858
        }
859
860
        $backendUser = $this->getBackendUser();
861
        $systemMaintainers = array_map('intval', $GLOBALS['TYPO3_CONF_VARS']['SYS']['systemMaintainers'] ?? []);
862
        if ($backendUser->getOriginalUserIdWhenInSwitchUserMode() && in_array((int)$backendUser->user['uid'], $systemMaintainers, true)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $backendUser->getOrigina...dWhenInSwitchUserMode() of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
863
            // DataHandler denies changing password of system maintainer users in switch user mode.
864
            // Do not show the password fields is this case.
865
            $key = array_search('password', $allowedFields);
866
            if ($key !== false) {
867
                unset($allowedFields[$key]);
868
            }
869
            $key = array_search('password2', $allowedFields);
870
            if ($key !== false) {
871
                unset($allowedFields[$key]);
872
            }
873
        }
874
875
        if (!is_array($this->tsFieldConf)) {
0 ignored issues
show
introduced by
The condition is_array($this->tsFieldConf) is always true.
Loading history...
876
            return $allowedFields;
877
        }
878
        foreach ($this->tsFieldConf as $fieldName => $userTsFieldConfig) {
879
            if (!empty($userTsFieldConfig['disabled'])) {
880
                $fieldName = rtrim($fieldName, '.');
881
                $key = array_search($fieldName, $allowedFields);
882
                if ($key !== false) {
883
                    unset($allowedFields[$key]);
884
                }
885
            }
886
        }
887
        return $allowedFields;
888
    }
889
890
    /**
891
     * Get Avatar fileUid
892
     *
893
     * @param int $beUserId
894
     * @return int
895
     */
896
    protected function getAvatarFileUid($beUserId)
897
    {
898
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_file_reference');
899
        $file = $queryBuilder
900
            ->select('uid_local')
901
            ->from('sys_file_reference')
902
            ->where(
903
                $queryBuilder->expr()->eq(
904
                    'tablenames',
905
                    $queryBuilder->createNamedParameter('be_users', \PDO::PARAM_STR)
906
                ),
907
                $queryBuilder->expr()->eq(
908
                    'fieldname',
909
                    $queryBuilder->createNamedParameter('avatar', \PDO::PARAM_STR)
910
                ),
911
                $queryBuilder->expr()->eq(
912
                    'table_local',
913
                    $queryBuilder->createNamedParameter('sys_file', \PDO::PARAM_STR)
914
                ),
915
                $queryBuilder->expr()->eq(
916
                    'uid_foreign',
917
                    $queryBuilder->createNamedParameter($beUserId, \PDO::PARAM_INT)
918
                )
919
            )
920
            ->execute()
921
            ->fetchColumn();
922
        return (int)$file;
923
    }
924
925
    /**
926
     * Set avatar fileUid for backend user
927
     *
928
     * @param int $beUserId
929
     * @param int $fileUid
930
     * @param array $storeRec
931
     */
932
    protected function setAvatarFileUid($beUserId, $fileUid, array &$storeRec)
933
    {
934
935
        // Update is only needed when new fileUid is set
936
        if ((int)$fileUid === $this->getAvatarFileUid($beUserId)) {
937
            return;
938
        }
939
940
        // If user is not allowed to modify avatar $fileUid is empty - so don't overwrite existing avatar
941
        if (empty($fileUid)) {
942
            return;
943
        }
944
945
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_file_reference');
946
        $queryBuilder->getRestrictions()->removeAll();
947
        $queryBuilder
948
            ->delete('sys_file_reference')
949
            ->where(
950
                $queryBuilder->expr()->eq(
951
                    'tablenames',
952
                    $queryBuilder->createNamedParameter('be_users', \PDO::PARAM_STR)
953
                ),
954
                $queryBuilder->expr()->eq(
955
                    'fieldname',
956
                    $queryBuilder->createNamedParameter('avatar', \PDO::PARAM_STR)
957
                ),
958
                $queryBuilder->expr()->eq(
959
                    'table_local',
960
                    $queryBuilder->createNamedParameter('sys_file', \PDO::PARAM_STR)
961
                ),
962
                $queryBuilder->expr()->eq(
963
                    'uid_foreign',
964
                    $queryBuilder->createNamedParameter($beUserId, \PDO::PARAM_INT)
965
                )
966
            )
967
            ->execute();
968
969
        // If Avatar is marked for delete => set it to empty string so it will be updated properly
970
        if ($fileUid === 'delete') {
0 ignored issues
show
introduced by
The condition $fileUid === 'delete' is always false.
Loading history...
971
            $fileUid = '';
972
        }
973
974
        // Create new reference
975
        if ($fileUid) {
976
977
            // Get file object
978
            try {
979
                $file = GeneralUtility::makeInstance(ResourceFactory::class)->getFileObject($fileUid);
980
            } catch (FileDoesNotExistException $e) {
981
                $file = false;
982
            }
983
984
            // Check if user is allowed to use the image (only when not in simulation mode)
985
            if ($file && !$file->getStorage()->checkFileActionPermission('read', $file)) {
986
                $file = false;
987
            }
988
989
            // Check if extension is allowed
990
            if ($file && $file->isImage()) {
991
992
                // Create new file reference
993
                $storeRec['sys_file_reference']['NEW1234'] = [
994
                    'uid_local' => (int)$fileUid,
995
                    'uid_foreign' => (int)$beUserId,
996
                    'tablenames' => 'be_users',
997
                    'fieldname' => 'avatar',
998
                    'pid' => 0,
999
                    'table_local' => 'sys_file',
1000
                ];
1001
                $storeRec['be_users'][(int)$beUserId]['avatar'] = 'NEW1234';
1002
            }
1003
        }
1004
    }
1005
1006
    /**
1007
     * Returns the current BE user.
1008
     *
1009
     * @return BackendUserAuthentication
1010
     */
1011
    protected function getBackendUser()
1012
    {
1013
        return $GLOBALS['BE_USER'];
1014
    }
1015
1016
    /**
1017
     * @return LanguageService
1018
     */
1019
    protected function getLanguageService()
1020
    {
1021
        return $GLOBALS['LANG'];
1022
    }
1023
1024
    /**
1025
     * Add FlashMessages for various actions
1026
     */
1027
    protected function addFlashMessages()
1028
    {
1029
        $flashMessages = [];
1030
1031
        // Show if setup was saved
1032
        if ($this->setupIsUpdated && !$this->settingsAreResetToDefault) {
1033
            $flashMessages[] = $this->getFlashMessage('setupWasUpdated', 'UserSettings');
1034
        }
1035
1036
        // Show if temporary data was cleared
1037
        if ($this->settingsAreResetToDefault) {
1038
            $flashMessages[] = $this->getFlashMessage('settingsAreReset', 'resetConfiguration');
1039
        }
1040
1041
        // Notice
1042
        if ($this->setupIsUpdated || $this->settingsAreResetToDefault) {
1043
            $flashMessages[] = $this->getFlashMessage('activateChanges', '', FlashMessage::INFO);
1044
        }
1045
1046
        // If password is updated, output whether it failed or was OK.
1047
        if ($this->passwordIsSubmitted) {
1048
            switch ($this->passwordIsUpdated) {
1049
                case self::PASSWORD_OLD_WRONG:
1050
                    $flashMessages[] = $this->getFlashMessage('oldPassword_failed', 'newPassword', FlashMessage::ERROR);
1051
                    break;
1052
                case self::PASSWORD_NOT_THE_SAME:
1053
                    $flashMessages[] = $this->getFlashMessage('newPassword_failed', 'newPassword', FlashMessage::ERROR);
1054
                    break;
1055
                case self::PASSWORD_UPDATED:
1056
                    $flashMessages[] = $this->getFlashMessage('newPassword_ok', 'newPassword');
1057
                    break;
1058
            }
1059
        }
1060
        if (!empty($flashMessages)) {
1061
            $this->enqueueFlashMessages($flashMessages);
1062
        }
1063
    }
1064
1065
    /**
1066
     * @param array $flashMessages
1067
     * @throws \TYPO3\CMS\Core\Exception
1068
     */
1069
    protected function enqueueFlashMessages(array $flashMessages)
1070
    {
1071
        $flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class);
1072
        $defaultFlashMessageQueue = $flashMessageService->getMessageQueueByIdentifier();
1073
        foreach ($flashMessages as $flashMessage) {
1074
            $defaultFlashMessageQueue->enqueue($flashMessage);
1075
        }
1076
    }
1077
1078
    /**
1079
     * @param string $message
1080
     * @param string $title
1081
     * @param int $severity
1082
     * @return FlashMessage
1083
     */
1084
    protected function getFlashMessage($message, $title, $severity = FlashMessage::OK)
1085
    {
1086
        $title = !empty($title) ? $this->getLanguageService()->getLL($title) : ' ';
1087
        return GeneralUtility::makeInstance(
1088
            FlashMessage::class,
1089
            $this->getLanguageService()->getLL($message),
1090
            $title,
1091
            $severity
1092
        );
1093
    }
1094
}
1095