Skip to content

Commit

Permalink
Merge pull request #61 from nabbar/ldap_group_info
Browse files Browse the repository at this point in the history
# Add feature in Package LDAP : 
- add function to retrieve group information

# Change in Package LDAP : 
- fix config validator for port : int to number
- fix config validator for uri : url to fqdn
  • Loading branch information
Nicolas JUHEL authored Sep 22, 2020
2 parents 66c9a32 + 9a8ecd5 commit 09648d4
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
3 changes: 3 additions & 0 deletions ldap/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const (
ErrorLDAPAttributeNotFound
ErrorLDAPAttributeEmpty
ErrorLDAPValidatorError
ErrorLDAPGroupNotFound
)

var isCodeError = false
Expand Down Expand Up @@ -97,6 +98,8 @@ func getMessage(code errors.CodeError) (message string) {
return "requested attribute is empty"
case ErrorLDAPValidatorError:
return "invalid validation config"
case ErrorLDAPGroupNotFound:
return "group not found"
}

return ""
Expand Down
28 changes: 28 additions & 0 deletions ldap/ldap.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,34 @@ func (lc *HelperLDAP) UserInfo(username string) (map[string]string, errors.Error
return userRes, nil
}

//GroupInfo used to retrieve the information of a given group cn.
func (lc *HelperLDAP) GroupInfo(groupname string) (map[string]interface{}, errors.Error) {
var (
err errors.Error
src *ldap.SearchResult
grpInfo map[string]interface{}
)

src, err = lc.runSearch(fmt.Sprintf(lc.config.FilterGroup, groupname), []string{})
if err != nil {
return grpInfo, err
}

if len(src.Entries) == 0 {
return nil, ErrorLDAPGroupNotFound.Error(nil)
}

grpInfo = make(map[string]interface{}, len(src.Entries[0].Attributes))
for _, entry := range src.Entries {
for _, entryAttribute := range entry.Attributes {
grpInfo[entryAttribute.Name] = entryAttribute.Values
}
}

logger.DebugLevel.Logf("Info for group [%s] find on server '%s' with tls mode '%s' : %v", groupname, lc.config.ServerAddr(lc.tlsMode == TLSMODE_TLS), lc.tlsMode.String(), grpInfo)
return grpInfo, nil
}

//UserMemberOf returns the group list of a given user.
func (lc *HelperLDAP) UserMemberOf(username string) ([]string, errors.Error) {
var (
Expand Down
6 changes: 3 additions & 3 deletions ldap/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ func GetDefaultAttributes() []string {
}

type Config struct {
Uri string `cloud:"uri" mapstructure:"uri" json:"uri" yaml:"uri" toml:"uri" validate:"url,required"`
PortLdap int `cloud:"port-ldap" mapstructure:"port-ldap" json:"port-ldap" yaml:"port-ldap" toml:"port-ldap" validate:"int,gte=0,nefield=Portldaps,required"`
Portldaps int `cloud:"port-ldaps" mapstructure:"port-ldaps" json:"port-ldaps" yaml:"port-ldaps" toml:"port-ldaps" validate:"int,nefield=Portldap,omitempty"`
Uri string `cloud:"uri" mapstructure:"uri" json:"uri" yaml:"uri" toml:"uri" validate:"fqdn,required"`
PortLdap int `cloud:"port-ldap" mapstructure:"port-ldap" json:"port-ldap" yaml:"port-ldap" toml:"port-ldap" validate:"number,gte=0,nefield=Portldaps,required"`
Portldaps int `cloud:"port-ldaps" mapstructure:"port-ldaps" json:"port-ldaps" yaml:"port-ldaps" toml:"port-ldaps" validate:"number,nefield=Portldap,omitempty"`
Basedn string `cloud:"basedn" mapstructure:"basedn" json:"basedn" yaml:"basedn" toml:"basedn" validate:"printascii,omitempty"`
FilterGroup string `cloud:"filter-group" mapstructure:"filter-group" json:"filter-group" yaml:"filter-group" toml:"filter-group" validate:"printascii,required"`
FilterUser string `cloud:"filter-user" mapstructure:"filter-user" json:"filter-user" yaml:"filter-user" toml:"filter-user" validate:"printascii,required"`
Expand Down

0 comments on commit 09648d4

Please sign in to comment.