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:
1 parent
429c05a319
commit
098886e0e4
46 files changed
+2372
-468
No files matched your search
+131
-34
@@ -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
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user