diff --git a/internal/web/activity.go b/internal/web/activity.go index a5463dc..b3be1f4 100644 --- a/internal/web/activity.go +++ b/internal/web/activity.go @@ -136,7 +136,7 @@ type activityData struct { const activityPage = 100 func (s *Server) activityPage(w http.ResponseWriter, r *http.Request) { - if s.admin(w, r) == nil { + if !s.adminPage(w, r) { return } q := r.URL.Query() diff --git a/internal/web/connections.go b/internal/web/connections.go index c50a65c..8e5862b 100644 --- a/internal/web/connections.go +++ b/internal/web/connections.go @@ -185,7 +185,7 @@ type connRow struct { } func (s *Server) connectionsPage(w http.ResponseWriter, r *http.Request) { - if s.admin(w, r) == nil { + if !s.adminPage(w, r) { return } changes := map[string]connChange{} diff --git a/internal/web/connections_test.go b/internal/web/connections_test.go index c911773..3d3ffff 100644 --- a/internal/web/connections_test.go +++ b/internal/web/connections_test.go @@ -65,3 +65,17 @@ func TestServiceOf(t *testing.T) { } } } + +// The local edition has no one signed in: its owner still opens the admin +// pages that don't need an account. +func TestAdminPagesLocal(t *testing.T) { + _, h := newTestServer(t, nil) + for _, p := range []string{"/settings/connections", "/settings/posting", "/settings/activity"} { + if rec := do(h, http.MethodGet, p, nil, nil); rec.Code != http.StatusOK { + t.Errorf("%s: %d, want 200", p, rec.Code) + } + } + if rec := postForm(h, "/settings/posting", url.Values{}); rec.Code != http.StatusSeeOther { + t.Errorf("saving posting accounts: %d, want 303", rec.Code) + } +} diff --git a/internal/web/posting.go b/internal/web/posting.go index c645825..4768c57 100644 --- a/internal/web/posting.go +++ b/internal/web/posting.go @@ -294,7 +294,7 @@ type postingGrid struct { } func (s *Server) postingPage(w http.ResponseWriter, r *http.Request) { - if s.admin(w, r) == nil { + if !s.adminPage(w, r) { return } g := postingGrid{Accounts: s.postingAccounts(), Working: map[int64]bool{}, Owners: map[int64]string{}} @@ -321,7 +321,7 @@ func (s *Server) postingPage(w http.ResponseWriter, r *http.Request) { } func (s *Server) postingSave(w http.ResponseWriter, r *http.Request) { - if s.admin(w, r) == nil { + if !s.adminPage(w, r) { return } if err := r.ParseForm(); err != nil { diff --git a/internal/web/users.go b/internal/web/users.go index dae790b..bd3a58e 100644 --- a/internal/web/users.go +++ b/internal/web/users.go @@ -91,6 +91,16 @@ func (s *Server) admin(w http.ResponseWriter, r *http.Request) *viewer { return v } +// adminPage says whether the viewer may use an admin page that needs no one +// signed in, as the local edition's owner may; or answers 403. +func (s *Server) adminPage(w http.ResponseWriter, r *http.Request) bool { + if !viewerOf(r).Member.Admin() { + s.forbidden(w, r) + return false + } + return true +} + // forbidden is the answer to a request outside someone's access. func (s *Server) forbidden(w http.ResponseWriter, r *http.Request) { if r.Header.Get("HX-Request") != "" || !safeMethod(r.Method) {