CHANGE: added report section

This commit is contained in:
2025-02-26 15:05:50 +01:00
parent 0ddca8b9ce
commit 54104d5a0e
15 changed files with 376 additions and 96 deletions

View File

@@ -1,6 +1,7 @@
package endpoints
import (
"arbeitszeitmessung/helper"
"arbeitszeitmessung/models"
"arbeitszeitmessung/templates"
"log"
@@ -18,27 +19,26 @@ func CreateSessionManager(lifetime time.Duration) *scs.SessionManager {
Session.Lifetime = lifetime
return Session
}
func LoginHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
showLoginForm(w, r, false)
showLoginPage(w, r, false)
break
case http.MethodPost:
loginUser(w, r)
break
default:
showLoginForm(w, r, false)
http.Error(w, "Method not allowed!", http.StatusMethodNotAllowed)
break
}
}
func UserHandler(w http.ResponseWriter, r *http.Request) {
// if !Session.Exists(r.Context(), "user") {
// http.Redirect(w, r, "/user/login", http.StatusSeeOther)
// }
helper.RequiresLogin(Session, w, r)
switch r.Method {
case http.MethodGet:
showPWForm(w, r, 0)
showUserPage(w, r, 0)
break
case http.MethodPost:
changePassword(w, r)
@@ -49,7 +49,7 @@ func UserHandler(w http.ResponseWriter, r *http.Request) {
}
}
func showLoginForm(w http.ResponseWriter, r *http.Request, failed bool) {
func showLoginPage(w http.ResponseWriter, r *http.Request, failed bool) {
templates.LoginPage(failed).Render(r.Context(), w)
}
@@ -85,10 +85,10 @@ func loginUser(w http.ResponseWriter, r *http.Request) {
Session.Put(r.Context(), "user", user.PersonalNummer)
http.Redirect(w, r, "/time", http.StatusSeeOther) //with this browser always uses GET
} else {
showLoginForm(w, r, true)
showLoginPage(w, r, true)
return
}
showLoginForm(w, r, false)
showLoginPage(w, r, false)
return
}
@@ -103,26 +103,26 @@ func changePassword(w http.ResponseWriter, r *http.Request) {
password := r.FormValue("password")
newPassword := r.FormValue("new_password")
if password == "" || newPassword == "" || newPassword != r.FormValue("new_password_repeat") {
showPWForm(w, r, http.StatusBadRequest)
showUserPage(w, r, http.StatusBadRequest)
return
}
user, err := (*models.User).GetByPersonalNummer(nil, Session.GetInt(r.Context(), "user"))
if err != nil {
log.Println("Error getting user!", err)
showPWForm(w, r, http.StatusBadRequest)
showUserPage(w, r, http.StatusBadRequest)
}
auth, err := user.ChangePass(password, newPassword)
if err != nil {
log.Println("Error when changing password!", err)
}
if auth {
showPWForm(w, r, http.StatusOK)
showUserPage(w, r, http.StatusOK)
return
}
showPWForm(w, r, http.StatusUnauthorized)
showUserPage(w, r, http.StatusUnauthorized)
}
func showPWForm(w http.ResponseWriter, r *http.Request, status int) {
func showUserPage(w http.ResponseWriter, r *http.Request, status int) {
templates.UserPage(status).Render(r.Context(), w)
return
}