2024-02-22 20:52:37 +01:00
|
|
|
package templates
|
|
|
|
|
2024-02-23 11:43:23 +01:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gin-contrib/sessions"
|
|
|
|
"github.com/gin-gonic/gin"
|
2024-02-23 18:39:05 +01:00
|
|
|
|
|
|
|
"git.jmbit.de/jmb/goipam/utils"
|
2024-02-23 11:43:23 +01:00
|
|
|
)
|
|
|
|
|
2024-02-22 20:52:37 +01:00
|
|
|
type IndexCounts struct {
|
|
|
|
SubnetCount int
|
|
|
|
AddressCount int
|
|
|
|
DeviceCount int
|
|
|
|
LocationCount int
|
|
|
|
}
|
2024-02-23 11:43:23 +01:00
|
|
|
|
|
|
|
type MetaContent struct {
|
|
|
|
Username string
|
|
|
|
IsLoggedIn bool
|
|
|
|
IsAdmin bool
|
|
|
|
Timestamp string
|
|
|
|
ErrorTitle string
|
|
|
|
ErrorText string
|
|
|
|
}
|
|
|
|
|
|
|
|
// MetaContent collects all the values needed to render the templates that aren't specific to the view
|
|
|
|
// and combines them into a structure expected by the template
|
|
|
|
func GenMetaContent(c *gin.Context) MetaContent {
|
|
|
|
session := sessions.Default(c)
|
|
|
|
sessionUserName := session.Get("username")
|
|
|
|
sessionIsLoggedIn := session.Get("isLoggedIn")
|
|
|
|
sessionIsAdmin := session.Get("isAdmin")
|
2024-02-23 18:39:05 +01:00
|
|
|
errorText := utils.SliceToLines(c.Errors.Errors())
|
2024-02-23 11:43:23 +01:00
|
|
|
var username string
|
|
|
|
var isLoggedIn bool
|
|
|
|
var isAdmin bool
|
|
|
|
|
|
|
|
// Validate Types
|
|
|
|
if sessionUserName != nil {
|
|
|
|
username, _ = sessionUserName.(string)
|
|
|
|
} else {
|
|
|
|
username = ""
|
|
|
|
}
|
|
|
|
if sessionIsLoggedIn != nil {
|
|
|
|
isLoggedIn, _ = sessionIsLoggedIn.(bool)
|
|
|
|
} else {
|
|
|
|
isLoggedIn = false
|
|
|
|
}
|
|
|
|
if sessionIsAdmin != nil {
|
|
|
|
isAdmin, _ = sessionIsAdmin.(bool)
|
|
|
|
} else {
|
|
|
|
isAdmin = false
|
|
|
|
}
|
|
|
|
|
|
|
|
return MetaContent{
|
|
|
|
Username: username,
|
|
|
|
IsLoggedIn: isLoggedIn,
|
|
|
|
IsAdmin: isAdmin,
|
|
|
|
Timestamp: fmt.Sprintf("%d", time.Now().Year()),
|
2024-02-23 18:39:05 +01:00
|
|
|
ErrorText: errorText,
|
|
|
|
ErrorTitle: "Error",
|
2024-02-23 11:43:23 +01:00
|
|
|
}
|
|
|
|
}
|