Reapply "Adminbereich mit Rollen und Nutzerauswertung ergänzen"

This reverts commit 2447537652.
This commit is contained in:
Eugenius Naumenko
2026-10-11 21:52:58 +00:00
parent 2447537652
commit a59c69353d
14 changed files with 634 additions and 18 deletions
+17
View File
@@ -125,6 +125,23 @@ func (s *Server) requireAuth(next http.Handler) http.Handler {
})
}
// Admin rights are read from PostgreSQL for every request. A previously
// issued token does not preserve privileges after an account is demoted.
func (s *Server) requireAdmin(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
user, err := s.store.GetUser(r.Context(), currentUserID(r))
if err != nil {
writeError(w, http.StatusInternalServerError, err)
return
}
if user.Role != "admin" {
writeError(w, http.StatusForbidden, nil)
return
}
next.ServeHTTP(w, r)
})
}
func currentUserID(r *http.Request) string {
value, _ := r.Context().Value(userIDKey).(string)
return value