package sec

import (
	"git.clearsky.net.au/cody/gex.git/pkg/sess"
	"git.clearsky.net.au/cody/gex.git/pkg/srv"
)

func Middleware(req *srv.Req, res srv.Res) bool {
	pattern := req.Pattern

	// Cancel the security check as there are no permissions for this route
	if len(permissions[pattern]) == 0 {
		return true
	}

	handlerRoles := permissions[pattern]
	sess := req.Ctx["Sess"].(sess.Sess)

	for _, role := range handlerRoles {
		if sess.HasRole(role) {
			return true
		}
	}

	res.Send("No Access")
	return false
}
