diff --git a/cmd/ui/handlers.go b/cmd/ui/handlers.go index 92454e0..db262e9 100644 --- a/cmd/ui/handlers.go +++ b/cmd/ui/handlers.go @@ -1,6 +1,7 @@ package ui import ( + "fmt" "html/template" "log" "net/http" @@ -25,9 +26,9 @@ func HandleLogin(db *data.DB) http.HandlerFunc { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) return - } else { - template.Must(template.ParseFiles("web/templates/editor.html")).ExecuteTemplate(w, "page-content", nil) } + + template.Must(template.ParseFiles("web/templates/editor.html")).ExecuteTemplate(w, "page-content", nil) } } @@ -54,32 +55,38 @@ func HandleAddUser(db *data.DB) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { var writer, editor, admin bool - user := r.PostFormValue("username") + data := AddUserData{ + User: r.PostFormValue("username"), + First: r.PostFormValue("first-name"), + Last: r.PostFormValue("last-name"), + Role: r.PostFormValue("role"), + } pass := r.PostFormValue("password") pass2 := r.PostFormValue("password2") - first := r.PostFormValue("first-name") - last := r.PostFormValue("last-name") - role := r.PostFormValue("role") - if inputsEmpty(user, pass, pass2, first, last, role) { - log.Println("inputsEmpty") - template.Must(template.ParseFiles("web/templates/add-user.html")).Execute(w, nil) + if inputsEmpty(data.User, pass, pass2, data.First, data.Last, data.Role) { + data.Msg = "Alle Felder müssen ausgefüllt werden." + template.Must(template.ParseFiles("web/templates/add-user.html")).ExecuteTemplate(w, "page-content", data) + return } - _, _, ok := checkUserStrings(user, first, last) + userString, stringLen, ok := checkUserStrings(data.User, data.First, data.Last) if !ok { - log.Println("checkUserStrings") - template.Must(template.ParseFiles("web/templates/add-user.html")).Execute(w, nil) + data.Msg = fmt.Sprint(userString, " ist zu lang. Maximal ", stringLen, " Zeichen erlaubt.") + template.Must(template.ParseFiles("web/templates/add-user.html")).ExecuteTemplate(w, "page-content", data) + return } - id, _ := db.GetID(user) + id, _ := db.GetID(data.User) if id != 0 { - log.Println("GetID") - template.Must(template.ParseFiles("web/templates/add-user.html")).Execute(w, nil) + data.Msg = fmt.Sprint(data.User, " ist bereits vergeben. Bitte anderen Benutzernamen wählen.") + template.Must(template.ParseFiles("web/templates/add-user.html")).ExecuteTemplate(w, "page-content", data) + return } if pass != pass2 { - log.Println("pass") - template.Must(template.ParseFiles("web/templates/add-user.html")).Execute(w, nil) + data.Msg = "Die Passwörter stimmen nicht überein." + template.Must(template.ParseFiles("web/templates/add-user.html")).ExecuteTemplate(w, "page-content", data) + return } - switch role { + switch data.Role { case "writer": writer = true editor = false @@ -93,11 +100,13 @@ func HandleAddUser(db *data.DB) http.HandlerFunc { editor = false admin = true default: - log.Println("switch") - template.Must(template.ParseFiles("web/templates/add-user.html")).Execute(w, nil) + log.Println("When setting up", data.User, "the HTML was altered.") + data.Msg = "Das HTML der Seite zu verändern ist unzulässig. Dieser Vorfall wurde gespeichert." + template.Must(template.ParseFiles("web/templates/add-user.html")).ExecuteTemplate(w, "page-content", data) + return } - if err := db.AddUser(user, pass, first, last, writer, editor, admin); err != nil { + if err := db.AddUser(data.User, pass, data.First, data.Last, writer, editor, admin); err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) return diff --git a/cmd/ui/helpers.go b/cmd/ui/helpers.go index 6ea045a..8d9401c 100644 --- a/cmd/ui/helpers.go +++ b/cmd/ui/helpers.go @@ -14,11 +14,11 @@ func checkUserStrings(user, first, last string) (string, int, bool) { nameLen := 50 if len(user) > userLen { - return user, userLen, false + return "Benutzername", userLen, false } else if len(first) > nameLen { - return first, nameLen, false + return "Vorname", nameLen, false } else if len(last) > nameLen { - return last, nameLen, false + return "Nachname", nameLen, false } else { return "", 0, true } diff --git a/cmd/ui/htmlStructs.go b/cmd/ui/htmlStructs.go index 5b1faa2..2c36c45 100644 --- a/cmd/ui/htmlStructs.go +++ b/cmd/ui/htmlStructs.go @@ -1 +1,9 @@ package ui + +type AddUserData struct { + User string + First string + Last string + Role string + Msg string +} diff --git a/web/templates/add-user.html b/web/templates/add-user.html index 4cfacfd..eb558cd 100644 --- a/web/templates/add-user.html +++ b/web/templates/add-user.html @@ -1,20 +1,27 @@ {{define "page-content"}}