User.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. /**
  49. * @return array
  50. * @throws Exception
  51. */
  52. public static function getOptions(): array {
  53. $options = [];
  54. foreach(self::getAll() as $user) {
  55. $options[$user->id] = $user->getDisplayName();
  56. }
  57. return $options;
  58. }
  59. public static function requireStaff(?int $user_id): User {
  60. $user = self::get($user_id);
  61. if(!$user || !$user->isAdmin()) {
  62. throw new RuntimeException(__("Invalid staff."));
  63. }
  64. return $user;
  65. }
  66. // Object methods
  67. /**
  68. * @return Role
  69. * @throws Exception
  70. */
  71. public function getRole(): Role {
  72. return Role::get($this->role_id);
  73. }
  74. /**
  75. * @param string|null $password
  76. * @return bool
  77. * @throws Exception
  78. */
  79. public function validatePassword(?string $password): bool {
  80. return password_verify($password, $this->password);
  81. }
  82. /**
  83. * @return void
  84. * @throws Exception
  85. */
  86. public function refreshCsrfToken(): void {
  87. $this->csrf_token = bin2hex(random_bytes(16));
  88. $this->commit();
  89. }
  90. /**
  91. * @return void
  92. * @throws Exception
  93. */
  94. public function insertCsrfToken(): void {
  95. if(!$this->csrf_token) {
  96. $this->refreshCsrfToken();
  97. }
  98. ?>
  99. <input type="hidden" name="csrf_token" value="<?=htmlspecialchars($this->csrf_token)?>" />
  100. <?php
  101. }
  102. public function validateCsrfToken(): bool {
  103. $request = Request::fromRequest();
  104. return $request->raw("csrf_token") === $this->csrf_token;
  105. }
  106. public function requireValidCsrfToken(): void {
  107. if(!$this->validateCsrfToken()) {
  108. throw new RuntimeException(__("CSRF validation failed."));
  109. }
  110. }
  111. public function getDateTimeCreated(): CarbonImmutable {
  112. return CarbonImmutable::parse($this->dts_created, "UTC");
  113. }
  114. /**
  115. * @return bool
  116. * @throws Exception
  117. */
  118. public function touch(): bool {
  119. $this->dts_last_seen = Database::dts();
  120. return $this->commit();
  121. }
  122. public function isAdmin(): bool {
  123. return (bool) $this->admin;
  124. }
  125. public function getUrl(): string {
  126. return sprintf("/users/%d", $this->id);
  127. }
  128. public function requireAdmin(): void {
  129. if(!$this->isAdmin()) {
  130. throw new RuntimeException(__("You do not have permission to access this page."));
  131. }
  132. }
  133. public function getDisplayName(): string {
  134. return $this->first_name . " " . $this->last_name;
  135. }
  136. public function getAvatarUrl(): string {
  137. return "https://www.gravatar.com/avatar/" . md5($this->email) . "?d=mp";
  138. }
  139. public function formatDateTime(?DateTimeInterface $dt, string $format = "d/m/Y H:i"): string {
  140. if(!$dt) {
  141. return __("Never");
  142. }
  143. return CarbonImmutable::instance($dt)->setTimezone("Europe/London")->format($format);
  144. }
  145. public function formatDate(?DateTimeInterface $dt): string {
  146. return $this->formatDateTime($dt, "d/m/Y");
  147. }
  148. public function getTimezone(): string {
  149. return "Europe/London";
  150. }
  151. }