Users.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. /**
  3. * This file is part of the ForkBB <https://github.com/forkbb>.
  4. *
  5. * @copyright (c) Visman <mio.visman@yandex.ru, https://github.com/MioVisman>
  6. * @license The MIT License (MIT)
  7. */
  8. declare(strict_types=1);
  9. namespace ForkBB\Models\User;
  10. use ForkBB\Models\Manager;
  11. use ForkBB\Models\User\User;
  12. use RuntimeException;
  13. class Users extends Manager
  14. {
  15. const CACHE_NAME = 'guest';
  16. /**
  17. * Ключ модели для контейнера
  18. * @var string
  19. */
  20. protected $cKey = 'Users';
  21. /**
  22. * Создает новую модель пользователя
  23. */
  24. public function create(array $attrs = []): User
  25. {
  26. return $this->c->UserModel->setAttrs($attrs);
  27. }
  28. /**
  29. * Получает пользователя по id
  30. */
  31. public function load(int $id): ?User
  32. {
  33. if ($this->isset($id)) {
  34. return $this->get($id);
  35. } else {
  36. $user = $this->Load->load($id);
  37. $this->set($id, $user);
  38. return $user;
  39. }
  40. }
  41. /**
  42. * Получает массив пользователей по ids
  43. */
  44. public function loadByIds(array $ids): array
  45. {
  46. $result = [];
  47. $data = [];
  48. foreach ($ids as $id) {
  49. if (0 === $id) { // это гость, его грузим через guest()
  50. continue;
  51. } elseif ($this->isset($id)) {
  52. $result[$id] = $this->get($id);
  53. } else {
  54. $result[$id] = null;
  55. $data[] = $id;
  56. $this->set($id, null);
  57. }
  58. }
  59. if (empty($data)) {
  60. return $result;
  61. }
  62. foreach ($this->Load->loadByIds($data) as $user) {
  63. if ($user instanceof User) {
  64. $result[$user->id] = $user;
  65. $this->set($user->id, $user);
  66. }
  67. }
  68. return $result;
  69. }
  70. /**
  71. * Возвращает результат
  72. */
  73. protected function returnUser(?User $user): ?User
  74. {
  75. if ($user instanceof User) {
  76. $loadedUser = $this->get($user->id);
  77. if ($loadedUser instanceof User) {
  78. return $loadedUser;
  79. } else {
  80. $this->set($user->id, $user);
  81. return $user;
  82. }
  83. } else {
  84. return null;
  85. }
  86. }
  87. /**
  88. * Получает пользователя по имени
  89. */
  90. public function loadByName(string $name, bool $caseInsencytive = false): ?User
  91. {
  92. if ('' === $name) {
  93. return null;
  94. } else {
  95. return $this->returnUser($this->Load->loadByName($name, $caseInsencytive));
  96. }
  97. }
  98. /**
  99. * Получает пользователя по email
  100. */
  101. public function loadByEmail(string $email): ?User
  102. {
  103. if ('' === $email) {
  104. return null;
  105. } else {
  106. return $this->returnUser($this->Load->loadByEmail($email));
  107. }
  108. }
  109. /**
  110. * Обновляет данные пользователя
  111. */
  112. public function update(User $user): User
  113. {
  114. return $this->Save->update($user);
  115. }
  116. /**
  117. * Добавляет новую запись в таблицу пользователей
  118. */
  119. public function insert(User $user): int
  120. {
  121. $id = $this->Save->insert($user);
  122. $this->set($id, $user);
  123. return $id;
  124. }
  125. /**
  126. * Создает гостя
  127. */
  128. public function guest(array $attrs = []): User
  129. {
  130. $cache = $this->c->Cache->get(self::CACHE_NAME);
  131. if (! \is_array($cache)) {
  132. $cache = $this->c->groups->get(FORK_GROUP_GUEST)->getAttrs();
  133. if (true !== $this->c->Cache->set(self::CACHE_NAME, $cache)) {
  134. throw new RuntimeException('Unable to write value to cache - ' . self::CACHE_NAME);
  135. }
  136. }
  137. $set = [
  138. 'id' => 0,
  139. 'group_id' => FORK_GROUP_GUEST,
  140. 'time_format' => 0,
  141. 'date_format' => 0,
  142. ] + $attrs;
  143. if (isset($this->c->config->a_guest_set)) {
  144. $set += $this->c->config->a_guest_set;
  145. }
  146. return $this->create($set + $cache);
  147. }
  148. /**
  149. * Сбрасывает кеш гостя
  150. */
  151. public function resetGuest(): Users
  152. {
  153. if (true !== $this->c->Cache->delete(self::CACHE_NAME)) {
  154. throw new RuntimeException('Unable to remove key from cache - ' . self::CACHE_NAME);
  155. }
  156. return $this;
  157. }
  158. }