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"
"streifling.com/jason/cpolis/cmd/data"
)
type AddUserData struct {
User string
First string
Last string
Role string
Msg string
}
2024-02-24 09:54:25 +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
}
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
}
}
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) {
var writer, editor, admin bool
2024-03-01 11:30:31 +01:00
htmlData := 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-03-01 11:30:31 +01:00
if inputsEmpty(htmlData.User, pass, pass2, htmlData.First, htmlData.Last, htmlData.Role) {
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 11:30:31 +01:00
userString, stringLen, ok := checkUserStrings(htmlData.User, htmlData.First, htmlData.Last)
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 11:30:31 +01:00
id, _ := db.GetID(htmlData.User)
if id != 0 {
2024-03-01 11:30:31 +01:00
htmlData.Msg = fmt.Sprint(htmlData.User, " ist bereits vergeben. Bitte anderen Benutzernamen wählen.")
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 11:30:31 +01:00
switch htmlData.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-03-01 11:30:31 +01:00
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
2024-02-24 10:28:12 +01:00
}
2024-03-01 11:30:31 +01:00
if err := db.AddUser(htmlData.User, pass, htmlData.First, htmlData.Last, writer, editor, admin); 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)
}
}