From e493b2f53d2e3a80ae304a8ab730fc53d5c62f76 Mon Sep 17 00:00:00 2001 From: Ilker Manap Date: Mon, 28 Sep 2026 19:23:12 +0200 Subject: [PATCH] Give an organization's managers a page of their own MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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) --- CHANGELOG.md | 16 ++ docs/onboarding.md | 16 ++ internal/cli/users.go | 2 +- internal/router/handlers.go | 4 +- internal/router/pages_auth_test.go | 31 +++ internal/store/memberships.go | 21 ++ internal/store/memberships_test.go | 73 +++++++ internal/store/postgres_memberships_test.go | 22 +++ internal/store/usagescope.go | 19 +- internal/store/usageseries.go | 50 +++-- internal/web/account.go | 2 + internal/web/admin_quotas.go | 7 +- internal/web/admin_usage.go | 47 +++-- internal/web/admin_users.go | 19 ++ internal/web/nav.go | 10 + internal/web/org_page.go | 207 ++++++++++++++++++++ internal/web/org_page_test.go | 106 ++++++++++ internal/web/static/base.css | 1 + internal/web/templates/admin_users.html | 11 +- internal/web/templates/nav.html | 7 + internal/web/templates/org.html | 93 +++++++++ 21 files changed, 716 insertions(+), 48 deletions(-) create mode 100644 internal/router/pages_auth_test.go create mode 100644 internal/web/org_page.go create mode 100644 internal/web/org_page_test.go create mode 100644 internal/web/templates/org.html diff --git a/CHANGELOG.md b/CHANGELOG.md index 1c6493c..78a0a30 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,22 @@ APIs change between minors without a deprecation period. ## Unreleased +### An organization's own page + +A member of an organization can be made its **manager** (`users join --org … +--role manager`, or the star on the people page). A manager gets `/org`: the +organization's usage over time by team, project or person, its teams and +projects with limits and spend, and its members — read-only, and only what was +spent in its projects. Administrators can open every organization's page. + +### Fixes + +- Usage by team, project or organization, and the spend on the quotas page, + left out every request made on the chat page: they were joined through the + key, and the chat page has none. They now count where a request was spent. +- `/org` was answered with "no api key was provided" for a signed-in browser; + a test now drives every session page through the authentication middleware. + ### People belong to many places, or to none A person was in exactly one project, chosen at approval, and could not use the diff --git a/docs/onboarding.md b/docs/onboarding.md index f7474ec..51e2c0e 100644 --- a/docs/onboarding.md +++ b/docs/onboarding.md @@ -154,6 +154,22 @@ auth: A personal key has no project, so it keeps no conversations or agent notes; those are kept per project. +### Organization managers + +A member of an organization can be made its manager: + +``` +llmgw users join boss@acme.com --org Acme --role manager +``` + +or with the star on their organization membership on `/admin/users`. A manager +gets an **Organization** link and a page at `/org`: usage over time by team, +project or person, the teams and projects with their limits and spend, and every +member. Only what was spent in that organization's projects is on it; what its +people do elsewhere is not. The page is read-only — a manager is not an +administrator of the cluster, and limits and memberships stay with those who +are. + `llmgw keys init` still works for provisioning scripts and is idempotent, but it will no longer move a team or project that already exists under another parent; it says so and stops. diff --git a/internal/cli/users.go b/internal/cli/users.go index f29a1ab..43fb597 100644 --- a/internal/cli/users.go +++ b/internal/cli/users.go @@ -376,7 +376,7 @@ func newUserJoinCmd(open userOpener) *cobra.Command { }, } where.bind(cmd) - cmd.Flags().StringVar(&role, "role", store.RoleMember, "member (manager is reserved)") + cmd.Flags().StringVar(&role, "role", store.RoleMember, "member, or manager: with --org, may see the organization's page") cmd.Flags().StringVar(&by, "by", "", "who added them, for the record") return cmd } diff --git a/internal/router/handlers.go b/internal/router/handlers.go index 7d439ee..f9a5d4d 100644 --- a/internal/router/handlers.go +++ b/internal/router/handlers.go @@ -149,6 +149,8 @@ func (r *Router) Handler() http.Handler { pages.SetPersonalDefaults(r.personalDefaults) pages.Register(mux) r.registerChat(mux, pages) + // An organization's own page, for the people who manage it. + pages.RegisterOrgPages(mux, r.store, r.acct) if r.clusterView != nil { // The cluster dashboard, reachable from a browser without exposing @@ -189,7 +191,7 @@ func (r *Router) Handler() http.Handler { return auth.Middleware(r.auth, r.log, "/health", "/ready", "/", "/_static/", "/claim/", "/signup", "/login", "/logout", "/profile", "/profile/", - "/admin", "/admin/", "/chat", "/chat/")(mux) + "/admin", "/admin/", "/chat", "/chat/", "/org", "/org/")(mux) } // parsedRequest is what the router needs from a request. Everything else in the diff --git a/internal/router/pages_auth_test.go b/internal/router/pages_auth_test.go new file mode 100644 index 0000000..312391e --- /dev/null +++ b/internal/router/pages_auth_test.go @@ -0,0 +1,31 @@ +package router + +import ( + "net/http" + "net/http/httptest" + "strings" + "testing" +) + +// Every page a browser opens with a session must get past the API-key check +// and reach its own sign-in guard. One missing from the exempt list answers a +// signed-in person with {"error": "no api key was provided"} — which is what +// /org did, and which a test that mounts the page without the middleware +// cannot see. +func TestSessionPagesAreNotAskedForAnAPIKey(t *testing.T) { + rt, _ := chatRouter(t, "http://127.0.0.1:1") + h := rt.Handler() + // The /admin pages are mounted only with a cluster view, which this + // router has none of; these are the session pages it does mount. + for _, path := range []string{"/profile", "/chat", "/org", "/org/org-x"} { + w := httptest.NewRecorder() + h.ServeHTTP(w, httptest.NewRequest(http.MethodGet, path, nil)) + if strings.Contains(w.Body.String(), "api key") { + t.Errorf("%s asked a browser for an API key: %d %s", path, w.Code, w.Body.String()) + } + if w.Code != http.StatusSeeOther || w.Header().Get("Location") != "/login" { + t.Errorf("%s without a session: %d to %q, want a redirect to /login", + path, w.Code, w.Header().Get("Location")) + } + } +} diff --git a/internal/store/memberships.go b/internal/store/memberships.go index f665af8..4548b20 100644 --- a/internal/store/memberships.go +++ b/internal/store/memberships.go @@ -282,3 +282,24 @@ func (s *DB) resolveChain(ctx context.Context, e *Effective, keyLevel bool) erro s.applyDefaults(e) return nil } + +// ManagedOrgs lists the organizations a person manages. +func (s *DB) ManagedOrgs(ctx context.Context, userID string) ([]Org, error) { + return queryAll(ctx, s.db, ` + SELECT o.id, o.name FROM memberships m JOIN organizations o ON o.id = m.scope_id + WHERE m.user_id = ? AND m.scope_type = 'org' AND m.role = 'manager' + ORDER BY o.name`, []any{userID}, func(rows *sql.Rows) (Org, error) { + var o Org + return o, rows.Scan(&o.ID, &o.Name) + }) +} + +// ManagesOrg reports whether a person manages an organization. +func (s *DB) ManagesOrg(ctx context.Context, userID, orgID string) (bool, error) { + var n int + err := s.db.QueryRowContext(ctx, ` + SELECT COUNT(*) FROM memberships + WHERE user_id = ? AND scope_type = 'org' AND scope_id = ? AND role = 'manager'`, + userID, orgID).Scan(&n) + return n > 0, err +} diff --git a/internal/store/memberships_test.go b/internal/store/memberships_test.go index 6deaf45..24406be 100644 --- a/internal/store/memberships_test.go +++ b/internal/store/memberships_test.go @@ -210,3 +210,76 @@ func TestSpendIsRestoredForPeopleAndPersonalKeys(t *testing.T) { t.Errorf("total %d personal %d project %d", total, personal, project) } } + +func TestAManagerIsWhatTheMembershipSays(t *testing.T) { + f := newFixture(t) + ctx := t.Context() + if err := f.s.AddMembership(ctx, f.user, ScopeOrg, f.acme, RoleManager, "t", time.Now()); err != nil { + t.Fatal(err) + } + if ok, _ := f.s.ManagesOrg(ctx, f.user, f.acme); !ok { + t.Error("a manager of Acme is not recognised as one") + } + if ok, _ := f.s.ManagesOrg(ctx, f.user, f.scania); ok { + t.Error("managing Acme made them a manager of Scania") + } + orgs, _ := f.s.ManagedOrgs(ctx, f.user) + if len(orgs) != 1 || orgs[0].Name != "Acme" { + t.Errorf("ManagedOrgs = %+v", orgs) + } + // Back to member: no longer a manager. + if err := f.s.AddMembership(ctx, f.user, ScopeOrg, f.acme, RoleMember, "t", time.Now()); err != nil { + t.Fatal(err) + } + if ok, _ := f.s.ManagesOrg(ctx, f.user, f.acme); ok { + t.Error("changing the role back did not take") + } +} + +func TestAnOrganizationSeesOnlyWhatWasSpentInIt(t *testing.T) { + f := newFixture(t) + ctx := t.Context() + now := time.Now() + if err := f.s.WriteUsage(ctx, []Usage{ + {At: now, KeyID: "key-u", UserID: f.user, ProjectID: f.gateway, CostMicros: 1, Model: "m", ServedModel: "m"}, + {At: now, KeyID: "session:" + f.user, UserID: f.user, ProjectID: f.research, CostMicros: 1, Model: "m", ServedModel: "m"}, + {At: now, KeyID: "key-p", UserID: f.user, CostMicros: 1, Model: "m", ServedModel: "m"}, + }); err != nil { + t.Fatal(err) + } + rows, err := f.s.UsageBucketsIn(ctx, GroupByUser, f.acme, now.Add(-time.Hour), time.Hour) + if err != nil { + t.Fatal(err) + } + if len(rows) != 1 || rows[0].Requests != 1 { + t.Errorf("Acme should see only the request made in Acme, got %+v", rows) + } +} + +func TestTheChatPagesUseIsReportedWhereItWasSpent(t *testing.T) { + f := newFixture(t) + ctx := t.Context() + now := time.Now() + // The chat page's requests have no key row; joined through api_keys they + // vanished from every report but the per-person one. + if err := f.s.WriteUsage(ctx, []Usage{ + {At: now, KeyID: "session:" + f.user, UserID: f.user, ProjectID: f.gateway, + CostMicros: 4, Model: "m", ServedModel: "m"}, + }); err != nil { + t.Fatal(err) + } + series, err := f.s.UsageBucketsBy(ctx, GroupByTeam, now.Add(-time.Hour), time.Hour) + if err != nil || len(series) != 1 || series[0].Requests != 1 { + t.Errorf("by team: %+v, %v", series, err) + } + scopes, err := f.s.UsageByScope(ctx, ScopeProject, now.Add(-time.Hour)) + var got int64 + for _, sc := range scopes { + if sc.ID == f.gateway { + got = sc.CostMicros + } + } + if err != nil || got != 4 { + t.Errorf("gateway's spend on the quota page: %d, %v", got, err) + } +} diff --git a/internal/store/postgres_memberships_test.go b/internal/store/postgres_memberships_test.go index 43fb436..9f2170d 100644 --- a/internal/store/postgres_memberships_test.go +++ b/internal/store/postgres_memberships_test.go @@ -84,4 +84,26 @@ func TestPostgresMemberships(t *testing.T) { if keys, err := f.s.AllKeys(ctx); err != nil || len(keys) != 3 { t.Errorf("AllKeys = %d rows, %v", len(keys), err) } + + // Reports by where it was spent, the chat page's included. + if series, err := f.s.UsageBucketsBy(ctx, GroupByTeam, now.Add(-time.Minute), time.Minute); err != nil || + len(series) != 1 || series[0].Requests != 1 { + t.Errorf("by team = %+v, %v; the chat request in research should count", series, err) + } + if in, err := f.s.UsageBucketsIn(ctx, GroupByUser, f.scania, now.Add(-time.Minute), time.Minute); err != nil || + len(in) != 1 || in[0].Requests != 1 { + t.Errorf("Scania's own use = %+v, %v", in, err) + } + if in, err := f.s.UsageBucketsIn(ctx, GroupByUser, f.acme, now.Add(-time.Minute), time.Minute); err != nil || len(in) != 0 { + t.Errorf("Acme saw use it did not have: %+v, %v", in, err) + } + if err := f.s.AddMembership(ctx, f.user, ScopeOrg, f.acme, RoleManager, "t", time.Now()); err != nil { + t.Fatal(err) + } + if ok, err := f.s.ManagesOrg(ctx, f.user, f.acme); err != nil || !ok { + t.Errorf("ManagesOrg = %v, %v", ok, err) + } + if orgs, err := f.s.ManagedOrgs(ctx, f.user); err != nil || len(orgs) != 1 { + t.Errorf("ManagedOrgs = %+v, %v", orgs, err) + } } diff --git a/internal/store/usagescope.go b/internal/store/usagescope.go index 1a41e8c..24dcc1d 100644 --- a/internal/store/usagescope.go +++ b/internal/store/usagescope.go @@ -52,25 +52,24 @@ func (s ScopeUsage) Spent() (float64, bool) { // from anywhere but here is the place they are interpolated. var scopeColumns = map[Scope]struct{ id, name, joins string }{ ScopeOrg: {"o.id", "o.name", ` - JOIN api_keys k ON k.id = u.key_id - JOIN projects p ON p.id = k.project_id + JOIN projects p ON p.id = NULLIF(u.project_id, '') JOIN teams t ON t.id = p.team_id JOIN organizations o ON o.id = t.org_id`}, ScopeTeam: {"t.id", "t.name", ` - JOIN api_keys k ON k.id = u.key_id - JOIN projects p ON p.id = k.project_id + JOIN projects p ON p.id = NULLIF(u.project_id, '') JOIN teams t ON t.id = p.team_id`}, ScopeProject: {"p.id", "p.name", ` - JOIN api_keys k ON k.id = u.key_id - JOIN projects p ON p.id = k.project_id`}, + JOIN projects p ON p.id = NULLIF(u.project_id, '')`}, } // UsageByScope totals usage for one level of the tenancy tree. // -// Joined through api_keys rather than read from usage.project_id, because that -// column records where the key was when the request was served and the tree is -// what the quota is written against. They agree until a key is moved, and the -// report that disagrees with the limit it is shown beside is the useless one. +// Read from usage.project_id, the project the request was spent in. It was +// joined through api_keys, which dropped every request the chat page made — +// those have no key row — so a project's spend on this page and its budget +// counter disagreed by exactly its chat use. Keys no longer move between +// projects, so where a key is and where its requests were spent are the same; +// a project moved to another team takes its history with it through the join. func (s *DB) UsageByScope(ctx context.Context, scope Scope, since time.Time) ([]ScopeUsage, error) { c, ok := scopeColumns[scope] if !ok { diff --git a/internal/store/usageseries.go b/internal/store/usageseries.go index 71a5460..56fd71a 100644 --- a/internal/store/usageseries.go +++ b/internal/store/usageseries.go @@ -68,24 +68,19 @@ func (u UsageSeries) Tokens() int64 { return u.Prompt + u.Completion } // anywhere but here is the place they are interpolated. A grouping that is not // in this map is refused before a query is built. // -// Joined through api_keys rather than read from usage.project_id, for the same -// reason UsageByScope is: that column records where the key was when the -// request was served, and the tree is what a quota is written against. They -// agree until a key is moved, and a report that disagrees with the limit shown -// beside it is the useless one. +// Read from usage.project_id, for the reason UsageByScope is: joined through +// api_keys, the chat page's requests — which have no key row — were missing +// from every grouping but the per-person one. var groupColumns = map[Grouping]struct{ id, name, joins string }{ GroupByOrg: {"o.id", "o.name", ` - JOIN api_keys k ON k.id = u.key_id - JOIN projects p ON p.id = k.project_id + JOIN projects p ON p.id = NULLIF(u.project_id, '') JOIN teams t ON t.id = p.team_id JOIN organizations o ON o.id = t.org_id`}, GroupByTeam: {"t.id", "t.name", ` - JOIN api_keys k ON k.id = u.key_id - JOIN projects p ON p.id = k.project_id + JOIN projects p ON p.id = NULLIF(u.project_id, '') JOIN teams t ON t.id = p.team_id`}, GroupByProject: {"p.id", "p.name", ` - JOIN api_keys k ON k.id = u.key_id - JOIN projects p ON p.id = k.project_id`}, + JOIN projects p ON p.id = NULLIF(u.project_id, '')`}, // Who spent it, as recorded with the spend — which includes personal keys // and the chat page, neither of which a key's project reaches. Traffic // from keys with no owner groups under an empty id, which the page names @@ -101,6 +96,25 @@ var groupColumns = map[Grouping]struct{ id, name, joins string }{ // thing being reported on. func (s *DB) UsageBucketsBy( ctx context.Context, g Grouping, since time.Time, width time.Duration, +) ([]UsageSeries, error) { + return s.usageBuckets(ctx, g, "", since, width) +} + +// UsageBucketsIn is UsageBucketsBy for one organization: only what was spent +// in its projects, grouped by its teams, projects or people. A person's use +// elsewhere — another organization, or on their own — is not this +// organization's to see. +func (s *DB) UsageBucketsIn( + ctx context.Context, g Grouping, orgID string, since time.Time, width time.Duration, +) ([]UsageSeries, error) { + if orgID == "" { + return nil, fmt.Errorf("which organization?") + } + return s.usageBuckets(ctx, g, orgID, since, width) +} + +func (s *DB) usageBuckets( + ctx context.Context, g Grouping, orgID string, since time.Time, width time.Duration, ) ([]UsageSeries, error) { c, ok := groupColumns[g] if !ok { @@ -111,6 +125,12 @@ func (s *DB) UsageBucketsBy( width = time.Hour } ms := width.Milliseconds() + inOrg := "" + if orgID != "" { + // By where it was spent, as recorded with the spend. + inOrg = ` AND NULLIF(u.project_id, '') IN ( + SELECT p.id FROM projects p JOIN teams t ON t.id = p.team_id WHERE t.org_id = ?)` + } // Grouped by ordinal. PostgreSQL matches a grouped expression to a selected // one syntactically, so "(u.ts / ?) * ?" written twice is two expressions @@ -122,11 +142,15 @@ func (s *DB) UsageBucketsBy( SUM(u.cost_micros), SUM(CASE WHEN u.status >= 400 THEN 1 ELSE 0 END) FROM usage u` + c.joins + ` - WHERE u.ts >= ? + WHERE u.ts >= ?` + inOrg + ` GROUP BY 1, 2, 3 ORDER BY 1, 3` - rows, err := s.db.QueryContext(ctx, q, ms, ms, millis(since)) + args := []any{ms, ms, millis(since)} + if orgID != "" { + args = append(args, orgID) + } + rows, err := s.db.QueryContext(ctx, q, args...) if err != nil { return nil, err } diff --git a/internal/web/account.go b/internal/web/account.go index db40bfb..8ae7363 100644 --- a/internal/web/account.go +++ b/internal/web/account.go @@ -65,6 +65,8 @@ type AccountPages struct { personalOff bool // personalDefaults is what personal use falls under by default. personalDefaults *store.Limits + // orgs is set when /org is mounted, so the menu can offer it to managers. + orgs OrgViewer // priced says whether a budget can ever be spent against. priced Priced // registry browses a model registry, or is nil where there is no way out. diff --git a/internal/web/admin_quotas.go b/internal/web/admin_quotas.go index 0b78c46..39ef95e 100644 --- a/internal/web/admin_quotas.go +++ b/internal/web/admin_quotas.go @@ -35,6 +35,11 @@ type QuotaAdmin interface { UsageByUser(ctx context.Context, since time.Time) ([]store.UserUsage, error) } +// scopeUsage is what spend per branch is read from. +type scopeUsage interface { + UsageByScope(ctx context.Context, scope store.Scope, since time.Time) ([]store.ScopeUsage, error) +} + // personRow is one person's own limits: across all their keys and projects. type personRow struct { ID, Email string @@ -288,7 +293,7 @@ func (p *AccountPages) renderQuotas(w http.ResponseWriter, r *http.Request, } // spendByScope is what each group has used in the window, keyed like a limit. -func (p *AccountPages) spendByScope(ctx context.Context, s QuotaAdmin, +func (p *AccountPages) spendByScope(ctx context.Context, s scopeUsage, since time.Time, ) map[string]int64 { out := map[string]int64{} diff --git a/internal/web/admin_usage.go b/internal/web/admin_usage.go index ec7049a..29ed43e 100644 --- a/internal/web/admin_usage.go +++ b/internal/web/admin_usage.go @@ -134,27 +134,7 @@ func (p *AccountPages) renderUsage(w http.ResponseWriter, r *http.Request, return } - d.Requests = LineChartTotals(seriesOf(rows, func(b store.Bucket) float64 { - return float64(b.Requests) - }), "", 4) - d.Tokens = LineChartTotals(seriesOf(rows, func(b store.Bucket) float64 { - return float64(b.Prompt + b.Completion) - }), "", 4) - d.Spend = LineChartTotals(seriesOf(rows, func(b store.Bucket) float64 { - // Shown in whole units, as every other spend figure is: a budget is - // written in units and a chart in micros would not compare with it. - return float64(b.CostMicros) / 1e6 - }), "", 4) - - for _, u := range rows { - d.Rows = append(d.Rows, usageRow{ - Name: displayName(u), - Requests: u.Requests, - Tokens: u.Tokens(), - Spend: unitsOf(u.CostMicros), - Errors: u.Errors, - }) - } + d.Requests, d.Tokens, d.Spend, d.Rows = usageCharts(rows) render(w, "admin_usage", d) } @@ -207,3 +187,28 @@ func groupLabel(g store.Grouping) string { return string(g) } } + +// usageCharts draws usage over time three ways, with the table beneath them. +func usageCharts(rows []store.UsageSeries) (req, tok, spend TimeSeries, table []usageRow) { + req = LineChartTotals(seriesOf(rows, func(b store.Bucket) float64 { + return float64(b.Requests) + }), "", 4) + tok = LineChartTotals(seriesOf(rows, func(b store.Bucket) float64 { + return float64(b.Prompt + b.Completion) + }), "", 4) + spend = LineChartTotals(seriesOf(rows, func(b store.Bucket) float64 { + // Shown in whole units, as every other spend figure is: a budget is + // written in units and a chart in micros would not compare with it. + return float64(b.CostMicros) / 1e6 + }), "", 4) + for _, u := range rows { + table = append(table, usageRow{ + Name: displayName(u), + Requests: u.Requests, + Tokens: u.Tokens(), + Spend: unitsOf(u.CostMicros), + Errors: u.Errors, + }) + } + return req, tok, spend, table +} diff --git a/internal/web/admin_users.go b/internal/web/admin_users.go index a7a0f9a..5e52e67 100644 --- a/internal/web/admin_users.go +++ b/internal/web/admin_users.go @@ -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"), ":") diff --git a/internal/web/nav.go b/internal/web/nav.go index 4576915..b9982ce 100644 --- a/internal/web/nav.go +++ b/internal/web/nav.go @@ -24,6 +24,9 @@ type Nav struct { Mirror, Quotas bool // CSRF signs the sign-out form. CSRF string + // Org offers the organization page: to an administrator, and to anyone + // who manages an organization. + Org bool } type navKey struct{} @@ -37,6 +40,13 @@ func (p *AccountPages) navFor(r *http.Request, user store.User, current string) Quotas: p.hasQuotas, CSRF: csrfFor(r), } + if p.orgs != nil { + 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 + } + } return n } diff --git a/internal/web/org_page.go b/internal/web/org_page.go new file mode 100644 index 0000000..420fc4b --- /dev/null +++ b/internal/web/org_page.go @@ -0,0 +1,207 @@ +package web + +import ( + "context" + "net/http" + "sort" + "time" + + "gitea.mynodes.xyz/ilker/llmgw/internal/store" +) + +// OrgViewer is what an organization's page reads. Nothing on it writes. +type OrgViewer interface { + ManagesOrg(ctx context.Context, userID, orgID string) (bool, error) + ManagedOrgs(ctx context.Context, userID string) ([]store.Org, error) + AllOrgs(ctx context.Context) ([]store.Org, error) + Tree(ctx context.Context) ([]store.TreeOrg, error) + AllMemberships(ctx context.Context) ([]store.Membership, error) + Users(ctx context.Context, state store.UserState) ([]store.User, error) + AllLimits(ctx context.Context) (map[string]store.Limits, error) + UsageBucketsIn(ctx context.Context, g store.Grouping, orgID string, + since time.Time, width time.Duration) ([]store.UsageSeries, error) + UsageByScope(ctx context.Context, scope store.Scope, since time.Time) ([]store.ScopeUsage, error) +} + +// orgMember is a person as an organization's page lists them: once, with +// every place in the organization they are a member of. +type orgMember struct { + Email string + State store.UserState + Places []orgPlace +} + +type orgPlace struct{ Where, Role string } + +type orgPageData struct { + Nav Nav + Org store.Org + Orgs []store.Org // the others this person may look at + + Groups []usageGroup + Windows []struct { + Key, Label string + Selected bool + } + By, Window string + + Requests TimeSeries + Tokens TimeSeries + Spend TimeSeries + Rows []usageRow + Priced bool + + Quotas []quotaRow + Members []orgMember + People int + + Error string + Version string + Now string +} + +// RegisterOrgPages mounts /org: an organization's own view of itself, for the +// people who manage it. +// +// Read-only on purpose. A manager sees their organization's use, members and +// limits — what they are accountable for — without being made an administrator +// of the whole cluster, and without anything on the page that changes who may +// do what. That stays with the cluster's administrators. +func (p *AccountPages) RegisterOrgPages(mux *http.ServeMux, s OrgViewer, priced 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 { + http.NotFound(w, r) + return + } + http.Redirect(w, r, "/org/"+orgs[0].ID, http.StatusSeeOther) + })) + mux.HandleFunc("GET /org/{id}", p.orgGuard(func(w http.ResponseWriter, r *http.Request, user store.User) { + id := r.PathValue("id") + if !user.IsAdmin { + ok, err := s.ManagesOrg(r.Context(), user.ID, id) + if err != nil || !ok { + // 404 rather than 403, like /admin: it does not confirm the + // organization exists to someone who may not see it. + http.NotFound(w, r) + return + } + } + p.renderOrg(w, r, s, user, id, priced) + })) +} + +// viewableOrgs is every organization an administrator may look at, or the +// ones a person manages. +func (p *AccountPages) viewableOrgs(ctx context.Context, s OrgViewer, user store.User) ([]store.Org, error) { + if user.IsAdmin { + return s.AllOrgs(ctx) + } + return s.ManagedOrgs(ctx, user.ID) +} + +func (p *AccountPages) orgGuard(next func(http.ResponseWriter, *http.Request, store.User)) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + noStore(w) + _, user, ok := p.session(r) + if !ok { + http.Redirect(w, r, "/login", http.StatusSeeOther) + return + } + next(w, p.withNav(r, user), user) + } +} + +func (p *AccountPages) renderOrg(w http.ResponseWriter, r *http.Request, s OrgViewer, + user store.User, orgID string, priced Priced, +) { + ctx := r.Context() + by := store.Grouping(r.URL.Query().Get("by")) + if by != store.GroupByProject && by != store.GroupByUser { + by = store.GroupByTeam + } + win := windowFor(r.URL.Query().Get("window")) + d := orgPageData{ + Nav: navFrom(r, "org"), + By: string(by), Window: win.Key, + Priced: priced != nil && priced.AnythingPriced(), + Version: p.version, + Now: time.Now().UTC().Format(time.RFC3339), + } + for _, g := range []store.Grouping{store.GroupByTeam, store.GroupByProject, store.GroupByUser} { + d.Groups = append(d.Groups, usageGroup{Key: string(g), Label: groupLabel(g), Selected: g == by}) + } + for _, uw := range usageWindows { + d.Windows = append(d.Windows, struct { + Key, Label string + Selected bool + }{uw.Key, uw.Label, uw.Key == win.Key}) + } + + d.Orgs, _ = p.viewableOrgs(ctx, s, user) + tree, err := s.Tree(ctx) + if err != nil { + p.log.Error("could not read the tree", "err", err) + d.Error = "could not read the organization" + } + var mine []store.TreeOrg + inOrg := map[string]string{} // scope:id -> path within the org + for _, o := range tree { + if o.ID != orgID { + continue + } + d.Org = store.Org{ID: o.ID, Name: o.Name} + mine = []store.TreeOrg{o} + inOrg["org:"+o.ID] = "the whole organization" + for _, t := range o.Teams { + inOrg["team:"+t.ID] = t.Name + for _, pr := range t.Projects { + inOrg["project:"+pr.ID] = t.Name + " / " + pr.Name + } + } + } + if d.Org.ID == "" { + http.NotFound(w, r) + return + } + + const quotaWindow = 30 * 24 * time.Hour + if limits, err := s.AllLimits(ctx); err == nil { + d.Quotas = quotaRows(mine, limits, p.spendByScope(ctx, s, time.Now().Add(-quotaWindow))) + } + + users, _ := s.Users(ctx, "") + byID := map[string]store.User{} + for _, u := range users { + byID[u.ID] = u + } + ms, _ := s.AllMemberships(ctx) + byPerson := map[string]*orgMember{} + for _, m := range ms { + where, ok := inOrg[string(m.Scope)+":"+m.ScopeID] + if !ok { + continue + } + om := byPerson[m.UserID] + if om == nil { + u := byID[m.UserID] + om = &orgMember{Email: u.Email, State: u.State} + byPerson[m.UserID] = om + } + om.Places = append(om.Places, orgPlace{Where: where, Role: m.Role}) + } + for _, om := range byPerson { + d.Members = append(d.Members, *om) + } + sort.Slice(d.Members, func(i, j int) bool { return d.Members[i].Email < d.Members[j].Email }) + d.People = len(byPerson) + + rows, err := s.UsageBucketsIn(ctx, by, orgID, time.Now().Add(-win.Span), win.Width) + if err != nil { + p.log.Error("could not read an organization's usage", "org", orgID, "err", err) + d.Error = "could not read usage" + } + d.Requests, d.Tokens, d.Spend, d.Rows = usageCharts(rows) + render(w, "org", d) +} diff --git a/internal/web/org_page_test.go b/internal/web/org_page_test.go new file mode 100644 index 0000000..1e419cb --- /dev/null +++ b/internal/web/org_page_test.go @@ -0,0 +1,106 @@ +package web + +import ( + "context" + "io" + "log/slog" + "net/http" + "strings" + "testing" + "time" + + "gitea.mynodes.xyz/ilker/llmgw/internal/store" +) + +// fakeOrgs: Acme (managed by u1) and Scania. +type fakeOrgs struct{ managers map[string]string } // org -> manager user id + +func (f fakeOrgs) ManagesOrg(_ context.Context, user, org string) (bool, error) { + return f.managers[org] == user, nil +} +func (f fakeOrgs) ManagedOrgs(_ context.Context, user string) ([]store.Org, error) { + var out []store.Org + for _, o := range []store.Org{{ID: "org-a", Name: "Acme"}, {ID: "org-s", Name: "Scania"}} { + if f.managers[o.ID] == user { + out = append(out, o) + } + } + return out, nil +} +func (f fakeOrgs) AllOrgs(context.Context) ([]store.Org, error) { + return []store.Org{{ID: "org-a", Name: "Acme"}, {ID: "org-s", Name: "Scania"}}, nil +} +func (f fakeOrgs) Tree(context.Context) ([]store.TreeOrg, error) { + return []store.TreeOrg{ + {ID: "org-a", Name: "Acme", Teams: []store.TreeTeam{{ID: "t1", Name: "ml", + Projects: []store.TreeProject{{ID: "p1", Name: "training"}}}}}, + {ID: "org-s", Name: "Scania"}, + }, nil +} +func (f fakeOrgs) AllMemberships(context.Context) ([]store.Membership, error) { + return []store.Membership{ + {UserID: "u1", Scope: store.ScopeOrg, ScopeID: "org-a", Role: store.RoleManager}, + {UserID: "u2", Scope: store.ScopeProject, ScopeID: "p1", Role: store.RoleMember}, + {UserID: "u3", Scope: store.ScopeOrg, ScopeID: "org-s", Role: store.RoleMember}, + }, nil +} +func (f fakeOrgs) Users(context.Context, store.UserState) ([]store.User, error) { + return []store.User{{ID: "u1", Email: "boss@acme"}, {ID: "u2", Email: "dev@acme"}, {ID: "u3", Email: "lars@scania"}}, nil +} +func (f fakeOrgs) AllLimits(context.Context) (map[string]store.Limits, error) { + return map[string]store.Limits{}, nil +} +func (f fakeOrgs) UsageBucketsIn(context.Context, store.Grouping, string, time.Time, time.Duration) ([]store.UsageSeries, error) { + return nil, nil +} +func (f fakeOrgs) UsageByScope(context.Context, store.Scope, time.Time) ([]store.ScopeUsage, error) { + return nil, nil +} + +func orgPage(t *testing.T, user store.User) *http.ServeMux { + t.Helper() + p := NewAccountPages(fakeAccounts{user: user}, nil, nil, "v0", slog.New(slog.NewTextHandler(io.Discard, nil))) + mux := http.NewServeMux() + p.RegisterOrgPages(mux, fakeOrgs{managers: map[string]string{"org-a": "u1"}}, nil) + return mux +} + +func TestAnOrganizationsManagerSeesItAndNothingElse(t *testing.T) { + mux := orgPage(t, store.User{ID: "u1", Email: "boss@acme"}) + w := mirrorGet(t, mux, "/org/org-a") + if w.Code != http.StatusOK { + t.Fatalf("the manager was refused their own organization: %d", w.Code) + } + body := w.Body.String() + for _, want := range []string{"

