Let an admin build the tenancy tree, instead of sending them to a shell
CI / test (push) Successful in 1m35s
CI / test (push) Successful in 1m35s
An account is approved into a project, a project belongs to a team, a team to an organization — and until now the only way to make any of them was `llmgw keys init` on the control plane. So the first thing to do after installing was leave the interface you were already in, and an approval form with an empty dropdown gave no hint why. The accounts page now creates all three. Creating a container is not granting access: nobody can use one until an account is approved into it, which is the step already on this page. Quotas stay on the CLI. Making somewhere to put people is structural; raising what they may spend is financial, and a stolen admin session should be able to do the first and not the second. Ids are random now instead of slugs of the name, which fixes a bug that only appears when you have a second organization — which is exactly what this feature invites. Team ids were slugs in one flat namespace and PutTeam upserts on id, so creating "platform" under a second organization moved the first one's team into it. Nothing failed. The team changed owner. Create* is separate from Put* rather than replacing it. The upserts exist so `keys init` can be re-run while provisioning a machine; a person filling in a form means something else by a name that already exists, and gets told. A duplicate is refused among siblings only. Two companies both having a "platform" team is ordinary, and refusing the second would be the flat namespace again wearing a better error message. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
a83493bddb
commit
fc1b47976d
5 files changed
+566
-5
No files matched your search
@@ -17,12 +17,23 @@ type AccountAdmin interface {
|
||||
SetUserState(ctx context.Context, id string, state store.UserState) error
|
||||
DeleteUserSessions(ctx context.Context, userID string) error
|
||||
AllProjects(ctx context.Context) ([]store.ProjectPath, error)
|
||||
|
||||
// The tenancy tree an account is approved into. Creating it used to need a
|
||||
// shell — `llmgw keys init` — so the first thing an operator had to do
|
||||
// after installing was leave the interface they were already in.
|
||||
AllOrgs(ctx context.Context) ([]store.Org, error)
|
||||
AllTeams(ctx context.Context) ([]store.TeamPath, error)
|
||||
CreateOrg(ctx context.Context, name string) (string, error)
|
||||
CreateTeam(ctx context.Context, orgID, name string) (string, error)
|
||||
CreateProject(ctx context.Context, teamID, name string) (string, error)
|
||||
}
|
||||
|
||||
type adminUsersData struct {
|
||||
Pending []store.User
|
||||
Active []store.User
|
||||
Projects []store.ProjectPath
|
||||
Orgs []store.Org
|
||||
Teams []store.TeamPath
|
||||
Message string
|
||||
Error string
|
||||
CSRF string
|
||||
@@ -75,6 +86,45 @@ func (p *AccountPages) RegisterAdminUsers(mux *http.ServeMux, s AccountAdmin) {
|
||||
return "suspended, and signed out everywhere", nil
|
||||
}))
|
||||
|
||||
// The tenancy tree. Creating a container is not granting access: nobody can
|
||||
// use one until an account is approved into it, which is the step above and
|
||||
// already lives here. What is still not here is limits — raising a budget
|
||||
// is a financial act, and a stolen session must not be able to perform one.
|
||||
mux.HandleFunc("POST /admin/tenancy/org", p.tenancyAction(s,
|
||||
func(ctx context.Context, r *http.Request) (string, error) {
|
||||
id, err := s.CreateOrg(ctx, r.FormValue("name"))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return "organization " + r.FormValue("name") + " created as " + id, nil
|
||||
}))
|
||||
|
||||
mux.HandleFunc("POST /admin/tenancy/team", p.tenancyAction(s,
|
||||
func(ctx context.Context, r *http.Request) (string, error) {
|
||||
org := r.FormValue("org")
|
||||
if org == "" {
|
||||
return "", errors.New("choose the organization this team belongs to")
|
||||
}
|
||||
id, err := s.CreateTeam(ctx, org, r.FormValue("name"))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return "team " + r.FormValue("name") + " created as " + id, nil
|
||||
}))
|
||||
|
||||
mux.HandleFunc("POST /admin/tenancy/project", p.tenancyAction(s,
|
||||
func(ctx context.Context, r *http.Request) (string, error) {
|
||||
team := r.FormValue("team")
|
||||
if team == "" {
|
||||
return "", errors.New("choose the team this project belongs to")
|
||||
}
|
||||
id, err := s.CreateProject(ctx, team, r.FormValue("name"))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return "project " + r.FormValue("name") + " created as " + id, nil
|
||||
}))
|
||||
|
||||
mux.HandleFunc("POST /admin/users/{id}/reinstate", p.adminAction(s,
|
||||
func(ctx context.Context, _ store.User, id string, _ *http.Request) (string, error) {
|
||||
if err := s.SetUserState(ctx, id, store.UserActive); err != nil {
|
||||
@@ -84,6 +134,35 @@ func (p *AccountPages) RegisterAdminUsers(mux *http.ServeMux, s AccountAdmin) {
|
||||
}))
|
||||
}
|
||||
|
||||
// tenancyAction wraps a change to the tree: admin session, CSRF, audit, render.
|
||||
//
|
||||
// Its own wrapper rather than adminAction because that one is about a subject
|
||||
// account and looks its email up to name it in the log. There is no subject
|
||||
// here, and passing an empty id through it would write audit lines about
|
||||
// nobody.
|
||||
func (p *AccountPages) tenancyAction(s AccountAdmin,
|
||||
do func(ctx context.Context, r *http.Request) (string, error),
|
||||
) http.HandlerFunc {
|
||||
return p.adminOnly(func(w http.ResponseWriter, r *http.Request, admin store.User) {
|
||||
sess, _, _ := p.session(r)
|
||||
if !p.accounts.CheckCSRF(sess, r.FormValue("csrf")) {
|
||||
http.Error(w, "bad request", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
msg, err := do(r.Context(), r)
|
||||
if err != nil {
|
||||
if errors.Is(err, store.ErrDuplicateName) {
|
||||
err = errors.New("there is already one of those here")
|
||||
}
|
||||
p.log.Warn("a tenancy change was refused", "admin", admin.Email, "err", err)
|
||||
p.renderUsers(w, r, s, "", err.Error())
|
||||
return
|
||||
}
|
||||
p.log.Info("the tenancy tree was changed", "admin", admin.Email, "what", msg)
|
||||
p.renderUsers(w, r, s, msg, "")
|
||||
})
|
||||
}
|
||||
|
||||
// adminOnly is the guard every page under /admin shares.
|
||||
func (p *AccountPages) adminOnly(
|
||||
next func(w http.ResponseWriter, r *http.Request, admin store.User),
|
||||
@@ -162,5 +241,11 @@ func (p *AccountPages) renderUsers(w http.ResponseWriter, r *http.Request,
|
||||
if d.Projects, err = s.AllProjects(r.Context()); err != nil {
|
||||
p.log.Error("could not list projects", "err", err)
|
||||
}
|
||||
if d.Orgs, err = s.AllOrgs(r.Context()); err != nil {
|
||||
p.log.Error("could not list organizations", "err", err)
|
||||
}
|
||||
if d.Teams, err = s.AllTeams(r.Context()); err != nil {
|
||||
p.log.Error("could not list teams", "err", err)
|
||||
}
|
||||
render(w, "admin_users", d)
|
||||
}
|
||||
Reference in new issue
Block a user