UserInformationService.php 7.71 KB
Newer Older
1
<?php
2

3
declare(strict_types=1);
4
5
6
7
8
9
10
11
12
13
14
15
16
17

/*
 * This file is part of the TYPO3 CMS project.
 *
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
 *
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
 *
 * The TYPO3 project - inspiring people to share!
 */

18
19
namespace TYPO3\CMS\Beuser\Service;

20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
use TYPO3\CMS\Core\Imaging\IconFactory;
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;

/**
 * Transform information of user and groups into better format
 * @internal
 */
class UserInformationService
{

    /**
     * @var IconFactory
     */
    protected $iconFactory;

38
    public function __construct(IconFactory $iconFactory)
39
    {
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
        $this->iconFactory = $iconFactory;
    }

    /**
     * Get all relevant information for a backend usergroup
     * @param int $groupId
     * @return array
     */
    public function getGroupInformation(int $groupId): array
    {
        $usergroupRecord = BackendUtility::getRecord('be_groups', $groupId);
        if (!$usergroupRecord) {
            return [];
        }

        $user = GeneralUtility::makeInstance(BackendUserAuthentication::class);
        $user->enablecolumns = [
            'deleted' => true
        ];
        // Setup dummy user to allow fetching all group data
        // @see \TYPO3\CMS\Core\Authentication\BackendUserAuthentication::fetchGroups
        $user->user = [
            'uid' => 3016,
            'options' => 3,
            $user->usergroup_column => $groupId
        ];
        $user->fetchGroupData();

        $data = $this->convert($user);
        $data['group'] = $usergroupRecord;

        return $data;
72
73
74
75
76
77
78
79
    }

    /**
     * Get all relevant information of the user
     *
     * @param int $userId
     * @return array
     */
80
    public function getUserInformation(int $userId): array
81
82
    {
        $user = GeneralUtility::makeInstance(BackendUserAuthentication::class);
83
84
85
        $user->enablecolumns = [
            'deleted' => true
        ];
86
        $user->setBeUserByUid($userId);
87
88
89
        if (!$user->user) {
            return [];
        }
90
91
        $user->fetchGroupData();

92
93
94
95
96
97
98
99
100
101
102
103
        return $this->convert($user);
    }

