Package Ldap:

- Add function to filter multiple LDAP group entries attributes based on user
This commit is contained in:
nabbar
2024-11-19 08:21:49 +01:00
parent 8451eae3be
commit 3cad34d6c9

View File

@@ -460,7 +460,7 @@ func (lc *HelperLDAP) runSearch(filter string, attributes []string) (*ldap.Searc
lc.config.Basedn,
ldap.ScopeWholeSubtree,
ldap.NeverDerefAliases,
100, 0, false,
0, 0, false,
filter,
attributes,
nil,
@@ -547,6 +547,39 @@ func (lc *HelperLDAP) UserInfoByField(username string, fieldOfUnicValue string)
func (lc *HelperLDAP) GroupInfo(groupname string) (map[string]interface{}, liberr.Error) {
return lc.GroupInfoByField(groupname, groupFieldCN)
}
func (lc *HelperLDAP) AttributeFilter(search string,
filter string, attribute string) (map[string][]string,
liberr.Error) {
var (
err liberr.Error
src *ldap.SearchResult
grpInfo map[string][]string
)
src, err = lc.runSearch(fmt.Sprintf("(&(objectClass~=groupOfNames)(%s=%s))", filter, search), []string{})
if err != nil {
return grpInfo, err
}
if len(src.Entries) == 0 {
return nil, ErrorLDAPGroupNotFound.Error(nil)
}
grpInfo = make(map[string][]string, len(src.Entries))
for _, entry := range src.Entries {
for _, entryAttribute := range entry.Attributes {
if entryAttribute.Name == attribute {
grpInfo[entryAttribute.Name] = append(grpInfo[entryAttribute.Name], entryAttribute.Values...)
}
}
}
lc.getLogEntry(loglvl.DebugLevel, "ldap group find success").FieldAdd("ldap.group", search).FieldAdd("ldap.map", grpInfo).Log()
return grpInfo, nil
}
// GroupInfoByField used to retrieve the information of a given group cn, but use a given field to make the search.
func (lc *HelperLDAP) GroupInfoByField(groupname string, fieldForUnicValue string) (map[string]interface{}, liberr.Error) {