Optimizations. User-Role handling in templates and routes.

This commit is contained in:
MaxJa4
2024-01-21 17:24:29 +01:00
parent 8edbbb4347
commit 4aae0896aa
14 changed files with 221 additions and 119 deletions

View File

@@ -2,7 +2,9 @@ package controllers
import (
"InfantrySkillCalculator/models"
"github.com/gin-gonic/gin"
"log"
"session"
)
func CreateUser(username string, hashedPassword string, enabled bool, usedCode string) {
@@ -56,6 +58,23 @@ func IsUserEnabled(username string) bool {
return user.Enabled
}
func GetUserRole(username string) models.Role {
var user models.User
err := models.DB.Where("username = ?", username).First(&user).Error
if err != nil {
log.Fatal(err)
}
return user.UserRole
}
func GetUserRoleByCtx(c *gin.Context) models.Role {
username, ok := session.GetUsername(c)
if !ok {
return models.ReaderRole
}
return GetUserRole(username)
}
func IsUserAdmin(username string) bool {
var user models.User
err := models.DB.Where("username = ?", username).First(&user).Error