-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathMember.php
More file actions
992 lines (798 loc) · 19 KB
/
Copy pathMember.php
File metadata and controls
992 lines (798 loc) · 19 KB
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
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Circles\Model;
use DateTime;
use JsonSerializable;
use OCA\Circles\AppInfo\Capabilities;
use OCA\Circles\Exceptions\MemberNotFoundException;
use OCA\Circles\Exceptions\MembershipNotFoundException;
use OCA\Circles\Exceptions\ParseMemberLevelException;
use OCA\Circles\Exceptions\RequestBuilderException;
use OCA\Circles\Exceptions\UnknownInterfaceException;
use OCA\Circles\Exceptions\UserTypeNotFoundException;
use OCA\Circles\IEntity;
use OCA\Circles\IFederatedUser;
use OCA\Circles\Model\Federated\RemoteInstance;
use OCA\Circles\Tools\Db\IQueryRow;
use OCA\Circles\Tools\Exceptions\InvalidItemException;
use OCA\Circles\Tools\IDeserializable;
use OCA\Circles\Tools\Traits\TArrayTools;
use OCA\Circles\Tools\Traits\TDeserialize;
/**
* Class Member
*
* @package OCA\Circles\Model
*/
class Member extends ManagedModel implements
IEntity,
IFederatedUser,
IDeserializable,
IQueryRow,
JsonSerializable {
use TArrayTools;
use TDeserialize;
public const LEVEL_NONE = 0;
public const LEVEL_MEMBER = 1;
public const LEVEL_MODERATOR = 4;
public const LEVEL_ADMIN = 8;
public const LEVEL_OWNER = 9;
public const TYPE_SINGLE = 0;
public const TYPE_USER = 1;
public const TYPE_GROUP = 2;
public const TYPE_MAIL = 4;
public const TYPE_CONTACT = 8;
public const TYPE_CIRCLE = 16;
public const TYPE_APP = 10000;
public const ALLOWING_ALL_TYPES = 31;
public const APP_CIRCLES = 10001;
public const APP_OCC = 10002;
public const APP_DEFAULT = 11000;
public static $TYPE = [
0 => 'single',
1 => 'user',
2 => 'group',
4 => 'mail',
8 => 'contact',
16 => 'circle',
10000 => 'app'
];
/**
* Note: When editing those values, update lib/Application/Capabilities.php
*
* @see Capabilities::generateConstantsMember()
*/
public const STATUS_INVITED = 'Invited';
public const STATUS_REQUEST = 'Requesting';
public const STATUS_MEMBER = 'Member';
public const STATUS_BLOCKED = 'Blocked';
/**
* Note: When editing those values, update lib/Application/Capabilities.php
*
* @see Capabilities::generateConstantsMember()
* @var array
*/
public static $DEF_LEVEL = [
1 => 'Member',
4 => 'Moderator',
8 => 'Admin',
9 => 'Owner'
];
public static $DEF_TYPE_MAX = 31;
/** @var string */
private $id = '';
/** @var string */
private $circleId = '';
/** @var string */
private $singleId = '';
/** @var string */
private $userId = '';
/** @var int */
private $userType = 0;
/** @var ?Circle */
private $basedOn;
/** @var Member */
private $inheritanceFrom;
/** @var FederatedUser */
private $inheritedBy;
/** @var string */
private $instance = '';
/** @var FederatedUser */
private $invitedBy;
/** @var RemoteInstance */
private $remoteInstance;
/** @var bool */
private $local = false;
/** @var int */
private $level = 0;
/** @var string */
private $status = 'Unknown';
/** @var array */
private $notes = [];
/** @var string */
private $displayName = '';
/** @var int */
private $displayUpdate = 0;
/** @var string */
private $contactId = '';
/** @var string */
private $contactMeta = '';
/** @var Circle */
private $circle;
/** @var int */
private $joined = 0;
/** @var Membership[] */
private $memberships = null;
/**
* Member constructor.
*/
public function __construct() {
}
/**
* @param string $id
*
* @return $this
*/
public function setId(string $id): self {
$this->id = $id;
return $this;
}
/**
* @return string
*/
public function getId(): string {
return $this->id;
}
/**
* @param string $circleId
*
* @return Member
*/
public function setCircleId(string $circleId): self {
$this->circleId = $circleId;
return $this;
}
/**
* @return string
*/
public function getCircleId(): string {
return $this->circleId;
}
/**
* This should replace user_id, user_type and instance; and will use the data from Circle with
* Config=CFG_SINGLE
*
* @param string $singleId
*
* @return $this
*/
public function setSingleId(string $singleId): self {
$this->singleId = $singleId;
return $this;
}
/**
* @return string
*/
public function getSingleId(): string {
return $this->singleId;
}
/**
* @param string $userId
*
* @return Member
*/
public function setUserId(string $userId): self {
$this->userId = $userId;
if ($this->displayName === '') {
$this->displayName = $userId;
}
return $this;
}
/**
* @return string
*/
public function getUserId(): string {
return $this->userId;
}
/**
* @param int $userType
*
* @return Member
*/
public function setUserType(int $userType): self {
$this->userType = $userType;
return $this;
}
/**
* @return int
*/
public function getUserType(): int {
return $this->userType;
}
/**
* @return int
* @deprecated 22.0.0 Use `getUserType()` instead
*/
public function getType(): int {
return $this->getUserType();
}
/**
* @param string $instance
*
* @return Member
*/
public function setInstance(string $instance): self {
$this->instance = $instance;
return $this;
}
/**
* @return string
*/
public function getInstance(): string {
return $this->instance;
}
/**
* @return bool
*/
public function isLocal(): bool {
return $this->getManager()->isLocalInstance($this->getInstance());
}
/**
* @param FederatedUser $invitedBy
*
* @return Member
*/
public function setInvitedBy(FederatedUser $invitedBy): Member {
$this->invitedBy = $invitedBy;
return $this;
}
/**
* @return FederatedUser
*/
public function getInvitedBy(): FederatedUser {
return $this->invitedBy;
}
/**
* @return bool
*/
public function hasInvitedBy(): bool {
return !is_null($this->invitedBy);
}
/**
* @return bool
*/
public function hasRemoteInstance(): bool {
return !is_null($this->remoteInstance);
}
/**
* @param RemoteInstance $remoteInstance
*
* @return Member
*/
public function setRemoteInstance(RemoteInstance $remoteInstance): self {
$this->remoteInstance = $remoteInstance;
return $this;
}
/**
* @return RemoteInstance
*/
public function getRemoteInstance(): RemoteInstance {
return $this->remoteInstance;
}
/**
* @return bool
*/
public function hasBasedOn(): bool {
return !is_null($this->basedOn);
}
/**
* @param ?Circle $basedOn
*
* @return $this
*/
public function setBasedOn(?Circle $basedOn): self {
$this->basedOn = $basedOn;
return $this;
}
/**
* @return ?Circle
*/
public function getBasedOn(): ?Circle {
return $this->basedOn;
}
/**
* @return bool
*/
public function hasInheritedBy(): bool {
return !is_null($this->inheritedBy);
}
/**
* @param FederatedUser $inheritedBy
*
* @return $this
*/
public function setInheritedBy(FederatedUser $inheritedBy): self {
$this->inheritedBy = $inheritedBy;
return $this;
}
/**
* @return FederatedUser
*/
public function getInheritedBy(): FederatedUser {
return $this->inheritedBy;
}
/**
* @return bool
*/
public function hasInheritanceFrom(): bool {
return !is_null($this->inheritanceFrom);
}
/**
* @param Member $inheritanceFrom
*
* @return $this
*/
public function setInheritanceFrom(Member $inheritanceFrom): self {
$this->inheritanceFrom = $inheritanceFrom;
return $this;
}
/**
* @return Member|null
*/
public function getInheritanceFrom(): ?Member {
return $this->inheritanceFrom;
}
/**
* @param int $level
*
* @return Member
*/
public function setLevel(int $level): self {
$this->level = $level;
return $this;
}
/**
* @return int
*/
public function getLevel(): int {
return $this->level;
}
/**
* @param string $status
*
* @return Member
*/
public function setStatus(string $status): self {
$this->status = $status;
return $this;
}
/**
* @return string
*/
public function getStatus(): string {
return $this->status;
}
/**
* @param array $notes
*
* @return Member
*/
public function setNotes(array $notes): self {
$this->notes = $notes;
return $this;
}
/**
* @return array
*/
public function getNotes(): array {
return $this->notes;
}
/**
* @param string $key
*
* @return string
*/
public function getNote(string $key): string {
return $this->get($key, $this->notes);
}
/**
* @param string $key
*
* @return array
*/
public function getNoteArray(string $key): array {
return $this->getArray($key, $this->notes);
}
/**
* @param string $key
* @param string $note
*
* @return $this
*/
public function setNote(string $key, string $note): self {
$this->notes[$key] = $note;
return $this;
}
/**
* @param string $key
* @param array $note
*
* @return $this
*/
public function setNoteArray(string $key, array $note): self {
$this->notes[$key] = $note;
return $this;
}
/**
* @param string $key
* @param JsonSerializable $obj
*
* @return $this
*/
public function setNoteObj(string $key, JsonSerializable $obj): self {
$this->notes[$key] = $obj;
return $this;
}
/**
* @param string $displayName
*
* @return $this
*/
public function setDisplayName(string $displayName): self {
if ($displayName !== '') {
$this->displayName = $displayName;
}
return $this;
}
/**
* @param int $displayUpdate
*
* @return Member
*/
public function setDisplayUpdate(int $displayUpdate): self {
$this->displayUpdate = $displayUpdate;
return $this;
}
/**
* @return int
*/
public function getDisplayUpdate(): int {
return $this->displayUpdate;
}
/**
* @return string
*/
public function getDisplayName(): string {
return $this->displayName;
}
/**
* @param string $contactId
*
* @return Member
*/
public function setContactId(string $contactId): self {
$this->contactId = $contactId;
return $this;
}
/**
* @return string
*/
public function getContactId(): string {
return $this->contactId;
}
/**
* @param string $contactMeta
*
* @return Member
*/
public function setContactMeta(string $contactMeta): self {
$this->contactMeta = $contactMeta;
return $this;
}
/**
* @return string
*/
public function getContactMeta(): string {
return $this->contactMeta;
}
/**
* @param Circle $circle
*
* @return self
*/
public function setCircle(Circle $circle): self {
$this->circle = $circle;
return $this;
}
/**
* @return Circle
*/
public function getCircle(): Circle {
return $this->circle;
}
/**
* @return bool
*/
public function hasCircle(): bool {
return (!is_null($this->circle));
}
/**
* @param int $joined
*
* @return Member
*/
public function setJoined(int $joined): self {
$this->joined = $joined;
return $this;
}
/**
* @return int
*/
public function getJoined(): int {
return $this->joined;
}
/**
* @return bool
*/
public function hasMemberships(): bool {
return !is_null($this->memberships);
}
/**
* @param array $memberships
*
* @return self
*/
public function setMemberships(array $memberships): IEntity {
$this->memberships = $memberships;
return $this;
}
/**
* @return Membership[]
*/
public function getMemberships(): array {
if (is_null($this->memberships)) {
$this->getManager()->getMemberships($this);
}
return $this->memberships;
}
/**
* @param string $singleId
* @param bool $detailed
*
* @return Membership
* @throws MembershipNotFoundException
* @throws RequestBuilderException
*/
public function getLink(string $singleId, bool $detailed = false): Membership {
if ($singleId !== '') {
$this->getManager()->getLink($this, $singleId, $detailed);
}
throw new MembershipNotFoundException();
}
/**
* @param string $circleId
* @param bool $detailed
*
* @return Membership
* @throws MembershipNotFoundException
* @throws RequestBuilderException
* @deprecated - use getLink();
*/
public function getMembership(string $circleId, bool $detailed = false): Membership {
return $this->getLink($circleId, $detailed);
}
/**
* @param Member $member
* @param bool $full
*
* @return bool
*/
public function compareWith(Member $member, bool $full = true): bool {
if ($this->getId() !== $member->getId()
|| $this->getCircleId() !== $member->getCircleId()
|| $this->getSingleId() !== $member->getSingleId()
|| $this->getUserId() !== $member->getUserId()
|| $this->getUserType() <> $member->getUserType()
|| $this->getInstance() !== $member->getInstance()) {
return false;
}
if ($full
&& ($this->getLevel() <> $member->getLevel()
|| $this->getStatus() !== $member->getStatus())) {
return false;
}
return true;
}
/**
* @param array $data
*
* @return $this
* @throws InvalidItemException
*/
public function import(array $data): IDeserializable {
if ($this->get('userId', $data) === '') {
throw new InvalidItemException();
}
$this->setId($this->get('id', $data));
$this->setCircleId($this->get('circleId', $data));
$this->setSingleId($this->get('singleId', $data));
$this->setUserId($this->get('userId', $data));
$this->setUserType($this->getInt('userType', $data));
$this->setInstance($this->get('instance', $data));
$this->setLevel($this->getInt('level', $data));
$this->setStatus($this->get('status', $data));
$this->setDisplayName($this->get('displayName', $data));
$this->setDisplayUpdate($this->getInt('displayUpdate', $data));
$this->setNotes($this->getArray('notes', $data));
$this->setContactId($this->get('contactId', $data));
$this->setContactMeta($this->get('contactMeta', $data));
$this->setJoined($this->getInt('joined', $data));
try {
/** @var Circle $circle */
$circle = $this->deserialize($this->getArray('circle', $data), Circle::class);
$this->setCircle($circle);
} catch (InvalidItemException $e) {
}
try {
/** @var Circle $circle */
$circle = $this->deserialize($this->getArray('basedOn', $data), Circle::class);
$this->setBasedOn($circle);
} catch (InvalidItemException) {
}
try {
/** @var FederatedUser $invitedBy */
$invitedBy = $this->deserialize($this->getArray('invitedBy', $data), FederatedUser::class);
$this->setInvitedBy($invitedBy);
} catch (InvalidItemException) {
}
try {
/** @var FederatedUSer $inheritedBy */
$inheritedBy = $this->deserialize($this->getArray('inheritedBy', $data), Membership::class);
$this->setInheritedBy($inheritedBy);
} catch (InvalidItemException) {
}
return $this;
}
/**
* @param array $data
* @param string $prefix
*
* @return IQueryRow
* @throws MemberNotFoundException
*/
public function importFromDatabase(array $data, string $prefix = ''): IQueryRow {
if ($this->get($prefix . 'single_id', $data) === '') {
throw new MemberNotFoundException();
}
$this->setId($this->get($prefix . 'member_id', $data));
$this->setCircleId($this->get($prefix . 'circle_id', $data));
$this->setSingleId($this->get($prefix . 'single_id', $data));
$this->setUserId($this->get($prefix . 'user_id', $data));
$this->setUserType($this->getInt($prefix . 'user_type', $data));
$this->setInstance($this->get($prefix . 'instance', $data));
$this->setLevel($this->getInt($prefix . 'level', $data));
$this->setStatus($this->get($prefix . 'status', $data));
$this->setDisplayName($this->get($prefix . 'cached_name', $data));
$this->setNotes($this->getArray($prefix . 'note', $data));
$this->setContactId($this->get($prefix . 'contact_id', $data));
$this->setContactMeta($this->get($prefix . 'contact_meta', $data));
$cachedUpdate = $this->get($prefix . 'cached_update', $data);
if ($cachedUpdate !== '') {
$this->setDisplayUpdate(DateTime::createFromFormat('Y-m-d H:i:s', $cachedUpdate)->getTimestamp());
}
$joined = $this->get($prefix . 'joined', $data);
if ($joined !== '') {
$this->setJoined(DateTime::createFromFormat('Y-m-d H:i:s', $joined)->getTimestamp());
}
if ($this->getInstance() === '') {
$this->setInstance($this->getManager()->getLocalInstance());
}
$this->getManager()->manageImportFromDatabase($this, $data, $prefix);
// in case invitedBy is not obtainable from 'invited_by', we reach data from 'note'
if (!$this->hasInvitedBy()) {
$invitedByArray = $this->getNoteArray('invitedBy');
if (!empty($invitedByArray)) {
try {
$invitedBy = new FederatedUser();
$this->setInvitedBy($invitedBy->import($invitedByArray));
} catch (InvalidItemException) {
}
}
}
return $this;
}
/**
* @return string[]
* @throws UnknownInterfaceException
*/
public function jsonSerialize(): array {
$arr = [
'id' => $this->getId(),
'circleId' => $this->getCircleId(),
'singleId' => $this->getSingleId(),
'userId' => $this->getUserId(),
'userType' => $this->getUserType(),
'instance' => $this->getManager()->fixInstance($this->getInstance()),
'local' => $this->isLocal(),
'level' => $this->getLevel(),
'status' => $this->getStatus(),
'displayName' => $this->getDisplayName(),
'displayUpdate' => $this->getDisplayUpdate(),
'notes' => $this->getNotes(),
'contactId' => $this->getContactId(),
'contactMeta' => $this->getContactMeta(),
'joined' => $this->getJoined()
];
if ($this->hasInvitedBy()) {
$arr['invitedBy'] = $this->getInvitedBy();
}
if ($this->hasBasedOn()) {
$arr['basedOn'] = $this->getBasedOn();
}
if ($this->hasInheritedBy()) {
$arr['inheritedBy'] = $this->getInheritedBy();
}
if ($this->hasInheritanceFrom()) {
$arr['inheritanceFrom'] = $this->getInheritanceFrom();
}
if ($this->hasCircle()) {
$arr['circle'] = $this->getCircle();
}
if ($this->hasMemberships()) {
$arr['memberships'] = $this->getMemberships();
}
if ($this->hasRemoteInstance()) {
$arr['remoteInstance'] = $this->getRemoteInstance();
}
return $arr;
}
/**
* @param int $level
*
* @return int
* @throws ParseMemberLevelException
*/
public static function parseLevelInt(int $level): int {
if (!array_key_exists($level, self::$DEF_LEVEL)) {
$all = implode(', ', array_keys(self::$DEF_LEVEL));
throw new ParseMemberLevelException('Available levels: ' . $all, 121);
}
return $level;
}
/**
* @param string $levelString
*
* @return int
* @throws ParseMemberLevelException
*/
public static function parseLevelString(string $levelString): int {
$levelString = ucfirst(strtolower($levelString));
$level = array_search($levelString, Member::$DEF_LEVEL);
if (!$level) {
$all = implode(', ', array_values(self::$DEF_LEVEL));
throw new ParseMemberLevelException('Available levels: ' . $all, 121);
}
return (int)$level;
}
/**
* @param string $typeString
*
* @return int
* @throws UserTypeNotFoundException
*/
public static function parseTypeString(string $typeString): int {
$typeString = strtolower($typeString);
if (array_key_exists($typeString, Member::$TYPE)) {
return (int)$typeString;
}
$type = array_search($typeString, Member::$TYPE);
if ($type === false) {
$all = implode(', ', array_values(self::$TYPE));
throw new UserTypeNotFoundException('Available types: ' . $all);
}
return (int)$type;
}
}