user.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. package model
  2. import (
  3. "errors"
  4. "one-api/common"
  5. )
  6. // User if you add sensitive fields, don't forget to clean them in setupLogin function.
  7. // Otherwise, the sensitive information will be saved on local storage in plain text!
  8. type User struct {
  9. Id int `json:"id"`
  10. Username string `json:"username" gorm:"unique;index" validate:"max=12"`
  11. Password string `json:"password" gorm:"not null;" validate:"min=8,max=20"`
  12. DisplayName string `json:"display_name" gorm:"index" validate:"max=20"`
  13. Role int `json:"role" gorm:"type:int;default:1"` // admin, common
  14. Status int `json:"status" gorm:"type:int;default:1"` // enabled, disabled
  15. Email string `json:"email" gorm:"index" validate:"max=50"`
  16. GitHubId string `json:"github_id" gorm:"column:github_id;index"`
  17. WeChatId string `json:"wechat_id" gorm:"column:wechat_id;index"`
  18. VerificationCode string `json:"verification_code" gorm:"-:all"` // this field is only for Email verification, don't save it to database!
  19. Balance int `json:"balance" gorm:"type:int;default:0"`
  20. }
  21. func GetMaxUserId() int {
  22. var user User
  23. DB.Last(&user)
  24. return user.Id
  25. }
  26. func GetAllUsers(startIdx int, num int) (users []*User, err error) {
  27. err = DB.Order("id desc").Limit(num).Offset(startIdx).Omit("password").Find(&users).Error
  28. return users, err
  29. }
  30. func SearchUsers(keyword string) (users []*User, err error) {
  31. err = DB.Omit("password").Where("id = ? or username LIKE ? or email LIKE ? or display_name LIKE ?", keyword, keyword+"%", keyword+"%", keyword+"%").Find(&users).Error
  32. return users, err
  33. }
  34. func GetUserById(id int, selectAll bool) (*User, error) {
  35. if id == 0 {
  36. return nil, errors.New("id 为空!")
  37. }
  38. user := User{Id: id}
  39. var err error = nil
  40. if selectAll {
  41. err = DB.First(&user, "id = ?", id).Error
  42. } else {
  43. err = DB.Omit("password").First(&user, "id = ?", id).Error
  44. }
  45. return &user, err
  46. }
  47. func DeleteUserById(id int) (err error) {
  48. if id == 0 {
  49. return errors.New("id 为空!")
  50. }
  51. user := User{Id: id}
  52. return user.Delete()
  53. }
  54. func (user *User) Insert() error {
  55. var err error
  56. if user.Password != "" {
  57. user.Password, err = common.Password2Hash(user.Password)
  58. if err != nil {
  59. return err
  60. }
  61. }
  62. err = DB.Create(user).Error
  63. return err
  64. }
  65. func (user *User) Update(updatePassword bool) error {
  66. var err error
  67. if updatePassword {
  68. user.Password, err = common.Password2Hash(user.Password)
  69. if err != nil {
  70. return err
  71. }
  72. }
  73. err = DB.Model(user).Updates(user).Error
  74. return err
  75. }
  76. func (user *User) Delete() error {
  77. if user.Id == 0 {
  78. return errors.New("id 为空!")
  79. }
  80. err := DB.Delete(user).Error
  81. return err
  82. }
  83. // ValidateAndFill check password & user status
  84. func (user *User) ValidateAndFill() (err error) {
  85. // When querying with struct, GORM will only query with non-zero fields,
  86. // that means if your field’s value is 0, '', false or other zero values,
  87. // it won’t be used to build query conditions
  88. password := user.Password
  89. if user.Username == "" || password == "" {
  90. return errors.New("用户名或密码为空")
  91. }
  92. DB.Where(User{Username: user.Username}).First(user)
  93. okay := common.ValidatePasswordAndHash(password, user.Password)
  94. if !okay || user.Status != common.UserStatusEnabled {
  95. return errors.New("用户名或密码错误,或用户已被封禁")
  96. }
  97. return nil
  98. }
  99. func (user *User) FillUserById() error {
  100. if user.Id == 0 {
  101. return errors.New("id 为空!")
  102. }
  103. DB.Where(User{Id: user.Id}).First(user)
  104. return nil
  105. }
  106. func (user *User) FillUserByEmail() error {
  107. if user.Email == "" {
  108. return errors.New("email 为空!")
  109. }
  110. DB.Where(User{Email: user.Email}).First(user)
  111. return nil
  112. }
  113. func (user *User) FillUserByGitHubId() error {
  114. if user.GitHubId == "" {
  115. return errors.New("GitHub id 为空!")
  116. }
  117. DB.Where(User{GitHubId: user.GitHubId}).First(user)
  118. return nil
  119. }
  120. func (user *User) FillUserByWeChatId() error {
  121. if user.WeChatId == "" {
  122. return errors.New("WeChat id 为空!")
  123. }
  124. DB.Where(User{WeChatId: user.WeChatId}).First(user)
  125. return nil
  126. }
  127. func (user *User) FillUserByUsername() error {
  128. if user.Username == "" {
  129. return errors.New("username 为空!")
  130. }
  131. DB.Where(User{Username: user.Username}).First(user)
  132. return nil
  133. }
  134. func IsEmailAlreadyTaken(email string) bool {
  135. return DB.Where("email = ?", email).Find(&User{}).RowsAffected == 1
  136. }
  137. func IsWeChatIdAlreadyTaken(wechatId string) bool {
  138. return DB.Where("wechat_id = ?", wechatId).Find(&User{}).RowsAffected == 1
  139. }
  140. func IsGitHubIdAlreadyTaken(githubId string) bool {
  141. return DB.Where("github_id = ?", githubId).Find(&User{}).RowsAffected == 1
  142. }
  143. func IsUsernameAlreadyTaken(username string) bool {
  144. return DB.Where("username = ?", username).Find(&User{}).RowsAffected == 1
  145. }
  146. func ResetUserPasswordByEmail(email string, password string) error {
  147. if email == "" || password == "" {
  148. return errors.New("邮箱地址或密码为空!")
  149. }
  150. hashedPassword, err := common.Password2Hash(password)
  151. if err != nil {
  152. return err
  153. }
  154. err = DB.Model(&User{}).Where("email = ?", email).Update("password", hashedPassword).Error
  155. return err
  156. }