    /**
     * Convert hard readable user & group information into structured
     * data which can be rendered later
     *
     * @param BackendUserAuthentication $user
     * @return array
     */
    protected function convert(BackendUserAuthentication $user): array
    {
104
105
106
107
        // usergroups
        $data = [
            'user' => $user->user ?? [],
            'groups' => [
108
                'inherit' => $user->userGroupsUID,
109
110
111
112
113
114
                'direct' => GeneralUtility::trimExplode(',', $user->user['usergroup'], true),
            ],
        ];
        $data['groups']['diff'] = array_diff($data['groups']['inherit'], $data['groups']['direct']);
        foreach ($data['groups'] as $type => $groups) {
            foreach ($groups as $key => $id) {
115
                $record = BackendUtility::getRecord('be_groups', (int)$id);
116
117
118
119
                if ($record) {
                    $data['groups']['all'][$record['uid']]['row'] = $record;
                    $data['groups']['all'][$record['uid']][$type] = 1;
                }
120
121
122
123
            }
        }

        // languages
124
        $languages = GeneralUtility::trimExplode(',', $user->groupData['allowed_languages'], true);
125
126
        asort($languages);
        foreach ($languages as $language) {
127
            $record = BackendUtility::getRecord('sys_language', (int)$language);
128
129
130
            if ($record) {
                $data['languages'][$language] = $record;
            }
131
132
133
        }

        // table permissions
134
135
        $data['tables']['tables_select'] = [];
        $data['tables']['tables_modify'] = [];
136
        foreach (['tables_select', 'tables_modify'] as $tableField) {
137
            $temp = GeneralUtility::trimExplode(',', $user->groupData[$tableField], true);
138
139
140
141
142
143
144
            foreach ($temp as $tableName) {
                $data['tables'][$tableField][$tableName] = $GLOBALS['TCA'][$tableName]['ctrl']['title'];
            }
        }
        $data['tables']['all'] = array_replace($data['tables']['tables_select'] ?? [], $data['tables']['tables_modify'] ?? []);

        // DB mounts
145
        $dbMounts = GeneralUtility::trimExplode(',', $user->groupData['webmounts'], true);
146
147
        asort($dbMounts);
        foreach ($dbMounts as $mount) {
148
            $record = BackendUtility::getRecord('pages', (int)$mount);
149
150
151
152
153
154
            if ($record) {
                $data['dbMounts'][] = $record;
            }
        }

        // File mounts
155
        $fileMounts = GeneralUtility::trimExplode(',', $user->groupData['filemounts'], true);
156
157
        asort($fileMounts);
        foreach ($fileMounts as $mount) {
158
            $record = BackendUtility::getRecord('sys_filemounts', (int)$mount);
159
160
161
            if ($record) {
                $data['fileMounts'][] = $record;
            }
162
163
164
        }

        // Modules
165
        $modules = GeneralUtility::trimExplode(',', $user->groupData['modules'], true);
166
167
168
        foreach ($modules as $module) {
            $data['modules'][$module] = $GLOBALS['TBE_MODULES']['_configuration'][$module];
        }
169
        $data['modules'] = array_filter((array)$data['modules']);
170
171

        // Categories
172
        $categories = $user->getCategoryMountPoints();
173
        foreach ($categories as $category) {
174
175
176
177
            $record = BackendUtility::getRecord('sys_category', $category);
            if ($record) {
                $data['categories'][$category] = $record;
            }
178
179
180
181
182
        }

        // workspaces
        if (ExtensionManagementUtility::isLoaded('workspaces')) {
            $data['workspaces'] = [
183
184
                'loaded' => true,
                'record' => $user->workspaceRec
185
186
187
            ];
        }

188
        // file & folder permissions
189
        if ($filePermissions = $user->groupData['file_permissions']) {
190
191
192
193
194
195
196
197
198
199
200
            $items = GeneralUtility::trimExplode(',', $filePermissions, true);
            foreach ($GLOBALS['TCA']['be_groups']['columns']['file_permissions']['config']['items'] as $availableItem) {
                if (in_array($availableItem[1], $items, true)) {
                    $data['fileFolderPermissions'][] = $availableItem;
                }
            }
        }

        // tsconfig
        $data['tsconfig'] = $user->getTSConfig();

201
        // non_exclude_fields
202
        $fieldListTmp = GeneralUtility::trimExplode(',', $user->groupData['non_exclude_fields'], true);
203
204
205
206
207
208
        $fieldList = [];
        foreach ($fieldListTmp as $item) {
            $split = explode(':', $item);
            $fieldList[$split[0]]['label'] = $GLOBALS['TCA'][$split[0]]['ctrl']['title'];
            $fieldList[$split[0]]['fields'][$split[1]] = $GLOBALS['TCA'][$split[0]]['columns'][$split[1]]['label'] ?? $split[1];
        }
209
210
211
212
        ksort($fieldList);
        foreach ($fieldList as &$fieldListItem) {
            ksort($fieldListItem['fields']);
        }
213
214
        $data['non_exclude_fields'] = $fieldList;

215
        // page types
216
217
218
        $specialItems = $GLOBALS['TCA']['pages']['columns']['doktype']['config']['items'];
        foreach ($specialItems as $specialItem) {
            $value = $specialItem[1];
219
            if (!GeneralUtility::inList($user->groupData['pagetypes_select'], $value)) {
220
221
222
223
224
225
226
227
228
229
                continue;
            }
            $label = $specialItem[0];
            $icon = $this->iconFactory->mapRecordTypeToIconIdentifier('pages', ['doktype' => $specialItem[1]]);
            $data['pageTypes'][] = ['label' => $label, 'value' => $value, 'icon' => $icon];
        }

        return $data;
    }
}