Added messages and field memory for adding user

This commit is contained in:
Jason Streifling 2024-02-24 13:25:32 +01:00
parent f716e9f0b5
commit 36f7a92a06
4 changed files with 54 additions and 30 deletions

View File

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

View File

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

View File

@ -1 +1,9 @@
package ui
type AddUserData struct {
User string
First string
Last string
Role string
Msg string
}

View File

@ -1,20 +1,27 @@
{{define "page-content"}}
<h2>Neuer Benutzer</h2>
<form>
<input required name="username" placeholder="Benutzername" type="text" />
<input required name="username" placeholder="Benutzername" type="text" value="{{.User}}" />
<input required name="password" placeholder="Passwort" type="password" />
<input required name="password2" placeholder="Passwort wiederholen" type="password" />
<input required name="first-name" placeholder="Vorname" type="text" />
<input required name="last-name" placeholder="Nachname" type="text" />
<input required name="first-name" placeholder="Vorname" type="text" value="{{.First}}" />
<input required name="last-name" placeholder="Nachname" type="text" value="{{.Last}}" />
<label for="writer">Schreiber</label>
<input required id="writer" name="role" type="radio" value="writer" />
<input required id="writer" name="role" type="radio" value="writer" {{if eq .Role "writer" }}checked{{end}} />
<label for="editor">Redakteur</label>
<input required id="editor" name="role" type="radio" value="editor" />
<input required id="editor" name="role" type="radio" value="editor" {{if eq .Role "editor" }}checked{{end}} />
<label for="admin">Admin</label>
<input required id="admin" name="role" type="radio" value="admin" />
<input required id="admin" name="role" type="radio" value="admin" {{if eq .Role "admin" }}checked{{end}} />
<input type="submit" value="Anlegen" hx-post="/add-user/" hx-target="#page-content" />
</form>
<script>
var msg = "{{.Msg}}";
if (msg != "") {
alert(msg);
}
</script>
{{end}}