123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- 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()
- }
- }
|