Customer branding (#155), the contract part: products and the portal can read and set a workspace's CI colour and logo. The derivation from the signup domain follows in a separate change. - Migration 0035: tenant_branding (colour, 256×256 PNG logo with its sha256 ETag, manual flag, provenance, derivation queue, notice ack). ON DELETE CASCADE, so a purge removes it. - Entitlements check: "branding" {color, logo_etag, notice, domain, name}, only when allowed and there is a colour or logo. A read error leaves the field out instead of failing the check. - GET/PUT /v1/tenants/{id}/branding (tenants.read/write): the portal's Erscheinungsbild page; a PUT makes the branding manual. Logos are decoded (PNG, JPEG, GIF, ICO) and re-encoded; SVG is refused. - GET /v1/tenants/{id}/branding/logo (branding.read): PNG, ETag, 304. - POST /v1/tenants/{id}/branding/ack (branding.ack): "Passt" by an IT admin or owner, checked in Keycloak; 403 not_admin otherwise. - BRANDING_DERIVE config switch (off by default), reported to the portal as derive_enabled. - CI runs the new internal/branding tests. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
219 lines
8.1 KiB
Go
219 lines
8.1 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
)
|
|
|
|
const brandingSelect = `
|
|
SELECT tenant_id::text, color, logo_etag, manual, domain, color_source, logo_source,
|
|
html_sha256, logo_sha256, derived_at, derive_state, derive_error, derive_attempts,
|
|
next_attempt_at, notice_ack_at, notice_ack_by, updated_by, updated_at
|
|
FROM tenant_branding `
|
|
|
|
func scanBranding(row pgx.Row) (*TenantBranding, error) {
|
|
var b TenantBranding
|
|
err := row.Scan(&b.TenantID, &b.Color, &b.LogoETag, &b.Manual, &b.Domain, &b.ColorSource, &b.LogoSource,
|
|
&b.HTMLSHA256, &b.LogoSHA256, &b.DerivedAt, &b.DeriveState, &b.DeriveError, &b.DeriveAttempts,
|
|
&b.NextAttemptAt, &b.NoticeAckAt, &b.NoticeAckBy, &b.UpdatedBy, &b.UpdatedAt)
|
|
if errors.Is(err, pgx.ErrNoRows) || isInvalidUUID(err) {
|
|
return nil, ErrNotFound
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &b, nil
|
|
}
|
|
|
|
func (p *Postgres) GetTenantBranding(ctx context.Context, tenantID string) (*TenantBranding, error) {
|
|
return scanBranding(p.pool.QueryRow(ctx, brandingSelect+`WHERE tenant_id = $1::uuid`, tenantID))
|
|
}
|
|
|
|
func (p *Postgres) GetTenantBrandingLogo(ctx context.Context, tenantID string) ([]byte, string, error) {
|
|
var png []byte
|
|
var etag string
|
|
err := p.pool.QueryRow(ctx, `
|
|
SELECT logo, logo_etag FROM tenant_branding
|
|
WHERE tenant_id = $1::uuid AND logo IS NOT NULL`, tenantID).Scan(&png, &etag)
|
|
if errors.Is(err, pgx.ErrNoRows) || isInvalidUUID(err) {
|
|
return nil, "", ErrNotFound
|
|
}
|
|
if err != nil {
|
|
return nil, "", err
|
|
}
|
|
return png, etag, nil
|
|
}
|
|
|
|
func (p *Postgres) EnqueueBranding(ctx context.Context, tenantID string, at time.Time) (bool, error) {
|
|
tag, err := p.pool.Exec(ctx, `
|
|
INSERT INTO tenant_branding (tenant_id, updated_at) VALUES ($1::uuid, $2)
|
|
ON CONFLICT (tenant_id) DO NOTHING`, tenantID, at.UTC())
|
|
if isFKViolation(err) || isInvalidUUID(err) {
|
|
return false, ErrNotFound
|
|
}
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
return tag.RowsAffected() == 1, nil
|
|
}
|
|
|
|
func (p *Postgres) EnqueueMissingBranding(ctx context.Context, at time.Time) (int, error) {
|
|
tag, err := p.pool.Exec(ctx, `
|
|
INSERT INTO tenant_branding (tenant_id, updated_at)
|
|
SELECT t.id, $1 FROM tenants t
|
|
WHERE t.kind = 'customer' AND t.status NOT IN ('demo', 'archived')
|
|
AND NOT EXISTS (SELECT 1 FROM tenant_branding b WHERE b.tenant_id = t.id)
|
|
ORDER BY t.created_at
|
|
ON CONFLICT (tenant_id) DO NOTHING`, at.UTC())
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return int(tag.RowsAffected()), nil
|
|
}
|
|
|
|
func (p *Postgres) DueBranding(ctx context.Context, now time.Time, limit int) ([]string, error) {
|
|
if limit <= 0 {
|
|
limit = 100
|
|
}
|
|
rows, err := p.pool.Query(ctx, `
|
|
SELECT tenant_id::text FROM tenant_branding
|
|
WHERE derive_state = 'pending' AND NOT manual
|
|
AND (next_attempt_at IS NULL OR next_attempt_at <= $1)
|
|
ORDER BY updated_at, tenant_id
|
|
LIMIT $2`, now.UTC(), limit)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
ids, err := pgx.CollectRows(rows, pgx.RowTo[string])
|
|
if ids == nil {
|
|
ids = []string{}
|
|
}
|
|
return ids, err
|
|
}
|
|
|
|
func (p *Postgres) SaveDerivedBranding(ctx context.Context, d DerivedBranding) (bool, error) {
|
|
if !validBrandingColor(d.Color) || (d.Logo == nil) != (d.LogoETag == "") {
|
|
return false, ErrInvalidInput
|
|
}
|
|
// One statement: the manual check and the write cannot be separated by
|
|
// an admin's save. The notice comes back only when what is shown
|
|
// changed; a forced run ("Neu ermitteln") counts as acknowledged.
|
|
tag, err := p.pool.Exec(ctx, `
|
|
INSERT INTO tenant_branding AS b (tenant_id, color, logo, logo_etag, manual, domain, color_source,
|
|
logo_source, html_sha256, logo_sha256, derived_at, derive_state, derive_error, derive_attempts,
|
|
next_attempt_at, notice_ack_at, notice_ack_by, updated_by, updated_at)
|
|
VALUES ($1::uuid, $2, $3, $4, false, $5, $6, $7, $8, $9, $10, 'done', '', 1, NULL,
|
|
CASE WHEN $11 THEN $10::timestamptz END, CASE WHEN $11 THEN $12 ELSE '' END, $12, $10)
|
|
ON CONFLICT (tenant_id) DO UPDATE SET
|
|
notice_ack_at = CASE
|
|
WHEN $11 THEN $10::timestamptz
|
|
WHEN b.color IS NOT DISTINCT FROM EXCLUDED.color AND b.logo_etag IS NOT DISTINCT FROM EXCLUDED.logo_etag THEN b.notice_ack_at
|
|
END,
|
|
notice_ack_by = CASE
|
|
WHEN $11 THEN $12
|
|
WHEN b.color IS NOT DISTINCT FROM EXCLUDED.color AND b.logo_etag IS NOT DISTINCT FROM EXCLUDED.logo_etag THEN b.notice_ack_by
|
|
ELSE '' END,
|
|
color = EXCLUDED.color, logo = EXCLUDED.logo, logo_etag = EXCLUDED.logo_etag, manual = false,
|
|
domain = EXCLUDED.domain, color_source = EXCLUDED.color_source, logo_source = EXCLUDED.logo_source,
|
|
html_sha256 = EXCLUDED.html_sha256, logo_sha256 = EXCLUDED.logo_sha256, derived_at = EXCLUDED.derived_at,
|
|
derive_state = 'done', derive_error = '', derive_attempts = b.derive_attempts + 1, next_attempt_at = NULL,
|
|
updated_by = EXCLUDED.updated_by, updated_at = EXCLUDED.updated_at
|
|
WHERE NOT b.manual OR $11`,
|
|
d.TenantID, d.Color, nilIfEmpty(d.Logo), optString(d.LogoETag), d.Domain, d.ColorSource, d.LogoSource,
|
|
d.HTMLSHA256, d.LogoSHA256, d.At.UTC(), d.Force, d.By)
|
|
switch {
|
|
case isFKViolation(err), isInvalidUUID(err):
|
|
return false, ErrNotFound
|
|
case isCheckViolation(err):
|
|
return false, ErrInvalidInput
|
|
case err != nil:
|
|
return false, err
|
|
}
|
|
return tag.RowsAffected() == 1, nil
|
|
}
|
|
|
|
func (p *Postgres) FailBranding(ctx context.Context, tenantID, msg string, next *time.Time, at time.Time) error {
|
|
state := BrandingFailed
|
|
if next != nil {
|
|
state = BrandingPending
|
|
n := next.UTC()
|
|
next = &n
|
|
}
|
|
_, err := p.pool.Exec(ctx, `
|
|
INSERT INTO tenant_branding AS b (tenant_id, derive_state, derive_error, derive_attempts, next_attempt_at, updated_at)
|
|
VALUES ($1::uuid, $2, $3, 1, $4, $5)
|
|
ON CONFLICT (tenant_id) DO UPDATE SET
|
|
derive_state = EXCLUDED.derive_state, derive_error = EXCLUDED.derive_error,
|
|
derive_attempts = b.derive_attempts + 1, next_attempt_at = EXCLUDED.next_attempt_at,
|
|
updated_at = EXCLUDED.updated_at`,
|
|
tenantID, state, msg, next, at.UTC())
|
|
if isFKViolation(err) || isInvalidUUID(err) {
|
|
return ErrNotFound
|
|
}
|
|
return err
|
|
}
|
|
|
|
func (p *Postgres) SetManualBranding(ctx context.Context, w BrandingManualWrite) (*TenantBranding, error) {
|
|
if (w.SetColor && !validBrandingColor(w.Color)) || (w.SetLogo && (w.Logo == nil) != (w.LogoETag == "")) {
|
|
return nil, ErrInvalidInput
|
|
}
|
|
colorSource := ""
|
|
if w.Color != nil {
|
|
colorSource = BrandingSourceManual
|
|
}
|
|
// A first row written by hand is not waiting for a derivation.
|
|
_, err := p.pool.Exec(ctx, `
|
|
INSERT INTO tenant_branding AS b (tenant_id, color, color_source, logo, logo_etag, manual, derive_state, updated_by, updated_at)
|
|
VALUES ($1::uuid,
|
|
CASE WHEN $2 THEN $3 END, CASE WHEN $2 THEN $4 ELSE '' END,
|
|
CASE WHEN $5 THEN $6::bytea END, CASE WHEN $5 THEN $7 END,
|
|
true, 'done', $8, $9)
|
|
ON CONFLICT (tenant_id) DO UPDATE SET
|
|
color = CASE WHEN $2 THEN $3 ELSE b.color END,
|
|
color_source = CASE WHEN $2 THEN $4 ELSE b.color_source END,
|
|
logo = CASE WHEN $5 THEN $6::bytea ELSE b.logo END,
|
|
logo_etag = CASE WHEN $5 THEN $7 ELSE b.logo_etag END,
|
|
logo_source = CASE WHEN $5 THEN '' ELSE b.logo_source END,
|
|
logo_sha256 = CASE WHEN $5 THEN '' ELSE b.logo_sha256 END,
|
|
manual = true, updated_by = EXCLUDED.updated_by, updated_at = EXCLUDED.updated_at`,
|
|
w.TenantID, w.SetColor, w.Color, colorSource, w.SetLogo, nilIfEmpty(w.Logo), optString(w.LogoETag), w.By, w.At.UTC())
|
|
switch {
|
|
case isFKViolation(err), isInvalidUUID(err):
|
|
return nil, ErrNotFound
|
|
case isCheckViolation(err):
|
|
return nil, ErrInvalidInput
|
|
case err != nil:
|
|
return nil, err
|
|
}
|
|
return p.GetTenantBranding(ctx, w.TenantID)
|
|
}
|
|
|
|
func (p *Postgres) AckBrandingNotice(ctx context.Context, tenantID, by string, at time.Time) (bool, error) {
|
|
tag, err := p.pool.Exec(ctx, `
|
|
UPDATE tenant_branding SET notice_ack_at = $2, notice_ack_by = $3
|
|
WHERE tenant_id = $1::uuid AND notice_ack_at IS NULL`, tenantID, at.UTC(), by)
|
|
if isInvalidUUID(err) {
|
|
return false, ErrNotFound
|
|
}
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
if tag.RowsAffected() == 1 {
|
|
return true, nil
|
|
}
|
|
if _, err := p.GetTenantBranding(ctx, tenantID); err != nil {
|
|
return false, err
|
|
}
|
|
return false, nil
|
|
}
|
|
|
|
func nilIfEmpty(b []byte) []byte {
|
|
if len(b) == 0 {
|
|
return nil
|
|
}
|
|
return b
|
|
}
|