package ldap import ( "errors" "github.com/astaxie/beego/orm" "nginx-ui/server/base" "nginx-ui/server/config" "nginx-ui/server/models" "nginx-ui/server/vo" ) type ServerController struct { base.Controller } var ServiceInstance = new(Service) // GetServer 获取一个可用的LDAP 连接, 用于登录时获取服务信息 func (c *ServerController) GetServer() { server, err := ServiceInstance.GetServer() if err != nil { c.ErrorJson(errors.New("no server")) return } resp := make(map[string]interface{}) resp["server"] = server.Key c.SetData(resp).Json() } // GetServerDetail 获取用户所有的LDAP连接 // get /ldap/server func (c *ServerController) GetServerDetail() { current := c.RequiredUser() if current == nil { return } id, err := c.GetIntQuery("id") if err != nil { c.ErrorJson(err) return } server := models.LdapServer{ Id: id, } o := orm.NewOrm() err = o.Read(&server) if err != nil { c.ErrorJson(err) return } server.Password = config.ReplacePassword c.SetData(server).Json() } // GetServers 获取用户所有的LDAP连接 // get /ldap/server func (c *ServerController) GetServers() { current := c.RequiredUser() if current == nil { return } req := vo.PageReq{} if !c.ReadBody(&req) { return } resp, err := ServiceInstance.GetServers(current, &req) if err != nil { c.ErrorJson(err) return } c.SetData(resp).Json() } // Update 保存或者修改 // post /ldap/server func (c *ServerController) Update() { if current := c.RequiredUser(); current != nil { var body = models.LdapServer{} if !c.ReadBody(&body) { return } user, err := ServiceInstance.Update(current, &body) if err != nil { c.ErrorJson(err) return } c.SetData(user).Json() } } // Verify 保存或者修改 // post /ldap/server/verify func (c *ServerController) Verify() { if current := c.RequiredUser(); current != nil { var body = VerifyReq{} if !c.ReadBody(&body) { return } list, err := ServiceInstance.VerifyServer(&body) if err != nil { c.ErrorJson(err) return } for _, user := range list { user.Password = config.ReplacePassword } if body.Active { o := orm.NewOrm() o.Begin() _, err := o.Raw("UPDATE `ldap_server` SET `active` = 0 WHERE `active` = 1").Exec() if err != nil { o.Rollback() c.ErrorJson(err) return } _, err = o.Update(&models.LdapServer{Id: body.Id, Active: true}, "Active") if err != nil { o.Rollback() c.ErrorJson(err) return } err = o.Commit() } c.SetData(&list).Json() } }