SetupModuleController::renderUserSetup()   F
last analyzed

Complexity

Conditions 54
Paths > 20000

Size

Total Lines 245
Code Lines 191

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 191
dl 0
loc 245
rs 0
c 2
b 0
f 0
cc 54
nc 32266
nop 0

How to fix   Long Method    Complexity   

Long Method

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:

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

312
                $this->/** @scrutinizer ignore-call */ 
313
                       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...
313
314
                $this->saveData = true;
315
            }
316
            // Inserts the overriding values.
317
            $backendUser->overrideUC();
318
            $save_after = md5(serialize($backendUser->uc));
319
            // If something in the uc-array of the user has changed, we save the array...
320
            if ($save_before != $save_after) {
321
                $backendUser->writeUC($backendUser->uc);
322
                $backendUser->writelog(SystemLogType::SETTING, SystemLogSettingAction::CHANGE, SystemLogErrorClassification::MESSAGE, 1, 'Personal settings changed', []);
323
                $this->setupIsUpdated = true;
324
            }
325
            // Persist data if something has changed:
326
            if (!empty($storeRec) && $this->saveData) {
327
                // Set user to admin to circumvent DataHandler restrictions.
328
                // Not using isAdmin() to fetch the original value, just in case it has been boolean casted.
329
                $savedUserAdminState = $backendUser->user['admin'];
330
                $backendUser->user['admin'] = true;
331
                // Make dedicated instance of TCE for storing the changes.
332
                $dataHandler = GeneralUtility::makeInstance(DataHandler::class);
333
                $dataHandler->start($storeRec, [], $backendUser);
334
                // This is to make sure that the users record can be updated even if in another workspace. This is tolerated.
335
                $dataHandler->bypassWorkspaceRestrictions = true;
336
                $dataHandler->process_datamap();
337
                // reset the user record admin flag to previous value, just in case it gets used any further.
338
                $backendUser->user['admin'] = $savedUserAdminState;
339
                if ($this->passwordIsUpdated === self::PASSWORD_NOT_UPDATED || count($storeRec['be_users'][$beUserId]) > 1) {
340
                    $this->setupIsUpdated = true;
341
                }
342
                BackendUtility::setUpdateSignal('updateTopbar');
343
            }
344
        }
345
    }
346
347
    /**
348
     * Injects the request object, checks if data should be saved, and prepares a HTML page
349
     *
350
     * @param ServerRequestInterface $request the current request
351
     * @return ResponseInterface the response with the content
352
     */
353
    public function mainAction(ServerRequestInterface $request): ResponseInterface
354
    {
355
        $this->initialize($request);
356
        if ($request->getMethod() === 'POST') {
357
            $postData = $request->getParsedBody();
358
            if (is_array($postData) && !empty($postData)) {
359
                $this->storeIncomingData($postData);
360
            }
361
        }
362
        if ($this->languageUpdate) {
363
            $this->content .= $this->buildInstructionDataTag('TYPO3.ModuleMenu.App.refreshMenu');
364
            $this->content .= $this->buildInstructionDataTag('TYPO3.Backend.Topbar.refresh');
365
        }
366
        if ($this->pagetreeNeedsRefresh) {
367
            BackendUtility::setUpdateSignal('updatePageTree');
368
        }
369
370
        $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
371
        $this->content .= '<form action="' . $uriBuilder->buildUriFromRoute('user_setup') . '" method="post" id="SetupModuleController" name="usersetup" enctype="multipart/form-data">';
372
        $this->content .= '<div id="user-setup-wrapper">';
373
        $this->content .= $this->moduleTemplate->header($this->getLanguageService()->getLL('UserSettings'));
374
        $this->addFlashMessages();
375
376
        $formToken = $this->formProtection->generateToken('BE user setup', 'edit');
377
378
        // Render the menu items
379
        $menuItems = $this->renderUserSetup();
380
        $this->content .= $this->moduleTemplate->getDynamicTabMenu($menuItems, 'user-setup', 1, false, false);
381
        $this->content .= '<div>';
382
        $this->content .= '<input type="hidden" name="formToken" value="' . htmlspecialchars($formToken) . '" />
383
            <input type="hidden" value="1" name="data[save]" />
384
            <input type="hidden" name="data[setValuesToDefault]" value="0" id="setValuesToDefault" />';
385
        $this->content .= '</div>';
386
        // End of wrapper div
387
        $this->content .= '</div>';
388
        // Setting up the buttons and markers for docheader
389
        $this->getButtons();
390
        // Build the <body> for the module
391
        // Renders the module page
392
        $this->moduleTemplate->setContent($this->content);
393
        $this->content .= '</form>';
394
        return new HtmlResponse($this->moduleTemplate->renderContent());
395
    }
