‪TYPO3CMS  11.5
UserAspect.php
Go to the documentation of this file.
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 
19 
24 
37 {
41  protected ‪$user;
42 
48  protected ‪$groups;
49 
54  public function ‪__construct(‪AbstractUserAuthentication ‪$user = null, array $alternativeGroups = null)
55  {
56  $this->user = ‪$user ?? $this->‪createPseudoUser();
57  $this->groups = $alternativeGroups;
58  }
59 
63  private function ‪createPseudoUser(): \stdClass
64  {
65  ‪$user = new \stdClass();
66  ‪$user->user = [];
67  return ‪$user;
68  }
69 
77  public function get(string $name)
78  {
79  switch ($name) {
80  case 'id':
81  return (int)($this->user->user[$this->user->userid_column ?? 'uid'] ?? 0);
82  case 'username':
83  return (string)($this->user->user[$this->user->username_column ?? 'username'] ?? '');
84  case 'isLoggedIn':
85  return $this->‪isLoggedIn();
86  case 'isAdmin':
87  return $this->‪isAdmin();
88  case 'groupIds':
89  return $this->‪getGroupIds();
90  case 'groupNames':
91  return $this->‪getGroupNames();
92  }
93  throw new ‪AspectPropertyNotFoundException('Property "' . $name . '" not found in Aspect "' . __CLASS__ . '".', 1529996567);
94  }
95 
107  public function ‪isLoggedIn(): bool
108  {
109  return ($this->user->user[$this->user->userid_column ?? 'uid'] ?? 0) > 0;
110  }
111 
117  public function ‪isAdmin(): bool
118  {
119  $isAdmin = false;
120  if ($this->user instanceof ‪BackendUserAuthentication) {
121  $isAdmin = $this->user->isAdmin();
122  }
123  return $isAdmin;
124  }
125 
135  public function ‪getGroupIds(): array
136  {
137  // Alternative groups are set
138  if (is_array($this->groups)) {
139  return ‪$this->groups;
140  }
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  // If a user is logged in, always add "-2"
148  ‪$groups = [0, -2];
149  if (!empty($this->user->userGroups)) {
150  ‪$groups = array_merge(‪$groups, array_keys($this->user->userGroups));
151  }
152  } else {
153  ‪$groups = [0, -1];
154  }
155  }
156  return ‪$groups;
157  }
158 
164  public function ‪getGroupNames(): array
165  {
166  $groupNames = [];
167  if ($this->user instanceof ‪AbstractUserAuthentication) {
168  foreach ($this->user->userGroups as $userGroup) {
169  $groupNames[] = $userGroup['title'];
170  }
171  }
172  return $groupNames;
173  }
174 
180  public function ‪isUserOrGroupSet(): bool
181  {
182  if ($this->user instanceof ‪FrontendUserAuthentication) {
183  ‪$groups = $this->‪getGroupIds();
184  return $this->‪isLoggedIn() || implode(',', ‪$groups) !== '0,-1';
185  }
186  return $this->‪isLoggedIn();
187  }
188 }
‪TYPO3\CMS\Core\Context\UserAspect\isUserOrGroupSet
‪bool isUserOrGroupSet()
Definition: UserAspect.php:178
‪TYPO3\CMS\Core\Context\UserAspect\$user
‪AbstractUserAuthentication stdClass $user
Definition: UserAspect.php:40
‪TYPO3\CMS\Core\Context\UserAspect\getGroupNames
‪array getGroupNames()
Definition: UserAspect.php:162
‪TYPO3\CMS\Core\Context
Definition: AspectInterface.php:18
‪TYPO3\CMS\Core\Context\UserAspect\__construct
‪__construct(AbstractUserAuthentication $user=null, array $alternativeGroups=null)
Definition: UserAspect.php:52
‪TYPO3\CMS\Core\Context\UserAspect\getGroupIds
‪array getGroupIds()
Definition: UserAspect.php:133
‪TYPO3\CMS\Core\Context\UserAspect\isAdmin
‪bool isAdmin()
Definition: UserAspect.php:115
‪TYPO3\CMS\Core\Context\AspectInterface
Definition: AspectInterface.php:27
‪TYPO3\CMS\Core\Context\UserAspect\isLoggedIn
‪bool isLoggedIn()
Definition: UserAspect.php:105
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:62
‪TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication
Definition: FrontendUserAuthentication.php:32
‪TYPO3\CMS\Core\Context\UserAspect\$groups
‪int[] null $groups
Definition: UserAspect.php:46
‪TYPO3\CMS\Core\Context\UserAspect
Definition: UserAspect.php:37
‪TYPO3\CMS\Core\Context\Exception\AspectPropertyNotFoundException
Definition: AspectPropertyNotFoundException.php:25
‪TYPO3\CMS\Core\Authentication\AbstractUserAuthentication
Definition: AbstractUserAuthentication.php:56
‪TYPO3\CMS\Core\Context\UserAspect\createPseudoUser
‪stdClass createPseudoUser()
Definition: UserAspect.php:61