Просмотр исходного кода

Merge pull request #40 from IamTaoChen/resetEmptyPassWD

Reset empty password
1 год назад
Родитель
Сommit
095640af3f
2 измененных файлов с 31 добавлено и 4 удалено
  1. 8 4
      http/controller/admin/user.go
  2. 23 0
      service/user.go

+ 8 - 4
http/controller/admin/user.go

@@ -248,10 +248,14 @@ func (ct *User) ChangeCurPwd(c *gin.Context) {
248 248
 		return
249 249
 	}
250 250
 	u := service.AllService.UserService.CurUser(c)
251
-	oldPwd := service.AllService.UserService.EncryptPassword(f.OldPassword)
252
-	if u.Password != oldPwd {
253
-		response.Fail(c, 101, response.TranslateMsg(c, "OldPasswordError"))
254
-		return
251
+	// If the password is not empty, the old password is verified
252
+	// otherwise, the old password is not verified
253
+	if !service.AllService.UserService.IsPasswordEmptyByUser(u) {
254
+		oldPwd := service.AllService.UserService.EncryptPassword(f.OldPassword)
255
+		if u.Password != oldPwd {
256
+			response.Fail(c, 101, response.TranslateMsg(c, "OldPasswordError"))
257
+			return
258
+		}
255 259
 	}
256 260
 	err := service.AllService.UserService.UpdatePassword(u, f.NewPassword)
257 261
 	if err != nil {

+ 23 - 0
service/user.go

@@ -324,6 +324,29 @@ func (us *UserService) FindLatestUserIdFromLoginLogByUuid(uuid string) uint {
324 324
 	return llog.UserId
325 325
 }
326 326
 
327
+// IsPasswordEmptyById 根据用户id判断密码是否为空,主要用于第三方登录的自动注册
328
+func (us *UserService) IsPasswordEmptyById(id uint) bool {
329
+	u := &model.User{}
330
+	if global.DB.Where("id = ?", id).First(u).Error != nil {
331
+		return false
332
+	}
333
+	return u.Password == ""
334
+}
335
+
336
+// IsPasswordEmptyByUsername 根据用户id判断密码是否为空,主要用于第三方登录的自动注册
337
+func (us *UserService) IsPasswordEmptyByUsername(username string) bool {
338
+	u := &model.User{}
339
+	if global.DB.Where("username = ?", username).First(u).Error != nil {
340
+		return false
341
+	}
342
+	return u.Password == ""
343
+}
344
+
345
+// IsPasswordEmptyByUser 判断密码是否为空,主要用于第三方登录的自动注册
346
+func (us *UserService) IsPasswordEmptyByUser(u *model.User) bool {
347
+	return us.IsPasswordEmptyById(u.Id)
348
+}
349
+
327 350
 func (us *UserService) Register(username string, password string) *model.User {
328 351
 	u := &model.User{
329 352
 		Username: username,