UserAspect.php 5.37 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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
72
73
74
<?php
declare(strict_types = 1);
namespace TYPO3\CMS\Core\Context;

/*
 * 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!
 */

use TYPO3\CMS\Core\Authentication\AbstractUserAuthentication;
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
use TYPO3\CMS\Core\Context\Exception\AspectPropertyNotFoundException;
use TYPO3\CMS\Core\Utility\GeneralUtility;
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"
     *
     * @var int[]
     */
    protected $groups;

    /**
     * @param AbstractUserAuthentication|null $user
     * @param array|null $alternativeGroups
     */
    public function __construct(AbstractUserAuthentication $user = null, array $alternativeGroups = null)
    {
        $this->user = $user ?? (object)['user' => []];
        $this->groups = $alternativeGroups;
    }

    /**
     * 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();
75
76
            case 'isAdmin':
                return $this->isAdmin();
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
            case 'groupIds':
                return $this->getGroupIds();
            case 'groupNames':
                return $this->getGroupNames();
        }
        throw new AspectPropertyNotFoundException('Property "' . $name . '" not found in Aspect "' . __CLASS__ . '".', 1529996567);
    }

    /**
     * If a frontend user is checked, he/she also needs to have a group, otherwise it is only
     * checked if the frontend user has a uid > 0
     *
     * @return bool
     */
    public function isLoggedIn(): bool
    {
        if ($this->user instanceof FrontendUserAuthentication) {
            return ($this->user->user[$this->user->userid_column ?? 'uid'] ?? 0) > 0 && !empty($this->user->groupData['uid'] ?? null);
        }
        return ($this->user->user[$this->user->userid_column ?? 'uid'] ?? 0) > 0;
    }

99
100
101
102
103
104
105
106
107
108
    /**
     * Check if admin is set
     *
     * @return bool
     */
    public function isAdmin(): bool
    {
        return $this->user->user['admin'] === 1 ?? false;
    }

109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
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
    /**
     * 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
    {
        $groups = [];
        if ($this->user instanceof BackendUserAuthentication) {
            $groups = GeneralUtility::intExplode(',', $this->user->groupList, true);
        }
        if ($this->user instanceof FrontendUserAuthentication) {
            // Alternative groups are set
            if (is_array($this->groups)) {
                $groups = $this->groups;
            } elseif ($this->isLoggedIn()) {
                // 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();
            return is_array($this->user->user ?? null) || implode(',', $groups) !== '0,-1';
        }
        return $this->isLoggedIn();
    }
}