cpolis/cmd/ui/admin.go

113 lines
3.1 KiB
Go

package ui
import (
"fmt"
"html/template"
"log"
"net/http"
"streifling.com/jason/cpolis/cmd/data"
)
type AddUserData struct {
User string
First string
Last string
Role string
Msg string
}
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
}
}
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)
}
}
func AddUser(db *data.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var writer, editor, admin bool
data := AddUserData{
User: r.PostFormValue("username"),
First: r.PostFormValue("first-name"),
Last: r.PostFormValue("last-name"),
Role: r.PostFormValue("role"),
}
pass := r.PostFormValue("password")
pass2 := r.PostFormValue("password2")
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
}
userString, stringLen, ok := checkUserStrings(data.User, data.First, data.Last)
if !ok {
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
}
id, _ := db.GetID(data.User)
if id != 0 {
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
}
if pass != pass2 {
data.Msg = "Die Passwörter stimmen nicht überein."
template.Must(template.ParseFiles("web/templates/add-user.html")).ExecuteTemplate(w, "page-content", data)
return
}
switch data.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", 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
}
if err := db.AddUser(data.User, pass, data.First, data.Last, writer, editor, admin); err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
template.Must(template.ParseFiles("web/templates/hub.html")).ExecuteTemplate(w, "page-content", nil)
}
}