‪TYPO3CMS  11.5
UserSettingsController.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Psr\Http\Message\ResponseInterface;
21 use Psr\Http\Message\ServerRequestInterface;
25 
32 {
33  private const ‪ALLOWED_ACTIONS = [
34  'GET' => ['get', 'getAll'],
35  'POST' => ['set', 'addToList', 'removeFromList', 'unset', 'clear'],
36  ];
37 
44  public function ‪processAjaxRequest(ServerRequestInterface $request): ResponseInterface
45  {
46  // do the regular / main logic, depending on the action parameter
47  $action = $this->‪getValidActionFromRequest($request);
48 
49  $key = $request->getParsedBody()['key'] ?? $request->getQueryParams()['key'] ?? '';
50  $value = $request->getParsedBody()['value'] ?? $request->getQueryParams()['value'] ?? '';
51  $backendUserConfiguration = GeneralUtility::makeInstance(BackendUserConfiguration::class);
52  switch ($action) {
53  case 'get':
54  $content = $backendUserConfiguration->get($key);
55  break;
56  case 'getAll':
57  $content = $backendUserConfiguration->getAll();
58  break;
59  case 'set':
60  $backendUserConfiguration->set($key, $value);
61  $content = $backendUserConfiguration->getAll();
62  break;
63  case 'addToList':
64  $backendUserConfiguration->addToList($key, $value);
65  $content = $backendUserConfiguration->getAll();
66  break;
67  case 'removeFromList':
68  $backendUserConfiguration->removeFromList($key, $value);
69  $content = $backendUserConfiguration->getAll();
70  break;
71  case 'unset':
72  $backendUserConfiguration->unsetOption($key);
73  $content = $backendUserConfiguration->getAll();
74  break;
75  case 'clear':
76  $backendUserConfiguration->clear();
77  $content = ['result' => true];
78  break;
79  default:
80  $content = ['result' => false];
81  }
82  return new ‪JsonResponse($content);
83  }
84 
85  protected function ‪getValidActionFromRequest(ServerRequestInterface $request): string
86  {
87  $action = $request->getParsedBody()['action'] ?? $request->getQueryParams()['action'] ?? '';
88  return in_array($action, (self::ALLOWED_ACTIONS[$request->getMethod()] ?? []), true) ? $action : '';
89  }
90 }
‪TYPO3\CMS\Backend\Controller\UserSettingsController\processAjaxRequest
‪ResponseInterface processAjaxRequest(ServerRequestInterface $request)
Definition: UserSettingsController.php:44
‪TYPO3\CMS\Backend\Controller\UserSettingsController
Definition: UserSettingsController.php:32
‪TYPO3\CMS\Backend\Controller\UserSettingsController\getValidActionFromRequest
‪getValidActionFromRequest(ServerRequestInterface $request)
Definition: UserSettingsController.php:85
‪TYPO3\CMS\Backend\Controller\UserSettingsController\ALLOWED_ACTIONS
‪const ALLOWED_ACTIONS
Definition: UserSettingsController.php:33
‪TYPO3\CMS\Backend\Configuration\BackendUserConfiguration
Definition: BackendUserConfiguration.php:30
‪TYPO3\CMS\Core\Http\JsonResponse
Definition: JsonResponse.php:28
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Backend\Controller
Definition: AboutController.php:16