‪TYPO3CMS  10.4
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 
25 
38 {
42  protected ‪$user;
43 
49  protected ‪$groups;
50 
55  public function ‪__construct(‪AbstractUserAuthentication ‪$user = null, array $alternativeGroups = null)
56  {
57  $this->user = ‪$user ?? $this->‪createPseudoUser();
58  $this->groups = $alternativeGroups;
59  }
60 
64  private function ‪createPseudoUser(): \stdClass
65  {
66  ‪$user = new \stdClass();
67  ‪$user->user = [];
68  return ‪$user;
69  }
70 
78  public function get(string $name)
79  {
80  switch ($name) {
81  case 'id':
82  return (int)($this->user->user[$this->user->userid_column ?? 'uid'] ?? 0);
83  case 'username':
84  return (string)($this->user->user[$this->user->username_column ?? 'username'] ?? '');
85  case 'isLoggedIn':
86  return $this->‪isLoggedIn();
87  case 'isAdmin':
88  return $this->‪isAdmin();
89  case 'groupIds':
90  return $this->‪getGroupIds();
91  case 'groupNames':
92  return $this->‪getGroupNames();
93  }
94  throw new ‪AspectPropertyNotFoundException('Property "' . $name . '" not found in Aspect "' . __CLASS__ . '".', 1529996567);
95  }
96 
108  public function ‪isLoggedIn(): bool
109  {
110  return ($this->user->user[$this->user->userid_column ?? 'uid'] ?? 0) > 0;
111  }
112 
118  public function ‪isAdmin(): bool
119  {
120  $isAdmin = false;
121  if ($this->user instanceof ‪BackendUserAuthentication) {
122  $isAdmin = $this->user->isAdmin();
123  }
124  return $isAdmin;
125  }
126 
136  public function ‪getGroupIds(): array
137  {
138  // Alternative groups are set
139  if (is_array($this->groups)) {
140  return ‪$this->groups;
141  }
142  if ($this->user instanceof ‪BackendUserAuthentication) {
143  return ‪GeneralUtility::intExplode(',', $this->user->groupList, true);
144  }
145  ‪$groups = [];
146  if ($this->user instanceof ‪FrontendUserAuthentication) {
147  if ($this->‪isLoggedIn()) {
148  // If a user is logged in, always add "-2"
149  ‪$groups = [0, -2];
150  if (!empty($this->user->groupData['uid'])) {
151  ‪$groups = array_merge(‪$groups, array_map('intval', $this->user->groupData['uid']));
152  }
153  } else {
154  ‪$groups = [0, -1];
155  }
156  }
157  return ‪$groups;
158  }
159 
165  public function ‪getGroupNames(): array
166  {
167  $groupNames = [];
168  if ($this->user instanceof ‪FrontendUserAuthentication) {
169  $groupNames = $this->user->groupData['title'];
170  }
171  if ($this->user instanceof ‪BackendUserAuthentication) {
172  foreach ($this->user->userGroups as $userGroup) {
173  $groupNames[] = $userGroup['title'];
174  }
175  }
176  return $groupNames;
177  }
178 
184  public function ‪isUserOrGroupSet(): bool
185  {
186  if ($this->user instanceof ‪FrontendUserAuthentication) {
187  ‪$groups = $this->‪getGroupIds();
188  return $this->‪isLoggedIn() || implode(',', ‪$groups) !== '0,-1';
189  }
190  return $this->‪isLoggedIn();
191  }
192 }
‪TYPO3\CMS\Core\Context\UserAspect\isUserOrGroupSet
‪bool isUserOrGroupSet()
Definition: UserAspect.php:182
‪TYPO3\CMS\Core\Context\UserAspect\$user
‪AbstractUserAuthentication stdClass $user
Definition: UserAspect.php:41
‪TYPO3\CMS\Core\Context\UserAspect\getGroupNames
‪array getGroupNames()
Definition: UserAspect.php:163
‪TYPO3\CMS\Core\Context
Definition: AspectInterface.php:18
‪TYPO3\CMS\Core\Context\UserAspect\__construct
‪__construct(AbstractUserAuthentication $user=null, array $alternativeGroups=null)
Definition: UserAspect.php:53
‪TYPO3\CMS\Core\Context\UserAspect\getGroupIds
‪array getGroupIds()
Definition: UserAspect.php:134
‪TYPO3\CMS\Core\Context\UserAspect\isAdmin
‪bool isAdmin()
Definition: UserAspect.php:116
‪TYPO3\CMS\Core\Context\AspectInterface
Definition: AspectInterface.php:27
‪TYPO3\CMS\Core\Context\UserAspect\isLoggedIn
‪bool isLoggedIn()
Definition: UserAspect.php:106
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:62
‪TYPO3\CMS\Core\Utility\GeneralUtility\intExplode
‪static int[] intExplode($delimiter, $string, $removeEmptyValues=false, $limit=0)
Definition: GeneralUtility.php:988
‪TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication
Definition: FrontendUserAuthentication.php:30
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Context\UserAspect\$groups
‪int[] null $groups
Definition: UserAspect.php:47
‪TYPO3\CMS\Core\Context\UserAspect
Definition: UserAspect.php:38
‪TYPO3\CMS\Core\Context\Exception\AspectPropertyNotFoundException
Definition: AspectPropertyNotFoundException.php:26
‪TYPO3\CMS\Core\Authentication\AbstractUserAuthentication
Definition: AbstractUserAuthentication.php:51
‪TYPO3\CMS\Core\Context\UserAspect\createPseudoUser
‪stdClass createPseudoUser()
Definition: UserAspect.php:62