diff options
author | MrArmonius <mrarmonius@gmail.com> | 2021-07-21 21:21:46 +0200 |
---|---|---|
committer | Alex Auvolat <alex@adnab.me> | 2021-08-16 12:54:08 +0200 |
commit | b1ce932cd67be22d73606f377d2432ea389be71f (patch) | |
tree | c438829f56c749b325ab9fdb960a635b725a66bb /directory.go | |
parent | fa5476e963371a6e4b26ee60887f7bc356914728 (diff) | |
download | guichet-b1ce932cd67be22d73606f377d2432ea389be71f.tar.gz guichet-b1ce932cd67be22d73606f377d2432ea389be71f.zip |
Add Route + file Directory.go
Diffstat (limited to 'directory.go')
-rw-r--r-- | directory.go | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/directory.go b/directory.go new file mode 100644 index 0000000..2be0a8b --- /dev/null +++ b/directory.go @@ -0,0 +1,77 @@ +package main + +import ( + "encoding/json" + "html/template" + "net/http" + "strings" + + "github.com/go-ldap/ldap/v3" + "github.com/gorilla/mux" +) + +func handleDirectory(w http.ResponseWriter, r *http.Request) { + templateDirectory := template.Must(template.ParseFiles("templates/layout.html", "templates/directory.html")) + + login := checkLogin(w, r) + if login == nil { + return + } + + templateDirectory.Execute(w, nil) +} + +type SearchResult struct { + Identifiant string `json:"identifiant"` + Name string `json:"name"` + Email string `json:"email"` + DN string `json:"dn"` +} + +type Results []SearchResult + +func handleSearch(w http.ResponseWriter, r *http.Request) { + //Get input value by user + input := mux.Vars(r)["input"] + //Log to allow the research + login := checkLogin(w, r) + if login == nil { + return + } + + //Search value with ldap and filter + searchRequest := ldap.NewSearchRequest( + config.UserBaseDN, + ldap.ScopeSingleLevel, ldap.NeverDerefAliases, 0, 0, false, + "(&(objectclass=organizationalPerson)(visibility=all))", + []string{config.UserNameAttr, "displayname", "mail"}, + nil) + + sr, err := login.conn.Search(searchRequest) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + //Transform the researh's result in a correct struct to send HSON + var result Results + for _, values := range sr.Entries { + + if strings.Contains(values.GetAttributeValue("cn"), input) { + result = append(result, SearchResult{ + Identifiant: values.GetAttributeValue("cn"), + Name: values.GetAttributeValue("displayname"), + Email: values.GetAttributeValue("email"), + DN: values.DN, + }) + } + + } + + //Send JSON through xhttp + w.Header().Set("Content-Type", "application/json; charset=UTF-8") + w.WriteHeader(http.StatusCreated) + if err := json.NewEncoder(w).Encode(result); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + } +} |