396
397
    protected function buildInstructionDataTag(string $dispatchAction): string
398
    {
399
        return sprintf(
400
            '<typo3-immediate-action action="%s"></typo3-immediate-action>' . "\n",
401
            htmlspecialchars($dispatchAction)
402
        );
403
    }
404
405
    /**
406
     * Create the panel of buttons for submitting the form or otherwise perform operations.
407
     */
408
    protected function getButtons(): void
409
    {
410
        $buttonBar = $this->moduleTemplate->getDocHeaderComponent()->getButtonBar();
411
        $cshButton = $buttonBar->makeHelpButton()
412
            ->setModuleName('_MOD_user_setup')
413
            ->setFieldName('');
414
        $buttonBar->addButton($cshButton);
415
416
        $saveButton = $buttonBar->makeInputButton()
417
            ->setName('data[save]')
418
            ->setTitle($this->getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:rm.saveDoc'))
419
            ->setValue('1')
420
            ->setForm('SetupModuleController')
421
            ->setShowLabelText(true)
422
            ->setIcon($this->iconFactory->getIcon('actions-document-save', Icon::SIZE_SMALL));
423
424
        $buttonBar->addButton($saveButton);
425
        $shortcutButton = $buttonBar->makeShortcutButton()
426
            ->setRouteIdentifier($this->moduleName)
427
            ->setDisplayName($this->getLanguageService()->sL('LLL:EXT:setup/Resources/Private/Language/locallang_mod.xlf:mlang_labels_tablabel'));
428
        $buttonBar->addButton($shortcutButton);
429
    }
430
431
    /******************************
432
     *
433
     * Render module
434
     *
435
     ******************************/
436
437
    /**
438
     * renders the data for all tabs in the user setup and returns
439
     * everything that is needed with tabs and dyntab menu
440
     *
441
     * @return array Ready to use for the dyntabmenu itemarray
442
     */
443
    protected function renderUserSetup()
444
    {
445
        $backendUser = $this->getBackendUser();
446
        $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
447
        $html = '';
448
        $result = [];
449
        $firstTabLabel = '';
450
        $code = [];
451
        $fieldArray = $this->getFieldsFromShowItem();
452
        $tabLabel = '';
453
        foreach ($fieldArray as $fieldName) {
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
470
            $config = $GLOBALS['TYPO3_USER_SETTINGS']['columns'][$fieldName] ?? null;
471
            if ($config && isset($config['access']) && !$this->checkAccess($config)) {
472
                continue;
473
            }
474
475
            $label = $this->getLabel($config['label'] ?? '', $fieldName);
476
            $label = $this->getCSH(($config['csh'] ?? false) ?: $fieldName, $label, $fieldName);
477
            $type = $config['type'] ?? '';
478
            $class = $config['class'] ?? '';
479
            if ($type !== 'check' && $type !== 'select') {
480
                $class .= ' form-control';
481
            }
482
            if ($type === 'select') {
483
                $class .= ' form-select';
484
            }
485
            $more = '';
486
            if ($class) {
487
                $more .= ' class="' . htmlspecialchars($class) . '"';
488
            }
489
            $style = $config['style'] ?? '';
490
            if ($style) {
491
                $more .= ' style="' . htmlspecialchars($style) . '"';
492
            }
493
            if (isset($this->overrideConf[$fieldName])) {
494
                $more .= ' disabled="disabled"';
495
            }
496
            $isBeUsersTable = ($config['table'] ?? false) === 'be_users';
497
            $value = $isBeUsersTable ? ($backendUser->user[$fieldName] ?? false) : ($backendUser->uc[$fieldName] ?? false);
498
            if (!$value && isset($config['default'])) {
499
                $value = $config['default'];
500
            }
501
            $dataAdd = $isBeUsersTable ? '[be_users]' : '';
502
503
            switch ($type) {
504
                case 'text':
505
                case 'number':
506
                case 'email':
507
                case 'password':
508
                    $noAutocomplete = '';
509
510
                    $maxLength = $config['max'] ?? 0;
511
                    if ((int)$maxLength > 0) {
512
                        $more .= ' maxlength="' . (int)$maxLength . '"';
513
                    }
514
515
                    if ($type === 'password') {
516
                        $value = '';
517
                        $noAutocomplete = 'autocomplete="new-password" ';
518
                    }
519
                    $html = '<input aria-labelledby="label_' . htmlspecialchars($fieldName) . '" id="field_' . htmlspecialchars($fieldName) . '"
520
                        type="' . htmlspecialchars($type) . '"
521
                        name="data' . $dataAdd . '[' . htmlspecialchars($fieldName) . ']" ' .
522
                        $noAutocomplete .
523
                        'value="' . htmlspecialchars($value) . '" ' .
524
                        $more .
525
                        ' />';
526
                    break;
527
                case 'check':
528
                    $html = $label . '<div class="form-check form-switch"><input id="field_' . htmlspecialchars($fieldName) . '"
529
                        type="checkbox"
530
                        class="form-check-input"
531
                        aria-labelledby="label_' . htmlspecialchars($fieldName) . '"
532
                        name="data' . $dataAdd . '[' . htmlspecialchars($fieldName) . ']"' .
533
                        ($value ? ' checked="checked"' : '') .
534
                        $more .
535
                        ' /></div>';
536
                    $label = '';
537
                    break;
538
                case 'language':
539
                    $html = $this->renderLanguageSelect();
540
                    break;
541
                case 'select':
542
                    if ($config['itemsProcFunc'] ?? false) {
543
                        $html = GeneralUtility::callUserFunction($config['itemsProcFunc'], $config, $this);
544
                    } else {
545
                        $html = '<select id="field_' . htmlspecialchars($fieldName) . '"
546
                            aria-labelledby="label_' . htmlspecialchars($fieldName) . '"
547
                            name="data' . $dataAdd . '[' . htmlspecialchars($fieldName) . ']"' .
548
                            $more . '>' . LF;
549
                        foreach ($config['items'] as $key => $optionLabel) {
550
                            $html .= '<option value="' . htmlspecialchars($key) . '"' . ($value == $key ? ' selected="selected"' : '') . '>' . $this->getLabel($optionLabel, '', false) . '</option>' . LF;
551
                        }
552
                        $html .= '</select>';
553
                    }
554
                    break;
555
                case 'user':
556
                    $html = GeneralUtility::callUserFunction($config['userFunc'], $config, $this);
557
                    break;
558
                case 'button':
559
                    if (!empty($config['clickData'])) {
560
                        $clickData = $config['clickData'];
561
                        $buttonAttributes = [
562
                            'type' => 'button',
563
                            'class' => 'btn btn-default',
564
                            'aria-labelledby' => 'label_' . htmlspecialchars($fieldName),
565
                            'value' => $this->getLabel($config['buttonlabel'], '', false),
566
                        ];
567
                        if (isset($clickData['eventName'])) {
568
                            $buttonAttributes['data-event'] = 'click';
569
                            $buttonAttributes['data-event-name'] = htmlspecialchars($clickData['eventName']);
570
                            $buttonAttributes['data-event-payload'] = htmlspecialchars($fieldName);
571
                        }
572
                        $html = '<br><input '
573
                            . GeneralUtility::implodeAttributes($buttonAttributes, false) . ' />';
574
                    } elseif (!empty($config['onClick'])) {
575
                        /**
576
                         * @deprecated Will be removed in TYPO3 v12.0
577
                         */
578
                        $onClick = $config['onClick'];
579
                        if ($config['onClickLabels'] ?? false) {
580
                            foreach ($config['onClickLabels'] as $key => $labelclick) {
581
                                $config['onClickLabels'][$key] = $this->getLabel($labelclick, '', false);
582
                            }
583
                            $onClick = vsprintf($onClick, $config['onClickLabels']);
584
                        }
585
                        $html = '<br><input class="btn btn-default" type="button"
586
                            aria-labelledby="label_' . htmlspecialchars($fieldName) . '"
587
                            value="' . $this->getLabel($config['buttonlabel'], '', false) . '"
588
                            onclick="' . $onClick . '" />';
589
                    }
590
                    if (!empty($config['confirm'])) {
591
                        $confirmData = $config['confirmData'];
592
                        // cave: values must be processed by `htmlspecialchars()`
593
                        $buttonAttributes = [
594
                            'type' => 'button',
595
                            'class' => 'btn btn-default t3js-modal-trigger',
596
                            'data-severity' => 'warning',
597
                            'data-title' => $this->getLabel($config['label'], '', false),
598
                            'data-bs-content' => $this->getLabel($confirmData['message'], '', false),
599
                            'value' => htmlspecialchars($this->getLabel($config['buttonlabel'], '', false)),
600
                        ];
601
                        if (isset($confirmData['eventName'])) {
602
                            $buttonAttributes['data-event'] = 'confirm';
603
                            $buttonAttributes['data-event-name'] = htmlspecialchars($confirmData['eventName']);
604
                            $buttonAttributes['data-event-payload'] = htmlspecialchars($fieldName);
605
                        }
606
                        if (isset($confirmData['jsCodeAfterOk'])) {
607
                            /**
608
                             * @deprecated Will be removed in TYPO3 v12.0
609
                             */
610
                            $buttonAttributes['data-href'] = 'javascript:' . htmlspecialchars($confirmData['jsCodeAfterOk']);
611
                        }
612
                        $html = '<br><input '
613
                            . GeneralUtility::implodeAttributes($buttonAttributes, false) . ' />';
614
                    }
615
                    break;
616
                case 'avatar':
617
                    // Get current avatar image
618
                    $html = '<br>';
619
                    $avatarFileUid = $this->getAvatarFileUid($backendUser->user['uid']);
620
621
                    if ($avatarFileUid) {
622
                        $defaultAvatarProvider = GeneralUtility::makeInstance(DefaultAvatarProvider::class);
623
                        $avatarImage = $defaultAvatarProvider->getImage($backendUser->user, 32);
624
                        if ($avatarImage) {
625
                            $icon = '<span class="avatar"><span class="avatar-image">' .
626
                                '<img alt="" src="' . htmlspecialchars($avatarImage->getUrl(true)) . '"' .
627
                                ' width="' . (int)$avatarImage->getWidth() . '" ' .
628
                                'height="' . (int)$avatarImage->getHeight() . '" />' .
629
                                '</span></span>';
630
                            $html .= '<span class="pull-left" style="padding-right: 10px" id="image_' . htmlspecialchars($fieldName) . '">' . $icon . ' </span>';
631
                        }
632
                    }
633
                    $html .= '<input id="field_' . htmlspecialchars($fieldName) . '" type="hidden" ' .
634
                            'name="data' . $dataAdd . '[' . htmlspecialchars($fieldName) . ']"' . $more .
635
                            ' value="' . $avatarFileUid . '" data-setup-avatar-field="' . htmlspecialchars($fieldName) . '" />';
636
637
                    $html .= '<div class="btn-group">';
638
                    if ($avatarFileUid) {
639
                        $html .=
640
                            '<button type="button" id="clear_button_' . htmlspecialchars($fieldName) . '" aria-label="' . htmlspecialchars($this->getLanguageService()->getLL('avatar.clear')) . '" '
641
                                . ' class="btn btn-default">'
642
                                . $this->iconFactory->getIcon('actions-delete', Icon::SIZE_SMALL)
643
                            . '</button>';
644
                    }
645
                    $html .=
646
                        '<button type="button" id="add_button_' . htmlspecialchars($fieldName) . '" class="btn btn-default btn-add-avatar"'
647
                            . ' aria-label="' . htmlspecialchars($this->getLanguageService()->getLL('avatar.openFileBrowser')) . '"'
648
                            . ' data-setup-avatar-url="' . htmlspecialchars((string)$uriBuilder->buildUriFromRoute('wizard_element_browser', ['mode' => 'file', 'bparams' => '||||__IDENTIFIER__'])) . '"'
649
                            . '>' . $this->iconFactory->getIcon('actions-insert-record', Icon::SIZE_SMALL)
650
                            . '</button></div>';
651
                    break;
652
                case 'mfa':
653
                    $html = '';
654
                    $lang = $this->getLanguageService();
655
                    $hasActiveProviders = $this->mfaProviderRegistry->hasActiveProviders($backendUser);
656
                    if ($hasActiveProviders) {
657
                        if ($this->mfaProviderRegistry->hasLockedProviders($backendUser)) {
658
                            $html .= ' <span class="badge badge-danger">' . htmlspecialchars($lang->getLL('mfaProviders.lockedMfaProviders')) . '</span>';
659
                        } else {
660
                            $html .= ' <span class="badge badge-success">' . htmlspecialchars($lang->getLL('mfaProviders.enabled')) . '</span>';
661
                        }
662
                    }
663
                    $html .= '<p class="text-muted">' . nl2br(htmlspecialchars($lang->getLL('mfaProviders.description'))) . '</p>';
664
                    if (!$this->mfaProviderRegistry->hasProviders()) {
665
                        $html .= '<span class="badge badge-danger">' . htmlspecialchars($lang->getLL('mfaProviders.notAvailable')) . '</span>';
666
                        break;
667
                    }
668
                    $html .= '<a href="' . htmlspecialchars((string)$uriBuilder->buildUriFromRoute('mfa')) . '" class="btn btn-' . ($hasActiveProviders ? 'default' : 'success') . '">';
669
                    $html .=    $this->iconFactory->getIcon($hasActiveProviders ? 'actions-cog' : 'actions-add', Icon::SIZE_SMALL);
670
                    $html .=    ' <span>' . htmlspecialchars($lang->getLL('mfaProviders.' . ($hasActiveProviders ? 'manageLinkTitle' : 'setupLinkTitle'))) . '</span>';
671
                    $html .= '</a>';
672
                    break;
673
                default:
674
                    $html = '';
675
            }
676
677
            $code[] = '<div class="form-section"><div class="row"><div class="form-group t3js-formengine-field-item col-md-12">' .
678
                $label .
679
                $html .
680
                '</div></div></div>';
681
        }
682
683
        $result[] = [
684
            'label' => $tabLabel,
685
            'content' => count($code) ? implode(LF, $code) : ''
686
        ];
687
        return $result;
688
    }
689
690
    /**
691
     * Return a select with available languages.
692
     * This method is called from the setup module fake TCA userFunc.
693
     *
694
     * @return string Complete select as HTML string or warning box if something went wrong.
695
     */
696
    protected function renderLanguageSelect()
697
    {
698
        $tcaConfig = $GLOBALS['TCA']['be_users']['columns']['lang']['config'];
699
        $items = $tcaConfig['items'];
700
        $itemsProcFunc = [
701
            'items' => &$items
702
        ];
703
        GeneralUtility::callUserFunction($tcaConfig['itemsProcFunc'], $itemsProcFunc);
704
        $backendUser = $this->getBackendUser();
705
        $currentSelectedLanguage = (string)($backendUser->user['lang'] ?? 'default');
706
        $languageService = $this->getLanguageService();
707
        $content = '';
708
        // get all labels in default language as well
709
        $defaultLanguageLabelService = $this->languageServiceFactory->create('default');
710
        $defaultLanguageLabelService->includeLLFile('EXT:setup/Resources/Private/Language/locallang.xlf');
711
        foreach ($items as $item) {
712
            $languageCode = $item[1];
713
            $name = $item[0];
714
            $available = in_array($languageCode, $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['lang']['availableLanguages'] ?? [], true) || is_dir(Environment::getLabelsPath() . '/' . $languageCode);
715
            if ($available || $languageCode === 'default') {
716
                $localizedName = htmlspecialchars($languageService->getLL('lang_' . $languageCode) ?: $name);
717
                $defaultName = $defaultLanguageLabelService->getLL('lang_' . $languageCode);
718
                if ($defaultName === $localizedName || $defaultName === '') {
719
                    $defaultName = $languageCode;
720
                }
721
                if ($defaultName !== $languageCode) {
722
                    $defaultName .= ' - ' . $languageCode;
723
                }
724
                $localLabel = ' [' . htmlspecialchars($defaultName) . ']';
725
                $content .= '<option value="' . $languageCode . '"' . ($currentSelectedLanguage === $languageCode ? ' selected="selected"' : '') . '>' . $localizedName . $localLabel . '</option>';
726
            }
727
        }
728
        $content = '<select aria-labelledby="label_lang" id="field_lang" name="data[be_users][lang]" class="form-select">' . $content . '</select>';
729
        if ($currentSelectedLanguage !== 'default' && !@is_dir(Environment::getLabelsPath() . '/' . $currentSelectedLanguage)) {
730
            $languageUnavailableWarning = htmlspecialchars(sprintf($languageService->getLL('languageUnavailable'), $languageService->getLL('lang_' . $currentSelectedLanguage))) . '&nbsp;&nbsp;<br />&nbsp;&nbsp;' . htmlspecialchars($languageService->getLL('languageUnavailable.' . ($backendUser->isAdmin() ? 'admin' : 'user')));
731
            $content = '<br /><span class="label label-danger">' . $languageUnavailableWarning . '</span><br /><br />' . $content;
732
        }
733
        return $content;
734
    }
735
736
    /**
737
     * Returns a select with all modules for startup.
738
     * This method is called from the setup module fake TCA userFunc.
739
     *
740
     * @return string Complete select as HTML string
741
     */
742
    public function renderStartModuleSelect()
743
    {
744
        // Load available backend modules
745
        $loadModules = GeneralUtility::makeInstance(ModuleLoader::class);
746
        $loadModules->observeWorkspaces = true;
747
        $loadModules->load($GLOBALS['TBE_MODULES']);
748
        $startModuleSelect = '<option value="">' . htmlspecialchars($this->getLanguageService()->getLL('startModule.firstInMenu')) . '</option>';
749
        foreach ($loadModules->modules as $mainMod => $modData) {
750
            $hasSubmodules = !empty($modData['sub']) && is_array($modData['sub']);
751
            $isStandalone = $modData['standalone'] ?? false;
752
            if ($hasSubmodules || $isStandalone) {
753
                $modules = '';
754
                if (($hasSubmodules)) {
755
                    foreach ($modData['sub'] as $subData) {
756
                        $modName = $subData['name'];
757
                        $modules .= '<option value="' . htmlspecialchars($modName) . '"';
758
                        $modules .= ($this->getBackendUser()->uc['startModule'] ?? '') === $modName ? ' selected="selected"' : '';
759
                        $modules .= '>' . htmlspecialchars($this->getLanguageService()->sL($loadModules->getLabelsForModule($modName)['title'])) . '</option>';
760
                    }
761
                } elseif ($isStandalone) {
762
                    $modName = $modData['name'];
763
                    $modules .= '<option value="' . htmlspecialchars($modName) . '"';
764
                    $modules .= ($this->getBackendUser()->uc['startModule'] ?? '') === $modName ? ' selected="selected"' : '';
765
                    $modules .= '>' . htmlspecialchars($this->getLanguageService()->sL($loadModules->getLabelsForModule($modName)['title'])) . '</option>';
766
                }
767
                $groupLabel = htmlspecialchars($this->getLanguageService()->sL($loadModules->getLabelsForModule($mainMod)['title']));
768
                $startModuleSelect .= '<optgroup label="' . htmlspecialchars($groupLabel) . '">' . $modules . '</optgroup>';
769
            }
770
        }
771
        return '<select id="field_startModule" aria-labelledby="label_startModule" name="data[startModule]" class="form-select">' . $startModuleSelect . '</select>';
772
    }
773
774
    /**
775
     * Returns access check (currently only "admin" is supported)
776
     *
777
     * @param array $config Configuration of the field, access mode is defined in key 'access'
778
     * @return bool Whether it is allowed to modify the given field
779
     */
780
    protected function checkAccess(array $config)
781
    {
782
        $access = $config['access'];
783
        if (isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['setup']['accessLevelCheck'][$access])) {
784
            if (class_exists($access)) {
785
                $accessObject = GeneralUtility::makeInstance($access);
786
                if (method_exists($accessObject, 'accessLevelCheck')) {
787
                    // Initialize vars. If method fails, $set will be set to FALSE
788
                    return $accessObject->accessLevelCheck($config);
789
                }
790
            }
791
        } elseif ($access === 'admin') {
792
            return $this->getBackendUser()->isAdmin();
793
        }
794
795
        return false;
796
    }
