utils.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package ldap
  2. import (
  3. "encoding/json"
  4. "github.com/astaxie/beego/logs"
  5. "github.com/go-ldap/ldap/v3"
  6. "nginx-ui/server/models"
  7. "strings"
  8. "time"
  9. )
  10. func modifyLDAPUser(user *models.LdapUser, entry *ldap.Entry) {
  11. user.LastSyncDate = time.Now()
  12. user.Password = entry.GetAttributeValue("userPassword")
  13. user.UserName = entry.GetAttributeValue("cn")
  14. user.Mail = entry.GetAttributeValue("mail")
  15. user.DN = entry.DN
  16. var organizeList []string
  17. items := strings.Split(user.DN, ",")
  18. for _, item := range items {
  19. if !strings.HasPrefix(item, "cn=") {
  20. organizeList = append(organizeList, item)
  21. }
  22. }
  23. user.Organize = strings.Join(organizeList, ",")
  24. }
  25. func createUser(entry *ldap.Entry) models.LdapUser {
  26. user := models.LdapUser{
  27. Account: entry.GetAttributeValue("uid"),
  28. }
  29. modifyLDAPUser(&user, entry)
  30. jsonBytes, err := json.Marshal(entry.Attributes)
  31. if err != nil {
  32. logs.Error("marshal fail : %v", err)
  33. user.Remark = "attributes marshal fail: " + err.Error()
  34. } else {
  35. user.Attributes = string(jsonBytes)
  36. }
  37. return user
  38. }
  39. func CreateLocalUser(user *models.User, from *models.LdapUser) {
  40. user.Password = from.Password
  41. user.Account = from.Account
  42. user.Nickname = from.UserName
  43. user.Email = from.Mail
  44. user.Source = "LDAP"
  45. }