Force scope usage even for insert

Signed-off-by: gearnode <bryan@frimin.fr>
This commit is contained in:
gearnode
2025-03-10 10:27:30 +01:00
parent dc2cac9317
commit a4611f5433
22 changed files with 145 additions and 50 deletions

View File

@@ -59,7 +59,7 @@ func (c Control) CursorKey() page.CursorKey {
func (c *Control) LoadByID( func (c *Control) LoadByID(
ctx context.Context, ctx context.Context,
conn pg.Conn, conn pg.Conn,
scope *Scope, scope Scoper,
controlID gid.GID, controlID gid.GID,
) error { ) error {
q := ` q := `
@@ -121,10 +121,12 @@ LIMIT 1;
func (c Control) Insert( func (c Control) Insert(
ctx context.Context, ctx context.Context,
conn pg.Conn, conn pg.Conn,
scope Scoper,
) error { ) error {
q := ` q := `
INSERT INTO INSERT INTO
controls ( controls (
tenant_id,
id, id,
framework_id, framework_id,
category, category,
@@ -136,6 +138,7 @@ INSERT INTO
version version
) )
VALUES ( VALUES (
@tenant_id,
@control_id, @control_id,
@framework_id, @framework_id,
@category, @category,
@@ -149,6 +152,7 @@ VALUES (
` `
args := pgx.StrictNamedArgs{ args := pgx.StrictNamedArgs{
"tenant_id": scope.GetTenantID(),
"control_id": c.ID, "control_id": c.ID,
"framework_id": c.FrameworkID, "framework_id": c.FrameworkID,
"category": c.Category, "category": c.Category,
@@ -166,7 +170,7 @@ VALUES (
func (c *Controls) LoadByFrameworkID( func (c *Controls) LoadByFrameworkID(
ctx context.Context, ctx context.Context,
conn pg.Conn, conn pg.Conn,
scope *Scope, scope Scoper,
frameworkID gid.GID, frameworkID gid.GID,
cursor *page.Cursor, cursor *page.Cursor,
) error { ) error {
@@ -227,7 +231,7 @@ WHERE
func (c *Control) Update( func (c *Control) Update(
ctx context.Context, ctx context.Context,
conn pg.Conn, conn pg.Conn,
scope *Scope, scope Scoper,
params UpdateControlParams, params UpdateControlParams,
) error { ) error {
q := ` q := `

View File

@@ -42,10 +42,12 @@ func (cst ControlStateTransition) CursorKey() page.CursorKey {
func (cst ControlStateTransition) Insert( func (cst ControlStateTransition) Insert(
ctx context.Context, ctx context.Context,
conn pg.Conn, conn pg.Conn,
scope Scoper,
) error { ) error {
q := ` q := `
INSERT INTO INSERT INTO
control_state_transitions ( control_state_transitions (
tenant_id,
id, id,
control_id, control_id,
from_state, from_state,
@@ -55,6 +57,7 @@ INSERT INTO
updated_at updated_at
) )
VALUES ( VALUES (
@tenant_id,
@control_state_transition_id, @control_state_transition_id,
@control_id, @control_id,
@from_state, @from_state,
@@ -66,6 +69,7 @@ VALUES (
` `
args := pgx.StrictNamedArgs{ args := pgx.StrictNamedArgs{
"tenant_id": scope.GetTenantID(),
"control_state_transition_id": cst.ID, "control_state_transition_id": cst.ID,
"control_id": cst.ControlID, "control_id": cst.ControlID,
"from_state": cst.FromState, "from_state": cst.FromState,
@@ -81,7 +85,7 @@ VALUES (
func (cst *ControlStateTransitions) LoadByControlID( func (cst *ControlStateTransitions) LoadByControlID(
ctx context.Context, ctx context.Context,
conn pg.Conn, conn pg.Conn,
scope *Scope, scope Scoper,
controlID gid.GID, controlID gid.GID,
cursor *page.Cursor, cursor *page.Cursor,
) error { ) error {

View File

@@ -49,10 +49,12 @@ func (e Evidence) CursorKey() page.CursorKey {
func (e Evidence) Insert( func (e Evidence) Insert(
ctx context.Context, ctx context.Context,
conn pg.Conn, conn pg.Conn,
scope Scoper,
) error { ) error {
q := ` q := `
INSERT INTO INSERT INTO
evidences ( evidences (
tenant_id,
id, id,
task_id, task_id,
object_key, object_key,
@@ -63,6 +65,7 @@ INSERT INTO
updated_at updated_at
) )
VALUES ( VALUES (
@tenant_id,
@evidence_id, @evidence_id,
@task_id, @task_id,
@object_key, @object_key,
@@ -75,6 +78,7 @@ VALUES (
` `
args := pgx.StrictNamedArgs{ args := pgx.StrictNamedArgs{
"tenant_id": scope.GetTenantID(),
"evidence_id": e.ID, "evidence_id": e.ID,
"task_id": e.TaskID, "task_id": e.TaskID,
"object_key": e.ObjectKey, "object_key": e.ObjectKey,
@@ -91,7 +95,7 @@ VALUES (
func (e *Evidence) LoadByID( func (e *Evidence) LoadByID(
ctx context.Context, ctx context.Context,
conn pg.Conn, conn pg.Conn,
scope *Scope, scope Scoper,
evidenceID gid.GID, evidenceID gid.GID,
) error { ) error {
q := ` q := `
@@ -153,7 +157,7 @@ LIMIT 1;
func (e *Evidences) LoadByTaskID( func (e *Evidences) LoadByTaskID(
ctx context.Context, ctx context.Context,
conn pg.Conn, conn pg.Conn,
scope *Scope, scope Scoper,
taskID gid.GID, taskID gid.GID,
cursor *page.Cursor, cursor *page.Cursor,
) error { ) error {
@@ -215,7 +219,7 @@ WHERE
func (e Evidence) Delete( func (e Evidence) Delete(
ctx context.Context, ctx context.Context,
conn pg.Conn, conn pg.Conn,
scope *Scope, scope Scoper,
) error { ) error {
q := ` q := `
DELETE FROM DELETE FROM

View File

@@ -42,10 +42,12 @@ func (cst EvidenceStateTransition) CursorKey() page.CursorKey {
func (est EvidenceStateTransition) Insert( func (est EvidenceStateTransition) Insert(
ctx context.Context, ctx context.Context,
conn pg.Conn, conn pg.Conn,
scope Scoper,
) error { ) error {
q := ` q := `
INSERT INTO INSERT INTO
evidence_state_transitions ( evidence_state_transitions (
tenant_id,
id, id,
evidence_id, evidence_id,
from_state, from_state,
@@ -55,6 +57,7 @@ INSERT INTO
updated_at updated_at
) )
VALUES ( VALUES (
@tenant_id,
@evidence_state_transition_id, @evidence_state_transition_id,
@evidence_id, @evidence_id,
@from_state, @from_state,
@@ -66,6 +69,7 @@ VALUES (
` `
args := pgx.StrictNamedArgs{ args := pgx.StrictNamedArgs{
"tenant_id": scope.GetTenantID(),
"evidence_state_transition_id": est.ID, "evidence_state_transition_id": est.ID,
"evidence_id": est.EvidenceID, "evidence_id": est.EvidenceID,
"from_state": est.FromState, "from_state": est.FromState,
@@ -81,7 +85,7 @@ VALUES (
func (cst *EvidenceStateTransitions) LoadByEvidenceID( func (cst *EvidenceStateTransitions) LoadByEvidenceID(
ctx context.Context, ctx context.Context,
conn pg.Conn, conn pg.Conn,
scope *Scope, scope Scoper,
evidenceID gid.GID, evidenceID gid.GID,
cursor *page.Cursor, cursor *page.Cursor,
) error { ) error {
@@ -125,7 +129,7 @@ WHERE
func (cst *EvidenceStateTransitions) DeleteForEvidenceID( func (cst *EvidenceStateTransitions) DeleteForEvidenceID(
ctx context.Context, ctx context.Context,
conn pg.Conn, conn pg.Conn,
scope *Scope, scope Scoper,
evidenceID gid.GID, evidenceID gid.GID,
) error { ) error {
q := ` q := `

View File

@@ -54,7 +54,7 @@ func (f Framework) CursorKey() page.CursorKey {
func (f *Frameworks) LoadByOrganizationID( func (f *Frameworks) LoadByOrganizationID(
ctx context.Context, ctx context.Context,
conn pg.Conn, conn pg.Conn,
scope *Scope, scope Scoper,
organizationID gid.GID, organizationID gid.GID,
cursor *page.Cursor, cursor *page.Cursor,
) error { ) error {
@@ -100,7 +100,7 @@ WHERE
func (f *Framework) LoadByID( func (f *Framework) LoadByID(
ctx context.Context, ctx context.Context,
conn pg.Conn, conn pg.Conn,
scope *Scope, scope Scoper,
frameworkID gid.GID, frameworkID gid.GID,
) error { ) error {
q := ` q := `
@@ -143,10 +143,12 @@ LIMIT 1;
func (f Framework) Insert( func (f Framework) Insert(
ctx context.Context, ctx context.Context,
conn pg.Conn, conn pg.Conn,
scope Scoper,
) error { ) error {
q := ` q := `
INSERT INTO INSERT INTO
frameworks ( frameworks (
tenant_id,
id, id,
organization_id, organization_id,
name, name,
@@ -157,6 +159,7 @@ INSERT INTO
version version
) )
VALUES ( VALUES (
@tenant_id,
@framework_id, @framework_id,
@organization_id, @organization_id,
@name, @name,
@@ -169,6 +172,7 @@ VALUES (
` `
args := pgx.StrictNamedArgs{ args := pgx.StrictNamedArgs{
"tenant_id": scope.GetTenantID(),
"framework_id": f.ID, "framework_id": f.ID,
"organization_id": f.OrganizationID, "organization_id": f.OrganizationID,
"name": f.Name, "name": f.Name,
@@ -185,7 +189,7 @@ VALUES (
func (f Framework) Delete( func (f Framework) Delete(
ctx context.Context, ctx context.Context,
conn pg.Conn, conn pg.Conn,
scope *Scope, scope Scoper,
) error { ) error {
q := ` q := `
DELETE DELETE
@@ -207,7 +211,7 @@ WHERE
func (f *Framework) Update( func (f *Framework) Update(
ctx context.Context, ctx context.Context,
conn pg.Conn, conn pg.Conn,
scope *Scope, scope Scoper,
params UpdateFrameworkParams, params UpdateFrameworkParams,
) error { ) error {
q := ` q := `

View File

@@ -38,7 +38,7 @@ type (
func (o *Organization) LoadByID( func (o *Organization) LoadByID(
ctx context.Context, ctx context.Context,
conn pg.Conn, conn pg.Conn,
scope *Scope, scope Scoper,
organizationID gid.GID, organizationID gid.GID,
) error { ) error {
q := ` q := `
@@ -79,18 +79,21 @@ LIMIT 1;
func (o *Organization) Insert( func (o *Organization) Insert(
ctx context.Context, ctx context.Context,
conn pg.Conn, conn pg.Conn,
scope Scoper,
) error { ) error {
q := ` q := `
INSERT INTO organizations ( INSERT INTO organizations (
tenant_id,
id, id,
name, name,
logo_url, logo_url,
created_at, created_at,
updated_at updated_at
) VALUES (@id, @name, @logo_url, @created_at, @updated_at) ) VALUES (@tenant_id, @id, @name, @logo_url, @created_at, @updated_at)
` `
args := pgx.StrictNamedArgs{ args := pgx.StrictNamedArgs{
"tenant_id": scope.GetTenantID(),
"id": o.ID, "id": o.ID,
"name": o.Name, "name": o.Name,
"logo_url": o.LogoURL, "logo_url": o.LogoURL,

View File

@@ -57,7 +57,7 @@ func (p People) CursorKey() page.CursorKey {
func (p *People) LoadByID( func (p *People) LoadByID(
ctx context.Context, ctx context.Context,
conn pg.Conn, conn pg.Conn,
scope *Scope, scope Scoper,
peopleID gid.GID, peopleID gid.GID,
) error { ) error {
q := ` q := `
@@ -102,10 +102,12 @@ LIMIT 1;
func (p People) Insert( func (p People) Insert(
ctx context.Context, ctx context.Context,
conn pg.Conn, conn pg.Conn,
scope Scoper,
) error { ) error {
q := ` q := `
INSERT INTO INSERT INTO
peoples ( peoples (
tenant_id,
id, id,
organization_id, organization_id,
kind, kind,
@@ -117,6 +119,7 @@ INSERT INTO
version version
) )
VALUES ( VALUES (
@tenant_id,
@people_id, @people_id,
@organization_id, @organization_id,
@kind, @kind,
@@ -130,6 +133,7 @@ VALUES (
` `
args := pgx.StrictNamedArgs{ args := pgx.StrictNamedArgs{
"tenant_id": scope.GetTenantID(),
"people_id": p.ID, "people_id": p.ID,
"organization_id": p.OrganizationID, "organization_id": p.OrganizationID,
"kind": p.Kind, "kind": p.Kind,
@@ -147,7 +151,7 @@ VALUES (
func (p People) Delete( func (p People) Delete(
ctx context.Context, ctx context.Context,
conn pg.Conn, conn pg.Conn,
scope *Scope, scope Scoper,
) error { ) error {
q := ` q := `
DELETE FROM peoples WHERE %s AND id = @people_id DELETE FROM peoples WHERE %s AND id = @people_id
@@ -165,7 +169,7 @@ DELETE FROM peoples WHERE %s AND id = @people_id
func (p *Peoples) LoadByOrganizationID( func (p *Peoples) LoadByOrganizationID(
ctx context.Context, ctx context.Context,
conn pg.Conn, conn pg.Conn,
scope *Scope, scope Scoper,
organizationID gid.GID, organizationID gid.GID,
cursor *page.Cursor, cursor *page.Cursor,
) error { ) error {
@@ -212,7 +216,7 @@ WHERE
func (p *People) Update( func (p *People) Update(
ctx context.Context, ctx context.Context,
conn pg.Conn, conn pg.Conn,
scope *Scope, scope Scoper,
params UpdatePeopleParams, params UpdatePeopleParams,
) error { ) error {
q := ` q := `

View File

@@ -45,7 +45,7 @@ func (p Policy) CursorKey() page.CursorKey {
func (p *Policy) LoadByID( func (p *Policy) LoadByID(
ctx context.Context, ctx context.Context,
conn pg.Conn, conn pg.Conn,
scope *Scope, scope Scoper,
policyID gid.GID, policyID gid.GID,
) error { ) error {
q := ` q := `
@@ -91,7 +91,7 @@ LIMIT 1;
func (p *Policies) LoadByOrganizationID( func (p *Policies) LoadByOrganizationID(
ctx context.Context, ctx context.Context,
conn pg.Conn, conn pg.Conn,
scope *Scope, scope Scoper,
organizationID gid.GID, organizationID gid.GID,
cursor *page.Cursor, cursor *page.Cursor,
) error { ) error {
@@ -139,10 +139,12 @@ WHERE
func (p Policy) Insert( func (p Policy) Insert(
ctx context.Context, ctx context.Context,
conn pg.Conn, conn pg.Conn,
scope Scoper,
) error { ) error {
q := ` q := `
INSERT INTO INSERT INTO
policies ( policies (
tenant_id,
id, id,
organization_id, organization_id,
owner_id, owner_id,
@@ -155,6 +157,7 @@ INSERT INTO
version version
) )
VALUES ( VALUES (
@tenant_id,
@policy_id, @policy_id,
@organization_id, @organization_id,
@owner_id, @owner_id,
@@ -169,6 +172,7 @@ VALUES (
` `
args := pgx.StrictNamedArgs{ args := pgx.StrictNamedArgs{
"tenant_id": scope.GetTenantID(),
"policy_id": p.ID, "policy_id": p.ID,
"organization_id": p.OrganizationID, "organization_id": p.OrganizationID,
"owner_id": p.OwnerID, "owner_id": p.OwnerID,
@@ -187,7 +191,7 @@ VALUES (
func (p Policy) Delete( func (p Policy) Delete(
ctx context.Context, ctx context.Context,
conn pg.Conn, conn pg.Conn,
scope *Scope, scope Scoper,
) error { ) error {
q := ` q := `
DELETE FROM policies WHERE %s AND id = @policy_id DELETE FROM policies WHERE %s AND id = @policy_id
@@ -205,7 +209,7 @@ DELETE FROM policies WHERE %s AND id = @policy_id
func (p *Policy) Update( func (p *Policy) Update(
ctx context.Context, ctx context.Context,
conn pg.Conn, conn pg.Conn,
scope *Scope, scope Scoper,
params UpdatePolicyParams, params UpdatePolicyParams,
) error { ) error {
q := ` q := `

View File

@@ -19,17 +19,56 @@ import (
) )
type ( type (
Scope struct{} Scoper interface {
) SQLArguments() pgx.StrictNamedArgs
SQLFragment() string
func NewScope() *Scope { GetTenantID() *string
return &Scope{}
} }
func (*Scope) SQLArguments() pgx.StrictNamedArgs { NoScope struct{}
Scope struct {
TenantID string
}
)
var (
_ Scoper = (*NoScope)(nil)
_ Scoper = (*Scope)(nil)
)
func NewNoScope() *NoScope {
return &NoScope{}
}
func (*NoScope) SQLArguments() pgx.StrictNamedArgs {
return pgx.StrictNamedArgs{} return pgx.StrictNamedArgs{}
} }
func (*Scope) SQLFragment() string { func (*NoScope) SQLFragment() string {
return "TRUE" return "TRUE"
} }
func (*NoScope) GetTenantID() *string {
return nil
}
func NewScope(tenantID string) *Scope {
return &Scope{
TenantID: tenantID,
}
}
func (s *Scope) SQLArguments() pgx.StrictNamedArgs {
return pgx.StrictNamedArgs{
"tenant_id": s.TenantID,
}
}
func (*Scope) SQLFragment() string {
return "tenant_id = @tenant_id"
}
func (s *Scope) GetTenantID() *string {
return &s.TenantID
}

View File

@@ -49,7 +49,7 @@ func (t Task) CursorKey() page.CursorKey {
func (t *Task) LoadByID( func (t *Task) LoadByID(
ctx context.Context, ctx context.Context,
conn pg.Conn, conn pg.Conn,
scope *Scope, scope Scoper,
taskID gid.GID, taskID gid.GID,
) error { ) error {
q := ` q := `
@@ -127,10 +127,12 @@ LIMIT 1;
func (t Task) Insert( func (t Task) Insert(
ctx context.Context, ctx context.Context,
conn pg.Conn, conn pg.Conn,
scope Scoper,
) error { ) error {
q := ` q := `
WITH task_insert AS ( WITH task_insert AS (
INSERT INTO tasks ( INSERT INTO tasks (
tenant_id,
id, id,
name, name,
description, description,
@@ -139,6 +141,7 @@ WITH task_insert AS (
updated_at updated_at
) )
VALUES ( VALUES (
@tenant_id,
@task_id, @task_id,
@name, @name,
@description, @description,
@@ -161,6 +164,7 @@ VALUES (
` `
args := pgx.StrictNamedArgs{ args := pgx.StrictNamedArgs{
"tenant_id": scope.GetTenantID(),
"task_id": t.ID, "task_id": t.ID,
"control_id": t.ControlID, "control_id": t.ControlID,
"name": t.Name, "name": t.Name,
@@ -176,7 +180,7 @@ VALUES (
func (t *Tasks) LoadByControlID( func (t *Tasks) LoadByControlID(
ctx context.Context, ctx context.Context,
conn pg.Conn, conn pg.Conn,
scope *Scope, scope Scoper,
controlID gid.GID, controlID gid.GID,
cursor *page.Cursor, cursor *page.Cursor,
) error { ) error {
@@ -253,7 +257,7 @@ WHERE
func (t *Task) Delete( func (t *Task) Delete(
ctx context.Context, ctx context.Context,
conn pg.Conn, conn pg.Conn,
scope *Scope, scope Scoper,
) error { ) error {
q := ` q := `
WITH control_count AS ( WITH control_count AS (

View File

@@ -41,10 +41,12 @@ func (tst TaskStateTransition) CursorKey() page.CursorKey {
func (tst TaskStateTransition) Insert( func (tst TaskStateTransition) Insert(
ctx context.Context, ctx context.Context,
conn pg.Conn, conn pg.Conn,
scope Scoper,
) error { ) error {
q := ` q := `
INSERT INTO INSERT INTO
task_state_transitions ( task_state_transitions (
tenant_id,
id, id,
task_id, task_id,
from_state, from_state,
@@ -54,6 +56,7 @@ INSERT INTO
updated_at updated_at
) )
VALUES ( VALUES (
@tenant_id,
@task_state_transition_id, @task_state_transition_id,
@task_id, @task_id,
@from_state, @from_state,
@@ -65,6 +68,7 @@ VALUES (
` `
args := pgx.StrictNamedArgs{ args := pgx.StrictNamedArgs{
"tenant_id": scope.GetTenantID(),
"task_state_transition_id": tst.ID, "task_state_transition_id": tst.ID,
"task_id": tst.TaskID, "task_id": tst.TaskID,
"from_state": tst.FromState, "from_state": tst.FromState,
@@ -80,7 +84,7 @@ VALUES (
func (tst *TaskStateTransitions) LoadByTaskID( func (tst *TaskStateTransitions) LoadByTaskID(
ctx context.Context, ctx context.Context,
conn pg.Conn, conn pg.Conn,
scope *Scope, scope Scoper,
taskID gid.GID, taskID gid.GID,
cursor *page.Cursor, cursor *page.Cursor,
) error { ) error {

View File

@@ -70,7 +70,7 @@ func (v Vendor) CursorKey() page.CursorKey {
func (v *Vendor) LoadByID( func (v *Vendor) LoadByID(
ctx context.Context, ctx context.Context,
conn pg.Conn, conn pg.Conn,
scope *Scope, scope Scoper,
vendorID gid.GID, vendorID gid.GID,
) error { ) error {
q := ` q := `
@@ -121,10 +121,12 @@ LIMIT 1;
func (v Vendor) Insert( func (v Vendor) Insert(
ctx context.Context, ctx context.Context,
conn pg.Conn, conn pg.Conn,
scope Scoper,
) error { ) error {
q := ` q := `
INSERT INTO INSERT INTO
vendors ( vendors (
tenant_id,
id, id,
organization_id, organization_id,
name, name,
@@ -141,6 +143,7 @@ INSERT INTO
version version
) )
VALUES ( VALUES (
@tenant_id,
@vendor_id, @vendor_id,
@organization_id, @organization_id,
@name, @name,
@@ -159,6 +162,7 @@ VALUES (
` `
args := pgx.StrictNamedArgs{ args := pgx.StrictNamedArgs{
"tenant_id": scope.GetTenantID(),
"vendor_id": v.ID, "vendor_id": v.ID,
"organization_id": v.OrganizationID, "organization_id": v.OrganizationID,
"name": v.Name, "name": v.Name,
@@ -180,7 +184,7 @@ VALUES (
func (v Vendor) Delete( func (v Vendor) Delete(
ctx context.Context, ctx context.Context,
conn pg.Conn, conn pg.Conn,
scope *Scope, scope Scoper,
) error { ) error {
q := ` q := `
DELETE FROM vendors WHERE %s AND id = @vendor_id DELETE FROM vendors WHERE %s AND id = @vendor_id
@@ -198,7 +202,7 @@ DELETE FROM vendors WHERE %s AND id = @vendor_id
func (v *Vendors) LoadByOrganizationID( func (v *Vendors) LoadByOrganizationID(
ctx context.Context, ctx context.Context,
conn pg.Conn, conn pg.Conn,
scope *Scope, scope Scoper,
organizationID gid.GID, organizationID gid.GID,
cursor *page.Cursor, cursor *page.Cursor,
) error { ) error {
@@ -250,7 +254,7 @@ WHERE
func (v *Vendor) Update( func (v *Vendor) Update(
ctx context.Context, ctx context.Context,
conn pg.Conn, conn pg.Conn,
scope *Scope, scope Scoper,
params UpdateVendorParams, params UpdateVendorParams,
) error { ) error {
q := ` q := `

View File

@@ -81,11 +81,11 @@ func (s Service) CreateControl(
return fmt.Errorf("cannot load framework %q: %w", req.FrameworkID, err) return fmt.Errorf("cannot load framework %q: %w", req.FrameworkID, err)
} }
if err := control.Insert(ctx, conn); err != nil { if err := control.Insert(ctx, conn, s.scope); err != nil {
return fmt.Errorf("cannot insert control: %w", err) return fmt.Errorf("cannot insert control: %w", err)
} }
if err := controlStateTransition.Insert(ctx, conn); err != nil { if err := controlStateTransition.Insert(ctx, conn, s.scope); err != nil {
return fmt.Errorf("cannot insert control state transition: %w", err) return fmt.Errorf("cannot insert control state transition: %w", err)
} }

View File

@@ -117,11 +117,11 @@ func (s Service) CreateEvidence(
return fmt.Errorf("cannot load task %q: %w", req.TaskID, err) return fmt.Errorf("cannot load task %q: %w", req.TaskID, err)
} }
if err := evidence.Insert(ctx, conn); err != nil { if err := evidence.Insert(ctx, conn, s.scope); err != nil {
return fmt.Errorf("cannot insert evidence: %w", err) return fmt.Errorf("cannot insert evidence: %w", err)
} }
if err := evidenceStateTransition.Insert(ctx, conn); err != nil { if err := evidenceStateTransition.Insert(ctx, conn, s.scope); err != nil {
return fmt.Errorf("cannot insert evidence state transition: %w", err) return fmt.Errorf("cannot insert evidence state transition: %w", err)
} }

View File

@@ -56,7 +56,7 @@ func (s Service) CreateFramework(
err = s.pg.WithConn( err = s.pg.WithConn(
ctx, ctx,
func(conn pg.Conn) error { func(conn pg.Conn) error {
return framework.Insert(ctx, conn) return framework.Insert(ctx, conn, s.scope)
}, },
) )

View File

@@ -50,7 +50,7 @@ func (s Service) CreateOrganization(
err = s.pg.WithConn( err = s.pg.WithConn(
ctx, ctx,
func(conn pg.Conn) error { func(conn pg.Conn) error {
if err := organization.Insert(ctx, conn); err != nil { if err := organization.Insert(ctx, conn, s.scope); err != nil {
return fmt.Errorf("cannot insert control: %w", err) return fmt.Errorf("cannot insert control: %w", err)
} }

View File

@@ -63,7 +63,7 @@ func (s Service) CreatePeople(
return fmt.Errorf("cannot load organization %q: %w", req.OrganizationID, err) return fmt.Errorf("cannot load organization %q: %w", req.OrganizationID, err)
} }
if err := people.Insert(ctx, conn); err != nil { if err := people.Insert(ctx, conn, s.scope); err != nil {
return fmt.Errorf("cannot insert people: %w", err) return fmt.Errorf("cannot insert people: %w", err)
} }

View File

@@ -79,11 +79,11 @@ func (s Service) CreateTask(
return fmt.Errorf("cannot laod control %q: %w", req.ControlID, err) return fmt.Errorf("cannot laod control %q: %w", req.ControlID, err)
} }
if err := task.Insert(ctx, conn); err != nil { if err := task.Insert(ctx, conn, s.scope); err != nil {
return fmt.Errorf("cannot insert task: %w", err) return fmt.Errorf("cannot insert task: %w", err)
} }
if err := taskStateTransition.Insert(ctx, conn); err != nil { if err := taskStateTransition.Insert(ctx, conn, s.scope); err != nil {
return fmt.Errorf("cannot insert task state transition: %w", err) return fmt.Errorf("cannot insert task state transition: %w", err)
} }

View File

@@ -73,7 +73,7 @@ func (s Service) CreateVendor(
return fmt.Errorf("cannot load organization %q: %w", req.OrganizationID, err) return fmt.Errorf("cannot load organization %q: %w", req.OrganizationID, err)
} }
if err := vendor.Insert(ctx, conn); err != nil { if err := vendor.Insert(ctx, conn, s.scope); err != nil {
return fmt.Errorf("cannot insert vendor: %w", err) return fmt.Errorf("cannot insert vendor: %w", err)
} }

View File

@@ -86,7 +86,7 @@ func (s *PolicyService) Create(
return fmt.Errorf("cannot load organization %q: %w", req.OrganizationID, err) return fmt.Errorf("cannot load organization %q: %w", req.OrganizationID, err)
} }
if err := policy.Insert(ctx, conn); err != nil { if err := policy.Insert(ctx, conn, s.svc.scope); err != nil {
return fmt.Errorf("cannot insert policy: %w", err) return fmt.Errorf("cannot insert policy: %w", err)
} }

View File

@@ -27,9 +27,9 @@ import (
type ( type (
Service struct { Service struct {
pg *pg.Client pg *pg.Client
scope *coredata.Scope
s3 *s3.Client s3 *s3.Client
bucket string bucket string
scope coredata.Scoper
Policies *PolicyService Policies *PolicyService
} }
@@ -53,11 +53,24 @@ func NewService(
svc := &Service{ svc := &Service{
pg: pgClient, pg: pgClient,
s3: s3Client, s3: s3Client,
scope: coredata.NewScope(), // must be created from auth
bucket: bucket, bucket: bucket,
scope: coredata.NewNoScope(),
} }
svc.Policies = &PolicyService{svc: svc} svc.Policies = &PolicyService{svc: svc}
return svc, nil return svc, nil
} }
func (s *Service) WithTenant(tenantID string) *Service {
newSvc := &Service{
pg: s.pg,
s3: s.s3,
bucket: s.bucket,
scope: coredata.NewScope(tenantID),
}
newSvc.Policies = &PolicyService{svc: newSvc}
return newSvc
}

View File

@@ -72,7 +72,7 @@ func (s Service) UpdateTaskState(
err = s.pg.WithConn( err = s.pg.WithConn(
ctx, ctx,
func(conn pg.Conn) error { func(conn pg.Conn) error {
if err := taskStateTransition.Insert(ctx, conn); err != nil { if err := taskStateTransition.Insert(ctx, conn, s.scope); err != nil {
return fmt.Errorf("cannot insert task state transition: %w", err) return fmt.Errorf("cannot insert task state transition: %w", err)
} }