2021-07-21 19:21:46 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"html/template"
|
|
|
|
"net/http"
|
2021-08-16 14:27:20 +00:00
|
|
|
"sort"
|
2021-07-21 19:21:46 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/go-ldap/ldap/v3"
|
|
|
|
)
|
|
|
|
|
2021-08-16 13:30:14 +00:00
|
|
|
const FIELD_NAME_PROFILE_PICTURE = "profilePicture"
|
|
|
|
const FIELD_NAME_DIRECTORY_VISIBILITY = "directoryVisibility"
|
|
|
|
|
2021-07-21 19:21:46 +00:00
|
|
|
func handleDirectory(w http.ResponseWriter, r *http.Request) {
|
2022-12-01 22:05:59 +00:00
|
|
|
templateDirectory := getTemplate("directory.html")
|
2021-07-21 19:21:46 +00:00
|
|
|
|
|
|
|
login := checkLogin(w, r)
|
|
|
|
if login == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
templateDirectory.Execute(w, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
type SearchResult struct {
|
2021-08-16 14:28:42 +00:00
|
|
|
DN string
|
|
|
|
Id string
|
|
|
|
DisplayName string
|
|
|
|
Email string
|
|
|
|
Description string
|
2021-08-16 14:27:20 +00:00
|
|
|
ProfilePicture string
|
2021-07-21 19:21:46 +00:00
|
|
|
}
|
|
|
|
|
2021-08-16 14:27:20 +00:00
|
|
|
type SearchResults struct {
|
|
|
|
Results []SearchResult
|
2021-07-26 14:54:51 +00:00
|
|
|
}
|
2021-07-21 19:21:46 +00:00
|
|
|
|
2021-08-16 14:27:20 +00:00
|
|
|
func handleDirectorySearch(w http.ResponseWriter, r *http.Request) {
|
2022-12-01 22:05:59 +00:00
|
|
|
templateDirectoryResults := template.Must(template.ParseFiles(templatePath + "/directory_results.html"))
|
2021-07-29 08:55:44 +00:00
|
|
|
|
2021-07-21 19:21:46 +00:00
|
|
|
//Get input value by user
|
2021-08-16 14:27:20 +00:00
|
|
|
r.ParseMultipartForm(1024)
|
|
|
|
input := strings.TrimSpace(strings.Join(r.Form["query"], ""))
|
|
|
|
|
2021-08-18 10:44:44 +00:00
|
|
|
if r.Method != "POST" {
|
2021-08-16 14:27:20 +00:00
|
|
|
http.Error(w, "Invalid request", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-07-21 19:21:46 +00:00
|
|
|
//Log to allow the research
|
|
|
|
login := checkLogin(w, r)
|
|
|
|
if login == nil {
|
2021-08-16 14:27:20 +00:00
|
|
|
http.Error(w, "Login required", http.StatusUnauthorized)
|
2021-07-21 19:21:46 +00:00
|
|
|
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-08-16 13:30:14 +00:00
|
|
|
"(&(objectclass=organizationalPerson)("+FIELD_NAME_DIRECTORY_VISIBILITY+"=on))",
|
|
|
|
[]string{
|
|
|
|
config.UserNameAttr,
|
|
|
|
"displayname",
|
|
|
|
"mail",
|
|
|
|
"description",
|
|
|
|
FIELD_NAME_PROFILE_PICTURE,
|
|
|
|
},
|
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
|
|
|
|
}
|
|
|
|
|
2021-07-30 14:30:15 +00:00
|
|
|
//Transform the researh's result in a correct struct to send JSON
|
2021-08-16 14:27:20 +00:00
|
|
|
results := []SearchResult{}
|
2021-07-21 19:21:46 +00:00
|
|
|
|
2021-08-16 14:27:20 +00:00
|
|
|
for _, values := range sr.Entries {
|
2021-08-18 10:44:44 +00:00
|
|
|
if input == "" ||
|
|
|
|
ContainsI(values.GetAttributeValue(config.UserNameAttr), input) ||
|
2021-08-16 14:27:20 +00:00
|
|
|
ContainsI(values.GetAttributeValue("displayname"), input) ||
|
|
|
|
ContainsI(values.GetAttributeValue("mail"), input) {
|
|
|
|
results = append(results, SearchResult{
|
2021-08-16 14:28:42 +00:00
|
|
|
DN: values.DN,
|
|
|
|
Id: values.GetAttributeValue(config.UserNameAttr),
|
|
|
|
DisplayName: values.GetAttributeValue("displayname"),
|
|
|
|
Email: values.GetAttributeValue("mail"),
|
|
|
|
Description: values.GetAttributeValue("description"),
|
2021-08-16 14:27:20 +00:00
|
|
|
ProfilePicture: values.GetAttributeValue(FIELD_NAME_PROFILE_PICTURE),
|
|
|
|
})
|
2021-07-26 14:54:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-16 14:27:20 +00:00
|
|
|
search_results := SearchResults{
|
|
|
|
Results: results,
|
2021-07-29 08:55:44 +00:00
|
|
|
}
|
2021-08-16 14:27:20 +00:00
|
|
|
sort.Sort(&search_results)
|
2021-07-29 08:55:44 +00:00
|
|
|
|
2021-08-16 14:27:20 +00:00
|
|
|
templateDirectoryResults.Execute(w, search_results)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ContainsI(a string, b string) bool {
|
|
|
|
return strings.Contains(
|
|
|
|
strings.ToLower(a),
|
|
|
|
strings.ToLower(b),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *SearchResults) Len() int {
|
|
|
|
return len(r.Results)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *SearchResults) Less(i, j int) bool {
|
|
|
|
return r.Results[i].Id < r.Results[j].Id
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *SearchResults) Swap(i, j int) {
|
|
|
|
r.Results[i], r.Results[j] = r.Results[j], r.Results[i]
|
2021-07-21 19:21:46 +00:00
|
|
|
}
|