‪TYPO3CMS  ‪main
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 
41  public function ‪processAjaxRequest(ServerRequestInterface $request): ResponseInterface
42  {
43  // do the regular / main logic, depending on the action parameter
44  $action = $this->‪getValidActionFromRequest($request);
45 
46  $key = $request->getParsedBody()['key'] ?? $request->getQueryParams()['key'] ?? '';
47  $value = $request->getParsedBody()['value'] ?? $request->getQueryParams()['value'] ?? '';
48  $backendUserConfiguration = GeneralUtility::makeInstance(BackendUserConfiguration::class);
49  switch ($action) {
50  case 'get':
51  $content = $backendUserConfiguration->get($key);
52  break;
53  case 'getAll':
54  $content = $backendUserConfiguration->getAll();
55  break;
56  case 'set':
57  $backendUserConfiguration->set($key, $value);
58  $content = $backendUserConfiguration->getAll();
59  break;
60  case 'addToList':
61  $backendUserConfiguration->addToList($key, $value);
62  $content = $backendUserConfiguration->getAll();
63  break;
64  case 'removeFromList':
65  $backendUserConfiguration->removeFromList($key, $value);
66  $content = $backendUserConfiguration->getAll();
67  break;
68  case 'unset':
69  $backendUserConfiguration->unsetOption($key);
70  $content = $backendUserConfiguration->getAll();
71  break;
72  case 'clear':
73  $backendUserConfiguration->clear();
74  $content = ['result' => true];
75  break;
76  default:
77  $content = ['result' => false];
78  }
79  return new ‪JsonResponse($content);
80  }
81 
82  protected function ‪getValidActionFromRequest(ServerRequestInterface $request): string
83  {
84  $action = $request->getParsedBody()['action'] ?? $request->getQueryParams()['action'] ?? '';
85  return in_array($action, (self::ALLOWED_ACTIONS[$request->getMethod()] ?? []), true) ? $action : '';
86  }
87 }
‪TYPO3\CMS\Backend\Controller\UserSettingsController
Definition: UserSettingsController.php:32
‪TYPO3\CMS\Backend\Controller\UserSettingsController\getValidActionFromRequest
‪getValidActionFromRequest(ServerRequestInterface $request)
Definition: UserSettingsController.php:82
‪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:52
‪TYPO3\CMS\Backend\Controller
Definition: AboutController.php:18
‪TYPO3\CMS\Backend\Controller\UserSettingsController\processAjaxRequest
‪processAjaxRequest(ServerRequestInterface $request)
Definition: UserSettingsController.php:41