diff --git a/http/controller/admin/user.go b/http/controller/admin/user.go index 61eca66..67f71b8 100644 --- a/http/controller/admin/user.go +++ b/http/controller/admin/user.go @@ -248,10 +248,14 @@ func (ct *User) ChangeCurPwd(c *gin.Context) { return } u := service.AllService.UserService.CurUser(c) - oldPwd := service.AllService.UserService.EncryptPassword(f.OldPassword) - if u.Password != oldPwd { - response.Fail(c, 101, response.TranslateMsg(c, "OldPasswordError")) - return + // If the password is not empty, the old password is verified + // otherwise, the old password is not verified + if !service.AllService.UserService.IsPasswordEmptyByUser(u) { + oldPwd := service.AllService.UserService.EncryptPassword(f.OldPassword) + if u.Password != oldPwd { + response.Fail(c, 101, response.TranslateMsg(c, "OldPasswordError")) + return + } } err := service.AllService.UserService.UpdatePassword(u, f.NewPassword) if err != nil { diff --git a/service/user.go b/service/user.go index e586bfc..5708f1a 100644 --- a/service/user.go +++ b/service/user.go @@ -324,6 +324,29 @@ func (us *UserService) FindLatestUserIdFromLoginLogByUuid(uuid string) uint { return llog.UserId } +// IsPasswordEmptyById 根据用户id判断密码是否为空,主要用于第三方登录的自动注册 +func (us *UserService) IsPasswordEmptyById(id uint) bool { + u := &model.User{} + if global.DB.Where("id = ?", id).First(u).Error != nil { + return false + } + return u.Password == "" +} + +// IsPasswordEmptyByUsername 根据用户id判断密码是否为空,主要用于第三方登录的自动注册 +func (us *UserService) IsPasswordEmptyByUsername(username string) bool { + u := &model.User{} + if global.DB.Where("username = ?", username).First(u).Error != nil { + return false + } + return u.Password == "" +} + +// IsPasswordEmptyByUser 判断密码是否为空,主要用于第三方登录的自动注册 +func (us *UserService) IsPasswordEmptyByUser(u *model.User) bool { + return us.IsPasswordEmptyById(u.Id) +} + func (us *UserService) Register(username string, password string) *model.User { u := &model.User{ Username: username,