2024-07-13 13:58:36 +02:00
|
|
|
package frontend
|
2024-03-11 21:08:27 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"html/template"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
|
2024-07-13 14:09:11 +02:00
|
|
|
b "streifling.com/jason/cpolis/cmd/backend"
|
2024-03-11 21:08:27 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type UserData struct {
|
2024-07-13 14:09:11 +02:00
|
|
|
*b.User
|
2024-03-11 21:08:27 +01:00
|
|
|
Msg string
|
|
|
|
}
|
|
|
|
|
2024-07-13 14:09:11 +02:00
|
|
|
func checkUserStrings(user *b.User) (string, int, bool) {
|
2024-03-11 21:08:27 +01:00
|
|
|
userLen := 15
|
|
|
|
nameLen := 50
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-18 11:20:06 +02:00
|
|
|
func CreateUser(c *b.Config, s *b.CookieStore) http.HandlerFunc {
|
2024-03-29 09:07:17 +01:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2024-08-18 11:20:06 +02:00
|
|
|
if _, err := getSession(w, r, c, s); err != nil {
|
2024-10-04 10:35:32 +02:00
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
2024-08-18 11:20:06 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-03-29 09:07:17 +01:00
|
|
|
tmpl, err := template.ParseFiles(c.WebDir + "/templates/add-user.html")
|
2024-10-04 10:21:56 +02:00
|
|
|
if err = template.Must(tmpl, err).ExecuteTemplate(w, "page-content", nil); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2024-03-29 09:07:17 +01:00
|
|
|
}
|
2024-03-11 21:08:27 +01:00
|
|
|
}
|
|
|
|
|
2024-07-13 14:09:11 +02:00
|
|
|
func AddUser(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
2024-03-11 21:08:27 +01:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2024-08-18 11:20:06 +02:00
|
|
|
session, err := getSession(w, r, c, s)
|
|
|
|
if err != nil {
|
2024-10-04 10:35:32 +02:00
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
2024-08-18 11:20:06 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-03-11 21:08:27 +01:00
|
|
|
role, err := strconv.Atoi(r.PostFormValue("role"))
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
htmlData := UserData{
|
2024-07-13 14:09:11 +02:00
|
|
|
User: &b.User{
|
2024-03-11 21:08:27 +01:00
|
|
|
UserName: r.PostFormValue("username"),
|
|
|
|
FirstName: r.PostFormValue("first-name"),
|
|
|
|
LastName: r.PostFormValue("last-name"),
|
|
|
|
Role: role,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
pass := r.PostFormValue("password")
|
|
|
|
pass2 := r.PostFormValue("password2")
|
|
|
|
|
|
|
|
if len(htmlData.UserName) == 0 || len(htmlData.FirstName) == 0 ||
|
|
|
|
len(htmlData.LastName) == 0 || len(pass) == 0 || len(pass2) == 0 {
|
|
|
|
htmlData.Msg = "Alle Felder müssen ausgefüllt werden."
|
2024-03-29 09:07:17 +01:00
|
|
|
tmpl, err := template.ParseFiles(c.WebDir + "/templates/add-user.html")
|
2024-10-04 10:21:56 +02:00
|
|
|
if err = template.Must(tmpl, err).ExecuteTemplate(w, "page-content", htmlData); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2024-03-11 21:08:27 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
userString, stringLen, ok := checkUserStrings(htmlData.User)
|
|
|
|
if !ok {
|
|
|
|
htmlData.Msg = fmt.Sprint(userString, " ist zu lang. Maximal ",
|
|
|
|
stringLen, " Zeichen erlaubt.")
|
2024-03-29 09:07:17 +01:00
|
|
|
tmpl, err := template.ParseFiles(c.WebDir + "/templates/add-user.html")
|
2024-10-04 10:21:56 +02:00
|
|
|
if err = template.Must(tmpl, err).ExecuteTemplate(w, "page-content", htmlData); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2024-03-11 21:08:27 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
id, _ := db.GetID(htmlData.UserName)
|
|
|
|
if id != 0 {
|
|
|
|
htmlData.Msg = fmt.Sprint(htmlData.UserName,
|
|
|
|
" ist bereits vergeben. Bitte anderen Benutzernamen wählen.")
|
2024-03-29 09:07:17 +01:00
|
|
|
tmpl, err := template.ParseFiles(c.WebDir + "/templates/add-user.html")
|
2024-10-04 10:21:56 +02:00
|
|
|
if err = template.Must(tmpl, err).ExecuteTemplate(w, "page-content", htmlData); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2024-03-11 21:08:27 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if pass != pass2 {
|
|
|
|
htmlData.Msg = "Die Passwörter stimmen nicht überein."
|
2024-03-29 09:07:17 +01:00
|
|
|
tmpl, err := template.ParseFiles(c.WebDir + "/templates/add-user.html")
|
2024-10-04 10:21:56 +02:00
|
|
|
if err = template.Must(tmpl, err).ExecuteTemplate(w, "page-content", htmlData); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2024-03-11 21:08:27 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-04-01 19:26:18 +02:00
|
|
|
_, err = db.AddUser(htmlData.User, pass)
|
2024-03-11 21:08:27 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2024-03-28 07:00:37 +01:00
|
|
|
|
2024-03-29 09:07:17 +01:00
|
|
|
tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html")
|
2024-04-12 08:46:34 +02:00
|
|
|
tmpl = template.Must(tmpl, err)
|
2024-10-04 10:21:56 +02:00
|
|
|
if err = tmpl.ExecuteTemplate(w, "page-content", session.Values["role"].(int)); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2024-03-11 21:08:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-13 14:09:11 +02:00
|
|
|
func EditSelf(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
2024-03-12 19:56:22 +01:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2024-08-18 11:20:06 +02:00
|
|
|
session, err := getSession(w, r, c, s)
|
2024-03-12 19:56:22 +01:00
|
|
|
if err != nil {
|
2024-10-04 10:35:32 +02:00
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
2024-08-18 11:20:06 +02:00
|
|
|
return
|
2024-03-12 19:56:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
user, err := db.GetUser(session.Values["id"].(int64))
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-04-09 19:06:29 +02:00
|
|
|
tmpl, err := template.ParseFiles(c.WebDir + "/templates/edit-self.html")
|
2024-10-04 10:21:56 +02:00
|
|
|
if err = template.Must(tmpl, err).ExecuteTemplate(w, "page-content", user); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2024-03-12 19:56:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-13 14:09:11 +02:00
|
|
|
func UpdateSelf(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
2024-03-11 21:08:27 +01:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2024-08-18 11:20:06 +02:00
|
|
|
session, err := getSession(w, r, c, s)
|
2024-03-11 21:08:27 +01:00
|
|
|
if err != nil {
|
2024-10-04 10:35:32 +02:00
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
2024-08-18 11:20:06 +02:00
|
|
|
return
|
2024-03-11 21:08:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
userData := UserData{
|
2024-07-13 14:09:11 +02:00
|
|
|
User: &b.User{
|
2024-03-11 21:08:27 +01:00
|
|
|
ID: session.Values["id"].(int64),
|
|
|
|
UserName: r.PostFormValue("username"),
|
|
|
|
FirstName: r.PostFormValue("first-name"),
|
|
|
|
LastName: r.PostFormValue("last-name"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
oldPass := r.PostFormValue("old-password")
|
|
|
|
newPass := r.PostFormValue("password")
|
|
|
|
newPass2 := r.PostFormValue("password2")
|
|
|
|
|
|
|
|
if len(userData.UserName) == 0 || len(userData.FirstName) == 0 ||
|
|
|
|
len(userData.LastName) == 0 {
|
|
|
|
userData.Msg = "Alle Felder mit * müssen ausgefüllt sein."
|
2024-03-29 09:07:17 +01:00
|
|
|
tmpl, err := template.ParseFiles(c.WebDir + "/templates/edit-user.html")
|
2024-03-11 21:08:27 +01:00
|
|
|
tmpl = template.Must(tmpl, err)
|
2024-10-04 10:21:56 +02:00
|
|
|
if err = tmpl.ExecuteTemplate(w, "page-content", userData.Msg); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2024-03-11 21:08:27 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
userString, stringLen, ok := checkUserStrings(userData.User)
|
|
|
|
if !ok {
|
|
|
|
userData.Msg = fmt.Sprint(userString, " ist zu lang. Maximal ",
|
|
|
|
stringLen, " Zeichen erlaubt.")
|
2024-03-29 09:07:17 +01:00
|
|
|
tmpl, err := template.ParseFiles(c.WebDir + "/templates/edit-user.html")
|
2024-03-11 21:08:27 +01:00
|
|
|
tmpl = template.Must(tmpl, err)
|
2024-10-04 10:21:56 +02:00
|
|
|
if err = tmpl.ExecuteTemplate(w, "page-content", userData); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2024-03-11 21:08:27 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-03-12 19:56:22 +01:00
|
|
|
if id, ok := db.GetID(userData.UserName); ok {
|
|
|
|
if id != userData.ID {
|
|
|
|
userData.Msg = "Benutzername bereits vergeben."
|
|
|
|
userData.UserName = ""
|
2024-03-29 09:07:17 +01:00
|
|
|
tmpl, err := template.ParseFiles(c.WebDir + "/templates/edit-user.html")
|
2024-03-12 19:56:22 +01:00
|
|
|
tmpl = template.Must(tmpl, err)
|
2024-10-04 10:21:56 +02:00
|
|
|
if err = tmpl.ExecuteTemplate(w, "page-content", userData); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2024-03-12 19:56:22 +01:00
|
|
|
return
|
|
|
|
}
|
2024-03-11 21:08:27 +01:00
|
|
|
}
|
|
|
|
|
2024-04-09 19:06:29 +02:00
|
|
|
if err = db.UpdateOwnAttributes(
|
2024-03-15 18:37:24 +01:00
|
|
|
userData.ID,
|
|
|
|
userData.UserName,
|
|
|
|
userData.FirstName,
|
|
|
|
userData.LastName,
|
|
|
|
oldPass,
|
|
|
|
newPass,
|
|
|
|
newPass2); err != nil {
|
|
|
|
userData.Msg = "Aktualisierung der Benutzerdaten fehlgeschlagen."
|
2024-03-29 09:07:17 +01:00
|
|
|
tmpl, err := template.ParseFiles(c.WebDir + "/templates/edit-user.html")
|
2024-10-04 10:21:56 +02:00
|
|
|
if err = template.Must(tmpl, err).ExecuteTemplate(w, "page-content", userData); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2024-03-11 21:08:27 +01:00
|
|
|
}
|
|
|
|
|
2024-03-29 09:07:17 +01:00
|
|
|
tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html")
|
2024-03-11 21:08:27 +01:00
|
|
|
tmpl = template.Must(tmpl, err)
|
2024-10-04 10:21:56 +02:00
|
|
|
if err = tmpl.ExecuteTemplate(w, "page-content", session.Values["role"].(int)); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2024-03-11 21:08:27 +01:00
|
|
|
}
|
|
|
|
}
|
2024-04-01 19:26:18 +02:00
|
|
|
|
2024-07-13 14:09:11 +02:00
|
|
|
func AddFirstUser(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
2024-04-01 19:26:18 +02:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2024-08-18 11:20:06 +02:00
|
|
|
var err error
|
2024-04-01 19:26:18 +02:00
|
|
|
htmlData := UserData{
|
2024-07-13 14:09:11 +02:00
|
|
|
User: &b.User{
|
2024-04-01 19:26:18 +02:00
|
|
|
UserName: r.PostFormValue("username"),
|
|
|
|
FirstName: r.PostFormValue("first-name"),
|
|
|
|
LastName: r.PostFormValue("last-name"),
|
2024-07-13 14:09:11 +02:00
|
|
|
Role: b.Admin,
|
2024-04-01 19:26:18 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
pass := r.PostFormValue("password")
|
|
|
|
pass2 := r.PostFormValue("password2")
|
|
|
|
|
|
|
|
if len(htmlData.UserName) == 0 || len(htmlData.FirstName) == 0 ||
|
|
|
|
len(htmlData.LastName) == 0 || len(pass) == 0 || len(pass2) == 0 {
|
|
|
|
htmlData.Msg = "Alle Felder müssen ausgefüllt werden."
|
|
|
|
tmpl, err := template.ParseFiles(c.WebDir + "/templates/add-user.html")
|
2024-10-04 10:21:56 +02:00
|
|
|
if err = template.Must(tmpl, err).ExecuteTemplate(w, "page-content", htmlData); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2024-04-01 19:26:18 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
userString, stringLen, ok := checkUserStrings(htmlData.User)
|
|
|
|
if !ok {
|
|
|
|
htmlData.Msg = fmt.Sprint(userString, " ist zu lang. Maximal ",
|
|
|
|
stringLen, " Zeichen erlaubt.")
|
|
|
|
tmpl, err := template.ParseFiles(c.WebDir + "/templates/add-user.html")
|
2024-10-04 10:21:56 +02:00
|
|
|
if err = template.Must(tmpl, err).ExecuteTemplate(w, "page-content", htmlData); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2024-04-01 19:26:18 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
id, _ := db.GetID(htmlData.UserName)
|
|
|
|
if id != 0 {
|
|
|
|
htmlData.Msg = fmt.Sprint(htmlData.UserName,
|
|
|
|
" ist bereits vergeben. Bitte anderen Benutzernamen wählen.")
|
|
|
|
tmpl, err := template.ParseFiles(c.WebDir + "/templates/add-user.html")
|
2024-10-04 10:21:56 +02:00
|
|
|
if err = template.Must(tmpl, err).ExecuteTemplate(w, "page-content", htmlData); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2024-04-01 19:26:18 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if pass != pass2 {
|
|
|
|
htmlData.Msg = "Die Passwörter stimmen nicht überein."
|
|
|
|
tmpl, err := template.ParseFiles(c.WebDir + "/templates/add-user.html")
|
2024-10-04 10:21:56 +02:00
|
|
|
if err = template.Must(tmpl, err).ExecuteTemplate(w, "page-content", htmlData); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2024-04-01 19:26:18 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-04-02 19:37:53 +02:00
|
|
|
htmlData.ID, err = db.AddFirstUser(htmlData.User, pass)
|
2024-04-01 19:26:18 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2024-04-02 19:37:53 +02:00
|
|
|
if htmlData.ID > 1 {
|
|
|
|
errString := "error: there is already a first user"
|
|
|
|
log.Println(errString)
|
|
|
|
http.Error(w, errString, http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2024-04-01 19:26:18 +02:00
|
|
|
|
|
|
|
if err := saveSession(w, r, s, htmlData.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
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html")
|
2024-10-04 10:21:56 +02:00
|
|
|
if err = template.Must(tmpl, err).ExecuteTemplate(w, "page-content", 0); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2024-04-01 19:26:18 +02:00
|
|
|
}
|
|
|
|
}
|
2024-04-09 19:06:29 +02:00
|
|
|
|
2024-07-13 14:09:11 +02:00
|
|
|
func ShowAllUsers(c *b.Config, db *b.DB, s *b.CookieStore, action string) http.HandlerFunc {
|
2024-04-09 19:06:29 +02:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2024-08-18 11:20:06 +02:00
|
|
|
session, err := getSession(w, r, c, s)
|
|
|
|
if err != nil {
|
2024-10-04 10:35:32 +02:00
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
2024-08-18 11:20:06 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-09-10 19:59:56 +02:00
|
|
|
data := new(struct {
|
2024-07-13 14:09:11 +02:00
|
|
|
Users map[int64]*b.User
|
2024-04-12 08:46:34 +02:00
|
|
|
Action string
|
2024-09-10 19:59:56 +02:00
|
|
|
})
|
2024-04-12 08:46:34 +02:00
|
|
|
|
2024-09-10 19:59:56 +02:00
|
|
|
data.Action = action
|
2024-04-12 08:46:34 +02:00
|
|
|
data.Users, err = db.GetAllUsers()
|
2024-04-09 19:06:29 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-04-12 08:46:34 +02:00
|
|
|
delete(data.Users, session.Values["id"].(int64))
|
2024-04-09 19:06:29 +02:00
|
|
|
tmpl, err := template.ParseFiles(c.WebDir + "/templates/show-all-users.html")
|
2024-10-04 10:21:56 +02:00
|
|
|
if err = template.Must(tmpl, err).ExecuteTemplate(w, "page-content", data); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2024-04-09 19:06:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-18 11:20:06 +02:00
|
|
|
func EditUser(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
2024-04-09 19:06:29 +02:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2024-08-18 11:20:06 +02:00
|
|
|
if _, err := getSession(w, r, c, s); err != nil {
|
2024-10-04 10:35:32 +02:00
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
2024-08-18 11:20:06 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-04-09 19:06:29 +02:00
|
|
|
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(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")
|
2024-10-04 10:21:56 +02:00
|
|
|
if err = template.Must(tmpl, err).ExecuteTemplate(w, "page-content", user); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2024-04-09 19:06:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-13 14:09:11 +02:00
|
|
|
func UpdateUser(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
2024-04-09 19:06:29 +02:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2024-08-18 11:20:06 +02:00
|
|
|
session, err := getSession(w, r, c, s)
|
|
|
|
if err != nil {
|
2024-10-04 10:35:32 +02:00
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
2024-08-18 11:20:06 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-04-09 19:06:29 +02:00
|
|
|
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
role, err := strconv.Atoi(r.PostFormValue("role"))
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
userData := UserData{
|
2024-07-13 14:09:11 +02:00
|
|
|
User: &b.User{
|
2024-04-09 19:06:29 +02:00
|
|
|
ID: id,
|
|
|
|
UserName: r.PostFormValue("username"),
|
|
|
|
FirstName: r.PostFormValue("first-name"),
|
|
|
|
LastName: r.PostFormValue("last-name"),
|
|
|
|
Role: role,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
newPass := r.PostFormValue("password")
|
|
|
|
newPass2 := r.PostFormValue("password2")
|
|
|
|
|
|
|
|
if len(userData.UserName) == 0 || len(userData.FirstName) == 0 ||
|
|
|
|
len(userData.LastName) == 0 {
|
|
|
|
userData.Msg = "Alle Felder mit * müssen ausgefüllt sein."
|
|
|
|
tmpl, err := template.ParseFiles(c.WebDir + "/templates/edit-user.html")
|
|
|
|
tmpl = template.Must(tmpl, err)
|
2024-10-04 10:21:56 +02:00
|
|
|
if err = tmpl.ExecuteTemplate(w, "page-content", userData.Msg); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2024-04-09 19:06:29 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
userString, stringLen, ok := checkUserStrings(userData.User)
|
|
|
|
if !ok {
|
|
|
|
userData.Msg = fmt.Sprint(userString, " ist zu lang. Maximal ",
|
|
|
|
stringLen, " Zeichen erlaubt.")
|
|
|
|
tmpl, err := template.ParseFiles(c.WebDir + "/templates/edit-user.html")
|
|
|
|
tmpl = template.Must(tmpl, err)
|
2024-10-04 10:21:56 +02:00
|
|
|
if err = tmpl.ExecuteTemplate(w, "page-content", userData); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2024-04-09 19:06:29 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if id, ok := db.GetID(userData.UserName); ok {
|
|
|
|
if id != userData.ID {
|
|
|
|
userData.Msg = "Benutzername bereits vergeben."
|
|
|
|
userData.UserName = ""
|
|
|
|
tmpl, err := template.ParseFiles(c.WebDir + "/templates/edit-user.html")
|
|
|
|
tmpl = template.Must(tmpl, err)
|
2024-10-04 10:21:56 +02:00
|
|
|
if err = tmpl.ExecuteTemplate(w, "page-content", userData); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2024-04-09 19:06:29 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = db.UpdateUserAttributes(
|
|
|
|
userData.ID,
|
|
|
|
userData.UserName,
|
|
|
|
userData.FirstName,
|
|
|
|
userData.LastName,
|
|
|
|
newPass,
|
|
|
|
newPass2,
|
|
|
|
userData.Role); err != nil {
|
|
|
|
userData.Msg = "Aktualisierung der Benutzerdaten fehlgeschlagen."
|
|
|
|
tmpl, err := template.ParseFiles(c.WebDir + "/templates/edit-user.html")
|
2024-10-04 10:21:56 +02:00
|
|
|
if err = template.Must(tmpl, err).ExecuteTemplate(w, "page-content", userData); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2024-04-09 19:06:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html")
|
|
|
|
tmpl = template.Must(tmpl, err)
|
2024-10-04 10:21:56 +02:00
|
|
|
if err = tmpl.ExecuteTemplate(w, "page-content", session.Values["role"].(int)); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2024-04-12 08:46:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-13 14:09:11 +02:00
|
|
|
func DeleteUser(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
|
2024-04-12 08:46:34 +02:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2024-08-18 11:20:06 +02:00
|
|
|
session, err := getSession(w, r, c, s)
|
|
|
|
if err != nil {
|
2024-10-04 10:35:32 +02:00
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
2024-08-18 11:20:06 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-04-12 08:46:34 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html")
|
|
|
|
tmpl = template.Must(tmpl, err)
|
2024-10-04 10:21:56 +02:00
|
|
|
if err = tmpl.ExecuteTemplate(w, "page-content", session.Values["role"].(int)); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2024-04-09 19:06:29 +02:00
|
|
|
}
|
|
|
|
}
|