2024-02-24 09:54:25 +01:00
|
|
|
package ui
|
|
|
|
|
|
|
|
import (
|
2024-02-24 13:25:32 +01:00
|
|
|
"fmt"
|
2024-02-24 09:54:25 +01:00
|
|
|
"html/template"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"streifling.com/jason/cpolis/cmd/data"
|
|
|
|
)
|
|
|
|
|
2024-02-27 09:03:21 +01:00
|
|
|
type AddUserData struct {
|
|
|
|
User string
|
|
|
|
First string
|
|
|
|
Last string
|
|
|
|
Role string
|
|
|
|
Msg string
|
|
|
|
}
|
2024-02-24 09:54:25 +01:00
|
|
|
|
2024-02-27 09:03:21 +01:00
|
|
|
func inputsEmpty(user, pass, pass2, first, last, role string) bool {
|
|
|
|
return len(user) == 0 ||
|
|
|
|
len(pass) == 0 ||
|
|
|
|
len(pass2) == 0 ||
|
|
|
|
len(first) == 0 ||
|
|
|
|
len(last) == 0 ||
|
|
|
|
len(role) == 0
|
|
|
|
}
|
2024-02-24 13:25:32 +01:00
|
|
|
|
2024-02-27 09:03:21 +01:00
|
|
|
func checkUserStrings(user, first, last string) (string, int, bool) {
|
|
|
|
userLen := 15
|
|
|
|
nameLen := 50
|
|
|
|
|
|
|
|
if len(user) > userLen {
|
|
|
|
return "Benutzername", userLen, false
|
|
|
|
} else if len(first) > nameLen {
|
|
|
|
return "Vorname", nameLen, false
|
|
|
|
} else if len(last) > nameLen {
|
|
|
|
return "Nachname", nameLen, false
|
|
|
|
} else {
|
|
|
|
return "", 0, true
|
2024-02-24 09:54:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-27 09:03:21 +01:00
|
|
|
func CreateUser() http.HandlerFunc {
|
2024-02-24 09:54:25 +01:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2024-02-27 09:03:21 +01:00
|
|
|
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) {
|
|
|
|
var writer, editor, admin bool
|
|
|
|
|
2024-02-24 13:25:32 +01:00
|
|
|
data := AddUserData{
|
|
|
|
User: r.PostFormValue("username"),
|
|
|
|
First: r.PostFormValue("first-name"),
|
|
|
|
Last: r.PostFormValue("last-name"),
|
|
|
|
Role: r.PostFormValue("role"),
|
|
|
|
}
|
2024-02-24 10:28:12 +01:00
|
|
|
pass := r.PostFormValue("password")
|
|
|
|
pass2 := r.PostFormValue("password2")
|
|
|
|
|
2024-02-24 13:25:32 +01:00
|
|
|
if inputsEmpty(data.User, pass, pass2, data.First, data.Last, data.Role) {
|
|
|
|
data.Msg = "Alle Felder müssen ausgefüllt werden."
|
|
|
|
template.Must(template.ParseFiles("web/templates/add-user.html")).ExecuteTemplate(w, "page-content", data)
|
|
|
|
return
|
2024-02-24 12:10:34 +01:00
|
|
|
}
|
2024-02-24 13:25:32 +01:00
|
|
|
userString, stringLen, ok := checkUserStrings(data.User, data.First, data.Last)
|
2024-02-24 10:28:12 +01:00
|
|
|
if !ok {
|
2024-02-24 13:25:32 +01:00
|
|
|
data.Msg = fmt.Sprint(userString, " ist zu lang. Maximal ", stringLen, " Zeichen erlaubt.")
|
|
|
|
template.Must(template.ParseFiles("web/templates/add-user.html")).ExecuteTemplate(w, "page-content", data)
|
|
|
|
return
|
2024-02-24 10:56:12 +01:00
|
|
|
}
|
2024-02-24 13:25:32 +01:00
|
|
|
id, _ := db.GetID(data.User)
|
2024-02-24 10:56:12 +01:00
|
|
|
if id != 0 {
|
2024-02-24 13:25:32 +01:00
|
|
|
data.Msg = fmt.Sprint(data.User, " ist bereits vergeben. Bitte anderen Benutzernamen wählen.")
|
|
|
|
template.Must(template.ParseFiles("web/templates/add-user.html")).ExecuteTemplate(w, "page-content", data)
|
|
|
|
return
|
2024-02-24 10:28:12 +01:00
|
|
|
}
|
|
|
|
if pass != pass2 {
|
2024-02-24 13:25:32 +01:00
|
|
|
data.Msg = "Die Passwörter stimmen nicht überein."
|
|
|
|
template.Must(template.ParseFiles("web/templates/add-user.html")).ExecuteTemplate(w, "page-content", data)
|
|
|
|
return
|
2024-02-24 10:28:12 +01:00
|
|
|
}
|
2024-02-24 13:25:32 +01:00
|
|
|
switch data.Role {
|
2024-02-24 10:28:12 +01:00
|
|
|
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:
|
2024-02-24 13:25:32 +01:00
|
|
|
log.Println("When setting up", data.User, "the HTML was altered.")
|
|
|
|
data.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", data)
|
|
|
|
return
|
2024-02-24 10:28:12 +01:00
|
|
|
}
|
|
|
|
|
2024-02-24 13:25:32 +01:00
|
|
|
if err := db.AddUser(data.User, pass, data.First, data.Last, writer, editor, admin); err != nil {
|
2024-02-24 10:56:12 +01:00
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
2024-02-24 11:41:01 +01:00
|
|
|
return
|
2024-02-24 10:56:12 +01:00
|
|
|
}
|
2024-02-24 15:31:33 +01:00
|
|
|
template.Must(template.ParseFiles("web/templates/hub.html")).ExecuteTemplate(w, "page-content", nil)
|
|
|
|
}
|
|
|
|
}
|