Let a person belong to many places, or to none

A person was in exactly one project, chosen at approval, and could not
use the gateway before that. People belong to several teams and to
none, so:

- memberships: a person is a member of any number of organizations,
  teams and projects, or of none; a member of a team or organization
  may use every project in it. users join/leave, and the people page
  adds and takes out from either side. Approval no longer needs a
  project.
- personal keys: a key may have an owner and no project. /profile asks
  what a new key spends against; keys create --for does the same.
  auth.personal sets default limits for personal use or turns it off.
- limits for people: --scope user holds a person across every key,
  project and the chat page; --per-member on an org, team or project
  gives each member their own share there. Resolution folds the tree,
  each per-member limit for the key's owner, and the owner's own, each
  in its own pool; the quotas page shows and sets both.
- the chat page asks what to spend against and checks it per message.
- usage records who spent it, so a person's budget survives a restart,
  personal use and chat included; restore no longer drops rows with no
  project.

Leaving a project revokes the keys made there unless another membership
still reaches it. A personal key keeps no threads or notes, which are
per project, and is refused rather than dropped into the anonymous pool.

Migration 0010 turns each person's project into a membership, adds
usage.user_id and backfills it, and makes api_keys.project_id nullable.
SQLite cannot drop NOT NULL, so the table is rebuilt; that needs foreign
keys off, which only works outside a transaction, so migrations marked
for it run on one connection with foreign_key_check before commit.
Tested on a populated SQLite store with claims, and on a copy of the
cluster's PostgreSQL store: 5 people became members, 17 keys and 3
claims intact, 145 of 577 usage rows and all 69 chat rows attributed.
The PostgreSQL store tests, including new membership cases, pass on 16.

users move is gone; join and leave replace it.

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:03:46 +02:00
1 parent 429c05a319
commit 098886e0e4
46 files changed
+2372 -468

No files matched your search

