@@ -75,6 +75,8 @@ const (
|
||||
StateOfApplicabilityEntityType uint16 = 49
|
||||
StateOfApplicabilityControlEntityType uint16 = 50
|
||||
MembershipProfileEntityType uint16 = 51
|
||||
SCIMConfigurationEntityType uint16 = 52
|
||||
SCIMEventEntityType uint16 = 53
|
||||
)
|
||||
|
||||
func NewEntityFromID(id gid.GID) (any, bool) {
|
||||
@@ -173,8 +175,14 @@ func NewEntityFromID(id gid.GID) (any, bool) {
|
||||
return &DataProtectionImpactAssessment{ID: id}, true
|
||||
case TransferImpactAssessmentEntityType:
|
||||
return &TransferImpactAssessment{ID: id}, true
|
||||
case RightsRequestEntityType:
|
||||
return &RightsRequest{ID: id}, true
|
||||
case MembershipProfileEntityType:
|
||||
return &MembershipProfile{ID: id}, true
|
||||
case SCIMConfigurationEntityType:
|
||||
return &SCIMConfiguration{ID: id}, true
|
||||
case SCIMEventEntityType:
|
||||
return &SCIMEvent{ID: id}, true
|
||||
default:
|
||||
return nil, false
|
||||
}
|
||||
@@ -390,6 +398,14 @@ var entityRegistry = map[uint16]EntityInfo{
|
||||
Model: "MembershipProfile",
|
||||
Table: "iam_membership_profiles",
|
||||
},
|
||||
SCIMConfigurationEntityType: {
|
||||
Model: "SCIMConfiguration",
|
||||
Table: "iam_scim_configurations",
|
||||
},
|
||||
SCIMEventEntityType: {
|
||||
Model: "SCIMEvent",
|
||||
Table: "iam_scim_events",
|
||||
},
|
||||
}
|
||||
|
||||
func EntityTable(entityType uint16) (string, bool) {
|
||||
|
||||
@@ -393,6 +393,7 @@ UPDATE
|
||||
iam_memberships
|
||||
SET
|
||||
role = @role,
|
||||
source = @source,
|
||||
updated_at = @updated_at
|
||||
WHERE
|
||||
id = @id
|
||||
@@ -404,6 +405,7 @@ WHERE
|
||||
args := pgx.StrictNamedArgs{
|
||||
"id": m.ID,
|
||||
"role": m.Role,
|
||||
"source": m.Source,
|
||||
"updated_at": m.UpdatedAt,
|
||||
}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
@@ -672,3 +674,35 @@ WHERE
|
||||
*m = memberships
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Memberships) ResetSCIMSources(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
organizationID gid.GID,
|
||||
) error {
|
||||
q := `
|
||||
UPDATE iam_memberships
|
||||
SET
|
||||
source = 'MANUAL',
|
||||
updated_at = @updated_at
|
||||
WHERE
|
||||
%s
|
||||
AND organization_id = @organization_id
|
||||
AND source = 'SCIM'
|
||||
`
|
||||
q = fmt.Sprintf(q, scope.SQLFragment())
|
||||
|
||||
args := pgx.NamedArgs{
|
||||
"organization_id": organizationID,
|
||||
"updated_at": time.Now(),
|
||||
}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
|
||||
_, err := conn.Exec(ctx, q, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot reset SCIM membership sources: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@ type MembershipSource string
|
||||
const (
|
||||
MembershipSourceManual MembershipSource = "MANUAL"
|
||||
MembershipSourceSAML MembershipSource = "SAML"
|
||||
MembershipSourceSCIM MembershipSource = "SCIM"
|
||||
)
|
||||
|
||||
func (s MembershipSource) String() string {
|
||||
@@ -46,6 +47,8 @@ func (s *MembershipSource) Scan(value any) error {
|
||||
*s = MembershipSourceManual
|
||||
case "SAML":
|
||||
*s = MembershipSourceSAML
|
||||
case "SCIM":
|
||||
*s = MembershipSourceSCIM
|
||||
default:
|
||||
return fmt.Errorf("invalid MembershipSource value: %q", str)
|
||||
}
|
||||
|
||||
14
pkg/coredata/migrations/20260104T155201Z.sql
Normal file
14
pkg/coredata/migrations/20260104T155201Z.sql
Normal file
@@ -0,0 +1,14 @@
|
||||
-- Create SCIM configurations table
|
||||
CREATE TABLE iam_scim_configurations (
|
||||
id TEXT PRIMARY KEY,
|
||||
tenant_id TEXT NOT NULL,
|
||||
organization_id TEXT NOT NULL REFERENCES organizations(id) ON DELETE CASCADE,
|
||||
hashed_token BYTEA NOT NULL,
|
||||
created_at TIMESTAMP WITH TIME ZONE NOT NULL,
|
||||
updated_at TIMESTAMP WITH TIME ZONE NOT NULL,
|
||||
CONSTRAINT iam_scim_configurations_organization_unique UNIQUE (organization_id)
|
||||
);
|
||||
|
||||
CREATE INDEX idx_iam_scim_configurations_tenant_id ON iam_scim_configurations(tenant_id);
|
||||
CREATE INDEX idx_iam_scim_configurations_organization_id ON iam_scim_configurations(organization_id);
|
||||
|
||||
22
pkg/coredata/migrations/20260104T155202Z.sql
Normal file
22
pkg/coredata/migrations/20260104T155202Z.sql
Normal file
@@ -0,0 +1,22 @@
|
||||
-- Create SCIM events table for debugging/audit
|
||||
CREATE TABLE iam_scim_events (
|
||||
id TEXT PRIMARY KEY,
|
||||
tenant_id TEXT NOT NULL,
|
||||
organization_id TEXT NOT NULL REFERENCES organizations(id) ON DELETE CASCADE,
|
||||
scim_configuration_id TEXT NOT NULL REFERENCES iam_scim_configurations(id) ON DELETE CASCADE,
|
||||
method TEXT NOT NULL,
|
||||
path TEXT NOT NULL,
|
||||
request_body TEXT,
|
||||
response_body TEXT,
|
||||
status_code INTEGER NOT NULL,
|
||||
error_message TEXT,
|
||||
membership_id TEXT REFERENCES iam_memberships(id) ON DELETE SET NULL,
|
||||
ip_address INET NOT NULL,
|
||||
created_at TIMESTAMP WITH TIME ZONE NOT NULL
|
||||
);
|
||||
|
||||
CREATE INDEX idx_iam_scim_events_tenant_id ON iam_scim_events(tenant_id);
|
||||
CREATE INDEX idx_iam_scim_events_organization_id ON iam_scim_events(organization_id);
|
||||
CREATE INDEX idx_iam_scim_events_scim_configuration_id ON iam_scim_events(scim_configuration_id);
|
||||
CREATE INDEX idx_iam_scim_events_created_at ON iam_scim_events(created_at DESC);
|
||||
|
||||
299
pkg/coredata/scim_configuration.go
Normal file
299
pkg/coredata/scim_configuration.go
Normal file
@@ -0,0 +1,299 @@
|
||||
// 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 (
|
||||
"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/page"
|
||||
)
|
||||
|
||||
type (
|
||||
SCIMConfiguration struct {
|
||||
ID gid.GID `db:"id"`
|
||||
OrganizationID gid.GID `db:"organization_id"`
|
||||
HashedToken []byte `db:"hashed_token"`
|
||||
CreatedAt time.Time `db:"created_at"`
|
||||
UpdatedAt time.Time `db:"updated_at"`
|
||||
}
|
||||
|
||||
SCIMConfigurations []*SCIMConfiguration
|
||||
)
|
||||
|
||||
func (s *SCIMConfiguration) CursorKey(orderBy SCIMConfigurationOrderField) page.CursorKey {
|
||||
switch orderBy {
|
||||
case SCIMConfigurationOrderFieldCreatedAt:
|
||||
return page.NewCursorKey(s.ID, s.CreatedAt)
|
||||
}
|
||||
|
||||
panic(fmt.Sprintf("unsupported order by: %s", orderBy))
|
||||
}
|
||||
|
||||
func (s *SCIMConfiguration) AuthorizationAttributes(ctx context.Context, conn pg.Conn) (map[string]string, error) {
|
||||
q := `SELECT organization_id FROM iam_scim_configurations WHERE id = $1 LIMIT 1;`
|
||||
|
||||
var organizationID gid.GID
|
||||
if err := conn.QueryRow(ctx, q, s.ID).Scan(&organizationID); err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return nil, ErrResourceNotFound
|
||||
}
|
||||
return nil, fmt.Errorf("cannot query scim configuration authorization attributes: %w", err)
|
||||
}
|
||||
|
||||
return map[string]string{"organization_id": organizationID.String()}, nil
|
||||
}
|
||||
|
||||
func (s *SCIMConfiguration) LoadByID(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
configID gid.GID,
|
||||
) error {
|
||||
q := `
|
||||
SELECT
|
||||
id,
|
||||
organization_id,
|
||||
hashed_token,
|
||||
created_at,
|
||||
updated_at
|
||||
FROM
|
||||
iam_scim_configurations
|
||||
WHERE
|
||||
%s
|
||||
AND id = @id
|
||||
LIMIT 1;
|
||||
`
|
||||
|
||||
q = fmt.Sprintf(q, scope.SQLFragment())
|
||||
|
||||
args := pgx.StrictNamedArgs{"id": configID}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
|
||||
rows, err := conn.Query(ctx, q, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot query iam_scim_configurations: %w", err)
|
||||
}
|
||||
|
||||
config, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[SCIMConfiguration])
|
||||
if err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return ErrResourceNotFound
|
||||
}
|
||||
|
||||
return fmt.Errorf("cannot collect scim_configuration: %w", err)
|
||||
}
|
||||
|
||||
*s = config
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *SCIMConfiguration) LoadByOrganizationID(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
organizationID gid.GID,
|
||||
) error {
|
||||
q := `
|
||||
SELECT
|
||||
id,
|
||||
organization_id,
|
||||
hashed_token,
|
||||
created_at,
|
||||
updated_at
|
||||
FROM
|
||||
iam_scim_configurations
|
||||
WHERE
|
||||
%s
|
||||
AND organization_id = @organization_id
|
||||
LIMIT 1;
|
||||
`
|
||||
|
||||
q = fmt.Sprintf(q, scope.SQLFragment())
|
||||
|
||||
args := pgx.StrictNamedArgs{"organization_id": organizationID}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
|
||||
rows, err := conn.Query(ctx, q, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot query iam_scim_configurations: %w", err)
|
||||
}
|
||||
|
||||
config, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[SCIMConfiguration])
|
||||
if err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return ErrResourceNotFound
|
||||
}
|
||||
|
||||
return fmt.Errorf("cannot collect scim_configuration: %w", err)
|
||||
}
|
||||
|
||||
*s = config
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *SCIMConfiguration) LoadByHashedToken(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
hashedToken []byte,
|
||||
) error {
|
||||
q := `
|
||||
SELECT
|
||||
id,
|
||||
organization_id,
|
||||
hashed_token,
|
||||
created_at,
|
||||
updated_at
|
||||
FROM
|
||||
iam_scim_configurations
|
||||
WHERE
|
||||
hashed_token = @hashed_token
|
||||
LIMIT 1;
|
||||
`
|
||||
|
||||
args := pgx.StrictNamedArgs{"hashed_token": hashedToken}
|
||||
|
||||
rows, err := conn.Query(ctx, q, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot query iam_scim_configurations: %w", err)
|
||||
}
|
||||
|
||||
config, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[SCIMConfiguration])
|
||||
if err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return ErrResourceNotFound
|
||||
}
|
||||
|
||||
return fmt.Errorf("cannot collect scim_configuration: %w", err)
|
||||
}
|
||||
|
||||
*s = config
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *SCIMConfiguration) Insert(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
) error {
|
||||
q := `
|
||||
INSERT INTO iam_scim_configurations (
|
||||
id,
|
||||
tenant_id,
|
||||
organization_id,
|
||||
hashed_token,
|
||||
created_at,
|
||||
updated_at
|
||||
) VALUES (
|
||||
@id,
|
||||
@tenant_id,
|
||||
@organization_id,
|
||||
@hashed_token,
|
||||
@created_at,
|
||||
@updated_at
|
||||
)
|
||||
`
|
||||
|
||||
args := pgx.StrictNamedArgs{
|
||||
"id": s.ID,
|
||||
"tenant_id": scope.GetTenantID(),
|
||||
"organization_id": s.OrganizationID,
|
||||
"hashed_token": s.HashedToken,
|
||||
"created_at": s.CreatedAt,
|
||||
"updated_at": s.UpdatedAt,
|
||||
}
|
||||
|
||||
_, err := conn.Exec(ctx, q, args)
|
||||
if err != nil {
|
||||
var pgErr *pgconn.PgError
|
||||
if errors.As(err, &pgErr) {
|
||||
if pgErr.Code == "23505" && pgErr.ConstraintName == "iam_scim_configurations_organization_unique" {
|
||||
return ErrResourceAlreadyExists
|
||||
}
|
||||
}
|
||||
|
||||
return fmt.Errorf("cannot insert scim_configuration: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *SCIMConfiguration) Update(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
) error {
|
||||
q := `
|
||||
UPDATE iam_scim_configurations
|
||||
SET
|
||||
hashed_token = @hashed_token,
|
||||
updated_at = @updated_at
|
||||
WHERE
|
||||
%s
|
||||
AND id = @id
|
||||
`
|
||||
|
||||
q = fmt.Sprintf(q, scope.SQLFragment())
|
||||
|
||||
args := pgx.StrictNamedArgs{
|
||||
"id": s.ID,
|
||||
"hashed_token": s.HashedToken,
|
||||
"updated_at": s.UpdatedAt,
|
||||
}
|
||||
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
|
||||
_, err := conn.Exec(ctx, q, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot update scim_configuration: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *SCIMConfiguration) Delete(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
) error {
|
||||
q := `
|
||||
DELETE FROM iam_scim_configurations
|
||||
WHERE
|
||||
%s
|
||||
AND id = @id
|
||||
`
|
||||
|
||||
q = fmt.Sprintf(q, scope.SQLFragment())
|
||||
|
||||
args := pgx.StrictNamedArgs{"id": s.ID}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
|
||||
_, err := conn.Exec(ctx, q, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot delete scim_configuration: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
40
pkg/coredata/scim_configuration_order_field.go
Normal file
40
pkg/coredata/scim_configuration_order_field.go
Normal file
@@ -0,0 +1,40 @@
|
||||
// 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
|
||||
|
||||
type (
|
||||
SCIMConfigurationOrderField string
|
||||
)
|
||||
|
||||
const (
|
||||
SCIMConfigurationOrderFieldCreatedAt SCIMConfigurationOrderField = "CREATED_AT"
|
||||
)
|
||||
|
||||
func (p SCIMConfigurationOrderField) Column() string {
|
||||
return string(p)
|
||||
}
|
||||
|
||||
func (p SCIMConfigurationOrderField) String() string {
|
||||
return string(p)
|
||||
}
|
||||
|
||||
func (p SCIMConfigurationOrderField) MarshalText() ([]byte, error) {
|
||||
return []byte(p.String()), nil
|
||||
}
|
||||
|
||||
func (p *SCIMConfigurationOrderField) UnmarshalText(text []byte) error {
|
||||
*p = SCIMConfigurationOrderField(text)
|
||||
return nil
|
||||
}
|
||||
344
pkg/coredata/scim_event.go
Normal file
344
pkg/coredata/scim_event.go
Normal file
@@ -0,0 +1,344 @@
|
||||
// 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 (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"maps"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"go.gearno.de/kit/pg"
|
||||
"go.probo.inc/probo/pkg/gid"
|
||||
"go.probo.inc/probo/pkg/page"
|
||||
)
|
||||
|
||||
type (
|
||||
SCIMEvent struct {
|
||||
ID gid.GID `db:"id"`
|
||||
OrganizationID gid.GID `db:"organization_id"`
|
||||
SCIMConfigurationID gid.GID `db:"scim_configuration_id"`
|
||||
Method string `db:"method"`
|
||||
Path string `db:"path"`
|
||||
RequestBody *string `db:"request_body"`
|
||||
ResponseBody *string `db:"response_body"`
|
||||
StatusCode int `db:"status_code"`
|
||||
ErrorMessage *string `db:"error_message"`
|
||||
MembershipID *gid.GID `db:"membership_id"`
|
||||
IPAddress net.IP `db:"ip_address"`
|
||||
CreatedAt time.Time `db:"created_at"`
|
||||
}
|
||||
|
||||
SCIMEvents []*SCIMEvent
|
||||
)
|
||||
|
||||
func (s *SCIMEvent) CursorKey(orderBy SCIMEventOrderField) page.CursorKey {
|
||||
switch orderBy {
|
||||
case SCIMEventOrderFieldCreatedAt:
|
||||
return page.NewCursorKey(s.ID, s.CreatedAt)
|
||||
}
|
||||
|
||||
panic(fmt.Sprintf("unsupported order by: %s", orderBy))
|
||||
}
|
||||
|
||||
func (s *SCIMEvent) AuthorizationAttributes(ctx context.Context, conn pg.Conn) (map[string]string, error) {
|
||||
q := `SELECT organization_id FROM iam_scim_events WHERE id = $1 LIMIT 1;`
|
||||
|
||||
var organizationID gid.GID
|
||||
if err := conn.QueryRow(ctx, q, s.ID).Scan(&organizationID); err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return nil, ErrResourceNotFound
|
||||
}
|
||||
return nil, fmt.Errorf("cannot query scim event authorization attributes: %w", err)
|
||||
}
|
||||
|
||||
return map[string]string{"organization_id": organizationID.String()}, nil
|
||||
}
|
||||
|
||||
func (s *SCIMEvent) LoadByID(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
eventID gid.GID,
|
||||
) error {
|
||||
q := `
|
||||
SELECT
|
||||
id,
|
||||
organization_id,
|
||||
scim_configuration_id,
|
||||
method,
|
||||
path,
|
||||
request_body,
|
||||
response_body,
|
||||
status_code,
|
||||
error_message,
|
||||
membership_id,
|
||||
ip_address,
|
||||
created_at
|
||||
FROM
|
||||
iam_scim_events
|
||||
WHERE
|
||||
%s
|
||||
AND id = @id
|
||||
LIMIT 1;
|
||||
`
|
||||
|
||||
q = fmt.Sprintf(q, scope.SQLFragment())
|
||||
|
||||
args := pgx.StrictNamedArgs{"id": eventID}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
|
||||
rows, err := conn.Query(ctx, q, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot query iam_scim_events: %w", err)
|
||||
}
|
||||
|
||||
event, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[SCIMEvent])
|
||||
if err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return ErrResourceNotFound
|
||||
}
|
||||
|
||||
return fmt.Errorf("cannot collect scim_event: %w", err)
|
||||
}
|
||||
|
||||
*s = event
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *SCIMEvent) Insert(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
) error {
|
||||
q := `
|
||||
INSERT INTO iam_scim_events (
|
||||
id,
|
||||
tenant_id,
|
||||
organization_id,
|
||||
scim_configuration_id,
|
||||
method,
|
||||
path,
|
||||
request_body,
|
||||
response_body,
|
||||
status_code,
|
||||
error_message,
|
||||
membership_id,
|
||||
ip_address,
|
||||
created_at
|
||||
) VALUES (
|
||||
@id,
|
||||
@tenant_id,
|
||||
@organization_id,
|
||||
@scim_configuration_id,
|
||||
@method,
|
||||
@path,
|
||||
@request_body,
|
||||
@response_body,
|
||||
@status_code,
|
||||
@error_message,
|
||||
@membership_id,
|
||||
@ip_address,
|
||||
@created_at
|
||||
)
|
||||
`
|
||||
|
||||
args := pgx.StrictNamedArgs{
|
||||
"id": s.ID,
|
||||
"tenant_id": scope.GetTenantID(),
|
||||
"organization_id": s.OrganizationID,
|
||||
"scim_configuration_id": s.SCIMConfigurationID,
|
||||
"method": s.Method,
|
||||
"path": s.Path,
|
||||
"request_body": s.RequestBody,
|
||||
"response_body": s.ResponseBody,
|
||||
"status_code": s.StatusCode,
|
||||
"error_message": s.ErrorMessage,
|
||||
"membership_id": s.MembershipID,
|
||||
"ip_address": s.IPAddress,
|
||||
"created_at": s.CreatedAt,
|
||||
}
|
||||
|
||||
_, err := conn.Exec(ctx, q, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot insert scim_event: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *SCIMEvents) LoadByOrganizationID(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
organizationID gid.GID,
|
||||
cursor *page.Cursor[SCIMEventOrderField],
|
||||
) error {
|
||||
q := `
|
||||
SELECT
|
||||
id,
|
||||
organization_id,
|
||||
scim_configuration_id,
|
||||
method,
|
||||
path,
|
||||
request_body,
|
||||
response_body,
|
||||
status_code,
|
||||
error_message,
|
||||
membership_id,
|
||||
ip_address,
|
||||
created_at
|
||||
FROM
|
||||
iam_scim_events
|
||||
WHERE
|
||||
%s
|
||||
AND organization_id = @organization_id
|
||||
AND %s
|
||||
`
|
||||
|
||||
q = fmt.Sprintf(q, scope.SQLFragment(), cursor.SQLFragment())
|
||||
|
||||
args := pgx.StrictNamedArgs{"organization_id": organizationID}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
maps.Copy(args, cursor.SQLArguments())
|
||||
|
||||
rows, err := conn.Query(ctx, q, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot query iam_scim_events: %w", err)
|
||||
}
|
||||
|
||||
events, err := pgx.CollectRows(rows, pgx.RowToAddrOfStructByName[SCIMEvent])
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot collect scim_events: %w", err)
|
||||
}
|
||||
|
||||
*s = events
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *SCIMEvents) CountByOrganizationID(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
organizationID gid.GID,
|
||||
) (int, error) {
|
||||
q := `
|
||||
SELECT
|
||||
COUNT(*)
|
||||
FROM
|
||||
iam_scim_events
|
||||
WHERE
|
||||
%s
|
||||
AND organization_id = @organization_id
|
||||
`
|
||||
|
||||
q = fmt.Sprintf(q, scope.SQLFragment())
|
||||
|
||||
args := pgx.StrictNamedArgs{"organization_id": organizationID}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
|
||||
row := conn.QueryRow(ctx, q, args)
|
||||
var count int
|
||||
if err := row.Scan(&count); err != nil {
|
||||
return 0, fmt.Errorf("cannot count scim_events: %w", err)
|
||||
}
|
||||
|
||||
return count, nil
|
||||
}
|
||||
|
||||
func (s *SCIMEvents) LoadBySCIMConfigurationID(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
scimConfigurationID gid.GID,
|
||||
cursor *page.Cursor[SCIMEventOrderField],
|
||||
) error {
|
||||
q := `
|
||||
SELECT
|
||||
id,
|
||||
organization_id,
|
||||
scim_configuration_id,
|
||||
method,
|
||||
path,
|
||||
request_body,
|
||||
response_body,
|
||||
status_code,
|
||||
error_message,
|
||||
membership_id,
|
||||
ip_address,
|
||||
created_at
|
||||
FROM
|
||||
iam_scim_events
|
||||
WHERE
|
||||
%s
|
||||
AND scim_configuration_id = @scim_configuration_id
|
||||
AND %s
|
||||
`
|
||||
|
||||
q = fmt.Sprintf(q, scope.SQLFragment(), cursor.SQLFragment())
|
||||
|
||||
args := pgx.StrictNamedArgs{"scim_configuration_id": scimConfigurationID}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
maps.Copy(args, cursor.SQLArguments())
|
||||
|
||||
rows, err := conn.Query(ctx, q, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot query iam_scim_events: %w", err)
|
||||
}
|
||||
|
||||
events, err := pgx.CollectRows(rows, pgx.RowToAddrOfStructByName[SCIMEvent])
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot collect scim_events: %w", err)
|
||||
}
|
||||
|
||||
*s = events
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *SCIMEvents) CountBySCIMConfigurationID(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
scimConfigurationID gid.GID,
|
||||
) (int, error) {
|
||||
q := `
|
||||
SELECT
|
||||
COUNT(*)
|
||||
FROM
|
||||
iam_scim_events
|
||||
WHERE
|
||||
%s
|
||||
AND scim_configuration_id = @scim_configuration_id
|
||||
`
|
||||
|
||||
q = fmt.Sprintf(q, scope.SQLFragment())
|
||||
|
||||
args := pgx.StrictNamedArgs{"scim_configuration_id": scimConfigurationID}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
|
||||
row := conn.QueryRow(ctx, q, args)
|
||||
var count int
|
||||
if err := row.Scan(&count); err != nil {
|
||||
return 0, fmt.Errorf("cannot count scim_events: %w", err)
|
||||
}
|
||||
|
||||
return count, nil
|
||||
}
|
||||
40
pkg/coredata/scim_event_order_field.go
Normal file
40
pkg/coredata/scim_event_order_field.go
Normal file
@@ -0,0 +1,40 @@
|
||||
// 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
|
||||
|
||||
type (
|
||||
SCIMEventOrderField string
|
||||
)
|
||||
|
||||
const (
|
||||
SCIMEventOrderFieldCreatedAt SCIMEventOrderField = "CREATED_AT"
|
||||
)
|
||||
|
||||
func (p SCIMEventOrderField) Column() string {
|
||||
return string(p)
|
||||
}
|
||||
|
||||
func (p SCIMEventOrderField) String() string {
|
||||
return string(p)
|
||||
}
|
||||
|
||||
func (p SCIMEventOrderField) MarshalText() ([]byte, error) {
|
||||
return []byte(p.String()), nil
|
||||
}
|
||||
|
||||
func (p *SCIMEventOrderField) UnmarshalText(text []byte) error {
|
||||
*p = SCIMEventOrderField(text)
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user