package ui import ( "fmt" "html/template" "log" "net/http" "strconv" "streifling.com/jason/cpolis/cmd/data" ) type AddUserData struct { *data.User Msg string } func inputsEmpty(user *data.User, pass, pass2 string) bool { return len(user.UserName) == 0 || len(user.FirstName) == 0 || len(user.LastName) == 0 || len(pass) == 0 || len(pass2) == 0 } func checkUserStrings(user *data.User) (string, int, bool) { userLen := 15 nameLen := 50 if len(user.UserName) > userLen { return "Benutzername", userLen, false } else if len(user.FirstName) > nameLen { return "Vorname", nameLen, false } else if len(user.LastName) > nameLen { return "Nachname", nameLen, false } else { return "", 0, true } } func CreateUser(w http.ResponseWriter, r *http.Request) { tmpl, err := template.ParseFiles("web/templates/add-user.html") template.Must(tmpl, err).ExecuteTemplate(w, "page-content", nil) } func AddUser(db *data.DB, s *data.CookieStore) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { role, err := strconv.Atoi(r.PostFormValue("role")) if err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) return } htmlData := AddUserData{ User: &data.User{ UserName: r.PostFormValue("username"), FirstName: r.PostFormValue("first-name"), LastName: r.PostFormValue("last-name"), Role: role, }, } pass := r.PostFormValue("password") pass2 := r.PostFormValue("password2") if inputsEmpty(htmlData.User, pass, pass2) { htmlData.Msg = "Alle Felder müssen ausgefüllt werden." tmpl, err := template.ParseFiles("web/templates/add-user.html") template.Must(tmpl, err).ExecuteTemplate(w, "page-content", htmlData) return } userString, stringLen, ok := checkUserStrings(htmlData.User) if !ok { htmlData.Msg = fmt.Sprint(userString, " ist zu lang. Maximal ", stringLen, " Zeichen erlaubt.") tmpl, err := template.ParseFiles("web/templates/add-user.html") template.Must(tmpl, err).ExecuteTemplate(w, "page-content", htmlData) return } id, _ := db.GetID(htmlData.UserName) if id != 0 { htmlData.Msg = fmt.Sprint(htmlData.UserName, " ist bereits vergeben. Bitte anderen Benutzernamen wählen.") tmpl, err := template.ParseFiles("web/templates/add-user.html") template.Must(tmpl, err).ExecuteTemplate(w, "page-content", htmlData) return } if pass != pass2 { htmlData.Msg = "Die Passwörter stimmen nicht überein." tmpl, err := template.ParseFiles("web/templates/add-user.html") template.Must(tmpl, err).ExecuteTemplate(w, "page-content", htmlData) return } num, err := db.CountEntries() if err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) return } if num == 0 { if htmlData.Role != data.Admin { htmlData.Msg = "Der erste Benutzer muss ein Administrator sein." htmlData.Role = data.Admin tmpl, err := template.ParseFiles("web/templates/add-user.html") tmpl = template.Must(tmpl, err) tmpl.ExecuteTemplate(w, "page-content", htmlData) return } if err := saveSession(w, r, s, htmlData.User); err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) return } } if err := db.AddUser(htmlData.User, pass); err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) return } tmpl, err := template.ParseFiles("web/templates/hub.html") template.Must(tmpl, err).ExecuteTemplate(w, "page-content", 0) } }