1
0

User.php 4.1 KB

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