Optimizations. User-Role handling in templates and routes.
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user