UserAspect.php 5.68 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\Core\Context;

20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
use TYPO3\CMS\Core\Authentication\AbstractUserAuthentication;
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
use TYPO3\CMS\Core\Context\Exception\AspectPropertyNotFoundException;
use TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication;

/**
 * The aspect contains information about a user.
 * Can be used for frontend and backend users.
 *
 * Allowed properties:
 * - id
 * - username
 * - isLoggedIn
 * - groupIds (Array of Ids)
 * - groupNames
 */
class UserAspect implements AspectInterface
{
    /**
     * @var AbstractUserAuthentication
     */
    protected $user;

    /**
     * Alternative list of groups, usually useful for frontend logins with "magic" groups like "-1" and "-2"
     *
46
     * @var int[]|null
47
48
49
50
51
52
53
54
55
     */
    protected $groups;

    /**
     * @param AbstractUserAuthentication|null $user
     * @param array|null $alternativeGroups
     */
    public function __construct(AbstractUserAuthentication $user = null, array $alternativeGroups = null)
    {
56
        $this->user = $user ?? $this->createPseudoUser();
57
58
59
        $this->groups = $alternativeGroups;
    }

60
61
62
63
64
    /**
     * @return object
     */
    private function createPseudoUser(): object
    {
65
        $user = new \stdClass();
66
67
68
69
        $user->user = [];
        return $user;
    }

70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
    /**
     * Fetch common information about the user
     *
     * @param string $name
     * @return int|bool|string|array
     * @throws AspectPropertyNotFoundException
     */
    public function get(string $name)
    {
        switch ($name) {
            case 'id':
                return (int)($this->user->user[$this->user->userid_column ?? 'uid'] ?? 0);
            case 'username':
                return (string)($this->user->user[$this->user->username_column ?? 'username'] ?? '');
            case 'isLoggedIn':
                return $this->isLoggedIn();
86
87
            case 'isAdmin':
                return $this->isAdmin();
88
89
90
91
92
93
94
95
96
            case 'groupIds':
                return $this->getGroupIds();
            case 'groupNames':
                return $this->getGroupNames();
        }
        throw new AspectPropertyNotFoundException('Property "' . $name . '" not found in Aspect "' . __CLASS__ . '".', 1529996567);
    }

    /**
97
98
99
100
101
102
103
     * A user is logged in if the user has a UID, but does not care about groups.
     *
     * For frontend purposes, it is possible to e.g. simulate groups, but this would still be defined as "not logged in".
     * This is also possible in frontend where there are cases that a user can be marked as NOT logged IN, but
     * be logged in but the groups are explicitly NOT defined (see pages.fe_login_mode)
     *
     * For backend, only the check on the user ID is used.
104
105
106
107
108
109
110
111
     *
     * @return bool
     */
    public function isLoggedIn(): bool
    {
        return ($this->user->user[$this->user->userid_column ?? 'uid'] ?? 0) > 0;
    }

112
113
114
115
116
117
118
    /**
     * Check if admin is set
     *
     * @return bool
     */
    public function isAdmin(): bool
    {
119
120
121
122
123
        $isAdmin = false;
        if ($this->user instanceof BackendUserAuthentication) {
            $isAdmin = $this->user->isAdmin();
        }
        return $isAdmin;
124
125
    }

126
127
128
129
130
131
132
133
134
135
136
    /**
     * Return the groups the user is a member of
     *
     * For Frontend Users there are two special groups:
     * "-1" = hide at login
     * "-2" = show at any login
     *
     * @return array
     */
    public function getGroupIds(): array
    {
137
138
139
140
        // Alternative groups are set
        if (is_array($this->groups)) {
            return $this->groups;
        }
141
        if ($this->user instanceof BackendUserAuthentication) {
142
            return $this->user->userGroupsUID;
143
        }
144
        $groups = [];
145
        if ($this->user instanceof FrontendUserAuthentication) {
146
            if ($this->isLoggedIn()) {
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
                // If a user is logged in, always add "-2"
                $groups = [0, -2];
                if (!empty($this->user->groupData['uid'])) {
                    $groups = array_merge($groups, array_map('intval', $this->user->groupData['uid']));
                }
            } else {
                $groups = [0, -1];
            }
        }
        return $groups;
    }

    /**
     * Get the name of all groups, used in Fluid's IfHasRole ViewHelper
     *
     * @return array
     */
    public function getGroupNames(): array
    {
        $groupNames = [];
        if ($this->user instanceof FrontendUserAuthentication) {
            $groupNames = $this->user->groupData['title'];
        }
        if ($this->user instanceof BackendUserAuthentication) {
            foreach ($this->user->userGroups as $userGroup) {
                $groupNames[] = $userGroup['title'];
            }
        }
        return $groupNames;
    }

    /**
     * Checking if a user is logged in or a group constellation different from "0,-1"
     *
     * @return bool TRUE if either a login user is found OR if the group list is set to something else than '0,-1' (could be done even without a user being logged in!)
     */
    public function isUserOrGroupSet(): bool
    {
        if ($this->user instanceof FrontendUserAuthentication) {
            $groups = $this->getGroupIds();
187
            return $this->isLoggedIn() || implode(',', $groups) !== '0,-1';
188
189
190
191
        }
        return $this->isLoggedIn();
    }
}