fix: Admin-Link in der Navigation zeigen

Nach dem Login landet jeder Nutzer (auch ein Admin, jeder Login gehört
zu einem Account) auf der normalen Startseite — ohne einen Link zu
/admin in der Navigation war der Admin-Bereich für einen Admin ohne
die URL im Kopf praktisch unerreichbar (genau das hat der Nutzer nach
dem Login gemeldet).

navData{IsAdmin} wird jetzt von jeder angemeldeten Seite (Start,
Beiträge, Beitrag-Detail, alle Admin-Seiten) an den gemeinsamen
"nav"-Template-Block durchgereicht; der Link erscheint nur für
role=admin. Test deckt beide Fälle ab (Admin sieht den Link, Mandant
nicht).
This commit is contained in:
noroot
2026-08-27 19:04:04 +02:00
parent 835ad9f0a7
commit 8e5d06aafb
13 changed files with 60 additions and 14 deletions

View File

@@ -34,6 +34,7 @@ func (s *Server) handlePublicKanzleiList(w http.ResponseWriter, r *http.Request)
type adminDashboardData struct {
Title string
Nav navData
AccountCount int
UnverifiedCount int
RecentAuditCount int
@@ -61,7 +62,7 @@ func (s *Server) handleAdminDashboard(w http.ResponseWriter, r *http.Request) {
}
data := adminDashboardData{
Title: "Admin", AccountCount: len(accounts), UnverifiedCount: unverified, RecentAuditCount: len(auditLog),
Title: "Admin", Nav: navFor(r), AccountCount: len(accounts), UnverifiedCount: unverified, RecentAuditCount: len(auditLog),
}
if err := s.templates.ExecuteTemplate(w, "admin-dashboard", data); err != nil {
http.Error(w, "Seite konnte nicht gerendert werden", http.StatusInternalServerError)
@@ -77,6 +78,7 @@ type adminAccountListItem struct {
type adminAccountListData struct {
Title string
Nav navData
Accounts []adminAccountListItem
}
@@ -87,7 +89,7 @@ func (s *Server) handleAdminAccountList(w http.ResponseWriter, r *http.Request)
http.Error(w, "Accounts konnten nicht geladen werden: "+err.Error(), http.StatusInternalServerError)
return
}
data := adminAccountListData{Title: "Accounts"}
data := adminAccountListData{Title: "Accounts", Nav: navFor(r)}
for _, a := range accounts {
data.Accounts = append(data.Accounts, adminAccountListItem{
ID: a.ID, Name: a.Name, Verified: a.Verified, CreatedAt: a.CreatedAt.Format("02.01.2006 15:04"),
@@ -105,6 +107,7 @@ type adminUserView struct {
type adminAccountDetailData struct {
Title string
Nav navData
AccountID string
Name string
Verified bool
@@ -130,7 +133,7 @@ func (s *Server) handleAdminAccountDetail(w http.ResponseWriter, r *http.Request
}
data := adminAccountDetailData{
Title: "Account", AccountID: acc.ID, Name: acc.Name, Verified: acc.Verified,
Title: "Account", Nav: navFor(r), AccountID: acc.ID, Name: acc.Name, Verified: acc.Verified,
CreatedAt: acc.CreatedAt.Format("02.01.2006 15:04"),
}
for _, u := range users {
@@ -190,6 +193,7 @@ type adminAuditEntryView struct {
type adminAuditLogData struct {
Title string
Nav navData
Entries []adminAuditEntryView
}
@@ -200,7 +204,7 @@ func (s *Server) handleAdminAuditLog(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Audit-Log konnte nicht geladen werden: "+err.Error(), http.StatusInternalServerError)
return
}
data := adminAuditLogData{Title: "Audit-Log"}
data := adminAuditLogData{Title: "Audit-Log", Nav: navFor(r)}
for _, e := range entries {
data.Entries = append(data.Entries, adminAuditEntryView{
CreatedAt: e.CreatedAt.Format("02.01.2006 15:04:05"), Action: e.Action,

View File

@@ -40,6 +40,28 @@ func TestAdminDashboardAccessibleForAdmin(t *testing.T) {
}
}
// TestNavShowsAdminLinkOnlyForAdmins deckt genau den gemeldeten Fall ab:
// nach der Anmeldung als Admin landet man auf der normalen Startseite
// (jeder Nutzer hat einen Account+Login, auch ein Admin) — ohne einen
// sichtbaren Weg zu /admin wäre der Admin-Bereich für einen Admin, der
// die URL nicht auswendig kennt, praktisch unerreichbar.
func TestNavShowsAdminLinkOnlyForAdmins(t *testing.T) {
fs := newFakeStore()
s := newServer(t, fakeExtractor{}, fs)
adminCookie := seedAccountWithRole(t, fs, "Deklarix Admin", "admin@example.com", "admin")
tenantCookie := seedAccount(t, fs, "Mandant", "mandant@example.com")
adminResp := getWithCookie(t, s, adminCookie, "/")
if !strings.Contains(adminResp.Body.String(), `href="/admin"`) {
t.Errorf("expected an /admin nav link for an admin user, got: %s", adminResp.Body.String())
}
tenantResp := getWithCookie(t, s, tenantCookie, "/")
if strings.Contains(tenantResp.Body.String(), `href="/admin"`) {
t.Errorf("expected no /admin nav link for a non-admin user, got: %s", tenantResp.Body.String())
}
}
func TestAdminAccountListShowsAllAccountsAcrossTenants(t *testing.T) {
fs := newFakeStore()
s := newServer(t, fakeExtractor{}, fs)

View File

@@ -18,6 +18,7 @@ type submissionListItem struct {
type submissionListData struct {
Title string
Nav navData
Submissions []submissionListItem
}
@@ -31,7 +32,7 @@ func (s *Server) handleSubmissionList(w http.ResponseWriter, r *http.Request) {
return
}
data := submissionListData{Title: "Beiträge"}
data := submissionListData{Title: "Beiträge", Nav: navFor(r)}
for _, sum := range summaries {
data.Submissions = append(data.Submissions, submissionListItem{
ID: sum.ID, Platform: sum.Platform, PostType: sum.PostType, Status: sum.Status,
@@ -56,6 +57,7 @@ type participantView struct {
type submissionDetailData struct {
Title string
Nav navData
SubmissionID string
Platform string
PostType string
@@ -125,7 +127,7 @@ func (s *Server) handleSubmissionDetail(w http.ResponseWriter, r *http.Request)
}
data := submissionDetailData{
Title: "Beitrag", SubmissionID: sub.ID, Platform: sub.Platform, PostType: sub.PostType,
Title: "Beitrag", Nav: navFor(r), SubmissionID: sub.ID, Platform: sub.Platform, PostType: sub.PostType,
Caption: sub.Caption, Status: sub.Status, CreatedAt: sub.CreatedAt.Format("02.01.2006 15:04"),
CanArchive: sub.Status == "checked", IsPublished: sub.Status == "published",
DossierURL: "/dossier/" + sub.ID, Findings: findings, Participants: toParticipantViews(participants),

View File

@@ -21,10 +21,12 @@ func (s *Server) handleHealth(w http.ResponseWriter, r *http.Request) {
type indexData struct {
Title string
Nav navData
}
func (s *Server) handleIndex(w http.ResponseWriter, r *http.Request) {
if err := s.templates.ExecuteTemplate(w, "index", indexData{Title: "Pre-Publish-Prüfung"}); err != nil {
data := indexData{Title: "Pre-Publish-Prüfung", Nav: navFor(r)}
if err := s.templates.ExecuteTemplate(w, "index", data); err != nil {
http.Error(w, "Seite konnte nicht gerendert werden", http.StatusInternalServerError)
}
}

View File

@@ -88,6 +88,21 @@ func (s *Server) requireAdmin(next http.HandlerFunc) http.HandlerFunc {
}
}
// navData steuert die gemeinsame Navigation (layout.html, "nav"-Block).
// Eigenes, kleines Struct statt jeder Seite Zugriff auf den vollen
// currentUser zu geben — die Navigation braucht nur, ob ein Admin-Link
// gezeigt werden soll.
type navData struct {
IsAdmin bool
}
// navFor liefert die Nav-Daten für den angemeldeten Nutzer der Anfrage.
// Nur für Seiten hinter requirePage/requireAdmin aufrufbar (braucht
// currentUser).
func navFor(r *http.Request) navData {
return navData{IsAdmin: currentUser(r).Role == "admin"}
}
// currentUser liest den Nutzer, den requirePage/requireAPI in den
// Kontext gelegt haben. Panics, wenn es aufgerufen wird, ohne dass eine
// dieser Middlewares vorgeschaltet war — das ist ein Programmierfehler,

View File

@@ -2,7 +2,7 @@
<html lang="de">
<head>{{template "head" .}}</head>
<body>
{{template "nav" .}}
{{template "nav" .Nav}}
<div class="page">
<p><a href="/admin/accounts">&larr; Alle Accounts</a></p>
<h1>{{.Name}}</h1>

View File

@@ -2,7 +2,7 @@
<html lang="de">
<head>{{template "head" .}}</head>
<body>
{{template "nav" .}}
{{template "nav" .Nav}}
<div class="page">
<p><a href="/admin">&larr; Admin</a></p>
<h1>Accounts</h1>

View File

@@ -2,7 +2,7 @@
<html lang="de">
<head>{{template "head" .}}</head>
<body>
{{template "nav" .}}
{{template "nav" .Nav}}
<div class="page">
<p><a href="/admin">&larr; Admin</a></p>
<h1>Audit-Log</h1>

View File

@@ -2,7 +2,7 @@
<html lang="de">
<head>{{template "head" .}}</head>
<body>
{{template "nav" .}}
{{template "nav" .Nav}}
<div class="page">
<h1>Admin</h1>
<ul class="admin-kacheln">

View File

@@ -2,7 +2,7 @@
<html lang="de">
<head>{{template "head" .}}</head>
<body>
{{template "nav" .}}
{{template "nav" .Nav}}
<div class="page">
<h1>Beiträge</h1>

View File

@@ -2,7 +2,7 @@
<html lang="de">
<head>{{template "head" .}}</head>
<body>
{{template "nav" .}}
{{template "nav" .Nav}}
<div class="page">
<p><a href="/beitraege">&larr; Alle Beiträge</a></p>
<h1>{{.Platform}} · {{.PostType}}</h1>

View File

@@ -2,7 +2,7 @@
<html lang="de">
<head>{{template "head" .}}</head>
<body>
{{template "nav" .}}
{{template "nav" .Nav}}
<div class="page">
<h1>Pre-Publish-Prüfung</h1>
<p>Caption und Plattform eingeben, um auf Kennzeichnungsrisiken zu prüfen.</p>

View File

@@ -10,6 +10,7 @@
<nav>
<a href="/">Prüfen</a>
<a href="/beitraege">Beiträge</a>
{{if .IsAdmin}}<a href="/admin">Admin</a>{{end}}
<form method="post" action="/logout" style="display:inline">
<button type="submit">Abmelden</button>
</form>