package view import ( "fmt" "html/template" "log" "net/http" "strconv" "streifling.com/jason/cpolis/cmd/control" "streifling.com/jason/cpolis/cmd/model" ) type UserData struct { *model.User Msg string } func checkUserStrings(user *model.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 *model.DB, s *control.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 := UserData{ User: &model.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 len(htmlData.UserName) == 0 || len(htmlData.FirstName) == 0 || len(htmlData.LastName) == 0 || len(pass) == 0 || len(pass2) == 0 { 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("users") if err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) return } if num == 0 { if htmlData.Role != model.Admin { htmlData.Msg = "Der erste Benutzer muss ein Administrator sein." htmlData.Role = model.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) } } func EditUser(db *model.DB, s *control.CookieStore) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { session, err := s.Get(r, "cookie") if err != nil { tmpl, err := template.ParseFiles("web/templates/login.html") msg := "Session nicht mehr gültig. Bitte erneut anmelden." template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg) } user, err := db.GetUser(session.Values["id"].(int64)) if err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) return } tmpl, err := template.ParseFiles("web/templates/edit-user.html") template.Must(tmpl, err).ExecuteTemplate(w, "page-content", user) } } func UpdateUser(db *model.DB, s *control.CookieStore) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { session, err := s.Get(r, "cookie") if err != nil { tmpl, err := template.ParseFiles("web/templates/login.html") msg := "Session nicht mehr gültig. Bitte erneut anmelden." template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg) } userData := UserData{ User: &model.User{ ID: session.Values["id"].(int64), UserName: r.PostFormValue("username"), FirstName: r.PostFormValue("first-name"), LastName: r.PostFormValue("last-name"), }, } oldPass := r.PostFormValue("old-password") newPass := r.PostFormValue("password") newPass2 := r.PostFormValue("password2") if len(userData.UserName) == 0 || len(userData.FirstName) == 0 || len(userData.LastName) == 0 { userData.Msg = "Alle Felder mit * müssen ausgefüllt sein." tmpl, err := template.ParseFiles("web/templates/edit-user.html") tmpl = template.Must(tmpl, err) tmpl.ExecuteTemplate(w, "page-content", userData.Msg) return } userString, stringLen, ok := checkUserStrings(userData.User) if !ok { userData.Msg = fmt.Sprint(userString, " ist zu lang. Maximal ", stringLen, " Zeichen erlaubt.") tmpl, err := template.ParseFiles("web/templates/edit-user.html") tmpl = template.Must(tmpl, err) tmpl.ExecuteTemplate(w, "page-content", userData) return } if id, ok := db.GetID(userData.UserName); ok { if id != userData.ID { userData.Msg = "Benutzername bereits vergeben." userData.UserName = "" tmpl, err := template.ParseFiles("web/templates/edit-user.html") tmpl = template.Must(tmpl, err) tmpl.ExecuteTemplate(w, "page-content", userData) return } } if err = db.UpdateUserAttributes( userData.ID, userData.UserName, userData.FirstName, userData.LastName, oldPass, newPass, newPass2); err != nil { userData.Msg = "Aktualisierung der Benutzerdaten fehlgeschlagen." tmpl, err := template.ParseFiles("web/templates/edit-user.html") template.Must(tmpl, err).ExecuteTemplate(w, "page-content", userData) } tmpl, err := template.ParseFiles("web/templates/hub.html") tmpl = template.Must(tmpl, err) tmpl.ExecuteTemplate(w, "page-content", session.Values["role"].(int)) } }