server_controller.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package ldap
  2. import (
  3. "errors"
  4. "github.com/astaxie/beego/orm"
  5. "nginx-ui/server/base"
  6. "nginx-ui/server/config"
  7. "nginx-ui/server/models"
  8. "nginx-ui/server/vo"
  9. )
  10. type ServerController struct {
  11. base.Controller
  12. }
  13. var ServiceInstance = new(Service)
  14. // GetServer 获取一个可用的LDAP 连接, 用于登录时获取服务信息
  15. func (c *ServerController) GetServer() {
  16. server, err := ServiceInstance.GetServer()
  17. if err != nil {
  18. c.ErrorJson(errors.New("no server"))
  19. return
  20. }
  21. resp := make(map[string]interface{})
  22. resp["server"] = server.Key
  23. c.SetData(resp).Json()
  24. }
  25. // GetServerDetail 获取用户所有的LDAP连接
  26. // get /ldap/server
  27. func (c *ServerController) GetServerDetail() {
  28. current := c.RequiredUser()
  29. if current == nil {
  30. return
  31. }
  32. id, err := c.GetIntQuery("id")
  33. if err != nil {
  34. c.ErrorJson(err)
  35. return
  36. }
  37. server := models.LdapServer{
  38. Id: id,
  39. }
  40. o := orm.NewOrm()
  41. err = o.Read(&server)
  42. if err != nil {
  43. c.ErrorJson(err)
  44. return
  45. }
  46. server.Password = config.ReplacePassword
  47. c.SetData(server).Json()
  48. }
  49. // GetServers 获取用户所有的LDAP连接
  50. // get /ldap/server
  51. func (c *ServerController) GetServers() {
  52. current := c.RequiredUser()
  53. if current == nil {
  54. return
  55. }
  56. req := vo.PageReq{}
  57. if !c.ReadBody(&req) {
  58. return
  59. }
  60. resp, err := ServiceInstance.GetServers(current, &req)
  61. if err != nil {
  62. c.ErrorJson(err)
  63. return
  64. }
  65. c.SetData(resp).Json()
  66. }
  67. // Update 保存或者修改
  68. // post /ldap/server
  69. func (c *ServerController) Update() {
  70. if current := c.RequiredUser(); current != nil {
  71. var body = models.LdapServer{}
  72. if !c.ReadBody(&body) {
  73. return
  74. }
  75. user, err := ServiceInstance.Update(current, &body)
  76. if err != nil {
  77. c.ErrorJson(err)
  78. return
  79. }
  80. c.SetData(user).Json()
  81. }
  82. }
  83. // Verify 保存或者修改
  84. // post /ldap/server/verify
  85. func (c *ServerController) Verify() {
  86. if current := c.RequiredUser(); current != nil {
  87. var body = VerifyReq{}
  88. if !c.ReadBody(&body) {
  89. return
  90. }
  91. list, err := ServiceInstance.VerifyServer(&body)
  92. if err != nil {
  93. c.ErrorJson(err)
  94. return
  95. }
  96. for _, user := range list {
  97. user.Password = config.ReplacePassword
  98. }
  99. if body.Active {
  100. o := orm.NewOrm()
  101. o.Begin()
  102. _, err := o.Raw("UPDATE `ldap_server` SET `active` = 0 WHERE `active` = 1").Exec()
  103. if err != nil {
  104. o.Rollback()
  105. c.ErrorJson(err)
  106. return
  107. }
  108. _, err = o.Update(&models.LdapServer{Id: body.Id, Active: true}, "Active")
  109. if err != nil {
  110. o.Rollback()
  111. c.ErrorJson(err)
  112. return
  113. }
  114. err = o.Commit()
  115. }
  116. c.SetData(&list).Json()
  117. }
  118. }