123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345 |
- <?php
- namespace ForkBB\Models\Pages\Admin\Users;
- use ForkBB\Core\Validator;
- use ForkBB\Models\Pages\Admin\Users;
- use RuntimeException;
- class Action extends Users
- {
- /**
- * Возвращает список имен пользователей
- *
- * @param array $users
- *
- * @return array
- */
- protected function nameList(array $users)
- {
- $result = [];
- foreach ($users as $user) {
- $result[] = $user->username;
- }
- \sort($result, \SORT_STRING | \SORT_FLAG_CASE);
- return $result;
- }
- /**
- * Подготавливает данные для шаблона(ов) действия
- *
- * @param array $args
- * @param string $method
- *
- * @throws RuntimeException
- *
- * @return Page
- */
- public function view(array $args, $method)
- {
- if (isset($args['token'])) {
- if (! $this->c->Csrf->verify($args['token'], 'AdminUsersAction', $args)) {
- return $this->c->Message->message('Bad token');
- }
- $profile = true;
- } else {
- $profile = false;
- }
- $this->rules = $this->c->UsersRules->init();
- $error = false;
- switch ($args['action']) {
- case self::ACTION_BAN:
- if (! $this->rules->banUsers) {
- $error = true;
- }
- break;
- case self::ACTION_DEL:
- if (! $this->rules->deleteUsers) {
- $error = true;
- }
- break;
- case self::ACTION_CHG:
- if ($profile && ! $this->rules->canChangeGroup($this->c->users->load((int) $args['ids']), true)) {
- $error = true;
- } elseif (! $profile && ! $this->rules->changeGroup) {
- $error = true;
- }
- break;
- default:
- $error = true;
- }
- if ($error) {
- return $this->c->Message->message('Bad request');
- }
- $ids = $this->checkSelected(\explode('-', $args['ids']), $args['action'], $profile);
- if (false === $ids) {
- $message = $this->c->Message->message('Action not available');
- $message->fIswev = $this->fIswev; //????
- return $message;
- }
- $this->userList = $this->c->users->load($ids);
- switch ($args['action']) {
- case self::ACTION_BAN:
- return $this->ban($args, $method);
- case self::ACTION_DEL:
- return $this->delete($args, $method);
- case self::ACTION_CHG:
- return $this->change($args, $method, $profile);
- default:
- throw new RuntimeException("The action {$args['action']} is unavailable");
- }
- }
- /**
- * Удаляет пользователей
- *
- * @param array $args
- * @param string $method
- *
- * @return Page
- */
- protected function delete(array $args, $method)
- {
- if ('POST' === $method) {
- $v = $this->c->Validator->reset()
- ->addRules([
- 'token' => 'token:AdminUsersAction',
- 'confirm' => 'required|integer|in:0,1',
- 'delete_posts' => 'required|integer|in:0,1',
- 'delete' => 'string',
- ])->addAliases([
- ])->addArguments([
- 'token' => $args,
- ]);
- if (! $v->validation($_POST) || $v->confirm !== 1) {
- return $this->c->Redirect->page('AdminUsers')->message('No confirm redirect');
- }
- if (1 === $v->delete_posts) {
- foreach ($this->userList as $user) {
- $user->__deleteAllPost = true;
- }
- }
- $this->c->users->delete(...$this->userList);
- $this->c->Cache->delete('stats'); //???? перенести в manager
- $this->c->Cache->delete('forums_mark'); //???? с авто обновлением кеша
- return $this->c->Redirect->page('AdminUsers')->message('Users delete redirect');
- }
- $this->nameTpl = 'admin/form';
- $this->classForm = 'delete-users';
- $this->titleForm = \ForkBB\__('Deleting users');
- $this->aCrumbs[] = [$this->c->Router->link('AdminUsersAction', $args), \ForkBB\__('Deleting users')];
- $this->form = $this->formDelete($args);
- return $this;
- }
- /**
- * Создает массив данных для формы удаления пользователей
- *
- * @param array $args
- *
- * @return array
- */
- protected function formDelete(array $args)
- {
- $yn = [1 => \ForkBB\__('Yes'), 0 => \ForkBB\__('No')];
- $names = \implode(', ', $this->nameList($this->userList));
- $form = [
- 'action' => $this->c->Router->link('AdminUsersAction', $args),
- 'hidden' => [
- 'token' => $this->c->Csrf->create('AdminUsersAction', $args),
- ],
- 'sets' => [
- 'options' => [
- 'fields' => [
- 'confirm' => [
- 'type' => 'radio',
- 'value' => 0,
- 'values' => $yn,
- 'caption' => \ForkBB\__('Delete users'),
- 'info' => \ForkBB\__('Confirm delete info', $names),
- ],
- 'delete_posts' => [
- 'type' => 'radio',
- 'value' => 0,
- 'values' => $yn,
- 'caption' => \ForkBB\__('Delete posts'),
- ],
- ],
- ],
- 'info2' => [
- 'info' => [
- 'info2' => [
- 'type' => '', //????
- 'value' => \ForkBB\__('Delete warning'),
- 'html' => true,
- ],
- ],
- ],
- ],
- 'btns' => [
- 'delete' => [
- 'type' => 'submit',
- 'value' => \ForkBB\__('Delete users'),
- 'accesskey' => 'd',
- ],
- 'cancel' => [
- 'type' => 'btn',
- 'value' => \ForkBB\__('Cancel'),
- 'link' => $this->c->Router->link('AdminUsers'),
- ],
- ],
- ];
- return $form;
- }
- /**
- * Возвращает список групп доступных для замены
- *
- * @param bool $profile
- *
- * @return array
- */
- protected function groupListForChange($profile)
- {
- $list = [];
- foreach ($this->c->groups->getList() as $id => $group) {
- $list[$id] = $group->g_title;
- }
- unset($list[$this->c->GROUP_GUEST]);
- if (! $profile) {
- unset($list[$this->c->GROUP_ADMIN]);
- } elseif (! $this->user->isAdmin) {
- $list = [$this->c->GROUP_MEMBER => $list[$this->c->GROUP_MEMBER]];
- }
- return $list;
- }
- /**
- * Изменяет группу пользователей
- *
- * @param array $args
- * @param string $method
- * @param bool $profile
- *
- * @return Page
- */
- protected function change(array $args, $method, $profile)
- {
- if ($profile) {
- $user = $this->c->users->load((int) $args['ids']);
- $link = $this->c->Router->link('EditUserProfile', ['id' => $user->id]);
- } else {
- $link = $this->c->Router->link('AdminUsers');
- }
- if ('POST' === $method) {
- $v = $this->c->Validator->reset()
- ->addRules([
- 'token' => 'token:AdminUsersAction',
- 'new_group' => 'required|integer|in:' . \implode(',', \array_keys($this->groupListForChange($profile))),
- 'confirm' => 'required|integer|in:0,1',
- 'move' => 'string',
- ])->addAliases([
- ])->addArguments([
- 'token' => $args,
- ]);
- $redirect = $this->c->Redirect;
- if (! $v->validation($_POST) || $v->confirm !== 1) {
- return $redirect->url($link)->message('No confirm redirect');
- }
- $this->c->users->changeGroup($v->new_group, ...$this->userList);
- $this->c->Cache->delete('stats'); //???? перенести в manager
- $this->c->Cache->delete('forums_mark'); //???? с авто обновлением кеша
- if ($profile) {
- if ($this->c->ProfileRules->setUser($user)->editProfile) {
- $redirect->url($link);
- } else {
- $redirect->page('User', ['id' => $user->id, 'name' => $user->username]);
- }
- } else {
- $redirect->page('AdminUsers');
- }
- return $redirect->message('Users move redirect');
- }
- $this->nameTpl = 'admin/form';
- $this->classForm = 'change-group';
- $this->titleForm = \ForkBB\__('Change user group');
- $this->aCrumbs[] = [$this->c->Router->link('AdminUsersAction', $args), \ForkBB\__('Change user group')];
- $this->form = $this->formChange($args, $profile, $link);
- return $this;
- }
- /**
- * Создает массив данных для формы изменения группы пользователей
- *
- * @param array $args
- * @param bool $profile
- * @param string $linkCancel
- *
- * @return array
- */
- protected function formChange(array $args, $profile, $linkCancel)
- {
- $yn = [1 => \ForkBB\__('Yes'), 0 => \ForkBB\__('No')];
- $names = \implode(', ', $this->nameList($this->userList));
- $form = [
- 'action' => $this->c->Router->link('AdminUsersAction', $args),
- 'hidden' => [
- 'token' => $this->c->Csrf->create('AdminUsersAction', $args),
- ],
- 'sets' => [
- 'options' => [
- 'fields' => [
- 'new_group' => [
- 'type' => 'select',
- 'options' => $this->groupListForChange($profile),
- 'value' => $this->c->config->o_default_user_group,
- 'caption' => \ForkBB\__('New group label'),
- 'info' => \ForkBB\__('New group help', $names),
- ],
- 'confirm' => [
- 'type' => 'radio',
- 'value' => 0,
- 'values' => $yn,
- 'caption' => \ForkBB\__('Move users'),
- ],
- ],
- ],
- ],
- 'btns' => [
- 'move' => [
- 'type' => 'submit',
- 'value' => \ForkBB\__('Move users'),
- 'accesskey' => 'm',
- ],
- 'cancel' => [
- 'type' => 'btn',
- 'value' => \ForkBB\__('Cancel'),
- 'link' => $linkCancel,
- ],
- ],
- ];
- return $form;
- }
- }
|