2021-07-21 19:21:46 +00:00
|
|
|
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"`
|
2021-07-26 21:01:48 +00:00
|
|
|
Description string `json:"description"`
|
2021-07-21 19:21:46 +00:00
|
|
|
DN string `json:"dn"`
|
|
|
|
}
|
|
|
|
|
2021-07-26 14:54:51 +00:00
|
|
|
type Results struct {
|
|
|
|
Search []SearchResult `json:"search"`
|
|
|
|
MessageID uint32 `json:"id"`
|
|
|
|
}
|
2021-07-21 19:21:46 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-07-26 14:54:51 +00:00
|
|
|
session, err := store.Get(r, SESSION_NAME)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-07-26 21:01:48 +00:00
|
|
|
//Search values with ldap and filter
|
2021-07-21 19:21:46 +00:00
|
|
|
searchRequest := ldap.NewSearchRequest(
|
|
|
|
config.UserBaseDN,
|
|
|
|
ldap.ScopeSingleLevel, ldap.NeverDerefAliases, 0, 0, false,
|
2021-07-26 21:01:48 +00:00
|
|
|
"(&(objectclass=organizationalPerson)(visibility=on))",
|
|
|
|
[]string{config.UserNameAttr, "displayname", "mail", "description"},
|
2021-07-21 19:21:46 +00:00
|
|
|
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) {
|
2021-07-26 14:54:51 +00:00
|
|
|
result = Results{
|
|
|
|
Search: append(result.Search, SearchResult{
|
|
|
|
Identifiant: values.GetAttributeValue("cn"),
|
|
|
|
Name: values.GetAttributeValue("displayname"),
|
|
|
|
Email: values.GetAttributeValue("email"),
|
2021-07-26 21:01:48 +00:00
|
|
|
Description: values.GetAttributeValue("description"),
|
2021-07-26 14:54:51 +00:00
|
|
|
DN: values.DN,
|
|
|
|
}),
|
|
|
|
}
|
2021-07-21 19:21:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-07-26 21:01:48 +00:00
|
|
|
//Convert interface to uint32 with Type Assertions and not a simple convert for messageID
|
2021-07-26 14:54:51 +00:00
|
|
|
if val_Raw, ok_raw := session.Values["MessageID"]; ok_raw {
|
|
|
|
if val, ok := val_Raw.(uint32); ok {
|
|
|
|
val += 1
|
|
|
|
session.Values["MessageID"] = val
|
|
|
|
result.MessageID = val
|
|
|
|
err = session.Save(r, w)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-21 19:21:46 +00:00
|
|
|
//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)
|
|
|
|
}
|
|
|
|
}
|