‪TYPO3CMS  9.5
UserAspect.php
Go to the documentation of this file.
1 <?php
2 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 
23 
36 {
40  protected ‪$user;
41 
47  protected ‪$groups;
48 
53  public function ‪__construct(‪AbstractUserAuthentication ‪$user = null, array $alternativeGroups = null)
54  {
55  $this->user = ‪$user ?? (object)['user' => []];
56  $this->groups = $alternativeGroups;
57  }
58 
66  public function get(string $name)
67  {
68  switch ($name) {
69  case 'id':
70  return (int)($this->user->user[$this->user->userid_column ?? 'uid'] ?? 0);
71  case 'username':
72  return (string)($this->user->user[$this->user->username_column ?? 'username'] ?? '');
73  case 'isLoggedIn':
74  return $this->‪isLoggedIn();
75  case 'isAdmin':
76  return $this->‪isAdmin();
77  case 'groupIds':
78  return $this->‪getGroupIds();
79  case 'groupNames':
80  return $this->‪getGroupNames();
81  }
82  throw new ‪AspectPropertyNotFoundException('Property "' . $name . '" not found in Aspect "' . __CLASS__ . '".', 1529996567);
83  }
84 
91  public function ‪isLoggedIn(): bool
92  {
93  if ($this->user instanceof ‪FrontendUserAuthentication) {
94  return ($this->user->user[$this->user->userid_column ?? 'uid'] ?? 0) > 0 && !empty($this->user->groupData['uid'] ?? null);
95  }
96  return ($this->user->user[$this->user->userid_column ?? 'uid'] ?? 0) > 0;
97  }
98 
104  public function ‪isAdmin(): bool
105  {
106  $isAdmin = false;
107  if ($this->user instanceof ‪BackendUserAuthentication) {
108  $isAdmin = $this->user->isAdmin();
109  }
110  return $isAdmin;
111  }
112 
122  public function ‪getGroupIds(): array
123  {
124  ‪$groups = [];
125  if ($this->user instanceof ‪BackendUserAuthentication) {
126  ‪$groups = GeneralUtility::intExplode(',', $this->user->groupList, true);
127  }
128  if ($this->user instanceof ‪FrontendUserAuthentication) {
129  // Alternative groups are set
130  if (is_array($this->groups)) {
132  } elseif ($this->‪isLoggedIn()) {
133  // If a user is logged in, always add "-2"
134  ‪$groups = [0, -2];
135  if (!empty($this->user->groupData['uid'])) {
136  ‪$groups = array_merge(‪$groups, array_map('intval', $this->user->groupData['uid']));
137  }
138  } else {
139  ‪$groups = [0, -1];
140  }
141  }
142  return ‪$groups;
143  }
144 
150  public function ‪getGroupNames(): array
151  {
152  $groupNames = [];
153  if ($this->user instanceof ‪FrontendUserAuthentication) {
154  $groupNames = $this->user->groupData['title'];
155  }
156  if ($this->user instanceof ‪BackendUserAuthentication) {
157  foreach ($this->user->userGroups as $userGroup) {
158  $groupNames[] = $userGroup['title'];
159  }
160  }
161  return $groupNames;
162  }
163 
169  public function ‪isUserOrGroupSet(): bool
170  {
171  if ($this->user instanceof ‪FrontendUserAuthentication) {
172  ‪$groups = $this->‪getGroupIds();
173  return is_array($this->user->user ?? null) || implode(',', ‪$groups) !== '0,-1';
174  }
175  return $this->‪isLoggedIn();
176  }
177 }
‪TYPO3\CMS\Core\Context\UserAspect\isUserOrGroupSet
‪bool isUserOrGroupSet()
Definition: UserAspect.php:167
‪TYPO3\CMS\Core\Context\UserAspect\$groups
‪int[] $groups
Definition: UserAspect.php:45
‪TYPO3\CMS\Core\Context\UserAspect\getGroupNames
‪array getGroupNames()
Definition: UserAspect.php:148
‪TYPO3\CMS\Core\Context
Definition: AspectInterface.php:3
‪TYPO3\CMS\Core\Context\UserAspect\__construct
‪__construct(AbstractUserAuthentication $user=null, array $alternativeGroups=null)
Definition: UserAspect.php:51
‪TYPO3\CMS\Core\Context\UserAspect\getGroupIds
‪array getGroupIds()
Definition: UserAspect.php:120
‪TYPO3\CMS\Core\Context\UserAspect\isAdmin
‪bool isAdmin()
Definition: UserAspect.php:102
‪TYPO3\CMS\Core\Context\AspectInterface
Definition: AspectInterface.php:25
‪TYPO3\CMS\Core\Context\UserAspect\isLoggedIn
‪bool isLoggedIn()
Definition: UserAspect.php:89
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:45
‪TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication
Definition: FrontendUserAuthentication.php:28
‪TYPO3\CMS\Core\Context\UserAspect\$user
‪AbstractUserAuthentication $user
Definition: UserAspect.php:39
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\Context\UserAspect
Definition: UserAspect.php:36
‪TYPO3\CMS\Core\Context\Exception\AspectPropertyNotFoundException
Definition: AspectPropertyNotFoundException.php:24
‪TYPO3\CMS\Core\Authentication\AbstractUserAuthentication
Definition: AbstractUserAuthentication.php:51