ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilCmiXapiUser.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
31{
32 public const DB_TABLE_NAME = 'cmix_users';
33
34 protected ?int $objId;
35
36 protected ?int $usrId;
37
38 protected ?int $privacyIdent;
39
40 protected bool $proxySuccess;
41
42 protected bool $satisfied;
43
45
46 protected string $usrIdent;
47
48 protected string $registration;
49
51
52
53 public function __construct(?int $objId = null, ?int $usrId = null, ?int $privacyIdent = null)
54 {
55 global $DIC;
56 $this->database = $DIC->database();
57 $this->objId = $objId;
58 $this->usrId = $usrId;
59 $this->privacyIdent = $privacyIdent;
60 $this->proxySuccess = false;
61 $this->satisfied = false;
62 $this->fetchUntil = new ilCmiXapiDateTime(0, IL_CAL_UNIX);
63 $this->usrIdent = '';
64 $this->registration = '';
65
66 if ($objId !== null && $usrId !== null && $privacyIdent !== null) {
67 $this->load();
68 }
69 }
70
71 public function getObjId(): ?int
72 {
73 return $this->objId;
74 }
75
76 public function setObjId(int $objId): void
77 {
78 $this->objId = $objId;
79 }
80
81 public function getPrivacyIdent(): ?int
82 {
84 }
85
86 public function setPrivacyIdent(int $privacyIdent): void
87 {
88 $this->privacyIdent = $privacyIdent;
89 }
90
91 public function getUsrId(): ?int
92 {
93 return $this->usrId;
94 }
95
96 public function setUsrId(int $usrId): void
97 {
98 $this->usrId = $usrId;
99 }
100
101 public function getUsrIdent(): string
102 {
103 return $this->usrIdent;
104 }
105
106 public function setUsrIdent(string $usrIdent): void
107 {
108 $this->usrIdent = $usrIdent;
109 }
110
111 public function getRegistration(): string
112 {
113 return $this->registration;
114 }
115
116 public function setRegistration(string $registration): void
117 {
118 $this->registration = $registration;
119 }
120
121 public static function getIliasUuid(): ?string
122 {
123 $setting = new ilSetting('cmix');
124 // Fallback
125 if (null == $setting->get('ilias_uuid', null)) {
126 // $uuid = (new \Ramsey\Uuid\UuidFactory())->uuid4()->toString();
127 $uuid = self::getUUID(32);
128 $setting->set('ilias_uuid', $uuid);
129 }
130 return $setting->get('ilias_uuid');
131 }
132
133 public function hasProxySuccess(): bool
134 {
135 return $this->proxySuccess;
136 }
137
138 public function setProxySuccess(bool $proxySuccess): void
139 {
140 $this->proxySuccess = $proxySuccess;
141 }
142
143 public function setSatisfied(bool $satisfied): void
144 {
145 $this->satisfied = $satisfied;
146 }
147
151 public function getSatisfied(): bool
152 {
153 return $this->satisfied;
154 }
155
157 {
158 return $this->fetchUntil;
159 }
160
162 {
163 $this->fetchUntil = $fetchUntil;
164 }
165
166 protected function load(): void
167 {
168 $res = $this->database->queryF(
169 "SELECT * FROM " . self::DB_TABLE_NAME . " WHERE obj_id = %s AND usr_id = %s AND privacy_ident = %s",
170 array('integer', 'integer', 'integer'),
171 array($this->getObjId(), $this->getUsrId(), $this->getPrivacyIdent())
172 );
173
174 while ($row = $this->database->fetchAssoc($res)) {
175 $this->assignFromDbRow($row);
176 }
177 }
178
179 public function assignFromDbRow($dbRow): void
180 {
181 $this->setObjId((int) $dbRow['obj_id']);
182 $this->setUsrId((int) $dbRow['usr_id']);
183 $this->setProxySuccess((bool) $dbRow['proxy_success']);
184 $this->setSatisfied((bool) $dbRow['satisfied']);
185 $this->setFetchUntil(new ilCmiXapiDateTime($dbRow['fetched_until'], IL_CAL_DATETIME));
186 $this->setUsrIdent((string) $dbRow['usr_ident']);
187 $this->setPrivacyIdent((int) $dbRow['privacy_ident']);
188 $this->setRegistration((string) $dbRow['registration']);
189 }
190
191 public function save(): void
192 {
193 global $DIC; /* @var \ILIAS\DI\Container $DIC */
194
195 if (!ilObjUser::_isAnonymous($this->getUsrId())) {
196 $DIC->database()->replace(
197 self::DB_TABLE_NAME,
198 array(
199 'obj_id' => array('integer', (int) $this->getObjId()),
200 'usr_id' => array('integer', (int) $this->getUsrId()),
201 'privacy_ident' => array('integer', (int) $this->getPrivacyIdent())
202 ),
203 array(
204 'proxy_success' => array('integer', (int) $this->hasProxySuccess()),
205 'fetched_until' => array('timestamp', $this->getFetchUntil()->get(IL_CAL_DATETIME)),
206 'usr_ident' => array('text', $this->getUsrIdent()),
207 'registration' => array('text', $this->getRegistration()),
208 'satisfied' => array('integer', (int) $this->getSatisfied())
209 )
210 );
211 }
212 }
213
214 // ToDo Only for Deletion -> Core
218 public static function getInstancesByObjectIdAndUsrId(int $objId, int $usrId): array
219 {
220 global $DIC; /* @var \ILIAS\DI\Container $DIC */
221 $res = $DIC->database()->queryF(
222 "SELECT * FROM " . self::DB_TABLE_NAME . " WHERE obj_id = %s AND usr_id = %s",
223 array('integer', 'integer'),
224 array($objId, $usrId)
225 );
226 $cmixUsers = array();
227 while ($row = $DIC->database()->fetchAssoc($res)) {
228 $cmixUser = new self();
229 $cmixUser->assignFromDbRow($row);
230 $cmixUsers[] = $cmixUser;
231 }
232 return $cmixUsers;
233 }
234
236 {
237 global $DIC; /* @var \ILIAS\DI\Container $DIC */
238 $res = $DIC->database()->queryF(
239 "SELECT * FROM " . self::DB_TABLE_NAME . " WHERE obj_id = %s AND usr_ident = %s",
240 array('integer', 'text'),
241 array($objId, $usrIdent)
242 );
243
244 $cmixUser = new self();
245
246 while ($row = $DIC->database()->fetchAssoc($res)) {
247 $cmixUser->assignFromDbRow($row);
248 }
249
250 return $cmixUser;
251 }
252
253 public static function saveProxySuccess(int $objId, int $usrId, int $privacyIdent): void //TODO
254 {
255 global $DIC; /* @var \ILIAS\DI\Container $DIC */
256 $DIC->database()->update(
257 self::DB_TABLE_NAME,
258 array(
259 'proxy_success' => array('integer', 1)
260 ),
261 array(
262 'obj_id' => array('integer', (int) $objId),
263 'usr_id' => array('integer', (int) $usrId),
264 'privacy_ident' => array('integer', (int) $privacyIdent)
265 )
266 );
267 }
268
269 public static function getIdent(int $userIdentMode, ilObjUser $user): string
270 {
271 if (ilObjUser::_isAnonymous($user->getId())) {
272 return self::buildPseudoEmail(hash("sha256", (string) microtime()), self::getIliasUuid());
273 }
274 switch ($userIdentMode) {
276
277 return self::buildPseudoEmail((string) $user->getId(), self::getIliasUuid());
278
280
281 if (!ilUtil::is_email($user->getLogin())) {
282 return self::buildPseudoEmail($user->getLogin(), self::getIliasUuid());
283 } else {
284 return $user->getLogin();
285 }
286
287 // no break
289
290 return self::buildPseudoEmail($user->getExternalAccount(), self::getIliasUuid());
291
293
294 return self::buildPseudoEmail(hash("sha256", '' . $user->getId() . $user->getCreateDate()), self::getIliasUuid());
295
297 $tmpHash = hash("sha256", '' . $user->getId() . $user->getCreateDate()) . '@' . str_replace('www.', '', $_SERVER['HTTP_HOST']);
298 if (strlen($tmpHash) > 80) {
299 $tmpHash = substr($tmpHash, strlen($tmpHash) - 80);
300 }
301 return $tmpHash;
302
304
305 return self::buildPseudoEmail(self::getUserObjectUniqueId(), self::getIliasUuid());
306
308
309 return $user->getEmail();
310 }
311
312 return '';
313 }
314
315 public static function getIdentAsId(int $userIdentMode, ilObjUser $user): string
316 {
317 switch ($userIdentMode) {
319
320 return (string) $user->getId();
321
323
324 return $user->getLogin();
325
327
328 return $user->getExternalAccount();
329
331
332 return hash("sha256", '' . $user->getId() . $user->getCreateDate());
333
335 $tmpHash = hash("sha256", '' . $user->getId() . $user->getCreateDate());
336 $tmpHost = '@' . str_replace('www.', '', $_SERVER['HTTP_HOST']);
337 if (strlen($tmpHash . $tmpHost) > 80) {
338 $tmpHash = substr($tmpHash, strlen($tmpHash) - (80 - strlen($tmpHost)));
339 }
340 return $tmpHash;
341
343
345
347
348 return 'realemail' . $user->getId();
349 }
350
351 return '';
352 }
353
354 protected static function buildPseudoEmail(string $mbox, string $domain): string
355 {
356 return "{$mbox}@{$domain}.ilias";
357 }
358
359 public static function getName(int $userNameMode, ilObjUser $user): string
360 {
361 $usrName = "";
362 switch ($userNameMode) {
364 $usrName = $user->getFirstname();
365 break;
366
368 $usrName = $user->getUTitle() ? $user->getUTitle() . ' ' : '';
369 $usrName .= $user->getLastname();
370 break;
371
373 $usrName = $user->getFullname();
374 break;
375
377 default:
378 $usrName = '-';
379 break;
380 }
381 return $usrName;
382 }
383
387 public static function getUsersForObject(int $objId, bool $asUsrId = false): array
388 {
389 global $DIC; /* @var \ILIAS\DI\Container $DIC */
390
391 $res = $DIC->database()->queryF(
392 "SELECT * FROM " . self::DB_TABLE_NAME . " WHERE obj_id = %s",
393 array('integer'),
394 array($objId)
395 );
396
397 $users = [];
398
399 if ($asUsrId === false) {
400 while ($row = $DIC->database()->fetchAssoc($res)) {
401 $cmixUser = new self();
402 $cmixUser->assignFromDbRow($row);
403
404 $users[] = $cmixUser;
405 }
406 } else {
407 while ($row = $DIC->database()->fetchAssoc($res)) {
408 $users[] = $row['usr_id'];
409 }
410 }
411 return $users;
412 }
413
417 public static function getUserIdents(int $objId, int $usrId): array
418 {
419 global $DIC; /* @var \ILIAS\DI\Container $DIC */
420
421 $res = $DIC->database()->queryF(
422 "SELECT usr_ident FROM " . self::DB_TABLE_NAME . " WHERE obj_id = %s AND usr_id = %s",
423 array('integer','integer'),
424 array($objId,$usrId)
425 );
426
427 $usrIdents = [];
428 while ($row = $DIC->database()->fetchAssoc($res)) {
429 $usrIdents[] = $row['usr_ident'];
430 }
431 return $usrIdents;
432 }
433
434 public static function exists(int $objId, int $usrId, int $privacyIdent = 999): bool
435 {
436 global $DIC; /* @var \ILIAS\DI\Container $DIC */
437 if ($privacyIdent == 999) {
438 $query = "SELECT count(*) cnt FROM " . self::DB_TABLE_NAME . " WHERE obj_id = %s AND usr_id = %s";
439 $res = $DIC->database()->queryF(
440 $query,
441 array('integer', 'integer'),
442 array($objId, $usrId)
443 );
444 } else {
445 $query = "SELECT count(*) cnt FROM " . self::DB_TABLE_NAME . " WHERE obj_id = %s AND usr_id = %s AND privacy_ident = %s";
446 $res = $DIC->database()->queryF(
447 $query,
448 array('integer', 'integer', 'integer'),
450 );
451 }
452
453 while ($row = $DIC->database()->fetchAssoc($res)) {
454 return (bool) $row['cnt'];
455 }
456
457 return false;
458 }
459
463 public static function getCmixObjectsHavingUsersMissingProxySuccess(): array
464 {
465 global $DIC; /* @var \ILIAS\DI\Container $DIC */
466
467 $query = "
468 SELECT DISTINCT cu.obj_id
469 FROM " . self::DB_TABLE_NAME . " cu
470 INNER JOIN object_data od
471 ON od.obj_id = cu.obj_id
472 AND od.type = 'cmix'
473 WHERE cu.proxy_success != %s
474 ";
475
476 $res = $DIC->database()->queryF($query, array('integer'), array(1));
477
478 $objects = array();
479
480 while ($row = $DIC->database()->fetchAssoc($res)) {
481 $objects[] = (int) $row['obj_id'];
482 }
483
484 return $objects;
485 }
486
487 public static function updateFetchedUntilForObjects(ilCmiXapiDateTime $fetchedUntil, array $objectIds): void
488 {
489 global $DIC; /* @var \ILIAS\DI\Container $DIC */
490
491 $IN_objIds = $DIC->database()->in('obj_id', $objectIds, false, 'integer');
492
493 $query = "UPDATE " . self::DB_TABLE_NAME . " SET fetched_until = %s WHERE $IN_objIds";
494 $DIC->database()->manipulateF($query, array('timestamp'), array($fetchedUntil->get(IL_CAL_DATETIME)));
495 }
496
500 public static function lookupObjectIds(int $usrId, string $type = ''): array
501 {
502 global $DIC; /* @var \ILIAS\DI\Container $DIC */
503
504 $TYPE_JOIN = '';
505
506 if (strlen($type)) {
507 $TYPE_JOIN = "
508 INNER JOIN object_data od
509 ON od.obj_id = cu.obj_id
510 AND od.type = {$DIC->database()->quote($type, 'text')}
511 ";
512 }
513
514 $query = "
515 SELECT cu.obj_id
516 FROM " . self::DB_TABLE_NAME . " cu
517 {$TYPE_JOIN}
518 WHERE cu.usr_id = {$DIC->database()->quote($usrId, 'integer')}
519 ";
520
521 $res = $DIC->database()->query($query);
522
523 $objIds = [];
524
525 while ($row = $DIC->database()->fetchAssoc($res)) {
526 $objIds[] = (int) $row['obj_id'];
527 }
528
529 return $objIds;
530 }
531 public static function getUserObjectUniqueId(int $length = 32): string
532 {
533 // $storedId = self::readUserObjectUniqueId();
534 // if( (bool)strlen($storedId) ) {
535 // return strstr($storedId,'@', true);
536 // }
537
538 // $getId = function( $length ) {
539 // $multiplier = floor($length/8) * 2;
540 // $uid = str_shuffle(str_repeat(uniqid(), $multiplier));
541
542 // try {
543 // $ident = bin2hex(random_bytes($length));
544 // } catch (Exception $e) {
545 // $ident = $uid;
546 // }
547
548 // $start = rand(0, strlen($ident) - $length - 1);
549 // return substr($ident, $start, $length);
550 // };
551
552 $id = self::getUUID($length);//$getId($length);
554 while ($exists) {
555 $id = self::getUUID($length);//$getId($length);
557 }
558
559 return $id;
560 }
561
562 public static function getUUID(int $length = 32): string
563 {
564 $multiplier = (int) floor($length / 8) * 2;
565 $uid = str_shuffle(str_repeat(uniqid(), $multiplier));
566
567 try {
568 $ident = bin2hex(random_bytes($length));
569 } catch (Exception $e) {
570 $ident = $uid;
571 }
572
573 $start = rand(0, strlen($ident) - $length - 1);
574 return substr($ident, $start, $length);
575 }
576
577 private static function userObjectUniqueIdExists(string $id): bool
578 {
579 global $DIC; /* @var \ILIAS\DI\Container $DIC */
580
581 $query = "SELECT usr_ident FROM " . self::DB_TABLE_NAME . " WHERE " . $DIC->database()->like('usr_ident', 'text', $id . '@%');
582 $result = $DIC->database()->query($query);
583 return $result->numRows() != 0;
584 }
585
586 public static function generateCMI5Registration(int $objId, int $usrId): \Ramsey\Uuid\UuidInterface
587 {
588 return (new \Ramsey\Uuid\UuidFactory())->uuid3(self::getIliasUuid(), $objId . '-' . $usrId);
589 }
590
591 public static function generateRegistration(ilObjCmiXapi $obj, ilObjUser $user): \Ramsey\Uuid\UuidInterface
592 {
593 return (new \Ramsey\Uuid\UuidFactory())->uuid3(self::getIliasUuid(), $obj->getRefId() . '-' . $user->getId());
594 }
595
596 public static function getCMI5RegistrationFromAuthToken(ilCmiXapiAuthToken $authToken): \Ramsey\Uuid\UuidInterface
597 {
598 return (new \Ramsey\Uuid\UuidFactory())->uuid3(self::getIliasUuid(), $authToken->getObjId() . '-' . $authToken->getUsrId());
599 }
600
601 public static function getRegistrationFromAuthToken(ilCmiXapiAuthToken $authToken): \Ramsey\Uuid\UuidInterface
602 {
603 return (new \Ramsey\Uuid\UuidFactory())->uuid3(self::getIliasUuid(), $authToken->getRefId() . '-' . $authToken->getUsrId());
604 }
605
606 public static function getUsrIdForObjectAndUsrIdent(int $objId, string $userIdent): ?int
607 {
608 global $DIC; /* @var \ILIAS\DI\Container $DIC */
609
610 $query = "SELECT usr_id FROM " . self::DB_TABLE_NAME . " WHERE obj_id = "
611 . $DIC->database()->quote($objId, 'integer')
612 . " AND" . $DIC->database()->like('usr_ident', 'text', $userIdent . '@%');
613 $res = $DIC->database()->query($query);
614
615 $usrId = null;
616 while ($row = $DIC->database()->fetchAssoc($res)) {
617 $usrId = (int) $row['usr_id'];
618 }
619
620 return $usrId;
621 }
622
623 public static function deleteUsersForObject(int $objId, ?array $users = []): void
624 {
625 global $DIC; /* @var \ILIAS\DI\Container $DIC */
626 $query = "DELETE FROM cmix_users WHERE obj_id = " . $DIC->database()->quote($objId, 'integer');
627 if (count($users) == 0) {
628 $DIC->database()->manipulate($query);
629 } else {
630 $DIC->database()->manipulateF(
631 $query . " AND usr_id = %s",
632 array('integer'),
633 $users
634 );
635 }
636 }
637
638}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
const IL_CAL_UNIX
const IL_CAL_DATETIME
setRegistration(string $registration)
static saveProxySuccess(int $objId, int $usrId, int $privacyIdent)
setUsrId(int $usrId)
setObjId(int $objId)
static deleteUsersForObject(int $objId, ?array $users=[])
setPrivacyIdent(int $privacyIdent)
static getIdent(int $userIdentMode, ilObjUser $user)
static getUsrIdForObjectAndUsrIdent(int $objId, string $userIdent)
static getName(int $userNameMode, ilObjUser $user)
static getInstanceByObjectIdAndUsrIdent(int $objId, string $usrIdent)
static getUserObjectUniqueId(int $length=32)
static getUserIdents(int $objId, int $usrId)
setSatisfied(bool $satisfied)
static userObjectUniqueIdExists(string $id)
static getInstancesByObjectIdAndUsrId(int $objId, int $usrId)
static lookupObjectIds(int $usrId, string $type='')
static getIdentAsId(int $userIdentMode, ilObjUser $user)
ilCmiXapiDateTime $fetchUntil
static exists(int $objId, int $usrId, int $privacyIdent=999)
static getUUID(int $length=32)
static updateFetchedUntilForObjects(ilCmiXapiDateTime $fetchedUntil, array $objectIds)
static getCMI5RegistrationFromAuthToken(ilCmiXapiAuthToken $authToken)
setProxySuccess(bool $proxySuccess)
static buildPseudoEmail(string $mbox, string $domain)
setFetchUntil(ilCmiXapiDateTime $fetchUntil)
static getUsersForObject(int $objId, bool $asUsrId=false)
static getRegistrationFromAuthToken(ilCmiXapiAuthToken $authToken)
ilDBInterface $database
__construct(?int $objId=null, ?int $usrId=null, ?int $privacyIdent=null)
setUsrIdent(string $usrIdent)
static generateRegistration(ilObjCmiXapi $obj, ilObjUser $user)
static generateCMI5Registration(int $objId, int $usrId)
static getCmixObjectsHavingUsersMissingProxySuccess()
get(int $a_format, string $a_format_str='', string $a_tz='')
get formatted date
const PRIVACY_IDENT_IL_UUID_USER_ID
const PRIVACY_IDENT_IL_UUID_RANDOM
const PRIVACY_IDENT_IL_UUID_LOGIN
const PRIVACY_IDENT_REAL_EMAIL
const PRIVACY_IDENT_IL_UUID_SHA256
const PRIVACY_IDENT_IL_UUID_EXT_ACCOUNT
const PRIVACY_IDENT_IL_UUID_SHA256URL
User class.
static _isAnonymous(int $usr_id)
getFullname(int $max_strlen=0)
getCreateDate()
Get create date in YYYY-MM-DD HH-MM-SS format.
ILIAS Setting Class.
static is_email(string $a_email, ?ilMailRfc822AddressParserFactory $mailAddressParserFactory=null)
This preg-based function checks whether an e-mail address is formally valid.
Interface ilDBInterface.
$res
Definition: ltiservices.php:69
$_SERVER['HTTP_HOST']
Definition: raiseError.php:26
global $DIC
Definition: shib_login.php:26