ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
class.ilCmiXapiUser.php
Go to the documentation of this file.
1<?php
2
3/* Copyright (c) 1998-2019 ILIAS open source, Extended GPL, see docs/LICENSE */
4
5
16{
17 const DB_TABLE_NAME = 'cmix_users';
18
22 protected $objId;
23
27 protected $usrId;
28
32 protected $privacyIdent;
33
37 protected $proxySuccess;
38
42 protected $satisfied;
43
47 protected $fetchUntil;
48
52 protected $usrIdent;
53
57 protected $registration;
58
59
60 public function __construct($objId = null, $usrId = null, $privacyIdent = null)
61 {
62 $this->objId = $objId;
63 $this->usrId = $usrId;
64 $this->privacyIdent = $privacyIdent;
65 $this->proxySuccess = false;
66 $this->satisfied = false;
67 $this->fetchUntil = new ilCmiXapiDateTime(0, IL_CAL_UNIX);
68 $this->usrIdent = '';
69 $this->registration = '';
70
71 if ($objId !== null && $usrId !== null && $privacyIdent !== null) {
72 $this->load();
73 }
74 }
75
79 public function getObjId()
80 {
81 return $this->objId;
82 }
83
87 public function setObjId($objId)
88 {
89 $this->objId = $objId;
90 }
91
95 public function getPrivacyIdent()
96 {
98 }
99
104 {
105 $this->privacyIdent = $privacyIdent;
106 }
107
111 public function getUsrId()
112 {
113 return $this->usrId;
114 }
115
119 public function setUsrId($usrId)
120 {
121 $this->usrId = $usrId;
122 }
123
127 public function getUsrIdent() : string
128 {
129 return $this->usrIdent;
130 }
131
135 public function setUsrIdent(string $usrIdent)
136 {
137 $this->usrIdent = $usrIdent;
138 }
139
143 public function getRegistration() : string
144 {
145 return $this->registration;
146 }
147
151 public function setRegistration(string $registration)
152 {
153 $this->registration = $registration;
154 }
155
159 public static function getIliasUuid() : string
160 {
161 $setting = new ilSetting('cmix');
162 // Fallback
163 if (!$setting->get('ilias_uuid', false)) {
164 // $uuid = (new \Ramsey\Uuid\UuidFactory())->uuid4()->toString();
165 $uuid = self::getUUID(32);
166 $setting->set('ilias_uuid', $uuid);
167 }
168 $ilUuid = $setting->get('ilias_uuid');
169 return $ilUuid;
170 }
171
175 public function hasProxySuccess()
176 {
177 return $this->proxySuccess;
178 }
179
184 {
185 $this->proxySuccess = $proxySuccess;
186 }
187
191 public function setSatisfied($satisfied)
192 {
193 $this->satisfied = $satisfied;
194 }
195
199 public function getSatisfied()
200 {
201 return $this->satisfied;
202 }
203
208 {
209 return $this->fetchUntil;
210 }
211
216 {
217 $this->fetchUntil = $fetchUntil;
218 }
219
220 public function load()
221 {
222 global $DIC; /* @var \ILIAS\DI\Container $DIC */
223
224 $res = $DIC->database()->queryF(
225 "SELECT * FROM " . self::DB_TABLE_NAME . " WHERE obj_id = %s AND usr_id = %s AND privacy_ident = %s",
226 array('integer', 'integer', 'integer'),
227 array($this->getObjId(), $this->getUsrId(), $this->getPrivacyIdent())
228 );
229
230 while ($row = $DIC->database()->fetchAssoc($res)) {
231 $this->assignFromDbRow($row);
232 }
233 }
234
235 public function assignFromDbRow($dbRow)
236 {
237 $this->setObjId((int) $dbRow['obj_id']);
238 $this->setUsrId((int) $dbRow['usr_id']);
239 $this->setProxySuccess((bool) $dbRow['proxy_success']);
240 $this->setSatisfied((bool) $dbRow['satisfied']);
241 $this->setFetchUntil(new ilCmiXapiDateTime($dbRow['fetched_until'], IL_CAL_DATETIME));
242 $this->setUsrIdent((string) $dbRow['usr_ident']);
243 $this->setPrivacyIdent((int) $dbRow['privacy_ident']);
244 $this->setRegistration((string) $dbRow['registration']);
245 }
246
247 public function save()
248 {
249 global $DIC; /* @var \ILIAS\DI\Container $DIC */
250
251 $DIC->database()->replace(
252 self::DB_TABLE_NAME,
253 array(
254 'obj_id' => array('integer', (int) $this->getObjId()),
255 'usr_id' => array('integer', (int) $this->getUsrId()),
256 'privacy_ident' => array('integer', (int) $this->getPrivacyIdent())
257 ),
258 array(
259 'proxy_success' => array('integer', (int) $this->hasProxySuccess()),
260 'fetched_until' => array('timestamp', $this->getFetchUntil()->get(IL_CAL_DATETIME)),
261 'usr_ident' => array('text', $this->getUsrIdent()),
262 'registration' => array('text', $this->getRegistration()),
263 'satisfied' => array('integer', (int) $this->getSatisfied())
264 )
265 );
266 }
267
268 // ToDo Only for Deletion -> Core
270 {
271 global $DIC; /* @var \ILIAS\DI\Container $DIC */
272 $res = $DIC->database()->queryF(
273 "SELECT * FROM " . self::DB_TABLE_NAME . " WHERE obj_id = %s AND usr_id = %s",
274 array('integer', 'integer'),
275 array($objId, $usrId)
276 );
277 $cmixUsers = array();
278 while ($row = $DIC->database()->fetchAssoc($res)) {
279 $cmixUser = new self();
280 $cmixUser->assignFromDbRow($row);
281 $cmixUsers[] = $cmixUser;
282 }
283 return $cmixUsers;
284 }
285
287 {
288 global $DIC; /* @var \ILIAS\DI\Container $DIC */
289
290 $res = $DIC->database()->queryF(
291 "SELECT * FROM " . self::DB_TABLE_NAME . " WHERE obj_id = %s AND usr_ident = %s",
292 array('integer', 'text'),
293 array($objId, $usrIdent)
294 );
295
296 $cmixUser = new self();
297
298 while ($row = $DIC->database()->fetchAssoc($res)) {
299 $cmixUser->assignFromDbRow($row);
300 }
301
302 return $cmixUser;
303 }
304
310 public static function saveProxySuccess($objId, $usrId, $privacyIdent) //TODO
311 {
312 global $DIC; /* @var \ILIAS\DI\Container $DIC */
313
314 $DIC->database()->update(
315 self::DB_TABLE_NAME,
316 array(
317 'proxy_success' => array('integer', 1)
318 ),
319 array(
320 'obj_id' => array('integer', (int) $objId),
321 'usr_id' => array('integer', (int) $usrId),
322 'privacy_ident' => array('integer', (int) $privacyIdent)
323 )
324 );
325 }
326
332 public static function getIdent($userIdentMode, ilObjUser $user)
333 {
334 switch ($userIdentMode) {
336
337 return self::buildPseudoEmail($user->getId(), self::getIliasUuid());
338
340
341 return self::buildPseudoEmail($user->getLogin(), self::getIliasUuid());
342
344
345 return self::buildPseudoEmail($user->getExternalAccount(), self::getIliasUuid());
346
348
349 return self::buildPseudoEmail(hash("sha256",'' . $user->getId() . $user->getCreateDate()), self::getIliasUuid());
350
352
353 return self::buildPseudoEmail(self::getUserObjectUniqueId(), self::getIliasUuid());
354
356
357 return $user->getEmail();
358 }
359
360 return '';
361 }
362
368 public static function getIdentAsId($userIdentMode, ilObjUser $user)
369 {
370 switch ($userIdentMode) {
372
373 return $user->getId();
374
376
377 return $user->getLogin();
378
380
381 return $user->getExternalAccount();
382
384
385 return hash("sha256",'' . $user->getId() . $user->getCreateDate());
386
388
390
392
393 return 'realemail' . $user->getId();
394 }
395
396 return '';
397 }
398
404 protected static function buildPseudoEmail($mbox, $domain)
405 {
406 return "{$mbox}@{$domain}.ilias";
407 }
408
414 public static function getName($userNameMode, ilObjUser $user)
415 {
416 switch ($userNameMode) {
418 $usrName = $user->getFirstname();
419 break;
420
422 $usrName = $user->getUTitle() ? $user->getUTitle() . ' ' : '';
423 $usrName .= $user->getLastname();
424 break;
425
427 $usrName = $user->getFullname();
428 break;
429
431 default:
432 $usrName = '-';
433 break;
434 }
435 return $usrName;
436 }
437
442 public static function getUsersForObject($objId, $asUsrId = false) : array
443 {
444 global $DIC; /* @var \ILIAS\DI\Container $DIC */
445
446 $res = $DIC->database()->queryF(
447 "SELECT * FROM " . self::DB_TABLE_NAME . " WHERE obj_id = %s",
448 array('integer'),
449 array($objId)
450 );
451
452 $users = [];
453
454 if ($asUsrId === false)
455 {
456 while ($row = $DIC->database()->fetchAssoc($res))
457 {
458 $cmixUser = new self();
459 $cmixUser->assignFromDbRow($row);
460
461 $users[] = $cmixUser;
462 }
463 }
464 else
465 {
466 while ($row = $DIC->database()->fetchAssoc($res))
467 {
468 $users[] = $row['usr_id'];
469 }
470 }
471 return $users;
472 }
473
479 public static function getUserIdents($objId, $usrId) : array
480 {
481 global $DIC; /* @var \ILIAS\DI\Container $DIC */
482
483 $res = $DIC->database()->queryF(
484 "SELECT usr_ident FROM " . self::DB_TABLE_NAME . " WHERE obj_id = %s AND usr_id = %s",
485 array('integer','integer'),
486 array($objId,$usrId)
487 );
488
489 $usrIdents = [];
490 while ($row = $DIC->database()->fetchAssoc($res))
491 {
492 $usrIdents[] = $row['usr_ident'];
493 }
494 return $usrIdents;
495 }
496
501 public static function deleteUsersForObject(int $objId, ?array $users = [])
502 {
503 global $DIC; /* @var \ILIAS\DI\Container $DIC */
504 $query = "DELETE FROM " . self::DB_TABLE_NAME . " WHERE obj_id = ".$DIC->database()->quote($objId, 'integer');
505 if (count($users) == 0) {
506 $DIC->database()->manipulate($query);
507 }
508 else {
509 $DIC->database()->manipulateF($query." AND usr_id = %s",
510 array('integer'),
511 $users
512 );
513 }
514 }
515
516 // $withIdent requires constructed object with privacyIdent!
517 public static function exists($objId, $usrId, $privacyIdent = 999)
518 {
519 global $DIC; /* @var \ILIAS\DI\Container $DIC */
520 if ($privacyIdent == 999)
521 {
522 $query = "SELECT count(*) cnt FROM " . self::DB_TABLE_NAME . " WHERE obj_id = %s AND usr_id = %s";
523 $res = $DIC->database()->queryF(
524 $query,
525 array('integer', 'integer'),
526 array($objId, $usrId)
527 );
528 }
529 else
530 {
531 $query = "SELECT count(*) cnt FROM " . self::DB_TABLE_NAME . " WHERE obj_id = %s AND usr_id = %s AND privacy_ident = %s";
532 $res = $DIC->database()->queryF(
533 $query,
534 array('integer', 'integer', 'integer'),
536 );
537 }
538
539 while ($row = $DIC->database()->fetchAssoc($res)) {
540 return (bool) $row['cnt'];
541 }
542
543 return false;
544 }
545
547 {
548 global $DIC; /* @var \ILIAS\DI\Container $DIC */
549
550 $query = "
551 SELECT DISTINCT cu.obj_id
552 FROM " . self::DB_TABLE_NAME . " cu
553 INNER JOIN object_data od
554 ON od.obj_id = cu.obj_id
555 AND od.type = 'cmix'
556 WHERE cu.proxy_success != %s
557 ";
558
559 $res = $DIC->database()->queryF($query, array('integer'), array(1));
560
561 $objects = array();
562
563 while ($row = $DIC->database()->fetchAssoc($res)) {
564 $objects[] = $row['obj_id'];
565 }
566
567 return $objects;
568 }
569
570 public static function updateFetchedUntilForObjects(ilCmiXapiDateTime $fetchedUntil, $objectIds)
571 {
572 global $DIC; /* @var \ILIAS\DI\Container $DIC */
573
574 $IN_objIds = $DIC->database()->in('obj_id', $objectIds, false, 'integer');
575
576 $query = "UPDATE " . self::DB_TABLE_NAME . " SET fetched_until = %s WHERE $IN_objIds";
577 $DIC->database()->manipulateF($query, array('timestamp'), array($fetchedUntil->get(IL_CAL_DATETIME)));
578 }
579
580 public static function lookupObjectIds($usrId, $type = '')
581 {
582 global $DIC; /* @var \ILIAS\DI\Container $DIC */
583
584 $TYPE_JOIN = '';
585
586 if (strlen($type)) {
587 $TYPE_JOIN = "
588 INNER JOIN object_data od
589 ON od.obj_id = cu.obj_id
590 AND od.type = {$DIC->database()->quote($type, 'text')}
591 ";
592 }
593
594 $query = "
595 SELECT cu.obj_id
596 FROM " . self::DB_TABLE_NAME . " cu
597 {$TYPE_JOIN}
598 WHERE cu.usr_id = {$DIC->database()->quote($usrId, 'integer')}
599 ";
600
601 $res = $DIC->database()->query($query);
602
603 $objIds = [];
604
605 while ($row = $DIC->database()->fetchAssoc($res)) {
606 $objIds[] = $row['obj_id'];
607 }
608
609 return $objIds;
610 }
615 public static function getUserObjectUniqueId( $length = 32 )
616 {
617 // $storedId = self::readUserObjectUniqueId();
618 // if( (bool)strlen($storedId) ) {
619 // return strstr($storedId,'@', true);
620 // }
621
622 // $getId = function( $length ) {
623 // $multiplier = floor($length/8) * 2;
624 // $uid = str_shuffle(str_repeat(uniqid(), $multiplier));
625
626 // try {
627 // $ident = bin2hex(random_bytes($length));
628 // } catch (Exception $e) {
629 // $ident = $uid;
630 // }
631
632 // $start = rand(0, strlen($ident) - $length - 1);
633 // return substr($ident, $start, $length);
634 // };
635
636 $id = self::getUUID($length);//$getId($length);
637 $exists = self::userObjectUniqueIdExists($id);
638 while( $exists ) {
639 $id = self::getUUID($length);//$getId($length);
640 $exists = self::userObjectUniqueIdExists($id);
641 }
642
643 return $id;
644
645 }
646
647 public static function getUUID($length = 32 )
648 {
649 $multiplier = floor($length/8) * 2;
650 $uid = str_shuffle(str_repeat(uniqid(), $multiplier));
651
652 try {
653 $ident = bin2hex(random_bytes($length));
654 } catch (Exception $e) {
655 $ident = $uid;
656 }
657
658 $start = rand(0, strlen($ident) - $length - 1);
659 return substr($ident, $start, $length);
660 }
661
662 private static function userObjectUniqueIdExists($id)
663 {
664 global $DIC; /* @var \ILIAS\DI\Container $DIC */
665
666 $query = "SELECT usr_ident FROM " . self::DB_TABLE_NAME . " WHERE " . $DIC->database()->like('usr_ident', 'text', $id . '@%');
667 $result = $DIC->database()->query($query);
668 if ($result->numRows() == 0) {
669 return false;
670 }
671 return true;
672 }
673
674 public static function generateCMI5Registration($objId, $usrId)
675 {
676 return (new \Ramsey\Uuid\UuidFactory())->uuid3(self::getIliasUuid(),$objId . '-' . $usrId);
677 }
678
679 public static function generateRegistration(ilObjCmiXapi $obj, ilObjUser $user)
680 {
681 return (new \Ramsey\Uuid\UuidFactory())->uuid3(self::getIliasUuid(),$obj->getRefId() . '-' . $user->getId());
682 }
683
684 public static function getCMI5RegistrationFromAuthToken(ilCmiXapiAuthToken $authToken): \Ramsey\Uuid\UuidInterface
685 {
686 return (new \Ramsey\Uuid\UuidFactory())->uuid3(self::getIliasUuid(), $authToken->getObjId() . '-' . $authToken->getUsrId());
687 }
688
689 public static function getRegistrationFromAuthToken(ilCmiXapiAuthToken $authToken): \Ramsey\Uuid\UuidInterface
690 {
691 return (new \Ramsey\Uuid\UuidFactory())->uuid3(self::getIliasUuid(), $authToken->getRefId() . '-' . $authToken->getUsrId());
692 }
693}
$result
An exception for terminatinating execution or to throw for unit testing.
const IL_CAL_UNIX
const IL_CAL_DATETIME
setRegistration(string $registration)
static saveProxySuccess($objId, $usrId, $privacyIdent)
setSatisfied($satisfied)
static deleteUsersForObject(int $objId, ?array $users=[])
static buildPseudoEmail($mbox, $domain)
static updateFetchedUntilForObjects(ilCmiXapiDateTime $fetchedUntil, $objectIds)
setProxySuccess($proxySuccess)
static getUUID($length=32)
static getInstancesByObjectIdAndUsrId($objId, $usrId)
static userObjectUniqueIdExists($id)
static getIdent($userIdentMode, ilObjUser $user)
setPrivacyIdent($privacyIdent)
static getName($userNameMode, ilObjUser $user)
static getUsersForObject($objId, $asUsrId=false)
static lookupObjectIds($usrId, $type='')
static getUserIdents($objId, $usrId)
static getCMI5RegistrationFromAuthToken(ilCmiXapiAuthToken $authToken)
__construct($objId=null, $usrId=null, $privacyIdent=null)
static getInstanceByObjectIdAndUsrIdent($objId, $usrIdent)
setFetchUntil(ilCmiXapiDateTime $fetchUntil)
static getUserObjectUniqueId( $length=32)
static generateCMI5Registration($objId, $usrId)
static getIdentAsId($userIdentMode, ilObjUser $user)
static exists($objId, $usrId, $privacyIdent=999)
static getRegistrationFromAuthToken(ilCmiXapiAuthToken $authToken)
setUsrIdent(string $usrIdent)
static generateRegistration(ilObjCmiXapi $obj, ilObjUser $user)
static getCmixObjectsHavingUsersMissingProxySuccess()
get($a_format, $a_format_str='', $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
getLastname()
get lastname @access public
getUTitle()
get user title (note: don't mix up this method with getTitle() that is derived from ilObject and gets...
getEmail()
get email address @access public
getFirstname()
get firstname @access public
getLogin()
get login / username @access public
getFullname($a_max_strlen=0)
get fullname @access public
getExternalAccount()
get external account
getRefId()
get reference id @access public
getId()
get object id @access public
getCreateDate()
get create date @access public
ILIAS Setting Class.
$query
$type
foreach($_POST as $key=> $value) $res
$DIC
Definition: xapitoken.php:46