diff --git a/internal/migrations/sql/00025_legal_documents.sql b/internal/migrations/sql/00025_legal_documents.sql new file mode 100644 index 0000000..51bf6ad --- /dev/null +++ b/internal/migrations/sql/00025_legal_documents.sql @@ -0,0 +1,25 @@ +-- +goose Up +-- Legal notice and privacy policy, either as Markdown served by us or as a +-- link to a page elsewhere; a link wins when both are set. Rows without an +-- organization belong to the platform: the console and status pages of +-- organizations without their own. +CREATE TABLE legal_documents ( + org_id uuid REFERENCES orgs (id) ON DELETE CASCADE, + kind text NOT NULL CHECK (kind IN ('imprint', 'privacy')), + url text NOT NULL DEFAULT '' CHECK (length(url) <= 2000), + markdown text NOT NULL DEFAULT '' CHECK (length(markdown) <= 100000), + updated_at timestamptz NOT NULL DEFAULT now(), + UNIQUE NULLS NOT DISTINCT (org_id, kind) +); + +ALTER TABLE legal_documents ENABLE ROW LEVEL SECURITY; +ALTER TABLE legal_documents FORCE ROW LEVEL SECURITY; +-- Platform documents are public. +CREATE POLICY legal_documents_read ON legal_documents FOR SELECT + USING (app_scope_system() OR org_id IS NULL OR org_id = app_org_id()); +CREATE POLICY legal_documents_write ON legal_documents FOR ALL + USING (app_scope_system() OR org_id = app_org_id()) + WITH CHECK (app_scope_system() OR org_id = app_org_id()); + +-- +goose Down +DROP TABLE legal_documents; diff --git a/internal/store/legal.sql.go b/internal/store/legal.sql.go new file mode 100644 index 0000000..9c65a29 --- /dev/null +++ b/internal/store/legal.sql.go @@ -0,0 +1,82 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.31.1 +// source: legal.sql + +package store + +import ( + "context" + + "github.com/google/uuid" +) + +const deleteLegalDocument = `-- name: DeleteLegalDocument :exec +DELETE FROM legal_documents WHERE org_id IS NOT DISTINCT FROM $1 AND kind = $2 +` + +type DeleteLegalDocumentParams struct { + OrgID *uuid.UUID + Kind string +} + +func (q *Queries) DeleteLegalDocument(ctx context.Context, arg DeleteLegalDocumentParams) error { + _, err := q.db.Exec(ctx, deleteLegalDocument, arg.OrgID, arg.Kind) + return err +} + +const legalDocuments = `-- name: LegalDocuments :many +SELECT org_id, kind, url, markdown, updated_at FROM legal_documents +WHERE org_id IS NULL OR org_id = $1 +ORDER BY org_id NULLS LAST, kind +` + +// The organization's documents and the platform's, organization first. +func (q *Queries) LegalDocuments(ctx context.Context, orgID *uuid.UUID) ([]LegalDocument, error) { + rows, err := q.db.Query(ctx, legalDocuments, orgID) + if err != nil { + return nil, err + } + defer rows.Close() + items := []LegalDocument{} + for rows.Next() { + var i LegalDocument + if err := rows.Scan( + &i.OrgID, + &i.Kind, + &i.Url, + &i.Markdown, + &i.UpdatedAt, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + +const upsertLegalDocument = `-- name: UpsertLegalDocument :exec +INSERT INTO legal_documents (org_id, kind, url, markdown) +VALUES ($1, $2, $3, $4) +ON CONFLICT (org_id, kind) DO UPDATE SET url = EXCLUDED.url, markdown = EXCLUDED.markdown, updated_at = now() +` + +type UpsertLegalDocumentParams struct { + OrgID *uuid.UUID + Kind string + Url string + Markdown string +} + +func (q *Queries) UpsertLegalDocument(ctx context.Context, arg UpsertLegalDocumentParams) error { + _, err := q.db.Exec(ctx, upsertLegalDocument, + arg.OrgID, + arg.Kind, + arg.Url, + arg.Markdown, + ) + return err +} diff --git a/internal/store/models.go b/internal/store/models.go index f090333..c286443 100644 --- a/internal/store/models.go +++ b/internal/store/models.go @@ -115,6 +115,14 @@ type Invitation struct { CreatedAt time.Time } +type LegalDocument struct { + OrgID *uuid.UUID + Kind string + Url string + Markdown string + UpdatedAt time.Time +} + type Membership struct { OrgID uuid.UUID UserID uuid.UUID diff --git a/internal/store/querier.go b/internal/store/querier.go index 642ed48..42ffda2 100644 --- a/internal/store/querier.go +++ b/internal/store/querier.go @@ -51,6 +51,7 @@ type Querier interface { DeleteGroup(ctx context.Context, id uuid.UUID) (int64, error) DeleteInvitation(ctx context.Context, id uuid.UUID) (int64, error) DeleteInvitationsForEmail(ctx context.Context, email string) error + DeleteLegalDocument(ctx context.Context, arg DeleteLegalDocumentParams) error DeleteMonitor(ctx context.Context, id uuid.UUID) (int64, error) DeleteMonitorRegions(ctx context.Context, monitorID uuid.UUID) error DeleteOrg(ctx context.Context, id uuid.UUID) (int64, error) @@ -112,6 +113,8 @@ type Querier interface { // Newest result per monitor and region in the org. LatestResults(ctx context.Context, orgID uuid.UUID) ([]LatestResultsRow, error) LatestResultsForMonitor(ctx context.Context, arg LatestResultsForMonitorParams) ([]LatestResultsForMonitorRow, error) + // The organization's documents and the platform's, organization first. + LegalDocuments(ctx context.Context, orgID *uuid.UUID) ([]LegalDocument, error) ListAPITokens(ctx context.Context, arg ListAPITokensParams) ([]ListAPITokensRow, error) // Agents owned by org, or platform agents when org is NULL. ListAgents(ctx context.Context, orgID *uuid.UUID) ([]ListAgentsRow, error) @@ -214,6 +217,7 @@ type Querier interface { UpdateRole(ctx context.Context, arg UpdateRoleParams) (Role, error) UpdateSSOProvider(ctx context.Context, arg UpdateSSOProviderParams) (SsoProvider, error) UpdateStatusPage(ctx context.Context, arg UpdateStatusPageParams) (StatusPage, error) + UpsertLegalDocument(ctx context.Context, arg UpsertLegalDocumentParams) error // Uptime per monitor from the hourly aggregate, including the not yet // materialized most recent hours (real-time aggregation). UptimeSince(ctx context.Context, arg UptimeSinceParams) ([]UptimeSinceRow, error) diff --git a/internal/store/queries/legal.sql b/internal/store/queries/legal.sql new file mode 100644 index 0000000..7ae241e --- /dev/null +++ b/internal/store/queries/legal.sql @@ -0,0 +1,13 @@ +-- name: UpsertLegalDocument :exec +INSERT INTO legal_documents (org_id, kind, url, markdown) +VALUES ($1, $2, $3, $4) +ON CONFLICT (org_id, kind) DO UPDATE SET url = EXCLUDED.url, markdown = EXCLUDED.markdown, updated_at = now(); + +-- name: DeleteLegalDocument :exec +DELETE FROM legal_documents WHERE org_id IS NOT DISTINCT FROM $1 AND kind = $2; + +-- name: LegalDocuments :many +-- The organization's documents and the platform's, organization first. +SELECT org_id, kind, url, markdown, updated_at FROM legal_documents +WHERE org_id IS NULL OR org_id = sqlc.narg(org_id) +ORDER BY org_id NULLS LAST, kind;