个人设置界面增加修改密码模块

其他零散改进不多赘述
This commit is contained in:
Chenx221 2024-03-03 15:32:49 +08:00
parent ae1ed4e1a0
commit 84b087362f
Signed by: chenx221
GPG Key ID: D7A9EC07024C3021
3 changed files with 90 additions and 18 deletions

View File

@ -32,6 +32,7 @@ class UserController extends Controller
'class' => VerbFilter::className(), 'class' => VerbFilter::className(),
'actions' => [ 'actions' => [
'delete' => ['POST'], 'delete' => ['POST'],
'change-password' => ['POST'],
], ],
], ],
] ]
@ -323,7 +324,7 @@ class UserController extends Controller
/** /**
* @return string|Response * @return string|Response
*/ */
public function actionInfo(): Response|string public function actionInfo(string $focus = null): Response|string
{ {
if (Yii::$app->user->isGuest) { if (Yii::$app->user->isGuest) {
Yii::$app->session->setFlash('error', '请先登录'); Yii::$app->session->setFlash('error', '请先登录');
@ -331,14 +332,19 @@ class UserController extends Controller
} }
$model = Yii::$app->user->identity; $model = Yii::$app->user->identity;
$dataDirectory = Yii::getAlias(Yii::$app->params['dataDirectory']) . '/' . Yii::$app->user->id; $usedSpace = FileSizeHelper::getDirectorySize(Yii::getAlias(Yii::$app->params['dataDirectory']) . '/' . Yii::$app->user->id);
$usedSpace = FileSizeHelper::getDirectorySize($dataDirectory);
$vaultUsedSpace = 0; // 保险箱已用空间暂时为0 $vaultUsedSpace = 0; // 保险箱已用空间暂时为0
$storageLimit = $model->storage_limit; $storageLimit = $model->storage_limit;
if (Yii::$app->request->isPost && $model->load(Yii::$app->request->post())) { if (Yii::$app->request->isPost && $model->load(Yii::$app->request->post())) {
if($model->save()){ if ($model->save()) {
Yii::$app->session->setFlash('success', '个人简介已更新'); Yii::$app->session->setFlash('success', '个人简介已更新');
return $this->refresh(); return $this->render('info', [
'model' => $model,
'usedSpace' => $usedSpace, // B
'vaultUsedSpace' => $vaultUsedSpace, // B
'storageLimit' => $storageLimit, // MB
'focus' => 'bio',
]);
} }
} }
return $this->render('info', [ return $this->render('info', [
@ -346,7 +352,31 @@ class UserController extends Controller
'usedSpace' => $usedSpace, // B 'usedSpace' => $usedSpace, // B
'vaultUsedSpace' => $vaultUsedSpace, // B 'vaultUsedSpace' => $vaultUsedSpace, // B
'storageLimit' => $storageLimit, // MB 'storageLimit' => $storageLimit, // MB
'focus' => $focus,
]); ]);
} }
/**
* @return Response|string
* @throws Exception
*/
public function actionChangePassword(): Response|string
{
if (Yii::$app->user->isGuest) {
return $this->goHome();
}
$model = Yii::$app->user->identity;
$model->scenario = 'changePassword';
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
$model->password = Yii::$app->security->generatePasswordHash($model->newPassword);
if ($model->save(false)) {
Yii::$app->session->setFlash('success', 'Password changed successfully.');
} else {
Yii::$app->session->setFlash('error', 'Failed to change password.');
}
}
return $this->redirect(['user/info', 'focus' => 'password']);
}
} }

View File

