676 lines
14 KiB
Go
676 lines
14 KiB
Go
// Copyright (c) 2025-2026 Probo Inc <hello@getprobo.com>.
|
|
//
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
// copyright notice and this permission notice appear in all copies.
|
|
//
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
// PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
package coredata
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"maps"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
"github.com/jackc/pgx/v5/pgconn"
|
|
"go.gearno.de/kit/pg"
|
|
"go.probo.inc/probo/pkg/gid"
|
|
"go.probo.inc/probo/pkg/iam/policy"
|
|
"go.probo.inc/probo/pkg/page"
|
|
)
|
|
|
|
type (
|
|
CookieBanner struct {
|
|
ID gid.GID `db:"id"`
|
|
OrganizationID gid.GID `db:"organization_id"`
|
|
Name string `db:"name"`
|
|
Origin string `db:"origin"`
|
|
State CookieBannerState `db:"state"`
|
|
PrivacyPolicyURL *string `db:"privacy_policy_url"`
|
|
CookiePolicyURL string `db:"cookie_policy_url"`
|
|
ConsentExpiryDays int `db:"consent_expiry_days"`
|
|
ShowBranding bool `db:"show_branding"`
|
|
DefaultLanguage string `db:"default_language"`
|
|
PatternAnalysisRequestedAt *time.Time `db:"pattern_analysis_requested_at"`
|
|
CreatedAt time.Time `db:"created_at"`
|
|
UpdatedAt time.Time `db:"updated_at"`
|
|
}
|
|
|
|
CookieBanners []*CookieBanner
|
|
)
|
|
|
|
func (b *CookieBanner) CursorKey(field CookieBannerOrderField) page.CursorKey {
|
|
switch field {
|
|
case CookieBannerOrderFieldCreatedAt:
|
|
return page.NewCursorKey(b.ID, b.CreatedAt)
|
|
}
|
|
|
|
panic(fmt.Sprintf("unsupported order by: %s", field))
|
|
}
|
|
|
|
func (b *CookieBanner) AuthorizationAttributes(
|
|
ctx context.Context,
|
|
conn pg.Querier,
|
|
resourceIDs []gid.GID,
|
|
) (policy.AttributesByID, error) {
|
|
q := `SELECT id, organization_id FROM cookie_banners WHERE id = ANY(@resource_ids::text[])`
|
|
|
|
args := pgx.StrictNamedArgs{
|
|
"resource_ids": resourceIDs,
|
|
}
|
|
|
|
rows, err := conn.Query(ctx, q, args)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("cannot query authorization attributes: %w", err)
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
attrsByID := make(policy.AttributesByID)
|
|
|
|
for rows.Next() {
|
|
var id, organizationID gid.GID
|
|
|
|
if err := rows.Scan(&id, &organizationID); err != nil {
|
|
return nil, fmt.Errorf("cannot scan authorization attributes: %w", err)
|
|
}
|
|
|
|
attrsByID[id] = policy.Attributes{
|
|
"organization_id": organizationID.String(),
|
|
}
|
|
}
|
|
|
|
if err := rows.Err(); err != nil {
|
|
return nil, fmt.Errorf("cannot iterate authorization attributes: %w", err)
|
|
}
|
|
|
|
return attrsByID, nil
|
|
}
|
|
|
|
func (b *CookieBanner) LoadByID(
|
|
ctx context.Context,
|
|
conn pg.Querier,
|
|
scope Scoper,
|
|
bannerID gid.GID,
|
|
) error {
|
|
q := `
|
|
SELECT
|
|
id,
|
|
organization_id,
|
|
name,
|
|
origin,
|
|
state,
|
|
privacy_policy_url,
|
|
cookie_policy_url,
|
|
consent_expiry_days,
|
|
show_branding,
|
|
default_language,
|
|
pattern_analysis_requested_at,
|
|
created_at,
|
|
updated_at
|
|
FROM
|
|
cookie_banners
|
|
WHERE
|
|
%s
|
|
AND id = @banner_id
|
|
LIMIT 1;
|
|
`
|
|
|
|
q = fmt.Sprintf(q, scope.SQLFragment())
|
|
|
|
args := pgx.StrictNamedArgs{"banner_id": bannerID}
|
|
maps.Copy(args, scope.SQLArguments())
|
|
|
|
rows, err := conn.Query(ctx, q, args)
|
|
if err != nil {
|
|
return fmt.Errorf("cannot query cookie banners: %w", err)
|
|
}
|
|
|
|
banner, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[CookieBanner])
|
|
if err != nil {
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return ErrResourceNotFound
|
|
}
|
|
|
|
return fmt.Errorf("cannot collect cookie banner: %w", err)
|
|
}
|
|
|
|
*b = banner
|
|
|
|
return nil
|
|
}
|
|
|
|
func (b *CookieBanner) LoadActiveByID(
|
|
ctx context.Context,
|
|
conn pg.Querier,
|
|
bannerID gid.GID,
|
|
) error {
|
|
q := `
|
|
SELECT
|
|
id,
|
|
organization_id,
|
|
name,
|
|
origin,
|
|
state,
|
|
privacy_policy_url,
|
|
cookie_policy_url,
|
|
consent_expiry_days,
|
|
show_branding,
|
|
default_language,
|
|
pattern_analysis_requested_at,
|
|
created_at,
|
|
updated_at
|
|
FROM
|
|
cookie_banners
|
|
WHERE
|
|
id = @banner_id
|
|
AND state = @state
|
|
LIMIT 1;
|
|
`
|
|
|
|
args := pgx.StrictNamedArgs{
|
|
"banner_id": bannerID,
|
|
"state": CookieBannerStateActive,
|
|
}
|
|
|
|
rows, err := conn.Query(ctx, q, args)
|
|
if err != nil {
|
|
return fmt.Errorf("cannot query cookie banners: %w", err)
|
|
}
|
|
|
|
banner, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[CookieBanner])
|
|
if err != nil {
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return ErrResourceNotFound
|
|
}
|
|
|
|
return fmt.Errorf("cannot collect cookie banner: %w", err)
|
|
}
|
|
|
|
*b = banner
|
|
|
|
return nil
|
|
}
|
|
|
|
func (b *CookieBanner) LoadActiveByOrigin(
|
|
ctx context.Context,
|
|
conn pg.Querier,
|
|
scope Scoper,
|
|
origin string,
|
|
) error {
|
|
q := `
|
|
SELECT
|
|
id,
|
|
organization_id,
|
|
name,
|
|
origin,
|
|
state,
|
|
privacy_policy_url,
|
|
cookie_policy_url,
|
|
consent_expiry_days,
|
|
show_branding,
|
|
default_language,
|
|
pattern_analysis_requested_at,
|
|
created_at,
|
|
updated_at
|
|
FROM
|
|
cookie_banners
|
|
WHERE
|
|
%s
|
|
AND origin = @origin
|
|
AND state = @state
|
|
LIMIT 1;
|
|
`
|
|
|
|
q = fmt.Sprintf(q, scope.SQLFragment())
|
|
|
|
args := pgx.StrictNamedArgs{
|
|
"origin": origin,
|
|
"state": CookieBannerStateActive,
|
|
}
|
|
maps.Copy(args, scope.SQLArguments())
|
|
|
|
rows, err := conn.Query(ctx, q, args)
|
|
if err != nil {
|
|
return fmt.Errorf("cannot query cookie banners: %w", err)
|
|
}
|
|
|
|
banner, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[CookieBanner])
|
|
if err != nil {
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return ErrResourceNotFound
|
|
}
|
|
|
|
return fmt.Errorf("cannot collect cookie banner: %w", err)
|
|
}
|
|
|
|
*b = banner
|
|
|
|
return nil
|
|
}
|
|
|
|
func (b *CookieBanners) LoadByIDs(
|
|
ctx context.Context,
|
|
conn pg.Querier,
|
|
scope Scoper,
|
|
bannerIDs []gid.GID,
|
|
) error {
|
|
q := `
|
|
SELECT
|
|
id,
|
|
organization_id,
|
|
name,
|
|
origin,
|
|
state,
|
|
privacy_policy_url,
|
|
cookie_policy_url,
|
|
consent_expiry_days,
|
|
show_branding,
|
|
default_language,
|
|
pattern_analysis_requested_at,
|
|
created_at,
|
|
updated_at
|
|
FROM
|
|
cookie_banners
|
|
WHERE
|
|
%s
|
|
AND id = ANY(@banner_ids)
|
|
`
|
|
|
|
q = fmt.Sprintf(q, scope.SQLFragment())
|
|
|
|
args := pgx.StrictNamedArgs{"banner_ids": bannerIDs}
|
|
maps.Copy(args, scope.SQLArguments())
|
|
|
|
rows, err := conn.Query(ctx, q, args)
|
|
if err != nil {
|
|
return fmt.Errorf("cannot query cookie banners: %w", err)
|
|
}
|
|
|
|
banners, err := pgx.CollectRows(rows, pgx.RowToAddrOfStructByName[CookieBanner])
|
|
if err != nil {
|
|
return fmt.Errorf("cannot collect cookie banners: %w", err)
|
|
}
|
|
|
|
*b = banners
|
|
|
|
return nil
|
|
}
|
|
|
|
func (b *CookieBanners) LoadByOrganizationID(
|
|
ctx context.Context,
|
|
conn pg.Querier,
|
|
scope Scoper,
|
|
organizationID gid.GID,
|
|
cursor *page.Cursor[CookieBannerOrderField],
|
|
filter *CookieBannerFilter,
|
|
) error {
|
|
q := `
|
|
SELECT
|
|
id,
|
|
organization_id,
|
|
name,
|
|
origin,
|
|
state,
|
|
privacy_policy_url,
|
|
cookie_policy_url,
|
|
consent_expiry_days,
|
|
show_branding,
|
|
default_language,
|
|
pattern_analysis_requested_at,
|
|
created_at,
|
|
updated_at
|
|
FROM
|
|
cookie_banners
|
|
WHERE
|
|
%s
|
|
AND organization_id = @organization_id
|
|
AND %s
|
|
AND %s
|
|
`
|
|
|
|
q = fmt.Sprintf(q, scope.SQLFragment(), filter.SQLFragment(), cursor.SQLFragment())
|
|
|
|
args := pgx.StrictNamedArgs{"organization_id": organizationID}
|
|
maps.Copy(args, scope.SQLArguments())
|
|
maps.Copy(args, filter.SQLArguments())
|
|
maps.Copy(args, cursor.SQLArguments())
|
|
|
|
rows, err := conn.Query(ctx, q, args)
|
|
if err != nil {
|
|
return fmt.Errorf("cannot query cookie banners: %w", err)
|
|
}
|
|
|
|
banners, err := pgx.CollectRows(rows, pgx.RowToAddrOfStructByName[CookieBanner])
|
|
if err != nil {
|
|
return fmt.Errorf("cannot collect cookie banners: %w", err)
|
|
}
|
|
|
|
*b = banners
|
|
|
|
return nil
|
|
}
|
|
|
|
func (b *CookieBanners) CountByOrganizationID(
|
|
ctx context.Context,
|
|
conn pg.Querier,
|
|
scope Scoper,
|
|
organizationID gid.GID,
|
|
filter *CookieBannerFilter,
|
|
) (int, error) {
|
|
q := `
|
|
SELECT
|
|
COUNT(id)
|
|
FROM
|
|
cookie_banners
|
|
WHERE
|
|
%s
|
|
AND organization_id = @organization_id
|
|
AND %s
|
|
`
|
|
|
|
q = fmt.Sprintf(q, scope.SQLFragment(), filter.SQLFragment())
|
|
|
|
args := pgx.StrictNamedArgs{"organization_id": organizationID}
|
|
maps.Copy(args, scope.SQLArguments())
|
|
maps.Copy(args, filter.SQLArguments())
|
|
|
|
row := conn.QueryRow(ctx, q, args)
|
|
|
|
var count int
|
|
if err := row.Scan(&count); err != nil {
|
|
return 0, fmt.Errorf("cannot scan count: %w", err)
|
|
}
|
|
|
|
return count, nil
|
|
}
|
|
|
|
func (b *CookieBanner) Insert(
|
|
ctx context.Context,
|
|
tx pg.Tx,
|
|
scope Scoper,
|
|
) error {
|
|
q := `
|
|
INSERT INTO cookie_banners (
|
|
id,
|
|
tenant_id,
|
|
organization_id,
|
|
name,
|
|
origin,
|
|
state,
|
|
privacy_policy_url,
|
|
cookie_policy_url,
|
|
consent_expiry_days,
|
|
show_branding,
|
|
default_language,
|
|
pattern_analysis_requested_at,
|
|
created_at,
|
|
updated_at
|
|
) VALUES (
|
|
@id,
|
|
@tenant_id,
|
|
@organization_id,
|
|
@name,
|
|
@origin,
|
|
@state,
|
|
@privacy_policy_url,
|
|
@cookie_policy_url,
|
|
@consent_expiry_days,
|
|
@show_branding,
|
|
@default_language,
|
|
@pattern_analysis_requested_at,
|
|
@created_at,
|
|
@updated_at
|
|
)
|
|
`
|
|
|
|
args := pgx.StrictNamedArgs{
|
|
"id": b.ID,
|
|
"tenant_id": scope.GetTenantID(),
|
|
"organization_id": b.OrganizationID,
|
|
"name": b.Name,
|
|
"origin": b.Origin,
|
|
"state": b.State,
|
|
"privacy_policy_url": b.PrivacyPolicyURL,
|
|
"cookie_policy_url": b.CookiePolicyURL,
|
|
"consent_expiry_days": b.ConsentExpiryDays,
|
|
"show_branding": b.ShowBranding,
|
|
"default_language": b.DefaultLanguage,
|
|
"pattern_analysis_requested_at": b.PatternAnalysisRequestedAt,
|
|
"created_at": b.CreatedAt,
|
|
"updated_at": b.UpdatedAt,
|
|
}
|
|
|
|
_, err := tx.Exec(ctx, q, args)
|
|
if err != nil {
|
|
if pgErr, ok := errors.AsType[*pgconn.PgError](err); ok {
|
|
if pgErr.Code == "23505" && pgErr.ConstraintName == "idx_cookie_banners_unique_active_origin" {
|
|
return ErrResourceAlreadyExists
|
|
}
|
|
}
|
|
|
|
return fmt.Errorf("cannot insert cookie banner: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (b *CookieBanner) Update(
|
|
ctx context.Context,
|
|
tx pg.Tx,
|
|
scope Scoper,
|
|
) error {
|
|
q := `
|
|
UPDATE cookie_banners
|
|
SET
|
|
name = @name,
|
|
state = @state,
|
|
privacy_policy_url = @privacy_policy_url,
|
|
cookie_policy_url = @cookie_policy_url,
|
|
consent_expiry_days = @consent_expiry_days,
|
|
show_branding = @show_branding,
|
|
default_language = @default_language,
|
|
updated_at = @updated_at
|
|
WHERE
|
|
%s
|
|
AND id = @id
|
|
`
|
|
|
|
q = fmt.Sprintf(q, scope.SQLFragment())
|
|
|
|
args := pgx.StrictNamedArgs{
|
|
"id": b.ID,
|
|
"name": b.Name,
|
|
"state": b.State,
|
|
"privacy_policy_url": b.PrivacyPolicyURL,
|
|
"cookie_policy_url": b.CookiePolicyURL,
|
|
"consent_expiry_days": b.ConsentExpiryDays,
|
|
"show_branding": b.ShowBranding,
|
|
"default_language": b.DefaultLanguage,
|
|
"updated_at": b.UpdatedAt,
|
|
}
|
|
maps.Copy(args, scope.SQLArguments())
|
|
|
|
result, err := tx.Exec(ctx, q, args)
|
|
if err != nil {
|
|
if pgErr, ok := errors.AsType[*pgconn.PgError](err); ok {
|
|
if pgErr.Code == "23505" && pgErr.ConstraintName == "idx_cookie_banners_unique_active_origin" {
|
|
return ErrResourceAlreadyExists
|
|
}
|
|
}
|
|
|
|
return fmt.Errorf("cannot update cookie banner: %w", err)
|
|
}
|
|
|
|
if result.RowsAffected() == 0 {
|
|
return ErrResourceNotFound
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (b *CookieBanner) UpdateShowBranding(
|
|
ctx context.Context,
|
|
tx pg.Tx,
|
|
scope Scoper,
|
|
show bool,
|
|
) error {
|
|
q := `
|
|
UPDATE cookie_banners
|
|
SET
|
|
show_branding = @show_branding,
|
|
updated_at = @updated_at
|
|
WHERE
|
|
%s
|
|
AND id = @id
|
|
`
|
|
|
|
q = fmt.Sprintf(q, scope.SQLFragment())
|
|
|
|
args := pgx.StrictNamedArgs{
|
|
"id": b.ID,
|
|
"show_branding": show,
|
|
"updated_at": time.Now(),
|
|
}
|
|
maps.Copy(args, scope.SQLArguments())
|
|
|
|
result, err := tx.Exec(ctx, q, args)
|
|
if err != nil {
|
|
return fmt.Errorf("cannot update cookie banner show_branding: %w", err)
|
|
}
|
|
|
|
if result.RowsAffected() == 0 {
|
|
return ErrResourceNotFound
|
|
}
|
|
|
|
b.ShowBranding = show
|
|
|
|
return nil
|
|
}
|
|
|
|
func (b *CookieBanner) Delete(
|
|
ctx context.Context,
|
|
tx pg.Tx,
|
|
scope Scoper,
|
|
) error {
|
|
q := `
|
|
DELETE FROM cookie_banners
|
|
WHERE
|
|
%s
|
|
AND id = @id
|
|
`
|
|
|
|
q = fmt.Sprintf(q, scope.SQLFragment())
|
|
|
|
args := pgx.StrictNamedArgs{"id": b.ID}
|
|
maps.Copy(args, scope.SQLArguments())
|
|
|
|
_, err := tx.Exec(ctx, q, args)
|
|
if err != nil {
|
|
return fmt.Errorf("cannot delete cookie banner: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (b *CookieBanner) LoadNextForPatternAnalysisForUpdateSkipLocked(
|
|
ctx context.Context,
|
|
tx pg.Tx,
|
|
) error {
|
|
q := `
|
|
SELECT
|
|
id,
|
|
organization_id,
|
|
name,
|
|
origin,
|
|
state,
|
|
privacy_policy_url,
|
|
cookie_policy_url,
|
|
consent_expiry_days,
|
|
show_branding,
|
|
default_language,
|
|
pattern_analysis_requested_at,
|
|
created_at,
|
|
updated_at
|
|
FROM
|
|
cookie_banners
|
|
WHERE
|
|
pattern_analysis_requested_at IS NOT NULL
|
|
ORDER BY
|
|
pattern_analysis_requested_at ASC
|
|
FOR UPDATE SKIP LOCKED
|
|
LIMIT 1;
|
|
`
|
|
|
|
rows, err := tx.Query(ctx, q)
|
|
if err != nil {
|
|
return fmt.Errorf("cannot query cookie banners for pattern analysis: %w", err)
|
|
}
|
|
|
|
banner, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[CookieBanner])
|
|
if err != nil {
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return ErrResourceNotFound
|
|
}
|
|
|
|
return fmt.Errorf("cannot collect cookie banner: %w", err)
|
|
}
|
|
|
|
*b = banner
|
|
|
|
return nil
|
|
}
|
|
|
|
func (b *CookieBanner) ClearPatternAnalysisRequestedAt(
|
|
ctx context.Context,
|
|
tx pg.Tx,
|
|
) error {
|
|
q := `
|
|
UPDATE cookie_banners
|
|
SET pattern_analysis_requested_at = NULL
|
|
WHERE id = @id
|
|
`
|
|
|
|
args := pgx.StrictNamedArgs{"id": b.ID}
|
|
|
|
_, err := tx.Exec(ctx, q, args)
|
|
if err != nil {
|
|
return fmt.Errorf("cannot clear pattern analysis requested at: %w", err)
|
|
}
|
|
|
|
b.PatternAnalysisRequestedAt = nil
|
|
|
|
return nil
|
|
}
|
|
|
|
func (b *CookieBanner) SetPatternAnalysisRequested(
|
|
ctx context.Context,
|
|
tx pg.Tx,
|
|
) error {
|
|
q := `
|
|
UPDATE cookie_banners
|
|
SET pattern_analysis_requested_at = NOW()
|
|
WHERE id = @id
|
|
AND pattern_analysis_requested_at IS NULL
|
|
`
|
|
|
|
args := pgx.StrictNamedArgs{"id": b.ID}
|
|
|
|
_, err := tx.Exec(ctx, q, args)
|
|
if err != nil {
|
|
return fmt.Errorf("cannot set pattern analysis requested: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|