Files
Sebastian UnterschützandClaude Opus 5.5 8d7c1f856c
ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Successful in 9m21s
ci / test (pull_request) Successful in 13m53s
ci / build (pull_request) Successful in 11m22s
ci / image (pull_request) Successful in 4m36s
feat: terms of service and versioned consent; data rights for every account
Terms of service join the legal notice and privacy policy, for the
platform only. Every account accepts the platform's privacy policy and
terms: on sign-up (also through an invitation) by ticking a box for the
versions shown, and after a sign-in through a provider right away. When
an admin saves a material change with "everyone has to accept again",
the document gets a new version and each account is asked at its next
request; corrections ask nobody. Acceptances are kept per version with
their time, as proof, and are part of the data export.

Until an account accepted, and while it is suspended, it can read the
documents, export its data, delete itself and sign out, nothing else:
pages lead to /consent or /account/suspended, other API calls answer 403
consent_required or account_suspended. Suspended accounts therefore sign
in again, by password or provider; their API tokens stay blocked.

Deleting an account deletes the organizations it owns, monitors and all,
also when they have other members (each organization has one owner); it
no longer refuses. The account settings name those organizations first.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
2026-11-23 22:26:20 +01:00

305 lines
8.7 KiB
Go

package api
import (
"context"
"errors"
"net/http"
"strings"
"time"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/jackc/pgx/v5"
"git.zb-server.de/ZB-Server/uptimesebi/internal/store"
"git.zb-server.de/ZB-Server/uptimesebi/internal/tenant"
"git.zb-server.de/ZB-Server/uptimesebi/internal/web"
)
// Platform admins moderate public status pages and the organizations and
// accounts behind them: suspending hides or stops them and can be undone,
// deleting cannot. Suspended pages leave status page servers when these
// next ask for them, within half a minute.
type moderatedPage struct {
ID uuid.UUID `json:"id"`
Slug string `json:"slug"`
Title string `json:"title"`
Published bool `json:"published"`
Domain string `json:"custom_domain,omitempty"`
SuspendedAt *time.Time `json:"suspended_at"`
Reason string `json:"suspended_reason,omitempty"`
CreatedAt time.Time `json:"created_at"`
Org moderatedOrg `json:"org"`
Owner *moderatedUser `json:"owner"`
}
type moderatedOrg struct {
ID uuid.UUID `json:"id"`
Slug string `json:"slug"`
Name string `json:"name"`
SuspendedAt *time.Time `json:"suspended_at"`
Reason string `json:"suspended_reason,omitempty"`
}
type moderatedUser struct {
ID uuid.UUID `json:"id"`
Email string `json:"email"`
SuspendedAt *time.Time `json:"suspended_at"`
Reason string `json:"suspended_reason,omitempty"`
// Admin marks platform admins, who cannot be suspended or deleted.
Admin bool `json:"admin,omitempty"`
}
func (s *Server) moderatedPages(ctx context.Context, search string, pg pageReq) ([]moderatedPage, pageInfo, error) {
var rows []store.AdminListStatusPagesRow
err := s.db.Tx(ctx, tenant.System(), func(q *store.Queries) error {
var err error
rows, err = q.AdminListStatusPages(ctx, store.AdminListStatusPagesParams{
Search: truncate(strings.TrimSpace(search), 200), MaxRows: pg.fetch(),
AfterT: pg.afterT(), AfterID: pg.afterID(), BeforeT: pg.beforeT(), BeforeID: pg.beforeID(),
})
return err
})
rows, info := paginate(pg, rows, func(r store.AdminListStatusPagesRow) cursor { return cursor{T: r.CreatedAt, ID: r.ID} })
out := make([]moderatedPage, len(rows))
for i, r := range rows {
p := &out[i]
p.ID, p.Slug, p.Title, p.Published, p.SuspendedAt, p.Reason, p.CreatedAt = r.ID, r.Slug, r.Title, r.Published, r.SuspendedAt, r.SuspendedReason, r.CreatedAt
if r.CustomDomain != nil && r.DomainVerifiedAt != nil {
p.Domain = *r.CustomDomain
}
p.Org = moderatedOrg{r.OrgID, r.OrgSlug, r.OrgName, r.OrgSuspendedAt, r.OrgSuspendedReason}
if r.OwnerID != uuid.Nil {
p.Owner = &moderatedUser{r.OwnerID, r.OwnerEmail, r.OwnerDisabledAt, r.OwnerDisabledReason, r.OwnerIsAdmin}
}
}
return out, info, err
}
func (s *Server) adminListStatusPages(c *gin.Context) {
pg, ok := parsePage(c, apiPageSize)
if !ok {
return
}
pages, info, err := s.moderatedPages(c.Request.Context(), c.Query("q"), pg)
if err != nil {
internalError(c, err)
return
}
c.JSON(http.StatusOK, gin.H{"status_pages": pages, "next_cursor": info.Next, "prev_cursor": info.Prev})
}
type suspensionRequest struct {
Suspended bool `json:"suspended"`
Reason string `json:"reason" binding:"max=500"`
}
// suspension binds a suspension request and the target ID.
func suspension(c *gin.Context) (uuid.UUID, suspensionRequest, bool) {
id, err := uuid.Parse(c.Param("id"))
if err != nil {
fail(c, http.StatusNotFound, "not_found")
return uuid.Nil, suspensionRequest{}, false
}
var req suspensionRequest
if !bind(c, &req) {
return uuid.Nil, req, false
}
req.Reason = strings.TrimSpace(req.Reason)
auditDetail(c, "suspended", req.Suspended)
if req.Reason != "" {
auditDetail(c, "reason", req.Reason)
}
return id, req, true
}
// moderated finishes a moderation request: 404 for unknown targets.
func moderated(c *gin.Context, err error) bool {
if errors.Is(err, pgx.ErrNoRows) {
fail(c, http.StatusNotFound, "not_found")
return false
}
if err != nil {
internalError(c, err)
return false
}
return true
}
func (s *Server) adminSuspendStatusPage(c *gin.Context) {
id, req, ok := suspension(c)
if !ok {
return
}
ctx := c.Request.Context()
err := s.db.Tx(ctx, tenant.System(), func(q *store.Queries) error {
row, err := q.SuspendStatusPage(ctx, store.SuspendStatusPageParams{ID: id, Suspend: req.Suspended, Reason: req.Reason})
auditDetail(c, "slug", row.Slug)
return err
})
if moderated(c, err) {
c.Status(http.StatusNoContent)
}
}
func (s *Server) adminDeleteStatusPage(c *gin.Context) {
id, err := uuid.Parse(c.Param("id"))
if err != nil {
fail(c, http.StatusNotFound, "not_found")
return
}
ctx := c.Request.Context()
err = s.db.Tx(ctx, tenant.System(), func(q *store.Queries) error {
page, err := q.GetStatusPage(ctx, id)
if err != nil {
return err
}
auditDetail(c, "slug", page.Slug)
_, err = q.DeleteStatusPage(ctx, id)
return err
})
if moderated(c, err) {
c.Status(http.StatusNoContent)
}
}
func (s *Server) adminSuspendOrg(c *gin.Context) {
id, req, ok := suspension(c)
if !ok {
return
}
ctx := c.Request.Context()
err := s.db.Tx(ctx, tenant.System(), func(q *store.Queries) error {
row, err := q.SuspendOrg(ctx, store.SuspendOrgParams{ID: id, Suspend: req.Suspended, Reason: req.Reason})
if err != nil {
return err
}
auditDetail(c, "slug", row.Slug)
if !req.Suspended {
return q.RestartHeartbeatWait(ctx, id)
}
return nil
})
if !moderated(c, err) {
return
}
// Monitors leave or rejoin the schedule with its next reconciliation.
c.Status(http.StatusNoContent)
}
func (s *Server) adminDeleteOrg(c *gin.Context) {
id, err := uuid.Parse(c.Param("id"))
if err != nil {
fail(c, http.StatusNotFound, "not_found")
return
}
ctx := c.Request.Context()
err = s.db.Tx(ctx, tenant.System(), func(q *store.Queries) error {
org, err := q.GetOrg(ctx, id)
if err != nil {
return err
}
auditDetail(c, "slug", org.Slug)
auditDetail(c, "name", org.Name)
return purgeOrg(ctx, q, id)
})
if moderated(c, err) {
c.Status(http.StatusNoContent)
}
}
// moderatedUser loads the target account, refusing platform admins: they
// would otherwise lock each other, or themselves, out.
func (s *Server) moderatedUser(c *gin.Context) (store.User, bool) {
id, err := uuid.Parse(c.Param("id"))
if err != nil {
fail(c, http.StatusNotFound, "not_found")
return store.User{}, false
}
u, err := s.loadUser(c.Request.Context(), id)
if !moderated(c, err) {
return u, false
}
auditDetail(c, "email", u.Email)
if u.IsAdmin {
fail(c, http.StatusConflict, "target_is_admin")
return u, false
}
return u, true
}
func (s *Server) adminSuspendUser(c *gin.Context) {
id, req, ok := suspension(c)
if !ok {
return
}
u, ok := s.moderatedUser(c)
if !ok {
return
}
ctx := c.Request.Context()
err := s.db.Tx(ctx, tenant.System(), func(q *store.Queries) error {
_, err := q.SuspendUser(ctx, store.SuspendUserParams{ID: id, Suspend: req.Suspended, Reason: req.Reason})
return err
})
if !moderated(c, err) {
return
}
if req.Suspended {
if err := s.sessions.DeleteAllForUser(ctx, u.ID); err != nil {
internalError(c, err)
return
}
}
c.Status(http.StatusNoContent)
}
func (s *Server) adminDeleteUser(c *gin.Context) {
u, ok := s.moderatedUser(c)
if !ok {
return
}
ctx := c.Request.Context()
orgsDeleted, err := s.eraseAccount(ctx, u)
if err != nil {
internalError(c, err)
return
}
auditDetail(c, "organizations_deleted", orgsDeleted)
c.Status(http.StatusNoContent)
}
func (s *Server) moderationView(c *gin.Context, pg pageReq) (web.ModerationView, error) {
pages, info, err := s.moderatedPages(c.Request.Context(), c.Query("q"), pg)
v := web.ModerationView{Search: strings.TrimSpace(c.Query("q")), Pager: pager(c, info), Next: c.Request.URL.RequestURI()}
state := func(at *time.Time, reason string) string {
switch {
case at == nil:
return ""
case reason == "":
return "-"
}
return reason
}
for _, p := range pages {
u := strings.TrimSuffix(s.cfg.StatusPageURL, "/") + "/" + p.Slug
if p.Domain != "" {
u = "https://" + p.Domain + "/"
}
mp := web.ModeratedPage{
ID: p.ID.String(), Title: p.Title, Slug: p.Slug, URL: u, Created: p.CreatedAt.Format(time.DateOnly),
Published: p.Published, Suspended: state(p.SuspendedAt, p.Reason),
Org: web.ModeratedTarget{ID: p.Org.ID.String(), Name: p.Org.Name, Detail: p.Org.Slug, Suspended: state(p.Org.SuspendedAt, p.Org.Reason)},
}
if p.Domain != "" {
mp.Slug = p.Domain
}
if p.Owner != nil {
mp.Owner = &web.ModeratedTarget{ID: p.Owner.ID.String(), Name: p.Owner.Email, Suspended: state(p.Owner.SuspendedAt, p.Owner.Reason), Admin: p.Owner.Admin}
}
v.Pages = append(v.Pages, mp)
}
return v, err
}