user.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. WeChatCorpAccountClientType string `json:"wechat_corp_account_client_type" gorm:"wechat_corp_account_client_type;default=plugin"`
  29. LarkWebhookURL string `json:"lark_webhook_url"`
  30. LarkWebhookSecret string `json:"lark_webhook_secret"`
  31. DingWebhookURL string `json:"ding_webhook_url"`
  32. DingWebhookSecret string `json:"ding_webhook_secret"`
  33. }
  34. func GetMaxUserId() int {
  35. var user User
  36. DB.Last(&user)
  37. return user.Id
  38. }
  39. func GetAllUsers() (users []*User, err error) {
  40. err = DB.Select([]string{"id", "username", "display_name", "role", "status", "email"}).Find(&users).Error
  41. return users, err
  42. }
  43. func GetAllUsersWithSecrets() (users []*User, err error) {
  44. err = DB.Find(&users).Error
  45. return users, err
  46. }
  47. func SearchUsers(keyword string) (users []*User, err error) {
  48. 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
  49. return users, err
  50. }
  51. func GetUserById(id int, selectAll bool) (*User, error) {
  52. user := User{Id: id}
  53. var err error = nil
  54. if selectAll {
  55. err = DB.First(&user, "id = ?", id).Error
  56. } else {
  57. err = DB.Select([]string{"id", "username", "display_name", "role", "status", "email", "wechat_id", "github_id", "token"}).First(&user, "id = ?", id).Error
  58. }
  59. return &user, err
  60. }
  61. func DeleteUserById(id int) (err error) {
  62. user := User{Id: id}
  63. err = DB.Delete(&user).Error
  64. return err
  65. }
  66. func (user *User) Insert() error {
  67. var err error
  68. if user.Password != "" {
  69. user.Password, err = common.Password2Hash(user.Password)
  70. if err != nil {
  71. return err
  72. }
  73. }
  74. err = DB.Create(user).Error
  75. return err
  76. }
  77. func (user *User) Update(updatePassword bool) error {
  78. var err error
  79. if updatePassword {
  80. user.Password, err = common.Password2Hash(user.Password)
  81. if err != nil {
  82. return err
  83. }
  84. }
  85. err = DB.Model(user).Updates(user).Error
  86. return err
  87. }
  88. func (user *User) Delete() error {
  89. var err error
  90. err = DB.Delete(user).Error
  91. return err
  92. }
  93. // ValidateAndFill check password & user status
  94. func (user *User) ValidateAndFill() (err error) {
  95. // When querying with struct, GORM will only query with non-zero fields,
  96. // that means if your field’s value is 0, '', false or other zero values,
  97. // it won’t be used to build query conditions
  98. password := user.Password
  99. DB.Where(User{Username: user.Username}).First(user)
  100. okay := common.ValidatePasswordAndHash(password, user.Password)
  101. if !okay || user.Status != common.UserStatusEnabled {
  102. return errors.New("用户名或密码错误,或者该用户已被封禁")
  103. }
  104. return nil
  105. }
  106. func (user *User) FillUserById() {
  107. DB.Where(User{Id: user.Id}).First(user)
  108. }
  109. func (user *User) FillUserByEmail() {
  110. DB.Where(User{Email: user.Email}).First(user)
  111. }
  112. func (user *User) FillUserByGitHubId() {
  113. DB.Where(User{GitHubId: user.GitHubId}).First(user)
  114. }
  115. func (user *User) FillUserByWeChatId() {
  116. DB.Where(User{WeChatId: user.WeChatId}).First(user)
  117. }
  118. func (user *User) FillUserByUsername() {
  119. DB.Where(User{Username: user.Username}).First(user)
  120. }
  121. func IsEmailAlreadyTaken(email string) bool {
  122. return DB.Where("email = ?", email).Find(&User{}).RowsAffected == 1
  123. }
  124. func IsWeChatIdAlreadyTaken(wechatId string) bool {
  125. return DB.Where("wechat_id = ?", wechatId).Find(&User{}).RowsAffected == 1
  126. }
  127. func IsGitHubIdAlreadyTaken(githubId string) bool {
  128. return DB.Where("github_id = ?", githubId).Find(&User{}).RowsAffected == 1
  129. }
  130. func IsUsernameAlreadyTaken(username string) bool {
  131. return DB.Where("username = ?", username).Find(&User{}).RowsAffected == 1
  132. }
  133. func ResetUserPasswordByEmail(email string, password string) error {
  134. hashedPassword, err := common.Password2Hash(password)
  135. if err != nil {
  136. return err
  137. }
  138. err = DB.Model(&User{}).Where("email = ?", email).Update("password", hashedPassword).Error
  139. return err
  140. }