Profile.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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\Pages;
  10. use ForkBB\Core\Validator;
  11. use ForkBB\Models\Page;
  12. use ForkBB\Models\User\User;
  13. use SensitiveParameter;
  14. use function \ForkBB\__;
  15. abstract class Profile extends Page
  16. {
  17. /**
  18. * Инициализирует профиль
  19. */
  20. protected function initProfile(int $id): bool
  21. {
  22. $this->curUser = $this->c->users->load($id);
  23. if (
  24. ! $this->curUser instanceof User
  25. || $this->curUser->isGuest
  26. || (
  27. $this->curUser->isUnverified
  28. && ! $this->user->isAdmMod
  29. )
  30. ) {
  31. return false;
  32. }
  33. $this->c->Lang->load('profile');
  34. $this->hhsLevel = 'secure';
  35. $this->rules = $this->c->ProfileRules->setUser($this->curUser);
  36. $this->robots = 'noindex';
  37. $this->fIndex = $this->rules->my ? self::FI_PROFL : self::FI_USERS;
  38. $this->nameTpl = 'profile';
  39. $this->onlinePos = 'profile-' . $this->curUser->id; // ????
  40. return true;
  41. }
  42. /**
  43. * Проверяет пароль на совпадение с текущим пользователем
  44. */
  45. public function vCheckPassword(
  46. Validator $v,
  47. #[SensitiveParameter]
  48. string $password
  49. ): string {
  50. if (! \password_verify($password, $this->user->password)) {
  51. $v->addError('Invalid passphrase');
  52. }
  53. return $password;
  54. }
  55. /**
  56. * Возвращает массив хлебных крошек
  57. * Заполняет массив титула страницы
  58. */
  59. protected function crumbs(/* mixed */ ...$crumbs): array
  60. {
  61. $crumbs[] = [$this->curUser->link, ['User %s', $this->curUser->username]];
  62. $crumbs[] = [$this->c->Router->link('Userlist'), 'User list'];
  63. return parent::crumbs(...$crumbs);
  64. }
  65. /**
  66. * Формирует массив кнопок
  67. */
  68. protected function btns(string $type): array
  69. {
  70. $btns = [];
  71. if (
  72. $this->user->isAdmin
  73. && ! $this->rules->editProfile
  74. ) {
  75. $btns['change-user-group'] = [
  76. $this->linkChangeGroup(),
  77. __('Change user group'),
  78. ];
  79. }
  80. if ($this->rules->banUser) {
  81. $id = $this->c->bans->banFromName($this->curUser->username);
  82. if ($id > 0) {
  83. $btns['unban-user'] = [
  84. $this->c->Router->link(
  85. 'AdminBansDelete',
  86. [
  87. 'id' => $id,
  88. 'uid' => $this->curUser->id,
  89. ]
  90. ),
  91. __('Unban user'),
  92. ];
  93. } else {
  94. $btns['ban-user'] = [
  95. $this->c->Router->link(
  96. 'AdminBansNew',
  97. [
  98. 'ids' => $this->curUser->id,
  99. 'uid' => $this->curUser->id,
  100. ]
  101. ),
  102. __('Ban user'),
  103. ];
  104. }
  105. }
  106. if ($this->rules->deleteUser) {
  107. $btns['delete-user'] = [
  108. $this->c->Router->link(
  109. 'AdminUsersAction',
  110. [
  111. 'action' => 'delete',
  112. 'ids' => $this->curUser->id,
  113. ]
  114. ), // ????
  115. __('Delete user'),
  116. ];
  117. }
  118. if (
  119. 'edit' != $type
  120. && $this->rules->editProfile
  121. ) {
  122. $btns['edit-profile'] = [
  123. $this->c->Router->link(
  124. 'EditUserProfile',
  125. [
  126. 'id' => $this->curUser->id,
  127. ]
  128. ),
  129. __('Edit '),
  130. ];
  131. }
  132. if ('view' != $type) {
  133. $btns['view-profile'] = [
  134. $this->curUser->link,
  135. __('View '),
  136. ];
  137. }
  138. if (
  139. 'config' != $type
  140. && $this->rules->editConfig
  141. ) {
  142. $btns['edit-settings'] = [
  143. $this->c->Router->link(
  144. 'EditUserBoardConfig',
  145. [
  146. 'id' => $this->curUser->id,
  147. ]
  148. ),
  149. __('Configure '),
  150. ];
  151. }
  152. return $btns;
  153. }
  154. /**
  155. * Формирует ссылку на изменение группы пользователя
  156. */
  157. protected function linkChangeGroup(): string
  158. {
  159. return $this->c->Router->link(
  160. 'AdminUsersAction',
  161. [
  162. 'action' => 'change_group',
  163. 'ids' => $this->curUser->id,
  164. ]
  165. );
  166. }
  167. }