Add_Directory_and_ProfilePicture #9
2 changed files with 31 additions and 23 deletions
32
directory.go
32
directory.go
|
@ -34,6 +34,10 @@ type Results struct {
|
||||||
MessageID uint32 `json:"id"`
|
MessageID uint32 `json:"id"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type UniqueID struct {
|
||||||
|
Id int `json:"id"`
|
||||||
|
}
|
||||||
|
|
||||||
func handleSearch(w http.ResponseWriter, r *http.Request) {
|
func handleSearch(w http.ResponseWriter, r *http.Request) {
|
||||||
//Get input value by user
|
//Get input value by user
|
||||||
input := mux.Vars(r)["input"]
|
input := mux.Vars(r)["input"]
|
||||||
|
@ -43,10 +47,10 @@ func handleSearch(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
session, err := store.Get(r, SESSION_NAME)
|
/**session, err := store.Get(r, SESSION_NAME)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}**/
|
||||||
|
|
||||||
//Search values with ldap and filter
|
//Search values with ldap and filter
|
||||||
searchRequest := ldap.NewSearchRequest(
|
searchRequest := ldap.NewSearchRequest(
|
||||||
|
@ -79,20 +83,26 @@ func handleSearch(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
if result.Search == nil {
|
||||||
|
result = Results{
|
||||||
|
Search: append(result.Search, SearchResult{
|
||||||
|
Identifiant: "",
|
||||||
|
Name: "",
|
||||||
|
Email: "",
|
||||||
|
Description: "",
|
||||||
|
DN: "",
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//Convert interface to uint32 with Type Assertions and not a simple convert for messageID
|
var id UniqueID
|
||||||
if val_Raw, ok_raw := session.Values["MessageID"]; ok_raw {
|
//Decode JSON body
|
||||||
if val, ok := val_Raw.(uint32); ok {
|
err = json.NewDecoder(r.Body).Decode(&id)
|
||||||
val += 1
|
|
||||||
session.Values["MessageID"] = val
|
|
||||||
result.MessageID = val
|
|
||||||
err = session.Save(r, w)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
result.MessageID = uint32(id.Id)
|
||||||
}
|
|
||||||
|
|
||||||
//Send JSON through xhttp
|
//Send JSON through xhttp
|
||||||
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
var perso_id = 0;
|
var perso_id = 0;
|
||||||
|
var last_id = 0;
|
||||||
|
|
||||||
function searchDirectory() {
|
function searchDirectory() {
|
||||||
var input = document.getElementById("search").value;
|
var input = document.getElementById("search").value;
|
||||||
|
@ -10,8 +11,8 @@ function searchDirectory() {
|
||||||
//Response from Request Ajax
|
//Response from Request Ajax
|
||||||
var jsonResponse = JSON.parse(xhttp.responseText);
|
var jsonResponse = JSON.parse(xhttp.responseText);
|
||||||
|
|
||||||
if (perso_id < jsonResponse.id) {
|
if (last_id < jsonResponse.id) {
|
||||||
perso_id = jsonResponse.id
|
last_id = jsonResponse.id
|
||||||
//We get the old table element, we create an new table element then we increment this new table.
|
//We get the old table element, we create an new table element then we increment this new table.
|
||||||
//After the new add, we replace the old table by the new one.
|
//After the new add, we replace the old table by the new one.
|
||||||
var old_table = document.getElementById("users");
|
var old_table = document.getElementById("users");
|
||||||
|
@ -29,19 +30,16 @@ function searchDirectory() {
|
||||||
identifiant.innerHTML = `<a href="/admin/ldap/${jsonResponse.search[i].dn}">${jsonResponse.search[i].identifiant}</a>`
|
identifiant.innerHTML = `<a href="/admin/ldap/${jsonResponse.search[i].dn}">${jsonResponse.search[i].identifiant}</a>`
|
||||||
name.innerHTML = jsonResponse.search[i].name
|
name.innerHTML = jsonResponse.search[i].name
|
||||||
email.innerHTML = jsonResponse.search[i].email
|
email.innerHTML = jsonResponse.search[i].email
|
||||||
description.innerHTML = `${jsonResponse.search[i].description}`
|
description.innerHTML = jsonResponse.search[i].description
|
||||||
|
|
||||||
}
|
}
|
||||||
old_table.parentNode.replaceChild(table, old_table)
|
old_table.parentNode.replaceChild(table, old_table)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
perso_id += 1
|
||||||
xhttp.overrideMimeType("application/json");
|
xhttp.overrideMimeType("application/json");
|
||||||
xhttp.open("GET", "/search/".concat(input), true);
|
xhttp.open("POST", "/search/".concat(input), true);
|
||||||
xhttp.send();
|
xhttp.send(JSON.stringify({"id": perso_id}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in a new issue