forked from Deuxfleurs/guichet
Add MessageID for asynchronous request
ID is stored in the session with the type `uint32` Correction javascript For the `for loop`, we need don't forget than know we have in format `JSON` the response: `search:{..}, id:0` For the id, don't forget to change the global value of `JS`.
This commit is contained in:
parent
819d7bf02f
commit
cf4918e901
3 changed files with 53 additions and 21 deletions
30
directory.go
30
directory.go
|
@ -28,7 +28,10 @@ type SearchResult struct {
|
||||||
DN string `json:"dn"`
|
DN string `json:"dn"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Results []SearchResult
|
type Results struct {
|
||||||
|
Search []SearchResult `json:"search"`
|
||||||
|
MessageID uint32 `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
|
||||||
|
@ -39,6 +42,11 @@ func handleSearch(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
session, err := store.Get(r, SESSION_NAME)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
//Search value with ldap and filter
|
//Search value with ldap and filter
|
||||||
searchRequest := ldap.NewSearchRequest(
|
searchRequest := ldap.NewSearchRequest(
|
||||||
config.UserBaseDN,
|
config.UserBaseDN,
|
||||||
|
@ -58,16 +66,32 @@ func handleSearch(w http.ResponseWriter, r *http.Request) {
|
||||||
for _, values := range sr.Entries {
|
for _, values := range sr.Entries {
|
||||||
|
|
||||||
if strings.Contains(values.GetAttributeValue("cn"), input) {
|
if strings.Contains(values.GetAttributeValue("cn"), input) {
|
||||||
result = append(result, SearchResult{
|
result = Results{
|
||||||
|
Search: append(result.Search, SearchResult{
|
||||||
Identifiant: values.GetAttributeValue("cn"),
|
Identifiant: values.GetAttributeValue("cn"),
|
||||||
Name: values.GetAttributeValue("displayname"),
|
Name: values.GetAttributeValue("displayname"),
|
||||||
Email: values.GetAttributeValue("email"),
|
Email: values.GetAttributeValue("email"),
|
||||||
DN: values.DN,
|
DN: values.DN,
|
||||||
})
|
}),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Convert interface to uint32 with Type Assertions and not a simple convert
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//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")
|
||||||
w.WriteHeader(http.StatusCreated)
|
w.WriteHeader(http.StatusCreated)
|
||||||
|
|
3
main.go
3
main.go
|
@ -392,6 +392,9 @@ func handleLogin(w http.ResponseWriter, r *http.Request) *LoginInfo {
|
||||||
session.Values["login_password"] = password
|
session.Values["login_password"] = password
|
||||||
session.Values["login_dn"] = user_dn
|
session.Values["login_dn"] = user_dn
|
||||||
|
|
||||||
|
//Add Value MessageID
|
||||||
|
session.Values["MessageID"] = uint32(0)
|
||||||
|
|
||||||
err = session.Save(r, w)
|
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)
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
var perso_id = 0;
|
||||||
|
|
||||||
function searchDirectory() {
|
function searchDirectory() {
|
||||||
var input = document.getElementById("search").value;
|
var input = document.getElementById("search").value;
|
||||||
if(input){
|
if(input){
|
||||||
|
@ -8,24 +10,27 @@ 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) {
|
||||||
|
perso_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");
|
||||||
var table = document.createElement('tbody');
|
var table = document.createElement('tbody');
|
||||||
table.setAttribute("id","users");
|
table.setAttribute("id","users");
|
||||||
|
|
||||||
for (let i =0; i < Object.keys(jsonResponse).length; i++) {
|
for (let i =0; i < Object.keys(jsonResponse.search).length; i++) {
|
||||||
var row = table.insertRow(0);
|
var row = table.insertRow(0);
|
||||||
var identifiant = row.insertCell(0);
|
var identifiant = row.insertCell(0);
|
||||||
var name = row.insertCell(1);
|
var name = row.insertCell(1);
|
||||||
var email = row.insertCell(2);
|
var email = row.insertCell(2);
|
||||||
identifiant.innerHTML = `<a href="/admin/ldap/${jsonResponse[i].dn}">${jsonResponse[i].identifiant}</a>`
|
identifiant.innerHTML = `<a href="/admin/ldap/${jsonResponse.search[i].dn}">${jsonResponse.search[i].identifiant}</a>`
|
||||||
name.innerHTML = jsonResponse[i].name
|
name.innerHTML = jsonResponse.search[i].name
|
||||||
email.innerHTML = jsonResponse[i].email
|
email.innerHTML = jsonResponse.search[i].email
|
||||||
|
|
||||||
}
|
}
|
||||||
old_table.parentNode.replaceChild(table, old_table)
|
old_table.parentNode.replaceChild(table, old_table)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
xhttp.overrideMimeType("application/json");
|
xhttp.overrideMimeType("application/json");
|
||||||
xhttp.open("GET", "/search/".concat(input), true);
|
xhttp.open("GET", "/search/".concat(input), true);
|
||||||
|
|
Loading…
Reference in a new issue