Skip to content

Commit ece982c

Browse files
authored
optimize: select and update necessary fields in ChangePassword method (#2058)
- Simplify `ChangePassword` method signature by removing unnecessary return type. - Use `Select()` to fetch only the necessary fields (`id` and `password`) from the database. - Replace `Save()` with `Update()` for more efficient password update operation. Note: use `Save(&user)` to update the whole user record, which will cover other unchanged fields as well, causing data inconsistency when data race conditions.
2 parents 65a0a7d + b2287e7 commit ece982c

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

server/api/v1/system/sys_user.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ func (b *BaseApi) ChangePassword(c *gin.Context) {
184184
}
185185
uid := utils.GetUserID(c)
186186
u := &system.SysUser{GVA_MODEL: global.GVA_MODEL{ID: uid}, Password: req.Password}
187-
_, err = userService.ChangePassword(u, req.NewPassword)
187+
err = userService.ChangePassword(u, req.NewPassword)
188188
if err != nil {
189189
global.GVA_LOG.Error("修改失败!", zap.Error(err))
190190
response.FailWithMessage("修改失败,原密码与当前账户不符", c)

server/service/system/sys_user.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ package system
33
import (
44
"errors"
55
"fmt"
6+
"time"
7+
68
"github.com/flipped-aurora/gin-vue-admin/server/model/common"
79
systemReq "github.com/flipped-aurora/gin-vue-admin/server/model/system/request"
8-
"time"
910

1011
"github.com/flipped-aurora/gin-vue-admin/server/global"
1112
"github.com/flipped-aurora/gin-vue-admin/server/model/system"
@@ -63,20 +64,20 @@ func (userService *UserService) Login(u *system.SysUser) (userInter *system.SysU
6364
//@function: ChangePassword
6465
//@description: 修改用户密码
6566
//@param: u *model.SysUser, newPassword string
66-
//@return: userInter *model.SysUser,err error
67+
//@return: err error
6768

68-
func (userService *UserService) ChangePassword(u *system.SysUser, newPassword string) (userInter *system.SysUser, err error) {
69+
func (userService *UserService) ChangePassword(u *system.SysUser, newPassword string) (err error) {
6970
var user system.SysUser
70-
if err = global.GVA_DB.Where("id = ?", u.ID).First(&user).Error; err != nil {
71-
return nil, err
71+
err = global.GVA_DB.Select("id, password").Where("id = ?", u.ID).First(&user).Error
72+
if err != nil {
73+
return err
7274
}
7375
if ok := utils.BcryptCheck(u.Password, user.Password); !ok {
74-
return nil, errors.New("原密码错误")
76+
return errors.New("原密码错误")
7577
}
76-
user.Password = utils.BcryptHash(newPassword)
77-
err = global.GVA_DB.Save(&user).Error
78-
return &user, err
79-
78+
pwd := utils.BcryptHash(newPassword)
79+
err = global.GVA_DB.Model(&user).Update("password", pwd).Error
80+
return err
8081
}
8182

8283
//@author: [piexlmax](https://github.com/piexlmax)

0 commit comments

Comments
 (0)