475 lines
14 KiB
Go
475 lines
14 KiB
Go
package frontend
|
|
|
|
import (
|
|
"fmt"
|
|
"html/template"
|
|
"log"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
b "streifling.com/jason/cpolis/cmd/backend"
|
|
)
|
|
|
|
func checkUserStrings(user *b.User) (string, int, bool) {
|
|
userLen := 255
|
|
nameLen := 56 // max value when aes encrypting up to 255 bytes
|
|
|
|
if len(user.UserName) > userLen {
|
|
return "Benutzername", userLen, false
|
|
} else if len(user.FirstName) > nameLen {
|
|
return "Vorname", nameLen, false
|
|
} else if len(user.LastName) > nameLen {
|
|
return "Nachname", nameLen, false
|
|
} else {
|
|
return "", 0, true
|
|
}
|
|
}
|
|
|
|
func CreateUser(c *b.Config, s *b.CookieStore) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
if _, err := getSession(w, r, c, s); err != nil {
|
|
log.Println(err)
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
tmpl, err := template.ParseFiles(c.WebDir + "/templates/add-user.html")
|
|
if err = template.Must(tmpl, err).ExecuteTemplate(w, "page-content", nil); err != nil {
|
|
log.Println(err)
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
func AddUser(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
session, err := getSession(w, r, c, s)
|
|
if err != nil {
|
|
log.Println(err)
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
user := &b.User{
|
|
UserName: r.PostFormValue("username"),
|
|
FirstName: r.PostFormValue("first-name"),
|
|
LastName: r.PostFormValue("last-name"),
|
|
Email: r.PostFormValue("email"),
|
|
}
|
|
pass := r.PostFormValue("password")
|
|
pass2 := r.PostFormValue("password2")
|
|
email2 := r.PostFormValue("email2")
|
|
|
|
if len(user.UserName) == 0 || len(user.FirstName) == 0 || len(user.LastName) == 0 || len(user.Email) == 0 || len(email2) == 0 || len(pass) == 0 || len(pass2) == 0 {
|
|
http.Error(w, "Bitte alle Felder ausfüllen.", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
userString, stringLen, ok := checkUserStrings(user)
|
|
if !ok {
|
|
http.Error(w, fmt.Sprint(userString, " ist zu lang. Maximal ", stringLen, " Zeichen erlaubt."), http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
if id := db.GetID(user.UserName); id != 0 {
|
|
http.Error(w, user.UserName+" ist bereits vergeben. Bitte anderen Benutzernamen wählen.", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
if user.Email != email2 {
|
|
http.Error(w, "Die Emailadressen stimmen nicht überein.", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
if pass != pass2 {
|
|
http.Error(w, "Die Passwörter stimmen nicht überein.", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
roleString := r.PostFormValue("role")
|
|
if len(roleString) == 0 {
|
|
http.Error(w, "Bitte eine Aufgabe vergeben.", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
user.Role, err = strconv.Atoi(roleString)
|
|
if err != nil {
|
|
log.Println(err)
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
_, err = db.AddUser(c, user, pass)
|
|
if err != nil {
|
|
log.Println(err)
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
data := new(struct{ Role int })
|
|
data.Role = session.Values["role"].(int)
|
|
|
|
tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html")
|
|
tmpl = template.Must(tmpl, err)
|
|
if err = tmpl.ExecuteTemplate(w, "page-content", data); err != nil {
|
|
log.Println(err)
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
func EditSelf(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
session, err := getSession(w, r, c, s)
|
|
if err != nil {
|
|
log.Println(err)
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
user, err := db.GetUser(c, session.Values["id"].(int64))
|
|
if err != nil {
|
|
log.Println(err)
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
tmpl, err := template.ParseFiles(c.WebDir + "/templates/edit-self.html")
|
|
if err = template.Must(tmpl, err).ExecuteTemplate(w, "page-content", user); err != nil {
|
|
log.Println(err)
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
func UpdateSelf(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
session, err := getSession(w, r, c, s)
|
|
if err != nil {
|
|
log.Println(err)
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
user := &b.User{
|
|
ID: session.Values["id"].(int64),
|
|
UserName: r.PostFormValue("username"),
|
|
FirstName: r.PostFormValue("first-name"),
|
|
LastName: r.PostFormValue("last-name"),
|
|
Email: r.PostFormValue("email"),
|
|
}
|
|
|
|
oldPass := r.PostFormValue("old-password")
|
|
newPass := r.PostFormValue("password")
|
|
newPass2 := r.PostFormValue("password2")
|
|
email2 := r.PostFormValue("email2")
|
|
|
|
if len(user.UserName) == 0 {
|
|
http.Error(w, "Bitte den Benutzernamen ausfüllen.", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
if len(user.FirstName) == 0 || len(user.LastName) == 0 {
|
|
http.Error(w, "Bitte den vollständigen Namen ausfüllen.", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
if len(user.Email) == 0 || len(email2) == 0 {
|
|
http.Error(w, "Bitte die Emailadresse ausfüllen.", http.StatusBadRequest)
|
|
return
|
|
}
|
|
if user.Email != email2 {
|
|
http.Error(w, "Die Emailadressen stimmen nicht überein", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
if newPass != newPass2 {
|
|
http.Error(w, "Die Passwörter stimmen nicht überein", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
userString, stringLen, ok := checkUserStrings(user)
|
|
if !ok {
|
|
http.Error(w, fmt.Sprint(userString, " ist zu lang. Maximal ", stringLen, " Zeichen erlaubt."), http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
if id := db.GetID(user.UserName); id != 0 && id != user.ID {
|
|
http.Error(w, user.UserName+" ist bereits vergeben. Bitte anderen Benutzernamen wählen.", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
if err = db.UpdateOwnUserAttributes(c, user.ID, user.UserName, user.FirstName, user.LastName, user.Email, oldPass, newPass); err != nil {
|
|
log.Println("error: user:", user.ID, err)
|
|
http.Error(w, "Benutzerdaten konnten nicht aktualisiert werden.", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
data := new(struct{ Role int })
|
|
data.Role = session.Values["role"].(int)
|
|
|
|
tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html")
|
|
tmpl = template.Must(tmpl, err)
|
|
if err = tmpl.ExecuteTemplate(w, "page-content", data); err != nil {
|
|
log.Println(err)
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
func AddFirstUser(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
var err error
|
|
user := &b.User{
|
|
UserName: r.PostFormValue("username"),
|
|
FirstName: r.PostFormValue("first-name"),
|
|
LastName: r.PostFormValue("last-name"),
|
|
Email: r.PostFormValue("email"),
|
|
Role: b.Admin,
|
|
}
|
|
pass := r.PostFormValue("password")
|
|
pass2 := r.PostFormValue("password2")
|
|
email2 := r.PostFormValue("email2")
|
|
|
|
if len(user.UserName) == 0 || len(user.FirstName) == 0 || len(user.LastName) == 0 || len(user.Email) == 0 || len(email2) == 0 || len(pass) == 0 || len(pass2) == 0 {
|
|
http.Error(w, "Bitte alle Felder ausfüllen.", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
userString, stringLen, ok := checkUserStrings(user)
|
|
if !ok {
|
|
http.Error(w, fmt.Sprint(userString, " ist zu lang. Maximal ", stringLen, " Zeichen erlaubt."), http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
if user.Email != email2 {
|
|
http.Error(w, "Die Emailadressen stimmen nicht überein.", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
if pass != pass2 {
|
|
http.Error(w, "Die Passwörter stimmen nicht überein.", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
user.ID, err = db.AddFirstUser(c, user, pass)
|
|
if err != nil {
|
|
log.Println(err)
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
if user.ID == -1 {
|
|
http.Error(w, "Bitte ein Benutzerkonto von einem Administrator anlegen lassen.", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
if err := saveSession(w, r, s, user); err != nil {
|
|
log.Println(err)
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
if _, err := db.AddIssue(); err != nil {
|
|
log.Println(err)
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
data := new(struct{ Role int })
|
|
data.Role = 0
|
|
|
|
tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html")
|
|
if err = template.Must(tmpl, err).ExecuteTemplate(w, "page-content", data); err != nil {
|
|
log.Println(err)
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
func ShowAllUsers(c *b.Config, db *b.DB, s *b.CookieStore, action string) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
session, err := getSession(w, r, c, s)
|
|
if err != nil {
|
|
log.Println(err)
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
data := new(struct {
|
|
Users map[int64]*b.User
|
|
Action string
|
|
})
|
|
|
|
data.Action = action
|
|
data.Users, err = db.GetAllUsers(c)
|
|
if err != nil {
|
|
log.Println(err)
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
delete(data.Users, session.Values["id"].(int64))
|
|
tmpl, err := template.ParseFiles(c.WebDir + "/templates/show-all-users.html")
|
|
if err = template.Must(tmpl, err).ExecuteTemplate(w, "page-content", data); err != nil {
|
|
log.Println(err)
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
func EditUser(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
if _, err := getSession(w, r, c, s); err != nil {
|
|
log.Println(err)
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
|
if err != nil {
|
|
log.Println(err)
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
user, err := db.GetUser(c, id)
|
|
if err != nil {
|
|
log.Println(err)
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
tmpl, err := template.ParseFiles(c.WebDir + "/templates/edit-user.html")
|
|
if err = template.Must(tmpl, err).ExecuteTemplate(w, "page-content", user); err != nil {
|
|
log.Println(err)
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
func UpdateUser(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
session, err := getSession(w, r, c, s)
|
|
if err != nil {
|
|
log.Println(err)
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
user := new(b.User)
|
|
user.ID, err = strconv.ParseInt(r.PathValue("id"), 10, 64)
|
|
if err != nil {
|
|
log.Println(err)
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
user.Role, err = strconv.Atoi(r.PostFormValue("role"))
|
|
if err != nil {
|
|
log.Println(err)
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
user.UserName = r.PostFormValue("username")
|
|
if len(user.UserName) == 0 {
|
|
http.Error(w, "Bitte den Benutzernamen ausfüllen.", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
user.FirstName = r.PostFormValue("first-name")
|
|
user.LastName = r.PostFormValue("last-name")
|
|
if len(user.FirstName) == 0 || len(user.LastName) == 0 {
|
|
http.Error(w, "Bitte den vollständigen Namen ausfüllen.", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
user.Email = r.PostFormValue("email")
|
|
email2 := r.PostFormValue("email2")
|
|
if len(user.Email) == 0 || len(email2) == 0 {
|
|
http.Error(w, "Bitte die Emailadresse ausfüllen.", http.StatusBadRequest)
|
|
return
|
|
}
|
|
if user.Email != email2 {
|
|
http.Error(w, "Die Emailadressen stimmen nicht überein.", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
newPass := r.PostFormValue("password")
|
|
newPass2 := r.PostFormValue("password2")
|
|
if newPass != newPass2 {
|
|
http.Error(w, "Die Passwörter stimmen nicht überein.", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
userString, stringLen, ok := checkUserStrings(user)
|
|
if !ok {
|
|
http.Error(w, fmt.Sprint(userString, " ist zu lang. Maximal ", stringLen, " Zeichen erlaubt."), http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
if id := db.GetID(user.UserName); id != 0 && id != user.ID {
|
|
http.Error(w, user.UserName+" ist bereits vergeben. Bitte anderen Benutzernamen wählen.", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
if err = db.UpdateUserAttributes(c, user.ID, user.UserName, user.FirstName, user.LastName, user.Email, newPass, user.Role); err != nil {
|
|
log.Println("error: user:", user.ID, err)
|
|
http.Error(w, "Benutzerdaten konnten nicht aktualisiert werden.", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
data := new(struct{ Role int })
|
|
data.Role = session.Values["role"].(int)
|
|
|
|
tmpl := template.Must(template.ParseFiles(c.WebDir + "/templates/hub.html"))
|
|
if err = tmpl.ExecuteTemplate(w, "page-content", data); err != nil {
|
|
log.Println(err)
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
func DeleteUser(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
session, err := getSession(w, r, c, s)
|
|
if err != nil {
|
|
log.Println(err)
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
|
if err != nil {
|
|
log.Println(err)
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
if err = db.DeleteUser(id); err != nil {
|
|
log.Println(err)
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
data := new(struct{ Role int })
|
|
data.Role = session.Values["role"].(int)
|
|
|
|
tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html")
|
|
tmpl = template.Must(tmpl, err)
|
|
if err = tmpl.ExecuteTemplate(w, "page-content", data); err != nil {
|
|
log.Println(err)
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
}
|
|
}
|