‪TYPO3CMS  ‪main
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 
60  private function ‪createPseudoUser(): \stdClass
61  {
62  ‪$user = new \stdClass();
63  ‪$user->user = [];
64  return ‪$user;
65  }
66 
73  public function get(string $name)
74  {
75  switch ($name) {
76  case 'id':
77  return (int)($this->user->user[$this->user->userid_column ?? 'uid'] ?? 0);
78  case 'username':
79  return (string)($this->user->user[$this->user->username_column ?? 'username'] ?? '');
80  case 'isLoggedIn':
81  return $this->‪isLoggedIn();
82  case 'isAdmin':
83  return $this->‪isAdmin();
84  case 'groupIds':
85  return $this->‪getGroupIds();
86  case 'groupNames':
87  return $this->‪getGroupNames();
88  }
89  throw new AspectPropertyNotFoundException('Property "' . $name . '" not found in Aspect "' . __CLASS__ . '".', 1529996567);
90  }
91 
99  public function ‪isLoggedIn(): bool
100  {
101  return ($this->user->user[$this->user->userid_column ?? 'uid'] ?? 0) > 0;
102  }
103 
107  public function ‪isAdmin(): bool
108  {
109  $isAdmin = false;
110  if ($this->user instanceof BackendUserAuthentication) {
111  $isAdmin = $this->user->isAdmin();
112  }
113  return $isAdmin;
114  }
115 
123  public function ‪getGroupIds(): array
124  {
125  // Alternative groups are set
126  if (is_array($this->groups)) {
127  return ‪$this->groups;
128  }
129  if ($this->user instanceof BackendUserAuthentication) {
130  return $this->user->userGroupsUID;
131  }
132  ‪$groups = [];
133  if ($this->user instanceof FrontendUserAuthentication) {
134  if ($this->‪isLoggedIn()) {
135  // If a user is logged in, always add "-2"
136  ‪$groups = [0, -2];
137  if (!empty($this->user->userGroups)) {
138  ‪$groups = array_merge(‪$groups, array_keys($this->user->userGroups));
139  }
140  } else {
141  ‪$groups = [0, -1];
142  }
143  }
144  return ‪$groups;
145  }
146 
150  public function ‪getGroupNames(): array
151  {
152  $groupNames = [];
153  if ($this->user instanceof AbstractUserAuthentication) {
154  foreach ($this->user->userGroups as $userGroup) {
155  $groupNames[] = $userGroup['title'];
156  }
157  }
158  return $groupNames;
159  }
160 
166  public function ‪isUserOrGroupSet(): bool
167  {
168  if ($this->user instanceof FrontendUserAuthentication) {
169  ‪$groups = $this->‪getGroupIds();
170  return $this->‪isLoggedIn() || implode(',', ‪$groups) !== '0,-1';
171  }
172  return $this->‪isLoggedIn();
173  }
174 }
‪TYPO3\CMS\Core\Context\UserAspect\isUserOrGroupSet
‪bool isUserOrGroupSet()
Definition: UserAspect.php:164
‪TYPO3\CMS\Core\Context\UserAspect\$user
‪AbstractUserAuthentication stdClass $user
Definition: UserAspect.php:40
‪TYPO3\CMS\Core\Context\UserAspect\getGroupNames
‪getGroupNames()
Definition: UserAspect.php:148
‪TYPO3\CMS\Core\Context\UserAspect\isLoggedIn
‪isLoggedIn()
Definition: UserAspect.php:97
‪TYPO3\CMS\Core\Context
Definition: AspectInterface.php:18
‪TYPO3\CMS\Core\Context\UserAspect\isAdmin
‪isAdmin()
Definition: UserAspect.php:105
‪TYPO3\CMS\Core\Context\UserAspect\__construct
‪__construct(AbstractUserAuthentication $user=null, array $alternativeGroups=null)
Definition: UserAspect.php:52
‪TYPO3\CMS\Core\Context\UserAspect\getGroupIds
‪getGroupIds()
Definition: UserAspect.php:121
‪TYPO3\CMS\Core\Context\AspectInterface
Definition: AspectInterface.php:27
‪TYPO3\CMS\Core\Context\UserAspect\createPseudoUser
‪createPseudoUser()
Definition: UserAspect.php:58
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:62
‪TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication
Definition: FrontendUserAuthentication.php:33
‪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:65