Action.php 12 KB

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