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(
|
func (c *Controls) LoadByFrameworkID(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
conn pg.Conn,
|
conn pg.Conn,
|
||||||
|
scope *Scope,
|
||||||
frameworkID string,
|
frameworkID string,
|
||||||
cursor *page.Cursor,
|
cursor *page.Cursor,
|
||||||
) error {
|
) error {
|
||||||
@@ -74,12 +75,14 @@ SELECT
|
|||||||
FROM
|
FROM
|
||||||
controls
|
controls
|
||||||
WHERE
|
WHERE
|
||||||
framework_id = @framework_id
|
%s
|
||||||
|
AND framework_id = @framework_id
|
||||||
AND %s
|
AND %s
|
||||||
`
|
`
|
||||||
q = fmt.Sprintf(q, cursor.SQLFragment())
|
q = fmt.Sprintf(q, scope.SQLFragment(), cursor.SQLFragment())
|
||||||
|
|
||||||
args := pgx.NamedArgs{"framework_id": frameworkID}
|
args := pgx.NamedArgs{"framework_id": frameworkID}
|
||||||
|
maps.Copy(args, scope.SQLArguments())
|
||||||
maps.Copy(args, cursor.SQLArguments())
|
maps.Copy(args, cursor.SQLArguments())
|
||||||
|
|
||||||
r, err := conn.Query(ctx, q, args)
|
r, err := conn.Query(ctx, q, args)
|
||||||
|
|||||||
@@ -58,6 +58,7 @@ func (f *Framework) scan(r pgx.Row) error {
|
|||||||
func (f *Frameworks) LoadByOrganizationID(
|
func (f *Frameworks) LoadByOrganizationID(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
conn pg.Conn,
|
conn pg.Conn,
|
||||||
|
scope *Scope,
|
||||||
organizationID string,
|
organizationID string,
|
||||||
cursor *page.Cursor,
|
cursor *page.Cursor,
|
||||||
) error {
|
) error {
|
||||||
@@ -73,14 +74,16 @@ SELECT
|
|||||||
FROM
|
FROM
|
||||||
frameworks
|
frameworks
|
||||||
WHERE
|
WHERE
|
||||||
organization_id = @organization_id
|
%s
|
||||||
|
AND organization_id = @organization_id
|
||||||
AND %s
|
AND %s
|
||||||
`
|
`
|
||||||
|
|
||||||
q = fmt.Sprintf(q, cursor.SQLFragment())
|
q = fmt.Sprintf(q, scope.SQLFragment(), cursor.SQLFragment())
|
||||||
|
|
||||||
args := pgx.NamedArgs{"organization_id": organizationID}
|
args := pgx.NamedArgs{"organization_id": organizationID}
|
||||||
maps.Copy(args, cursor.SQLArguments())
|
maps.Copy(args, cursor.SQLArguments())
|
||||||
|
maps.Copy(args, cursor.SQLArguments())
|
||||||
|
|
||||||
r, err := conn.Query(ctx, q, args)
|
r, err := conn.Query(ctx, q, args)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -110,6 +113,7 @@ WHERE
|
|||||||
func (f *Framework) LoadByID(
|
func (f *Framework) LoadByID(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
conn pg.Conn,
|
conn pg.Conn,
|
||||||
|
scope *Scope,
|
||||||
frameworkID string,
|
frameworkID string,
|
||||||
) error {
|
) error {
|
||||||
q := `
|
q := `
|
||||||
@@ -124,10 +128,15 @@ SELECT
|
|||||||
FROM
|
FROM
|
||||||
frameworks
|
frameworks
|
||||||
WHERE
|
WHERE
|
||||||
framework_id = @framework_id
|
%s
|
||||||
|
AND framework_id = @framework_id
|
||||||
LIMIT 1;
|
LIMIT 1;
|
||||||
`
|
`
|
||||||
|
|
||||||
|
q = fmt.Sprintf(q, scope.SQLFragment())
|
||||||
|
|
||||||
args := pgx.NamedArgs{"framework_id": frameworkID}
|
args := pgx.NamedArgs{"framework_id": frameworkID}
|
||||||
|
maps.Copy(args, scope.SQLArguments())
|
||||||
r := conn.QueryRow(ctx, q, args)
|
r := conn.QueryRow(ctx, q, args)
|
||||||
|
|
||||||
f2 := Framework{}
|
f2 := Framework{}
|
||||||
|
|||||||
@@ -17,6 +17,8 @@ package coredata
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"time"
|
"time"
|
||||||
|
"maps"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
"github.com/jackc/pgx/v5"
|
"github.com/jackc/pgx/v5"
|
||||||
"go.gearno.de/kit/pg"
|
"go.gearno.de/kit/pg"
|
||||||
@@ -43,6 +45,7 @@ func (o *Organization) scan(r pgx.Row) error {
|
|||||||
func (o *Organization) LoadByID(
|
func (o *Organization) LoadByID(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
conn pg.Conn,
|
conn pg.Conn,
|
||||||
|
scope *Scope,
|
||||||
organizationID string,
|
organizationID string,
|
||||||
) error {
|
) error {
|
||||||
q := `
|
q := `
|
||||||
@@ -54,11 +57,16 @@ SELECT
|
|||||||
FROM
|
FROM
|
||||||
organizations
|
organizations
|
||||||
WHERE
|
WHERE
|
||||||
organization_id = @organization_id
|
%s
|
||||||
|
AND organization_id = @organization_id
|
||||||
LIMIT 1;
|
LIMIT 1;
|
||||||
`
|
`
|
||||||
|
|
||||||
|
q = fmt.Sprintf(q, scope.SQLFragment())
|
||||||
|
|
||||||
args := pgx.NamedArgs{"organization_id": organizationID}
|
args := pgx.NamedArgs{"organization_id": organizationID}
|
||||||
|
maps.Copy(args, scope.SQLArguments())
|
||||||
|
|
||||||
r := conn.QueryRow(ctx, q, args)
|
r := conn.QueryRow(ctx, q, args)
|
||||||
|
|
||||||
o2 := Organization{}
|
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(
|
func (t *Tasks) LoadByControlID(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
conn pg.Conn,
|
conn pg.Conn,
|
||||||
|
scope *Scope,
|
||||||
controlID string,
|
controlID string,
|
||||||
cursor *page.Cursor,
|
cursor *page.Cursor,
|
||||||
) error {
|
) error {
|
||||||
@@ -68,13 +69,15 @@ SELECT
|
|||||||
FROM
|
FROM
|
||||||
tasks
|
tasks
|
||||||
WHERE
|
WHERE
|
||||||
control_id = @control_id
|
%s
|
||||||
|
AND control_id = @control_id
|
||||||
AND %s
|
AND %s
|
||||||
`
|
`
|
||||||
|
|
||||||
q = fmt.Sprintf(q, cursor.SQLFragment())
|
q = fmt.Sprintf(q, scope.SQLFragment(), cursor.SQLFragment())
|
||||||
|
|
||||||
args := pgx.NamedArgs{"control_id": controlID}
|
args := pgx.NamedArgs{"control_id": controlID}
|
||||||
|
maps.Copy(args, scope.SQLArguments())
|
||||||
maps.Copy(args, cursor.SQLArguments())
|
maps.Copy(args, cursor.SQLArguments())
|
||||||
|
|
||||||
r, err := conn.Query(ctx, q, args)
|
r, err := conn.Query(ctx, q, args)
|
||||||
|
|||||||
@@ -25,12 +25,14 @@ import (
|
|||||||
type (
|
type (
|
||||||
Service struct {
|
Service struct {
|
||||||
pg *pg.Client
|
pg *pg.Client
|
||||||
|
scope *coredata.Scope
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewService(ctx context.Context, pgClient *pg.Client) *Service {
|
func NewService(ctx context.Context, pgClient *pg.Client) *Service {
|
||||||
return &Service{
|
return &Service{
|
||||||
pg: pgClient,
|
pg: pgClient,
|
||||||
|
scope: coredata.NewScope(), // must be created from auth
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -43,7 +45,7 @@ func (s *Service) GetOrganization(
|
|||||||
err := s.pg.WithConn(
|
err := s.pg.WithConn(
|
||||||
ctx,
|
ctx,
|
||||||
func(conn pg.Conn) error {
|
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(
|
err := s.pg.WithConn(
|
||||||
ctx,
|
ctx,
|
||||||
func(conn pg.Conn) error {
|
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(
|
err := s.pg.WithConn(
|
||||||
ctx,
|
ctx,
|
||||||
func(conn pg.Conn) error {
|
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(
|
err := s.pg.WithConn(
|
||||||
ctx,
|
ctx,
|
||||||
func(conn pg.Conn) error {
|
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