Action.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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\Users;
  10. use ForkBB\Core\Validator;
  11. use ForkBB\Models\Page;
  12. use ForkBB\Models\Pages\Admin\Users;
  13. use RuntimeException;
  14. use function \ForkBB\__;
  15. class Action extends Users
  16. {
  17. /**
  18. * Возвращает список имен пользователей
  19. */
  20. protected function nameList(array $users): array
  21. {
  22. $result = [];
  23. foreach ($users as $user) {
  24. $result[] = $user->username;
  25. }
  26. \sort($result, \SORT_STRING | \SORT_FLAG_CASE);
  27. return $result;
  28. }
  29. /**
  30. * Подготавливает данные для шаблона(ов) действия
  31. */
  32. public function view(array $args, string $method): Page
  33. {
  34. if (isset($args['token'])) {
  35. if (! $this->c->Csrf->verify($args['token'], 'AdminUsersAction', $args)) {
  36. return $this->c->Message->message($this->c->Csrf->getError());
  37. }
  38. $profile = true;
  39. } else {
  40. $profile = false;
  41. }
  42. $error = false;
  43. switch ($args['action']) {
  44. /*
  45. case self::ACTION_BAN:
  46. if (! $this->c->userRules->banUsers) {
  47. $error = true;
  48. }
  49. break;
  50. */
  51. case self::ACTION_DEL:
  52. if (! $this->c->userRules->deleteUsers) {
  53. $error = true;
  54. }
  55. break;
  56. case self::ACTION_CHG:
  57. if (
  58. $profile
  59. && ! $this->c->userRules->canChangeGroup($this->c->users->load((int) $args['ids']), true)
  60. ) {
  61. $error = true;
  62. } elseif (
  63. ! $profile
  64. && ! $this->c->userRules->changeGroup
  65. ) {
  66. $error = true;
  67. }
  68. break;
  69. default:
  70. $error = true;
  71. }
  72. if ($error) {
  73. return $this->c->Message->message('Bad request');
  74. }
  75. $ids = $this->checkSelected(\explode('-', $args['ids']), $args['action'], $profile);
  76. if (false === $ids) {
  77. $message = $this->c->Message->message('Action not available');
  78. $message->fIswev = $this->fIswev; // тут идет дополнение, а не замена
  79. return $message;
  80. }
  81. $this->userList = $this->c->users->loadByIds($ids);
  82. switch ($args['action']) {
  83. /*
  84. case self::ACTION_BAN:
  85. return $this->ban($args, $method);
  86. */
  87. case self::ACTION_DEL:
  88. return $this->delete($args, $method);
  89. case self::ACTION_CHG:
  90. return $this->change($args, $method, $profile);
  91. default:
  92. throw new RuntimeException("The action {$args['action']} is unavailable");
  93. }
  94. }
  95. /**
  96. * Удаляет пользователей
  97. */
  98. protected function delete(array $args, string $method): Page
  99. {
  100. if ('POST' === $method) {
  101. $v = $this->c->Validator->reset()
  102. ->addRules([
  103. 'token' => 'token:AdminUsersAction',
  104. 'confirm' => 'required|integer|in:0,1',
  105. 'delete_posts' => 'required|integer|in:0,1',
  106. 'delete' => 'required|string',
  107. ])->addAliases([
  108. ])->addArguments([
  109. 'token' => $args,
  110. ]);
  111. if (
  112. ! $v->validation($_POST)
  113. || 1 !== $v->confirm
  114. ) {
  115. return $this->c->Redirect->page('AdminUsers')->message('No confirm redirect');
  116. }
  117. if (1 === $v->delete_posts) {
  118. foreach ($this->userList as $user) {
  119. $user->__deleteAllPost = true;
  120. }
  121. }
  122. $this->c->users->delete(...$this->userList);
  123. $this->c->forums->reset();
  124. return $this->c->Redirect->page('AdminUsers')->message('Users delete redirect');
  125. }
  126. $this->nameTpl = 'admin/form';
  127. $this->classForm = ['delete-users'];
  128. $this->titleForm = 'Deleting users';
  129. $this->aCrumbs[] = [
  130. $this->c->Router->link('AdminUsersAction', $args),
  131. __('Deleting users'),
  132. ];
  133. $this->form = $this->formDelete($args);
  134. return $this;
  135. }
  136. /**
  137. * Создает массив данных для формы удаления пользователей
  138. */
  139. protected function formDelete(array $args): array
  140. {
  141. $yn = [1 => __('Yes'), 0 => __('No')];
  142. $names = \implode(', ', $this->nameList($this->userList));
  143. $form = [
  144. 'action' => $this->c->Router->link('AdminUsersAction', $args),
  145. 'hidden' => [
  146. 'token' => $this->c->Csrf->create('AdminUsersAction', $args),
  147. ],
  148. 'sets' => [
  149. 'options' => [
  150. 'fields' => [
  151. 'confirm' => [
  152. 'type' => 'radio',
  153. 'value' => 0,
  154. 'values' => $yn,
  155. 'caption' => 'Delete users',
  156. 'help' => ['Confirm delete info', $names],
  157. ],
  158. 'delete_posts' => [
  159. 'type' => 'radio',
  160. 'value' => 0,
  161. 'values' => $yn,
  162. 'caption' => 'Delete posts',
  163. ],
  164. ],
  165. ],
  166. 'info2' => [
  167. 'info' => [
  168. [
  169. 'value' => __('Delete warning'),
  170. 'html' => true,
  171. ],
  172. ],
  173. ],
  174. ],
  175. 'btns' => [
  176. 'delete' => [
  177. 'type' => 'submit',
  178. 'value' => __('Delete users'),
  179. ],
  180. 'cancel' => [
  181. 'type' => 'btn',
  182. 'value' => __('Cancel'),
  183. 'link' => $this->c->Router->link('AdminUsers'),
  184. ],
  185. ],
  186. ];
  187. return $form;
  188. }
  189. /**
  190. * Возвращает список групп доступных для замены
  191. */
  192. protected function groupListForChange(bool $profile): array
  193. {
  194. $list = [];
  195. foreach ($this->c->groups->getList() as $id => $group) {
  196. $list[$id] = $group->g_title;
  197. }
  198. unset($list[FORK_GROUP_GUEST]);
  199. if (! $profile) {
  200. unset($list[FORK_GROUP_ADMIN]);
  201. } elseif (! $this->user->isAdmin) {
  202. $list = [FORK_GROUP_MEMBER => $list[FORK_GROUP_MEMBER]];
  203. }
  204. return $list;
  205. }
  206. /**
  207. * Изменяет группу пользователей
  208. */
  209. protected function change(array $args, string $method, bool $profile): Page
  210. {
  211. $rulePass = 'absent';
  212. if ($profile) {
  213. $user = $this->c->users->load((int) $args['ids']);
  214. $link = $this->c->Router->link(
  215. 'EditUserProfile',
  216. [
  217. 'id' => $user->id,
  218. ]
  219. );
  220. if (
  221. $user->isAdmin
  222. || $user->id === $this->user->id
  223. ) {
  224. $rulePass = 'required|string:trim|max:100000|check_password';
  225. }
  226. } else {
  227. $link = $this->c->Router->link('AdminUsers');
  228. }
  229. if ('POST' === $method) {
  230. $v = $this->c->Validator->reset()
  231. ->addValidators([
  232. 'check_password' => [$this, 'vCheckPassword'],
  233. ])->addRules([
  234. 'token' => 'token:AdminUsersAction',
  235. 'new_group' => 'required|integer|in:' . \implode(',', \array_keys($this->groupListForChange($profile))),
  236. 'confirm' => 'required|integer|in:0,1',
  237. 'password' => $rulePass,
  238. 'move' => 'required|string',
  239. ])->addAliases([
  240. ])->addArguments([
  241. 'token' => $args,
  242. ]);
  243. $redirect = $this->c->Redirect;
  244. if ($v->validation($_POST)) {
  245. if (1 !== $v->confirm) {
  246. return $redirect->url($link)->message('No confirm redirect');
  247. }
  248. $this->c->users->changeGroup($v->new_group, ...$this->userList);
  249. $this->c->forums->reset();
  250. if ($profile) {
  251. if ($this->c->ProfileRules->setUser($user)->editProfile) {
  252. $redirect->url($link);
  253. } else {
  254. $redirect->page('User', ['id' => $user->id, 'name' => $user->username]);
  255. }
  256. } else {
  257. $redirect->page('AdminUsers');
  258. }
  259. return $redirect->message('Users move redirect');
  260. }
  261. $this->fIswev = $v->getErrors();
  262. }
  263. $this->nameTpl = 'admin/form';
  264. $this->classForm = ['change-group'];
  265. $this->titleForm = 'Change user group';
  266. $this->aCrumbs[] = [
  267. $this->c->Router->link('AdminUsersAction', $args),
  268. __('Change user group'),
  269. ];
  270. $this->form = $this->formChange($args, $profile, $link, 'absent' !== $rulePass);
  271. return $this;
  272. }
  273. /**
  274. * Проверяет пароль на совпадение с текущим пользователем
  275. */
  276. public function vCheckPassword(Validator $v, string $password): string
  277. {
  278. if (! \password_verify($password, $this->user->password)) {
  279. $v->addError('Invalid passphrase');
  280. }
  281. return $password;
  282. }
  283. /**
  284. * Создает массив данных для формы изменения группы пользователей
  285. */
  286. protected function formChange(array $args, bool $profile, string $linkCancel, bool $checkPass): array
  287. {
  288. $yn = [1 => __('Yes'), 0 => __('No')];
  289. $names = \implode(', ', $this->nameList($this->userList));
  290. $form = [
  291. 'action' => $this->c->Router->link('AdminUsersAction', $args),
  292. 'hidden' => [
  293. 'token' => $this->c->Csrf->create('AdminUsersAction', $args),
  294. ],
  295. 'sets' => [
  296. 'options' => [
  297. 'fields' => [
  298. 'new_group' => [
  299. 'type' => 'select',
  300. 'options' => $this->groupListForChange($profile),
  301. 'value' => $this->c->config->i_default_user_group,
  302. 'caption' => 'New group label',
  303. 'help' => ['New group help', $names],
  304. ],
  305. 'confirm' => [
  306. 'type' => 'radio',
  307. 'value' => 0,
  308. 'values' => $yn,
  309. 'caption' => 'Move users',
  310. ],
  311. ],
  312. ],
  313. ],
  314. 'btns' => [
  315. 'move' => [
  316. 'type' => 'submit',
  317. 'value' => __('Move users'),
  318. ],
  319. 'cancel' => [
  320. 'type' => 'btn',
  321. 'value' => __('Cancel'),
  322. 'link' => $linkCancel,
  323. ],
  324. ],
  325. ];
  326. if ($checkPass) {
  327. $form['sets']['options']['fields']['password'] = [
  328. 'type' => 'password',
  329. 'caption' => 'Your passphrase',
  330. 'required' => true,
  331. ];
  332. }
  333. return $form;
  334. }
  335. }