Signed-in users see the console navigation on the legal notice and privacy policy; visitors get a header with sign-in and language choice. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
221 lines
6.3 KiB
Go
221 lines
6.3 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
|
|
}
|
|
|
|
func validLegalKind(kind string) bool { return slices.Contains(web.LegalKinds, kind) }
|
|
|
|
type legalRequest struct {
|
|
URL string `json:"url" binding:"max=2000"`
|
|
Markdown string `json:"markdown" binding:"max=100000"`
|
|
}
|
|
|
|
// 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 !validLegalKind(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)
|
|
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})
|
|
})
|
|
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) {
|
|
if err := s.legal.bump(c.Request.Context(), s.rdb); err != nil {
|
|
s.log.ErrorContext(c.Request.Context(), "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"`
|
|
}
|
|
|
|
// 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(web.LegalKinds))
|
|
for _, kind := range web.LegalKinds {
|
|
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
|
|
}
|
|
}
|
|
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)
|
|
}
|
|
|
|
func toLegalForms(docs []legalView) []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}
|
|
}
|
|
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)), 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)))
|
|
}
|