+ Add group change for Admin -> Users

This commit is contained in:
Visman 2018-09-15 14:55:10 +07:00
parent 79026a06e4
commit d30e918a1d
8 changed files with 231 additions and 23 deletions

View file

@ -3,6 +3,7 @@
namespace ForkBB\Models\Forum;
use ForkBB\Models\DataModel;
use ForkBB\Models\User\Model as User;
use RuntimeException;
use InvalidArgumentException;
use PDO;
@ -161,6 +162,29 @@ class Model extends DataModel
}
}
/**
* Удаляет указанных пользователей из списка модераторов
*
* @param array ...$users
*
* @throws InvalidArgumentException
*/
public function modDelete(...$users)
{
$moderators = $this->moderators;
foreach ($users as $user) {
if (! $user instanceof User) {
throw new InvalidArgumentException('Expected User');
}
unset($moderators[$user->id]);
}
if ($moderators !== $this->moderators) {
$this->moderators = $moderators;
}
}
/**
* Возвращает общую статистику по дереву разделов с корнем в текущем разделе
*

View file

@ -137,7 +137,7 @@ class Action extends Users
}
/**
* Создает массив данных для формы статистики пользователя по ip
* Создает массив данных для формы удаления пользователей
*
* @param array $stat
* @param int $number
@ -154,14 +154,6 @@ class Action extends Users
'token' => $this->c->Csrf->create('AdminUsersAction', $args),
],
'sets' => [
'info' => [
'info' => [
'info1' => [
'type' => '', //????
'value' => \ForkBB\__('Confirm delete info', $names),
],
],
],
'options' => [
'fields' => [
'confirm' => [
@ -169,6 +161,7 @@ class Action extends Users
'value' => 0,
'values' => $yn,
'caption' => \ForkBB\__('Delete users'),
'info' => \ForkBB\__('Confirm delete info', $names),
],
'delete_posts' => [
'type' => 'radio',
@ -204,4 +197,121 @@ class Action extends Users
return $form;
}
/**
* Возвращает список групп доступных для замены
*
* @return array
*/
protected function groupListForChange()
{
$list = [];
foreach ($this->c->groups->getList() as $id => $group) {
if (! $group->groupGuest && ! $group->groupAdmin) {
$list[$id] = $group->g_title;
}
}
return $list;
}
/**
* Изменяет группу пользователей
*
* @param array $args
* @param string $method
*
* @return Page
*/
protected function change(array $args, $method)
{
if ('POST' === $method) {
$groupList = \implode(',', \array_keys($this->groupListForChange()));
$v = $this->c->Validator->reset()
->addRules([
'token' => 'token:AdminUsersAction',
'new_group' => 'required|integer|in:' . $groupList,
'confirm' => 'required|integer|in:0,1',
'move' => 'string',
])->addAliases([
])->addArguments([
'token' => $args,
]);
if (! $v->validation($_POST) || $v->confirm !== 1) {
return $this->c->Redirect->page('AdminUsers')->message('No confirm redirect');
}
$this->c->DB->beginTransaction();
$this->c->users->changeGroup($v->new_group, ...$this->userList);
$this->c->DB->commit();
$this->c->Cache->delete('stats'); //???? перенести в manager
$this->c->Cache->delete('forums_mark'); //???? с авто обновлением кеша
return $this->c->Redirect->page('AdminUsers')->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);
return $this;
}
/**
* Создает массив данных для формы изменения группы пользователей
*
* @param array $stat
* @param int $number
*
* @return array
*/
protected function formChange(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' => [
'new_group' => [
'type' => 'select',
'options' => $this->groupListForChange(),
'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' => $this->c->Router->link('AdminUsers'),
],
],
];
return $form;
}
}

View file

@ -0,0 +1,66 @@
<?php
namespace ForkBB\Models\User;
use ForkBB\Models\Action;
use ForkBB\Models\Forum\Model as Forum;
use ForkBB\Models\User\Model as User;
use InvalidArgumentException;
use RuntimeException;
class ChangeGroup extends Action
{
/**
* Обновляет группу указанных пользователей
*
* @param int $newGroupId
* @param array ...$users
*
* @throws InvalidArgumentException
* @throws RuntimeException
*/
public function changeGroup($newGroupId, ...$users)
{
$newGroup = $this->c->groups->get($newGroupId);
if (null === $newGroup || $newGroup->groupGuest) {
throw new InvalidArgumentException('Expected group number');
}
$ids = [];
$moderators = [];
foreach ($users as $user) {
if (! $user instanceof User) {
throw new InvalidArgumentException('Expected User');
}
if ($user->isGuest) {
throw new RuntimeException('Guest can not change group');
}
if (1 != $newGroup->g_moderator && $user->isAdmMod) {
$moderators[$user->id] = $user;
}
$ids[] = $user->id;
$user->__group_id = $newGroupId;
}
if (! empty($moderators)) {
$root = $this->c->forums->get(0);
if ($root instanceof Forum) {
foreach ($this->c->forums->depthList($root, 0) as $forum) {
$forum->modDelete(...$moderators);
$this->c->forums->update($forum);
}
}
}
$vars = [
':new' => $newGroupId,
':ids' => $ids,
];
$sql = 'UPDATE ::users AS u
SET u.group_id = ?i:new
WHERE u.id IN (?ai:ids)';
$this->c->DB->exec($sql, $vars);
}
}

