server_controller.go 2.4 KB

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