feat(webui): add user accounts list, admin middleware and admin restricted menu entries

This commit is contained in:
Julien Dessaux 2025-02-26 00:07:35 +01:00
parent fb98b4fffe
commit e23a146490
Signed by: adyxax
GPG key ID: F92E51B86E07177E
9 changed files with 143 additions and 2 deletions

28
pkg/webui/admin.go Normal file
View file

@ -0,0 +1,28 @@
package webui
import (
"fmt"
"net/http"
"git.adyxax.org/adyxax/tfstated/pkg/database"
"git.adyxax.org/adyxax/tfstated/pkg/model"
)
func adminMiddleware(db *database.DB, requireLogin func(http.Handler) http.Handler) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return requireLogin(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
account := r.Context().Value(model.AccountContextKey{})
if account == nil {
// this could happen if the account was deleted in the short
// time between retrieving the session and here
http.Redirect(w, r, "/login", http.StatusFound)
return
}
if !account.(*model.Account).IsAdmin {
errorResponse(w, http.StatusForbidden, fmt.Errorf("Only administrators can perform this request."))
return
}
next.ServeHTTP(w, r)
}))
}
}