change user class migrate to admin

This commit is contained in:
xiaomlove
2023-01-07 19:34:59 +08:00
parent 02058237f1
commit 2cef77a7f2
11 changed files with 122 additions and 20 deletions

View File

@@ -67,6 +67,9 @@ class UserProfile extends ViewRecord
$actions[] = $this->buildResetPasswordAction();
$actions[] = $this->buildEnableDisableAction();
$actions[] = $this->buildEnableDisableDownloadPrivilegesAction();
if (user_can('user-change-class')) {
$actions[] = $this->buildChangeClassAction();
}
if (user_can('user-delete')) {
$actions[] = $this->buildDeleteAction();
}
@@ -355,4 +358,32 @@ class UserProfile extends ViewRecord
->where('expired_at', '>', Carbon::now())
->count();
}
private function buildChangeClassAction(): Actions\Action
{
return Actions\Action::make('change_class')
->label(__('admin.resources.user.actions.change_class_btn'))
->form([
Forms\Components\Select::make('class')
->options(User::listClass())
->default($this->record->class)
->label(__('user.labels.class'))
->required()
,
Forms\Components\TextInput::make('reason')
->label(__('admin.resources.user.actions.enable_disable_reason'))
->placeholder(__('admin.resources.user.actions.enable_disable_reason_placeholder'))
,
])
->action(function ($data) {
$userRep = $this->getRep();
try {
$userRep->changeClass(Auth::user(), $this->record, $data['class'], $data['reason']);
$this->notify('success', 'Success!');
$this->emitSelf(self::EVENT_RECORD_UPDATED, $this->record->id);
} catch (\Exception $exception) {
$this->notify('danger', $exception->getMessage());
}
});
}
}

View File

@@ -466,6 +466,39 @@ class UserRepository extends BaseRepository
return true;
}
public function changeClass($operator, $targetUser, $newClass, $reason = ''): bool
{
user_can('user-change-class', true);
$operator = $this->getUser($operator);
$targetUser = $this->getUser($targetUser);
if ($targetUser->class == $newClass) {
return true;
}
$locale = $targetUser->locale;
$subject = nexus_trans('user.edit_notifications.change_class.subject', [], $locale);
$body = nexus_trans('user.edit_notifications.change_class.body', [
'action' => nexus_trans( 'user.edit_notifications.change_class.' . ($newClass > $targetUser->class ? 'promote' : 'demote')),
'new_class' => User::getClassText($newClass),
'operator' => $operator->username ?? '',
'reason' => $reason,
], $locale);
$message = [
'sender' => 0,
'receiver' => $targetUser->id,
'subject' => $subject,
'msg' => $body,
'added' => Carbon::now(),
];
NexusDB::transaction(function () use ($targetUser, $newClass, $message) {
$modComment = date('Y-m-d') . " - " . $message['msg'];
$targetUser->updateWithModComment(['class' => $newClass], $modComment);
Message::add($message);
});
return true;
}
public function addMeta($user, array $metaData, array $keyExistsUpdates = [], $notify = true)
{
$user = $this->getUser($user);