server_service.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. package ldap
  2. import (
  3. "crypto/md5"
  4. "encoding/hex"
  5. "errors"
  6. "fmt"
  7. "github.com/astaxie/beego/orm"
  8. "nginx-ui/server/config"
  9. "nginx-ui/server/models"
  10. "nginx-ui/server/vo"
  11. )
  12. type ServerService struct {
  13. }
  14. // GetServer 获取一个可用的LDAP 连接, 用于登录时获取服务信息
  15. func (c *ServerService) GetServer() (*models.LdapServer, error) {
  16. o := orm.NewOrm()
  17. server := models.LdapServer{
  18. Active: true,
  19. }
  20. err := o.Read(&server, "Active")
  21. if err != nil {
  22. return nil, err
  23. }
  24. return &server, nil
  25. }
  26. func (c *ServerService) Login(req *LDAPLoginReq) (*models.User, error) {
  27. server := models.LdapServer{Key: req.ServerKey}
  28. o := orm.NewOrm()
  29. err := o.Read(&server, "Key")
  30. if err != nil {
  31. return nil, errors.New("未找到对应的LDAP服务!")
  32. }
  33. user, err := UserIns.Authentication(&server, req.Account, req.Password)
  34. if err != nil {
  35. return nil, err
  36. }
  37. user.Password = ""
  38. return user, nil
  39. }
  40. // GetServers 获取用户所有的LDAP连接
  41. // get /ldap/server
  42. func (c *ServerService) GetServers(current *models.User, req *vo.PageReq) (*vo.PageResp, error) {
  43. o := orm.NewOrm()
  44. req.Ensure()
  45. qs := o.QueryTable(&models.LdapServer{})
  46. if !current.IsAdmin() {
  47. qs = qs.Filter("Uid", current.Account)
  48. }
  49. total, err := qs.Count()
  50. if err != nil {
  51. return nil, err
  52. }
  53. qs.OrderBy("Id")
  54. qs.Offset(req.Offset)
  55. qs.Limit(req.PageSize)
  56. var list []*models.LdapServer
  57. _, err = qs.All(&list)
  58. for _, v := range list {
  59. v.Password = config.ReplacePassword
  60. }
  61. if err != nil {
  62. return nil, err
  63. }
  64. resp := vo.PageResp{
  65. Current: req.Current,
  66. PageSize: req.PageSize,
  67. Total: total,
  68. List: list,
  69. }
  70. return &resp, err
  71. }
  72. // Update 保存或者修改
  73. // post /ldap/server
  74. func (c *ServerService) Update(current *models.User, body *models.LdapServer) (*models.LdapServer, error) {
  75. body.Uid = string(rune(current.Id))
  76. if body.Url == "" {
  77. return nil, errors.New("请完成服务配置,缺少Url!")
  78. }
  79. if body.Key == "" {
  80. key := md5.Sum([]byte(body.Url))
  81. body.Key = hex.EncodeToString(key[:])
  82. }
  83. o := orm.NewOrm()
  84. if body.Id == 0 {
  85. exist := models.LdapServer{Key: body.Key}
  86. err := o.Read(&exist, "Key")
  87. if err != nil && !errors.Is(err, orm.ErrNoRows) {
  88. return nil, err
  89. }
  90. if exist.Id > 0 {
  91. return nil, errors.New("该服务Url已存在!")
  92. }
  93. }
  94. if body.Id > 0 {
  95. exist := models.LdapServer{Id: body.Id}
  96. err := o.Read(&exist, "Id")
  97. if err != nil {
  98. return nil, err
  99. }
  100. if config.ReplacePassword == body.Password {
  101. body.Password = exist.Password
  102. }
  103. _, err = o.Update(body)
  104. if err != nil {
  105. return nil, err
  106. }
  107. } else {
  108. id, err := o.Insert(body)
  109. if err != nil {
  110. return nil, err
  111. }
  112. body.Id = int(id)
  113. }
  114. CloseActiveClient(body)
  115. return body, nil
  116. }
  117. // Add Update 保存或者修改
  118. func (c *ServerService) Add(current *models.User, body *models.LdapServer) (*models.LdapServer, error) {
  119. if body.Url == "" {
  120. return nil, errors.New("请完成服务配置,缺少Url!")
  121. }
  122. if body.Key == "" {
  123. key := md5.Sum([]byte(body.Url))
  124. body.Key = hex.EncodeToString(key[:])
  125. }
  126. o := orm.NewOrm()
  127. if body.Id == 0 {
  128. exist := models.LdapServer{Key: body.Key}
  129. err := o.Read(&exist, "Key")
  130. if err != nil && !errors.Is(err, orm.ErrNoRows) {
  131. return nil, err
  132. }
  133. if exist.Id > 0 {
  134. return nil, errors.New("该服务Url已存在!")
  135. }
  136. }
  137. if body.Id > 0 {
  138. exist := models.LdapServer{Id: body.Id}
  139. err := o.Read(&exist, "Id")
  140. if err != nil {
  141. return nil, err
  142. }
  143. if config.ReplacePassword == body.Password {
  144. body.Password = exist.Password
  145. }
  146. _, err = o.Update(body)
  147. if err != nil {
  148. return nil, err
  149. }
  150. } else {
  151. id, err := o.Insert(body)
  152. if err != nil {
  153. return nil, err
  154. }
  155. body.Id = int(id)
  156. }
  157. return body, nil
  158. }
  159. // VerifyServer 验证服务
  160. func (c *ServerService) VerifyServer(req *VerifyReq) ([]*models.LdapUser, error) {
  161. var server = &models.LdapServer{
  162. Id: req.Id,
  163. }
  164. o := orm.NewOrm()
  165. err := o.Read(server, "Id")
  166. if err != nil {
  167. return nil, err
  168. }
  169. if req.Filter == "" && req.Username != "" {
  170. req.Filter = fmt.Sprintf("(&(objectClass=*)(uid=%s))", req.Username)
  171. }
  172. users, _, err := UserIns.Search(server, req.Filter)
  173. if err != nil {
  174. return nil, err
  175. }
  176. return users, nil
  177. }
  178. // GetUsers 获取全部用户
  179. // get /ldap/users
  180. func (c *ServerService) GetUsers(current *models.User, req *UserListReq) (*vo.PageResp, error) {
  181. req.Ensure()
  182. o := orm.NewOrm()
  183. qs := o.QueryTable(&models.LdapUser{})
  184. if !current.IsAdmin() {
  185. qs = qs.Filter("Uid", current.Account)
  186. }
  187. qs.Filter("ServerKey", req.ServerKey)
  188. total, err := qs.Count()
  189. if err != nil {
  190. return nil, err
  191. }
  192. qs.OrderBy("Id")
  193. qs.Offset(req.Offset)
  194. qs.Limit(req.PageSize)
  195. var list []*models.LdapUser
  196. _, err = qs.All(&list)
  197. if err != nil {
  198. return nil, err
  199. }
  200. resp := vo.PageResp{
  201. Current: req.Current,
  202. PageSize: req.PageSize,
  203. Total: total,
  204. List: list,
  205. }
  206. return &resp, nil
  207. }
  208. // GetServer 根据Key获取服信息
  209. func GetServer(key string) (*models.LdapServer, error) {
  210. server := models.LdapServer{
  211. Key: key,
  212. }
  213. o := orm.NewOrm()
  214. err := o.Read(&server, "Key")
  215. if err != nil {
  216. return nil, err
  217. }
  218. return &server, nil
  219. }