user.go 7.1 KB

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