797
798
    /**
799
     * Returns the label $str from getLL() and grays out the value if the $str/$key is found in $this->overrideConf array
800
     *
801
     * @param string $str Locallang key
802
     * @param string $key Alternative override-config key
803
     * @param bool $addLabelTag Defines whether the string should be wrapped in a <label> tag.
804
     * @return string HTML output.
805
     */
806
    protected function getLabel($str, $key = '', $addLabelTag = true)
807
    {
808
        if (strpos($str, 'LLL:') === 0) {
809
            $out = htmlspecialchars($this->getLanguageService()->sL($str));
810
        } else {
811
            $out = htmlspecialchars($str);
812
        }
813
        if (isset($this->overrideConf[$key ?: $str])) {
814
            $out = '<span style="color:#999999">' . $out . '</span>';
815
        }
816
        if ($addLabelTag) {
817
            $out = '<label>' . $out . '</label>';
818
        }
819
        return $out;
820
    }
821
822
    /**
823
     * Returns the CSH Icon for given string
824
     *
825
     * @param string $str Locallang key
826
     * @param string $label The label to be used, that should be wrapped in help
827
     * @param string $fieldName field name
828
     * @return string HTML output.
829
     */
830
    protected function getCSH($str, $label, $fieldName)
831
    {
832
        $context = '_MOD_user_setup';
833
        $field = $str;
834
        $strParts = explode(':', $str);
835
        if (count($strParts) > 1) {
836
            // Setting comes from another extension
837
            $context = $strParts[0];
838
            $field = $strParts[1];
839
        } elseif ($str !== 'language' && $str !== 'reset') {
840
            $field = 'option_' . $str;
841
        }
842
        return '<span id="label_' . htmlspecialchars($fieldName) . '">' . BackendUtility::wrapInHelp($context, $field, $label) . '</span>';
843
    }
