User.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace App;
  3. use Carbon\CarbonImmutable;
  4. use DateTimeInterface;
  5. use Exception;
  6. use RuntimeException;
  7. /**
  8. * Class User
  9. * @property int $id
  10. * @property int $role_id
  11. * @property string $title
  12. * @property string $first_name
  13. * @property string $last_name
  14. * @property string $email
  15. * @property string $password
  16. * @property bool $admin
  17. * @property string $csrf_token
  18. * @property string $dts_last_seen
  19. * @property string $dts_created
  20. * @property string $dts_updated
  21. * @property string $dts_deleted
  22. */
  23. class User extends Model {
  24. protected static $table = "user";
  25. protected static $idField = "id";
  26. protected static $fillableFields = [
  27. "role_id",
  28. "title", "first_name", "last_name", "email", "password",
  29. "csrf_token",
  30. "admin",
  31. "dts_last_seen",
  32. "dts_created",
  33. "dts_updated",
  34. "dts_deleted",
  35. ];
  36. // Static methods
  37. public static function getTitleOptions(): array {
  38. return ["Mr", "Mrs", "Ms"];
  39. }
  40. public static function getStaffOptions(): array {
  41. $staff_users = self::query(["admin" => 1]);
  42. $staff_options = [];
  43. foreach($staff_users as $staff_user) {
  44. $staff_options[$staff_user->id] = $staff_user->getDisplayName();
  45. }
  46. return $staff_options;
  47. }
  48. public static function requireStaff(?int $user_id): User {
  49. $user = self::get($user_id);
  50. if(!$user || !$user->isAdmin()) {
  51. throw new RuntimeException(__("Invalid staff."));
  52. }
  53. return $user;
  54. }
  55. // Object methods
  56. /**
  57. * @return Role
  58. * @throws Exception
  59. */
  60. public function getRole(): Role {
  61. return Role::get($this->role_id);
  62. }
  63. /**
  64. * @param string|null $password
  65. * @return bool
  66. * @throws Exception
  67. */
  68. public function validatePassword(?string $password): bool {
  69. return password_verify($password, $this->password);
  70. }
  71. /**
  72. * @return void
  73. * @throws Exception
  74. */
  75. public function refreshCsrfToken(): void {
  76. $this->csrf_token = bin2hex(random_bytes(16));
  77. $this->commit();
  78. }
  79. /**
  80. * @return void
  81. * @throws Exception
  82. */
  83. public function insertCsrfToken(): void {
  84. if(!$this->csrf_token) {
  85. $this->refreshCsrfToken();
  86. }
  87. ?>
  88. <input type="hidden" name="csrf_token" value="<?=htmlspecialchars($this->csrf_token)?>" />
  89. <?php
  90. }
  91. public function validateCsrfToken(): bool {
  92. $request = Request::fromRequest();
  93. return $request->raw("csrf_token") === $this->csrf_token;
  94. }
  95. public function requireValidCsrfToken(): void {
  96. if(!$this->validateCsrfToken()) {
  97. throw new RuntimeException(__("CSRF validation failed."));
  98. }
  99. }
  100. public function getDateTimeCreated(): CarbonImmutable {
  101. return CarbonImmutable::parse($this->dts_created, "UTC");
  102. }
  103. /**
  104. * @return bool
  105. * @throws Exception
  106. */
  107. public function touch(): bool {
  108. $this->dts_last_seen = Database::dts();
  109. return $this->commit();
  110. }
  111. public function isAdmin(): bool {
  112. return (bool) $this->admin;
  113. }
  114. public function getUrl(): string {
  115. return sprintf("/users/%d", $this->id);
  116. }
  117. public function requireAdmin(): void {
  118. if(!$this->isAdmin()) {
  119. throw new RuntimeException(__("You do not have permission to access this page."));
  120. }
  121. }
  122. public function getDisplayName(): string {
  123. return $this->first_name . " " . $this->last_name;
  124. }
  125. public function getAvatarUrl(): string {
  126. return "https://www.gravatar.com/avatar/" . md5($this->email) . "?d=mp";
  127. }
  128. public function formatDateTime(?DateTimeInterface $dt, string $format = "d/m/Y H:i"): string {
  129. if(!$dt) {
  130. return __("Never");
  131. }
  132. return CarbonImmutable::instance($dt)->setTimezone("Europe/London")->format("d/m/Y H:i");
  133. }
  134. public function formatDate(?DateTimeInterface $dt): string {
  135. return $this->formatDateTime($dt, "d/m/Y");
  136. }
  137. }