4434ab13cc41744d0c2283b51ecde864c687060b
[Packages/TYPO3.CMS.git] / typo3 / sysext / adminpanel / Classes / Service / ConfigurationService.php
1 <?php
2 declare(strict_types = 1);
3
4 namespace TYPO3\CMS\Adminpanel\Service;
5
6 /*
7 * This file is part of the TYPO3 CMS project.
8 *
9 * It is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License, either version 2
11 * of the License, or any later version.
12 *
13 * For the full copyright and license information, please read the
14 * LICENSE.txt file that was distributed with this source code.
15 *
16 * The TYPO3 project - inspiring people to share!
17 */
18
19 use Psr\Http\Message\ServerRequestInterface;
20 use TYPO3\CMS\Adminpanel\Modules\AdminPanelModuleInterface;
21 use TYPO3\CMS\Backend\FrontendBackendUserAuthentication;
22 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
23 use TYPO3\CMS\Core\SingletonInterface;
24
25 /**
26 * Admin Panel Service Class for Configuration Handling
27 *
28 * Scope: User TSConfig + Backend User UC
29 */
30 class ConfigurationService implements SingletonInterface
31 {
32 /**
33 * @var array
34 */
35 protected $mainConfiguration;
36
37 public function __construct()
38 {
39 $this->mainConfiguration = $this->getBackendUser()->getTSConfig('admPanel')['properties'];
40 }
41
42 /**
43 * Get MainConfiguration (User TSConfig admPanel)
44 *
45 * @return array
46 */
47 public function getMainConfiguration(): array
48 {
49 return $this->mainConfiguration;
50 }
51
52 /**
53 * Helper method to return configuration options
54 * Checks User TSConfig overrides and current backend user session
55 *
56 * @param string $identifier
57 * @param string $option
58 * @return string
59 */
60 public function getConfigurationOption(string $identifier, string $option): string
61 {
62 if ($identifier === '' || $option === '') {
63 throw new \InvalidArgumentException('Identifier and option may not be empty', 1532861423);
64 }
65
66 if (isset($this->mainConfiguration['override.'][$identifier . '.'][$option])) {
67 $returnValue = $this->mainConfiguration['override.'][$identifier . '.'][$option];
68 } else {
69 $returnValue = $this->getBackendUser()->uc['AdminPanel'][$identifier . '_' . $option] ?? '';
70 }
71
72 return (string)$returnValue;
73 }
74
75 /**
76 * Save admin panel configuration to backend user UC
77 * triggers onSubmit method of modules to enable each module
78 * to enhance the save action
79 *
80 * @param AdminPanelModuleInterface[] $modules
81 * @param ServerRequestInterface $request
82 * @throws \TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException
83 */
84 public function saveConfiguration(array $modules, ServerRequestInterface $request): void
85 {
86 $configurationToSave = $request->getParsedBody()['TSFE_ADMIN_PANEL'] ?? [];
87 $beUser = $this->getBackendUser();
88
89 // Trigger onSubmit
90 foreach ($modules as $module) {
91 if ($module->isEnabled()) {
92 $module->onSubmit($configurationToSave, $request);
93 foreach ($module->getSubModules() as $subModule) {
94 $subModule->onSubmit($configurationToSave, $request);
95 }
96 }
97 }
98 // Settings
99 $beUser->uc['AdminPanel'] = array_merge(
100 !is_array($beUser->uc['AdminPanel']) ? [] : $beUser->uc['AdminPanel'],
101 $configurationToSave
102 );
103 unset($beUser->uc['AdminPanel']['action']);
104 // Saving
105 $beUser->writeUC();
106 }
107
108 /**
109 * Returns the current BE user.
110 *
111 * @return BackendUserAuthentication|FrontendBackendUserAuthentication
112 */
113 protected function getBackendUser(): BackendUserAuthentication
114 {
115 return $GLOBALS['BE_USER'];
116 }
117 }