+53 -10
View File
@@ -85,7 +85,7 @@ func newKeyInitCmd(open opener) *cobra.Command {
}
func newKeyCreateCmd(open opener) *cobra.Command {
var project, band, profile, baseURL string
var project, band, profile, baseURL, forUser string
var expires, claimTTL time.Duration
var claim bool
@@ -108,9 +108,22 @@ func newKeyCreateCmd(open opener) *cobra.Command {
}
defer s.Close()
p, err := resolveProject(cmd.Context(), s, project)
if err != nil {
return err
// Three kinds: an operator's key in a project, a person's key in a
// project they are a member of, and a person's personal key.
var owner store.User
if forUser != "" {
if owner, err = resolveUser(cmd.Context(), s, forUser); err != nil {
return err
}
}
var p store.ProjectPath
if project != "" || forUser == "" {
if p, err = resolveProject(cmd.Context(), s, project); err != nil {
return err
}
}
if p.ID == "" {
p.Path = "personal, " + owner.Email
}
secret, k, err := store.NewKey(randomID("key"), args[0], p.ID)
if err != nil {
@@ -121,7 +134,15 @@ func newKeyCreateCmd(open opener) *cobra.Command {
if expires > 0 {
k.ExpiresAt = k.CreatedAt.Add(expires)
}
if err := s.CreateKey(cmd.Context(), k); err != nil {
if owner.ID != "" {
err = s.CreateUserKey(cmd.Context(), owner.ID, k)
} else {
err = s.CreateKey(cmd.Context(), k)
}
if errors.Is(err, store.ErrNotMember) {
return fmt.Errorf("%s is not a member of %s; `llmgw users join` first", owner.Email, p.Path)
}
if err != nil {
return fmt.Errorf("creating the key in %s: %w", p.Path, err)
}
@@ -161,6 +182,8 @@ func newKeyCreateCmd(open opener) *cobra.Command {
}
cmd.Flags().StringVar(&project, "project", "",
"project the key belongs to: id, path (Acme/platform/gateway) or unique name")
cmd.Flags().StringVar(&forUser, "for", "",
"a person the key belongs to (id or email); without --project it is their personal key")
cmd.Flags().StringVar(&band, "band", "", "admission band; empty inherits")
cmd.Flags().StringVar(&profile, "profile", "", "routing profile; empty inherits")
cmd.Flags().DurationVar(&expires, "expires-in", 0, "lifetime; 0 never expires")
@@ -200,8 +223,12 @@ func newKeyListCmd(open opener) *cobra.Command {
fmt.Fprintln(w, "ID\tNAME\tKEY\tPROJECT\tOWNER\tSTATE\tLAST USED")
now := time.Now()
for _, r := range rows {
where := r.ProjectPath
if where == "" {
where = "personal"
}
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%s\t%s\n", r.ID, r.Name, r.Prefix,
r.ProjectPath, dash(r.OwnerEmail), keyState(r.Key, now), ago(r.LastUsedAt, now))
where, dash(r.OwnerEmail), keyState(r.Key, now), ago(r.LastUsedAt, now))
}
return w.Flush()
}
@@ -260,6 +287,7 @@ func newKeyRevokeCmd(open opener) *cobra.Command {
func newLimitsCmd(open opener) *cobra.Command {
var scope, id, period string
var perMember bool
var rpm, tpm int
var budget float64
var models []string
@@ -298,14 +326,27 @@ func newLimitsCmd(open opener) *cobra.Command {
return err
}
scopeID = p.ID
case store.ScopeUser:
u, err := resolveUser(cmd.Context(), s, id)
if err != nil {
return err
}
scopeID = u.ID
case store.ScopeKey:
if id == "" {
return errors.New("--scope key needs --id, the key's id")
}
default:
return fmt.Errorf("--scope is org, team, project or key, not %q", scope)
return fmt.Errorf("--scope is org, team, project, user or key, not %q", scope)
}
l := store.Limits{Scope: store.Scope(scope), ScopeID: scopeID}
level := store.Scope(scope)
if perMember {
// Written on the scope, applied to each member separately.
if level = store.MemberScope(level); level == "" {
return errors.New("--per-member is for --scope org, team or project")
}
}
l := store.Limits{Scope: level, ScopeID: scopeID}
if cmd.Flags().Changed("rpm") {
l.RPM = &rpm
}
@@ -330,11 +371,13 @@ func newLimitsCmd(open opener) *cobra.Command {
if err := s.PutLimits(cmd.Context(), l); err != nil {
return err
}
fmt.Fprintf(cmd.OutOrStdout(), "limits set on %s %s\n", scope, scopeID)
fmt.Fprintf(cmd.OutOrStdout(), "limits set on %s %s\n", level, scopeID)
return nil
},
}
cmd.Flags().StringVar(&scope, "scope", "project", "org | team | project | key")
cmd.Flags().StringVar(&scope, "scope", "project", "org | team | project | user | key")
cmd.Flags().BoolVar(&perMember, "per-member", false,
"with org, team or project: a limit for each member there, not one shared by all")
cmd.Flags().StringVar(&id, "id", "", "which one: id, path or unique name (a key's id for --scope key)")
cmd.Flags().IntVar(&rpm, "rpm", 0, "requests per minute")
cmd.Flags().IntVar(&tpm, "tpm", 0, "tokens per minute")
+45 -18
View File
@@ -46,21 +46,40 @@ func newOrgTreeCmd(open dbOpener) *cobra.Command {
if err != nil {
return err
}
var byProject map[string][]store.User
var members map[string][]string
var alone []store.User
if people {
users, err := s.Users(cmd.Context(), "")
if err != nil {
return err
}
byProject = map[string][]store.User{}
all, err := s.AllMemberships(cmd.Context())
if err != nil {
return err
}
email := map[string]string{}
for _, u := range users {
byProject[u.ProjectID] = append(byProject[u.ProjectID], u)
email[u.ID] = u.Email + " " + string(u.State)
if u.IsAdmin {
email[u.ID] += " admin"
}
}
members = map[string][]string{}
in := map[string]bool{}
for _, m := range all {
members[string(m.Scope)+":"+m.ScopeID] = append(members[string(m.Scope)+":"+m.ScopeID], email[m.UserID])
in[m.UserID] = true
}
for _, u := range users {
if !in[u.ID] {
alone = append(alone, u)
}
}
}
printTree(cmd.OutOrStdout(), tree, byProject)
if people && len(byProject[""]) > 0 {
fmt.Fprintf(cmd.OutOrStdout(), "\nwaiting for approval\n")
for _, u := range byProject[""] {
printTree(cmd.OutOrStdout(), tree, members)
if people && len(alone) > 0 {
fmt.Fprintf(cmd.OutOrStdout(), "\non their own\n")
for _, u := range alone {
fmt.Fprintf(cmd.OutOrStdout(), " %s %s %s\n", u.Email, u.State, u.ID)
}
}
@@ -71,36 +90,44 @@ func newOrgTreeCmd(open dbOpener) *cobra.Command {
return cmd
}
func printTree(w io.Writer, tree []store.TreeOrg, people map[string][]store.User) {
func printTree(w io.Writer, tree []store.TreeOrg, members map[string][]string) {
if len(tree) == 0 {
fmt.Fprintln(w, "no organizations yet; `llmgw org add org <name>` starts one")
return
}
list := func(indent, key string) {
for _, m := range members[key] {
fmt.Fprintf(w, "%s%s\n", indent, m)
}
}
for _, o := range tree {
fmt.Fprintf(w, "%s (%s)\n", o.Name, o.ID)
fmt.Fprintf(w, "%s (%s)%s\n", o.Name, o.ID, membersNote(o.Members))
list(" · ", "org:"+o.ID)
if len(o.Teams) == 0 {
fmt.Fprintln(w, " no teams")
}
for _, t := range o.Teams {
fmt.Fprintf(w, " %s (%s)\n", t.Name, t.ID)
fmt.Fprintf(w, " %s (%s)%s\n", t.Name, t.ID, membersNote(t.Members))
list(" · ", "team:"+t.ID)
if len(t.Projects) == 0 {
fmt.Fprintln(w, " no projects")
}
for _, p := range t.Projects {
fmt.Fprintf(w, " %s (%s) %s, %s\n", p.Name, p.ID,
countOf(p.Users, "account"), keysOf(p))
for _, u := range people[p.ID] {
admin := ""
if u.IsAdmin {
admin = " admin"
}
fmt.Fprintf(w, " %s %s%s\n", u.Email, u.State, admin)
}
countOf(p.Users, "member"), keysOf(p))
list(" · ", "project:"+p.ID)
}
}
}
}
func membersNote(n int) string {
if n == 0 {
return ""
}
return " " + countOf(n, "member")
}
func keysOf(p store.TreeProject) string {
s := countOf(p.ActiveKeys, "key")
if revoked := p.Keys - p.ActiveKeys; revoked > 0 {
+12 -3
View File
@@ -51,15 +51,24 @@ func TestTheTreeIsManagedByNameNotId(t *testing.T) {
mustRun(t, db, "keys", "create", "laptop", "--project", "gateway")
out := mustRun(t, db, "org", "tree", "--people")
for _, want := range []string{"Acme", "gateway", "1 account, 1 key", "ayse@example.com"} {
for _, want := range []string{"Acme", "gateway", "1 member, 1 key", "ayse@example.com"} {
if !strings.Contains(out, want) {
t.Errorf("tree lacks %q:\n%s", want, out)
}
}
out = mustRun(t, db, "users", "move", "ayse@example.com", "--project", "research")
out = mustRun(t, db, "users", "join", "ayse@example.com", "--project", "research")
if !strings.Contains(out, "Scania / platform / research") {
t.Errorf("move said: %s", out)
t.Errorf("join said: %s", out)
}
mustRun(t, db, "users", "leave", "ayse@example.com", "--project", "Acme/platform/gateway")
// She now uses the gateway through research alone; a personal key is hers.
mustRun(t, db, "keys", "create", "mine", "--for", "ayse@example.com")
if out := mustRun(t, db, "keys", "list", "--all"); !strings.Contains(out, "personal") {
t.Errorf("a personal key is not listed as one:\n%s", out)
}
if _, err := run(t, db, "keys", "create", "x", "--for", "ayse@example.com", "--project", "gateway"); err == nil {
t.Error("a key was made for her in a project she left")
}
mustRun(t, db, "org", "rename", "project", "research", "lab")
+2 -1
View File
@@ -59,7 +59,8 @@ func TestInitLeavesAControlPlaneThatStarts(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if !u.IsAdmin || u.State != store.UserActive || u.ProjectID != "default" {
inDefault, _ := s.CanUseProject(context.Background(), u.ID, "default")
if !u.IsAdmin || u.State != store.UserActive || !inDefault {
t.Errorf("administrator not ready to use: %+v", u)
}
if !strings.Contains(out.String(), "password: ") {
+131 -34
View File
@@ -1,7 +1,9 @@
package cli
import (
"errors"
"fmt"
"strings"
"text/tabwriter"
"time"
@@ -33,7 +35,7 @@ func newUsersCmd() *cobra.Command {
newUserStateCmd(open, "reinstate", store.UserActive,
"Let a suspended account work again"),
newUserCreateCmd(open), newUserResetPasswordCmd(open),
newUserMoveCmd(open), newUserDeleteCmd(open),
newUserJoinCmd(open), newUserLeaveCmd(open), newUserDeleteCmd(open),
newUserAdminCmd(open, "promote", true, "Let an account see the cluster dashboard at /admin"),
newUserAdminCmd(open, "demote", false, "Take away access to the cluster dashboard"))
return cmd
@@ -67,16 +69,28 @@ func newUserListCmd(open userOpener) *cobra.Command {
return nil
}
all, err := s.AllMemberships(cmd.Context())
if err != nil {
return err
}
where := map[string][]string{}
for _, m := range all {
where[m.UserID] = append(where[m.UserID], m.Path)
}
w := tabwriter.NewWriter(cmd.OutOrStdout(), 0, 0, 2, ' ', 0)
fmt.Fprintln(w, "ID\tEMAIL\tNAME\tSTATE\tADMIN\tPROJECT\tSIGNED UP\tLAST LOGIN")
fmt.Fprintln(w, "ID\tEMAIL\tSTATE\tADMIN\tMEMBER OF\tSIGNED UP\tLAST LOGIN")
now := time.Now()
for _, u := range users {
admin := "-"
if u.IsAdmin {
admin = "yes"
}
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n",
u.ID, u.Email, u.Name, u.State, admin, dash(u.ProjectID),
member := "on their own"
if ms := where[u.ID]; len(ms) > 0 {
member = strings.Join(ms, "; ")
}
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%s\t%s\n",
u.ID, u.Email, u.State, admin, member,
ago(u.CreatedAt, now), ago(u.LastLoginAt, now))
}
return w.Flush()
@@ -91,9 +105,9 @@ func newUserApproveCmd(open userOpener) *cobra.Command {
cmd := &cobra.Command{
Use: "approve <user>",
Short: "Let an account sign in and issue its own keys",
Long: "The project is required: it decides which limits this person's keys\n" +
"inherit. An account approved into no project would pass every check and\n" +
"then fail at the first key it tried to make.",
Long: "A project is optional. Approved into nothing, a person uses the gateway on\n" +
"their own — personal keys and chat within their personal limits — and can\n" +
"be added to organizations, teams and projects with 'llmgw users join'.",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
s, err := open(cmd)
@@ -106,20 +120,26 @@ func newUserApproveCmd(open userOpener) *cobra.Command {
if err != nil {
return err
}
p, err := resolveProject(cmd.Context(), s, project)
if err != nil {
return err
var p store.ProjectPath
if project != "" {
if p, err = resolveProject(cmd.Context(), s, project); err != nil {
return err
}
}
if err := s.ApproveUser(cmd.Context(), u.ID, p.ID, by, time.Now()); err != nil {
return fmt.Errorf("approving %s: %w", u.Email, err)
}
fmt.Fprintf(cmd.OutOrStdout(), "%s approved into %s\n", u.Email, p.Path)
if p.ID == "" {
fmt.Fprintf(cmd.OutOrStdout(), "%s approved, on their own for now\n", u.Email)
} else {
fmt.Fprintf(cmd.OutOrStdout(), "%s approved into %s\n", u.Email, p.Path)
}
cmd.PrintErrln("They can sign in and create their own keys from /profile.")
return nil
},
}
cmd.Flags().StringVar(&project, "project", "",
"project the person's keys belong to: id, path (Acme/platform/gateway) or unique name")
"a project to put them in (optional): id, path (Acme/platform/gateway) or unique name")
cmd.Flags().StringVar(&by, "by", "", "who approved, for the record")
return cmd
}
@@ -254,9 +274,11 @@ func newUserCreateCmd(open userOpener) *cobra.Command {
}
defer s.Close()
p, err := resolveProject(cmd.Context(), s, project)
if err != nil {
return err
var p store.ProjectPath
if project != "" {
if p, err = resolveProject(cmd.Context(), s, project); err != nil {
return err
}
}
hash, err := account.HashPassword(password)
if err != nil {
@@ -274,27 +296,64 @@ func newUserCreateCmd(open userOpener) *cobra.Command {
return err
}
fmt.Fprintf(cmd.OutOrStdout(), "%s\n", id)
cmd.PrintErrf("%s created and approved into %s\n", args[0], p.Path)
if p.ID == "" {
cmd.PrintErrf("%s created and approved, on their own for now\n", args[0])
} else {
cmd.PrintErrf("%s created and approved into %s\n", args[0], p.Path)
}
return nil
},
}
cmd.Flags().StringVar(&project, "project", "",
"project the person's keys belong to: id, path or unique name (optional when there is one)")
"a project to put them in (optional): id, path or unique name")
cmd.Flags().StringVar(&password, "password", "", "initial password")
return cmd
}
func newUserMoveCmd(open userOpener) *cobra.Command {
var project string
// scopeFlags names one org, team or project by whichever flag is given.
type scopeFlags struct{ org, team, project string }
func (f *scopeFlags) bind(cmd *cobra.Command) {
cmd.Flags().StringVar(&f.org, "org", "", "an organization: id or name")
cmd.Flags().StringVar(&f.team, "team", "", "a team: id, path (Acme/ml) or unique name")
cmd.Flags().StringVar(&f.project, "project", "", "a project: id, path (Acme/ml/training) or unique name")
}
func (f *scopeFlags) resolve(cmd *cobra.Command, s *store.DB) (store.Scope, string, string, error) {
n := 0
for _, v := range []string{f.org, f.team, f.project} {
if v != "" {
n++
}
}
if n != 1 {
return "", "", "", fmt.Errorf("give exactly one of --org, --team or --project")
}
ctx := cmd.Context()
switch {
case f.org != "":
o, err := resolveOrg(ctx, s, f.org)
return store.ScopeOrg, o.ID, o.Name, err
case f.team != "":
t, err := resolveTeam(ctx, s, f.team)
return store.ScopeTeam, t.ID, t.OrgName + " / " + t.Name, err
default:
p, err := resolveProject(ctx, s, f.project)
return store.ScopeProject, p.ID, p.Path, err
}
}
func newUserJoinCmd(open userOpener) *cobra.Command {
var where scopeFlags
var role, by string
cmd := &cobra.Command{
Use: "move <user> --project <project>",
Short: "Put an approved account in another project, with the keys it made",
Long: "The person's own keys go with them, so they stop spending the old project's\n" +
"budget. Keys an operator issued in the old project stay there. Usage already\n" +
"recorded is not rewritten. A running server picks the change up when its key\n" +
"cache expires (auth.cache_ttl).",
Example: " llmgw users move ayse@example.com --project Acme/ml/research",
Args: cobra.ExactArgs(1),
Use: "join <user> --org|--team|--project <which>",
Short: "Make a person a member of an organization, team or project",
Long: "A person can be a member of any number of them. A member of a team or\n" +
"organization may use every project in it.",
Example: " llmgw users join ayse@example.com --team Acme/ml\n" +
" llmgw users join ayse@example.com --project Scania/research/vision",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
s, err := open(cmd)
if err != nil {
@@ -305,21 +364,59 @@ func newUserMoveCmd(open userOpener) *cobra.Command {
if err != nil {
return err
}
p, err := resolveProject(cmd.Context(), s, project)
scope, id, label, err := where.resolve(cmd, s)
if err != nil {
return err
}
moved, err := s.MoveUser(cmd.Context(), u.ID, p.ID)
if err != nil {
if err := s.AddMembership(cmd.Context(), u.ID, scope, id, role, by, time.Now()); err != nil {
return err
}
fmt.Fprintf(cmd.OutOrStdout(), "%s is now in %s (%s moved with them)\n",
u.Email, p.Path, countOf(moved, "key"))
fmt.Fprintf(cmd.OutOrStdout(), "%s is a %s of %s\n", u.Email, role, label)
return nil
},
}
cmd.Flags().StringVar(&project, "project", "", "where they go: id, path or unique name")
_ = cmd.MarkFlagRequired("project")
where.bind(cmd)
cmd.Flags().StringVar(&role, "role", store.RoleMember, "member (manager is reserved)")
cmd.Flags().StringVar(&by, "by", "", "who added them, for the record")
return cmd
}
func newUserLeaveCmd(open userOpener) *cobra.Command {
var where scopeFlags
cmd := &cobra.Command{
Use: "leave <user> --org|--team|--project <which>",
Short: "Take a person out of an organization, team or project",
Long: "Keys they made in a project they can no longer use are revoked; a project\n" +
"they still reach through another membership keeps them. Their personal\n" +
"keys are not touched.",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
s, err := open(cmd)
if err != nil {
return err
}
defer s.Close()
u, err := resolveUser(cmd.Context(), s, args[0])
if err != nil {
return err
}
scope, id, label, err := where.resolve(cmd, s)
if err != nil {
return err
}
revoked, err := s.RemoveMembership(cmd.Context(), u.ID, scope, id, time.Now())
if errors.Is(err, store.ErrNotFound) {
return fmt.Errorf("%s is not a member of %s itself", u.Email, label)
}
if err != nil {
return err
}
fmt.Fprintf(cmd.OutOrStdout(), "%s is no longer in %s (%s revoked)\n",
u.Email, label, countOf(revoked, "key"))
return nil
},
}
where.bind(cmd)
return cmd
}