ValidateAndFill check password & user status
()
| 410 | |
| 411 | // ValidateAndFill check password & user status |
| 412 | func (user *User) ValidateAndFill() (err error) { |
| 413 | // When querying with struct, GORM will only query with non-zero fields, |
| 414 | // that means if your field's value is 0, '', false or other zero values, |
| 415 | // it won't be used to build query conditions |
| 416 | password := user.Password |
| 417 | username := strings.TrimSpace(user.Username) |
| 418 | if username == "" || password == "" { |
| 419 | return errors.New("用户名或密码为空") |
| 420 | } |
| 421 | // find buy username or email |
| 422 | DB.Where("username = ? OR email = ?", username, username).First(user) |
| 423 | okay := common.ValidatePasswordAndHash(password, user.Password) |
| 424 | if !okay || user.Status != common.UserStatusEnabled { |
| 425 | return errors.New("用户名或密码错误,或用户已被封禁") |
| 426 | } |
| 427 | return nil |
| 428 | } |
| 429 | |
| 430 | func (user *User) FillUserById() error { |
| 431 | if user.Id == 0 { |
no test coverage detected