forked from Aspen-Discovery/aspen-discovery
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUser.php
5957 lines (5453 loc) · 215 KB
/
User.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php /** @noinspection PhpMissingFieldTypeInspection */
require_once ROOT_DIR . '/sys/DB/DataObject.php';
class User extends DataObject {
public $__table = 'user'; // table name
public $id;
public $source;
public $username;
public $unique_ils_id;
public $cat_username; //Old field for barcode/username from the ILS deprecated
public $cat_password; //Old field barcode/username from the ILS deprecated
public $ils_barcode; //The barcode for the user as stored within the ILS. Will be null for admin users and users not stored in the ils
public $ils_username; //A custom username for the user as stored within the ILS that can be used for login rather than using the barcode.
public $ils_password; //The password to use when logging in
public $displayName;
public $password;
public $firstname;
public $lastname;
public $email;
public $phone;
public $patronType;
public $created; // datetime(19) not_null binary
public $homeLocationId; // int(11)
public $myLocation1Id; // int(11)
public $myLocation2Id; // int(11)
public $trackReadingHistory; // tinyint
public $initialReadingHistoryLoaded;
public $lastReadingHistoryUpdate;
public $bypassAutoLogout; //tinyint
public $disableRecommendations; //tinyint
public $disableCoverArt; //tinyint
public $overdriveEmail;
public $promptForOverdriveEmail; //Semantics of this have changed to not prompting for hold settings
public $hooplaCheckOutConfirmation;
public $promptForAxis360Email;
public $axis360Email;
public $preferredLibraryInterface;
public $preferredTheme;
public $noPromptForUserReviews; //tinyint(1)
public $lockedFacets;
public $alternateLibraryCard;
public $alternateLibraryCardPassword;
public $hideResearchStarters;
public $disableAccountLinking;
public $disableCirculationActions;
public $oAuthAccessToken;
public $oAuthRefreshToken;
public $isLoggedInViaSSO;
public $userCookiePreferenceEssential;
public $userCookiePreferenceAnalytics;
public $userCookiePreferenceLocalAnalytics;
public $holdInfoLastLoaded;
public $checkoutInfoLastLoaded;
public $onboardAppNotifications;
public $shouldAskBrightness;
/** @var Role[] */
private $_roles;
private $_permissions;
private $_masqueradingRoles;
private $_additionalAdministrationLocations;
public $interfaceLanguage;
public $searchPreferenceLanguage;
public $rememberHoldPickupLocation;
public $pickupLocationId;
public $pickupSublocationId;
public $lastListUsed;
public $browseAddToHome;
public $lastLoginValidation;
public $twoFactorStatus; //Whether the user has enrolled
public $twoFactorAuthSettingId; //The settings based on their PType
public $updateMessage;
public $updateMessageIsError;
public $proPayPayerAccountId;
public $enableCostSavings;
public $totalCostSavings;
public $currentCostSavings;
public $isLocalTestUser;
/** @var User $parentUser */
private $parentUser;
/** @var User[] $linkedUsers */
private $linkedUsers;
private $viewers;
//Data that we load, but don't store in the User table
public $_fullname;
public $_preferredName;
public $_address1;
public $_address2;
public $_city;
public $_state;
public $_zip;
public $_workPhone;
public $_mobileNumber;
public $_web_note;
public $_expires;
public $_expired;
public $_expireClose;
public $_isBlockedFromIllRequests;
public $_fines;
public $_finesVal;
public $_homeLibrary;
public $_homeLocationCode;
public $_homeLocation;
public $_myLocation1;
public $_myLocation2;
public $_numCheckedOutIls;
public $_numHoldsIls;
public $_numHoldsAvailableIls;
public $_numHoldsRequestedIls;
private $_numCheckedOutOverDrive = 0;
private $_numHoldsOverDrive = 0;
private $_numHoldsAvailableOverDrive = 0;
private $_numCheckedOutHoopla = 0;
public $_notices;
public $_billingNotices = "-";
public $_noticePreferenceLabel;
private $_numMaterialsRequests = 0;
private $_readingHistorySize = 0;
// CarlX Option
public $_emailReceiptFlag;
public $_availableHoldNotice;
public $_comingDueNotice;
public $_phoneType;
//Staff Settings
public $materialsRequestEmailSignature;
public $materialsRequestReplyToAddress;
public $materialsRequestSendEmailOnAssign;
function getNumericColumnNames(): array {
return [
'id',
'trackReadingHistory',
'hooplaCheckOutConfirmation',
'initialReadingHistoryLoaded',
'updateMessageIsError',
'rememberHoldPickupLocation',
'materialsRequestSendEmailOnAssign',
'isLocalTestUser'
];
}
function getEncryptedFieldNames(): array {
return [
'password',
'cat_password',
'ils_password',
'firstname',
'lastname',
'email',
'displayName',
'phone',
'overdriveEmail',
'alternateLibraryCardPassword',
'axis360Email',
];
}
public function getUniquenessFields(): array {
return [
'source',
'username',
];
}
function getLists() {
require_once ROOT_DIR . '/sys/UserLists/UserList.php';
$lists = [];
$list = new UserList();
$list->user_id = $this->id;
$list->orderBy('title');
if ($list->find()) {
while ($list->fetch()) {
$lists[] = clone($list);
}
}
return $lists;
}
protected ?CatalogConnection $_catalogDriver = null;
/**
* Get a connection to the catalog for the user
*
* @return null|CatalogConnection
*/
function getCatalogDriver(): ?CatalogConnection {
if ($this->_catalogDriver == null) {
//Based off the source of the user, get the AccountProfile
$accountProfile = $this->getAccountProfile();
if ($accountProfile) {
$catalogDriver = trim($accountProfile->driver);
if (!empty($catalogDriver)) {
$this->_catalogDriver = CatalogFactory::getCatalogConnectionInstance($catalogDriver, $accountProfile);
}
}
}
return $this->_catalogDriver;
}
function hasIlsConnection() {
$driver = $this->getCatalogDriver();
if ($driver == null) {
return false;
} else {
if ($driver->driver == null) {
return false;
}
}
return true;
}
/** @var AccountProfile */
protected $_accountProfile;
/**
* @return AccountProfile
*/
function getAccountProfile() {
if ($this->_accountProfile != null) {
return $this->_accountProfile;
}
require_once ROOT_DIR . '/sys/Account/AccountProfile.php';
$accountProfile = new AccountProfile();
$accountProfile->name = $this->source;
if ($accountProfile->find(true)) {
$this->_accountProfile = $accountProfile;
} else {
$this->_accountProfile = null;
}
return $this->_accountProfile;
}
function __get($name) {
if ($name == 'roles') {
return $this->getRoles();
} elseif ($name == 'linkedUsers') {
return $this->getLinkedUsers();
} elseif ($name == 'additionalAdministrationLocations') {
return $this->getAdditionalAdministrationLocations();
} elseif ($name == 'homeLibraryName') {
return $this->getHomeLibrarySystemName();
} elseif ($name == 'homeLocation') {
return $this->getHomeLocationName();
} else {
return parent::__get($name);
}
}
function __set($name, $value) {
if ($name == 'roles') {
$this->setRoles($value);
} elseif ($name == 'additionalAdministrationLocations') {
$this->setAdditionalAdministrationLocations($value);
} else {
parent::__set($name, $value);
}
}
public function delete($useWhere = false): int {
$ret = parent::delete($useWhere);
if ($ret) {
// delete browse_category_dismissal
require_once ROOT_DIR . '/sys/Browse/BrowseCategoryDismissal.php';
$browseCategoryDismissals = new BrowseCategoryDismissal();
$browseCategoryDismissals->userId = $this->id;
$browseCategoryDismissals->find();
while ($browseCategoryDismissals->fetch()) {
$browseCategoryDismissals->delete();
}
// delete placard_dismissal
require_once ROOT_DIR . '/sys/LocalEnrichment/PlacardDismissal.php';
$placardDismissals = new PlacardDismissal();
$placardDismissals->userId = $this->id;
$placardDismissals->find();
while ($placardDismissals->fetch()) {
$placardDismissals->delete();
}
// delete system_message_dismissal
require_once ROOT_DIR . '/sys/LocalEnrichment/SystemMessageDismissal.php';
$systemMessageDismissals = new SystemMessageDismissal();
$systemMessageDismissals->userId = $this->id;
$systemMessageDismissals->find();
while ($systemMessageDismissals->fetch()) {
$systemMessageDismissals->delete();
}
// delete user_checkout
require_once ROOT_DIR . '/sys/User/Checkout.php';
$userCheckouts = new Checkout();
$userCheckouts->userId = $this->id;
$userCheckouts->find();
while ($userCheckouts->fetch()) {
$userCheckouts->delete();
}
// delete user_events_entry
require_once ROOT_DIR . '/sys/Events/UserEventsEntry.php';
$userEventsEntry = new UserEventsEntry();
$userEventsEntry->userId = $this->id;
$userEventsEntry->find();
while ($userEventsEntry->fetch()) {
$userEventsEntry->delete();
}
// delete user_events_registration
require_once ROOT_DIR . '/sys/Events/UserEventsRegistrations.php';
$userEventsRegistration = new UserEventsRegistrations();
$userEventsRegistration->userId = $this->id;
$userEventsRegistration->find();
while ($userEventsRegistration->fetch()) {
$userEventsRegistration->delete();
}
// delete user_hold
require_once ROOT_DIR . '/sys/User/Hold.php';
$userHolds = new Hold();
$userHolds->userId = $this->id;
$userHolds->find();
while ($userHolds->fetch()) {
$userHolds->delete();
}
// delete user_ils_message
require_once ROOT_DIR . '/sys/Account/UserILSMessage.php';
$userILSMessage = new UserILSMessage();
$userILSMessage->userId = $this->id;
$userILSMessage->find();
while ($userILSMessage->fetch()) {
$userILSMessage->delete();
}
// delete user_link
require_once ROOT_DIR . '/sys/Account/UserLink.php';
$userLink = new UserLink();
$userLink->primaryAccountId = $this->id;
$userLink->find();
while ($userLink->fetch()) {
$userLink->delete();
}
$userLink = new UserLink();
$userLink->linkedAccountId = $this->id;
$userLink->find();
while ($userLink->fetch()) {
$userLink->delete();
}
// delete user_list
require_once ROOT_DIR . '/sys/UserLists/UserList.php';
$userList = new UserList();
$userList->user_id = $this->id;
$userList->find();
while ($userList->fetch()) {
// delete user_list_entry
$userListEntry = new UserListEntry();
$userListEntry->listId = $userList->id;
$userListEntry->find();
while ($userListEntry->fetch()) {
$userListEntry->delete();
}
$userList->delete();
}
// delete user_messages
require_once ROOT_DIR . '/sys/Account/UserMessage.php';
$userMessage = new UserMessage();
$userMessage->userId = $this->id;
$userMessage->find();
while ($userMessage->fetch()) {
$userMessage->delete();
}
// delete user_not_interested
require_once ROOT_DIR . '/sys/LocalEnrichment/NotInterested.php';
$userNotInterested = new NotInterested();
$userNotInterested->userId = $this->id;
$userNotInterested->find();
while ($userNotInterested->fetch()) {
$userNotInterested->delete();
}
// delete user_notification_tokens
require_once ROOT_DIR . '/sys/Account/UserNotificationToken.php';
$userNotificationToken = new UserNotificationToken();
$userNotificationToken->userId = $this->id;
$userNotificationToken->find();
while ($userNotificationToken->fetch()) {
$userNotificationToken->delete();
}
// delete user_notifications
require_once ROOT_DIR . '/sys/Account/UserNotification.php';
$userNotification = new UserNotification();
$userNotification->userId = $this->id;
$userNotification->find();
while ($userNotification->fetch()) {
$userNotification->delete();
}
// delete user_reading_history_work
require_once ROOT_DIR . '/sys/ReadingHistoryEntry.php';
$userReadingHistory = new ReadingHistoryEntry();
$userReadingHistory->userId = $this->id;
$userReadingHistory->find();
while ($userReadingHistory->fetch()) {
$userReadingHistory->delete();
}
// delete user_roles
require_once ROOT_DIR . '/sys/Administration/UserRoles.php';
$userRoles = new UserRoles();
$userRoles->userId = $this->id;
$userRoles->find();
while ($userRoles->fetch()) {
$userRoles->delete();
}
// delete materials_request (createdBy)
require_once ROOT_DIR . '/sys/MaterialsRequests/MaterialsRequest.php';
$userMaterialsRequests = new MaterialsRequest();
$userMaterialsRequests->createdBy = $this->id;
$userMaterialsRequests->find();
while ($userMaterialsRequests->fetch()) {
$userMaterialsRequests->delete();
}
}
return $ret;
}
function setRoles($values) {
$rolesToAssign = [];
foreach ($values as $index => $value) {
if (is_object($value)) {
$rolesToAssign[$index] = $value;
} else {
$role = new Role();
$role->roleId = $value;
if ($role->find(true)) {
$rolesToAssign[$role->roleId] = clone $role;
}
}
}
$this->_roles = $rolesToAssign;
//Update the database, first remove existing values
$this->saveRoles();
}
function getRoles(): array {
if (is_null($this->_roles)) {
$this->_roles = [];
//Load roles for the user from the user
require_once ROOT_DIR . '/sys/Administration/Role.php';
require_once ROOT_DIR . '/sys/Account/PType.php';
$role = new Role();
$canUseTestRoles = false;
if ($this->id) {
//Get role based on patron type
$patronType = $this->getPTypeObj();
if (!empty($patronType)) {
if ($patronType->assignedRoleId != -1) {
$role = new Role();
$role->roleId = $patronType->assignedRoleId;
if ($role->find(true)) {
$role->setAssignedFromPType(true);
$this->_roles[$role->roleId] = clone $role;
if ($this->_roles[$role->roleId]->hasPermission('Test Roles')) {
$canUseTestRoles = true;
}
}
}
}
$escapedId = $this->escape($this->id);
/** @noinspection SqlResolve */
$role->query("SELECT roles.* FROM roles INNER JOIN user_roles ON roles.roleId = user_roles.roleId WHERE userId = " . $escapedId . " ORDER BY name");
while ($role->fetch()) {
$this->_roles[$role->roleId] = clone $role;
if ($this->_roles[$role->roleId]->hasPermission('Test Roles')) {
$canUseTestRoles = true;
}
}
}
//Set up a test role if provided
$testRole = '';
if (isset($_REQUEST['test_role'])) {
$testRole = $_REQUEST['test_role'];
} elseif (isset($_COOKIE['test_role'])) {
$testRole = $_COOKIE['test_role'];
}
if ($canUseTestRoles && $testRole != '') {
if (is_array($testRole)) {
$testRoles = $testRole;
} else {
$testRoles = [$testRole];
}
foreach ($testRoles as $tmpRole) {
$role = new Role();
if (is_numeric($tmpRole)) {
$role->roleId = $tmpRole;
} else {
$role->name = $tmpRole;
}
$found = $role->find(true);
if ($found == true) {
$this->_roles[$role->roleId] = clone $role;
}
}
}
}
return $this->_roles;
}
function setAdditionalAdministrationLocations($values): void {
$this->_additionalAdministrationLocations = $values;
//Update the database, first remove existing values
$this->saveAdditionalAdministrationLocations();
}
function getAdditionalAdministrationLocations(): array {
if (is_null($this->_additionalAdministrationLocations)) {
$this->_additionalAdministrationLocations = [];
require_once ROOT_DIR . '/sys/Administration/AdministrationLocation.php';
$locationsList = Location::getLocationList(false);
$administrationLocation = new AdministrationLocation();
$administrationLocation->userId = $this->id;
$administrationLocation->find();
while ($administrationLocation->fetch()) {
$this->_additionalAdministrationLocations[$administrationLocation->locationId] = $locationsList[$administrationLocation->locationId];
}
}
return $this->_additionalAdministrationLocations;
}
function saveAdditionalAdministrationLocations(): void {
if (isset($this->id) && isset($this->_additionalAdministrationLocations) && is_array($this->_additionalAdministrationLocations)) {
require_once ROOT_DIR . '/sys/Administration/AdministrationLocation.php';
$userAdministrationLocations = new AdministrationLocation();
$userAdministrationLocations->userId = $this->id;
$existingLocations = [];
$userAdministrationLocations->find();
while ($userAdministrationLocations->fetch()) {
$existingLocations[$userAdministrationLocations->locationId] = $userAdministrationLocations->locationId;
}
if (count($this->_additionalAdministrationLocations) > 0) {
foreach ($this->_additionalAdministrationLocations as $locationId) {
if (!array_key_exists($locationId, $existingLocations)) {
$administrationLocation = new AdministrationLocation();
$administrationLocation->userId = $this->id;
$administrationLocation->locationId = $locationId;
$administrationLocation->insert();
} else {
unset($existingLocations[$locationId]);
}
}
}
//delete any roles that no longer exist.
foreach ($existingLocations as $existingLocation) {
$administrationLocation = new AdministrationLocation();
$administrationLocation->userId = $this->id;
$administrationLocation->locationId = $existingLocation;
$administrationLocation->delete(true);
}
}
}
/**
* @return Role[]
*/
public function getRolesAssignedByPType(): array {
$rolesAssignedByPType = [];
if ($this->id) {
//Get role based on patron type
$patronType = $this->getPTypeObj();
if (!empty($patronType)) {
if ($patronType->assignedRoleId != -1) {
$role = new Role();
$role->roleId = $patronType->assignedRoleId;
if ($role->find(true)) {
$role->setAssignedFromPType(true);
$rolesAssignedByPType[$role->roleId] = clone $role;
}
}
}
}
return $rolesAssignedByPType;
}
function getBarcode() {
return $this->ils_barcode;
}
function getPasswordOrPin() {
return empty($this->ils_password) ? '' : $this->ils_password;
}
function getPasswordOrPinField() {
return 'ils_password';
}
function getBarcodeField() {
return 'ils_barcode';
}
function getAlternateLibraryCardBarcode() {
return empty($this->alternateLibraryCard) ? '' : $this->alternateLibraryCard;
}
function getAlternateLibraryCardPasswordOrPin() {
return empty($this->alternateLibraryCardPassword) ? '' : $this->alternateLibraryCardPassword;
}
function saveRoles() {
if (isset($this->id) && isset($this->_roles) && is_array($this->_roles)) {
require_once ROOT_DIR . '/sys/Administration/Role.php';
require_once ROOT_DIR . '/sys/Administration/UserRoles.php';
$userRoles = new UserRoles();
$userRoles->userId = $this->id;
$existingRoles = [];
$userRoles->find();
while ($userRoles->fetch()) {
$existingRoles[$userRoles->roleId] = $userRoles->roleId;
}
//$userRoles->delete(true);
$changesMade = false;
$message = '';
//Now add the new values.
if (count($this->_roles) > 0) {
foreach ($this->_roles as $roleObj) {
if (!$roleObj->isAssignedFromPType()) {
if (!array_key_exists($roleObj->roleId, $existingRoles)) {
$userRoles = new UserRoles();
$userRoles->userId = $this->id;
$userRoles->roleId = $roleObj->roleId;
$userRoles->insert();
$changesMade = true;
} else {
unset($existingRoles[$roleObj->roleId]);
}
}
}
}
//delete any roles that no longer exist.
foreach ($existingRoles as $existingRole) {
$userRoles = new UserRoles();
$userRoles->userId = $this->id;
$userRoles->roleId = $existingRole;
$userRoles->delete(true);
$changesMade = true;
}
if ($changesMade) {
//Check to see if we have any roles set by PType and warn the user
$rolesAssignedByPType = $this->getRolesAssignedByPType();
if (count($rolesAssignedByPType) > 0) {
foreach ($rolesAssignedByPType as $role) {
$message .= "Role {$role->name} is defined by PType <br/>";
}
UserAccount::getActiveUserObj()->updateMessage .= $message;
UserAccount::getActiveUserObj()->update();
}
unset ($this->_roles);
}
}
}
/**
* @return User[]
*/
function getLinkedUsers() {
if (is_null($this->linkedUsers)) {
$this->linkedUsers = [];
/* var Library $library */ global $library;
global $memCache;
global $serverName;
global $logger;
if ($this->id && $library->allowLinkedAccounts) {
require_once ROOT_DIR . '/sys/Account/UserLink.php';
$userLink = new UserLink();
$userLink->primaryAccountId = $this->id;
try {
$userLink->find();
while ($userLink->fetch()) {
if (!$this->isBlockedAccount($userLink->linkedAccountId)) {
$linkedUser = new User();
$linkedUser->id = $userLink->linkedAccountId;
if ($linkedUser->find(true)) {
/** @var User $userData */ //$userData = $memCache->get("user_{$serverName}_{$linkedUser->id}");
//if ($userData === false || isset($_REQUEST['reload'])) {
//Load full information from the catalog
$linkedUser = UserAccount::validateAccount($linkedUser->ils_barcode, $linkedUser->ils_password, $linkedUser->source, $this);
//} else {
// $logger->log("Found cached linked user {$userData->id}", Logger::LOG_DEBUG);
// $linkedUser = $userData;
//}
if ($linkedUser && !($linkedUser instanceof AspenError)) {
$this->linkedUsers[] = clone($linkedUser);
}
}
}
}
} catch (PDOException $e) {
//Disabling of linking has not been enabled yet.
}
}
}
return $this->linkedUsers;
}
private $linkedUserObjects;
function getLinkedUserObjects() {
if (is_null($this->linkedUserObjects)) {
$this->linkedUserObjects = [];
try {
/* var Library $library */ global $library;
if ($this->id && $library->allowLinkedAccounts) {
require_once ROOT_DIR . '/sys/Account/UserLink.php';
$userLink = new UserLink();
$userLink->primaryAccountId = $this->id;
$userLink->find();
while ($userLink->fetch()) {
if (!$this->isBlockedAccount($userLink->linkedAccountId)) {
$linkedUser = new User();
$linkedUser->id = $userLink->linkedAccountId;
if ($linkedUser->find(true)) {
/** @var User $userData */
$this->linkedUserObjects[] = clone($linkedUser);
}
}
}
}
} catch (Exception $e) {
//Tables are likely not fully updated
global $logger;
$logger->log("Error loading linked users $e", Logger::LOG_ERROR);
}
}
return $this->linkedUserObjects;
}
public function setParentUser($user) {
$this->parentUser = $user;
}
// Account Blocks //
private $blockAll = null; // set to null to signal unset, boolean when set
private array|null $blockedAccounts = null; // set to null to signal unset, array when set
/**
* Checks if there is any settings disallowing the account $accountIdToCheck to be linked to this user.
*
* @param $accountIdToCheck string linked account Id to check for blocking
* @return bool true for blocking, false for no blocking
*/
public function isBlockedAccount($accountIdToCheck) {
if (is_null($this->blockAll)) {
$this->setAccountBlocks();
}
return $this->blockAll || in_array($accountIdToCheck, $this->blockedAccounts);
}
private function setAccountBlocks() {
// default settings
$this->blockAll = false;
$this->blockedAccounts = [];
require_once ROOT_DIR . '/sys/Administration/BlockPatronAccountLink.php';
$accountBlock = new BlockPatronAccountLink();
$accountBlock->primaryAccountId = $this->id;
if ($accountBlock->find()) {
while ($accountBlock->fetch(false)) {
if ($accountBlock->blockLinking) {
$this->blockAll = true;
} // any one row that has block all on will set this setting to true for this account.
if ($accountBlock->blockedLinkAccountId) {
$this->blockedAccounts[] = $accountBlock->blockedLinkAccountId;
}
}
}
}
/**
* @param string $source
* @return User[]
*/
function getRelatedEcontentUsers($source) {
$users = [];
if ($this->isValidForEContentSource($source)) {
$users[$this->ils_barcode . ':' . $this->ils_password] = $this;
}
foreach ($this->getLinkedUsers() as $linkedUser) {
if ($linkedUser->isValidForEContentSource($source)) {
if (!array_key_exists($linkedUser->ils_barcode . ':' . $linkedUser->ils_password, $users)) {
$users[$linkedUser->ils_barcode . ':' . $linkedUser->ils_password] = $linkedUser;
}
}
}
return $users;
}
function isValidForEContentSource($source) {
global $enabledModules;
if ($this->parentUser == null || ($this->getBarcode() != $this->parentUser->getBarcode())) {
$userHomeLibrary = Library::getPatronHomeLibrary($this);
if ($userHomeLibrary) {
if ($source == 'overdrive') {
if (empty($this->getBarcode())) {
return false;
} else if (array_key_exists('OverDrive', $enabledModules)) {
$overDriveSettings = $userHomeLibrary->getLibraryOverdriveSettings();
foreach ($overDriveSettings as $libraryOverDriveSetting) {
if ($libraryOverDriveSetting->circulationEnabled) {
return true;
}
}
return false;
} else {
return false;
}
} elseif ($source == 'hoopla') {
return array_key_exists('Hoopla', $enabledModules) && $userHomeLibrary->hooplaLibraryID > 0;
} elseif ($source == 'cloud_library') {
return array_key_exists('Cloud Library', $enabledModules) && ($userHomeLibrary->cloudLibraryScope > 0);
} elseif ($source == 'axis360') {
return array_key_exists('Axis 360', $enabledModules) && ($userHomeLibrary->axis360ScopeId > 0);
} elseif ($source == 'palace_project') {
return array_key_exists('Palace Project', $enabledModules) && ($userHomeLibrary->palaceProjectScopeId > 0);
}
}
}
return false;
}
/**
* @return OverDriveSetting[]
*/
function getAvailableOverDriveSettings(string $readerName): array {
$overDriveUsers = $this->getRelatedEcontentUsers('overdrive');
$relatedLibraries = [];
foreach ($overDriveUsers as $overDriveUser) {
$userLibrary = $overDriveUser->getHomeLibrary();
$relatedLibraries[$userLibrary->libraryId] = $userLibrary;
}
$overDriveSettings = [];
foreach ($relatedLibraries as $library) {
foreach ($library->getLibraryOverDriveSettings() as $libraryOverDriveSetting) {
if (!empty($libraryOverDriveSetting->getOverDriveSettings())) {
if (empty($readerName) || $libraryOverDriveSetting->getOverDriveSettings()->readerName == $readerName) {
$overDriveSettings[$libraryOverDriveSetting->id] = $libraryOverDriveSetting;
}
}
}
}
return $overDriveSettings;
}
function hasInterlibraryLoan(): bool {
return $this->hasVDXInterlibraryLoan() || $this->hasOCLCRSFGInterlibraryLoan();
}
function hasVDXInterlibraryLoan(): bool {
try {
$homeLocation = Location::getDefaultLocationForUser();
if ($homeLocation != null) {
//Check to see if local ILL is available
$parentLibrary = $homeLocation->getParentLibrary();
if ($parentLibrary != null) {
if ($parentLibrary->localIllRequestType != 0) {
if ($homeLocation->localIllFormId > 0) {
return true;
}
}
}
//Local ILL is not available, check to see if VDX is available.
require_once ROOT_DIR . '/sys/VDX/VdxSetting.php';
require_once ROOT_DIR . '/sys/VDX/VdxForm.php';
$vdxSettings = new VdxSetting();
if ($vdxSettings->find(true)) {
//Get configuration for the form.
if ($homeLocation->vdxFormId != -1) {
return true;
}
}
}
} catch (Exception $e) {
//This happens if the tables aren't setup, ignore
}
return false;
}
function hasOCLCRSFGInterlibraryLoan(): bool {
try {
require_once ROOT_DIR . '/sys/OCLCRSFG/OCLCRSFGSetting.php';
require_once ROOT_DIR . '/sys/OCLCRSFG/OCLCRSFGForm.php';
$OCLCRSFGSettings = new OCLCRSFGSetting();
$homeLibrary = Library::getPatronHomeLibrary();
$OCLCRSFGSettings->whereAdd("id={$homeLibrary->oclcRSFGSettingsId}");
if ($OCLCRSFGSettings->find(true)) {
return true;
}
} catch (Exception $e) {
//This happens if the tables are not installed yet
}
return false;
}
function getInterlibraryLoanType(): string {
$homeLocation = Location::getDefaultLocationForUser();
if ($homeLocation != null) {
return $homeLocation->getInterlibraryLoanType();
} else {
return 'none';
}
}
/**
* Returns a list of users that can view this account
*
* @return User[]
*/
/** @noinspection PhpUnused */
function getViewers() {
if (is_null($this->viewers)) {
$this->viewers = [];
/* var Library $library */ global $library;
if ($this->id && $library->allowLinkedAccounts) {
require_once ROOT_DIR . '/sys/Account/UserLink.php';
$userLink = new UserLink();
$userLink->linkedAccountId = $this->id;
$userLink->find();
while ($userLink->fetch()) {
$linkedUser = new User();
$linkedUser->id = $userLink->primaryAccountId;
if ($linkedUser->find(true)) {
if (!$linkedUser->isBlockedAccount($this->id)) {
$this->viewers[] = clone($linkedUser);
}
}
}
}
}
return $this->viewers;
}
/**
* @param User $linkedUser
*
* @return boolean
*/
function addLinkedUser(User $linkedUser) {
/* var Library $library */ global $library;
if ($library->allowLinkedAccounts && $linkedUser->id != $this->id) { // library allows linked accounts and the account to link is not itself
$linkedUsers = $this->getLinkedUsers();
foreach ($linkedUsers as $existingUser) {
if ($existingUser->id == $linkedUser->id) {
//We already have a link to this user
return true;
}
}
// Check for Account Blocks
if ($this->isBlockedAccount($linkedUser->id)) {
return false;
}