138 lines
2.8 KiB
Go
138 lines
2.8 KiB
Go
package ui
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"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"
|
|
)
|
|
|
|
func getProfile(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.ProfilePage(templates.GenMetaContent(c), "User Profile", &user, err,
|
|
)
|
|
}
|
|
|
|
c.HTML(
|
|
http.StatusOK,
|
|
"",
|
|
templates.ProfilePage(templates.GenMetaContent(c), "User Profile", &user, nil),
|
|
)
|
|
} else {
|
|
c.HTML(http.StatusNotFound, "", templates.ProfilePage(templates.GenMetaContent(c), "User Profile", &user, nil))
|
|
}
|
|
|
|
}
|
|
|
|
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"))
|
|
}
|
|
}
|