2024-02-22 10:18:59 +01:00
|
|
|
package ui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2024-02-22 20:52:37 +01:00
|
|
|
|
|
|
|
"github.com/gin-contrib/sessions"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
|
|
|
"git.jmbit.de/jmb/goipam/db"
|
|
|
|
"git.jmbit.de/jmb/goipam/utils"
|
|
|
|
"git.jmbit.de/jmb/goipam/web/templates"
|
2024-02-22 10:18:59 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func getProfile(c *gin.Context) {
|
2024-02-22 20:52:37 +01:00
|
|
|
session := sessions.Default(c)
|
|
|
|
sessionUserName := session.Get("username")
|
|
|
|
var user db.User
|
|
|
|
if username, ok := sessionUserName.(string); ok {
|
|
|
|
|
|
|
|
user, err := db.GetUserByName(username)
|
|
|
|
if err != nil {
|
|
|
|
c.HTML(
|
|
|
|
http.StatusInternalServerError,
|
|
|
|
"",
|
2024-02-23 11:43:23 +01:00
|
|
|
templates.ProfilePage(templates.GenMetaContent(c), "User Profile", &user, err,
|
2024-02-22 20:52:37 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.HTML(
|
|
|
|
http.StatusOK,
|
|
|
|
"",
|
2024-02-23 11:43:23 +01:00
|
|
|
templates.ProfilePage(templates.GenMetaContent(c), "User Profile", &user, nil),
|
2024-02-22 20:52:37 +01:00
|
|
|
)
|
|
|
|
} else {
|
2024-02-23 11:43:23 +01:00
|
|
|
c.HTML(http.StatusNotFound, "", templates.ProfilePage(templates.GenMetaContent(c), "User Profile", &user, nil))
|
2024-02-22 20:52:37 +01:00
|
|
|
}
|
|
|
|
|
2024-02-22 10:18:59 +01:00
|
|
|
}
|
2024-02-23 11:22:28 +01:00
|
|
|
|
|
|
|
func getProfileEdit(c *gin.Context) {
|
|
|
|
session := sessions.Default(c)
|
|
|
|
sessionUserName := session.Get("username")
|
|
|
|
var user db.User
|
|
|
|
if username, ok := sessionUserName.(string); ok {
|
|
|
|
|
|
|
|
user, err := db.GetUserByName(username)
|
|
|
|
if err != nil {
|
|
|
|
c.HTML(
|
|
|
|
http.StatusInternalServerError,
|
|
|
|
"",
|
|
|
|
templates.ProfileStatic(&user),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.HTML(
|
|
|
|
http.StatusOK,
|
|
|
|
"",
|
|
|
|
|
|
|
|
templates.ProfileStatic(&user),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
c.HTML(http.StatusNotFound, "", templates.ProfileStatic(&user))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func postProfileEdit(c *gin.Context) {
|
|
|
|
session := sessions.Default(c)
|
|
|
|
sessionUserName := session.Get("username")
|
|
|
|
var user db.User
|
|
|
|
if username, ok := sessionUserName.(string); ok {
|
|
|
|
|
|
|
|
user, err := db.GetUserByName(username)
|
|
|
|
if err != nil {
|
|
|
|
c.HTML(
|
|
|
|
http.StatusInternalServerError,
|
|
|
|
"",
|
|
|
|
templates.ProfileStatic(&user),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.HTML(
|
|
|
|
http.StatusOK,
|
|
|
|
"",
|
|
|
|
|
|
|
|
templates.ProfileStatic(&user),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
c.HTML(http.StatusNotFound, "", templates.ProfileStatic(&user))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func postPassword(c *gin.Context) {
|
|
|
|
session := sessions.Default(c)
|
|
|
|
password := c.PostForm("password")
|
|
|
|
confirm := c.PostForm("confirm")
|
|
|
|
if password != confirm {
|
|
|
|
|
|
|
|
c.HTML(http.StatusConflict, "", templates.PasswordForm(false, "Passwords do not match"))
|
|
|
|
}
|
|
|
|
sessionUserName := session.Get("username")
|
|
|
|
if username, ok := sessionUserName.(string); ok {
|
|
|
|
|
|
|
|
user, err := db.GetUserByName(username)
|
|
|
|
if err != nil {
|
|
|
|
c.HTML(
|
|
|
|
http.StatusInternalServerError,
|
|
|
|
"",
|
|
|
|
templates.PasswordForm(false, err.Error()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
passHash, err := utils.HashPassword(password)
|
|
|
|
if err != nil {
|
|
|
|
c.HTML(
|
|
|
|
http.StatusInternalServerError,
|
|
|
|
"",
|
|
|
|
templates.PasswordForm(false, err.Error()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
db.SetUserPassHash(user, passHash)
|
|
|
|
if err != nil {
|
|
|
|
c.HTML(
|
|
|
|
http.StatusInternalServerError,
|
|
|
|
"",
|
|
|
|
templates.PasswordForm(false, err.Error()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.HTML(
|
|
|
|
http.StatusOK,
|
|
|
|
"",
|
|
|
|
|
|
|
|
templates.PasswordForm(true, "Password changed"),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
c.HTML(http.StatusNotFound, "", templates.PasswordForm(false, "User Not Found"))
|
|
|
|
}
|
|
|
|
}
|