Skip to content

Commit

Permalink
Add search example to LDAP client code
Browse files Browse the repository at this point in the history
  • Loading branch information
mertyildiran committed Dec 7, 2024
1 parent adc05b3 commit ae31de7
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions additions/ldap-client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,44 @@ func main() {
}
log.Printf("User %s modified successfully.\n", randomUsername)

// Search the user
searchReq := ldap.NewSearchRequest(
userDN,
ldap.ScopeWholeSubtree,
0,
0,
0,
false,
fmt.Sprintf("(CN=%s)", ldap.EscapeFilter(randomUsername)),
[]string{
"sAMAccountName",
"objectClass",
"sn",
"userPassword",
},
[]ldap.Control{},
)
searchResult, err := conn.Search(searchReq)
if err != nil {
log.Printf("Failed to search the user %s: %v\n", randomUsername, err)
continue
}
log.Printf(
"User %s search results: %+v\n",
randomUsername,
searchResult.Entries[0],
)
log.Printf(
"User %s searched attributes: %s:%+v %s:%+v %s:%+v\n",
randomUsername,
searchResult.Entries[0].Attributes[0].Name,
searchResult.Entries[0].Attributes[0].Values,
searchResult.Entries[0].Attributes[1].Name,
searchResult.Entries[0].Attributes[1].Values,
searchResult.Entries[0].Attributes[2].Name,
searchResult.Entries[0].Attributes[2].Values,
)

// Delete the user
delRequest := ldap.NewDelRequest(userDN, nil)
err = conn.Del(delRequest)
Expand Down

0 comments on commit ae31de7

Please sign in to comment.