user_controller.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package ldap
  2. import (
  3. "errors"
  4. "nginx-ui/server/base"
  5. "nginx-ui/server/models"
  6. )
  7. type UserController struct {
  8. base.Controller
  9. }
  10. func (c *UserController) Login() {
  11. req := LDAPLoginReq{}
  12. if c.ReadBody(&req) == false {
  13. return
  14. }
  15. user, err := ServerIns.Login(&req)
  16. if err != nil {
  17. c.ErrorJson(err)
  18. return
  19. }
  20. user.Password = ""
  21. c.SetSession("user", *user)
  22. c.PostJson(models.SuccessResp(user))
  23. }
  24. // SyncUsers 同步用户信息
  25. // post /ldap/user/sync
  26. func (c *UserController) SyncUsers() {
  27. current := c.RequiredUser()
  28. if current == nil {
  29. return
  30. }
  31. req := LDAPUserSyncReq{}
  32. if !c.ReadBody(&req) {
  33. return
  34. }
  35. count, err := UserIns.SyncUsers(current, &req)
  36. if err != nil {
  37. c.ErrorJson(err)
  38. return
  39. }
  40. resp := make(map[string]interface{})
  41. resp["count"] = count
  42. c.SetData(resp).Json()
  43. }
  44. // UpdateUserPassword 更新用户密码
  45. // post /ldap/user/modifyPassword
  46. func (c *UserController) UpdateUserPassword() {
  47. }
  48. // SaveUser 新增或者修改用户
  49. func (c *UserController) SaveUser() {
  50. current := c.RequiredUser()
  51. if current == nil {
  52. return
  53. }
  54. user := models.LdapUser{}
  55. if !c.ReadBody(&user) {
  56. return
  57. }
  58. if user.ServerKey == "" {
  59. server, err := ServerIns.GetServer()
  60. if err != nil {
  61. c.ErrorJson(errors.New("请先激活LDAP服务"))
  62. return
  63. }
  64. user.ServerKey = server.Key
  65. }
  66. resp, err := UserIns.Add(&user)
  67. if err != nil {
  68. c.ErrorJson(err)
  69. return
  70. }
  71. c.SetData(resp).Json()
  72. }
  73. // GetUsers 获取全部用户
  74. // get /ldap/users
  75. func (c *UserController) GetUsers() {
  76. current := c.RequiredUser()
  77. if current == nil {
  78. return
  79. }
  80. req := UserListReq{}
  81. if !c.ReadBody(&req) {
  82. return
  83. }
  84. resp, err := ServerIns.GetUsers(current, &req)
  85. if err != nil {
  86. c.ErrorJson(err)
  87. return
  88. }
  89. c.SetData(resp).Json()
  90. }
  91. func (c *UserController) GetDetail() {
  92. current := c.RequiredUser()
  93. if current == nil {
  94. return
  95. }
  96. id, err := c.GetIntQuery("id")
  97. if err != nil {
  98. c.ErrorJson(err)
  99. return
  100. }
  101. resp, err := UserIns.GetDetail(id)
  102. if err != nil {
  103. c.ErrorJson(err)
  104. return
  105. }
  106. c.SetData(resp).Json()
  107. }