Files
iam/internal/users/admin.go
zeekay e48704c976 users: rule who makes an org admin, and make a revoke take effect now
POST /v1/iam/users and PUT /v1/iam/users/{owner}/{name} answered 200 and
dropped user.isAdmin: the field was read from the calling code only, so a
SuperAdmin could not make hanzo/denis an admin and nothing said so.

One rule, users.Admin, now decides every change to an org-admin role: the
user create and update, SCIM, an admin membership, and an invitation that
carries the role. A SuperAdmin moves it anywhere; an org's admin moves it on
the others of a customer org; only a SuperAdmin makes an admin of a brand
(policy.IsPlatformOrg) or touches the admin org; nobody raises their own; the
last admin stays (409) unless a SuperAdmin takes them; applications, machines
and members are refused (403). The request is read for presence, so leaving
isAdmin out keeps it and a full-row write cannot demote by omission.

A revoke, a removed membership, an upsert lowering and a deleted account call
store.Void: User.Epoch moves to now, the account's token rows and sessions
are deleted, and verifyBearer refuses a token with iat <= epoch.

Invitations carry a role (member or admin), written only by who may grant it
and applied on signup and accept. GET /v1/iam/users takes isAdmin=true. Every
grant, revoke, role invitation, removal and void is an audit row under the
org, with actor, target, from, to, reason and request id.

Claude-Session: https://claude.ai/code/session_016DKtyuzxU43Gsc6Pm9vKTF
2026-11-02 12:07:57 -08:00

133 lines
4.9 KiB
Go

// Copyright 2026 Hanzo AI, Inc.
// SPDX-License-Identifier: MIT OR Apache-2.0
package users
import (
"context"
"strings"
policy "github.com/hanzoai/authz"
"github.com/hanzoai/orm"
"github.com/zap-proto/zip"
"github.com/hanzoai/iam/internal/principal"
"github.com/hanzoai/iam/pkg/store"
)
// Admin is THE rule for who may make the account owner/name an admin of org (next
// true) or stop it being one (next false). For the account's own org-admin flag,
// org is its owner; for an admin membership, the org the membership is in. Every
// writer that moves an org-admin role asks it: the user create and update, SCIM,
// an invitation that carries the role, and an admin role on the membership
// surface. A caller asks it only for a CHANGE; leaving a role where it is needs no
// authority.
//
// - A SuperAdmin (schema.User.SuperAdmin) moves it for any account in any org.
// - In the admin org, or for an account that lives there, only a SuperAdmin.
// - An application or a machine never moves it: org admin is a person's
// authority, granted by a person.
// - Anyone else must administer org (policy.Principal.AdminOf), and then:
// nobody makes themselves an admin; in an org the platform runs (a brand —
// hanzo, lux, zoo, pars — or another platform name, policy.IsPlatformOrg)
// only a SuperAdmin makes one, because an admin there holds the brand's
// tenancy and its pool; and taking away the org's last admin is refused (Last).
//
// A call with no principal is the embedding host's own, in process, and is
// trusted as Authorize trusts it. A request always carries one.
func Admin(ctx context.Context, db orm.DB, org, owner, name string, next bool) error {
p, ok := principal.From(ctx)
if !ok || (p.Sudo && p.App == nil) {
return nil
}
switch {
case org == policy.AdminOrg || owner == policy.AdminOrg:
return zip.ErrForbidden("only a SuperAdmin changes who administers the admin organization or an account in it")
case p.App != nil || p.Machine:
return zip.ErrForbidden("org admin is granted and revoked by a person who administers the organization, or by a SuperAdmin; an application or a machine does neither")
case !p.AdminOf(org):
return zip.ErrForbidden("only an admin of " + org + " or a SuperAdmin changes who administers it")
case next && p.Org == owner && strings.EqualFold(p.User, name):
return zip.ErrForbidden("nobody makes themselves an admin")
case next && policy.IsPlatformOrg(org):
return zip.ErrForbidden(org + " is run by the platform: only a SuperAdmin makes an admin there")
case next:
return nil
}
return Last(ctx, db, org, owner, name)
}
// Last refuses taking owner/name away as an admin of org when they are its last
// one: an org with no admin has nobody to run it but the platform. A SuperAdmin
// is not refused, since the platform must be able to take an org back. An
// account that is not counted as an admin (store.Admins) leaves nobody behind.
func Last(ctx context.Context, db orm.DB, org, owner, name string) error {
if p, ok := principal.From(ctx); ok && p.Sudo && p.App == nil {
return nil
}
admins, err := store.Admins(ctx, db, org)
if err != nil {
return zip.ErrInternal(err.Error())
}
counted, others := false, false
for _, id := range admins {
o, n, _ := strings.Cut(id, "/")
if o == owner && strings.EqualFold(n, name) {
counted = true
} else {
others = true
}
}
if counted && !others {
return zip.ErrConflict(owner + "/" + name + " is the last admin of " + org + "; make another admin first")
}
return nil
}
// keepsAnAdmin refuses removing the account owner/name while it is the last
// admin of an org it administers by membership: removing an account takes every
// membership it holds with it.
func keepsAnAdmin(ctx context.Context, db orm.DB, owner, name string) error {
rows, err := store.MembershipsByUser(ctx, db, owner+"/"+name)
if err != nil {
return zip.ErrInternal(err.Error())
}
for _, m := range rows {
if m == nil || m.Org == owner || m.Workspace != "" || m.Project != "" ||
(m.Role != store.RoleAdmin && m.Role != store.RoleOwner) {
continue
}
if err := Last(ctx, db, m.Org, owner, name); err != nil {
return err
}
}
return nil
}
// Act is the audit context of a privileged write reached through a typed op: the
// principal acting, the org acted in, why, and the request it rode on.
func Act(ctx context.Context, org, reason string) store.Act {
c := zip.CallerOf(ctx)
a := store.Act{Actor: actor(ctx), Org: org, Reason: reason, Request: c.RequestID, IP: c.IP}
if op, ok := zip.OpOf(ctx); ok {
a.Method = op.Method
}
if uri, ok := zip.AddressOf(ctx); ok {
a.URI = uri
}
return a
}
// actor names who is acting in ctx: "<org>/<name>" for a person,
// "app:<owner>/<name>" for a confidential client, "" for the host itself.
func actor(ctx context.Context) string {
p, ok := principal.From(ctx)
switch {
case !ok:
return ""
case p.App != nil:
return "app:" + p.App.Owner + "/" + p.App.Name
}
return p.Org + "/" + p.User
}