utils.go 974 B

123456789101112131415161718192021222324252627282930313233343536373839
  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. "time"
  8. )
  9. func modifyLDAPUser(user *models.LdapUser, entry *ldap.Entry) {
  10. user.LastSyncDate = time.Now()
  11. user.Password = entry.GetAttributeValue("userPassword")
  12. user.UserName = entry.GetAttributeValue("cn")
  13. user.Mail = entry.GetAttributeValue("mail")
  14. user.DN = entry.DN
  15. }
  16. func createUser(entry *ldap.Entry) models.LdapUser {
  17. user := models.LdapUser{
  18. Account: entry.GetAttributeValue("uid"),
  19. }
  20. modifyLDAPUser(&user, entry)
  21. jsonBytes, err := json.Marshal(entry.Attributes)
  22. if err != nil {
  23. logs.Error("marshal fail : %v", err)
  24. user.Remark = "attributes marshal fail: " + err.Error()
  25. } else {
  26. user.Attributes = string(jsonBytes)
  27. }
  28. return user
  29. }
  30. func createLocalUser(user *models.User, from *models.LdapUser) {
  31. user.Password = from.Password
  32. user.Account = from.Account
  33. user.Nickname = from.UserName
  34. user.Source = "LDAP"
  35. }