replaced AuthHost by context set&get

This commit is contained in:
Malte 2022-04-05 17:28:17 +02:00
parent 13f52a4bd8
commit 6432c042f5
2 changed files with 5 additions and 7 deletions

View File

@ -18,7 +18,6 @@ import (
type Handler struct {
DB *gorm.DB
AuthHost *model.Host
AuthAdmin bool
Config Envs
Title string
@ -49,15 +48,13 @@ type Error struct {
// To gather admin rights the username password combination must match with the credentials given by the env var.
func (h *Handler) AuthenticateUpdate(username, password string, c echo.Context) (bool, error) {
h.CheckClearInterval()
h.AuthHost = nil
host := &model.Host{}
if err := h.DB.Where(&model.Host{UserName: username, Password: password}).First(host).Error; err != nil {
log.Error("Error:", err)
return false, nil
}
h.AuthHost = host
c.Set("updateHost", host)
return true, nil
}

View File

@ -227,11 +227,12 @@ func (h *Handler) DeleteHost(c echo.Context) (err error) {
// Hostname, IP and senders IP are validated, a log entry is created
// and finally if everything is ok, the DNS Server will be updated
func (h *Handler) UpdateIP(c echo.Context) (err error) {
if h.AuthHost == nil {
host, ok := c.Get("updateHost").(*model.Host)
if !ok {
return c.String(http.StatusBadRequest, "badauth\n")
}
log := &model.Log{Status: false, Host: *h.AuthHost, TimeStamp: time.Now(), UserAgent: nswrapper.ShrinkUserAgent(c.Request().UserAgent())}
log := &model.Log{Status: false, Host: *host, TimeStamp: time.Now(), UserAgent: nswrapper.ShrinkUserAgent(c.Request().UserAgent())}
log.SentIP = c.QueryParam(("myip"))
// Get caller IP
@ -250,7 +251,7 @@ func (h *Handler) UpdateIP(c echo.Context) (err error) {
// Validate hostname
hostname := c.QueryParam("hostname")
if hostname == "" || hostname != h.AuthHost.Hostname+"."+h.AuthHost.Domain {
if hostname == "" || hostname != host.Hostname+"."+host.Domain {
log.Message = "Hostname or combination of authenticated user and hostname is invalid"
if err = h.CreateLogEntry(log); err != nil {
l.Error(err)