Users.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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\Admin;
  10. use ForkBB\Core\Container;
  11. use ForkBB\Models\Pages\Admin;
  12. use ForkBB\Models\User\User;
  13. use function \ForkBB\__;
  14. abstract class Users extends Admin
  15. {
  16. const ACTION_BAN = 'ban';
  17. const ACTION_DEL = 'delete';
  18. const ACTION_CHG = 'change_group';
  19. public function __construct(Container $container)
  20. {
  21. parent::__construct($container);
  22. $this->aIndex = 'users';
  23. $this->c->Lang->load('validator');
  24. $this->c->Lang->load('admin_users');
  25. }
  26. /**
  27. * Кодирует данные фильтра для url
  28. */
  29. protected function encodeData(/* array|string */ $data): string
  30. {
  31. if (\is_array($data)) {
  32. unset($data['token']);
  33. $data = \base64_encode(\json_encode($data));
  34. $hash = $this->c->Secury->hash($data);
  35. return "{$data}:{$hash}";
  36. } else {
  37. return "ip:{$data}";
  38. }
  39. }
  40. /**
  41. * Декодирует данные фильтра из url
  42. */
  43. protected function decodeData(string $data) /* : mixed */
  44. {
  45. $data = \explode(':', $data);
  46. if (2 !== \count($data)) {
  47. return false;
  48. }
  49. if ('ip' === $data[0]) {
  50. $ip = \filter_var($data[1], \FILTER_VALIDATE_IP);
  51. return false === $ip ? false : ['ip' => $ip];
  52. }
  53. if (
  54. ! \hash_equals($data[1], $this->c->Secury->hash($data[0]))
  55. || ! \is_array($data = \json_decode(\base64_decode($data[0], true), true))
  56. ) {
  57. return false;
  58. }
  59. return $data;
  60. }
  61. /**
  62. * Проверяет доступность действий над выбранными пользователями
  63. */
  64. protected function checkSelected(array $selected, string $action, bool $profile = false) /* : array|false */
  65. {
  66. $selected = \array_map('\\intval', $selected);
  67. $bad = \array_filter($selected, function ($value) {
  68. return $value < 1;
  69. });
  70. if (! empty($bad)) {
  71. $this->fIswev = ['v', 'Action not available'];
  72. return false;
  73. }
  74. $userList = $this->c->users->loadByIds($selected);
  75. $result = [];
  76. foreach ($userList as $user) {
  77. if (! $user instanceof User) {
  78. continue;
  79. }
  80. switch ($action) {
  81. case self::ACTION_BAN:
  82. if ($this->c->bans->banFromName($user->username) > 0) {
  83. $this->fIswev = ['i', ['User is ban', $user->username]];
  84. return false;
  85. }
  86. if (! $this->c->userRules->canBanUser($user)) {
  87. $this->fIswev = ['v', ['You are not allowed to ban the %s', $user->username]];
  88. if ($user->isAdmMod) {
  89. $this->fIswev = ['i', 'No ban admins message'];
  90. }
  91. return false;
  92. }
  93. break;
  94. case self::ACTION_DEL:
  95. if (! $this->c->userRules->canDeleteUser($user)) {
  96. $this->fIswev = ['v', ['You are not allowed to delete the %s', $user->username]];
  97. if ($user->isAdmMod) {
  98. $this->fIswev = ['i', 'No delete admins message'];
  99. }
  100. return false;
  101. }
  102. break;
  103. case self::ACTION_CHG:
  104. if (! $this->c->userRules->canChangeGroup($user, $profile)) {
  105. $this->fIswev = ['v', ['You are not allowed to change group for %s', $user->username]];
  106. if ($user->isAdmin) {
  107. $this->fIswev = ['i', 'No move admins message'];
  108. }
  109. return false;
  110. }
  111. break;
  112. default:
  113. $this->fIswev = ['v', 'Action not available'];
  114. return false;
  115. }
  116. $result[] = $user->id;
  117. if ($user->id === $this->user->id) {
  118. $this->fIswev = ['i', 'You are trying to change your own group'];
  119. }
  120. }
  121. if (empty($result)) {
  122. $this->fIswev = ['v', 'No users selected'];
  123. return false;
  124. }
  125. return $result;
  126. }
  127. }