From fa96e61d75127d087b776c69df4dadd90197e4e3 Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Sun, 9 Feb 2020 23:04:27 +0100 Subject: Add form to create objects --- admin.go | 81 +++++++++++++++++++++++++++++++++++++++++++++ main.go | 1 + templates/admin_create.html | 43 ++++++++++++++++++++++++ templates/admin_ldap.html | 8 +++++ 4 files changed, 133 insertions(+) create mode 100644 templates/admin_create.html diff --git a/admin.go b/admin.go index def23d6..63a1194 100644 --- a/admin.go +++ b/admin.go @@ -1,6 +1,7 @@ package main import ( + "regexp" "strings" "fmt" "html/template" @@ -453,3 +454,83 @@ func handleAdminLDAP(w http.ResponseWriter, r *http.Request) { Success: dSuccess, }) } + +type CreateData struct { + SuperDN string + + IdType string + IdValue string + DisplayName string + StructuralObjectClass string + ObjectClass string + + Error string +} + +func handleAdminCreate(w http.ResponseWriter, r *http.Request) { + templateAdminCreate := template.Must(template.ParseFiles("templates/layout.html", "templates/admin_create.html")) + + login := checkAdminLogin(w, r) + if login == nil { + return + } + + template := mux.Vars(r)["template"] + super_dn := mux.Vars(r)["super_dn"] + + data := &CreateData{ + SuperDN: super_dn, + } + if template == "user" { + data.IdType = config.UserNameAttr + data.StructuralObjectClass = "inetOrgPerson" + data.ObjectClass = "inetOrgPerson\norganizationalPerson\nperson\ntop" + } else if template == "group" { + data.IdType = config.UserNameAttr + data.StructuralObjectClass = "groupOfNames" + data.ObjectClass = "groupOfNames\ntop" + } + + if r.Method == "POST" { + r.ParseForm() + data.IdType = strings.Join(r.Form["idtype"], "") + data.IdValue = strings.Join(r.Form["idvalue"], "") + data.DisplayName = strings.Join(r.Form["displayname"], "") + data.StructuralObjectClass = strings.Join(r.Form["soc"], "") + data.ObjectClass = strings.Join(r.Form["oc"], "") + + object_class := []string{} + for _, oc := range strings.Split(data.ObjectClass, "\n") { + x := strings.TrimSpace(oc) + if x != "" { + object_class = append(object_class, x) + } + } + + if len(object_class) == 0 { + data.Error = "No object class specified" + } else if match, err := regexp.MatchString("^[a-z]+$", data.IdType); err != nil || !match { + data.Error = "Invalid identifier type" + } else if len(data.IdValue) == 0 { + data.Error = "No identifier specified" + } else if match, err := regexp.MatchString("^[\\d\\w_-]+$", data.IdValue); err != nil || !match { + data.Error = "Invalid identifier" + } else { + dn := data.IdType + "=" + data.IdValue + "," + super_dn + req := ldap.NewAddRequest(dn, nil) + req.Attribute("objectClass", object_class) + req.Attribute("structuralObjectClass", + []string{data.StructuralObjectClass}) + req.Attribute("displayname", []string{data.DisplayName}) + err := login.conn.Add(req) + if err != nil { + data.Error = err.Error() + } else { + http.Redirect(w, r, "/admin/ldap/" + dn, http.StatusFound) + } + + } + } + + templateAdminCreate.Execute(w, data) +} diff --git a/main.go b/main.go index a0d5b07..a35ce8b 100644 --- a/main.go +++ b/main.go @@ -115,6 +115,7 @@ func main() { r.HandleFunc("/admin/users", handleAdminUsers) r.HandleFunc("/admin/groups", handleAdminGroups) r.HandleFunc("/admin/ldap/{dn}", handleAdminLDAP) + r.HandleFunc("/admin/create/{template}/{super_dn}", handleAdminCreate) staticfiles := http.FileServer(http.Dir("static")) r.Handle("/static/{file:.*}", http.StripPrefix("/static/", staticfiles)) diff --git a/templates/admin_create.html b/templates/admin_create.html new file mode 100644 index 0000000..94dee5b --- /dev/null +++ b/templates/admin_create.html @@ -0,0 +1,43 @@ +{{define "title"}}Nouvel objet |{{end}} + +{{define "body"}} +
+

Créer un objet

+ Retour +
+ +{{if .Error}} +
Impossible de créer l'objet. +
{{ .Error }}
+
+{{end}} + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+ +
+ +{{end}} diff --git a/templates/admin_ldap.html b/templates/admin_ldap.html index fa4b934..53570fe 100644 --- a/templates/admin_ldap.html +++ b/templates/admin_ldap.html @@ -21,6 +21,14 @@ +
+ +
+ {{range .Children}} -- cgit v1.2.3