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
+120
-19
@@ -5,6 +5,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -41,7 +42,9 @@ type AccountAdmin interface {
|
||||
DeleteOrg(ctx context.Context, id string) error
|
||||
DeleteTeam(ctx context.Context, id string) error
|
||||
DeleteProject(ctx context.Context, id string) error
|
||||
MoveUser(ctx context.Context, userID, projectID string) (int, error)
|
||||
AddMembership(ctx context.Context, userID string, scope store.Scope, scopeID, role, by string, at time.Time) error
|
||||
RemoveMembership(ctx context.Context, userID string, scope store.Scope, scopeID string, at time.Time) (int, error)
|
||||
AllMemberships(ctx context.Context) ([]store.Membership, error)
|
||||
DeleteUser(ctx context.Context, userID string, at time.Time) error
|
||||
AllKeys(ctx context.Context) ([]store.KeyRow, error)
|
||||
RevokeKey(ctx context.Context, id string, at time.Time) error
|
||||
@@ -50,7 +53,17 @@ type AccountAdmin interface {
|
||||
// userRow is an account as the page shows it: with where it is, not an id.
|
||||
type userRow struct {
|
||||
store.User
|
||||
ProjectPath string
|
||||
// Memberships are where the person is, by path; none is a person who uses
|
||||
// the gateway on their own.
|
||||
Memberships []store.Membership
|
||||
// Role is their role in the scope a list of members is drawn for.
|
||||
Role string
|
||||
}
|
||||
|
||||
// scopeOption is somewhere a person can be put, for a chooser.
|
||||
type scopeOption struct {
|
||||
Value string // "project:proj-…"
|
||||
Label string // "Acme / ml / training"
|
||||
}
|
||||
|
||||
// keyRow is a key as the page shows it.
|
||||
@@ -64,8 +77,12 @@ type adminUsersData struct {
|
||||
Pending []store.User
|
||||
Accounts []userRow
|
||||
Tree []store.TreeOrg
|
||||
// People and Keys are keyed by project id.
|
||||
People map[string][]userRow
|
||||
// Members is each scope's own members, keyed "project:<id>" and likewise
|
||||
// for teams and organizations; Keys is keyed by project id.
|
||||
Members map[string][]userRow
|
||||
// Scopes is every place a person can be added, organizations and teams
|
||||
// included.
|
||||
Scopes []scopeOption
|
||||
Keys map[string][]keyRow
|
||||
Projects []store.ProjectPath
|
||||
Orgs []store.Org
|
||||
@@ -105,12 +122,12 @@ func (p *AccountPages) RegisterAdminUsers(mux *http.ServeMux, s AccountAdmin) {
|
||||
mux.HandleFunc("POST /admin/users/{id}/approve", p.adminAction(s,
|
||||
func(ctx context.Context, admin store.User, id string, r *http.Request) (string, error) {
|
||||
project := r.FormValue("project")
|
||||
if project == "" {
|
||||
return "", errors.New("choose a project for their keys")
|
||||
}
|
||||
if err := s.ApproveUser(ctx, id, project, admin.Email, time.Now()); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if project == "" {
|
||||
return "approved, on their own for now", nil
|
||||
}
|
||||
return "approved into " + projectPath(ctx, s, project), nil
|
||||
}))
|
||||
|
||||
@@ -140,17 +157,35 @@ func (p *AccountPages) RegisterAdminUsers(mux *http.ServeMux, s AccountAdmin) {
|
||||
return "reinstated", nil
|
||||
}))
|
||||
|
||||
mux.HandleFunc("POST /admin/users/{id}/move", p.adminAction(s,
|
||||
func(ctx context.Context, _ store.User, id string, r *http.Request) (string, error) {
|
||||
project := r.FormValue("project")
|
||||
if project == "" {
|
||||
return "", errors.New("choose the project they go to")
|
||||
mux.HandleFunc("POST /admin/users/{id}/join", 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"), ":")
|
||||
if !ok || scopeID == "" {
|
||||
return "", errors.New("choose where they go")
|
||||
}
|
||||
moved, err := s.MoveUser(ctx, id, project)
|
||||
if err := s.AddMembership(ctx, id, store.Scope(scope), scopeID, store.RoleMember,
|
||||
admin.Email, time.Now()); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return "added to " + scopeLabel(ctx, s, store.Scope(scope), scopeID), 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"), ":")
|
||||
if !ok {
|
||||
return "", errors.New("nothing to leave")
|
||||
}
|
||||
label := scopeLabel(ctx, s, store.Scope(scope), scopeID)
|
||||
revoked, err := s.RemoveMembership(ctx, id, store.Scope(scope), scopeID, time.Now())
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return fmt.Sprintf("moved to %s, with %s", projectPath(ctx, s, project), plural(moved, "key", "keys")), nil
|
||||
msg := "removed from " + label
|
||||
if revoked > 0 {
|
||||
msg += "; " + plural(revoked, "key", "keys") + " they made there revoked"
|
||||
}
|
||||
return msg, nil
|
||||
}))
|
||||
|
||||
mux.HandleFunc("POST /admin/users/{id}/delete", p.adminAction(s,
|
||||
@@ -264,6 +299,25 @@ func (p *AccountPages) RegisterAdminUsers(mux *http.ServeMux, s AccountAdmin) {
|
||||
return "removed", nil
|
||||
}))
|
||||
|
||||
// The same as joining, from the scope's side: a form on a project, team or
|
||||
// organization that names the person.
|
||||
mux.HandleFunc("POST /admin/tenancy/{kind}/{id}/add", p.tenancyAction(s,
|
||||
func(ctx context.Context, r *http.Request) (string, error) {
|
||||
user := r.FormValue("user")
|
||||
if user == "" {
|
||||
return "", errors.New("choose who to add")
|
||||
}
|
||||
scope, id := store.Scope(r.PathValue("kind")), r.PathValue("id")
|
||||
if err := s.AddMembership(ctx, user, scope, id, store.RoleMember, "", time.Now()); err != nil {
|
||||
return "", err
|
||||
}
|
||||
who := user
|
||||
if u, err := s.UserByID(ctx, user); err == nil {
|
||||
who = u.Email
|
||||
}
|
||||
return who + " added to " + scopeLabel(ctx, s, scope, id), nil
|
||||
}))
|
||||
|
||||
mux.HandleFunc("POST /admin/keys/{id}/revoke", p.tenancyAction(s,
|
||||
func(ctx context.Context, r *http.Request) (string, error) {
|
||||
if err := s.RevokeKey(ctx, r.PathValue("id"), time.Now()); err != nil {
|
||||
@@ -285,6 +339,31 @@ func projectPath(ctx context.Context, s AccountAdmin, id string) string {
|
||||
return id
|
||||
}
|
||||
|
||||
// scopeLabel names an org, team or project by its path.
|
||||
func scopeLabel(ctx context.Context, s AccountAdmin, scope store.Scope, id string) string {
|
||||
switch scope {
|
||||
case store.ScopeProject:
|
||||
return projectPath(ctx, s, id)
|
||||
case store.ScopeTeam:
|
||||
if teams, err := s.AllTeams(ctx); err == nil {
|
||||
for _, t := range teams {
|
||||
if t.ID == id {
|
||||
return t.OrgName + " / " + t.Name
|
||||
}
|
||||
}
|
||||
}
|
||||
case store.ScopeOrg:
|
||||
if orgs, err := s.AllOrgs(ctx); err == nil {
|
||||
for _, o := range orgs {
|
||||
if o.ID == id {
|
||||
return o.Name
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return id
|
||||
}
|
||||
|
||||
// plainError says what a store error means to the person who caused it.
|
||||
func plainError(err error) error {
|
||||
switch {
|
||||
@@ -393,7 +472,7 @@ func (p *AccountPages) renderUsers(w http.ResponseWriter, r *http.Request,
|
||||
HasMirror: p.hasMirror,
|
||||
HasQuotas: p.hasQuotas,
|
||||
Now: time.Now().UTC().Format(time.RFC3339),
|
||||
People: map[string][]userRow{},
|
||||
Members: map[string][]userRow{},
|
||||
Keys: map[string][]keyRow{},
|
||||
}
|
||||
var err error
|
||||
@@ -415,14 +494,26 @@ func (p *AccountPages) renderUsers(w http.ResponseWriter, r *http.Request,
|
||||
|
||||
all, err := s.Users(ctx, "")
|
||||
logErr("the accounts", err)
|
||||
memberships, err := s.AllMemberships(ctx)
|
||||
logErr("the memberships", err)
|
||||
byUser := map[string][]store.Membership{}
|
||||
for _, m := range memberships {
|
||||
byUser[m.UserID] = append(byUser[m.UserID], m)
|
||||
}
|
||||
users := map[string]store.User{}
|
||||
for _, u := range all {
|
||||
if u.State == store.UserPending && u.ProjectID == "" {
|
||||
users[u.ID] = u
|
||||
if u.State == store.UserPending {
|
||||
d.Pending = append(d.Pending, u)
|
||||
continue
|
||||
}
|
||||
row := userRow{User: u, ProjectPath: paths[u.ProjectID]}
|
||||
d.Accounts = append(d.Accounts, row)
|
||||
d.People[u.ProjectID] = append(d.People[u.ProjectID], row)
|
||||
d.Accounts = append(d.Accounts, userRow{User: u, Memberships: byUser[u.ID]})
|
||||
}
|
||||
for _, m := range memberships {
|
||||
if u, ok := users[m.UserID]; ok {
|
||||
k := string(m.Scope) + ":" + m.ScopeID
|
||||
d.Members[k] = append(d.Members[k], userRow{User: u, Memberships: byUser[u.ID], Role: m.Role})
|
||||
}
|
||||
}
|
||||
|
||||
keys, err := s.AllKeys(ctx)
|
||||
@@ -438,6 +529,16 @@ func (p *AccountPages) renderUsers(w http.ResponseWriter, r *http.Request,
|
||||
logErr("the organizations", err)
|
||||
d.Teams, err = s.AllTeams(ctx)
|
||||
logErr("the teams", err)
|
||||
for _, o := range d.Orgs {
|
||||
d.Scopes = append(d.Scopes, scopeOption{"org:" + o.ID, o.Name})
|
||||
}
|
||||
for _, t := range d.Teams {
|
||||
d.Scopes = append(d.Scopes, scopeOption{"team:" + t.ID, t.OrgName + " / " + t.Name})
|
||||
}
|
||||
for _, pp := range d.Projects {
|
||||
d.Scopes = append(d.Scopes, scopeOption{"project:" + pp.ID, pp.Path})
|
||||
}
|
||||
sort.Slice(d.Scopes, func(i, j int) bool { return d.Scopes[i].Label < d.Scopes[j].Label })
|
||||
render(w, "admin_users", d)
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user