Files
projet_gestion_commande/backend/gestion/middleware/block_middleware.go
T
2026-03-12 21:59:18 +01:00

73 lines
1.7 KiB
Go

package middleware
import (
"gestion/db"
"log"
"net/http"
"github.com/gin-gonic/gin"
)
func BlockClientIfPenalty(c *gin.Context) {
database := c.MustGet("database").(*db.Database)
if settings, err := database.GetSettings(); err == nil && !settings.PenaltiesEnabled {
c.Next()
return
}
clientID, exists := c.Get("client_id")
if !exists {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Authentification requise"})
c.Abort()
return
}
if id, ok := clientID.(int); ok {
if session, err := database.GetClientSession(id); err == nil && session.PenaltyCache > 0 {
log.Printf("🚫 [PENALTY] Checkout bloqué pour client_id=%d (amende=%.2f via cache)", id, session.PenaltyCache)
c.JSON(http.StatusForbidden, gin.H{
"error": "Commande bloquée : vous avez une amende en attente de paiement",
"amende": session.PenaltyCache,
"blocked": true,
})
c.Abort()
return
}
}
username, exists := c.Get("username")
if !exists {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Authentification requise"})
c.Abort()
return
}
usernameStr, ok := username.(string)
if !ok || usernameStr == "" {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Username invalide"})
c.Abort()
return
}
amende, err := database.GetClientAmende(usernameStr)
if err != nil {
log.Printf("❌ [PENALTY] Erreur vérification amende pour %s: %v", usernameStr, err)
c.Next()
return
}
if amende > 0 {
log.Printf("🚫 [PENALTY] Checkout bloqué pour %s (amende=%.2f via DB)", usernameStr, amende)
c.JSON(http.StatusForbidden, gin.H{
"error": "Commande bloquée : vous avez une amende en attente de paiement",
"amende": amende,
"blocked": true,
})
c.Abort()
return
}
c.Next()
}