cpolis/cmd/ui/admin.go

113 lines
3.2 KiB
Go
Raw Normal View History

2024-02-24 09:54:25 +01:00
package ui
import (
"fmt"
2024-02-24 09:54:25 +01:00
"html/template"
"log"
"net/http"
2024-03-01 12:25:53 +01:00
"strconv"
2024-02-24 09:54:25 +01:00
"streifling.com/jason/cpolis/cmd/data"
)
type AddUserData struct {
2024-03-01 12:25:53 +01:00
data.User
Msg string
}
2024-02-24 09:54:25 +01:00
2024-03-01 12:25:53 +01:00
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 ||
2024-03-01 12:25:53 +01:00
len(pass2) == 0
}
2024-03-01 12:25:53 +01:00
func checkUserStrings(user data.User) (string, int, bool) {
userLen := 15
nameLen := 50
2024-03-01 12:25:53 +01:00
if len(user.UserName) > userLen {
return "Benutzername", userLen, false
2024-03-01 12:25:53 +01:00
} else if len(user.FirstName) > nameLen {
return "Vorname", nameLen, false
2024-03-01 12:25:53 +01:00
} else if len(user.LastName) > nameLen {
return "Nachname", nameLen, false
} else {
return "", 0, true
2024-02-24 09:54:25 +01:00
}
}
2024-03-01 12:25:53 +01:00
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 {
2024-02-24 09:54:25 +01:00
return func(w http.ResponseWriter, r *http.Request) {
template.Must(template.ParseFiles("web/templates/add-user.html")).ExecuteTemplate(w, "page-content", nil)
2024-02-24 09:54:25 +01:00
}
}
2024-02-24 10:28:12 +01:00
2024-02-24 14:49:29 +01:00
func AddUser(db *data.DB) http.HandlerFunc {
2024-02-24 10:28:12 +01:00
return func(w http.ResponseWriter, r *http.Request) {
2024-03-01 12:25:53 +01:00
role, err := strconv.Atoi(r.PostFormValue("role"))
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
2024-02-24 10:28:12 +01:00
2024-03-01 11:30:31 +01:00
htmlData := AddUserData{
2024-03-01 12:25:53 +01:00
User: data.User{
UserName: r.PostFormValue("username"),
FirstName: r.PostFormValue("first-name"),
LastName: r.PostFormValue("last-name"),
Role: data.Role(role),
},
}
2024-02-24 10:28:12 +01:00
pass := r.PostFormValue("password")
pass2 := r.PostFormValue("password2")
2024-03-01 12:25:53 +01:00
if inputsEmpty(htmlData.User, pass, pass2) {
2024-03-01 11:30:31 +01:00
htmlData.Msg = "Alle Felder müssen ausgefüllt werden."
template.Must(template.ParseFiles("web/templates/add-user.html")).ExecuteTemplate(w, "page-content", htmlData)
return
}
2024-03-01 12:25:53 +01:00
userString, stringLen, ok := checkUserStrings(htmlData.User)
2024-02-24 10:28:12 +01:00
if !ok {
2024-03-01 11:30:31 +01:00
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
}
2024-03-01 12:25:53 +01:00
id, _ := db.GetID(htmlData.UserName)
if id != 0 {
2024-03-01 12:25:53 +01:00
htmlData.Msg = fmt.Sprint(htmlData.UserName, " ist bereits vergeben. Bitte anderen Benutzernamen wählen.")
2024-03-01 11:30:31 +01:00
template.Must(template.ParseFiles("web/templates/add-user.html")).ExecuteTemplate(w, "page-content", htmlData)
return
2024-02-24 10:28:12 +01:00
}
if pass != pass2 {
2024-03-01 11:30:31 +01:00
htmlData.Msg = "Die Passwörter stimmen nicht überein."
template.Must(template.ParseFiles("web/templates/add-user.html")).ExecuteTemplate(w, "page-content", htmlData)
return
2024-02-24 10:28:12 +01:00
}
2024-03-01 12:25:53 +01:00
if err := db.AddUser(htmlData.User, pass); err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
2024-02-24 11:41:01 +01:00
return
}
2024-02-24 15:31:33 +01:00
template.Must(template.ParseFiles("web/templates/hub.html")).ExecuteTemplate(w, "page-content", nil)
}
}