aboutsummaryrefslogtreecommitdiff
path: root/directory.go
blob: ab5dea34b3d48def4bf358a3da7e5ebfa6b61088 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package main

import (
	"html/template"
	"net/http"
	"sort"
	"strings"

	"github.com/go-ldap/ldap/v3"
)

const FIELD_NAME_PROFILE_PICTURE = "profilePicture"
const FIELD_NAME_DIRECTORY_VISIBILITY = "directoryVisibility"

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 {
	DN             string
	Id             string
	DisplayName    string
	Email          string
	Description    string
	ProfilePicture string
}

type SearchResults struct {
	Results []SearchResult
}

func handleDirectorySearch(w http.ResponseWriter, r *http.Request) {
	templateDirectoryResults := template.Must(template.ParseFiles("templates/directory_results.html"))

	//Get input value by user
	r.ParseMultipartForm(1024)
	input := strings.TrimSpace(strings.Join(r.Form["query"], ""))

	if r.Method != "POST" {
		http.Error(w, "Invalid request", http.StatusBadRequest)
		return
	}

	//Log to allow the research
	login := checkLogin(w, r)
	if login == nil {
		http.Error(w, "Login required", http.StatusUnauthorized)
		return
	}

	//Search values with ldap and filter
	searchRequest := ldap.NewSearchRequest(
		config.UserBaseDN,
		ldap.ScopeSingleLevel, ldap.NeverDerefAliases, 0, 0, false,
		"(&(objectclass=organizationalPerson)("+FIELD_NAME_DIRECTORY_VISIBILITY+"=on))",
		[]string{
			config.UserNameAttr,
			"displayname",
			"mail",
			"description",
			FIELD_NAME_PROFILE_PICTURE,
		},
		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 JSON
	results := []SearchResult{}

	for _, values := range sr.Entries {
		if input == "" ||
			ContainsI(values.GetAttributeValue(config.UserNameAttr), input) ||
			ContainsI(values.GetAttributeValue("displayname"), input) ||
			ContainsI(values.GetAttributeValue("mail"), input) {
			results = append(results, SearchResult{
				DN:             values.DN,
				Id:             values.GetAttributeValue(config.UserNameAttr),
				DisplayName:    values.GetAttributeValue("displayname"),
				Email:          values.GetAttributeValue("mail"),
				Description:    values.GetAttributeValue("description"),
				ProfilePicture: values.GetAttributeValue(FIELD_NAME_PROFILE_PICTURE),
			})
		}
	}

	search_results := SearchResults{
		Results: results,
	}
	sort.Sort(&search_results)

	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]
}