View file

@ -214,6 +214,7 @@ return [
'UserManagerPromote' => \ForkBB\Models\User\Promote::class,
'UserManagerFilter' => \ForkBB\Models\User\Filter::class,
'UserManagerDelete' => \ForkBB\Models\User\Delete::class,
'UserManagerChangeGroup' => \ForkBB\Models\User\ChangeGroup::class,
'ForumModel' => \ForkBB\Models\Forum\Model::class,
'ForumModelCalcStat' => \ForkBB\Models\Forum\CalcStat::class,

View file

@ -48,17 +48,20 @@ msgstr "Administrators cannot be banned. In order to ban administrators, you mus
msgid "No ban mods message"
msgstr "Moderators cannot be banned. In order to ban moderators, you must first move them to a different user group."
msgid "Move users"
msgid "Change user group"
msgstr "Change user group"
msgid "Move users"
msgstr "Move users"
msgid "Move users subhead"
msgstr "Select new user group"
msgstr "Select group"
msgid "New group label"
msgstr "New group"
msgid "New group help"
msgstr "Select the group to which the selected users will be moved. For security reasons, it is not possible to move multiple users to the administrator group."
msgstr "Select the group to which these users will be moved: <b>%s</b>."
msgid "Invalid group message"
msgstr "Invalid group ID."
@ -76,10 +79,10 @@ msgid "Confirm delete legend"
msgstr "Important: read before deleting users"
msgid "Confirm delete info"
msgstr "Please confirm that you want to delete these users: %s."
msgstr "Please confirm that you want to delete these users: <b>%s</b>."
msgid "Delete posts"
msgstr "Delete any posts and topics"
msgstr "Delete their posts and topics"
msgid "Delete warning"
msgstr "<b>Warning!</b> Deleted users and/or posts cannot be restored. If you choose not to delete the posts made by these users, the posts can only be deleted manually at a later time."

View file

@ -48,17 +48,20 @@ msgstr "Администраторы не могут быть забанены.
msgid "No ban mods message"
msgstr "Модераторы не могут быть забанены модераторами. Если вы хотите забанить модераторов, сообщите администратору."
msgid "Change user group"
msgstr "Изменение группы"
msgid "Move users"
msgstr "Изменение группы у пользователей"
msgstr "Переместить пользователей"
msgid "Move users subhead"
msgstr "Выберете новую группу"
msgstr "Выбор группы"
msgid "New group label"
msgstr "Новая группа"
msgid "New group help"
msgstr "Выберите группу, в которую будут перемещены выбранные пользователи."
msgstr "Выберите группу, в которую будут перемещены пользователи: <b>%s</b>."
msgid "Invalid group message"
msgstr "Неверная группа."
@ -76,10 +79,10 @@ msgid "Confirm delete legend"
msgstr "ВАЖНО: прочтите перед удалением"
msgid "Confirm delete info"
msgstr "Пожалуйста, подтвердите удаление этих пользователей: %s."
msgstr "Пожалуйста, подтвердите удаление пользователей: <b>%s</b>."
msgid "Delete posts"
msgstr "Удалить все темы и сообщения"
msgstr "Удалить их темы и сообщения"
msgid "Delete warning"
msgstr "<b>Предупреждение!</b> Удалённых пользователей (и сообщения) невозможно восстановить. Если вы выберете "не удалять сообщения", их можно будет удалить вручную в любое время."

View file

@ -40,7 +40,7 @@
@endif
@elseif ('select' === $cur['type'])
<select @if ($cur['required']) required @endif @if ($cur['disabled']) disabled @endif @if ($cur['autofocus']) autofocus @endif class="f-ctrl" id="id-{{ $key }}" name="{{ $key }}">
@if (null === ($count = null) && \is_array(reset($cur['options'])) && 1 === \count(reset($cur['options'])) && $count = 0) @endif
@if (null === ($count = null) && \is_array(\reset($cur['options'])) && 1 === \count(\reset($cur['options'])) && $count = 0) @endif
@foreach ($cur['options'] as $v => $option)
@if (\is_array($option))
@if (null !== $count && 1 === \count($option))
@ -61,10 +61,10 @@
</select>
@elseif ('multiselect' === $cur['type'])
<select @if ($cur['required']) required @endif @if ($cur['disabled']) disabled @endif @if ($cur['autofocus']) autofocus @endif @if ($cur['size']) size="{{ $cur['size'] }}" @endif multiple class="f-ctrl" id="id-{{ $key }}" name="{{ $key }}[]">
@if (null === ($count = null) && \is_array(reset($cur['options'])) && 1 === count(reset($cur['options'])) && $count = 0) @endif
@if (null === ($count = null) && \is_array(\reset($cur['options'])) && 1 === \count(\reset($cur['options'])) && $count = 0) @endif
@foreach ($cur['options'] as $v => $option)
@if (\is_array($option))
@if (null !== $count && 1 === count($option))
@if (null !== $count && 1 === \count($option))
@if (++$count > 1)
</optgroup>
@endif

View file

@ -2362,7 +2362,8 @@ select {
}
.f-search-result-form .f-btns .f-btn,
.f-delete-users-form .f-btns .f-btn{
.f-delete-users-form .f-btns .f-btn,
.f-change-group-form .f-btns .f-btn {
display: inline-block;
width: auto;
}