844
845
    /**
846
     * Returns array with fields defined in $GLOBALS['TYPO3_USER_SETTINGS']['showitem']
847
     * Remove fields which are disabled by user TSconfig
848
     *
849
     * @return string[] Array with field names visible in form
850
     */
851
    protected function getFieldsFromShowItem()
852
    {
853
        $allowedFields = GeneralUtility::trimExplode(',', $GLOBALS['TYPO3_USER_SETTINGS']['showitem'], true);
854
        if ($this->getBackendUser()->isAdmin()) {
855
            // Do not ask for current password if admin (unknown for other users and no security gain)
856
            $key = array_search('passwordCurrent', $allowedFields);
857
            if ($key !== false) {
858
                unset($allowedFields[$key]);
859
            }
860
        }
861
862
        $backendUser = $this->getBackendUser();
863
        $systemMaintainers = array_map('intval', $GLOBALS['TYPO3_CONF_VARS']['SYS']['systemMaintainers'] ?? []);
864
        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...
865
            // DataHandler denies changing password of system maintainer users in switch user mode.
866
            // Do not show the password fields is this case.
867
            $key = array_search('password', $allowedFields);
868
            if ($key !== false) {
869
                unset($allowedFields[$key]);
870
            }
871
            $key = array_search('password2', $allowedFields);
872
            if ($key !== false) {
873
                unset($allowedFields[$key]);
874
            }
875
        }
