Mod permission middleware to only request database once
Signed-off-by: Asai Neko <sugar@sne.moe>
This commit is contained in:
@@ -9,6 +9,9 @@ import (
|
|||||||
|
|
||||||
func Permission(requiredLevel uint) gin.HandlerFunc {
|
func Permission(requiredLevel uint) gin.HandlerFunc {
|
||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
|
var permissionLevel uint
|
||||||
|
permissionLevelPrev, ok := c.Get("permission_level")
|
||||||
|
if !ok {
|
||||||
userIdOrig, ok := c.Get("user_id")
|
userIdOrig, ok := c.Get("user_id")
|
||||||
if !ok || userIdOrig.(string) == "" {
|
if !ok || userIdOrig.(string) == "" {
|
||||||
c.AbortWithStatusJSON(401, gin.H{"status": "missing user id"})
|
c.AbortWithStatusJSON(401, gin.H{"status": "missing user id"})
|
||||||
@@ -27,10 +30,17 @@ func Permission(requiredLevel uint) gin.HandlerFunc {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if userData.PermissionLevel < requiredLevel {
|
permissionLevel = userData.PermissionLevel
|
||||||
|
c.Set("permission_level", userData.PermissionLevel)
|
||||||
|
} else {
|
||||||
|
permissionLevel = permissionLevelPrev.(uint)
|
||||||
|
}
|
||||||
|
|
||||||
|
if permissionLevel < requiredLevel {
|
||||||
c.AbortWithStatusJSON(403, gin.H{"status": "permission denied"})
|
c.AbortWithStatusJSON(403, gin.H{"status": "permission denied"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
c.Next()
|
c.Next()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user