// Copyright (c) 2026 Probo Inc . // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // 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/iam/policy" "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"` UserName string `db:"user_name"` 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.Querier, resourceIDs []gid.GID, ) (policy.AttributesByID, error) { q := `SELECT id, organization_id FROM iam_scim_events 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 (s *SCIMEvent) LoadByID( ctx context.Context, conn pg.Querier, scope Scoper, eventID gid.GID, ) error { q := ` SELECT id, organization_id, scim_configuration_id, method, path, request_body, response_body, status_code, error_message, user_name, 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.Tx, 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, user_name, ip_address, created_at ) VALUES ( @id, @tenant_id, @organization_id, @scim_configuration_id, @method, @path, @request_body, @response_body, @status_code, @error_message, @user_name, @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, "user_name": s.UserName, "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.Querier, scope Scoper, organizationID gid.GID, cursor *page.Cursor[SCIMEventOrderField], filter *SCIMEventFilter, ) error { q := ` SELECT id, organization_id, scim_configuration_id, method, path, request_body, response_body, status_code, error_message, user_name, ip_address, created_at FROM iam_scim_events 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 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.Querier, 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.Querier, 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, user_name, 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.Querier, 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 }