876
877
        if (!is_array($this->tsFieldConf)) {
0 ignored issues
show
introduced by
The condition is_array($this->tsFieldConf) is always true.
Loading history...
878
            return $allowedFields;
879
        }
880
        foreach ($this->tsFieldConf as $fieldName => $userTsFieldConfig) {
881
            if (!empty($userTsFieldConfig['disabled'])) {
882
                $fieldName = rtrim($fieldName, '.');
883
                $key = array_search($fieldName, $allowedFields);
884
                if ($key !== false) {
885
                    unset($allowedFields[$key]);
886
                }
887
            }
888
        }
889
        return $allowedFields;
890
    }
891
892
    /**
893
     * Get Avatar fileUid
894
     *
895
     * @param int $beUserId
896
     * @return int
897
     */
898
    protected function getAvatarFileUid($beUserId)
899
    {
900
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_file_reference');
901
        $file = $queryBuilder
902
            ->select('uid_local')
903
            ->from('sys_file_reference')
904
            ->where(
905
                $queryBuilder->expr()->eq(
906
                    'tablenames',
907
                    $queryBuilder->createNamedParameter('be_users', \PDO::PARAM_STR)
908
                ),
909
                $queryBuilder->expr()->eq(
910
                    'fieldname',
911
                    $queryBuilder->createNamedParameter('avatar', \PDO::PARAM_STR)
912
                ),
913
                $queryBuilder->expr()->eq(
914
                    'table_local',
915
                    $queryBuilder->createNamedParameter('sys_file', \PDO::PARAM_STR)
916
                ),
917
                $queryBuilder->expr()->eq(
918
                    'uid_foreign',
919
                    $queryBuilder->createNamedParameter($beUserId, \PDO::PARAM_INT)
920
                )
921
            )
922
            ->execute()
923
            ->fetchOne();
924
        return (int)$file;
925
    }
