User.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. // Object methods
  41. /**
  42. * @return Role
  43. * @throws Exception
  44. */
  45. public function getRole(): Role {
  46. return Role::get($this->role_id);
  47. }
  48. /**
  49. * @param string|null $password
  50. * @return bool
  51. * @throws Exception
  52. */
  53. public function validatePassword(?string $password): bool {
  54. return password_verify($password, $this->password);
  55. }
  56. /**
  57. * @return void
  58. * @throws Exception
  59. */
  60. public function refreshCsrfToken(): void {
  61. $this->csrf_token = bin2hex(random_bytes(16));
  62. $this->commit();
  63. }
  64. /**
  65. * @return void
  66. * @throws Exception
  67. */
  68. public function insertCsrfToken(): void {
  69. if(!$this->csrf_token) {
  70. $this->refreshCsrfToken();
  71. }
  72. ?>
  73. <input type="hidden" name="csrf_token" value="<?=htmlspecialchars($this->csrf_token)?>" />
  74. <?php
  75. }
  76. public function validateCsrfToken(): bool {
  77. $request = Request::fromRequest();
  78. return $request->raw("csrf_token") === $this->csrf_token;
  79. }
  80. public function requireValidCsrfToken(): void {
  81. if(!$this->validateCsrfToken()) {
  82. throw new RuntimeException(__("CSRF validation failed."));
  83. }
  84. }
  85. public function getDateTimeCreated(): CarbonImmutable {
  86. return CarbonImmutable::parse($this->dts_created, "UTC");
  87. }
  88. /**
  89. * @return bool
  90. * @throws Exception
  91. */
  92. public function touch(): bool {
  93. $this->dts_last_seen = Database::dts();
  94. return $this->commit();
  95. }
  96. public function isAdmin(): bool {
  97. return (bool) $this->admin;
  98. }
  99. public function getUrl(): string {
  100. return sprintf("/users/%d", $this->id);
  101. }
  102. public function requireAdmin(): void {
  103. if(!$this->isAdmin()) {
  104. throw new RuntimeException(__("You do not have permission to access this page."));
  105. }
  106. }
  107. public function getDisplayName(): string {
  108. return $this->first_name . " " . $this->last_name;
  109. }
  110. public function getAvatarUrl(): string {
  111. return "https://www.gravatar.com/avatar/" . md5($this->email) . "?d=mp";
  112. }
  113. public function formatDateTime(?DateTimeInterface $dt, string $format = "d/m/Y H:i"): string {
  114. if(!$dt) {
  115. return __("Never");
  116. }
  117. return CarbonImmutable::instance($dt)->setTimezone("Europe/London")->format("d/m/Y H:i");
  118. }
  119. public function formatDate(?DateTimeInterface $dt): string {
  120. return $this->formatDateTime($dt, "d/m/Y");
  121. }
  122. }