Files
musenalm/middleware/admin_or_user.go
2025-05-26 17:27:52 +02:00

25 lines
600 B
Go

package middleware
import (
"github.com/Theodor-Springmann-Stiftung/musenalm/templating"
"github.com/pocketbase/pocketbase/core"
)
// INFO: Here the URL must have a path value "uid" which is the user ID of the affected user.
func IsAdminOrUser() func(*core.RequestEvent) error {
return func(e *core.RequestEvent) error {
req := templating.NewRequest(e)
user := req.User()
if user == nil {
return e.Error(401, "Unauthorized", nil)
}
uid := e.Request.PathValue("uid")
if uid != user.Id && user.Role != "Admin" {
return e.Error(403, "Forbidden", nil)
}
return e.Next()
}
}