Give an organization's managers a page of their own

A member of an organization can be its manager (users join --org X
--role manager, or the star on the people page). /org shows a manager
their organization: usage over time by team, project or person, its
teams and projects with limits and 30-day spend, and its members, each
once with every place they belong. Read-only; administrators see every
organization. Only what was spent in the organization's projects is
shown — UsageBucketsIn filters by where a request was spent.

Found on the way:
- Usage by org, team and project, and the quotas page's spend, joined
  through api_keys and so dropped every chat-page request, which has no
  key row. They now read usage.project_id; keys no longer move between
  projects, so the two agree.
- /org was not exempt from API-key auth and answered browsers with a
  JSON error. A router test now drives the session pages through the
  middleware; it fails without the fix.

Membership queries, org-scoped usage and the reworked joins pass the
PostgreSQL store tests on 16.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Ilker ManapandClaude Opus 5.5 committed 2026-09-28 19:23:12 +02:00
1 parent 098886e0e4
commit e493b2f53d
21 files changed
+716 -48

No files matched your search

+19
View File
@@ -170,6 +170,25 @@ func (p *AccountPages) RegisterAdminUsers(mux *http.ServeMux, s AccountAdmin) {
return "added to " + scopeLabel(ctx, s, store.Scope(scope), scopeID), nil
}))
// Managing is a role on an organization membership: it lets the person see
// that organization's page, and nothing else changes.
mux.HandleFunc("POST /admin/users/{id}/role", p.adminAction(s,
func(ctx context.Context, admin store.User, id string, r *http.Request) (string, error) {
scope, scopeID, ok := strings.Cut(r.FormValue("scope"), ":")
role := r.FormValue("role")
if !ok || store.Scope(scope) != store.ScopeOrg || (role != store.RoleManager && role != store.RoleMember) {
return "", errors.New("a manager is a role in an organization")
}
if err := s.AddMembership(ctx, id, store.ScopeOrg, scopeID, role, admin.Email, time.Now()); err != nil {
return "", err
}
label := scopeLabel(ctx, s, store.ScopeOrg, scopeID)
if role == store.RoleManager {
return "now manages " + label, nil
}
return "no longer manages " + label, nil
}))
mux.HandleFunc("POST /admin/users/{id}/leave", p.adminAction(s,
func(ctx context.Context, _ store.User, id string, r *http.Request) (string, error) {
scope, scopeID, ok := strings.Cut(r.FormValue("scope"), ":")