service.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. package user
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "github.com/astaxie/beego/logs"
  6. "github.com/astaxie/beego/orm"
  7. "github.com/hashicorp/go-uuid"
  8. "nginx-ui/server/config"
  9. "nginx-ui/server/models"
  10. "nginx-ui/server/modules/ldap"
  11. "nginx-ui/server/utils"
  12. "nginx-ui/server/vo"
  13. "strings"
  14. )
  15. type Service struct {
  16. }
  17. func NewUserService() *Service {
  18. return &Service{}
  19. }
  20. func (u *Service) Login(user *models.User) *models.RespData {
  21. cipherPassword := user.Password
  22. o := orm.NewOrm()
  23. err := o.Read(user, "Account")
  24. if err != nil {
  25. return models.NewErrorResp(err)
  26. }
  27. encryptPassword := utils.GetSHA256HashCode(cipherPassword)
  28. if encryptPassword != user.Password {
  29. return models.ErrorResp("用户名或者密码不正确!")
  30. }
  31. user.Password = ""
  32. return models.SuccessResp(user)
  33. }
  34. func (u *Service) SignUp(req []byte) *models.RespData {
  35. var user models.User
  36. err := json.Unmarshal(req, &user)
  37. if err != nil {
  38. logs.Error(err, req)
  39. return models.NewErrorResp(err)
  40. }
  41. if len(user.Account) == 0 || len(user.Password) == 0 {
  42. return models.ErrorResp("账号或者密码不能为空!")
  43. }
  44. if len(user.Nickname) == 0 {
  45. user.Nickname = user.Account
  46. }
  47. user.Password = utils.GetSHA256HashCode(user.Password)
  48. o := orm.NewOrm()
  49. _, err = o.Insert(&user)
  50. if err != nil {
  51. return models.NewErrorResp(err)
  52. }
  53. return models.SuccessResp(user).SetMsg("注册成功!")
  54. }
  55. func (u *Service) Users(req *vo.PageReq) (*vo.PageResp, error) {
  56. req.Ensure()
  57. qs := orm.NewOrm().QueryTable(new(models.User))
  58. qs = qs.Offset(req.Offset).Limit(req.PageSize).OrderBy("-Id")
  59. var list []models.User
  60. _, err := qs.All(&list)
  61. if err != nil {
  62. return nil, err
  63. }
  64. count, err := qs.Count()
  65. if err != nil {
  66. return nil, err
  67. }
  68. var resList []models.User
  69. for _, user := range list {
  70. user.Password = config.ReplacePassword
  71. resList = append(resList, user)
  72. }
  73. resp := vo.PageResp{
  74. PageSize: req.PageSize,
  75. Current: req.Current,
  76. Total: count,
  77. List: resList,
  78. }
  79. return &resp, err
  80. }
  81. func (u *Service) Update(req *models.User) (*models.User, error) {
  82. o := orm.NewOrm()
  83. exist := models.User{Id: req.Id}
  84. err := o.Read(&exist)
  85. if err != nil && !errors.Is(err, orm.ErrNoRows) {
  86. return nil, err
  87. } else if err != nil {
  88. req.Password = utils.GetSHA256HashCode(req.Password)
  89. _, err = o.Insert(req)
  90. if err != nil {
  91. return nil, err
  92. }
  93. return req, nil
  94. }
  95. if req.Password == "" || req.Password == config.ReplacePassword {
  96. req.Password = exist.Password
  97. } else {
  98. req.Password = utils.GetSHA256HashCode(req.Password)
  99. }
  100. _, err = o.Update(req)
  101. if err != nil {
  102. return nil, errors.New("更新失败,请重试!")
  103. }
  104. return req, nil
  105. }
  106. func (u *Service) GetDetail(id int) (*models.User, error) {
  107. o := orm.NewOrm()
  108. exist := models.User{Id: id}
  109. err := o.Read(&exist)
  110. if err != nil {
  111. return nil, errors.New("该用户不存在或者已被删除!")
  112. }
  113. exist.Password = config.ReplacePassword
  114. return &exist, nil
  115. }
  116. // UserUpdatePassword 更新用户密码,如果存在LDAP账号,则更新下, force: 强制更改,不需要确认原密码
  117. func (u *Service) UserUpdatePassword(req *vo.UserUpdatePassword, force bool) error {
  118. o := orm.NewOrm()
  119. user := models.User{Id: req.Id}
  120. err := o.Read(&user)
  121. if err != nil {
  122. return errors.New("该用户不存在或者已被删除!")
  123. }
  124. if user.Source == "LDAP" {
  125. err = ldap.UserIns.UpdateUserPassword(&ldap.UpdatePasswordReq{
  126. Password: req.NewPassword,
  127. Account: user.Account,
  128. OldPassword: req.OldPassword,
  129. }, force)
  130. if err != nil {
  131. return err
  132. }
  133. user.Password = utils.GetSHA256HashCode(req.NewPassword)
  134. _, err = o.Update(&user)
  135. if err != nil {
  136. logs.Error("update password error: %v", err)
  137. }
  138. } else {
  139. if !force && utils.GetSHA256HashCode(req.OldPassword) != user.Password {
  140. return errors.New("当前密码不正确!")
  141. }
  142. user.Password = utils.GetSHA256HashCode(req.NewPassword)
  143. _, err = o.Update(&user)
  144. if err != nil {
  145. return err
  146. }
  147. err = ldap.UserIns.UpdateUserPassword(&ldap.UpdatePasswordReq{
  148. Password: req.NewPassword,
  149. Account: user.Account,
  150. }, true)
  151. if err != nil {
  152. logs.Error("LDAP updatePassword fail: %v", err)
  153. return errors.New("密码更新成功,但同步LDAP失败:" + err.Error())
  154. }
  155. }
  156. return nil
  157. }
  158. // UpdatePassword 更新用户密码,如果存在LDAP账号,则更新下
  159. func (u *Service) UpdatePassword(req *vo.UserUpdatePassword) error {
  160. return u.UserUpdatePassword(req, false)
  161. }
  162. // ResetPassword 用户忘记密码,重置密码
  163. func (u *Service) ResetPassword(req *ResetPasswordReq) error {
  164. o := orm.NewOrm()
  165. user := models.User{
  166. Account: req.Account,
  167. }
  168. err := o.Read(&user, "Account")
  169. if err != nil && errors.Is(err, orm.ErrNoRows) {
  170. ldapUser, err := ldap.UserIns.GetByAccount(req.Account)
  171. if err != nil {
  172. return errors.New("你输入的账号不正确!")
  173. }
  174. ldap.CreateLocalUser(&user, ldapUser)
  175. _, err = o.Insert(&user)
  176. if err != nil {
  177. return errors.New("你输入的账号不正确!")
  178. }
  179. } else if err != nil {
  180. logs.Error("read user fail: %v", err)
  181. return errors.New("你输入的账号不正确!")
  182. }
  183. if user.Email == "" && user.Source == "LDAP" {
  184. ldapUser, err := ldap.UserIns.GetByAccount(req.Account)
  185. if err != nil {
  186. logs.Error("get user fail: %v", err)
  187. return errors.New("你输入账号不正确或者未绑定邮箱!")
  188. }
  189. user.Email = ldapUser.Mail
  190. }
  191. if user.Email == "" {
  192. return errors.New("你输入账号不正确或者未绑定邮箱!")
  193. }
  194. if req.AuthCode != "" {
  195. if req.Password == "" {
  196. return errors.New("密码不能为空!")
  197. }
  198. if req.AuthCode == user.TempCode {
  199. // 重置密码
  200. err = u.UserUpdatePassword(&vo.UserUpdatePassword{
  201. Id: user.Id,
  202. NewPassword: req.Password,
  203. OldPassword: req.Password,
  204. }, true)
  205. if err == nil {
  206. user.TempCode = ""
  207. }
  208. } else {
  209. return errors.New("密码重置链接已过期,请重试!")
  210. }
  211. } else {
  212. authCode, _ := uuid.GenerateUUID()
  213. authCode = strings.ReplaceAll(authCode, "-", "")
  214. user.TempCode = authCode
  215. err = SendUserResetPassEmail(&user, authCode)
  216. }
  217. if err != nil {
  218. return err
  219. }
  220. _, _ = o.Update(&user)
  221. return nil
  222. }