Introduce multi tenant system
Signed-off-by: gearnode <bryan@frimin.fr>
This commit is contained in:
@@ -59,6 +59,7 @@ func (c *Control) scan(r pgx.Row) error {
|
||||
func (c *Controls) LoadByFrameworkID(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope *Scope,
|
||||
frameworkID string,
|
||||
cursor *page.Cursor,
|
||||
) error {
|
||||
@@ -74,12 +75,14 @@ SELECT
|
||||
FROM
|
||||
controls
|
||||
WHERE
|
||||
framework_id = @framework_id
|
||||
%s
|
||||
AND framework_id = @framework_id
|
||||
AND %s
|
||||
`
|
||||
q = fmt.Sprintf(q, cursor.SQLFragment())
|
||||
q = fmt.Sprintf(q, scope.SQLFragment(), cursor.SQLFragment())
|
||||
|
||||
args := pgx.NamedArgs{"framework_id": frameworkID}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
maps.Copy(args, cursor.SQLArguments())
|
||||
|
||||
r, err := conn.Query(ctx, q, args)
|
||||
|
||||
@@ -58,6 +58,7 @@ func (f *Framework) scan(r pgx.Row) error {
|
||||
func (f *Frameworks) LoadByOrganizationID(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope *Scope,
|
||||
organizationID string,
|
||||
cursor *page.Cursor,
|
||||
) error {
|
||||
@@ -73,14 +74,16 @@ SELECT
|
||||
FROM
|
||||
frameworks
|
||||
WHERE
|
||||
organization_id = @organization_id
|
||||
%s
|
||||
AND organization_id = @organization_id
|
||||
AND %s
|
||||
`
|
||||
|
||||
q = fmt.Sprintf(q, cursor.SQLFragment())
|
||||
q = fmt.Sprintf(q, scope.SQLFragment(), cursor.SQLFragment())
|
||||
|
||||
args := pgx.NamedArgs{"organization_id": organizationID}
|
||||
maps.Copy(args, cursor.SQLArguments())
|
||||
maps.Copy(args, cursor.SQLArguments())
|
||||
|
||||
r, err := conn.Query(ctx, q, args)
|
||||
if err != nil {
|
||||
@@ -110,6 +113,7 @@ WHERE
|
||||
func (f *Framework) LoadByID(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope *Scope,
|
||||
frameworkID string,
|
||||
) error {
|
||||
q := `
|
||||
@@ -124,10 +128,15 @@ SELECT
|
||||
FROM
|
||||
frameworks
|
||||
WHERE
|
||||
framework_id = @framework_id
|
||||
%s
|
||||
AND framework_id = @framework_id
|
||||
LIMIT 1;
|
||||
`
|
||||
|
||||
q = fmt.Sprintf(q, scope.SQLFragment())
|
||||
|
||||
args := pgx.NamedArgs{"framework_id": frameworkID}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
r := conn.QueryRow(ctx, q, args)
|
||||
|
||||
f2 := Framework{}
|
||||
|
||||
@@ -17,6 +17,8 @@ package coredata
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
"maps"
|
||||
"fmt"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"go.gearno.de/kit/pg"
|
||||
@@ -43,6 +45,7 @@ func (o *Organization) scan(r pgx.Row) error {
|
||||
func (o *Organization) LoadByID(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope *Scope,
|
||||
organizationID string,
|
||||
) error {
|
||||
q := `
|
||||
@@ -54,11 +57,16 @@ SELECT
|
||||
FROM
|
||||
organizations
|
||||
WHERE
|
||||
organization_id = @organization_id
|
||||
%s
|
||||
AND organization_id = @organization_id
|
||||
LIMIT 1;
|
||||
`
|
||||
|
||||
q = fmt.Sprintf(q, scope.SQLFragment())
|
||||
|
||||
args := pgx.NamedArgs{"organization_id": organizationID}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
|
||||
r := conn.QueryRow(ctx, q, args)
|
||||
|
||||
o2 := Organization{}
|
||||
|
||||
35
pkg/probo/coredata/scope.go
Normal file
35
pkg/probo/coredata/scope.go
Normal file
@@ -0,0 +1,35 @@
|
||||
// Copyright (c) 2025 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 (
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
type (
|
||||
Scope struct {}
|
||||
)
|
||||
|
||||
func NewScope() *Scope {
|
||||
return &Scope{}
|
||||
}
|
||||
|
||||
func (*Scope) SQLArguments() pgx.NamedArgs {
|
||||
return pgx.NamedArgs{}
|
||||
}
|
||||
|
||||
func (*Scope) SQLFragment() string {
|
||||
return "TRUE"
|
||||
}
|
||||
@@ -55,6 +55,7 @@ func (t *Task) scan(r pgx.Row) error {
|
||||
func (t *Tasks) LoadByControlID(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope *Scope,
|
||||
controlID string,
|
||||
cursor *page.Cursor,
|
||||
) error {
|
||||
@@ -68,13 +69,15 @@ SELECT
|
||||
FROM
|
||||
tasks
|
||||
WHERE
|
||||
control_id = @control_id
|
||||
%s
|
||||
AND control_id = @control_id
|
||||
AND %s
|
||||
`
|
||||
|
||||
q = fmt.Sprintf(q, cursor.SQLFragment())
|
||||
q = fmt.Sprintf(q, scope.SQLFragment(), cursor.SQLFragment())
|
||||
|
||||
args := pgx.NamedArgs{"control_id": controlID}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
maps.Copy(args, cursor.SQLArguments())
|
||||
|
||||
r, err := conn.Query(ctx, q, args)
|
||||
|
||||
@@ -25,12 +25,14 @@ import (
|
||||
type (
|
||||
Service struct {
|
||||
pg *pg.Client
|
||||
scope *coredata.Scope
|
||||
}
|
||||
)
|
||||
|
||||
func NewService(ctx context.Context, pgClient *pg.Client) *Service {
|
||||
return &Service{
|
||||
pg: pgClient,
|
||||
scope: coredata.NewScope(), // must be created from auth
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,7 +45,7 @@ func (s *Service) GetOrganization(
|
||||
err := s.pg.WithConn(
|
||||
ctx,
|
||||
func(conn pg.Conn) error {
|
||||
return organization.LoadByID(ctx, conn, organizationID)
|
||||
return organization.LoadByID(ctx, conn, s.scope, organizationID)
|
||||
},
|
||||
)
|
||||
|
||||
@@ -64,7 +66,7 @@ func (s *Service) ListOrganizationFrameworks(
|
||||
err := s.pg.WithConn(
|
||||
ctx,
|
||||
func(conn pg.Conn) error {
|
||||
return frameworks.LoadByOrganizationID(ctx, conn, organizationID, cursor)
|
||||
return frameworks.LoadByOrganizationID(ctx, conn, s.scope, organizationID, cursor)
|
||||
},
|
||||
)
|
||||
|
||||
@@ -85,7 +87,7 @@ func (s *Service) ListFrameworkControls(
|
||||
err := s.pg.WithConn(
|
||||
ctx,
|
||||
func(conn pg.Conn) error {
|
||||
return controls.LoadByFrameworkID(ctx, conn, frameworkID, cursor)
|
||||
return controls.LoadByFrameworkID(ctx, conn, s.scope, frameworkID, cursor)
|
||||
},
|
||||
)
|
||||
|
||||
@@ -106,7 +108,7 @@ func (s *Service) ListControlTasks(
|
||||
err := s.pg.WithConn(
|
||||
ctx,
|
||||
func(conn pg.Conn) error {
|
||||
return tasks.LoadByControlID(ctx, conn, controlID, cursor)
|
||||
return tasks.LoadByControlID(ctx, conn, s.scope, controlID, cursor)
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user