user.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package service
  2. import (
  3. "errors"
  4. "x-ui/database"
  5. "x-ui/database/model"
  6. "x-ui/logger"
  7. "gorm.io/gorm"
  8. )
  9. type UserService struct{}
  10. func (s *UserService) GetFirstUser() (*model.User, error) {
  11. db := database.GetDB()
  12. user := &model.User{}
  13. err := db.Model(model.User{}).
  14. First(user).
  15. Error
  16. if err != nil {
  17. return nil, err
  18. }
  19. return user, nil
  20. }
  21. func (s *UserService) CheckUser(username string, password string, secret string) *model.User {
  22. db := database.GetDB()
  23. user := &model.User{}
  24. err := db.Model(model.User{}).
  25. Where("username = ? and password = ? and login_secret = ?", username, password, secret).
  26. First(user).
  27. Error
  28. if err == gorm.ErrRecordNotFound {
  29. return nil
  30. } else if err != nil {
  31. logger.Warning("check user err:", err)
  32. return nil
  33. }
  34. return user
  35. }
  36. func (s *UserService) UpdateUser(id int, username string, password string) error {
  37. db := database.GetDB()
  38. return db.Model(model.User{}).
  39. Where("id = ?", id).
  40. Updates(map[string]interface{}{"username": username, "password": password}).
  41. Error
  42. }
  43. func (s *UserService) UpdateUserSecret(id int, secret string) error {
  44. db := database.GetDB()
  45. return db.Model(model.User{}).
  46. Where("id = ?", id).
  47. Update("login_secret", secret).
  48. Error
  49. }
  50. func (s *UserService) RemoveUserSecret() error {
  51. db := database.GetDB()
  52. return db.Model(model.User{}).
  53. Where("1 = 1").
  54. Update("login_secret", "").
  55. Error
  56. }
  57. func (s *UserService) GetUserSecret(id int) *model.User {
  58. db := database.GetDB()
  59. user := &model.User{}
  60. err := db.Model(model.User{}).
  61. Where("id = ?", id).
  62. First(user).
  63. Error
  64. if err == gorm.ErrRecordNotFound {
  65. return nil
  66. }
  67. return user
  68. }
  69. func (s *UserService) CheckSecretExistence() (bool, error) {
  70. db := database.GetDB()
  71. var count int64
  72. err := db.Model(model.User{}).
  73. Where("login_secret IS NOT NULL").
  74. Count(&count).
  75. Error
  76. if err != nil {
  77. return false, err
  78. }
  79. return count > 0, nil
  80. }
  81. func (s *UserService) UpdateFirstUser(username string, password string) error {
  82. if username == "" {
  83. return errors.New("username can not be empty")
  84. } else if password == "" {
  85. return errors.New("password can not be empty")
  86. }
  87. db := database.GetDB()
  88. user := &model.User{}
  89. err := db.Model(model.User{}).First(user).Error
  90. if database.IsNotFound(err) {
  91. user.Username = username
  92. user.Password = password
  93. return db.Model(model.User{}).Create(user).Error
  94. } else if err != nil {
  95. return err
  96. }
  97. user.Username = username
  98. user.Password = password
  99. return db.Save(user).Error
  100. }