-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathUser.php
More file actions
1004 lines (916 loc) · 30.5 KB
/
Copy pathUser.php
File metadata and controls
1004 lines (916 loc) · 30.5 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
993
994
995
996
997
998
999
1000
<?php /* vim: set colorcolumn= expandtab shiftwidth=2 softtabstop=2 tabstop=4 smarttab: */
namespace BNETDocs\Libraries\User;
use \BNETDocs\Libraries\Core\Config;
use \BNETDocs\Libraries\Core\DateTimeImmutable;
use \BNETDocs\Libraries\Core\StringProcessor;
use \BNETDocs\Libraries\Core\UrlFormatter;
use \BNETDocs\Libraries\Db\MariaDb;
use \BNETDocs\Libraries\Discord\EmbedAuthor as DiscordEmbedAuthor;
use \BNETDocs\Libraries\Tag\Tag;
use \BNETDocs\Libraries\Tag\Types as TagTypes;
use \BNETDocs\Libraries\User\Profile as UserProfile;
use \DateTimeInterface;
use \DateTimeZone;
use \OutOfBoundsException;
use \StdClass;
use \UnexpectedValueException;
class User implements \BNETDocs\Interfaces\DatabaseObject, \JsonSerializable
{
public const DEFAULT_BCRYPT_COST = 10;
public const DEFAULT_OPTION = self::OPTION_ACL_COMMENT_CREATE;
public const DEFAULT_TZ = null; // null means no timezone preference/automatic.
// Maximum SQL field lengths, alter as appropriate
public const MAX_DISPLAY_NAME = 0xFF;
public const MAX_EMAIL = 0xFF;
public const MAX_ID = 0x7FFFFFFFFFFFFFFF;
public const MAX_OPTIONS = 0x7FFFFFFFFFFFFFFF;
public const MAX_PASSWORD_HASH = 0xFF;
public const MAX_PASSWORD_SALT = 0xFF;
public const MAX_TIMEZONE = 0xFF;
public const MAX_USERNAME = 0xFF;
public const MAX_VERIFIER_TOKEN = 0xFF;
public const OPTION_DISABLED = 0x00000001; // User login disabled, active sessions force-expired
public const OPTION_VERIFIED = 0x00000002; // A token sent via email was returned to us
public const OPTION_ACL_DOCUMENT_CREATE = 0x00000004;
public const OPTION_ACL_DOCUMENT_MODIFY = 0x00000008;
public const OPTION_ACL_DOCUMENT_DELETE = 0x00000010;
public const OPTION_ACL_COMMENT_CREATE = 0x00000020;
public const OPTION_ACL_COMMENT_MODIFY = 0x00000040;
public const OPTION_ACL_COMMENT_DELETE = 0x00000080;
public const OPTION_ACL_EVENT_LOG_VIEW = 0x00000100;
public const OPTION_ACL_EVENT_LOG_MODIFY = 0x00000200;
public const OPTION_ACL_EVENT_LOG_DELETE = 0x00000400;
public const OPTION_ACL_NEWS_CREATE = 0x00000800;
public const OPTION_ACL_NEWS_MODIFY = 0x00001000;
public const OPTION_ACL_NEWS_DELETE = 0x00002000;
public const OPTION_ACL_PACKET_CREATE = 0x00004000;
public const OPTION_ACL_PACKET_MODIFY = 0x00008000;
public const OPTION_ACL_PACKET_DELETE = 0x00010000;
public const OPTION_ACL_SERVER_CREATE = 0x00020000;
public const OPTION_ACL_SERVER_MODIFY = 0x00040000;
public const OPTION_ACL_SERVER_DELETE = 0x00080000;
public const OPTION_ACL_USER_CREATE = 0x00100000;
public const OPTION_ACL_USER_MODIFY = 0x00200000;
public const OPTION_ACL_USER_DELETE = 0x00400000;
public const OPTION_ACL_PHPINFO = 0x00800000;
public const OPTION_ACL_ANALYTICS_VIEW = 0x01000000;
public const OPTION_STAFF = self::DEFAULT_OPTION
| self::OPTION_ACL_COMMENT_DELETE
| self::OPTION_ACL_COMMENT_MODIFY
| self::OPTION_ACL_DOCUMENT_CREATE
| self::OPTION_ACL_DOCUMENT_DELETE
| self::OPTION_ACL_DOCUMENT_MODIFY
| self::OPTION_ACL_EVENT_LOG_DELETE
| self::OPTION_ACL_EVENT_LOG_MODIFY
| self::OPTION_ACL_EVENT_LOG_VIEW
| self::OPTION_ACL_NEWS_CREATE
| self::OPTION_ACL_NEWS_DELETE
| self::OPTION_ACL_NEWS_MODIFY
| self::OPTION_ACL_PACKET_CREATE
| self::OPTION_ACL_PACKET_DELETE
| self::OPTION_ACL_PACKET_MODIFY
| self::OPTION_ACL_PHPINFO
| self::OPTION_ACL_SERVER_CREATE
| self::OPTION_ACL_SERVER_DELETE
| self::OPTION_ACL_SERVER_MODIFY
| self::OPTION_ACL_USER_CREATE
| self::OPTION_ACL_USER_DELETE
| self::OPTION_ACL_USER_MODIFY;
protected DateTimeInterface $created_datetime;
protected ?string $display_name;
protected string $email;
protected ?int $id;
protected int $options;
protected ?string $password_hash;
protected ?string $password_salt;
protected DateTimeInterface $record_updated;
protected ?string $timezone;
protected string $username;
protected ?DateTimeInterface $verified_datetime;
protected ?string $verifier_token;
/**
* Constructs a User object from properties, a user id to lookup, or null for a new record.
*
* @param StdClass|integer|null $value Object properties, user id, or null.
*/
public function __construct(StdClass|int|null $value)
{
if ($value instanceof StdClass)
{
$this->allocateObject($value);
}
else
{
$this->setId($value);
if (!$this->allocate()) throw new \BNETDocs\Exceptions\UserNotFoundException($this);
}
}
/**
* Allocates the properties of this object from the database.
*
* @return boolean Whether the operation was successful.
*/
public function allocate(): bool
{
// Set initial property values, but skip the id property.
$this->setCreatedDateTime(new DateTimeImmutable('now'));
$this->setDisplayName(null);
$this->setEmail('', true);
$this->setOptions(self::DEFAULT_OPTION);
$this->setPasswordHash(null);
$this->setPasswordSalt(null);
$this->setRecordUpdated(new DateTimeImmutable('now'));
$this->setTimezone(self::DEFAULT_TZ);
$this->setUsername('', true);
$this->setVerifiedDateTime(null);
$this->setVerifierToken(null);
// Get database record only if the id property is not null.
$id = $this->getId();
if (is_null($id)) return true;
$q = MariaDb::instance()->prepare('
SELECT
`created_datetime`,
`display_name`,
`email`,
`id`,
`options_bitmask`,
`password_hash`,
`password_salt`,
`record_updated`,
`timezone`,
`username`,
`verified_datetime`,
`verifier_token`
FROM `users` WHERE `id` = :id LIMIT 1;
');
if (!$q || !$q->execute([':id' => $id]) || $q->rowCount() != 1) return false;
$this->allocateObject($q->fetchObject());
$q->closeCursor();
return true;
}
/**
* Internal function to process and translate StdClass objects into properties.
*/
protected function allocateObject(StdClass $value): void
{
$this->setCreatedDateTime($value->created_datetime);
$this->setDisplayName($value->display_name);
$this->setEmail($value->email);
$this->setId($value->id);
$this->setOptions($value->options_bitmask);
$this->setPasswordHash($value->password_hash);
$this->setPasswordSalt($value->password_salt);
$this->setRecordUpdated($value->record_updated);
$this->setTimezone($value->timezone);
$this->setUsername($value->username);
$this->setVerifiedDateTime($value->verified_datetime);
$this->setVerifierToken($value->verifier_token);
}
/**
* Commits the properties of this object to the database.
*
* @return boolean Whether the operation was successful.
*/
public function commit(): bool
{
$q = MariaDb::instance()->prepare('
INSERT INTO `users` (
`created_datetime`,
`display_name`,
`email`,
`id`,
`options_bitmask`,
`password_hash`,
`password_salt`,
`record_updated`,
`timezone`,
`username`,
`verified_datetime`,
`verifier_token`
) VALUES (
:cdt, :dn, :e, :id, :o, :pwh, :pws, :rudt, :tz, :u, :vdt, :vt
) ON DUPLICATE KEY UPDATE
`created_datetime` = :cdt,
`display_name` = :dn,
`email` = :e,
`id` = :id,
`options_bitmask` = :o,
`password_hash` = :pwh,
`password_salt` = :pws,
`record_updated` = :rudt,
`timezone` = :tz,
`username` = :u,
`verified_datetime` = :vdt,
`verifier_token` = :vt;
');
$this->setRecordUpdated(new DateTimeImmutable('now', new DateTimeZone(self::DATE_TZ)));
$p = [
':cdt' => $this->getCreatedDateTime(),
':dn' => $this->getDisplayName(),
':e' => $this->getEmail(),
':id' => $this->getId(),
':o' => $this->getOptions(),
':pwh' => $this->getPasswordHash(),
':pws' => $this->getPasswordSalt(),
':rudt' => $this->getRecordUpdated(),
':tz' => $this->getTimezone(),
':u' => $this->getUsername(),
':vdt' => $this->getVerifiedDateTime(),
':vt' => $this->getVerifierToken(),
];
foreach ($p as $k => $v)
{
if ($v instanceof DateTimeInterface)
{
$p[$k] = $v->format(self::DATE_SQL);
}
}
if (!$q || !$q->execute($p)) return false;
if (is_null($p[':id'])) $this->setId(MariaDb::instance()->lastInsertId());
$q->closeCursor();
return true;
}
/**
* Checks whether this user's password matches or not against cleartext input.
* The match will always fail if this user's password needs rehashing.
*
* @param string $password The cleartext input.
* @return boolean Whether this user's password matches.
*/
public function checkPassword(string $password): bool // This function was refactored by OpenAI
{
if (is_null($this->password_hash))
{
return false;
}
else if (substr($this->password_hash, 0, 1) == '$')
{
// new style bcrypt password
$cost = Config::get('bnetdocs.user_password_bcrypt_cost') ?? self::DEFAULT_BCRYPT_COST;
$match = password_verify($password, $this->password_hash);
$rehash = password_needs_rehash(
$this->password_hash, PASSWORD_BCRYPT, array('cost' => $cost)
);
return $match && !$rehash;
}
else
{
// old style peppered and salted sha256 password
$salt = $this->password_salt;
if (is_null($salt)) return false;
$pepper = Config::get('bnetdocs.user_password_pepper') ?? '';
$hash = strtoupper(hash('sha256', $password.$salt.$pepper));
return strtoupper($this->password_hash) === $hash;
}
}
/**
* Creates a password bcrypt hash from a cleartext password input.
*
* @param string $password The cleartext password.
* @return string The hashed password.
*/
public static function createPassword(string $password): string
{
$cost = Config::get('bnetdocs.user_password_bcrypt_cost') ?? self::DEFAULT_BCRYPT_COST;
return password_hash($password, PASSWORD_BCRYPT, array('cost' => $cost));
}
/**
* Deallocates the properties of this object from the database.
*
* @return boolean Whether the operation was successful.
*/
public function deallocate(): bool
{
$id = $this->getId();
if (is_null($id)) return false;
$q = MariaDb::instance()->prepare('DELETE FROM `users` WHERE `id` = ? LIMIT 1;');
try { return $q && $q->execute([$id]); }
finally { $q->closeCursor(); }
}
/**
* Retrieves the user id of a user record by email address.
*
* @param string $value The email address.
* @return integer|null The user id of the user, or null if not found.
*/
public static function findIdByEmail(string $value): ?int
{
if (!filter_var($value, FILTER_VALIDATE_EMAIL))
throw new UnexpectedValueException('email is not formatted as a valid email address');
$q = MariaDb::instance()->prepare('SELECT `id` FROM `users` WHERE `email` = ? LIMIT 1;');
if (!$q || !$q->execute([$value]) || $q->rowCount() == 0) return null;
$r = $q->fetchObject();
$q->closeCursor();
return (int) $r->id;
}
/**
* Retrieves the user id of a user record by username.
*
* @param string $value The username.
* @return integer|null The user id of the user, or null if not found.
*/
public static function findIdByUsername(string $value): ?int
{
$q = MariaDb::instance()->prepare('SELECT `id` FROM `users` WHERE `username` = ? LIMIT 1;');
if (!$q || !$q->execute([$value]) || $q->rowCount() == 0) return null;
$r = $q->fetchObject();
$q->closeCursor();
return (int) $r->id;
}
/**
* Generates a verifier token for use with setVerifierToken().
*
* @param string $username The username, for entropy.
* @param string $email The email address, for entropy.
* @return string The verifier token.
*/
public static function generateVerifierToken(string $username, string $email): string
{
return hash('sha256', sprintf('%s%s%s', mt_rand(), $username, $email));
}
/**
* Retrieves all possible User record objects as one array.
*
* @param array|null $order The column to order by.
* @param integer|null $limit The limit of records to return.
* @param integer|null $index The starting index of records to return, as limited by $limit.
* @return array|null The User object records, or null on error.
*/
public static function &getAllUsers(?array $order = null, ?int $limit = null, ?int $index = null): ?array
{
if (!is_null($limit) && !is_null($index))
{
$limit_clause = sprintf('LIMIT %d,%d', $index, $limit);
}
else if (!is_null($limit))
{
$limit_clause = sprintf('LIMIT %d', $limit);
}
else
{
$limit_clause = '';
}
$q = MariaDb::instance()->prepare(sprintf('
SELECT
`created_datetime`,
`display_name`,
`email`,
`id`,
`options_bitmask`,
`password_hash`,
`password_salt`,
`record_updated`,
`timezone`,
`username`,
`verified_datetime`,
`verifier_token`
FROM `users` ORDER BY %s %s;
', (
$order ? (sprintf('`%s` %s, `id` %s', $order[0], $order[1], $order[1])) : '`id` ASC'
), $limit_clause));
if (!$q || !$q->execute()) return null;
$r = [];
while ($row = $q->fetchObject()) $r[] = new self($row);
$q->closeCursor();
return $r;
}
public function getAsDiscordEmbedAuthor(): DiscordEmbedAuthor
{
return new DiscordEmbedAuthor($this->getName(), $this->getURI(), $this->getAvatarURI(null));
}
public function getAsMarkdown(): string
{
return \sprintf('[%s](%s)', $this->getName(), $this->getURI());
}
/**
* Retrieves the avatar thumbnail url for this user. Requires the email address to be set.
*
* @param integer|null $size The pixel width & height size of the desired avatar thumbnail.
* @return string The avatar thumbnail url.
*/
public function getAvatarURI(?int $size): string
{
return UrlFormatter::format(
(new \BNETDocs\Libraries\User\Gravatar($this->getEmail()))->getUrl($size, 'identicon')
);
}
/**
* Retrieves the Date & Time this user record was created.
*
* @return DateTimeInterface|null The Date & Time interface-compatible value.
*/
public function getCreatedDateTime(): ?DateTimeInterface
{
return $this->created_datetime;
}
/**
* Retrieves the fuzzy estimate Date & Time this user record was created.
* Useful for printing in public spaces where the exact moment is less important than a vague when.
*
* @return string|null The relative Date & Time string.
*/
public function getCreatedEstimate(): string
{
$c = $this->getCreatedDateTime();
$now = new DateTimeImmutable('now');
$d = $c->diff($now);
$i = 0;
$r = '';
$t = 3;
if ($d->y > 0 && $i < $t) { ++$i; $r .= sprintf('%s%d %s%s', ($r ? ', ' : ''), $d->y, 'year', ($d->y !== 1 ? 's' : '')); }
if ($d->m > 0 && $i < $t) { ++$i; $r .= sprintf('%s%d %s%s', ($r ? ', ' : ''), $d->m, 'month', ($d->m !== 1 ? 's' : '')); }
if ($d->d > 0 && $i < $t) { ++$i; $r .= sprintf('%s%d %s%s', ($r ? ', ' : ''), $d->d, 'day', ($d->d !== 1 ? 's' : '')); }
if ($d->h > 0 && $i < $t) { ++$i; $r .= sprintf('%s%d %s%s', ($r ? ', ' : ''), $d->h, 'hour', ($d->h !== 1 ? 's' : '')); }
if ($d->i > 0 && $i < $t) { ++$i; $r .= sprintf('%s%d %s%s', ($r ? ', ' : ''), $d->i, 'minute', ($d->i !== 1 ? 's' : '')); }
if ($i === 0) $r = 'created a moment ago';
if ($c > $now) $r = '-' . $r;
return $r;
}
/**
* Retrieves the display name of this user.
*
* @return string|null The display name, or null if not set.
*/
public function getDisplayName(): ?string
{
return $this->display_name;
}
/**
* Retrieves the email address for this user.
*
* @return string The email address.
*/
public function getEmail(): string
{
return $this->email;
}
/**
* Retrieves the id for this user, or null if not yet committed to the database.
*
* @return integer|null The id.
*/
public function getId(): ?int
{
return $this->id;
}
/**
* Retrieves the printable name for this user, taking into consideration username and display name.
*
* @return string The display name, or if null, then the username.
*/
public function getName(): string
{
return $this->display_name ?? $this->username;
}
/**
* Retrieves an option in the options bitmask for this user.
*
* @param integer $option One of the OPTION_* constants.
* @return boolean Whether the option is set (true) or unset (false).
* @throws OutOfBoundsException if option must be between 0-MAX_OPTIONS.
*/
public function getOption(int $option): bool
{
if ($option < 0 || $option > self::MAX_OPTIONS)
{
throw new OutOfBoundsException(sprintf(
'value must be between 0-%d', self::MAX_OPTIONS
));
}
return ($this->options & $option) === $option;
}
/**
* Retrieves the options bitmask for this user.
*
* @return integer The options bitmask, that are set (1) or unset (0).
*/
public function getOptions(): int
{
return $this->options;
}
/**
* Retrieves the password hash for this user, or null for no password hash set.
*
* @return string|null The password hash.
*/
public function getPasswordHash(): ?string
{
return $this->password_hash;
}
/**
* Retrieves the password salt for this user, or null for no password salt set.
* Note: Salt is deprecated, this is now always set null for new passwords.
*
* @return string|null The password salt.
*/
public function getPasswordSalt(): ?string
{
return $this->password_salt;
}
/**
* Retrieves the Date & Time this user record was last updated.
*
* @return DateTimeInterface The Date & Time interface-compatible value.
*/
public function getRecordUpdated(): DateTimeInterface
{
return $this->record_updated;
}
/**
* Retrieves the Tag objects for this user.
*
* @return array The Tag objects.
*/
public function getTags(): array
{
$id = $this->getId();
return is_null($id) ? [] : (Tag::allocateAll(TagTypes::User, $this->getId()) ?? []);
}
/**
* Retrieves the preferred timezone for this user, or null for no timezone preference/automatic.
*
* @return string|null The timezone.
*/
public function getTimezone(): ?string
{
return $this->timezone;
}
/**
* Retrieves the unique URL for this user.
*
* @return string|null The URL, or null if the id is null.
*/
public function getURI(): ?string
{
$id = $this->getId();
if (is_null($id)) return null;
return UrlFormatter::format(sprintf(
'/user/%s/%s', $id, StringProcessor::sanitizeForUrl($this->getName(), true)
));
}
/**
* Retrieves the total number of registered users.
*
* @return integer|false The count, or false on error.
*/
public static function getUserCount(): int|false
{
$q = MariaDb::instance()->prepare('SELECT COUNT(*) AS `count` FROM `users`;');
if (!$q || !$q->execute() || $q->rowCount() != 1) return false;
$r = (int) $q->fetchObject()->count;
$q->closeCursor();
return $r;
}
/**
* Retrieves the username for this user.
*
* @return string The username.
*/
public function getUsername(): string
{
return $this->username;
}
/**
* Retrieves the UserProfile record for this user, or null if no profile record.
*
* @return UserProfile|null
*/
public function getUserProfile(): ?UserProfile
{
try
{
return is_null($this->id) ? null : new UserProfile($this->id);
}
catch (\BNETDocs\Exceptions\UserProfileNotFoundException)
{
return null;
}
}
/**
* Retrieves the Date & Time this user record was verified.
*
* @return string The Date & Time interface-compatible value.
*/
public function getVerifiedDateTime(): ?DateTimeInterface
{
return $this->verified_datetime;
}
/**
* Retrieves this user's verifier token value.
*
* @return string The verifier token.
*/
public function getVerifierToken(): ?string
{
return $this->verifier_token;
}
/**
* Retrieves whether this user is disabled administratively a.k.a. banned.
*
* @return boolean Whether OPTION_DISABLED is set or unset in this user's options bitmask.
*/
public function isDisabled(): bool
{
return $this->getOption(self::OPTION_DISABLED);
}
/**
* Retrieves whether this user is considered a staff member or not.
*
* @return boolean Whether a select few OPTION_ACL_* are set in this user's options bitmask.
*/
public function isStaff(): bool
{
return $this->getOption(self::OPTION_STAFF);
}
/**
* Retrieves whether this user has the verified status or not.
*
* @return boolean Whether OPTION_VERIFIED is set or unset in this user's options bitmask.
*/
public function isVerified(): bool
{
return $this->getOption(self::OPTION_VERIFIED);
}
/**
* Serializes this object's properties. Part of JsonSerializable interface.
*
* @return mixed The serialized value.
*/
public function jsonSerialize(): mixed
{
return [
'avatar_url' => $this->getAvatarURI(null),
'id' => $this->getId(),
'member_for' => $this->getCreatedEstimate(),
'name' => $this->getName(),
'timezone' => $this->getTimezone(),
'url' => $this->getURI(),
];
}
/**
* Sets the Date & Time this user record was created.
*
* @param DateTimeInterface|string $value The Date & Time interface-compatible value.
* @return void
*/
public function setCreatedDateTime(DateTimeInterface|string $value): void
{
$this->created_datetime = (is_string($value) ?
new DateTimeImmutable($value, new DateTimeZone(self::DATE_TZ)) : $value
);
}
/**
* Sets the display name for this user, displayed instead of the username.
*
* @param string|null $value The display name, or null to display the username instead.
* @return void
* @throws OutOfBoundsException if value must be null or between 1-MAX_DISPLAY_NAME characters.
*/
public function setDisplayName(?string $value): void
{
if (!is_null($value) && (empty($value) || strlen($value) > self::MAX_DISPLAY_NAME))
{
throw new OutOfBoundsException(sprintf(
'value must be null or between 1-%d characters', self::MAX_DISPLAY_NAME
));
}
$this->display_name = $value;
}
/**
* Sets the email address for this user.
*
* @param string $value The email address.
* @param boolean $ignore_empty Whether an empty value should be accepted, defaults false.
* @return void
* @throws OutOfBoundsException if value must be between 1-MAX_EMAIL characters.
* @throws UnexpectedValueException if value is not formatted as a valid email address.
*/
public function setEmail(string $value, bool $ignore_empty = false): void
{
if ((!$ignore_empty && empty($value)) || strlen($value) > self::MAX_EMAIL)
{
throw new OutOfBoundsException(sprintf(
'value must be between 1-%d characters', self::MAX_EMAIL
));
}
if (!empty($value) && !filter_var($value, FILTER_VALIDATE_EMAIL))
{
throw new UnexpectedValueException('value is not formatted as a valid email address');
}
$this->email = $value;
}
/**
* Sets the id for this user, or null for a new user not yet committed to the database.
*
* @param integer|null $value The id.
* @return void
* @throws OutOfBoundsException if value must be null or between 0-MAX_ID.
*/
public function setId(?int $value): void
{
if (!is_null($value) && ($value < 0 || $value > self::MAX_ID))
{
throw new OutOfBoundsException(sprintf(
'value must be null or an integer between 0-%d', self::MAX_ID
));
}
$this->id = $value;
}
/**
* Toggles an option in the options bitmask for this user.
*
* @param integer $option One of the OPTION_* constants.
* @param boolean $value Whether it should be set (true) or unset (false).
* @return void
* @throws OutOfBoundsException if option must be between 0-MAX_OPTIONS.
*/
public function setOption(int $option, bool $value): void
{
if ($option < 0 || $option > self::MAX_OPTIONS)
{
throw new OutOfBoundsException(sprintf(
'value must be between 0-%d', self::MAX_OPTIONS
));
}
if ($value) $this->options |= $option; // bitwise or
else $this->options &= ~$option; // bitwise and ones complement
}
/**
* Sets the options bitmask for this user.
*
* @param integer $value The options bitmask, that are set (1) or unset (0).
* @return void
* @throws OutOfBoundsException if option must be between 0-MAX_OPTIONS.
*/
public function setOptions(int $value): void
{
if ($value < 0 || $value > self::MAX_OPTIONS)
{
throw new OutOfBoundsException(sprintf(
'value must be an integer between 0-%d', self::MAX_OPTIONS
));
}
$this->options = $value;
}
/**
* Sets the password hash and salt for this user from cleartext user input.
*
* @param string $value The cleartext password.
* @return void
*/
public function setPassword(string $value): void
{
$this->setPasswordHash(self::createPassword($value));
$this->setPasswordSalt(null); // Deprecated
}
/**
* Sets the password hash for this user.
*
* @param string|null $value The password hash.
* @return void
* @throws OutOfBoundsException if value must be null or between 1-MAX_PASSWORD_HASH characters.
*/
public function setPasswordHash(?string $value): void
{
if (!is_null($value) && (empty($value) || strlen($value) > self::MAX_PASSWORD_HASH))
{
throw new OutOfBoundsException(sprintf(
'value must be null or between 1-%d characters', self::MAX_PASSWORD_HASH
));
}
$this->password_hash = $value;
}
/**
* Sets the password salt for this user.
* Note: Salt is deprecated, this is now always set null for new passwords.
*
* @param string|null $value The password salt.
* @return void
* @throws OutOfBoundsException if value must be null or between 1-MAX_PASSWORD_SALT characters.
*/
public function setPasswordSalt(?string $value): void
{
if (!is_null($value) && (empty($value) || strlen($value) > self::MAX_PASSWORD_SALT))
{
throw new OutOfBoundsException(sprintf(
'value must be null or between 1-%d characters', self::MAX_PASSWORD_SALT
));
}
$this->password_salt = $value;
}
/**
* Sets the Date & Time this user record was last updated.
* This is implicitly called by allocate() & commit() and is thus unnecessary to call elsewhere.
*
* @param DateTimeInterface|string $value The Date & Time interface-compatible value.
* @return void
*/
public function setRecordUpdated(DateTimeInterface|string $value): void
{
$this->record_updated = (is_string($value) ?
new DateTimeImmutable($value, new DateTimeZone(self::DATE_TZ)) : $value
);
}
/**
* Sets the preferred timezone for this user, or null for no preference/automatic.
*
* @param string|null $value The timezone.
* @return void
* @throws OutOfBoundsException if value must be null or between 1-MAX_TIMEZONE characters.
* @throws UnexpectedValueException if value must be a valid timezone.
*/
public function setTimezone(?string $value): void
{
if (!is_null($value) && (empty($value) || strlen($value) > self::MAX_TIMEZONE))
{
throw new OutOfBoundsException(sprintf(
'value must be null or between 1-%d characters', self::MAX_TIMEZONE
));
}
// Create anonymous DateTimeZone object with $value to test for unknown or bad timezone.
// PHP throws Exception(sprintf("Unknown or bad timezone (%s)", $value)) if so.
// This Exception is wrapped into a new UnexpectedValueException.
try
{
if (!is_null($value) && !empty($value)) new DateTimeZone($value);
}
catch (\Exception $e)
{
throw new UnexpectedValueException('value must be a valid timezone', 0, $e);
}
$this->timezone = $value;
}
/**
* Sets the username for this user.
*
* @param string $value The username.
* @param boolean $ignore_empty Whether an empty value should be accepted, defaults false.
* @return void
* @throws OutOfBoundsException if value must be between 1-MAX_USERNAME characters.
*/
public function setUsername(string $value, bool $ignore_empty = false): void
{
if ((!$ignore_empty && empty($value)) || strlen($value) > self::MAX_USERNAME)
{
throw new OutOfBoundsException(sprintf(
'value must be a string between 1-%d characters', self::MAX_USERNAME
));
}
$this->username = $value;
}
/**
* Sets the verified status for this user.
* If the verification changes, the Date & Time and verifier token also will be updated.
*
* @param boolean $value Whether this user is verified.
* @param boolean $reset Whether the Date & Time and verifier token should be reset if value is unchanged.
* @return void
*/
public function setVerified(bool $value, bool $reset = false): void
{
$old_value = $this->getOption(self::OPTION_VERIFIED);
if (!$reset && $old_value === $value) return; // avoid resetting values every call
$this->setOption(self::OPTION_VERIFIED, $value);
if ($value)
{
// verified
$this->setVerifiedDateTime(new DateTimeImmutable('now'));
$this->setVerifierToken(null);
}
else
{
// not verified
$this->setVerifiedDateTime(null);
$this->setVerifierToken(self::generateVerifierToken($this->username ?? '', $this->email ?? ''));
}
}
/**
* Sets the Date & Time this user record was verified via email address, or null for not verified.
*
* @param DateTimeInterface|string|null $value The Date & Time interface-compatible value.
* @return void
*/
public function setVerifiedDateTime(DateTimeInterface|string|null $value): void
{
$this->verified_datetime = (is_string($value) ?
new DateTimeImmutable($value, new DateTimeZone(self::DATE_TZ)) : $value
);
}
/**
* Sets the verifier token for this user, to be used with email address verification.
*
* @param string|null $value The verifier token.
* @return void
* @throws OutOfBoundsException if value must be null or between 1-MAX_VERIFIER_TOKEN characters.
*/
public function setVerifierToken(?string $value): void
{
if (!is_null($value) && (empty($value) || strlen($value) > self::MAX_VERIFIER_TOKEN))
{
throw new OutOfBoundsException(sprintf(
'value must be null or between 1-%d characters', self::MAX_VERIFIER_TOKEN
));
}