@ -36,6 +36,10 @@ class User extends ActiveRecord implements IdentityInterface
public $password2; // 重复密码 public $password2; // 重复密码
public $rememberMe; // 记住我 public $rememberMe; // 记住我
public $oldPassword; // 旧密码 修改密码用
public $newPassword; // 新密码 修改密码用
public $newPasswordRepeat; // 重复新密码 修改密码用
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
@ -63,9 +67,28 @@ class User extends ActiveRecord implements IdentityInterface
['email', 'email', 'on' => 'register'], ['email', 'email', 'on' => 'register'],
['username', 'unique', 'on' => 'register'], ['username', 'unique', 'on' => 'register'],
['email', 'unique', 'on' => 'register'], ['email', 'unique', 'on' => 'register'],
[['oldPassword', 'newPassword', 'newPasswordRepeat'], 'required', 'on' => 'changePassword'],
['oldPassword', 'validatePassword2', 'on' => 'changePassword'],
['newPassword', 'string', 'min' => 6, 'max' => 24, 'on' => 'changePassword'],
['newPasswordRepeat', 'compare', 'compareAttribute' => 'newPassword', 'on' => 'changePassword'],
['newPassword', 'compare', 'compareAttribute' => 'oldPassword', 'operator' => '!=', 'message' => '新密码不能与旧密码相同', 'on' => 'changePassword'],
]; ];
} }
/**
* @param $attribute
* @param $params
* @return void
*/
public function validatePassword2($attribute, $params): void
{
if (!$this->hasErrors()) {
if (!Yii::$app->security->validatePassword($this->$attribute, $this->password)) {
$this->addError($attribute, '原密码不匹配');
}
}
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */

View File

@ -8,10 +8,14 @@
/* @var $storageLimit int */ /* @var $storageLimit int */
/* @var $focus string */
use app\assets\FontAwesomeAsset; use app\assets\FontAwesomeAsset;
use app\utils\FileSizeHelper; use app\utils\FileSizeHelper;
use app\utils\IPLocation; use app\utils\IPLocation;
use yii\bootstrap5\ActiveForm;
use yii\bootstrap5\Html; use yii\bootstrap5\Html;
use yii\helpers\Url;
$this->title = '个人设置'; $this->title = '个人设置';
FontAwesomeAsset::register($this); FontAwesomeAsset::register($this);
@ -75,8 +79,9 @@ $totalUsedPercent = min(($usedPercent + $vaultUsedPercent), 100); //总已用百
<div class="accordion userAccordion" id="userAccordion"> <div class="accordion userAccordion" id="userAccordion">
<div class="accordion-item"> <div class="accordion-item">
<h2 class="accordion-header" id="headingStorage"> <h2 class="accordion-header" id="headingStorage">
<button class="accordion-button" type="button" data-bs-toggle="collapse" <button class="accordion-button <?= ($focus === 'storage' || $focus === null) ? '' : 'collapsed' ?>"
data-bs-target="#collapseStorage" aria-expanded="true"> type="button" data-bs-toggle="collapse"
data-bs-target="#collapseStorage" <?= ($focus === 'storage' || $focus === null) ? 'aria-expanded="true"' : '' ?>>
<span class="accordion-storage-content"> <span class="accordion-storage-content">
<span> <span>
<i class="fa-solid fa-hard-drive"></i> <i class="fa-solid fa-hard-drive"></i>
@ -88,7 +93,8 @@ $totalUsedPercent = min(($usedPercent + $vaultUsedPercent), 100); //总已用百
</span> </span>
</button> </button>
</h2> </h2>
<div id="collapseStorage" class="accordion-collapse collapse show"> <div id="collapseStorage"
class="accordion-collapse collapse <?= ($focus === 'storage' || $focus === null) ? 'show' : '' ?>">
<div class="accordion-body"> <div class="accordion-body">
<div class="storage-info"> <div class="storage-info">
<div class="storage-columns"> <div class="storage-columns">
@ -135,13 +141,14 @@ $totalUsedPercent = min(($usedPercent + $vaultUsedPercent), 100); //总已用百
</div> </div>
<div class="accordion-item"> <div class="accordion-item">
<h2 class="accordion-header" id="headingBio"> <h2 class="accordion-header" id="headingBio">
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" <button class="accordion-button <?= ($focus === 'bio') ? '' : 'collapsed' ?>" type="button"
data-bs-target="#collapseBio"> data-bs-toggle="collapse"
data-bs-target="#collapseBio" <?= ($focus === 'bio') ? 'aria-expanded="true"' : '' ?>>
<i class="fa-solid fa-address-card"></i> <i class="fa-solid fa-address-card"></i>
<span>个人简介</span> <span>个人简介</span>
</button> </button>
</h2> </h2>
<div id="collapseBio" class="accordion-collapse collapse"> <div id="collapseBio" class="accordion-collapse collapse <?= ($focus === 'bio') ? 'show' : '' ?>">
<div class="accordion-body"> <div class="accordion-body">
<?php $form = yii\widgets\ActiveForm::begin(); ?> <?php $form = yii\widgets\ActiveForm::begin(); ?>
<?= $form->field($model, 'bio')->textarea(['rows' => 6])->label('简介') ?> <?= $form->field($model, 'bio')->textarea(['rows' => 6])->label('简介') ?>
@ -154,27 +161,39 @@ $totalUsedPercent = min(($usedPercent + $vaultUsedPercent), 100); //总已用百
</div> </div>
<div class="accordion-item"> <div class="accordion-item">
<h2 class="accordion-header" id="headingPassword"> <h2 class="accordion-header" id="headingPassword">
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" <button class="accordion-button <?= ($focus === 'password') ? '' : 'collapsed' ?>" type="button"
data-bs-target="#collapsePassword"> data-bs-toggle="collapse"
data-bs-target="#collapsePassword" <?= ($focus === 'password') ? 'aria-expanded="true"' : '' ?>>
<i class="fa-solid fa-key"></i> <i class="fa-solid fa-key"></i>
<span>修改密码</span> <span>修改密码</span>
</button> </button>
</h2> </h2>
<div id="collapsePassword" class="accordion-collapse collapse"> <div id="collapsePassword" class="accordion-collapse collapse <?= ($focus === 'password') ? 'show' : '' ?>">
<div class="accordion-body"> <div class="accordion-body">
<!-- 修改密码表单相关内容 --> <?php $form = ActiveForm::begin([
'action' => Url::to(['user/change-password']),
'method' => 'post'
]); ?>
<?= $form->field($model, 'oldPassword')->passwordInput()->label('原密码') ?>
<?= $form->field($model, 'newPassword')->passwordInput()->label('新密码') ?>
<?= $form->field($model, 'newPasswordRepeat')->passwordInput()->label('重复新密码') ?>
<div class="form-group">
<?= Html::submitButton('保存', ['class' => 'btn btn-success']) ?>
</div>
<?php ActiveForm::end(); ?>
</div> </div>
</div> </div>
</div> </div>
<div class="accordion-item"> <div class="accordion-item">
<h2 class="accordion-header" id="headingAdvanced"> <h2 class="accordion-header" id="headingAdvanced">
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" <button class="accordion-button <?= ($focus === 'advanced') ? '' : 'collapsed' ?>" type="button"
data-bs-target="#collapseAdvanced"> data-bs-toggle="collapse"
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"> <div id="collapseAdvanced" class="accordion-collapse collapse <?= ($focus === 'advanced') ? 'show' : '' ?>">
<div class="accordion-body"> <div class="accordion-body">
<!-- 高级选项相关内容 --> <!-- 高级选项相关内容 -->
</div> </div>