From c8bdaf73d3b01e75a84820eca74bdba091babc34 Mon Sep 17 00:00:00 2001 From: Chenx221 Date: Sat, 23 Mar 2024 14:28:36 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=A8=E6=88=B7=E7=AE=A1=E7=90=86=E5=8A=9F?= =?UTF-8?q?=E8=83=BD(1.75/5)=20=E7=94=A8=E6=88=B7=E5=90=AF=E7=94=A8/?= =?UTF-8?q?=E7=A6=81=E7=94=A8=20*=E7=A6=81=E7=94=A8=E6=97=B6=E4=BC=9A?= =?UTF-8?q?=E5=BC=BA=E5=88=B6=E4=B8=8B=E7=BA=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controllers/AdminController.php | 19 ++++++++++++++----- models/User.php | 23 ++++++++++++++--------- views/admin/user.php | 2 +- views/admin/user_view.php | 17 +++++++++++------ 4 files changed, 40 insertions(+), 21 deletions(-) diff --git a/controllers/AdminController.php b/controllers/AdminController.php index 7ce0bef..3d285fb 100644 --- a/controllers/AdminController.php +++ b/controllers/AdminController.php @@ -4,6 +4,7 @@ namespace app\controllers; use app\models\User; use app\models\UserSearch; +use app\utils\AdminSword; use Throwable; use Yii; use yii\db\StaleObjectException; @@ -165,8 +166,6 @@ class AdminController extends Controller } /** - * Deletes an existing User model. - * If deletion is successful, the browser will be redirected to the 'index' page. * @param int $id ID * @return Response * @throws NotFoundHttpException if the model cannot be found @@ -175,9 +174,19 @@ class AdminController extends Controller */ public function actionUserDelete(int $id): Response { - $this->findModel($id)->delete(); - - return $this->redirect(['user']); + $user = $this->findModel($id); + $alreadyDisabled = $user->status == 0; + $str = $alreadyDisabled ? '启用' : '禁用'; + if ($user->deleteAccount($alreadyDisabled)) { + $logout_result = ''; + if(!$alreadyDisabled){ + $logout_result = AdminSword::forceUserLogout($id); + } + Yii::$app->session->setFlash('success', '账户'.$str.'成功,'.$logout_result); + } else { + Yii::$app->session->setFlash('error', '账户'.$str.'失败'); + } + return $this->redirect(['user-view', 'id' => $id]); } /** diff --git a/models/User.php b/models/User.php index 714ea91..3e8e710 100644 --- a/models/User.php +++ b/models/User.php @@ -277,18 +277,23 @@ class User extends ActiveRecord implements IdentityInterface return $url; } - public function deleteAccount(): false|int - { - // 设置用户状态为禁用 - $this->status = 0; - // 保存用户模型 - if (!$this->save()) { + /** + * 名为删号 + * 实为禁用/启用账户 + * @param bool $rev 反转 + * @return false|int + */ + public function deleteAccount(bool $rev = false): false|int + { + $this->status = $rev ? 1 : 0; + + if (!$this->save(false)) { return false; // something wrong } - // 更新与用户相关的所有 CollectionTasks 和 Share 记录的状态为禁用 - CollectionTasks::updateAll(['status' => 0], ['user_id' => $this->id]); - Share::updateAll(['status' => 0], ['sharer_id' => $this->id]); + // 更新与用户相关的所有 CollectionTasks 和 Share 记录的状态 + CollectionTasks::updateAll(['status' => $rev ? 1 : 0], ['user_id' => $this->id]); + Share::updateAll(['status' => $rev ? 1 : 0], ['sharer_id' => $this->id]); return true; } diff --git a/views/admin/user.php b/views/admin/user.php index 8965bfb..6dd0e3b 100644 --- a/views/admin/user.php +++ b/views/admin/user.php @@ -73,7 +73,7 @@ $this->params['breadcrumbs'][] = $this->title; [ 'class' => ActionColumn::class, 'header' => '操作', - 'template' => '{view} {update}', + 'template' => '{view}', 'urlCreator' => function ($action, User $model, $key, $index, $column) { return Url::toRoute(['user-' . $action, 'id' => $model->id]); } diff --git a/views/admin/user_view.php b/views/admin/user_view.php index 9752979..99c9fd5 100644 --- a/views/admin/user_view.php +++ b/views/admin/user_view.php @@ -7,23 +7,28 @@ use yii\widgets\DetailView; /** @var yii\web\View $this */ /** @var app\models\User $model */ -$this->title = $model->name; -$this->params['breadcrumbs'][] = ['label' => 'Users', 'url' => ['user']]; +$this->title = '用户ID: '.$model->id; +$this->params['breadcrumbs'][] = ['label' => '用户管理', 'url' => ['user']]; $this->params['breadcrumbs'][] = $this->title; +$alreadyDisabled = $model->status == 0; +$isCurrentUser = Yii::$app->user->id == $model->id; +$str = $alreadyDisabled ? '启用' : '禁用'; YiiAsset::register($this); ?>
-

title) ?>

+

用户详情

- $model->id], ['class' => 'btn btn-primary']) ?> - $model->id], [ + $model->id], ['class' => 'btn btn-primary']) ?> + $model->id], [ 'class' => 'btn btn-danger', 'data' => [ - 'confirm' => 'Are you sure you want to delete this item?', + 'confirm' => '你确定要'.$str.'这个用户吗?', 'method' => 'post', ], + 'disabled' => $isCurrentUser, + 'title'=> $isCurrentUser ? '不能'.$str.'自己的账户' : '点击'.$str.'用户', ]) ?>