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
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
244 lines
7.2 KiB
Go
244 lines
7.2 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/url"
|
|
"slices"
|
|
"strings"
|
|
|
|
"github.com/a-h/templ"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/google/uuid"
|
|
|
|
"git.zb-server.de/ZB-Server/uptimesebi/internal/agentapi"
|
|
"git.zb-server.de/ZB-Server/uptimesebi/internal/auth"
|
|
"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"
|
|
)
|
|
|
|
// mergeLegal picks one document per kind, the organization's over the
|
|
// platform's. rows come organization first.
|
|
func mergeLegal(rows []store.LegalDocument) []agentapi.LegalDoc {
|
|
out := []agentapi.LegalDoc{}
|
|
seen := map[string]bool{}
|
|
for _, r := range rows {
|
|
if seen[r.Kind] || (r.Url == "" && r.Markdown == "") {
|
|
continue
|
|
}
|
|
seen[r.Kind] = true
|
|
out = append(out, agentapi.LegalDoc{Kind: r.Kind, URL: r.Url, Markdown: r.Markdown})
|
|
}
|
|
return out
|
|
}
|
|
|
|
// platformLegalDocs returns the platform's documents, which every console
|
|
// page links in its footer.
|
|
func (s *Server) platformLegalDocs(ctx context.Context) []agentapi.LegalDoc {
|
|
docs, err := s.legal.get(ctx, s.rdb, func(ctx context.Context) ([]agentapi.LegalDoc, error) {
|
|
var rows []store.LegalDocument
|
|
err := s.db.Tx(ctx, tenant.System(), func(q *store.Queries) error {
|
|
var err error
|
|
rows, err = q.LegalDocuments(ctx, nil)
|
|
return err
|
|
})
|
|
return mergeLegal(rows), err
|
|
})
|
|
if err != nil {
|
|
s.log.WarnContext(ctx, "loading legal documents failed", "error", err)
|
|
}
|
|
return docs
|
|
}
|
|
|
|
// legalKinds are the documents org (nil: the platform) can set.
|
|
func legalKinds(org *uuid.UUID) []string {
|
|
if org == nil {
|
|
return web.LegalKinds
|
|
}
|
|
return web.OrgLegalKinds
|
|
}
|
|
|
|
type legalRequest struct {
|
|
URL string `json:"url" binding:"max=2000"`
|
|
Markdown string `json:"markdown" binding:"max=100000"`
|
|
// NewVersion asks every account to accept the platform's privacy
|
|
// policy or terms again.
|
|
NewVersion bool `json:"new_version"`
|
|
}
|
|
|
|
// validLegalURL accepts absolute http and https URLs only, so a stored link
|
|
// cannot run script when clicked.
|
|
func validLegalURL(raw string) bool {
|
|
u, err := url.Parse(raw)
|
|
return err == nil && (u.Scheme == "https" || u.Scheme == "http") && u.Host != ""
|
|
}
|
|
|
|
// saveLegal stores or, with both fields empty, removes a document of org,
|
|
// nil for the platform.
|
|
func (s *Server) saveLegal(c *gin.Context, scope tenant.Scope, org *uuid.UUID) bool {
|
|
kind := c.Param("kind")
|
|
if !slices.Contains(legalKinds(org), kind) {
|
|
fail(c, http.StatusNotFound, "not_found")
|
|
return false
|
|
}
|
|
var req legalRequest
|
|
if !bind(c, &req) {
|
|
return false
|
|
}
|
|
req.URL, req.Markdown = strings.TrimSpace(req.URL), strings.TrimSpace(req.Markdown)
|
|
if req.URL != "" && !validLegalURL(req.URL) {
|
|
fail(c, http.StatusUnprocessableEntity, "invalid_legal_url")
|
|
return false
|
|
}
|
|
auditDetail(c, "kind", kind)
|
|
newVersion := req.NewVersion && org == nil && slices.Contains(web.ConsentKinds, kind)
|
|
if newVersion {
|
|
auditDetail(c, "new_version", "true")
|
|
}
|
|
ctx := c.Request.Context()
|
|
err := s.db.Tx(ctx, scope, func(q *store.Queries) error {
|
|
if req.URL == "" && req.Markdown == "" {
|
|
return q.DeleteLegalDocument(ctx, store.DeleteLegalDocumentParams{OrgID: org, Kind: kind})
|
|
}
|
|
return q.UpsertLegalDocument(ctx, store.UpsertLegalDocumentParams{OrgID: org, Kind: kind, Url: req.URL, Markdown: req.Markdown, NewVersion: newVersion})
|
|
})
|
|
if err != nil {
|
|
internalError(c, err)
|
|
return false
|
|
}
|
|
c.Status(http.StatusNoContent)
|
|
return true
|
|
}
|
|
|
|
func (s *Server) adminSaveLegal(c *gin.Context) {
|
|
if s.saveLegal(c, tenant.System(), nil) {
|
|
ctx := c.Request.Context()
|
|
for _, err := range []error{s.legal.bump(ctx, s.rdb), s.consent.bump(ctx, s.rdb)} {
|
|
if err != nil {
|
|
s.log.ErrorContext(ctx, "announcing legal document change failed", "error", err)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func (s *Server) orgSaveLegal(c *gin.Context) {
|
|
p := current(c)
|
|
s.saveLegal(c, p.scope(), &p.Org.ID)
|
|
}
|
|
|
|
type legalView struct {
|
|
Kind string `json:"kind"`
|
|
URL string `json:"url"`
|
|
Markdown string `json:"markdown"`
|
|
// Version changes whenever accounts have to accept the document again.
|
|
Version int64 `json:"version,omitempty"`
|
|
}
|
|
|
|
// ownLegal returns the documents stored for org (nil: the platform),
|
|
// without fallbacks.
|
|
func (s *Server) ownLegal(ctx context.Context, scope tenant.Scope, org *uuid.UUID) ([]legalView, error) {
|
|
var rows []store.LegalDocument
|
|
err := s.db.Tx(ctx, scope, func(q *store.Queries) error {
|
|
var err error
|
|
rows, err = q.LegalDocuments(ctx, org)
|
|
return err
|
|
})
|
|
out := make([]legalView, 0, len(legalKinds(org)))
|
|
for _, kind := range legalKinds(org) {
|
|
v := legalView{Kind: kind}
|
|
for _, r := range rows {
|
|
if r.Kind == kind && (r.OrgID == nil) == (org == nil) {
|
|
v.URL, v.Markdown = r.Url, r.Markdown
|
|
if org == nil && slices.Contains(web.ConsentKinds, kind) {
|
|
v.Version = r.Version
|
|
}
|
|
}
|
|
}
|
|
out = append(out, v)
|
|
}
|
|
return out, err
|
|
}
|
|
|
|
func (s *Server) adminListLegal(c *gin.Context) {
|
|
docs, err := s.ownLegal(c.Request.Context(), tenant.System(), nil)
|
|
if err != nil {
|
|
internalError(c, err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"documents": docs})
|
|
}
|
|
|
|
func (s *Server) orgListLegal(c *gin.Context) {
|
|
p := current(c)
|
|
docs, err := s.ownLegal(c.Request.Context(), p.scope(), &p.Org.ID)
|
|
if err != nil {
|
|
internalError(c, err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"documents": docs})
|
|
}
|
|
|
|
// legalPage serves a platform document to anyone: Markdown rendered here,
|
|
// links redirected.
|
|
func (s *Server) legalPage(c *gin.Context) {
|
|
kind := c.Param("kind")
|
|
for _, d := range s.platformLegalDocs(c.Request.Context()) {
|
|
if d.Kind != kind {
|
|
continue
|
|
}
|
|
if d.URL != "" {
|
|
c.Redirect(http.StatusFound, d.URL)
|
|
return
|
|
}
|
|
html, err := web.RenderMarkdown(d.Markdown)
|
|
if err != nil {
|
|
internalError(c, err)
|
|
return
|
|
}
|
|
var me *web.Me
|
|
if sess := auth.Current(c); sess != nil {
|
|
m, _, err := s.consoleMe(c, sess)
|
|
if err != nil {
|
|
internalError(c, err)
|
|
return
|
|
}
|
|
me = &m
|
|
}
|
|
web.Render(c, http.StatusOK, web.LegalPage(s.pageFor(c, web.LegalTitleID(kind)), me, html))
|
|
return
|
|
}
|
|
c.Status(http.StatusNotFound)
|
|
}
|
|
|
|
// toLegalForms prepares the forms; the platform's privacy policy and terms
|
|
// can ask everyone to accept them again.
|
|
func toLegalForms(docs []legalView, platform bool) []web.LegalForm {
|
|
out := make([]web.LegalForm, len(docs))
|
|
for i, d := range docs {
|
|
out[i] = web.LegalForm{Kind: d.Kind, URL: d.URL, Markdown: d.Markdown, Consent: platform && slices.Contains(web.ConsentKinds, d.Kind)}
|
|
}
|
|
return out
|
|
}
|
|
|
|
func (s *Server) orgLegalView(c *gin.Context, p *principal, page web.Page, me web.Me) (templ.Component, error) {
|
|
docs, err := s.ownLegal(c.Request.Context(), p.scope(), &p.Org.ID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return web.LegalSettingsPage(page, me, "legal.org_title", "legal.org_intro", "/org/legal/", "/settings/legal", toLegalForms(docs, false)), nil
|
|
}
|
|
|
|
func (s *Server) adminLegalPage(c *gin.Context) {
|
|
me, ok := s.adminMe(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
docs, err := s.ownLegal(c.Request.Context(), tenant.System(), nil)
|
|
if err != nil {
|
|
internalError(c, err)
|
|
return
|
|
}
|
|
web.Render(c, http.StatusOK, web.LegalSettingsPage(s.pageFor(c, "legal.platform_title"), me, "legal.platform_title", "legal.platform_intro", "/admin/legal/", "/admin/legal", toLegalForms(docs, true)))
|
|
}
|