From 13222204392009e07382eecb908b7e6e01155ec4 Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Sun, 9 Feb 2020 18:28:42 +0100 Subject: User list --- Makefile | 2 +- admin.go | 81 ++++++++++++++++++++++++++++++++++++++++++++++ main.go | 4 +++ templates/admin_users.html | 27 ++++++++++++++++ templates/passwd.html | 2 +- templates/profile.html | 2 +- 6 files changed, 115 insertions(+), 3 deletions(-) create mode 100644 admin.go create mode 100644 templates/admin_users.html diff --git a/Makefile b/Makefile index ef3c9f2..ed43d56 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ all: guichet -guichet: main.go +guichet: main.go ssha.go profile.go admin.go go get -v go build -v diff --git a/admin.go b/admin.go new file mode 100644 index 0000000..60296dc --- /dev/null +++ b/admin.go @@ -0,0 +1,81 @@ +package main + +import ( + "html/template" + "net/http" + "fmt" + "sort" + + "github.com/go-ldap/ldap/v3" +) + +func checkAdminLogin(w http.ResponseWriter, r *http.Request) *LoginStatus { + login := checkLogin(w, r) + if login == nil { + return nil + } + + can_admin := false + for _, group := range login.UserEntry.GetAttributeValues("memberof") { + if config.GroupCanAdmin != "" && group == config.GroupCanAdmin { + can_admin = true + } + } + + if !can_admin { + http.Redirect(w, r, "/", http.StatusFound) + return nil + } + + return login +} + +type AdminUsersTplData struct { + Login *LoginStatus + UserNameAttr string + Users []*ldap.Entry +} + +func handleAdminUsers(w http.ResponseWriter, r *http.Request) { + templateAdminUsers := template.Must(template.ParseFiles("templates/layout.html", "templates/admin_users.html")) + + login := checkLogin(w, r) + if login == nil { + return + } + + searchRequest := ldap.NewSearchRequest( + config.UserBaseDN, + ldap.ScopeSingleLevel, ldap.NeverDerefAliases, 0, 0, false, + fmt.Sprintf("(&(objectClass=organizationalPerson))"), + []string{config.UserNameAttr, "dn", "displayname", "givenname", "sn", "mail"}, + nil) + + sr, err := login.conn.Search(searchRequest) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + data := &AdminUsersTplData{ + Login: login, + UserNameAttr: config.UserNameAttr, + Users: sr.Entries, + } + sort.Sort(data) + + templateAdminUsers.Execute(w, data) +} + +func (d *AdminUsersTplData) Len() int { + return len(d.Users) +} + +func (d *AdminUsersTplData) Swap(i, j int) { + d.Users[i], d.Users[j] = d.Users[j], d.Users[i] +} + +func (d *AdminUsersTplData) Less(i, j int) bool { + return d.Users[i].GetAttributeValue(config.UserNameAttr) < + d.Users[j].GetAttributeValue(config.UserNameAttr) +} diff --git a/main.go b/main.go index 9717167..29da525 100644 --- a/main.go +++ b/main.go @@ -108,6 +108,10 @@ func main() { http.HandleFunc("/profile", handleProfile) http.HandleFunc("/passwd", handlePasswd) + http.HandleFunc("/admin/users", handleAdminUsers) + //http.HandleFunc("/admin/groups", handleAdminGroups) + //http.HandleFunc("/admin/ldap", handleAdminLDAP) + staticfiles := http.FileServer(http.Dir("static")) http.Handle("/static/", http.StripPrefix("/static/", staticfiles)) diff --git a/templates/admin_users.html b/templates/admin_users.html new file mode 100644 index 0000000..8ca80e2 --- /dev/null +++ b/templates/admin_users.html @@ -0,0 +1,27 @@ +{{define "title"}}Liste des utilisateurs |{{end}} + +{{define "body"}} +
+ Retour +
+ + + + + + + + + {{with $root := .}} + {{range $user := $root.Users}} + + + + + + {{end}} + {{end}} + +
{{ .UserNameAttr }}Nom completEmail
{{$user.GetAttributeValue $root.UserNameAttr}}{{$user.GetAttributeValue "displayname"}}{{$user.GetAttributeValue "mail"}}
+ +{{end}} diff --git a/templates/passwd.html b/templates/passwd.html index 7dffcb8..57ae234 100644 --- a/templates/passwd.html +++ b/templates/passwd.html @@ -12,7 +12,7 @@
Nouveau mot de passe enregistré.
- Retour + Retour {{else}}
diff --git a/templates/profile.html b/templates/profile.html index d2ff632..bdab685 100644 --- a/templates/profile.html +++ b/templates/profile.html @@ -12,7 +12,7 @@
Profil enregistré.
- Retour + Retour {{else}}
-- cgit v1.2.3