From 4b5929911e5d0828ac8e74259d6696a503beaae3 Mon Sep 17 00:00:00 2001 From: Jason Streifling Date: Fri, 1 Mar 2024 12:25:53 +0100 Subject: [PATCH] Implemented proper User struct --- cmd/data/db.go | 12 ++--- cmd/data/user.go | 18 ++++++++ cmd/ui/admin.go | 88 ++++++++++++++++++------------------- main.go | 16 +------ web/templates/add-user.html | 12 ++--- 5 files changed, 75 insertions(+), 71 deletions(-) create mode 100644 cmd/data/user.go diff --git a/cmd/data/db.go b/cmd/data/db.go index 9c0fd54..622b480 100644 --- a/cmd/data/db.go +++ b/cmd/data/db.go @@ -34,7 +34,7 @@ func OpenDB(dbName string) (*DB, error) { return &db, nil } -func (db *DB) AddUser(user, pass, first, last string, writer, editor, admin bool) error { +func (db *DB) AddUser(user User, pass string) error { hashedPass, err := bcrypt.GenerateFromPassword([]byte(pass), bcrypt.DefaultCost) if err != nil { return fmt.Errorf("error creating password hash: %v", err) @@ -42,11 +42,11 @@ func (db *DB) AddUser(user, pass, first, last string, writer, editor, admin bool query := ` INSERT INTO users - (username, password, first_name, last_name, writer, editor, admin) + (username, password, first_name, last_name, role) VALUES - (?, ?, ?, ?, ?, ?, ?) + (?, ?, ?, ?, ?) ` - _, err = db.Exec(query, user, string(hashedPass), first, last, writer, editor, admin) + _, err = db.Exec(query, user.UserName, string(hashedPass), user.FirstName, user.LastName, user.Role) if err != nil { return fmt.Errorf("error inserting user into DB: %v", err) } @@ -54,7 +54,7 @@ func (db *DB) AddUser(user, pass, first, last string, writer, editor, admin bool return nil } -func (db *DB) GetID(user string) (int64, error) { +func (db *DB) GetID(userName string) (int64, error) { var id int64 query := ` @@ -63,7 +63,7 @@ func (db *DB) GetID(user string) (int64, error) { WHERE username = ? ` - row := db.QueryRow(query, user) + row := db.QueryRow(query, userName) if err := row.Scan(&id); err != nil { return 0, fmt.Errorf("user not in DB: %v", err) } diff --git a/cmd/data/user.go b/cmd/data/user.go new file mode 100644 index 0000000..79cc090 --- /dev/null +++ b/cmd/data/user.go @@ -0,0 +1,18 @@ +package data + +const ( + Admin = iota + Editor + Writer +) + +type Role int + +type User struct { + RejectedArticles *ArticleList + UserName string + FirstName string + LastName string + ID int64 + Role +} diff --git a/cmd/ui/admin.go b/cmd/ui/admin.go index 9c8d1bd..ce93901 100644 --- a/cmd/ui/admin.go +++ b/cmd/ui/admin.go @@ -5,42 +5,54 @@ import ( "html/template" "log" "net/http" + "strconv" "streifling.com/jason/cpolis/cmd/data" ) type AddUserData struct { - User string - First string - Last string - Role string - Msg string + data.User + Msg string } -func inputsEmpty(user, pass, pass2, first, last, role string) bool { - return len(user) == 0 || +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 || - len(first) == 0 || - len(last) == 0 || - len(role) == 0 + len(pass2) == 0 } -func checkUserStrings(user, first, last string) (string, int, bool) { +func checkUserStrings(user data.User) (string, int, bool) { userLen := 15 nameLen := 50 - if len(user) > userLen { + if len(user.UserName) > userLen { return "Benutzername", userLen, false - } else if len(first) > nameLen { + } else if len(user.FirstName) > nameLen { return "Vorname", nameLen, false - } else if len(last) > nameLen { + } else if len(user.LastName) > nameLen { return "Nachname", nameLen, false } else { return "", 0, true } } +func HomePage(db *data.DB) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + numRows, err := db.CountEntries() + if err != nil { + log.Fatalln(err) + } + + if numRows == 0 { + template.Must(template.ParseFiles("web/templates/index.html", "web/templates/add-user.html")).Execute(w, nil) + } else { + template.Must(template.ParseFiles("web/templates/index.html", "web/templates/login.html")).Execute(w, nil) + } + } +} + 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) @@ -49,31 +61,38 @@ func CreateUser() http.HandlerFunc { func AddUser(db *data.DB) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - var writer, editor, admin bool + role, err := strconv.Atoi(r.PostFormValue("role")) + if err != nil { + log.Println(err) + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } htmlData := AddUserData{ - User: r.PostFormValue("username"), - First: r.PostFormValue("first-name"), - Last: r.PostFormValue("last-name"), - Role: r.PostFormValue("role"), + User: data.User{ + UserName: r.PostFormValue("username"), + FirstName: r.PostFormValue("first-name"), + LastName: r.PostFormValue("last-name"), + Role: data.Role(role), + }, } pass := r.PostFormValue("password") pass2 := r.PostFormValue("password2") - if inputsEmpty(htmlData.User, pass, pass2, htmlData.First, htmlData.Last, htmlData.Role) { + if inputsEmpty(htmlData.User, pass, pass2) { 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) + userString, stringLen, ok := checkUserStrings(htmlData.User) 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) + id, _ := db.GetID(htmlData.UserName) if id != 0 { - htmlData.Msg = fmt.Sprint(htmlData.User, " ist bereits vergeben. Bitte anderen Benutzernamen wählen.") + htmlData.Msg = fmt.Sprint(htmlData.UserName, " ist bereits vergeben. Bitte anderen Benutzernamen wählen.") template.Must(template.ParseFiles("web/templates/add-user.html")).ExecuteTemplate(w, "page-content", htmlData) return } @@ -82,27 +101,8 @@ func AddUser(db *data.DB) http.HandlerFunc { 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 { + if err := db.AddUser(htmlData.User, pass); err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) return diff --git a/main.go b/main.go index ba7e69f..5217125 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,6 @@ package main import ( - "html/template" "log" "net/http" "os" @@ -37,20 +36,7 @@ func main() { mux := http.NewServeMux() mux.Handle("/web/static/", http.StripPrefix("/web/static/", http.FileServer(http.Dir("web/static/")))) - numRows, err := db.CountEntries() - if err != nil { - log.Fatalln(err) - } - if numRows == 0 { - mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - template.Must(template.ParseFiles("web/templates/index.html", "web/templates/add-user.html")).Execute(w, nil) - }) - } else { - mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - template.Must(template.ParseFiles("web/templates/index.html", "web/templates/login.html")).Execute(w, nil) - }) - } - + mux.HandleFunc("/", ui.HomePage(db)) mux.HandleFunc("/rss/", ui.ShowRSS(feed)) mux.HandleFunc("GET /hub/", ui.ShowHub()) diff --git a/web/templates/add-user.html b/web/templates/add-user.html index eb558cd..7e7eb30 100644 --- a/web/templates/add-user.html +++ b/web/templates/add-user.html @@ -1,19 +1,19 @@ {{define "page-content"}}

Neuer Benutzer

- + - - + + - + - + - +