ILIAS  release_7 Revision v7.30-3-g800a261c036
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 if (!ilObjUser::_isAnonymous($this->getUsrId())) {
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
269 // ToDo Only for Deletion -> Core
271 {
272 global $DIC; /* @var \ILIAS\DI\Container $DIC */
273 $res = $DIC->database()->queryF(
274 "SELECT * FROM " . self::DB_TABLE_NAME . " WHERE obj_id = %s AND usr_id = %s",
275 array('integer', 'integer'),
276 array($objId, $usrId)
277 );
278 $cmixUsers = array();
279 while ($row = $DIC->database()->fetchAssoc($res)) {
280 $cmixUser = new self();
281 $cmixUser->assignFromDbRow($row);
282 $cmixUsers[] = $cmixUser;
283 }
284 return $cmixUsers;
285 }
286
288 {
289 global $DIC; /* @var \ILIAS\DI\Container $DIC */
290
291 $res = $DIC->database()->queryF(
292 "SELECT * FROM " . self::DB_TABLE_NAME . " WHERE obj_id = %s AND usr_ident = %s",
293 array('integer', 'text'),
294 array($objId, $usrIdent)
295 );
296
297 $cmixUser = new self();
298
299 while ($row = $DIC->database()->fetchAssoc($res)) {
300 $cmixUser->assignFromDbRow($row);
301 }
302
303 return $cmixUser;
304 }
305
311 public static function saveProxySuccess($objId, $usrId, $privacyIdent) //TODO
312 {
313 global $DIC; /* @var \ILIAS\DI\Container $DIC */
314
315 $DIC->database()->update(
316 self::DB_TABLE_NAME,
317 array(
318 'proxy_success' => array('integer', 1)
319 ),
320 array(
321 'obj_id' => array('integer', (int) $objId),
322 'usr_id' => array('integer', (int) $usrId),
323 'privacy_ident' => array('integer', (int) $privacyIdent)
324 )
325 );
326 }
327
333 public static function getIdent($userIdentMode, ilObjUser $user)
334 {
335 if (ilObjUser::_isAnonymous($user->getId())) {
336 return self::buildPseudoEmail(hash("sha256", (string) microtime()), self::getIliasUuid());
337 }
338 switch ($userIdentMode) {
340
341 return self::buildPseudoEmail($user->getId(), self::getIliasUuid());
342
344
345 if (!ilUtil::is_email($user->getLogin())) {
346 return self::buildPseudoEmail($user->getLogin(), self::getIliasUuid());
347 } else {
348 return $user->getLogin();
349 }
350
351 // no break
353
354 return self::buildPseudoEmail($user->getExternalAccount(), self::getIliasUuid());
355
357
358 return self::buildPseudoEmail(hash("sha256", '' . $user->getId() . $user->getCreateDate()), self::getIliasUuid());
359
361 $tmpHash = hash("sha256", '' . $user->getId() . $user->getCreateDate()) . '@' . str_replace('www.', '', $_SERVER['HTTP_HOST']);
362 if (strlen($tmpHash) > 80) {
363 $tmpHash = substr($tmpHash, strlen($tmpHash) - 80);
364 }
365 return $tmpHash;
366
368
369 return self::buildPseudoEmail(self::getUserObjectUniqueId(), self::getIliasUuid());
370
372
373 return $user->getEmail();
374 }
375
376 return '';
377 }
378
384 public static function getIdentAsId($userIdentMode, ilObjUser $user)
385 {
386 switch ($userIdentMode) {
388
389 return $user->getId();
390
392
393 return $user->getLogin();
394
396
397 return $user->getExternalAccount();
398
400
401 return hash("sha256", '' . $user->getId() . $user->getCreateDate());
402
404 $tmpHash = hash("sha256", '' . $user->getId() . $user->getCreateDate());
405 $tmpHost = '@' . str_replace('www.', '', $_SERVER['HTTP_HOST']);
406 if (strlen($tmpHash . $tmpHost) > 80) {
407 $tmpHash = substr($tmpHash, strlen($tmpHash) - (80 - strlen($tmpHost)));
408 }
409 return $tmpHash;
410
412
414
416
417 return 'realemail' . $user->getId();
418 }
419
420 return '';
421 }
422
428 protected static function buildPseudoEmail($mbox, $domain)
429 {
430 return "{$mbox}@{$domain}.ilias";
431 }
432
438 public static function getName($userNameMode, ilObjUser $user)
439 {
440 switch ($userNameMode) {
442 $usrName = $user->getFirstname();
443 break;
444
446 $usrName = $user->getUTitle() ? $user->getUTitle() . ' ' : '';
447 $usrName .= $user->getLastname();
448 break;
449
451 $usrName = $user->getFullname();
452 break;
453
455 default:
456 $usrName = '-';
457 break;
458 }
459 return $usrName;
460 }
461
466 public static function getUsersForObject($objId, $asUsrId = false) : array
467 {
468 global $DIC; /* @var \ILIAS\DI\Container $DIC */
469
470 $res = $DIC->database()->queryF(
471 "SELECT * FROM " . self::DB_TABLE_NAME . " WHERE obj_id = %s",
472 array('integer'),
473 array($objId)
474 );
475
476 $users = [];
477
478 if ($asUsrId === false) {
479 while ($row = $DIC->database()->fetchAssoc($res)) {
480 $cmixUser = new self();
481 $cmixUser->assignFromDbRow($row);
482
483 $users[] = $cmixUser;
484 }
485 } else {
486 while ($row = $DIC->database()->fetchAssoc($res)) {
487 $users[] = $row['usr_id'];
488 }
489 }
490 return $users;
491 }
492
498 public static function getUserIdents($objId, $usrId) : array
499 {
500 global $DIC; /* @var \ILIAS\DI\Container $DIC */
501
502 $res = $DIC->database()->queryF(
503 "SELECT usr_ident FROM " . self::DB_TABLE_NAME . " WHERE obj_id = %s AND usr_id = %s",
504 array('integer','integer'),
505 array($objId,$usrId)
506 );
507
508 $usrIdents = [];
509 while ($row = $DIC->database()->fetchAssoc($res)) {
510 $usrIdents[] = $row['usr_ident'];
511 }
512 return $usrIdents;
513 }
514
519 public static function deleteUsersForObject(int $objId, ?array $users = [])
520 {
521 global $DIC; /* @var \ILIAS\DI\Container $DIC */
522 $query = "DELETE FROM " . self::DB_TABLE_NAME . " WHERE obj_id = " . $DIC->database()->quote($objId, 'integer');
523 if (count($users) == 0) {
524 $DIC->database()->manipulate($query);
525 } else {
526 $DIC->database()->manipulateF(
527 $query . " AND usr_id = %s",
528 array('integer'),
529 $users
530 );
531 }
532 }
533
534 // $withIdent requires constructed object with privacyIdent!
535 public static function exists($objId, $usrId, $privacyIdent = 999)
536 {
537 global $DIC; /* @var \ILIAS\DI\Container $DIC */
538 if ($privacyIdent == 999) {
539 $query = "SELECT count(*) cnt FROM " . self::DB_TABLE_NAME . " WHERE obj_id = %s AND usr_id = %s";
540 $res = $DIC->database()->queryF(
541 $query,
542 array('integer', 'integer'),
543 array($objId, $usrId)
544 );
545 } else {
546 $query = "SELECT count(*) cnt FROM " . self::DB_TABLE_NAME . " WHERE obj_id = %s AND usr_id = %s AND privacy_ident = %s";
547 $res = $DIC->database()->queryF(
548 $query,
549 array('integer', 'integer', 'integer'),
551 );
552 }
553
554 while ($row = $DIC->database()->fetchAssoc($res)) {
555 return (bool) $row['cnt'];
556 }
557
558 return false;
559 }
560
562 {
563 global $DIC; /* @var \ILIAS\DI\Container $DIC */
564
565 $query = "
566 SELECT DISTINCT cu.obj_id
567 FROM " . self::DB_TABLE_NAME . " cu
568 INNER JOIN object_data od
569 ON od.obj_id = cu.obj_id
570 AND od.type = 'cmix'
571 WHERE cu.proxy_success != %s
572 ";
573
574 $res = $DIC->database()->queryF($query, array('integer'), array(1));
575
576 $objects = array();
577
578 while ($row = $DIC->database()->fetchAssoc($res)) {
579 $objects[] = $row['obj_id'];
580 }
581
582 return $objects;
583 }
584
585 public static function updateFetchedUntilForObjects(ilCmiXapiDateTime $fetchedUntil, $objectIds)
586 {
587 global $DIC; /* @var \ILIAS\DI\Container $DIC */
588
589 $IN_objIds = $DIC->database()->in('obj_id', $objectIds, false, 'integer');
590
591 $query = "UPDATE " . self::DB_TABLE_NAME . " SET fetched_until = %s WHERE $IN_objIds";
592 $DIC->database()->manipulateF($query, array('timestamp'), array($fetchedUntil->get(IL_CAL_DATETIME)));
593 }
594
595 public static function lookupObjectIds($usrId, $type = '')
596 {
597 global $DIC; /* @var \ILIAS\DI\Container $DIC */
598
599 $TYPE_JOIN = '';
600
601 if (strlen($type)) {
602 $TYPE_JOIN = "
603 INNER JOIN object_data od
604 ON od.obj_id = cu.obj_id
605 AND od.type = {$DIC->database()->quote($type, 'text')}
606 ";
607 }
608
609 $query = "
610 SELECT cu.obj_id
611 FROM " . self::DB_TABLE_NAME . " cu
612 {$TYPE_JOIN}
613 WHERE cu.usr_id = {$DIC->database()->quote($usrId, 'integer')}
614 ";
615
616 $res = $DIC->database()->query($query);
617
618 $objIds = [];
619
620 while ($row = $DIC->database()->fetchAssoc($res)) {
621 $objIds[] = $row['obj_id'];
622 }
623
624 return $objIds;
625 }
630 public static function getUserObjectUniqueId($length = 32)
631 {
632 // $storedId = self::readUserObjectUniqueId();
633 // if( (bool)strlen($storedId) ) {
634 // return strstr($storedId,'@', true);
635 // }
636
637 // $getId = function( $length ) {
638 // $multiplier = floor($length/8) * 2;
639 // $uid = str_shuffle(str_repeat(uniqid(), $multiplier));
640
641 // try {
642 // $ident = bin2hex(random_bytes($length));
643 // } catch (Exception $e) {
644 // $ident = $uid;
645 // }
646
647 // $start = rand(0, strlen($ident) - $length - 1);
648 // return substr($ident, $start, $length);
649 // };
650
651 $id = self::getUUID($length);//$getId($length);
652 $exists = self::userObjectUniqueIdExists($id);
653 while ($exists) {
654 $id = self::getUUID($length);//$getId($length);
655 $exists = self::userObjectUniqueIdExists($id);
656 }
657
658 return $id;
659 }
660
661 public static function getUUID($length = 32)
662 {
663 $multiplier = floor($length / 8) * 2;
664 $uid = str_shuffle(str_repeat(uniqid(), $multiplier));
665
666 try {
667 $ident = bin2hex(random_bytes($length));
668 } catch (Exception $e) {
669 $ident = $uid;
670 }
671
672 $start = rand(0, strlen($ident) - $length - 1);
673 return substr($ident, $start, $length);
674 }
675
676 private static function userObjectUniqueIdExists($id)
677 {
678 global $DIC; /* @var \ILIAS\DI\Container $DIC */
679
680 $query = "SELECT usr_ident FROM " . self::DB_TABLE_NAME . " WHERE " . $DIC->database()->like('usr_ident', 'text', $id . '@%');
681 $result = $DIC->database()->query($query);
682 if ($result->numRows() == 0) {
683 return false;
684 }
685 return true;
686 }
687
688 public static function generateCMI5Registration($objId, $usrId)
689 {
690 return (new \Ramsey\Uuid\UuidFactory())->uuid3(self::getIliasUuid(), $objId . '-' . $usrId);
691 }
692
693 public static function generateRegistration(ilObjCmiXapi $obj, ilObjUser $user)
694 {
695 return (new \Ramsey\Uuid\UuidFactory())->uuid3(self::getIliasUuid(), $obj->getRefId() . '-' . $user->getId());
696 }
697
698 public static function getCMI5RegistrationFromAuthToken(ilCmiXapiAuthToken $authToken) : \Ramsey\Uuid\UuidInterface
699 {
700 return (new \Ramsey\Uuid\UuidFactory())->uuid3(self::getIliasUuid(), $authToken->getObjId() . '-' . $authToken->getUsrId());
701 }
702
703 public static function getRegistrationFromAuthToken(ilCmiXapiAuthToken $authToken) : \Ramsey\Uuid\UuidInterface
704 {
705 return (new \Ramsey\Uuid\UuidFactory())->uuid3(self::getIliasUuid(), $authToken->getRefId() . '-' . $authToken->getUsrId());
706 }
707}
$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 getUserObjectUniqueId($length=32)
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 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
const PRIVACY_IDENT_IL_UUID_SHA256URL
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
static _isAnonymous($usr_id)
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.
static is_email($a_email, ilMailRfc822AddressParserFactory $mailAddressParserFactory=null)
This preg-based function checks whether an e-mail address is formally valid.
global $DIC
Definition: goto.php:24
$query
$type
$_SERVER['HTTP_HOST']
Definition: raiseError.php:10
foreach($_POST as $key=> $value) $res