Acme

", "dev@acme", "training", `href="/org"`} { + if !strings.Contains(body, want) { + t.Errorf("the page lacks %q", want) + } + } + if strings.Contains(body, "lars@scania") { + t.Error("another organization's member is on the page") + } + if w := mirrorGet(t, mux, "/org/org-s"); w.Code != http.StatusNotFound { + t.Errorf("a manager of Acme could open Scania: %d", w.Code) + } + if w := mirrorGet(t, mux, "/org"); w.Code != http.StatusSeeOther || w.Header().Get("Location") != "/org/org-a" { + t.Errorf("/org should lead to the organization they manage: %d %q", w.Code, w.Header().Get("Location")) + } +} + +func TestSomebodyWhoManagesNothingGetsNoOrganizationPage(t *testing.T) { + mux := orgPage(t, store.User{ID: "u2", Email: "dev@acme"}) + for _, path := range []string{"/org", "/org/org-a"} { + if w := mirrorGet(t, mux, path); w.Code != http.StatusNotFound { + t.Errorf("%s: %d, want 404", path, w.Code) + } + } +} + +func TestAnAdministratorSeesEveryOrganization(t *testing.T) { + mux := orgPage(t, store.User{ID: "admin", Email: "a@x", IsAdmin: true}) + w := mirrorGet(t, mux, "/org/org-s") + if w.Code != http.StatusOK || !strings.Contains(w.Body.String(), "lars@scania") { + t.Errorf("an administrator could not see Scania: %d", w.Code) + } +} diff --git a/internal/web/static/base.css b/internal/web/static/base.css index de6da73..6559d38 100644 --- a/internal/web/static/base.css +++ b/internal/web/static/base.css @@ -444,3 +444,4 @@ td.where { max-width: 26rem; } h3.limits-for { font-size: .92rem; margin: 1rem 0 .4rem; } table.narrow-table { max-width: 30rem; } .q-act a + a { margin-left: .8rem; } +.chip-role { font-size: .68rem; font-weight: 600; color: var(--accent); text-transform: uppercase; letter-spacing: .04em; } diff --git a/internal/web/templates/admin_users.html b/internal/web/templates/admin_users.html index 19d0d95..9372bb4 100644 --- a/internal/web/templates/admin_users.html +++ b/internal/web/templates/admin_users.html @@ -283,7 +283,16 @@ access than it had, so they need a shell on the control plane — {{$u.Email}}{{if $u.IsAdmin}} admin{{end}} {{$u.State}} - {{range $u.Memberships}}{{.Path}} + {{range $u.Memberships}}{{.Path}}{{if eq .Role "manager"}} manager{{end}} + {{if eq (printf "%s" .Scope) "org"}} +
+ + + + +
+ {{end}}
diff --git a/internal/web/templates/nav.html b/internal/web/templates/nav.html index 50cd6b7..2d41f31 100644 --- a/internal/web/templates/nav.html +++ b/internal/web/templates/nav.html @@ -2,6 +2,10 @@ page you are on is marked, not offered, so moving between pages reads as moving within one program. */}} {{define "nav"}}