User.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace App;
  3. use Carbon\CarbonImmutable;
  4. use Exception;
  5. use RuntimeException;
  6. /**
  7. * Class User
  8. * @property int $id
  9. * @property int $role_id
  10. * @property string $title
  11. * @property string $first_name
  12. * @property string $last_name
  13. * @property string $email
  14. * @property string $password
  15. * @property bool $admin
  16. * @property string $csrf_token
  17. * @property string $dts_last_seen
  18. * @property string $dts_created
  19. * @property string $dts_updated
  20. * @property string $dts_deleted
  21. */
  22. class User extends Model {
  23. protected static $table = "user";
  24. protected static $idField = "id";
  25. protected static $fillableFields = [
  26. "role_id",
  27. "title", "first_name", "last_name", "email", "password",
  28. "csrf_token",
  29. "admin",
  30. "dts_last_seen",
  31. "dts_created",
  32. "dts_updated",
  33. "dts_deleted",
  34. ];
  35. // Static methods
  36. public static function getTitleOptions(): array {
  37. return ["Mr", "Mrs", "Ms"];
  38. }
  39. // Object methods
  40. /**
  41. * @return Role
  42. * @throws Exception
  43. */
  44. public function getRole(): Role {
  45. return Role::get($this->role_id);
  46. }
  47. /**
  48. * @param string|null $password
  49. * @return bool
  50. * @throws Exception
  51. */
  52. public function validatePassword(?string $password): bool {
  53. return password_verify($password, $this->password);
  54. }
  55. /**
  56. * @return void
  57. * @throws Exception
  58. */
  59. public function refreshCsrfToken(): void {
  60. $this->csrf_token = bin2hex(random_bytes(16));
  61. $this->commit();
  62. }
  63. /**
  64. * @return void
  65. * @throws Exception
  66. */
  67. public function insertCsrfToken(): void {
  68. if(!$this->csrf_token) {
  69. $this->refreshCsrfToken();
  70. }
  71. ?>
  72. <input type="hidden" name="csrf_token" value="<?=htmlspecialchars($this->csrf_token)?>" />
  73. <?php
  74. }
  75. public function validateCsrfToken(): bool {
  76. $request = Request::fromRequest();
  77. return $request->raw("csrf_token") === $this->csrf_token;
  78. }
  79. public function requireValidCsrfToken(): void {
  80. if(!$this->validateCsrfToken()) {
  81. throw new RuntimeException(__("CSRF validation failed."));
  82. }
  83. }
  84. public function getDateTimeCreated(): CarbonImmutable {
  85. return CarbonImmutable::parse($this->dts_created, "UTC");
  86. }
  87. /**
  88. * @return bool
  89. * @throws Exception
  90. */
  91. public function touch(): bool {
  92. $this->dts_last_seen = Database::dts();
  93. return $this->commit();
  94. }
  95. public function isAdmin(): bool {
  96. return (bool) $this->admin;
  97. }
  98. public function getUrl(): string {
  99. return sprintf("/users/%d", $this->id);
  100. }
  101. public function requireAdmin(): void {
  102. if(!$this->isAdmin()) {
  103. throw new RuntimeException(__("You do not have permission to access this page."));
  104. }
  105. }
  106. }