From be64c58b112827f8b7d72ec3c5796c980fc284a8 Mon Sep 17 00:00:00 2001 From: Chenx221 Date: Sat, 9 Mar 2024 14:38:42 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=98=BE=E7=A4=BA/=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E7=94=A8=E6=88=B7=E6=98=B5=E7=A7=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controllers/UserController.php | 32 ++++++++++++++++++++++++++------ models/User.php | 2 +- views/user/info.php | 33 +++++++++++++++++++++++++++------ web/css/user-info.css | 18 ++++++++++++++++++ web/js/user-info.js | 5 +++++ 5 files changed, 77 insertions(+), 13 deletions(-) diff --git a/controllers/UserController.php b/controllers/UserController.php index 6caf0ff..5b5765a 100644 --- a/controllers/UserController.php +++ b/controllers/UserController.php @@ -45,8 +45,8 @@ class UserController extends Controller ], [ 'allow' => true, - 'actions' => ['logout', 'setup-two-factor', 'change-password', 'download-recovery-codes', 'remove-two-factor', 'set-theme'], - 'roles' => ['@'], // only logged-in user can do these + 'actions' => ['logout', 'setup-two-factor', 'change-password', 'download-recovery-codes', 'remove-two-factor', 'set-theme', 'change-name'], + 'roles' => ['@'], // only logged-in user can do these ( admin included ) ] ], ], @@ -64,6 +64,7 @@ class UserController extends Controller 'remove-two-factor' => ['POST'], 'verify-two-factor' => ['GET', 'POST'], 'set-theme' => ['POST'], + 'change-name' => ['POST'], ], ], ] @@ -183,7 +184,7 @@ class UserController extends Controller if ($model->load(Yii::$app->request->post())) { // 验证二步验证代码 - if(!is_null($model->totp_input)){ + if (!is_null($model->totp_input)) { $otp = TOTP::createFromSecret($user->otp_secret); if ($otp->verify($model->totp_input)) { $user->last_login = date('Y-m-d H:i:s'); @@ -197,7 +198,7 @@ class UserController extends Controller } else { Yii::$app->session->setFlash('error', '二步验证代码错误'); } - }elseif (!is_null($model->recoveryCode_input)) { + } elseif (!is_null($model->recoveryCode_input)) { $recoveryCodes = explode(',', $user->recovery_codes); if (in_array($model->recoveryCode_input, $recoveryCodes)) { //remove the used recovery code @@ -215,7 +216,7 @@ class UserController extends Controller } else { Yii::$app->session->setFlash('error', '恢复代码错误'); } - }else{ + } else { Yii::$app->session->setFlash('error', '请输入二步验证代码或恢复代码'); } } @@ -362,6 +363,10 @@ class UserController extends Controller } /** + * 显示用户信息 + * 同时接收post请求来修改用户bio + * 支持参数focus指定页面加载时自动展开哪一块区域 [null,storage,bio,password,advanced] + * * @param string|null $focus * @return string|Response */ @@ -508,7 +513,7 @@ class UserController extends Controller } else { // 如果用户没有启用 TOTP,返回一个错误消息 Yii::$app->session->setFlash('error', '获取失败,您还没有启用二步验证。'); - return $this->redirect(['user/info']); + return $this->redirect(['user/info', 'focus' => 'advanced']); } } @@ -524,4 +529,19 @@ class UserController extends Controller $user->save(); return $this->asJson(['success' => true]); } + + /** + * 修改用户昵称 + * @return Response + */ + public function actionChangeName(): Response + { + $model = Yii::$app->user->identity; + if ($model->load(Yii::$app->request->post()) && $model->save()) { + Yii::$app->session->setFlash('success', '昵称已更新'); + } else { + Yii::$app->session->setFlash('error', '昵称更新失败'); + } + return $this->redirect(['user/info']); + } } diff --git a/models/User.php b/models/User.php index 54ad145..f884ed7 100644 --- a/models/User.php +++ b/models/User.php @@ -60,7 +60,7 @@ class User extends ActiveRecord implements IdentityInterface return [ [['status', 'is_encryption_enabled', 'is_otp_enabled','dark_mode'], 'integer'], [['created_at', 'last_login'], 'safe'], - [['bio', 'totp_input','recoveryCode_input'], 'string'], + [['bio', 'totp_input','recoveryCode_input','name'], 'string'], [['encryption_key', 'otp_secret', 'recovery_codes'], 'string', 'max' => 255], [['last_login_ip'], 'string', 'max' => 45], [['username', 'password'], 'required', 'on' => 'login'], diff --git a/views/user/info.php b/views/user/info.php index 720bdb4..267a789 100644 --- a/views/user/info.php +++ b/views/user/info.php @@ -80,7 +80,10 @@ $darkMode = Yii::$app->user->identity->dark_mode;