diff --git a/api/ldap/ldap.go b/api/ldap/ldap.go index 523c672cd..e49d7e29b 100644 --- a/api/ldap/ldap.go +++ b/api/ldap/ldap.go @@ -165,6 +165,7 @@ func (*Service) SearchUsers(settings *portainer.LDAPSettings) ([]string, error) // SearchGroups searches for groups with the specified settings func (*Service) SearchGroups(settings *portainer.LDAPSettings) ([]portainer.LDAPUser, error) { + type groupSet map[string]bool connection, err := createConnection(settings) if err != nil { @@ -179,7 +180,7 @@ func (*Service) SearchGroups(settings *portainer.LDAPSettings) ([]portainer.LDAP } } - users := []portainer.LDAPUser{} + userGroups := map[string]groupSet{} for _, searchSettings := range settings.GroupSearchSettings { searchRequest := ldap.NewSearchRequest( @@ -190,25 +191,37 @@ func (*Service) SearchGroups(settings *portainer.LDAPSettings) ([]portainer.LDAP nil, ) - // Deliberately skip errors on the search request so that we can jump to other search settings - // if any issue arise with the current one. sr, err := connection.Search(searchRequest) if err != nil { - return users, err + return nil, err } for _, entry := range sr.Entries { members := entry.GetAttributeValues(searchSettings.GroupAttribute) for _, username := range members { - user := portainer.LDAPUser{ - Name: username, - Group: entry.GetAttributeValue("cn"), + _, ok := userGroups[username] + if !ok { + userGroups[username] = groupSet{} } - users = append(users, user) + userGroups[username][entry.GetAttributeValue("cn")] = true } } } + users := []portainer.LDAPUser{} + + for username, groups := range userGroups { + groupList := []string{} + for group := range groups { + groupList = append(groupList, group) + } + user := portainer.LDAPUser{ + Name: username, + Groups: groupList, + } + users = append(users, user) + } + return users, nil } diff --git a/api/portainer.go b/api/portainer.go index e2eca0d1a..515817d37 100644 --- a/api/portainer.go +++ b/api/portainer.go @@ -422,8 +422,8 @@ type ( // LDAPUser represents a LDAP user LDAPUser struct { - Name string - Group string + Name string + Groups []string } // LicenseInfo represents aggregated information about an instance license diff --git a/app/portainer/settings/authentication/ldap/ldap-custom-group-search/ldap-custom-group-search.html b/app/portainer/settings/authentication/ldap/ldap-custom-group-search/ldap-custom-group-search.html index 4473456d8..2423aa316 100644 --- a/app/portainer/settings/authentication/ldap/ldap-custom-group-search/ldap-custom-group-search.html +++ b/app/portainer/settings/authentication/ldap/ldap-custom-group-search/ldap-custom-group-search.html @@ -52,7 +52,7 @@
{{ group }}