926
927
    /**
928
     * Set avatar fileUid for backend user
929
     *
930
     * @param int $beUserId
931
     * @param int $fileUid
932
     * @param array $storeRec
933
     */
934
    protected function setAvatarFileUid($beUserId, $fileUid, array &$storeRec)
935
    {
936
937
        // Update is only needed when new fileUid is set
938
        if ((int)$fileUid === $this->getAvatarFileUid($beUserId)) {
939
            return;
940
        }
941
942
        // If user is not allowed to modify avatar $fileUid is empty - so don't overwrite existing avatar
943
        if (empty($fileUid)) {
944
            return;
945
        }
946
947
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_file_reference');
948
        $queryBuilder->getRestrictions()->removeAll();
949
        $queryBuilder
950
            ->delete('sys_file_reference')
951
            ->where(
952
                $queryBuilder->expr()->eq(
953
                    'tablenames',
954
                    $queryBuilder->createNamedParameter('be_users', \PDO::PARAM_STR)
955
                ),
956
                $queryBuilder->expr()->eq(
957
                    'fieldname',
958
                    $queryBuilder->createNamedParameter('avatar', \PDO::PARAM_STR)
959
                ),
960
                $queryBuilder->expr()->eq(
961
                    'table_local',
962
                    $queryBuilder->createNamedParameter('sys_file', \PDO::PARAM_STR)
963
                ),
964
                $queryBuilder->expr()->eq(
965
                    'uid_foreign',
966
                    $queryBuilder->createNamedParameter($beUserId, \PDO::PARAM_INT)
967
                )
968
            )
969
            ->execute();
970
971
        // If Avatar is marked for delete => set it to empty string so it will be updated properly
972
        if ($fileUid === 'delete') {
0 ignored issues
show
introduced by
The condition $fileUid === 'delete' is always false.
Loading history...
973
            $fileUid = '';
974
        }
975
976
        // Create new reference
977
        if ($fileUid) {
978
979
            // Get file object
980
            try {
981
                $file = GeneralUtility::makeInstance(ResourceFactory::class)->getFileObject($fileUid);
982
            } catch (FileDoesNotExistException $e) {
983
                $file = false;
984
            }
985
986
            // Check if user is allowed to use the image (only when not in simulation mode)
987
            if ($file && !$file->getStorage()->checkFileActionPermission('read', $file)) {
988
                $file = false;
989
            }
990
991
            // Check if extension is allowed
992
            if ($file && $file->isImage()) {
993
994
                // Create new file reference
995
                $storeRec['sys_file_reference']['NEW1234'] = [
996
                    'uid_local' => (int)$fileUid,
997
                    'uid_foreign' => (int)$beUserId,
998
                    'tablenames' => 'be_users',
999
                    'fieldname' => 'avatar',
1000
                    'pid' => 0,
1001
                    'table_local' => 'sys_file',
1002
                ];
1003
                $storeRec['be_users'][(int)$beUserId]['avatar'] = 'NEW1234';
1004
            }
1005
        }
1006
    }
