Add admin-route & dropdown-menu. Add clear-cache & create-code. Adjustments for activation-code and user models. Add sweet-alert for admin-tools.

This commit is contained in:
MaxJa4
2024-01-20 16:47:22 +01:00
parent f2ab72ba1e
commit e5d13f2270
15 changed files with 281 additions and 55 deletions

View File

@@ -5,6 +5,7 @@ import (
"InfantrySkillCalculator/utils"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"net/http"
"time"
)
@@ -49,8 +50,6 @@ func AddCache(c *gin.Context) {
cache := models.PlayerCache{CacheDate: input.CacheDate, PlayerID: input.PlayerID, Score: input.Score, Game: input.Game}
//CreateOrUpdateCache(cache)
c.JSON(http.StatusOK, cache)
}
@@ -68,15 +67,11 @@ func UpdateCacheByPlayerID(c *gin.Context) {
return
}
//log.Println("Updating cache for player #" + utils.UintToString(cache.PlayerID))
models.DB.Model(&cache).Updates(map[string]interface{}{
"CacheDate": input.CacheDate,
"Score": input.Score,
})
//UpdateCacheTimestamp()
c.JSON(http.StatusOK, cache)
}
@@ -90,11 +85,21 @@ func DeleteCacheByPlayerID(c *gin.Context) {
models.DB.Delete(&cache)
//UpdateCacheTimestamp()
c.JSON(http.StatusOK, true)
}
//log.Println("Deleted cache for player #" + utils.UintToString(cache.PlayerID))
// DeleteAllCaches DELETE /cache
func DeleteAllCaches(c *gin.Context) {
var caches []models.PlayerCache
if err := models.DB.
Session(&gorm.Session{AllowGlobalUpdate: true}).
Clauses(clause.Returning{}).
Delete(&caches).Error; err != nil {
c.String(http.StatusBadRequest, "Purge failed! Error: "+err.Error())
return
}
c.String(http.StatusOK, "Purged "+utils.UintToString(uint(len(caches)))+" caches!")
}
func FindCacheGin(out interface{}, c *gin.Context) *gorm.DB {

View File

@@ -0,0 +1,37 @@
package controllers
import (
"InfantrySkillCalculator/models"
"InfantrySkillCalculator/utils"
"github.com/gin-gonic/gin"
"net/http"
)
// CreateCode POST /code
func CreateCode(c *gin.Context) {
userRole, ok := c.GetPostForm("user_role")
if !ok {
c.String(http.StatusBadRequest, "Missing user role")
return
}
var role models.Role
switch userRole {
case "ADMIN":
role = models.AdminRole
case "AUTHOR":
role = models.AuthorRole
case "READER":
role = models.ReaderRole
default:
c.String(http.StatusInternalServerError, "Invalid user role: "+userRole)
}
newCode := utils.GenerateActivationCode()
newCodeObj := models.ActivationCode{Code: newCode, UserRole: role}
if err := models.DB.Create(&newCodeObj).Error; err != nil {
c.String(http.StatusInternalServerError, "Failed to create new code: "+err.Error())
return
}
c.String(http.StatusOK, "Activation code for role '"+string(role)+"': "+newCode)
}

View File

@@ -7,17 +7,39 @@ import (
func CreateUser(username string, hashedPassword string, enabled bool, usedCode string) {
user := models.User{Username: username, Password: hashedPassword, Enabled: enabled}
models.DB.Create(&user)
err := models.DB.Model(&models.ActivationCode{}).
Where("code = ?", usedCode).
Update("Used", true).Error
err := models.DB.Create(&user).Error
if err != nil {
log.Fatal(err)
log.Fatalf("Error while creating user: %v", err)
}
var code models.ActivationCode
err = models.DB.
Model(&models.ActivationCode{}).
Where("code = ?", usedCode).
First(&code).Error
if err != nil {
log.Fatalf("Error while getting activation code: %v", err)
}
code.UsedForUsername = username
err = models.DB.Save(&code).Error
if err != nil {
log.Fatalf("Error while updating activation code: %v", err)
}
user.UserRole = code.UserRole
err = models.DB.Save(&user).Error
if err != nil {
log.Fatalf("Error while updating user role: %v", err)
}
var bf2042 models.Game
models.DB.Where("tag = ?", "BF2042").First(&bf2042)
err = models.DB.
Where("tag = ?", "BF2042").
First(&bf2042).Error
if err != nil {
log.Fatalf("Error while getting game: %v", err)
}
userSettings := models.UserSettings{
Username: username,
ActiveGameID: bf2042.ID,
@@ -33,3 +55,13 @@ func IsUserEnabled(username string) bool {
models.DB.Where("username = ?", username).First(&user)
return user.Enabled
}
func IsUserAdmin(username string) bool {
var user models.User
err := models.DB.Where("username = ?", username).First(&user).Error
if err != nil {
log.Fatal(err)
return false
}
return user.UserRole == models.AdminRole
}