Say there are no organizations rather than 404
With every organization removed, /org answered an administrator with a 404 while the menu kept offering it. An administrator now gets a page saying there are none and where to make one, and the menu offers Organizations only when there is one to show (to a manager, one they manage). Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
e493b2f53d
commit
d8cefd8b21
4 files changed
+57
-3
No files matched your search
+8
-3
@@ -40,12 +40,17 @@ func (p *AccountPages) navFor(r *http.Request, user store.User, current string)
|
||||
Quotas: p.hasQuotas,
|
||||
CSRF: csrfFor(r),
|
||||
}
|
||||
// Only when there is an organization to show: a link that leads to "there
|
||||
// are none" on every page is noise.
|
||||
if p.orgs != nil {
|
||||
var orgs []store.Org
|
||||
var err error
|
||||
if user.IsAdmin {
|
||||
n.Org = true
|
||||
} else if orgs, err := p.orgs.ManagedOrgs(r.Context(), user.ID); err == nil && len(orgs) > 0 {
|
||||
n.Org = true
|
||||
orgs, err = p.orgs.AllOrgs(r.Context())
|
||||
} else {
|
||||
orgs, err = p.orgs.ManagedOrgs(r.Context(), user.ID)
|
||||
}
|
||||
n.Org = err == nil && len(orgs) > 0
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
@@ -71,6 +71,16 @@ func (p *AccountPages) RegisterOrgPages(mux *http.ServeMux, s OrgViewer, priced
|
||||
p.orgs = s
|
||||
mux.HandleFunc("GET /org", p.orgGuard(func(w http.ResponseWriter, r *http.Request, user store.User) {
|
||||
orgs, err := p.viewableOrgs(r.Context(), s, user)
|
||||
if err == nil && len(orgs) == 0 && user.IsAdmin {
|
||||
// Said, not a 404: an administrator following the link on a
|
||||
// cluster with no organizations should learn that, and where to
|
||||
// make one.
|
||||
render(w, "org-none", struct {
|
||||
Nav Nav
|
||||
Version, Now string
|
||||
}{navFrom(r, "org"), p.version, time.Now().UTC().Format(time.RFC3339)})
|
||||
return
|
||||
}
|
||||
if err != nil || len(orgs) == 0 {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
|
||||
@@ -104,3 +104,23 @@ func TestAnAdministratorSeesEveryOrganization(t *testing.T) {
|
||||
t.Errorf("an administrator could not see Scania: %d", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
type noOrgs struct{ fakeOrgs }
|
||||
|
||||
func (noOrgs) AllOrgs(context.Context) ([]store.Org, error) { return nil, nil }
|
||||
func (noOrgs) ManagedOrgs(context.Context, string) ([]store.Org, error) { return nil, nil }
|
||||
|
||||
func TestWithNoOrganizationsAnAdministratorIsToldSo(t *testing.T) {
|
||||
p := NewAccountPages(fakeAccounts{user: store.User{ID: "a", IsAdmin: true}}, nil, nil, "v0",
|
||||
slog.New(slog.NewTextHandler(io.Discard, nil)))
|
||||
mux := http.NewServeMux()
|
||||
p.RegisterOrgPages(mux, noOrgs{}, nil)
|
||||
w := mirrorGet(t, mux, "/org")
|
||||
if w.Code != http.StatusOK || !strings.Contains(w.Body.String(), "There are none") {
|
||||
t.Errorf("an empty cluster answered %d: %s", w.Code, w.Body.String())
|
||||
}
|
||||
// And the menu does not offer a page of nothing.
|
||||
if strings.Contains(w.Body.String(), `href="/org"`) {
|
||||
t.Error("the menu links to organizations when there are none")
|
||||
}
|
||||
}
|
||||
@@ -91,3 +91,22 @@ people do elsewhere — in another organization, or on their own — is not show
|
||||
|
||||
<footer><span>llmgw {{.Version}}</span><span>{{.Now}}</span></footer>
|
||||
</div></body></html>{{end}}
|
||||
|
||||
|
||||
{{define "org-none"}}<!doctype html>
|
||||
<html lang="en"><head>
|
||||
<meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
|
||||
<title>Organizations · llmgw</title><link rel="stylesheet" href="/_static/base.css">
|
||||
</head><body><div class="wrap">
|
||||
<header>
|
||||
<h1>Organizations</h1>
|
||||
<p class="sub">There are none on this cluster</p>
|
||||
{{template "nav" .Nav}}
|
||||
</header>
|
||||
<p>Everyone uses the gateway on their own, under their personal quota. An
|
||||
organization, once there is one, gets a page here for its managers: its use by
|
||||
team, project and person, its limits, and its members.</p>
|
||||
<p><a href="/admin/users">Create one on the people page</a>, then make someone
|
||||
its manager with the star on their membership.</p>
|
||||
<footer><span>llmgw {{.Version}}</span><span>{{.Now}}</span></footer>
|
||||
</div></body></html>{{end}}
|
||||
Reference in new issue
Block a user