新增删除自身账户功能

*仅简单测试了下
This commit is contained in:
Chenx221 2024-03-04 16:51:19 +08:00
parent 63fef55f65
commit 4a22f950e5
Signed by: chenx221
GPG Key ID: D7A9EC07024C3021
4 changed files with 256 additions and 141 deletions

View File

@ -110,18 +110,23 @@ class UserController extends Controller
]); ]);
} }
/** public function actionDelete(): Response
* 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
*/
public function actionDelete($id)
{ {
$this->findModel($id)->delete(); if (Yii::$app->user->isGuest) {
Yii::$app->session->setFlash('error', '滚');
return $this->goHome();
}
return $this->redirect(['index']); $model = Yii::$app->user->identity;
if ($model->deleteAccount()) {
Yii::$app->user->logout();
Yii::$app->session->setFlash('success', 'Account deleted successfully.');
} else {
Yii::$app->session->setFlash('error', 'Failed to delete account.');
}
return $this->redirect(['user/login']);
} }
/** /**
@ -182,10 +187,10 @@ class UserController extends Controller
Yii::$app->session->setFlash('error', '登陆成功,但出现了内部错误'); Yii::$app->session->setFlash('error', '登陆成功,但出现了内部错误');
} }
} else { } else {
Yii::$app->session->setFlash('error', 'Invalid username or password.'); Yii::$app->session->setFlash('error', '用户名密码错误或账户已禁用');
} }
} else { } else {
Yii::$app->session->setFlash('error', 'Invalid captcha.'); Yii::$app->session->setFlash('error', '请等待验证码加载并完成验证');
} }
} }
return $this->render('login', [ return $this->render('login', [

View File

@ -252,4 +252,20 @@ class User extends ActiveRecord implements IdentityInterface
return $url; return $url;
} }
public function deleteAccount(): false|int
{
// 设置用户状态为禁用
$this->status = 0;
// 保存用户模型
if (!$this->save()) {
return false; // something wrong
}
// 更新与用户相关的所有 CollectionTasks 和 Share 记录的状态为禁用
CollectionTasks::updateAll(['status' => 0], ['user_id' => $this->id]);
Share::updateAll(['status' => 0], ['sharer_id' => $this->id]);
return true;
}
} }

View File

@ -15,10 +15,14 @@ use app\utils\FileSizeHelper;
use app\utils\IPLocation; use app\utils\IPLocation;
use yii\bootstrap5\ActiveForm; use yii\bootstrap5\ActiveForm;
use yii\bootstrap5\Html; use yii\bootstrap5\Html;
use yii\bootstrap5\Modal;
use yii\helpers\Url; use yii\helpers\Url;
use yii\web\JqueryAsset;
use yii\web\View;
$this->title = '个人设置'; $this->title = '个人设置';
FontAwesomeAsset::register($this); FontAwesomeAsset::register($this);
JqueryAsset::register($this);
$this->registerCssFile('@web/css/user-info.css'); $this->registerCssFile('@web/css/user-info.css');
$details = IPLocation::getDetails($model->last_login_ip); // IP LOCATION $details = IPLocation::getDetails($model->last_login_ip); // IP LOCATION
@ -33,7 +37,7 @@ $vaultUsedPercent = $is_unlimited ? 0 : round($vaultUsedSpace / ($storageLimit *
$totalUsedPercent = min(($usedPercent + $vaultUsedPercent), 100); //总已用百分比 $totalUsedPercent = min(($usedPercent + $vaultUsedPercent), 100); //总已用百分比
?> ?>
<div class="user-info"> <div class="user-info">
<h1><?= Html::encode($this->title) ?></h1> <h1><?= Html::encode($this->title) ?></h1>
@ -118,7 +122,8 @@ $totalUsedPercent = min(($usedPercent + $vaultUsedPercent), 100); //总已用百
</div> </div>
<div class="storage-legend" style="color: rgb(140,139,139)"> <div class="storage-legend" style="color: rgb(140,139,139)">
<div class="legend-item"> <div class="legend-item">
<span class="legend-color" style="background-color: rgb(52,131,250);"></span> <span class="legend-color"
style="background-color: rgb(52,131,250);"></span>
<span>网盘已用空间</span> <span>网盘已用空间</span>
<span style="margin-left: auto;"><?= $usedSpace_F ?> <span style="margin-left: auto;"><?= $usedSpace_F ?>
<?= Html::a('<i class="fa-solid fa-arrow-up-right-from-square" style="font-size: 0.75rem;"></i>', ['home/index']) ?> <?= Html::a('<i class="fa-solid fa-arrow-up-right-from-square" style="font-size: 0.75rem;"></i>', ['home/index']) ?>
@ -168,7 +173,8 @@ $totalUsedPercent = min(($usedPercent + $vaultUsedPercent), 100); //总已用百
<span>修改密码</span> <span>修改密码</span>
</button> </button>
</h2> </h2>
<div id="collapsePassword" class="accordion-collapse collapse <?= ($focus === 'password') ? 'show' : '' ?>"> <div id="collapsePassword"
class="accordion-collapse collapse <?= ($focus === 'password') ? 'show' : '' ?>">
<div class="accordion-body"> <div class="accordion-body">
<?php $form = ActiveForm::begin([ <?php $form = ActiveForm::begin([
'action' => Url::to(['user/change-password']), 'action' => Url::to(['user/change-password']),
@ -178,7 +184,7 @@ $totalUsedPercent = min(($usedPercent + $vaultUsedPercent), 100); //总已用百
<?= $form->field($model, 'newPassword')->passwordInput()->label('新密码') ?> <?= $form->field($model, 'newPassword')->passwordInput()->label('新密码') ?>
<?= $form->field($model, 'newPasswordRepeat')->passwordInput()->label('重复新密码') ?> <?= $form->field($model, 'newPasswordRepeat')->passwordInput()->label('重复新密码') ?>
<div class="form-group"> <div class="form-group">
<?= Html::submitButton('保存', ['class' => 'btn btn-success']) ?> <?= Html::submitButton('修改密码', ['class' => 'btn btn-success']) ?>
</div> </div>
<?php ActiveForm::end(); ?> <?php ActiveForm::end(); ?>
</div> </div>
@ -190,15 +196,94 @@ $totalUsedPercent = min(($usedPercent + $vaultUsedPercent), 100); //总已用百
data-bs-toggle="collapse" data-bs-toggle="collapse"
data-bs-target="#collapseAdvanced" <?= ($focus === 'advanced') ? 'aria-expanded="true"' : '' ?>> data-bs-target="#collapseAdvanced" <?= ($focus === 'advanced') ? 'aria-expanded="true"' : '' ?>>
<i class="fa-solid fa-flask"></i> <i class="fa-solid fa-flask"></i>
<span>高级选项</span> <span>高级功能</span>
</button> </button>
</h2> </h2>
<div id="collapseAdvanced" class="accordion-collapse collapse <?= ($focus === 'advanced') ? 'show' : '' ?>"> <div id="collapseAdvanced"
class="accordion-collapse collapse <?= ($focus === 'advanced') ? 'show' : '' ?>">
<div class="accordion-body"> <div class="accordion-body">
<!-- 高级选项相关内容 --> <!--TODO:二步验证、passwordless-->
</div> <h4>二步验证</h4>
<hr>
<p>使用除您密码之外的第二种方法来增强您账号的安全性。</p>
<ul class="list-group list-group-flush">
<li class="list-group-item">
<h5>
<i class="fa-solid fa-shield-halved"></i>
TOTP (Authenticator app)
</h5>
<div>
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" role="switch" id="totp-enabled">
<label class="form-check-label" for="totp-enabled">启用 TOTP</label>
</div> </div>
</div> </div>
</li>
<li class="list-group-item">
<h5>
<i class="fa-solid fa-user-lock"></i>
备用码
</h5>
<div>
<button id="generate-backup-codes" class="btn btn-outline-primary btn-sm">
生成备用码
</button>
</div> </div>
</li>
</ul>
<br>
</div> <h4>无密码认证</h4>
<hr>
<p>遵循 FIDO2 标准为无密码身份验证设置您的账号。</p>
<br>
<h4>主题</h4>
<hr>
<p>可以在</p>
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" role="switch" id="useDarkTheme">
<label class="form-check-label" for="useDarkTheme">启用夜间模式</label>
</div>
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" role="switch" id="autoTheme">
<label class="form-check-label" for="autoTheme">Auto</label>
</div>
<br>
<h4>删除账户</h4>
<hr>
<p>这个操作不支持撤回,请谨慎操作。</p>
<button type="button" class="btn btn-danger" data-bs-toggle="modal"
data-bs-target="#deleteAccountModal">
删除账户
</button>
</div>
</div>
</div>
</div>
</div>
<?php
Modal::begin([
'title' => '<h4>确定?</h4>',
'id' => 'deleteAccountModal',
'size' => 'modal-sm',
]);
echo Html::tag('div', '确定要删除这个账户?', ['class' => 'modal-body']);
echo Html::beginForm(['user/delete'], 'post', ['id' => 'delete-form']);
echo '<div>';
echo Html::checkbox('deleteConfirm', false, ['label' => '确认','id'=>'deleteConfirm']);
echo '</div>';
echo '<div class="text-end">';
echo Html::submitButton('继续删除', ['class' => 'btn btn-danger', 'disabled' => true,'id' => 'deleteButton']);
echo '</div>';
echo Html::endForm();
Modal::end();
$this->registerJsFile('@web/js/user-info.js', ['depends' => [JqueryAsset::class], 'position' => View::POS_END]);
?>

9
web/js/user-info.js Normal file
View File

@ -0,0 +1,9 @@
$(document).ready(function() {
$('#deleteConfirm').change(function() {
if(this.checked) {
$('#deleteButton').prop('disabled', false);
} else {
$('#deleteButton').prop('disabled', true);
}
});
});