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(c *control.Config) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { tmpl, err := template.ParseFiles(c.WebDir + "/templates/add-user.html") template.Must(tmpl, err).ExecuteTemplate(w, "page-content", nil) } } func AddUser(c *control.Config, 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(c.WebDir + "/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(c.WebDir + "/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(c.WebDir + "/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(c.WebDir + "/templates/add-user.html") template.Must(tmpl, err).ExecuteTemplate(w, "page-content", htmlData) return } _, err = db.AddUser(htmlData.User, pass) if err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) return } tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html") template.Must(tmpl, err).ExecuteTemplate(w, "page-content", 0) } } func EditSelf(c *control.Config, 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(c.WebDir + "/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(c.WebDir + "/templates/edit-self.html") template.Must(tmpl, err).ExecuteTemplate(w, "page-content", user) } } func UpdateSelf(c *control.Config, 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(c.WebDir + "/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(c.WebDir + "/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(c.WebDir + "/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(c.WebDir + "/templates/edit-user.html") tmpl = template.Must(tmpl, err) tmpl.ExecuteTemplate(w, "page-content", userData) return } } if err = db.UpdateOwnAttributes( userData.ID, userData.UserName, userData.FirstName, userData.LastName, oldPass, newPass, newPass2); err != nil { userData.Msg = "Aktualisierung der Benutzerdaten fehlgeschlagen." tmpl, err := template.ParseFiles(c.WebDir + "/templates/edit-user.html") template.Must(tmpl, err).ExecuteTemplate(w, "page-content", userData) } tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html") tmpl = template.Must(tmpl, err) tmpl.ExecuteTemplate(w, "page-content", session.Values["role"].(int)) } } func AddFirstUser(c *control.Config, db *model.DB, s *control.CookieStore) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { var err error htmlData := UserData{ User: &model.User{ UserName: r.PostFormValue("username"), FirstName: r.PostFormValue("first-name"), LastName: r.PostFormValue("last-name"), Role: model.Admin, }, } 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(c.WebDir + "/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(c.WebDir + "/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(c.WebDir + "/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(c.WebDir + "/templates/add-user.html") template.Must(tmpl, err).ExecuteTemplate(w, "page-content", htmlData) return } htmlData.ID, err = db.AddFirstUser(htmlData.User, pass) if err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) return } if htmlData.ID > 1 { errString := "error: there is already a first user" log.Println(errString) http.Error(w, errString, http.StatusInternalServerError) 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.AddIssue(); err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) return } tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html") template.Must(tmpl, err).ExecuteTemplate(w, "page-content", 0) } } func ShowAllUsers(c *control.Config, db *model.DB) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { users, err := db.GetAllUsers() if err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) return } tmpl, err := template.ParseFiles(c.WebDir + "/templates/show-all-users.html") template.Must(tmpl, err).ExecuteTemplate(w, "page-content", users) } } func EditUser(c *control.Config, db *model.DB) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { id, err := strconv.ParseInt(r.PathValue("id"), 10, 64) if err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) return } user, err := db.GetUser(id) if err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) return } tmpl, err := template.ParseFiles(c.WebDir + "/templates/edit-user.html") template.Must(tmpl, err).ExecuteTemplate(w, "page-content", user) } } func UpdateUser(c *control.Config, db *model.DB, s *control.CookieStore) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { id, err := strconv.ParseInt(r.PathValue("id"), 10, 64) if err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) return } role, err := strconv.Atoi(r.PostFormValue("role")) if err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) return } userData := UserData{ User: &model.User{ ID: id, UserName: r.PostFormValue("username"), FirstName: r.PostFormValue("first-name"), LastName: r.PostFormValue("last-name"), Role: role, }, } 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(c.WebDir + "/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(c.WebDir + "/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(c.WebDir + "/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, newPass, newPass2, userData.Role); err != nil { userData.Msg = "Aktualisierung der Benutzerdaten fehlgeschlagen." tmpl, err := template.ParseFiles(c.WebDir + "/templates/edit-user.html") template.Must(tmpl, err).ExecuteTemplate(w, "page-content", userData) } tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html") tmpl = template.Must(tmpl, err) tmpl.ExecuteTemplate(w, "page-content", 0) } }