user.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package model
  2. import (
  3. "errors"
  4. "message-pusher/common"
  5. )
  6. type User struct {
  7. Id int `json:"id"`
  8. Username string `json:"username" gorm:"unique;index" validate:"max=12"`
  9. Password string `json:"password" gorm:"not null;" validate:"min=8,max=20"`
  10. DisplayName string `json:"display_name" gorm:"index" validate:"max=20"`
  11. Role int `json:"role" gorm:"type:int;default:1"` // admin, common
  12. Status int `json:"status" gorm:"type:int;default:1"` // enabled, disabled
  13. Token string `json:"token"`
  14. Email string `json:"email" gorm:"index" validate:"max=50"`
  15. GitHubId string `json:"github_id" gorm:"column:github_id;index"`
  16. WeChatId string `json:"wechat_id" gorm:"column:wechat_id;index"`
  17. Channel string `json:"channel"`
  18. VerificationCode string `json:"verification_code" gorm:"-:all"`
  19. WeChatTestAccountId string `json:"wechat_test_account_id" gorm:"column:wechat_test_account_id"`
  20. WeChatTestAccountSecret string `json:"wechat_test_account_secret" gorm:"column:wechat_test_account_secret"`
  21. WeChatTestAccountTemplateId string `json:"wechat_test_account_template_id" gorm:"column:wechat_test_account_template_id"`
  22. WeChatTestAccountOpenId string `json:"wechat_test_account_open_id" gorm:"column:wechat_test_account_open_id"`
  23. WeChatTestAccountVerificationToken string `json:"wechat_test_account_verification_token" gorm:"column:wechat_test_account_verification_token"`
  24. WeChatCorpAccountId string `json:"wechat_corp_account_id" gorm:"column:wechat_corp_account_id"`
  25. WeChatCorpAccountSecret string `json:"wechat_corp_account_secret" gorm:"column:wechat_corp_account_secret"`
  26. WeChatCorpAccountAgentId string `json:"wechat_corp_account_agent_id" gorm:"column:wechat_corp_account_agent_id"`
  27. WeChatCorpAccountUserId string `json:"wechat_corp_account_user_id" gorm:"column:wechat_corp_account_user_id"`
  28. LarkWebhookURL string `json:"lark_webhook_url"`
  29. LarkWebhookSecret string `json:"lark_webhook_secret"`
  30. }
  31. func GetMaxUserId() int {
  32. var user User
  33. DB.Last(&user)
  34. return user.Id
  35. }
  36. func GetAllUsers() (users []*User, err error) {
  37. err = DB.Select([]string{"id", "username", "display_name", "role", "status", "email"}).Find(&users).Error
  38. return users, err
  39. }
  40. func GetAllUsersWithSecrets() (users []*User, err error) {
  41. err = DB.Find(&users).Error
  42. return users, err
  43. }
  44. func SearchUsers(keyword string) (users []*User, err error) {
  45. err = DB.Select([]string{"id", "username", "display_name", "role", "status", "email"}).Where("id = ? or username LIKE ? or email LIKE ? or display_name LIKE ?", keyword, keyword+"%", keyword+"%", keyword+"%").Find(&users).Error
  46. return users, err
  47. }
  48. func GetUserById(id int, selectAll bool) (*User, error) {
  49. user := User{Id: id}
  50. var err error = nil
  51. if selectAll {
  52. err = DB.First(&user, "id = ?", id).Error
  53. } else {
  54. err = DB.Select([]string{"id", "username", "display_name", "role", "status", "email", "wechat_id", "github_id", "token"}).First(&user, "id = ?", id).Error
  55. }
  56. return &user, err
  57. }
  58. func DeleteUserById(id int) (err error) {
  59. user := User{Id: id}
  60. err = DB.Delete(&user).Error
  61. return err
  62. }
  63. func (user *User) Insert() error {
  64. var err error
  65. if user.Password != "" {
  66. user.Password, err = common.Password2Hash(user.Password)
  67. if err != nil {
  68. return err
  69. }
  70. }
  71. err = DB.Create(user).Error
  72. return err
  73. }
  74. func (user *User) Update(updatePassword bool) error {
  75. var err error
  76. if updatePassword {
  77. user.Password, err = common.Password2Hash(user.Password)
  78. if err != nil {
  79. return err
  80. }
  81. }
  82. err = DB.Model(user).Updates(user).Error
  83. return err
  84. }
  85. func (user *User) Delete() error {
  86. var err error
  87. err = DB.Delete(user).Error
  88. return err
  89. }
  90. // ValidateAndFill check password & user status
  91. func (user *User) ValidateAndFill() (err error) {
  92. // When querying with struct, GORM will only query with non-zero fields,
  93. // that means if your field’s value is 0, '', false or other zero values,
  94. // it won’t be used to build query conditions
  95. password := user.Password
  96. DB.Where(User{Username: user.Username}).First(user)
  97. okay := common.ValidatePasswordAndHash(password, user.Password)
  98. if !okay || user.Status != common.UserStatusEnabled {
  99. return errors.New("用户名或密码错误,或者该用户已被封禁")
  100. }
  101. return nil
  102. }
  103. func (user *User) FillUserById() {
  104. DB.Where(User{Id: user.Id}).First(user)
  105. }
  106. func (user *User) FillUserByEmail() {
  107. DB.Where(User{Email: user.Email}).First(user)
  108. }
  109. func (user *User) FillUserByGitHubId() {
  110. DB.Where(User{GitHubId: user.GitHubId}).First(user)
  111. }
  112. func (user *User) FillUserByWeChatId() {
  113. DB.Where(User{WeChatId: user.WeChatId}).First(user)
  114. }
  115. func (user *User) FillUserByUsername() {
  116. DB.Where(User{Username: user.Username}).First(user)
  117. }
  118. func IsEmailAlreadyTaken(email string) bool {
  119. return DB.Where("email = ?", email).Find(&User{}).RowsAffected == 1
  120. }
  121. func IsWeChatIdAlreadyTaken(wechatId string) bool {
  122. return DB.Where("wechat_id = ?", wechatId).Find(&User{}).RowsAffected == 1
  123. }
  124. func IsGitHubIdAlreadyTaken(githubId string) bool {
  125. return DB.Where("github_id = ?", githubId).Find(&User{}).RowsAffected == 1
  126. }
  127. func IsUsernameAlreadyTaken(username string) bool {
  128. return DB.Where("username = ?", username).Find(&User{}).RowsAffected == 1
  129. }
  130. func ResetUserPasswordByEmail(email string, password string) error {
  131. hashedPassword, err := common.Password2Hash(password)
  132. if err != nil {
  133. return err
  134. }
  135. err = DB.Model(&User{}).Where("email = ?", email).Update("password", hashedPassword).Error
  136. return err
  137. }