Phase 13, part 7: the platform's own admin
The people running the multi-tenant service (SUPERMARKET_PLATFORM_ADMINS, with two-factor sign-in) see every team at /platform with its size, people and what failed lately, and can suspend or restore a team, resend its invitations and release its domain. They don't see a team's data unless they view as it: with a reason, for at most an hour, read-only, recorded in the team's own audit log. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,224 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"supermarket/internal/audit"
|
||||
"supermarket/internal/users"
|
||||
)
|
||||
|
||||
// The platform's own admin (multi-tenant): the people running the service,
|
||||
// listed in SUPERMARKET_PLATFORM_ADMINS, at /platform, with two-factor
|
||||
// sign-in. They see the teams and their health, suspend a team, resend its
|
||||
// invitations, release a proved domain; they don't see a team's data,
|
||||
// unless they *view as* it: with a reason, for at most an hour, read-only,
|
||||
// and recorded in the team's own audit log.
|
||||
|
||||
const areaPlatform = "platform"
|
||||
|
||||
// maxViewAs is the longest a platform admin views a team.
|
||||
const maxViewAs = time.Hour
|
||||
|
||||
// isPlatformAdmin says whether a person runs the platform.
|
||||
func (s *Server) isPlatformAdmin(u *users.User) bool {
|
||||
if !s.multi() || s.Config == nil || u == nil || u.Kind != users.Person {
|
||||
return false
|
||||
}
|
||||
for _, a := range s.Config.PlatformAdmins {
|
||||
if strings.EqualFold(a, u.Email) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// readOnly is a membership that sees every module and changes nothing:
|
||||
// a platform admin viewing a team.
|
||||
func readOnly(team int64) users.Membership {
|
||||
m := users.Membership{Team: team, Role: users.Member, Modules: map[string]users.Access{}}
|
||||
for _, g := range grants() {
|
||||
m.Modules[g.ID] = users.View
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
// platformPaths are what a platform admin's session outside any team may
|
||||
// reach.
|
||||
func platformPath(p string) bool {
|
||||
return strings.HasPrefix(p, "/platform") || strings.HasPrefix(p, "/settings/account") || p == "/signout" ||
|
||||
strings.HasPrefix(p, "/users/") || strings.HasPrefix(p, "/static/") || p == "/favicon.ico"
|
||||
}
|
||||
|
||||
// teamRow is a team on the platform page.
|
||||
type teamRow struct {
|
||||
users.TeamInfo
|
||||
Members, Invited int
|
||||
Bytes int64
|
||||
FailedPosts int
|
||||
FailedEmails int
|
||||
FailedRequests int
|
||||
LastActivity time.Time
|
||||
Err string
|
||||
}
|
||||
|
||||
func (s *Server) platformPage(w http.ResponseWriter, r *http.Request) {
|
||||
v := viewerOf(r)
|
||||
if !v.Platform {
|
||||
s.forbidden(w, r)
|
||||
return
|
||||
}
|
||||
u, err := users.ByID(s.platform(), v.User.ID)
|
||||
if err != nil {
|
||||
s.serverError(w, err)
|
||||
return
|
||||
}
|
||||
if !u.TwoFactor {
|
||||
s.render(w, r, "platform", page{Title: "Platform", Nav: "platform", Data: map[string]any{"Need2FA": true}})
|
||||
return
|
||||
}
|
||||
list, err := users.ListTeams(s.platform())
|
||||
if err != nil {
|
||||
s.serverError(w, err)
|
||||
return
|
||||
}
|
||||
week := time.Now().AddDate(0, 0, -7).UTC()
|
||||
var rows []teamRow
|
||||
for _, t := range list {
|
||||
row := teamRow{TeamInfo: t}
|
||||
row.Members, row.Invited, _ = users.TeamCounts(s.platform(), t.ID)
|
||||
_ = s.platform().QueryRow(`SELECT coalesce(sum(pg_total_relation_size(c.oid)), 0) FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace
|
||||
WHERE n.nspname = ? AND c.relkind = 'r'`, s.Teams.Schema(t.ID)).Scan(&row.Bytes)
|
||||
// Health: what failed in the last week, counted, never read.
|
||||
if ts, err := s.forTeam(t.ID); err != nil {
|
||||
row.Err = err.Error()
|
||||
} else {
|
||||
wk := week.Format("2006-01-02T15:04:05Z")
|
||||
_ = ts.DB.QueryRow(`SELECT count(*) FROM drafts WHERE status = 'failed' AND updated_at >= ?`, wk).Scan(&row.FailedPosts)
|
||||
_ = ts.DB.QueryRow(`SELECT count(*) FROM outreach WHERE status = 'failed' AND created_at >= ?`, wk).Scan(&row.FailedEmails)
|
||||
_ = ts.DB.QueryRow(`SELECT count(*) FROM approvals WHERE status = 'failed' AND created_at >= ?`, week.Format("2006-01-02T15:04:05.000Z")).Scan(&row.FailedRequests)
|
||||
var last string
|
||||
_ = ts.DB.QueryRow(`SELECT coalesce(max(at), '') FROM audit_log`).Scan(&last)
|
||||
row.LastActivity, _ = time.Parse("2006-01-02T15:04:05.000Z", last)
|
||||
}
|
||||
rows = append(rows, row)
|
||||
}
|
||||
s.render(w, r, "platform", page{Title: "Platform", Nav: "platform", Data: map[string]any{"Teams": rows, "MaxView": int(maxViewAs / time.Minute)}})
|
||||
}
|
||||
|
||||
// platformAction: suspend, restore, resend, release-domain, view-as.
|
||||
func (s *Server) platformAction(w http.ResponseWriter, r *http.Request) {
|
||||
v := viewerOf(r)
|
||||
if !v.Platform {
|
||||
s.forbidden(w, r)
|
||||
return
|
||||
}
|
||||
if u, err := users.ByID(s.platform(), v.User.ID); err != nil || !u.TwoFactor {
|
||||
http.Redirect(w, r, "/platform", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
id, _ := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
||||
t, err := users.GetTeam(s.platform(), id)
|
||||
if err != nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
ts, err := s.forTeam(id)
|
||||
if err != nil {
|
||||
s.serverError(w, err)
|
||||
return
|
||||
}
|
||||
who := displayName(v.User) + " (the platform's admin)"
|
||||
log := func(action, detail string) {
|
||||
ts.record(audit.Entry{UserID: v.User.ID, UserName: who, Action: action, Target: t.Name, Detail: detail, IP: clientIP(r)})
|
||||
s.Log.Info("platform: "+action, "team", t.ID, "by", v.User.Email, "detail", detail)
|
||||
}
|
||||
now := time.Now()
|
||||
switch r.PathValue("action") {
|
||||
case "suspend":
|
||||
if err := users.SetTeamStatus(s.platform(), id, users.TeamSuspended, time.Time{}); err != nil {
|
||||
s.serverError(w, err)
|
||||
return
|
||||
}
|
||||
log("team-suspended", r.PostFormValue("reason"))
|
||||
redirect(w, r, "/platform", "team-suspended")
|
||||
case "restore":
|
||||
if err := users.SetTeamStatus(s.platform(), id, users.TeamActive, time.Time{}); err != nil {
|
||||
s.serverError(w, err)
|
||||
return
|
||||
}
|
||||
log("team-restored", "")
|
||||
redirect(w, r, "/platform", "team-restored")
|
||||
case "release-domain":
|
||||
if err := users.ReleaseDomain(s.platform(), id); err != nil {
|
||||
s.serverError(w, err)
|
||||
return
|
||||
}
|
||||
log("domain-released", t.Domain)
|
||||
redirect(w, r, "/platform", "domain-released")
|
||||
case "resend":
|
||||
members, err := users.Members(s.platform(), id)
|
||||
if err != nil {
|
||||
s.serverError(w, err)
|
||||
return
|
||||
}
|
||||
n := 0
|
||||
for _, m := range members {
|
||||
if m.Status == users.Invited && m.Kind == users.Person {
|
||||
if err := ts.sendInvitation(v, m.User); err != nil {
|
||||
s.serverError(w, err)
|
||||
return
|
||||
}
|
||||
n++
|
||||
}
|
||||
}
|
||||
log("invitations-resent", fmt.Sprintf("%d", n))
|
||||
redirectCounts(w, r, "/platform", "invitations-resent", n)
|
||||
case "view-as":
|
||||
reason := strings.TrimSpace(r.PostFormValue("reason"))
|
||||
mins, _ := strconv.Atoi(r.PostFormValue("minutes"))
|
||||
if reason == "" || mins <= 0 {
|
||||
redirect(w, r, "/platform", "view-needs-reason")
|
||||
return
|
||||
}
|
||||
d := time.Duration(mins) * time.Minute
|
||||
if d > maxViewAs {
|
||||
d = maxViewAs
|
||||
}
|
||||
if err := users.ViewAs(s.platform(), v.Session.ID, id, now.Add(d), reason); err != nil {
|
||||
s.serverError(w, err)
|
||||
return
|
||||
}
|
||||
log("viewed-as", fmt.Sprintf("%s (for %d minutes)", reason, int(d/time.Minute)))
|
||||
http.Redirect(w, r, "/", http.StatusSeeOther)
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
}
|
||||
|
||||
// platformStopViewing ends a view-as.
|
||||
func (s *Server) platformStopViewing(w http.ResponseWriter, r *http.Request) {
|
||||
v := viewerOf(r)
|
||||
if !v.Platform || v.Session == nil {
|
||||
s.forbidden(w, r)
|
||||
return
|
||||
}
|
||||
if err := users.ViewAs(s.platform(), v.Session.ID, 0, time.Time{}, ""); err != nil {
|
||||
s.serverError(w, err)
|
||||
return
|
||||
}
|
||||
http.Redirect(w, r, "/platform", http.StatusSeeOther)
|
||||
}
|
||||
|
||||
// teamClosed is what a suspended team's people see.
|
||||
func (s *Server) teamClosed(w http.ResponseWriter, r *http.Request, t *users.TeamInfo) {
|
||||
if r.Header.Get("HX-Request") != "" || !safeMethod(r.Method) {
|
||||
http.Error(w, "This team is "+t.Status+".", http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusForbidden)
|
||||
s.render(w, r, "team-closed", page{Title: t.Name, Bare: true, Data: t})
|
||||
}
|
||||
Reference in New Issue
Block a user