UserInformationService::convert()   F
last analyzed

Complexity

Conditions 23
Paths > 20000

Size

Total Lines 129
Code Lines 78

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 78
c 0
b 0
f 0
dl 0
loc 129
rs 0
cc 23
nc 93312
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
18
namespace TYPO3\CMS\Beuser\Service;
19
20
use TYPO3\CMS\Backend\Utility\BackendUtility;
21
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
22
use TYPO3\CMS\Core\Imaging\IconFactory;
23
use TYPO3\CMS\Core\Site\SiteFinder;
24
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
25
use TYPO3\CMS\Core\Utility\GeneralUtility;
26
27
/**
28
 * Transform information of user and groups into better format
29
 * @internal
30
 */
31
class UserInformationService
32
{
33
34
    /**
35
     * @var IconFactory
36
     */
37
    protected $iconFactory;
38
39
    public function __construct(IconFactory $iconFactory)
40
    {
41
        $this->iconFactory = $iconFactory;
42
    }
43
44
    /**
45
     * Get all relevant information for a backend usergroup
46
     * @param int $groupId
47
     * @return array
48
     */
49
    public function getGroupInformation(int $groupId): array
50
    {
51
        $usergroupRecord = BackendUtility::getRecord('be_groups', $groupId);
52
        if (!$usergroupRecord) {
53
            return [];
54
        }
55
56
        $user = GeneralUtility::makeInstance(BackendUserAuthentication::class);
57
        $user->enablecolumns = [
58
            'deleted' => true
59
        ];
60
        // Setup dummy user to allow fetching all group data
61
        // @see \TYPO3\CMS\Core\Authentication\BackendUserAuthentication::fetchGroups
62
        $user->user = [
63
            'uid' => PHP_INT_MAX,
64
            'options' => 3,
65
            'workspace_id' => -99,
66
            $user->usergroup_column => $groupId
67
        ];
68
        $user->fetchGroupData();
69
70
        $data = $this->convert($user);
71
        $data['group'] = $usergroupRecord;
72
73
        return $data;
74
    }
75
76
    /**
77
     * Get all relevant information of the user
78
     *
79
     * @param int $userId
80
     * @return array
81
     */
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
97
    /**
98
     * Convert hard readable user & group information into structured
99
     * data which can be rendered later
100
     *
101
     * @param BackendUserAuthentication $user
102
     * @return array
103
     */
104
    protected function convert(BackendUserAuthentication $user): array
105
    {
106
        // usergroups
107
        $data = [
108
            'user' => $user->user ?? [],
109
            'groups' => [
110
                'inherit' => $user->userGroupsUID,
111
                'direct' => GeneralUtility::trimExplode(',', $user->user['usergroup'], true),
112
            ],
113
            'modules' => [],
114
        ];
115
        $data['groups']['diff'] = array_diff($data['groups']['inherit'], $data['groups']['direct']);
116
        foreach ($data['groups'] as $type => $groups) {
117
            foreach ($groups as $key => $id) {
118
                $record = BackendUtility::getRecord('be_groups', (int)$id);
119
                if ($record) {
120
                    $data['groups']['all'][$record['uid']]['row'] = $record;
121
                    $data['groups']['all'][$record['uid']][$type] = 1;
122
                }
123
            }
124
        }
125
126
        // languages
127
        $siteLanguages = $this->getAllSiteLanguages();
128
        $userLanguages = GeneralUtility::trimExplode(',', $user->groupData['allowed_languages'], true);
129
        asort($userLanguages);
130
        foreach ($userLanguages as $languageId) {
131
            $languageId = (int)$languageId;
132
            $record = $siteLanguages[$languageId];
133
            if ($record) {
134
                $data['languages'][$languageId] = $record;
135
            }
136
        }
137
138
        // table permissions
139
        $data['tables']['tables_select'] = [];
140
        $data['tables']['tables_modify'] = [];
141
        foreach (['tables_select', 'tables_modify'] as $tableField) {
142
            $temp = GeneralUtility::trimExplode(',', $user->groupData[$tableField], true);
143
            foreach ($temp as $tableName) {
144
                $data['tables'][$tableField][$tableName] = $GLOBALS['TCA'][$tableName]['ctrl']['title'];
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 $module) {
172
            $data['modules'][$module] = $GLOBALS['TBE_MODULES']['_configuration'][$module];
173
        }
174
        $data['modules'] = array_filter($data['modules']);
175
176
        // Categories
177
        $categories = $user->getCategoryMountPoints();
178
        foreach ($categories as $category) {
179
            $record = BackendUtility::getRecord('sys_category', $category);
180
            if ($record) {
181
                $data['categories'][$category] = $record;
182
            }
183
        }
184
185
        // workspaces
186
        if (ExtensionManagementUtility::isLoaded('workspaces')) {
187
            $data['workspaces'] = [
188
                'loaded' => true,
189
                'record' => $user->workspaceRec
190
            ];
191
        }
192
193
        // file & folder permissions
194
        if ($filePermissions = $user->groupData['file_permissions']) {
195
            $items = GeneralUtility::trimExplode(',', $filePermissions, true);
196
            foreach ($GLOBALS['TCA']['be_groups']['columns']['file_permissions']['config']['items'] as $availableItem) {
197
                if (in_array($availableItem[1], $items, true)) {
198
                    $data['fileFolderPermissions'][] = $availableItem;
199
                }
200
            }
201
        }
202
203
        // tsconfig
204
        $data['tsconfig'] = $user->getTSConfig();
205
206
        // non_exclude_fields
207
        $fieldListTmp = GeneralUtility::trimExplode(',', $user->groupData['non_exclude_fields'], true);
208
        $fieldList = [];
209
        foreach ($fieldListTmp as $item) {
210
            $split = explode(':', $item);
211
            $fieldList[$split[0]]['label'] = $GLOBALS['TCA'][$split[0]]['ctrl']['title'];
212
            $fieldList[$split[0]]['fields'][$split[1]] = $GLOBALS['TCA'][$split[0]]['columns'][$split[1]]['label'] ?? $split[1];
213
        }
214
        ksort($fieldList);
215
        foreach ($fieldList as &$fieldListItem) {
216
            ksort($fieldListItem['fields']);
217
        }
218
        $data['non_exclude_fields'] = $fieldList;
219
220
        // page types
221
        $specialItems = $GLOBALS['TCA']['pages']['columns']['doktype']['config']['items'];
222
        foreach ($specialItems as $specialItem) {
223
            $value = $specialItem[1];
224
            if (!GeneralUtility::inList($user->groupData['pagetypes_select'], $value)) {
225
                continue;
226
            }
227
            $label = $specialItem[0];
228
            $icon = $this->iconFactory->mapRecordTypeToIconIdentifier('pages', ['doktype' => $specialItem[1]]);
229
            $data['pageTypes'][] = ['label' => $label, 'value' => $value, 'icon' => $icon];
230
        }
231
232
        return $data;
233
    }
234
235
    protected function getAllSiteLanguages(): array
236
    {
237
        $siteLanguages = [];
238
        foreach (GeneralUtility::makeInstance(SiteFinder::class)->getAllSites() as $site) {
239
            foreach ($site->getAllLanguages() as $languageId => $language) {
240
                if (isset($siteLanguages[$languageId])) {
241
                    // Language already provided by another site, check if values differ
242
                    if (strpos($siteLanguages[$languageId]['title'], $language->getTitle()) === false) {
243
                        // Language already provided by another site, but with a different title
244
                        $siteLanguages[$languageId]['title'] .= ', ' . $language->getTitle();
245
                    }
246
                    if ($siteLanguages[$languageId]['flagIconIdentifier'] !== $language->getFlagIdentifier()) {
247
                        // Language already provided by another site, but with a different flag icon identifier
248
                        $siteLanguages[$languageId]['flagIconIdentifier'] = 'flags-multiple';
249
                    }
250
                } else {
251
                    $siteLanguages[$languageId] = [
252
                        'title' => $language->getTitle(),
253
                        'flagIconIdentifier' => $language->getFlagIdentifier()
254
                    ];
255
                }
256
            }
257
        }
258
        return $siteLanguages;
259
    }
260
}
261