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:
@@ -33,6 +33,9 @@ type Session struct {
|
||||
IP string
|
||||
UserAgent string
|
||||
Team int64 // the team it works in (multi-tenant: switchable)
|
||||
// A platform admin viewing a team: until when, and why.
|
||||
ViewUntil time.Time
|
||||
ViewReason string
|
||||
}
|
||||
|
||||
// ErrNoSession is a cookie that leads nowhere: unknown, ended or expired,
|
||||
@@ -85,15 +88,15 @@ func expiry(created, seen time.Time) time.Time {
|
||||
return max
|
||||
}
|
||||
|
||||
const sessionCols = `id, user_id, created_at, seen_at, expires_at, ip, user_agent, team_id`
|
||||
const sessionCols = `id, user_id, created_at, seen_at, expires_at, ip, user_agent, team_id, view_until, view_reason`
|
||||
|
||||
func scanSession(row interface{ Scan(...any) error }) (*Session, error) {
|
||||
var s Session
|
||||
var created, seen, expires string
|
||||
if err := row.Scan(&s.ID, &s.User, &created, &seen, &expires, &s.IP, &s.UserAgent, &s.Team); err != nil {
|
||||
var created, seen, expires, until string
|
||||
if err := row.Scan(&s.ID, &s.User, &created, &seen, &expires, &s.IP, &s.UserAgent, &s.Team, &until, &s.ViewReason); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
s.Created, s.Seen, s.Expires = parseTS(created), parseTS(seen), parseTS(expires)
|
||||
s.Created, s.Seen, s.Expires, s.ViewUntil = parseTS(created), parseTS(seen), parseTS(expires), parseTS(until)
|
||||
return &s, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -183,3 +183,17 @@ func ReleaseDomain(d *db.DB, team int64) error {
|
||||
_, err := d.Exec(`UPDATE teams SET verified_at = '', verified_by = '' WHERE id = ?`, team)
|
||||
return err
|
||||
}
|
||||
|
||||
// ViewAs points a platform admin's session at a team for a while, with
|
||||
// the reason; team 0, and no time, ends it.
|
||||
func ViewAs(d *db.DB, sessionID string, team int64, until time.Time, reason string) error {
|
||||
_, err := d.Exec(`UPDATE sessions SET team_id = ?, view_until = ?, view_reason = ? WHERE id = ?`, team, tsOrEmpty(until), reason, sessionID)
|
||||
return err
|
||||
}
|
||||
|
||||
// TeamCounts are a team's members and pending invitations.
|
||||
func TeamCounts(d *db.DB, team int64) (members, invited int, err error) {
|
||||
err = d.QueryRow(`SELECT count(*), coalesce(sum(CASE WHEN u.status = 'invited' THEN 1 ELSE 0 END), 0) FROM memberships m JOIN users u ON u.id = m.user_id
|
||||
WHERE m.team_id = ? AND u.kind = 'person'`, team).Scan(&members, &invited)
|
||||
return members, invited, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user