34 lines
632 B
Go
34 lines
632 B
Go
package middlewares
|
|
|
|
import (
|
|
"git.readonly.ch/bouzoure/pop-camarades/helpers"
|
|
"git.readonly.ch/bouzoure/pop-camarades/models"
|
|
"github.com/gofiber/fiber/v2"
|
|
)
|
|
|
|
func AuthzAdmin(c *fiber.Ctx) error {
|
|
sess, err := helpers.GetSessionStore(c)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
db, err := helpers.GetDatabase()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
var allowAccess bool
|
|
userid := sess.Get("userid")
|
|
|
|
if userid != nil {
|
|
var user models.User
|
|
db.First(&user, "id = ?", userid.(uint))
|
|
allowAccess = user.IsAdmin
|
|
}
|
|
|
|
if !allowAccess {
|
|
return fiber.NewError(fiber.StatusForbidden, "Forbidden (authz_admin)")
|
|
}
|
|
|
|
return c.Next()
|
|
}
|