organize_service.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package ldap
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/astaxie/beego/logs"
  6. "github.com/astaxie/beego/orm"
  7. "github.com/go-ldap/ldap/v3"
  8. "nginx-ui/server/models"
  9. "strings"
  10. )
  11. type OrganizeService struct {
  12. }
  13. // Add Update 保存或者修改
  14. func (c *OrganizeService) Add(body *models.LdapOrganize) (*models.LdapOrganize, error) {
  15. server, err := GetServer(body.ServerKey)
  16. if err != nil {
  17. return nil, err
  18. }
  19. o := orm.NewOrm()
  20. _, err = models.InsertOrUpdate[models.LdapOrganize](o, body)
  21. if err != nil {
  22. return nil, err
  23. }
  24. if body.Name == "" {
  25. return body, nil
  26. }
  27. client, err := GetActiveClient(server)
  28. if err != nil {
  29. return nil, err
  30. }
  31. entries, err := client.Search(fmt.Sprintf("(&(objectClass=*)(ou=%s))", body.Name))
  32. if err != nil {
  33. logs.Error("search fail: %v", err)
  34. return body, errors.New("LDAP服务搜索失败!")
  35. }
  36. if len(entries) > 0 {
  37. entry := entries[0]
  38. if entry.DN != body.DN {
  39. logs.Warn("DN not match: {}, {}", entry.DN, body.DN)
  40. return body, errors.New("已存在该组织,但DN不相同!")
  41. }
  42. }
  43. var attrs = make([]*ldap.EntryAttribute, 0)
  44. attrs = append(attrs, &ldap.EntryAttribute{
  45. Name: "ou",
  46. Values: []string{body.Name},
  47. })
  48. ocList := strings.Split(body.ObjectClass, ",")
  49. attrs = append(attrs, &ldap.EntryAttribute{
  50. Name: "objectClass",
  51. Values: ocList,
  52. })
  53. isUpdate := len(entries) == 1
  54. if isUpdate {
  55. request := ldap.NewModifyRequest(body.DN, nil)
  56. var attrMap = make(map[string][]string)
  57. for _, attr := range entries[0].Attributes {
  58. attrMap[attr.Name] = attr.Values
  59. }
  60. for _, attr := range attrs {
  61. if attrMap[attr.Name] == nil {
  62. request.Add(attr.Name, attr.Values)
  63. } else {
  64. request.Replace(attr.Name, attr.Values)
  65. }
  66. }
  67. err = client.Conn.Modify(request)
  68. } else {
  69. request := ldap.NewAddRequest(body.DN, nil)
  70. for _, attr := range attrs {
  71. request.Attribute(attr.Name, attr.Values)
  72. }
  73. err = client.Conn.Add(request)
  74. }
  75. if err != nil {
  76. logs.Error("Add fail: %v", err)
  77. }
  78. return body, err
  79. }
  80. // GetDetail 获取详情
  81. func (c *OrganizeService) GetDetail(id int) (*models.LdapOrganize, error) {
  82. o := orm.NewOrm()
  83. user := models.LdapOrganize{Id: id}
  84. err := o.Read(&user, "Id")
  85. if err != nil {
  86. return nil, err
  87. }
  88. return &user, nil
  89. }
  90. // GetList 获取全部组织
  91. func (c *OrganizeService) GetList(serverKey string) ([]models.LdapOrganize, error) {
  92. o := orm.NewOrm()
  93. qs := o.QueryTable(&models.LdapOrganize{})
  94. qs.Filter("ServerKey", serverKey)
  95. qs.OrderBy("-Id")
  96. var list []models.LdapOrganize
  97. _, err := qs.All(&list)
  98. if err != nil {
  99. return nil, err
  100. }
  101. return list, nil
  102. }