1007
1008
    /**
1009
     * Returns the current BE user.
1010
     *
1011
     * @return BackendUserAuthentication
1012
     */
1013
    protected function getBackendUser()
1014
    {
1015
        return $GLOBALS['BE_USER'];
1016
    }
1017
1018
    protected function getLanguageService(): LanguageService
1019
    {
1020
        return $GLOBALS['LANG'];
1021
    }
1022
1023
    /**
1024
     * Add FlashMessages for various actions
1025
     */
1026
    protected function addFlashMessages()
1027
    {
1028
        $flashMessages = [];
1029
1030
        // Show if setup was saved
1031
        if ($this->setupIsUpdated && !$this->settingsAreResetToDefault) {
1032
            $flashMessages[] = $this->getFlashMessage('setupWasUpdated', 'UserSettings');
1033
        }
1034
1035
        // Show if temporary data was cleared
1036
        if ($this->settingsAreResetToDefault) {
1037
            $flashMessages[] = $this->getFlashMessage('settingsAreReset', 'resetConfiguration');
1038
        }
1039
1040
        // Notice
1041
        if ($this->setupIsUpdated || $this->settingsAreResetToDefault) {
1042
            $flashMessages[] = $this->getFlashMessage('activateChanges', '', FlashMessage::INFO);
1043
        }
1044
1045
        // If password is updated, output whether it failed or was OK.
1046
        if ($this->passwordIsSubmitted) {
1047
            switch ($this->passwordIsUpdated) {
1048
                case self::PASSWORD_OLD_WRONG:
1049
                    $flashMessages[] = $this->getFlashMessage('oldPassword_failed', 'newPassword', FlashMessage::ERROR);
1050
                    break;
1051
                case self::PASSWORD_NOT_THE_SAME:
1052
                    $flashMessages[] = $this->getFlashMessage('newPassword_failed', 'newPassword', FlashMessage::ERROR);
1053
                    break;
1054
                case self::PASSWORD_UPDATED:
1055
                    $flashMessages[] = $this->getFlashMessage('newPassword_ok', 'newPassword');
1056
                    break;
1057
            }
1058
        }
1059
        if (!empty($flashMessages)) {
1060
            $this->enqueueFlashMessages($flashMessages);
1061
        }
1062
    }
1063
1064
    /**
1065
     * @param array $flashMessages
1066
     * @throws \TYPO3\CMS\Core\Exception
1067
     */
1068
    protected function enqueueFlashMessages(array $flashMessages)
1069
    {
1070
        $flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class);
1071
        $defaultFlashMessageQueue = $flashMessageService->getMessageQueueByIdentifier();
1072
        foreach ($flashMessages as $flashMessage) {
1073
            $defaultFlashMessageQueue->enqueue($flashMessage);
1074
        }
1075
    }
1076
1077
    /**
1078
     * @param string $message
1079
     * @param string $title
1080
     * @param int $severity
1081
     * @return FlashMessage
1082
     */
1083
    protected function getFlashMessage($message, $title, $severity = FlashMessage::OK)
1084
    {
1085
        $title = !empty($title) ? $this->getLanguageService()->getLL($title) : ' ';
1086
        return GeneralUtility::makeInstance(
1087
            FlashMessage::class,
1088
            $this->getLanguageService()->getLL($message),
1089
            $title,
1090
            $severity
1091
        );
1092
    }
1093
}
1094