Completed
Push — master ( 19fed6...e690ae )
by
unknown
14:30
created

UserInformationService::getGroupInformation()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 14
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 24
rs 9.7998
1
<?php
2
declare(strict_types = 1);
3
4
namespace TYPO3\CMS\Beuser\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 TYPO3\CMS\Backend\Utility\BackendUtility;
20
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
21
use TYPO3\CMS\Core\Imaging\IconFactory;
22
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
23
use TYPO3\CMS\Core\Utility\GeneralUtility;
24
25
/**
26
 * Transform information of user and groups into better format
27
 * @internal
28
 */
29
class UserInformationService
30
{
31
32
    /**
33
     * @var IconFactory
34
     */
35
    protected $iconFactory;
36
37
    public function __construct(IconFactory $iconFactory)
38
    {
39
        $this->iconFactory = $iconFactory;
40
    }
41
42
    /**
43
     * Get all relevant information for a backend usergroup
44
     * @param int $groupId
45
     * @return array
46
     */
47
    public function getGroupInformation(int $groupId): array
48
    {
49
        $usergroupRecord = BackendUtility::getRecord('be_groups', $groupId);
50
        if (!$usergroupRecord) {
51
            return [];
52
        }
53
54
        $user = GeneralUtility::makeInstance(BackendUserAuthentication::class);
55
        $user->enablecolumns = [
56
            'deleted' => true
57
        ];
58
        // Setup dummy user to allow fetching all group data
59
        // @see \TYPO3\CMS\Core\Authentication\BackendUserAuthentication::fetchGroups
60
        $user->user = [
61
            'uid' => 3016,
62
            'options' => 3,
63
            $user->usergroup_column => $groupId
64
        ];
65
        $user->fetchGroupData();
66
67
        $data = $this->convert($user);
68
        $data['group'] = $usergroupRecord;
69
70
        return $data;
71
    }
72
73
    /**
74
     * Get all relevant information of the user
75
     *
76
     * @param int $userId
77
     * @return array
78
     */
79
    public function getUserInformation(int $userId): array
80
    {
81
        $user = GeneralUtility::makeInstance(BackendUserAuthentication::class);
82
        $user->enablecolumns = [
83
            'deleted' => true
84
        ];
85
        $user->setBeUserByUid($userId);
86
        if (!$user->user) {
87
            return [];
88
        }
89
        $user->fetchGroupData();
90
91
        return $this->convert($user);
92
    }
93
94
    /**
95
     * Convert hard readable user & group information into structured
96
     * data which can be rendered later
97
     *
98
     * @param BackendUserAuthentication $user
99
     * @return array
100
     */
101
    protected function convert(BackendUserAuthentication $user): array
102
    {
103
        // usergroups
104
        $data = [
105
            'user' => $user->user ?? [],
106
            'groups' => [
107
                'inherit' => GeneralUtility::trimExplode(',', $user->groupList, true),
108
                'direct' => GeneralUtility::trimExplode(',', $user->user['usergroup'], true),
109
            ],
110
        ];
111
        $data['groups']['diff'] = array_diff($data['groups']['inherit'], $data['groups']['direct']);
112
        foreach ($data['groups'] as $type => $groups) {
113
            foreach ($groups as $key => $id) {
114
                $record = BackendUtility::getRecord('be_groups', $id);
115
                if ($record) {
116
                    $data['groups']['all'][$record['uid']]['row'] = $record;
117
                    $data['groups']['all'][$record['uid']][$type] = 1;
118
                }
119
            }
120
        }
121
122
        // languages
123
        $languages = GeneralUtility::trimExplode(',', $user->dataLists['allowed_languages'], true);
124
        asort($languages);
125
        foreach ($languages as $language) {
126
            $record = BackendUtility::getRecord('sys_language', $language);
127
            if ($record) {
128
                $data['languages'][$language] = $record;
129
            }
130
        }
131
132
        // table permissions
133
        $data['tables']['tables_select'] = [];
134
        $data['tables']['tables_modify'] = [];
135
        foreach (['tables_select', 'tables_modify'] as $tableField) {
136
            $temp = GeneralUtility::trimExplode(',', $user->dataLists[$tableField], true);
137
            foreach ($temp as $tableName) {
138
                $data['tables'][$tableField][$tableName] = $GLOBALS['TCA'][$tableName]['ctrl']['title'];
139
            }
140
        }
141
        $data['tables']['all'] = array_replace($data['tables']['tables_select'] ?? [], $data['tables']['tables_modify'] ?? []);
142
143
        // DB mounts
144
        $dbMounts = GeneralUtility::trimExplode(',', $user->dataLists['webmount_list'], true);
145
        asort($dbMounts);
146
        foreach ($dbMounts as $mount) {
147
            $record = BackendUtility::getRecord('pages', $mount);
148
            if ($record) {
149
                $data['dbMounts'][] = $record;
150
            }
151
        }
152
153
        // File mounts
154
        $fileMounts = GeneralUtility::trimExplode(',', $user->dataLists['filemount_list'], true);
155
        asort($fileMounts);
156
        foreach ($fileMounts as $mount) {
157
            $record = BackendUtility::getRecord('sys_filemounts', $mount);
158
            if ($record) {
159
                $data['fileMounts'][] = $record;
160
            }
161
        }
162
163
        // Modules
164
        $modules = GeneralUtility::trimExplode(',', $user->dataLists['modList'], true);
165
        foreach ($modules as $module) {
166
            $data['modules'][$module] = $GLOBALS['TBE_MODULES']['_configuration'][$module];
167
        }
168
        $data['modules'] = array_filter((array)$data['modules']);
169
170
        // Categories
171
        $categories = $user->getCategoryMountPoints();
172
        foreach ($categories as $category) {
173
            $record = BackendUtility::getRecord('sys_category', $category);
174
            if ($record) {
175
                $data['categories'][$category] = $record;
176
            }
177
        }
178
179
        // workspaces
180
        if (ExtensionManagementUtility::isLoaded('workspaces')) {
181
            $data['workspaces'] = [
182
                'loaded' => true,
183
                'record' => $user->workspaceRec
184
            ];
185
        }
186
187
        // file & folder permissions
188
        if ($filePermissions = $user->dataLists['file_permissions']) {
189
            $items = GeneralUtility::trimExplode(',', $filePermissions, true);
190
            foreach ($GLOBALS['TCA']['be_groups']['columns']['file_permissions']['config']['items'] as $availableItem) {
191
                if (in_array($availableItem[1], $items, true)) {
192
                    $data['fileFolderPermissions'][] = $availableItem;
193
                }
194
            }
195
        }
196
197
        // tsconfig
198
        $data['tsconfig'] = $user->getTSConfig();
199
200
        // non_exclude_fields
201
        $fieldListTmp = GeneralUtility::trimExplode(',', $user->dataLists['non_exclude_fields'], true);
202
        $fieldList = [];
203
        foreach ($fieldListTmp as $item) {
204
            $split = explode(':', $item);
205
            $fieldList[$split[0]]['label'] = $GLOBALS['TCA'][$split[0]]['ctrl']['title'];
206
            $fieldList[$split[0]]['fields'][$split[1]] = $GLOBALS['TCA'][$split[0]]['columns'][$split[1]]['label'] ?? $split[1];
207
        }
208
        ksort($fieldList);
209
        foreach ($fieldList as &$fieldListItem) {
210
            ksort($fieldListItem['fields']);
211
        }
212
        $data['non_exclude_fields'] = $fieldList;
213
214
        // page types
215
        $specialItems = $GLOBALS['TCA']['pages']['columns']['doktype']['config']['items'];
216
        foreach ($specialItems as $specialItem) {
217
            $value = $specialItem[1];
218
            if (!GeneralUtility::inList($user->dataLists['pagetypes_select'], $value)) {
219
                continue;
220
            }
221
            $label = $specialItem[0];
222
            $icon = $this->iconFactory->mapRecordTypeToIconIdentifier('pages', ['doktype' => $specialItem[1]]);
223
            $data['pageTypes'][] = ['label' => $label, 'value' => $value, 'icon' => $icon];
224
        }
225
226
        return $data;
227
    }
228
}
229