用户管理功能(1.75/5)

用户启用/禁用
*禁用时会强制下线
This commit is contained in:
Chenx221 2024-03-23 14:28:36 +08:00
parent c77acae424
commit c8bdaf73d3
Signed by: chenx221
GPG Key ID: D7A9EC07024C3021
4 changed files with 40 additions and 21 deletions

View File

@ -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]);
}
/**

View File

@ -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;
}

View File

@ -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]);
}

View File

@ -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);
?>
<div class="user-view">
<h1><?= Html::encode($this->title) ?></h1>
<h1>用户详情</h1>
<p>
<?= Html::a('Update', ['user-update', 'id' => $model->id], ['class' => 'btn btn-primary']) ?>
<?= Html::a('Delete', ['user-delete', 'id' => $model->id], [
<?= Html::a('修改信息', ['user-update', 'id' => $model->id], ['class' => 'btn btn-primary']) ?>
<?= Html::a($str.'用户', ['user-delete', 'id' => $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.'用户',
]) ?>
</p>