From 57081792de330ae53c327dbeeb9689e21d37135a Mon Sep 17 00:00:00 2001 From: Tan Jiang Date: Mon, 25 Apr 2016 19:00:36 +0800 Subject: [PATCH] adapt to other ldap servers --- auth/ldap/ldap.go | 27 +++++++++++---------------- dao/register.go | 12 ++---------- 2 files changed, 13 insertions(+), 26 deletions(-) diff --git a/auth/ldap/ldap.go b/auth/ldap/ldap.go index 53d17a174..8de4d47fb 100644 --- a/auth/ldap/ldap.go +++ b/auth/ldap/ldap.go @@ -76,31 +76,25 @@ func (l *Auth) Authenticate(m models.AuthModel) (*models.User, error) { scope := openldap.LDAP_SCOPE_SUBTREE // LDAP_SCOPE_BASE, LDAP_SCOPE_ONELEVEL, LDAP_SCOPE_SUBTREE filter := "objectClass=*" - attributes := []string{"cn", "mail", "uid"} + attributes := []string{"mail"} result, err := ldap.SearchAll(baseDn, scope, filter, attributes) if err != nil { return nil, err } - if len(result.Entries()) != 1 { - log.Warningf("Found more than one entry.") - return nil, nil - } - en := result.Entries()[0] u := models.User{} - for _, attr := range en.Attributes() { - val := attr.Values()[0] - switch attr.Name() { - case "uid": - u.Username = val - case "mail": - u.Email = val - case "cn": - u.Realname = val + if len(result.Entries()) == 1 { + en := result.Entries()[0] + for _, attr := range en.Attributes() { + val := attr.Values()[0] + if attr.Name() == "mail" { + u.Email = val + } } } - log.Debug("username:", u.Username, ",email:", u.Email, ",realname:", u.Realname) + u.Username = m.Principal + log.Debug("username:", u.Username, ",email:", u.Email) exist, err := dao.UserExists(u, "username") if err != nil { @@ -114,6 +108,7 @@ func (l *Auth) Authenticate(m models.AuthModel) (*models.User, error) { } u.UserID = currentUser.UserID } else { + u.Realname = m.Principal u.Password = "12345678AbC" u.Comment = "registered from LDAP." userID, err := dao.Register(u) diff --git a/dao/register.go b/dao/register.go index f3e66cb1a..95798b380 100644 --- a/dao/register.go +++ b/dao/register.go @@ -17,7 +17,7 @@ package dao import ( "errors" - "regexp" + // "regexp" "time" "github.com/vmware/harbor/models" @@ -74,15 +74,7 @@ func validate(user models.User) error { return errors.New("Username already exists.") } - if m, _ := regexp.MatchString(`^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$`, user.Email); !m { - return errors.New("Email with illegal format.") - } - - if isIllegalLength(user.Email, 0, -1) { - return errors.New("Email cannot empty.") - } - - if exist, _ := UserExists(models.User{Email: user.Email}, "email"); exist { + if exist, _ := UserExists(models.User{Email: user.Email}, "email"); len(user.Email) > 0 && exist { return errors.New("Email already exists.") }