Add form to create objects

This commit is contained in:
Alex 2020-02-09 23:04:27 +01:00
parent e51bff05d2
commit fa96e61d75
4 changed files with 133 additions and 0 deletions

View file

@ -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)
}

View file

@ -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))

View file

@ -0,0 +1,43 @@
{{define "title"}}Nouvel objet |{{end}}
{{define "body"}}
<div class="d-flex">
<h4>Créer un objet</h4>
<a class="ml-auto btn btn-info" href="/">Retour</a>
</div>
{{if .Error}}
<div class="alert alert-danger mt-4">Impossible de créer l'objet.
<div style="font-size: 0.8em">{{ .Error }}</div>
</div>
{{end}}
<form method="POST" class="mt-4">
<div class="form-group">
<label>Parent:</label>
<input type="text" disabled="true" class="form-control" value="{{ .SuperDN }}" />
</div>
<div class="form-group">
<label for="idvalue">Identifiant:</label>
<input type="text" id="idvalue" name="idvalue" class="form-control" value="{{ .IdValue }}" />
</div>
<div class="form-group">
<label for="idtype">Type d'identifiant:</label>
<input type="text" id="idtype" name="idtype" class="form-control" value="{{ .IdType }}" />
</div>
<div class="form-group">
<label for="displayname">Nom:</label>
<input type="text" id="displayname" name="displayname" class="form-control" value="{{ .DisplayName }}" />
</div>
<div class="form-group">
<label for="soc">StructuralObjectClass:</label>
<input type="text" id="soc" name="soc" class="form-control" value="{{ .StructuralObjectClass }}" />
</div>
<div class="form-group">
<label for="oc">ObjectClass:</label>
<textarea rows="5" id="oc" name="oc" class="form-control">{{ .ObjectClass }}</textarea>
</div>
<button type="submit" class="btn btn-primary">Créer l'objet</button>
</form>
{{end}}

View file

@ -21,6 +21,14 @@
</nav>
</div>
<div class="mt-4">
<div class="d-flex">
<a class="ml-auto btn btn-sm btn-success" href="/admin/create/user/{{.DN}}">+utilisateur</a>
<a class="ml-4 btn btn-sm btn-success" href="/admin/create/group/{{.DN}}">+groupe</a>
<a class="ml-4 btn btn-sm btn-success" href="/admin/create/generic/{{.DN}}">+objet</a>
</div>
</div>
<table class="table mt-4">
<tbody>
{{range .Children}}