Add some directly callable functions into adaptor

This commit is contained in:
LittleSheep 2024-10-31 21:31:56 +08:00
parent aa624ee454
commit 6bb8eab3fc

View File

@ -1,6 +1,7 @@
package sec package sec
import ( import (
"fmt"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
"strings" "strings"
) )
@ -37,3 +38,22 @@ func ValidatorMiddleware(c *fiber.Ctx) error {
return c.Next() return c.Next()
} }
func EnsureAuthenticated(c *fiber.Ctx) error {
if _, ok := c.Locals("nex_user").(*UserInfo); !ok {
return fiber.NewError(fiber.StatusUnauthorized)
}
return nil
}
func EnsureGrantedPerm(c *fiber.Ctx, key string, val any) error {
if err := EnsureAuthenticated(c); err != nil {
return err
}
info := c.Locals("nex_user").(*UserInfo)
if !info.HasPermNode(key, val) {
return fiber.NewError(fiber.StatusForbidden, fmt.Sprintf("missing permission: %s", key))
}
return nil
}