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.'用户', ]) ?>