cpolis/cmd/ui/admin.go

128 lines
3.5 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-03 13:56:49 +01:00
*data.User
2024-03-01 12:25:53 +01:00
Msg string
}
2024-02-24 09:54:25 +01:00
2024-03-03 13:56:49 +01:00
func inputsEmpty(user *data.User, pass, pass2 string) bool {
2024-03-01 12:25:53 +01:00
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-03 13:56:49 +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-03 13:56:49 +01:00
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)
2024-02-24 09:54:25 +01:00
}
2024-02-24 10:28:12 +01:00
2024-03-03 13:56:49 +01:00
func AddUser(db *data.DB, s *data.CookieStore) 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-03 13:56:49 +01:00
User: &data.User{
2024-03-01 12:25:53 +01:00
UserName: r.PostFormValue("username"),
FirstName: r.PostFormValue("first-name"),
LastName: r.PostFormValue("last-name"),
2024-03-03 09:16:49 +01:00
Role: role,
2024-03-01 12:25:53 +01:00
},
}
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."
2024-03-02 09:09:55 +01:00
tmpl, err := template.ParseFiles("web/templates/add-user.html")
template.Must(tmpl, err).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-02 09:09:55 +01:00
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
}
2024-03-01 12:25:53 +01:00
id, _ := db.GetID(htmlData.UserName)
if id != 0 {
2024-03-02 09:09:55 +01:00
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
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."
2024-03-02 09:09:55 +01:00
tmpl, err := template.ParseFiles("web/templates/add-user.html")
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", htmlData)
return
2024-02-24 10:28:12 +01:00
}
2024-03-03 13:56:49 +01:00
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
}
}
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-03-03 13:56:49 +01:00
2024-03-02 09:09:55 +01:00
tmpl, err := template.ParseFiles("web/templates/hub.html")
2024-03-03 13:56:49 +01:00
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", 0)
2024-02-24 15:31:33 +01:00
}
}