service.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package user
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "github.com/astaxie/beego/logs"
  6. "github.com/astaxie/beego/orm"
  7. "nginx-ui/server/config"
  8. "nginx-ui/server/models"
  9. "nginx-ui/server/modules/ldap"
  10. "nginx-ui/server/utils"
  11. "nginx-ui/server/vo"
  12. )
  13. type UserService struct {
  14. }
  15. func NewUserService() *UserService {
  16. return &UserService{}
  17. }
  18. func (u *UserService) Login(user *models.User) *models.RespData {
  19. cipherPassword := user.Password
  20. o := orm.NewOrm()
  21. err := o.Read(user, "Account")
  22. if err != nil {
  23. return models.NewErrorResp(err)
  24. }
  25. encryptPassword := utils.GetSHA256HashCode(cipherPassword)
  26. if encryptPassword != user.Password {
  27. return models.ErrorResp("用户名或者密码不正确!")
  28. }
  29. user.Password = ""
  30. return models.SuccessResp(user)
  31. }
  32. func (u *UserService) SignUp(req []byte) *models.RespData {
  33. var user models.User
  34. err := json.Unmarshal(req, &user)
  35. if err != nil {
  36. logs.Error(err, req)
  37. return models.NewErrorResp(err)
  38. }
  39. if len(user.Account) == 0 || len(user.Password) == 0 {
  40. return models.ErrorResp("账号或者密码不能为空!")
  41. }
  42. if len(user.Nickname) == 0 {
  43. user.Nickname = user.Account
  44. }
  45. user.Password = utils.GetSHA256HashCode(user.Password)
  46. o := orm.NewOrm()
  47. _, err = o.Insert(&user)
  48. if err != nil {
  49. return models.NewErrorResp(err)
  50. }
  51. return models.SuccessResp(user).SetMsg("注册成功!")
  52. }
  53. func (u *UserService) Users(req *vo.PageReq) (*vo.PageResp, error) {
  54. req.Ensure()
  55. qs := orm.NewOrm().QueryTable(new(models.User))
  56. qs = qs.Offset(req.Offset).Limit(req.PageSize).OrderBy("-Id")
  57. var list []models.User
  58. _, err := qs.All(&list)
  59. if err != nil {
  60. return nil, err
  61. }
  62. count, err := qs.Count()
  63. if err != nil {
  64. return nil, err
  65. }
  66. var resList []models.User
  67. for _, user := range list {
  68. user.Password = config.ReplacePassword
  69. resList = append(resList, user)
  70. }
  71. resp := vo.PageResp{
  72. PageSize: req.PageSize,
  73. Current: req.Current,
  74. Total: count,
  75. List: resList,
  76. }
  77. return &resp, err
  78. }
  79. func (u *UserService) Update(req *models.User) (*models.User, error) {
  80. o := orm.NewOrm()
  81. exist := models.User{Id: req.Id}
  82. err := o.Read(&exist)
  83. if err != nil && !errors.Is(err, orm.ErrNoRows) {
  84. return nil, err
  85. } else if err != nil {
  86. req.Password = utils.GetSHA256HashCode(req.Password)
  87. _, err = o.Insert(req)
  88. if err != nil {
  89. return nil, err
  90. }
  91. return req, nil
  92. }
  93. if req.Password == "" || req.Password == config.ReplacePassword {
  94. req.Password = exist.Password
  95. } else {
  96. req.Password = utils.GetSHA256HashCode(req.Password)
  97. }
  98. _, err = o.Update(req)
  99. if err != nil {
  100. return nil, errors.New("更新失败,请重试!")
  101. }
  102. return req, nil
  103. }
  104. func (u *UserService) GetDetail(id int) (*models.User, error) {
  105. o := orm.NewOrm()
  106. exist := models.User{Id: id}
  107. err := o.Read(&exist)
  108. if err != nil {
  109. return nil, errors.New("该用户不存在或者已被删除!")
  110. }
  111. exist.Password = config.ReplacePassword
  112. return &exist, nil
  113. }
  114. // UpdatePassword 更新用户密码,如果存在LDAP账号,则更新下
  115. func (u *UserService) UpdatePassword(req *vo.UserUpdatePassword) error {
  116. o := orm.NewOrm()
  117. user := models.User{Id: req.Id}
  118. err := o.Read(&user)
  119. if err != nil {
  120. return errors.New("该用户不存在或者已被删除!")
  121. }
  122. if user.Source == "LDAP" {
  123. err = ldap.ServiceInstance.UpdateUserPassword(&ldap.UpdatePasswordReq{
  124. Password: req.NewPassword,
  125. Account: user.Account,
  126. OldPassword: req.OldPassword,
  127. }, false)
  128. if err != nil {
  129. return err
  130. }
  131. user.Password = req.NewPassword
  132. _, err = o.Update(&user)
  133. if err != nil {
  134. logs.Error("update password error: %v", err)
  135. }
  136. } else {
  137. if req.OldPassword != user.Password {
  138. return errors.New("当前密码不正确!")
  139. }
  140. user.Password = req.NewPassword
  141. _, err = o.Update(&user)
  142. if err != nil {
  143. return err
  144. }
  145. err = ldap.ServiceInstance.UpdateUserPassword(&ldap.UpdatePasswordReq{
  146. Password: req.NewPassword,
  147. Account: user.Account,
  148. }, true)
  149. if err != nil {
  150. logs.Error("LDAP updatePassword fail: %v", err)
  151. return errors.New("密码更新成功,但同步LDAP失败:" + err.Error())
  152. }
  153. }
  154. return nil
  155. }