ILIAS  release_8 Revision v8.24
User.php
Go to the documentation of this file.
1<?php
2
20
22
30class User
31{
37 public string $firstname = '';
38
44 public string $lastname = '';
45
51 public string $fullname = '';
52
58 public static bool $allowEmptyName = false;
59
65 public ?string $sourcedId = null;
66
72 public ?string $username = null;
73
79 public string $email = '';
80
86 public string $image = '';
87
93 public array $roles = array();
94
100 public array $groups = array();
101
107 public ?string $ltiUserId = null;
108
112 public function __construct()
113 {
114 $this->initialize();
115 }
116
120 public function initialize()
121 {
122 $this->firstname = '';
123 $this->lastname = '';
124 $this->fullname = '';
125 $this->sourcedId = null;
126 $this->username = null;
127 $this->email = '';
128 $this->image = '';
129 $this->roles = array();
130 $this->groups = array();
131 }
132
138 public function initialise()
139 {
140 $this->initialize();
141 }
142
149 public function setNames(string $firstname, string $lastname, string $fullname)
150 {
151 $names = array(0 => '', 1 => '');
152 if (!empty($fullname)) {
153 $this->fullname = trim($fullname);
154 $names = preg_split("/[\s]+/", $this->fullname, 2);
155 }
156 if (!empty($firstname)) {
157 $this->firstname = trim($firstname);
158 $names[0] = $this->firstname;
159 } elseif (!empty($names[0])) {
160 $this->firstname = $names[0];
161 } elseif (!static::$allowEmptyName) {
162 $this->firstname = 'User';
163 } else {
164 $this->firstname = '';
165 }
166 if (!empty($lastname)) {
167 $this->lastname = trim($lastname);
168 $names[1] = $this->lastname;
169 } elseif (!empty($names[1])) {
170 $this->lastname = $names[1];
171 } elseif (!static::$allowEmptyName) {
172 $this->lastname = $this->ltiUserId;
173 } else {
174 $this->lastname = '';
175 }
176 if (empty($this->fullname) && (!empty($this->firstname) || !empty($this->lastname))) {
177 $this->fullname = trim("{$this->firstname} {$this->lastname}");
178 }
179 }
180
186 public function setEmail(string $email, string $defaultEmail = null)
187 {
188 if (!empty($email)) {
189 $this->email = $email;
190 } elseif (!empty($defaultEmail)) {
191 $this->email = $defaultEmail;
192 if (substr($this->email, 0, 1) === '@') {
193 if (!empty($this->username)) {
194 $this->email = "{$this->username}{$this->email}";
195 } else {
196 $this->email = "{$this->ltiUserId}{$this->email}";
197 }
198 }
199 } else {
200 $this->email = '';
201 }
202 }
203
209 public function isAdmin(): bool
210 {
211 return $this->hasRole('Administrator') || $this->hasRole('urn:lti:sysrole:ims/lis/SysAdmin') ||
212 $this->hasRole('urn:lti:sysrole:ims/lis/Administrator') || $this->hasRole('urn:lti:instrole:ims/lis/Administrator');
213 }
214
220 public function isStaff(): bool
221 {
222 return ($this->hasRole('Instructor') || $this->hasRole('ContentDeveloper') || $this->hasRole('TeachingAssistant'));
223 }
224
230 public function isLearner(): bool
231 {
232 return $this->hasRole('Learner');
233 }
234
235 ###
236 ### PRIVATE METHODS
237 ###
238
244 private function hasRole(string $role): bool
245 {
246 $ok = in_array($role, $this->roles);
247 if (!$ok && (strpos($role, 'urn:') !== 0) && (strpos($role, 'http://') !== 0) && (strpos($role, 'https://') !== 0)) {
248 $role = "urn:lti:role:ims/lis/{$role}";
249 $ok = in_array($role, $this->roles);
250 }
251 if (!$ok) {
252 $role2 = null;
253 $role3 = null;
254 if (strpos($role, 'urn:') === 0) {
255 if (strpos($role, 'urn:lti:role:ims/lis/') === 0) {
256 $role2 = 'http://purl.imsglobal.org/vocab/lis/v2/membership#' . substr($role, 21);
257 } elseif (strpos($role, 'urn:lti:instrole:ims/lis/') === 0) {
258 $role2 = 'http://purl.imsglobal.org/vocab/lis/v2/person#' . substr($role, 25);
259 $role3 = 'http://purl.imsglobal.org/vocab/lis/v2/institution/person#' . substr($role, 25);
260 } elseif (strpos($role, 'urn:lti:sysrole:ims/lis/') === 0) {
261 $role2 = 'http://purl.imsglobal.org/vocab/lis/v2/person#' . substr($role, 24);
262 $role3 = 'http://purl.imsglobal.org/vocab/lis/v2/system/person#' . substr($role, 24);
263 }
264 } elseif (strpos($role, 'http://purl.imsglobal.org/vocab/lis/v2/') === 0) {
265 if (strpos($role, 'http://purl.imsglobal.org/vocab/lis/v2/membership#') === 0) {
266 $role2 = 'urn:lti:role:ims/lis/' . substr($role, 50);
267 } elseif (strpos($role, 'http://purl.imsglobal.org/vocab/lis/v2/person#') === 0) {
268 $role2 = 'urn:lti:instrole:ims/lis/' . substr($role, 46);
269 $role3 = 'urn:lti:sysrole:ims/lis/' . substr($role, 46);
270 } elseif (strpos($role, 'http://purl.imsglobal.org/vocab/lis/v2/institution/person#') === 0) {
271 $role2 = 'urn:lti:instrole:ims/lis/' . substr($role, 58);
272 $role3 = 'http://purl.imsglobal.org/vocab/lis/v2/person#' . substr($role, 58);
273 } elseif (strpos($role, 'http://purl.imsglobal.org/vocab/lis/v2/system/person#') === 0) {
274 $role2 = 'urn:lti:sysrole:ims/lis/' . substr($role, 53);
275 $role3 = 'http://purl.imsglobal.org/vocab/lis/v2/person#' . substr($role, 53);
276 }
277 }
278 if (!empty($role2)) {
279 $ok = in_array($role2, $this->roles);
280 if (!$ok && !empty($role3)) {
281 $ok = in_array($role3, $this->roles);
282 }
283 }
284 }
285
286 return $ok;
287 }
288}
Class to provide a connection to a persistent store for LTI objects.
Class to represent a platform user.
Definition: User.php:31
initialise()
Initialise the user.
Definition: User.php:138
static bool $allowEmptyName
Allow user name field to be empty?
Definition: User.php:58
hasRole(string $role)
Check whether the user has a specified role name.
Definition: User.php:244
isAdmin()
Check if the user is an administrator (at any of the system, institution or context levels).
Definition: User.php:209
string $firstname
User's first name.
Definition: User.php:37
setEmail(string $email, string $defaultEmail=null)
Set the user's email address.
Definition: User.php:186
string $sourcedId
User's sourcedId.
Definition: User.php:65
isStaff()
Check if the user is staff.
Definition: User.php:220
string $email
User's email address.
Definition: User.php:79
string $ltiUserId
user ID as supplied in the last connection request.
Definition: User.php:107
initialize()
Initialise the user.
Definition: User.php:120
string $username
User's username.
Definition: User.php:72
string $image
User's image URI.
Definition: User.php:86
array $groups
Groups for user.
Definition: User.php:100
array $roles
Roles for user.
Definition: User.php:93
string $lastname
User's last name (surname or family name).
Definition: User.php:44
__construct()
Class constructor.
Definition: User.php:112
setNames(string $firstname, string $lastname, string $fullname)
Set the user's name.
Definition: User.php:149
isLearner()
Check if the user is a learner.
Definition: User.php:230
string $fullname
User's fullname.
Definition: User.php:51
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: AccessToken.php:19