The admin panel lists every published status page, and every suspended page, organization or account, with the organization and owner behind it, searchable by title, address, organization or email. Each can be suspended with a reason, reinstated, or deleted. A suspended page is not served; status page servers drop it when they next ask. A suspended organization's monitors leave the schedule, its pages go offline, heartbeat pings and Alertmanager webhooks are no longer recorded, and its members and API tokens get 403 org_suspended; the console explains why. Reinstating restarts heartbeat waits so they do not fire at once. A suspended account is signed out everywhere and can neither sign in nor use its tokens; only someone who knows the password learns of the suspension. Platform admins cannot be suspended or deleted. Account deletion shares its logic with the GDPR erasure, including the refusal while the account owns shared organizations. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
308 lines
8.5 KiB
Go
308 lines
8.5 KiB
Go
package api
|
|
|
|
import (
|
|
"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/i18n"
|
|
"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"`
|
|
}
|
|
|
|
const moderationRows = 500
|
|
|
|
func (s *Server) moderatedPages(c *gin.Context) ([]moderatedPage, error) {
|
|
ctx := c.Request.Context()
|
|
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(c.Query("q")), 200), MaxRows: moderationRows,
|
|
})
|
|
return err
|
|
})
|
|
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, err
|
|
}
|
|
|
|
func (s *Server) adminListStatusPages(c *gin.Context) {
|
|
pages, err := s.moderatedPages(c)
|
|
if err != nil {
|
|
internalError(c, err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"status_pages": pages})
|
|
}
|
|
|
|
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()
|
|
shared, orgsDeleted, err := s.eraseAccount(ctx, u)
|
|
if err != nil {
|
|
internalError(c, err)
|
|
return
|
|
}
|
|
if len(shared) > 0 {
|
|
c.JSON(http.StatusConflict, gin.H{
|
|
"error": "owns_shared_orgs", "message": i18n.T(ctx, "error.user_owns_shared_orgs"), "organizations": shared,
|
|
})
|
|
return
|
|
}
|
|
auditDetail(c, "organizations_deleted", orgsDeleted)
|
|
c.Status(http.StatusNoContent)
|
|
}
|
|
|
|
func (s *Server) moderationView(c *gin.Context) (web.ModerationView, error) {
|
|
pages, err := s.moderatedPages(c)
|
|
v := web.ModerationView{Search: strings.TrimSpace(c.Query("q")), Limited: len(pages) == moderationRows}
|
|
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
|
|
}
|