‪TYPO3CMS  ‪main
UserInformationService.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 
21 use TYPO3\CMS\Backend\Utility\BackendUtility;
27 
33 {
34  public function ‪__construct(
35  protected readonly ‪IconFactory $iconFactory,
36  protected readonly ‪ModuleProvider $moduleProvider,
37  ) {}
38 
42  public function ‪getGroupInformation(int $groupId): array
43  {
44  $usergroupRecord = BackendUtility::getRecord('be_groups', $groupId);
45  if (!$usergroupRecord) {
46  return [];
47  }
48 
49  $user = GeneralUtility::makeInstance(BackendUserAuthentication::class);
50  $user->enablecolumns = [
51  'deleted' => true,
52  ];
53 
54  // Setup dummy user to allow fetching all group data
55  // @see \TYPO3\CMS\Core\Authentication\BackendUserAuthentication::fetchGroups
56  $user->user = [
57  'uid' => PHP_INT_MAX,
58  'options' => 3,
59  // The below admin flag is required to prevent workspace access checks,
60  // triggered by workspaceInit() in fetchGroupData(). Those would fail
61  // due to insufficient permissions of the dummy user and therefore might
62  // result in generating superfluous log entries.
63  'admin' => 1,
64  'workspace_id' => 0,
65  'realName' => 'fakeUser',
66  'email' => 'fake.user@typo3.org',
67  'TSconfig' => '',
68  'category_perms' => '',
69  $user->usergroup_column => $groupId,
70  ];
71  $user->fetchGroupData();
72 
73  $data = $this->‪convert($user);
74  $data['group'] = $usergroupRecord;
75 
76  return $data;
77  }
78 
82  public function ‪getUserInformation(int $userId): array
83  {
84  $user = GeneralUtility::makeInstance(BackendUserAuthentication::class);
85  $user->enablecolumns = [
86  'deleted' => true,
87  ];
88  $user->setBeUserByUid($userId);
89  if (!$user->user) {
90  return [];
91  }
92  $user->fetchGroupData();
93 
94  return $this->‪convert($user);
95  }
96 
101  protected function ‪convert(‪BackendUserAuthentication $user): array
102  {
103  // usergroups
104  $data = [
105  'user' => $user->user ?? [],
106  'groups' => [
107  'inherit' => $user->userGroupsUID,
108  'direct' => ‪GeneralUtility::trimExplode(',', (string)($user->user['usergroup'] ?? ''), true),
109  ],
110  'modules' => [],
111  ];
112  $data['groups']['diff'] = array_diff($data['groups']['inherit'], $data['groups']['direct']);
113  foreach ($data['groups'] as $type => $groups) {
114  foreach ($groups as $key => $id) {
115  ‪$record = BackendUtility::getRecord('be_groups', (int)$id);
116  if (isset(‪$record['uid'])) {
117  $recordId = ‪$record['uid'];
118  $data['groups']['all'][$recordId]['row'] = ‪$record;
119  $data['groups']['all'][$recordId][$type] = 1;
120  }
121  }
122  }
123 
124  // languages
125  $siteLanguages = $this->‪getAllSiteLanguages();
126  $userLanguages = ‪GeneralUtility::trimExplode(',', $user->groupData['allowed_languages'] ?? '', true);
127  asort($userLanguages);
128  foreach ($userLanguages as $languageId) {
129  $languageId = (int)$languageId;
130  ‪$record = $siteLanguages[$languageId];
131  if (‪$record) {
132  $data['languages'][$languageId] = ‪$record;
133  }
134  }
135 
136  // table permissions
137  $data['tables']['tables_select'] = [];
138  $data['tables']['tables_modify'] = [];
139  foreach (['tables_select', 'tables_modify'] as $tableField) {
140  $temp = ‪GeneralUtility::trimExplode(',', $user->groupData[$tableField] ?? '', true);
141  foreach ($temp as $tableName) {
142  if (isset(‪$GLOBALS['TCA'][$tableName]['ctrl']['title'])) {
143  $data['tables'][$tableField][$tableName] = ‪$GLOBALS['TCA'][$tableName]['ctrl']['title'];
144  }
145  }
146  }
147  $data['tables']['all'] = array_replace($data['tables']['tables_select'] ?? [], $data['tables']['tables_modify'] ?? []);
148 
149  // DB mounts
150  $dbMounts = ‪GeneralUtility::trimExplode(',', $user->groupData['webmounts'] ?? '', true);
151  asort($dbMounts);
152  foreach ($dbMounts as $mount) {
153  ‪$record = BackendUtility::getRecord('pages', (int)$mount);
154  if (‪$record) {
155  $data['dbMounts'][] = ‪$record;
156  }
157  }
158 
159  // File mounts
160  $fileMounts = ‪GeneralUtility::trimExplode(',', $user->groupData['filemounts'] ?? '', true);
161  asort($fileMounts);
162  foreach ($fileMounts as $mount) {
163  ‪$record = BackendUtility::getRecord('sys_filemounts', (int)$mount);
164  if (‪$record) {
165  $data['fileMounts'][] = ‪$record;
166  }
167  }
168 
169  // Modules
170  $modules = ‪GeneralUtility::trimExplode(',', $user->groupData['modules'] ?? '', true);
171  foreach ($modules as $moduleIdentifier) {
172  if ($this->moduleProvider->isModuleRegistered($moduleIdentifier)) {
173  $data['modules'][] = $this->moduleProvider->getModule($moduleIdentifier);
174  }
175  }
176 
177  // Categories
178  $categories = $user->‪getCategoryMountPoints();
179  foreach ($categories as $category) {
180  ‪$record = BackendUtility::getRecord('sys_category', $category);
181  if (‪$record) {
182  $data['categories'][$category] = ‪$record;
183  }
184  }
185 
186  // workspaces
187  if (‪ExtensionManagementUtility::isLoaded('workspaces')) {
188  $data['workspaces'] = [
189  'loaded' => true,
190  'record' => $user->workspaceRec,
191  ];
192  }
193 
194  // file & folder permissions
195  $filePermissions = $user->groupData['file_permissions'] ?? '';
196  if ($filePermissions) {
197  $items = ‪GeneralUtility::trimExplode(',', $filePermissions, true);
198  foreach (‪$GLOBALS['TCA']['be_groups']['columns']['file_permissions']['config']['items'] as $availableItem) {
199  if (in_array($availableItem['value'], $items, true)) {
200  $data['fileFolderPermissions'][] = $availableItem;
201  }
202  }
203  }
204 
205  // tsconfig
206  $data['tsconfig'] = $user->‪getTSConfig();
207 
208  // non_exclude_fields
209  $fieldListTmp = ‪GeneralUtility::trimExplode(',', $user->groupData['non_exclude_fields'] ?? '', true);
210  $fieldList = [];
211  foreach ($fieldListTmp as $item) {
212  $itemParts = explode(':', $item);
213  $itemTable = $itemParts[0];
214  $itemField = $itemParts[1] ?? '';
215  if (!empty($itemField) && isset(‪$GLOBALS['TCA'][$itemTable]['ctrl']['title'])) {
216  $fieldList[$itemTable]['label'] = ‪$GLOBALS['TCA'][$itemTable]['ctrl']['title'];
217  $fieldList[$itemTable]['fields'][$itemField] = ‪$GLOBALS['TCA'][$itemTable]['columns'][$itemField]['label'] ?? $itemField;
218  }
219  }
220  ksort($fieldList);
221  foreach ($fieldList as &$fieldListItem) {
222  ksort($fieldListItem['fields']);
223  }
224  $data['non_exclude_fields'] = $fieldList;
225 
226  // page types
227  $specialItems = ‪$GLOBALS['TCA']['pages']['columns']['doktype']['config']['items'];
228  foreach ($specialItems as $specialItem) {
229  $value = $specialItem['value'];
230  if (!‪GeneralUtility::inList($user->groupData['pagetypes_select'] ?? '', $value)) {
231  continue;
232  }
233  $label = $specialItem['label'];
234  $icon = $specialItem['icon'] ?? 'apps-pagetree-page-default';
235  $data['pageTypes'][] = ['label' => $label, 'value' => $value, 'icon' => $icon];
236  }
237 
238  return $data;
239  }
240 
241  protected function ‪getAllSiteLanguages(): array
242  {
243  $siteLanguages = [];
244  foreach (GeneralUtility::makeInstance(SiteFinder::class)->getAllSites() as $site) {
245  foreach ($site->getAllLanguages() as $languageId => $language) {
246  if (isset($siteLanguages[$languageId])) {
247  // Language already provided by another site, check if values differ
248  if (!str_contains($siteLanguages[$languageId]['title'], $language->getTitle())) {
249  // Language already provided by another site, but with a different title
250  $siteLanguages[$languageId]['title'] .= ', ' . $language->getTitle();
251  }
252  if ($siteLanguages[$languageId]['flagIconIdentifier'] !== $language->getFlagIdentifier()) {
253  // Language already provided by another site, but with a different flag icon identifier
254  $siteLanguages[$languageId]['flagIconIdentifier'] = 'flags-multiple';
255  }
256  } else {
257  $siteLanguages[$languageId] = [
258  'title' => $language->getTitle(),
259  'flagIconIdentifier' => $language->getFlagIdentifier(),
260  ];
261  }
262  }
263  }
264  return $siteLanguages;
265  }
266 }
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication\getTSConfig
‪array getTSConfig()
Definition: BackendUserAuthentication.php:850
‪TYPO3\CMS\Core\Site\SiteFinder
Definition: SiteFinder.php:31
‪TYPO3\CMS\Beuser\Service\UserInformationService\getAllSiteLanguages
‪getAllSiteLanguages()
Definition: UserInformationService.php:241
‪TYPO3\CMS\Core\Imaging\IconFactory
Definition: IconFactory.php:34
‪TYPO3\CMS\Backend\Module\ModuleProvider
Definition: ModuleProvider.php:29
‪TYPO3\CMS\Core\Utility\ExtensionManagementUtility\isLoaded
‪static isLoaded(string $key)
Definition: ExtensionManagementUtility.php:55
‪TYPO3\CMS\Core\Utility\ExtensionManagementUtility
Definition: ExtensionManagementUtility.php:32
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication\getCategoryMountPoints
‪array getCategoryMountPoints()
Definition: BackendUserAuthentication.php:1174
‪TYPO3\CMS\Beuser\Service\UserInformationService\convert
‪convert(BackendUserAuthentication $user)
Definition: UserInformationService.php:101
‪TYPO3\CMS\Beuser\Service\UserInformationService\__construct
‪__construct(protected readonly IconFactory $iconFactory, protected readonly ModuleProvider $moduleProvider,)
Definition: UserInformationService.php:34
‪TYPO3\CMS\Webhooks\Message\$record
‪identifier readonly int readonly array $record
Definition: PageModificationMessage.php:36
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:61
‪TYPO3\CMS\Beuser\Service\UserInformationService\getGroupInformation
‪getGroupInformation(int $groupId)
Definition: UserInformationService.php:42
‪TYPO3\CMS\Beuser\Service
Definition: UserInformationService.php:18
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Beuser\Service\UserInformationService\getUserInformation
‪getUserInformation(int $userId)
Definition: UserInformationService.php:82
‪TYPO3\CMS\Core\Utility\GeneralUtility\inList
‪static bool inList($list, $item)
Definition: GeneralUtility.php:422
‪TYPO3\CMS\Beuser\Service\UserInformationService
Definition: UserInformationService.php:33
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Utility\GeneralUtility\trimExplode
‪static list< string > trimExplode(string $delim, string $string, bool $removeEmptyValues=false, int $limit=0)
Definition: GeneralUtility.php:817