UserQuery.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace App\Database\Queries;
  3. use App\Database\DB;
  4. use App\Web\Session;
  5. use Psr\Http\Message\ServerRequestInterface as Request;
  6. use Slim\Exception\HttpNotFoundException;
  7. use Slim\Exception\HttpUnauthorizedException;
  8. class UserQuery
  9. {
  10. /**
  11. * @var DB
  12. */
  13. private $database;
  14. /**
  15. * @var Session
  16. */
  17. private $session;
  18. /**
  19. * UserQuery constructor.
  20. * @param DB $db
  21. * @param Session|null $session
  22. */
  23. public function __construct(DB $db, ?Session $session)
  24. {
  25. $this->database = $db;
  26. $this->session = $session;
  27. }
  28. /**
  29. * @param DB $db
  30. * @param Session|null $session
  31. * @return UserQuery
  32. */
  33. public static function make(DB $db, Session $session = null)
  34. {
  35. return new self($db, $session);
  36. }
  37. /**
  38. * @param Request $request
  39. * @param $id
  40. * @param bool $authorize
  41. * @return mixed
  42. * @throws HttpNotFoundException
  43. * @throws HttpUnauthorizedException
  44. */
  45. public function get(Request $request, $id, $authorize = false)
  46. {
  47. $user = $this->database->query('SELECT * FROM `users` WHERE `id` = ? LIMIT 1', $id)->fetch();
  48. if (!$user) {
  49. throw new HttpNotFoundException($request);
  50. }
  51. if ($authorize) {
  52. if ($this->session === null) {
  53. throw new \InvalidArgumentException('The session is null.');
  54. }
  55. if ($user->id !== $this->session->get('user_id') && !$this->session->get('admin', false)) {
  56. throw new HttpUnauthorizedException($request);
  57. }
  58. }
  59. return $user;
  60. }
  61. public function create(string $email, string $username, string $password, int $isAdmin = 0, int $isActive = 0, int $maxUserQuota = -1, string $activateToken = null)
  62. {
  63. do {
  64. $userCode = humanRandomString(5);
  65. } while ($this->database->query('SELECT COUNT(*) AS `count` FROM `users` WHERE `user_code` = ?', $userCode)->fetch()->count > 0);
  66. $token = $this->generateUserUploadToken();
  67. return $this->database->query('INSERT INTO `users`(`email`, `username`, `password`, `is_admin`, `active`, `user_code`, `token`, `max_disk_quota`, `activate_token`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)', [
  68. $email,
  69. $username,
  70. password_hash($password, PASSWORD_DEFAULT),
  71. $isAdmin,
  72. $isActive,
  73. $userCode,
  74. $token,
  75. $maxUserQuota,
  76. $activateToken,
  77. ]);
  78. }
  79. public function update($id, string $email, string $username, string $password = null, int $isAdmin = 0, int $isActive = 0, int $maxUserQuota = -1)
  80. {
  81. if (!empty($password)) {
  82. $this->database->query('UPDATE `users` SET `email`=?, `username`=?, `password`=?, `is_admin`=?, `active`=?, `max_disk_quota`=? WHERE `id` = ?', [
  83. $email,
  84. $username,
  85. password_hash($password, PASSWORD_DEFAULT),
  86. $isAdmin,
  87. $isActive,
  88. $maxUserQuota,
  89. $id,
  90. ]);
  91. } else {
  92. $this->database->query('UPDATE `users` SET `email`=?, `username`=?, `is_admin`=?, `active`=?, `max_disk_quota`=? WHERE `id` = ?', [
  93. $email,
  94. $username,
  95. $isAdmin,
  96. $isActive,
  97. $maxUserQuota,
  98. $id,
  99. ]);
  100. }
  101. }
  102. /**
  103. * @param $id
  104. * @return string
  105. */
  106. public function refreshToken($id)
  107. {
  108. $token = $this->generateUserUploadToken();
  109. $this->database->query('UPDATE `users` SET `token`=? WHERE `id` = ?', [
  110. $token,
  111. $id,
  112. ]);
  113. return $token;
  114. }
  115. /**
  116. * @return string
  117. */
  118. protected function generateUserUploadToken(): string
  119. {
  120. do {
  121. $token = 'token_'.md5(uniqid('', true));
  122. } while ($this->database->query('SELECT COUNT(*) AS `count` FROM `users` WHERE `token` = ?', $token)->fetch()->count > 0);
  123. return $token;
  124. }
  125. }