package ui import ( "fmt" "html/template" "log" "net/http" "streifling.com/jason/cpolis/cmd/data" ) type AddUserData struct { User string First string Last string Role string Msg string } func inputsEmpty(user, pass, pass2, first, last, role string) bool { return len(user) == 0 || len(pass) == 0 || len(pass2) == 0 || len(first) == 0 || len(last) == 0 || len(role) == 0 } func checkUserStrings(user, first, last string) (string, int, bool) { userLen := 15 nameLen := 50 if len(user) > userLen { return "Benutzername", userLen, false } else if len(first) > nameLen { return "Vorname", nameLen, false } else if len(last) > nameLen { return "Nachname", nameLen, false } else { return "", 0, true } } func CreateUser() http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { template.Must(template.ParseFiles("web/templates/add-user.html")).ExecuteTemplate(w, "page-content", nil) } } func AddUser(db *data.DB) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { var writer, editor, admin bool htmlData := 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") if inputsEmpty(htmlData.User, pass, pass2, htmlData.First, htmlData.Last, htmlData.Role) { htmlData.Msg = "Alle Felder müssen ausgefüllt werden." template.Must(template.ParseFiles("web/templates/add-user.html")).ExecuteTemplate(w, "page-content", htmlData) return } userString, stringLen, ok := checkUserStrings(htmlData.User, htmlData.First, htmlData.Last) if !ok { htmlData.Msg = fmt.Sprint(userString, " ist zu lang. Maximal ", stringLen, " Zeichen erlaubt.") template.Must(template.ParseFiles("web/templates/add-user.html")).ExecuteTemplate(w, "page-content", htmlData) return } id, _ := db.GetID(htmlData.User) if id != 0 { htmlData.Msg = fmt.Sprint(htmlData.User, " ist bereits vergeben. Bitte anderen Benutzernamen wählen.") template.Must(template.ParseFiles("web/templates/add-user.html")).ExecuteTemplate(w, "page-content", htmlData) return } if pass != pass2 { htmlData.Msg = "Die Passwörter stimmen nicht überein." template.Must(template.ParseFiles("web/templates/add-user.html")).ExecuteTemplate(w, "page-content", htmlData) return } switch htmlData.Role { case "writer": writer = true editor = false admin = false case "editor": writer = false editor = true admin = false case "admin": writer = false editor = false admin = true default: log.Println("When setting up", htmlData.User, "the HTML was altered.") htmlData.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", htmlData) return } if err := db.AddUser(htmlData.User, pass, htmlData.First, htmlData.Last, writer, editor, admin); err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) return } template.Must(template.ParseFiles("web/templates/hub.html")).ExecuteTemplate(w, "page-content", nil) } }