Add_Directory_and_ProfilePicture #9

Merged
lx merged 13 commits from Add_Directory into main 2021-08-16 14:44:53 +00:00
2 changed files with 80 additions and 0 deletions
Showing only changes of commit b1ce932cd6 - Show all commits

77
directory.go Normal file
View file

@ -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"`
erwan marked this conversation as resolved Outdated
Outdated
Review

L'identifiant, c'est le CN ? Dans tous les cas Identifiant c'est un mot français, il faudrait appeller ça plutôt juste Id.

L'identifiant, c'est le CN ? Dans tous les cas `Identifiant` c'est un mot français, il faudrait appeller ça plutôt juste `Id`.
Name string `json:"name"`
erwan marked this conversation as resolved Outdated
Outdated
Review

C'est le displayname que tu prends depuis le LDAP? Dans ce cas il faudrait appeller ça DisplayName

C'est le displayname que tu prends depuis le LDAP? Dans ce cas il faudrait appeller ça `DisplayName`
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,
})
}
erwan marked this conversation as resolved Outdated
Outdated
Review
  1. Ce n'est pas toujours cn l'attribut qui contient l'identifiant de la personne, c'est le paramètre config.UserNameAttr qui te donne le bon attribut à checker

  2. On aimerait aussi chercher dans le displayname, et peut-être aussi le mail

1. Ce n'est pas toujours `cn` l'attribut qui contient l'identifiant de la personne, c'est le paramètre `config.UserNameAttr` qui te donne le bon attribut à checker 2. On aimerait aussi chercher dans le displayname, et peut-être aussi le mail
}
//Send JSON through xhttp
erwan marked this conversation as resolved Outdated
Outdated
Review

Idem, c'est pas cn chez tout le monde

Idem, c'est pas `cn` chez tout le monde
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)
}
}

View file

@ -133,6 +133,9 @@ func main() {
r.HandleFunc("/profile", handleProfile)
r.HandleFunc("/passwd", handlePasswd)
r.HandleFunc("/directory", handleDirectory)
r.HandleFunc("/search/{input}", handleSearch)
r.HandleFunc("/invite/new_account", handleInviteNewAccount)
r.HandleFunc("/invite/send_code", handleInviteSendCode)
r.HandleFunc("/invitation/{code}", handleInvitationCode)