diff --git a/backend/v3/domain/id_provider.go b/backend/v3/domain/id_provider.go index 4998b6f44..12a962bba 100644 --- a/backend/v3/domain/id_provider.go +++ b/backend/v3/domain/id_provider.go @@ -40,7 +40,7 @@ const ( type IDPAutoLinkingField uint8 const ( - IDPAutoLinkingFieldUserName IDPAutoLinkingField = iota + 1 + IDPAutoLinkingFieldUsername IDPAutoLinkingField = iota + 1 IDPAutoLinkingFieldEmail ) @@ -64,12 +64,10 @@ type IdentityProvider struct { // Type represents the type of and idp. It is a pointer because it can be nil during the migration of the events Type *IDPType `json:"type,omitempty" db:"type"` AllowCreation bool `json:"allowCreation,omitempty" db:"allow_creation"` - AutoRegister bool `json:"autoRegister,omitempty" db:"auto_register"` AllowAutoCreation bool `json:"allowAutoCreation,omitempty" db:"allow_auto_creation"` AllowAutoUpdate bool `json:"allowAutoUpdate,omitempty" db:"allow_auto_update"` AllowLinking bool `json:"allowLinking,omitempty" db:"allow_linking"` AutoLinkingField *IDPAutoLinkingField `json:"autoLinkingField,omitempty" db:"auto_linking_field"` - StylingType *int16 `json:"stylingType,omitempty" db:"styling_type"` Payload json.RawMessage `json:"payload,omitempty" db:"payload"` CreatedAt time.Time `json:"createdAt,omitzero" db:"created_at"` UpdatedAt time.Time `json:"updatedAt,omitzero" db:"updated_at"` @@ -191,16 +189,16 @@ type IDPGitlab struct { Gitlab } -type GitlabSelfHosting struct { +type GitlabSelfHosted struct { Issuer string `json:"issuer"` ClientID string `json:"clientId,omitempty"` ClientSecret *crypto.CryptoValue `json:"clientSecret,omitempty"` Scopes []string `json:"scopes,omitempty"` } -type IDPGitlabSelfHosting struct { +type IDPGitlabSelfHosted struct { *IdentityProvider - GitlabSelfHosting + GitlabSelfHosted } type LDAP struct { @@ -269,13 +267,6 @@ type IDPSAML struct { SAML } -// IDPIdentifierCondition is used to help specify a single identity_provider, -// it will either be used as the identity_provider ID or identity_provider name, -// as identity_provider can be identified either using (instanceID + OrganizationID + ID) OR (instanceID + OrganizationID + name) -type IDPIdentifierCondition interface { - database.Condition -} - type idProviderColumns interface { InstanceIDColumn() database.Column OrgIDColumn() database.Column @@ -284,12 +275,10 @@ type idProviderColumns interface { NameColumn() database.Column TypeColumn() database.Column AllowCreationColumn() database.Column - AutoRegisterColumn() database.Column AllowAutoCreationColumn() database.Column AllowAutoUpdateColumn() database.Column AllowLinkingColumn() database.Column - AllowAutoLinkingColumn() database.Column - StylingTypeColumn() database.Column + AutoLinkingFieldColumn() database.Column PayloadColumn() database.Column CreatedAtColumn() database.Column UpdatedAtColumn() database.Column @@ -300,32 +289,24 @@ type idProviderConditions interface { PrimaryKeyCondition(instanceID, idpID string) database.Condition InstanceIDCondition(id string) database.Condition OrgIDCondition(id *string) database.Condition - IDCondition(id string) IDPIdentifierCondition + IDCondition(id string) database.Condition StateCondition(state IDPState) database.Condition - NameCondition(name string) IDPIdentifierCondition + NameCondition(op database.TextOperation, name string) database.Condition TypeCondition(typee IDPType) database.Condition - AutoRegisterCondition(allow bool) database.Condition - AllowCreationCondition(allow bool) database.Condition - AllowAutoCreationCondition(allow bool) database.Condition - AllowAutoUpdateCondition(allow bool) database.Condition - AllowLinkingCondition(allow bool) database.Condition - AllowAutoLinkingCondition(linkingType IDPAutoLinkingField) database.Condition - StylingTypeCondition(style int16) database.Condition - PayloadCondition(payload string) database.Condition } type idProviderChanges interface { SetName(name string) database.Change SetState(state IDPState) database.Change SetAllowCreation(allow bool) database.Change - SetAutoRegister(allow bool) database.Change SetAllowAutoCreation(allow bool) database.Change SetAllowAutoUpdate(allow bool) database.Change SetAllowLinking(allow bool) database.Change - SetAutoAllowLinking(allow bool) database.Change - SetStylingType(stylingType int16) database.Change + SetAutoLinkingField(field *IDPAutoLinkingField) database.Change SetPayload(payload string) database.Change - SetUpdatedAt(createdAt *time.Time) database.Change + SetUpdatedAt(updatedAt *time.Time) database.Change + // SetType is used for migrating generic idps to specific types. + SetType(typee IDPType) database.Change } type IDProviderRepository interface { @@ -335,25 +316,25 @@ type IDProviderRepository interface { idProviderConditions idProviderChanges - Get(ctx context.Context, client database.QueryExecutor, id IDPIdentifierCondition, instanceID string, orgID *string) (*IdentityProvider, error) - List(ctx context.Context, client database.QueryExecutor, conditions ...database.Condition) ([]*IdentityProvider, error) + Get(ctx context.Context, client database.QueryExecutor, opts ...database.QueryOption) (*IdentityProvider, error) + List(ctx context.Context, client database.QueryExecutor, opts ...database.QueryOption) ([]*IdentityProvider, error) Create(ctx context.Context, client database.QueryExecutor, idp *IdentityProvider) error - Update(ctx context.Context, client database.QueryExecutor, id IDPIdentifierCondition, instanceID string, orgID *string, changes ...database.Change) (int64, error) - Delete(ctx context.Context, client database.QueryExecutor, id IDPIdentifierCondition, instanceID string, orgID *string) (int64, error) + Update(ctx context.Context, client database.QueryExecutor, condition database.Condition, changes ...database.Change) (int64, error) + Delete(ctx context.Context, client database.QueryExecutor, condition database.Condition) (int64, error) - GetOIDC(ctx context.Context, client database.QueryExecutor, id IDPIdentifierCondition, instanceID string, orgID *string) (*IDPOIDC, error) - GetJWT(ctx context.Context, client database.QueryExecutor, id IDPIdentifierCondition, instanceID string, orgID *string) (*IDPJWT, error) + GetOIDC(ctx context.Context, client database.QueryExecutor, opts ...database.QueryOption) (*IDPOIDC, error) + GetJWT(ctx context.Context, client database.QueryExecutor, opts ...database.QueryOption) (*IDPJWT, error) - GetOAuth(ctx context.Context, client database.QueryExecutor, id IDPIdentifierCondition, instanceID string, orgID *string) (*IDPOAuth, error) + GetOAuth(ctx context.Context, client database.QueryExecutor, opts ...database.QueryOption) (*IDPOAuth, error) - GetAzureAD(ctx context.Context, client database.QueryExecutor, id IDPIdentifierCondition, instanceID string, orgID *string) (*IDPAzureAD, error) - GetGoogle(ctx context.Context, client database.QueryExecutor, id IDPIdentifierCondition, instanceID string, orgID *string) (*IDPGoogle, error) - GetGithub(ctx context.Context, client database.QueryExecutor, id IDPIdentifierCondition, instanceID string, orgID *string) (*IDPGithub, error) - GetGithubEnterprise(ctx context.Context, client database.QueryExecutor, id IDPIdentifierCondition, instanceID string, orgID *string) (*IDPGithubEnterprise, error) - GetGitlab(ctx context.Context, client database.QueryExecutor, id IDPIdentifierCondition, instanceID string, orgID *string) (*IDPGitlab, error) - GetGitlabSelfHosting(ctx context.Context, client database.QueryExecutor, id IDPIdentifierCondition, instanceID string, orgID *string) (*IDPGitlabSelfHosting, error) - GetLDAP(ctx context.Context, client database.QueryExecutor, id IDPIdentifierCondition, instanceID string, orgID *string) (*IDPLDAP, error) - GetApple(ctx context.Context, client database.QueryExecutor, id IDPIdentifierCondition, instanceID string, orgID *string) (*IDPApple, error) - GetSAML(ctx context.Context, client database.QueryExecutor, id IDPIdentifierCondition, instanceID string, orgID *string) (*IDPSAML, error) + GetAzureAD(ctx context.Context, client database.QueryExecutor, opts ...database.QueryOption) (*IDPAzureAD, error) + GetGoogle(ctx context.Context, client database.QueryExecutor, opts ...database.QueryOption) (*IDPGoogle, error) + GetGithub(ctx context.Context, client database.QueryExecutor, opts ...database.QueryOption) (*IDPGithub, error) + GetGithubEnterprise(ctx context.Context, client database.QueryExecutor, opts ...database.QueryOption) (*IDPGithubEnterprise, error) + GetGitlab(ctx context.Context, client database.QueryExecutor, opts ...database.QueryOption) (*IDPGitlab, error) + GetGitlabSelfHosted(ctx context.Context, client database.QueryExecutor, opts ...database.QueryOption) (*IDPGitlabSelfHosted, error) + GetLDAP(ctx context.Context, client database.QueryExecutor, opts ...database.QueryOption) (*IDPLDAP, error) + GetApple(ctx context.Context, client database.QueryExecutor, opts ...database.QueryOption) (*IDPApple, error) + GetSAML(ctx context.Context, client database.QueryExecutor, opts ...database.QueryOption) (*IDPSAML, error) } diff --git a/backend/v3/domain/idpautolinkingfield_enumer.go b/backend/v3/domain/idpautolinkingfield_enumer.go index 037c471eb..62b14a6b5 100644 --- a/backend/v3/domain/idpautolinkingfield_enumer.go +++ b/backend/v3/domain/idpautolinkingfield_enumer.go @@ -25,15 +25,15 @@ func (i IDPAutoLinkingField) String() string { // Re-run the stringer command to generate them again. func _IDPAutoLinkingFieldNoOp() { var x [1]struct{} - _ = x[IDPAutoLinkingFieldUserName-(1)] + _ = x[IDPAutoLinkingFieldUsername-(1)] _ = x[IDPAutoLinkingFieldEmail-(2)] } -var _IDPAutoLinkingFieldValues = []IDPAutoLinkingField{IDPAutoLinkingFieldUserName, IDPAutoLinkingFieldEmail} +var _IDPAutoLinkingFieldValues = []IDPAutoLinkingField{IDPAutoLinkingFieldUsername, IDPAutoLinkingFieldEmail} var _IDPAutoLinkingFieldNameToValueMap = map[string]IDPAutoLinkingField{ - _IDPAutoLinkingFieldName[0:8]: IDPAutoLinkingFieldUserName, - _IDPAutoLinkingFieldLowerName[0:8]: IDPAutoLinkingFieldUserName, + _IDPAutoLinkingFieldName[0:8]: IDPAutoLinkingFieldUsername, + _IDPAutoLinkingFieldLowerName[0:8]: IDPAutoLinkingFieldUsername, _IDPAutoLinkingFieldName[8:13]: IDPAutoLinkingFieldEmail, _IDPAutoLinkingFieldLowerName[8:13]: IDPAutoLinkingFieldEmail, } diff --git a/backend/v3/storage/database/dialect/postgres/migration/005_identity_providers_table/up.sql b/backend/v3/storage/database/dialect/postgres/migration/005_identity_providers_table/up.sql index 760efeeb2..86723edc7 100644 --- a/backend/v3/storage/database/dialect/postgres/migration/005_identity_providers_table/up.sql +++ b/backend/v3/storage/database/dialect/postgres/migration/005_identity_providers_table/up.sql @@ -10,13 +10,11 @@ CREATE TABLE zitadel.identity_providers ( , state zitadel.idp_state NOT NULL DEFAULT 'active' , name TEXT NOT NULL CHECK (name <> '') , type SMALLINT DEFAULT NULL - , auto_register BOOLEAN NOT NULL DEFAULT TRUE , allow_creation BOOLEAN NOT NULL DEFAULT TRUE , allow_auto_creation BOOLEAN NOT NULL DEFAULT TRUE , allow_auto_update BOOLEAN NOT NULL DEFAULT TRUE , allow_linking BOOLEAN NOT NULL DEFAULT TRUE , auto_linking_field SMALLINT DEFAULT NULL - , styling_type SMALLINT , payload JSONB , created_at TIMESTAMPTZ NOT NULL DEFAULT now() @@ -25,8 +23,8 @@ CREATE TABLE zitadel.identity_providers ( , PRIMARY KEY (instance_id, id) , CONSTRAINT identity_providers_id_unique UNIQUE NULLS NOT DISTINCT (instance_id, org_id, id) , CONSTRAINT identity_providers_name_unique UNIQUE NULLS NOT DISTINCT (instance_id, org_id, name) - , FOREIGN KEY (instance_id) REFERENCES zitadel.instances(id) - , FOREIGN KEY (instance_id, org_id) REFERENCES zitadel.organizations(instance_id, id) + , FOREIGN KEY (instance_id) REFERENCES zitadel.instances(id) ON DELETE CASCADE + , FOREIGN KEY (instance_id, org_id) REFERENCES zitadel.organizations(instance_id, id) ON DELETE CASCADE ); -- CREATE INDEX idx_identity_providers_org_id ON identity_providers(instance_id, org_id) WHERE org_id IS NOT NULL; diff --git a/backend/v3/storage/database/events_testing/id_provider_instance_test.go b/backend/v3/storage/database/events_testing/id_provider_instance_test.go index 7c6a5377c..bfabf3bfd 100644 --- a/backend/v3/storage/database/events_testing/id_provider_instance_test.go +++ b/backend/v3/storage/database/events_testing/id_provider_instance_test.go @@ -145,7 +145,7 @@ func TestServer_TestIDProviderInstanceReduces(t *testing.T) { name := gofakeit.Name() before := time.Now() - addOIDC, err := AdminClient.AddOIDCIDP(IAMCTX, defaultOIDCIDReq(name, true)) + addOIDC, err := AdminClient.AddOIDCIDP(IAMCTX, defaultOIDCIDPReq(name, true)) after := time.Now() require.NoError(t, err) @@ -153,11 +153,7 @@ func TestServer_TestIDProviderInstanceReduces(t *testing.T) { retryDuration, tick := integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - idp, err := idpRepo.Get(IAMCTX, pool, - idpRepo.NameCondition(name), - instanceID, - nil, - ) + idp, err := idpRepo.Get(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.NameCondition(database.TextOperationEqual, name)))) require.NoError(t, err) // event iam.idp.config.added @@ -166,12 +162,10 @@ func TestServer_TestIDProviderInstanceReduces(t *testing.T) { assert.Equal(t, addOIDC.IdpId, idp.ID) assert.Equal(t, domain.IDPStateActive, idp.State) assert.Equal(t, name, idp.Name) - assert.Equal(t, true, idp.AutoRegister) assert.Equal(t, true, idp.AllowCreation) assert.Equal(t, false, idp.AllowAutoUpdate) assert.Equal(t, true, idp.AllowLinking) assert.Nil(t, idp.AutoLinkingField) - assert.Equal(t, int16(idp_grpc.IDPStylingType_STYLING_TYPE_GOOGLE), *idp.StylingType) assert.WithinRange(t, idp.UpdatedAt, before, after) assert.WithinRange(t, idp.CreatedAt, before, after) }, retryDuration, tick) @@ -180,7 +174,7 @@ func TestServer_TestIDProviderInstanceReduces(t *testing.T) { t.Run("test instance idp update reduces", func(t *testing.T) { name := gofakeit.Name() - addOIDC, err := AdminClient.AddOIDCIDP(IAMCTX, defaultOIDCIDReq(name, true)) + addOIDC, err := AdminClient.AddOIDCIDP(IAMCTX, defaultOIDCIDPReq(name, true)) require.NoError(t, err) name = "new_" + name @@ -199,18 +193,12 @@ func TestServer_TestIDProviderInstanceReduces(t *testing.T) { retryDuration, tick := integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - idp, err := idpRepo.Get(IAMCTX, pool, - idpRepo.NameCondition(name), - instanceID, - nil, - ) + idp, err := idpRepo.Get(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.NameCondition(database.TextOperationEqual, name)))) require.NoError(t, err) // event iam.idp.config.changed assert.Equal(t, addOIDC.IdpId, idp.ID) assert.Equal(t, name, idp.Name) - assert.Equal(t, false, idp.AutoRegister) - assert.Equal(t, int16(idp_grpc.IDPStylingType_STYLING_TYPE_UNSPECIFIED), *idp.StylingType) assert.WithinRange(t, idp.UpdatedAt, before, after) }, retryDuration, tick) }) @@ -218,7 +206,7 @@ func TestServer_TestIDProviderInstanceReduces(t *testing.T) { t.Run("test instance idp deactivate reduces", func(t *testing.T) { name := gofakeit.Name() - addOIDC, err := AdminClient.AddOIDCIDP(IAMCTX, defaultOIDCIDReq(name, true)) + addOIDC, err := AdminClient.AddOIDCIDP(IAMCTX, defaultOIDCIDPReq(name, true)) require.NoError(t, err) // deactivate idp @@ -233,11 +221,7 @@ func TestServer_TestIDProviderInstanceReduces(t *testing.T) { retryDuration, tick := integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - idp, err := idpRepo.Get(IAMCTX, pool, - idpRepo.IDCondition(addOIDC.IdpId), - instanceID, - nil, - ) + idp, err := idpRepo.Get(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.IDCondition(addOIDC.IdpId)))) require.NoError(t, err) // event iam.idp.config.deactivated @@ -250,7 +234,7 @@ func TestServer_TestIDProviderInstanceReduces(t *testing.T) { t.Run("test instance idp config reactivate reduces", func(t *testing.T) { name := gofakeit.Name() - addOIDC, err := AdminClient.AddOIDCIDP(IAMCTX, defaultOIDCIDReq(name, true)) + addOIDC, err := AdminClient.AddOIDCIDP(IAMCTX, defaultOIDCIDPReq(name, true)) require.NoError(t, err) idpRepo := repository.IDProviderRepository() @@ -263,11 +247,7 @@ func TestServer_TestIDProviderInstanceReduces(t *testing.T) { // wait for idp to be deactivated retryDuration, tick := integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - idp, err := idpRepo.Get(IAMCTX, pool, - idpRepo.IDCondition(addOIDC.IdpId), - instanceID, - nil, - ) + idp, err := idpRepo.Get(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.IDCondition(addOIDC.IdpId)))) require.NoError(t, err) assert.Equal(t, addOIDC.IdpId, idp.ID) @@ -284,11 +264,7 @@ func TestServer_TestIDProviderInstanceReduces(t *testing.T) { retryDuration, tick = integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - idp, err := idpRepo.Get(IAMCTX, pool, - idpRepo.IDCondition(addOIDC.IdpId), - instanceID, - nil, - ) + idp, err := idpRepo.Get(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.IDCondition(addOIDC.IdpId)))) require.NoError(t, err) // event iam.idp.config.reactivated @@ -302,10 +278,16 @@ func TestServer_TestIDProviderInstanceReduces(t *testing.T) { name := gofakeit.Name() // add idp - addOIDC, err := AdminClient.AddOIDCIDP(IAMCTX, defaultOIDCIDReq(name, true)) + addOIDC, err := AdminClient.AddOIDCIDP(IAMCTX, defaultOIDCIDPReq(name, true)) require.NoError(t, err) idpRepo := repository.IDProviderRepository() + retryDuration, tick := integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) + assert.EventuallyWithT(t, func(t *assert.CollectT) { + _, err := idpRepo.Get(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.IDCondition(addOIDC.IdpId)))) + + require.NoError(t, err) + }, retryDuration, tick) // remove idp _, err = AdminClient.RemoveIDP(IAMCTX, &admin.RemoveIDPRequest{ @@ -313,13 +295,9 @@ func TestServer_TestIDProviderInstanceReduces(t *testing.T) { }) require.NoError(t, err) - retryDuration, tick := integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) + retryDuration, tick = integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - _, err := idpRepo.Get(IAMCTX, pool, - idpRepo.IDCondition(addOIDC.IdpId), - instanceID, - nil, - ) + _, err := idpRepo.Get(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.IDCondition(addOIDC.IdpId)))) // event iam.idp.config.remove require.ErrorIs(t, &database.NoRowFoundError{}, err) @@ -330,18 +308,14 @@ func TestServer_TestIDProviderInstanceReduces(t *testing.T) { name := gofakeit.Name() // add oidc - addOIDC, err := AdminClient.AddOIDCIDP(IAMCTX, defaultOIDCIDReq(name, false)) + addOIDC, err := AdminClient.AddOIDCIDP(IAMCTX, defaultOIDCIDPReq(name, false)) require.NoError(t, err) idpRepo := repository.IDProviderRepository() retryDuration, tick := integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - oidc, err := idpRepo.GetOIDC(IAMCTX, pool, - idpRepo.IDCondition(addOIDC.IdpId), - instanceID, - nil, - ) + oidc, err := idpRepo.GetOIDC(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.IDCondition(addOIDC.IdpId)))) require.NoError(t, err) // event org.idp.oidc.config.added @@ -356,8 +330,6 @@ func TestServer_TestIDProviderInstanceReduces(t *testing.T) { assert.Equal(t, "issuer", oidc.Issuer) assert.Equal(t, "clientID", oidc.ClientID) assert.Equal(t, []string{"scope"}, oidc.Scopes) - assert.Equal(t, int16(idp_grpc.IDPStylingType_STYLING_TYPE_GOOGLE), *oidc.StylingType) - assert.Equal(t, false, oidc.AutoRegister) assert.Equal(t, domain.OIDCMappingField(idp.OIDCMappingField_OIDC_MAPPING_FIELD_EMAIL), oidc.IDPDisplayNameMapping) assert.Equal(t, domain.OIDCMappingField(idp.OIDCMappingField_OIDC_MAPPING_FIELD_EMAIL), oidc.UserNameMapping) }, retryDuration, tick) @@ -367,16 +339,16 @@ func TestServer_TestIDProviderInstanceReduces(t *testing.T) { name := gofakeit.Name() // add oidc - addOIDC, err := AdminClient.AddOIDCIDP(IAMCTX, defaultOIDCIDReq(name, true)) + addOIDC, err := AdminClient.AddOIDCIDP(IAMCTX, defaultOIDCIDPReq(name, true)) require.NoError(t, err) idpRepo := repository.IDProviderRepository() - // check original values for OCID + // ensure oidc is added before updating var oidc *domain.IDPOIDC retryDuration, tick := integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - oidc, err = idpRepo.GetOIDC(IAMCTX, pool, idpRepo.IDCondition(addOIDC.IdpId), instanceID, nil) + oidc, err = idpRepo.GetOIDC(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.IDCondition(addOIDC.IdpId)))) require.NoError(t, err) assert.Equal(t, addOIDC.IdpId, oidc.ID) }, retryDuration, tick) @@ -396,14 +368,9 @@ func TestServer_TestIDProviderInstanceReduces(t *testing.T) { retryDuration, tick = integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - updateOIDC, err := idpRepo.GetOIDC(IAMCTX, pool, - idpRepo.IDCondition(addOIDC.IdpId), - instanceID, - nil, - ) + updateOIDC, err := idpRepo.GetOIDC(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.IDCondition(addOIDC.IdpId)))) require.NoError(t, err) - // event org.idp.oidc.config.changed // idp assert.Equal(t, instanceID, oidc.InstanceID) assert.Nil(t, oidc.OrgID) @@ -444,11 +411,7 @@ func TestServer_TestIDProviderInstanceReduces(t *testing.T) { retryDuration, tick := integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - jwt, err := idpRepo.GetJWT(IAMCTX, pool, - idpRepo.IDCondition(addJWT.IdpId), - instanceID, - nil, - ) + jwt, err := idpRepo.GetJWT(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.IDCondition(addJWT.IdpId)))) require.NoError(t, err) // event iam.idp.jwt.config.added @@ -458,7 +421,6 @@ func TestServer_TestIDProviderInstanceReduces(t *testing.T) { assert.Equal(t, name, jwt.Name) assert.Equal(t, addJWT.IdpId, jwt.ID) assert.Equal(t, domain.IDPTypeJWT, domain.IDPType(*jwt.Type)) - assert.Equal(t, int16(idp_grpc.IDPStylingType_STYLING_TYPE_GOOGLE), *jwt.StylingType) // jwt assert.Equal(t, "jwtEndpoint", jwt.JWTEndpoint) @@ -498,11 +460,7 @@ func TestServer_TestIDProviderInstanceReduces(t *testing.T) { retryDuration, tick := integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - updateJWT, err := idpRepo.GetJWT(IAMCTX, pool, - idpRepo.IDCondition(addJWT.IdpId), - instanceID, - nil, - ) + updateJWT, err := idpRepo.GetJWT(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.IDCondition(addJWT.IdpId)))) require.NoError(t, err) // event iam.idp.jwt.config.changed @@ -551,7 +509,7 @@ func TestServer_TestIDProviderInstanceReduces(t *testing.T) { var oauth *domain.IDPOAuth retryDuration, tick := integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - oauth, err = idpRepo.GetOAuth(IAMCTX, pool, idpRepo.IDCondition(addOAuth.Id), instanceID, nil) + oauth, err = idpRepo.GetOAuth(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.IDCondition(addOAuth.Id)))) require.NoError(t, err) // event instance.idp.oauth.added @@ -610,7 +568,7 @@ func TestServer_TestIDProviderInstanceReduces(t *testing.T) { var oauth *domain.IDPOAuth retryDuration, tick := integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - oauth, err = idpRepo.GetOAuth(IAMCTX, pool, idpRepo.IDCondition(addOAuth.Id), instanceID, nil) + oauth, err = idpRepo.GetOAuth(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.IDCondition(addOAuth.Id)))) require.NoError(t, err) assert.Equal(t, addOAuth.Id, oauth.ID) }, retryDuration, tick) @@ -641,11 +599,7 @@ func TestServer_TestIDProviderInstanceReduces(t *testing.T) { retryDuration, tick = integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - updateOauth, err := idpRepo.GetOAuth(IAMCTX, pool, - idpRepo.IDCondition(addOAuth.Id), - instanceID, - nil, - ) + updateOauth, err := idpRepo.GetOAuth(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.IDCondition(addOAuth.Id)))) require.NoError(t, err) // event instance.idp.oauth.changed @@ -658,7 +612,7 @@ func TestServer_TestIDProviderInstanceReduces(t *testing.T) { assert.Equal(t, true, updateOauth.AllowLinking) assert.Equal(t, true, updateOauth.AllowCreation) assert.Equal(t, true, updateOauth.AllowAutoUpdate) - assert.Equal(t, domain.IDPAutoLinkingFieldUserName, domain.IDPAutoLinkingField(*updateOauth.AutoLinkingField)) + assert.Equal(t, domain.IDPAutoLinkingFieldUsername, domain.IDPAutoLinkingField(*updateOauth.AutoLinkingField)) assert.Equal(t, true, updateOauth.UsePKCE) assert.WithinRange(t, updateOauth.UpdatedAt, before, after) @@ -701,7 +655,7 @@ func TestServer_TestIDProviderInstanceReduces(t *testing.T) { // check values for oidc retryDuration, tick := integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - oidc, err := idpRepo.GetOIDC(IAMCTX, pool, idpRepo.IDCondition(addOIDC.Id), instanceID, nil) + oidc, err := idpRepo.GetOIDC(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.IDCondition(addOIDC.Id)))) require.NoError(t, err) // event instance.idp.oidc added @@ -729,7 +683,7 @@ func TestServer_TestIDProviderInstanceReduces(t *testing.T) { }, retryDuration, tick) }) - t.Run("test instanceidp oidc changed reduces", func(t *testing.T) { + t.Run("test instance idp oidc changed reduces", func(t *testing.T) { name := gofakeit.Name() addOIDC, err := AdminClient.AddGenericOIDCProvider(IAMCTX, &admin.AddGenericOIDCProviderRequest{ @@ -756,7 +710,7 @@ func TestServer_TestIDProviderInstanceReduces(t *testing.T) { var oidc *domain.IDPOIDC retryDuration, tick := integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - oidc, err = idpRepo.GetOIDC(IAMCTX, pool, idpRepo.IDCondition(addOIDC.Id), instanceID, nil) + oidc, err = idpRepo.GetOIDC(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.IDCondition(addOIDC.Id)))) require.NoError(t, err) }, retryDuration, tick) @@ -784,11 +738,7 @@ func TestServer_TestIDProviderInstanceReduces(t *testing.T) { retryDuration, tick = integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - updateOIDC, err := idpRepo.GetOIDC(IAMCTX, pool, - idpRepo.IDCondition(addOIDC.Id), - instanceID, - nil, - ) + updateOIDC, err := idpRepo.GetOIDC(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.IDCondition(addOIDC.Id)))) require.NoError(t, err) // event instance.idp.oidc.changed @@ -801,7 +751,7 @@ func TestServer_TestIDProviderInstanceReduces(t *testing.T) { assert.Equal(t, true, updateOIDC.AllowLinking) assert.Equal(t, true, updateOIDC.AllowCreation) assert.Equal(t, true, updateOIDC.AllowAutoUpdate) - assert.Equal(t, domain.IDPAutoLinkingFieldUserName, domain.IDPAutoLinkingField(*updateOIDC.AutoLinkingField)) + assert.Equal(t, domain.IDPAutoLinkingFieldUsername, domain.IDPAutoLinkingField(*updateOIDC.AutoLinkingField)) assert.WithinRange(t, updateOIDC.UpdatedAt, before, after) // oidc @@ -841,7 +791,7 @@ func TestServer_TestIDProviderInstanceReduces(t *testing.T) { var oidc *domain.IDPOIDC retryDuration, tick := integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - oidc, err = idpRepo.GetOIDC(IAMCTX, pool, idpRepo.IDCondition(addOIDC.Id), instanceID, nil) + oidc, err = idpRepo.GetOIDC(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.IDCondition(addOIDC.Id)))) require.NoError(t, err) assert.Equal(t, domain.IDPTypeOIDC, domain.IDPType(*oidc.Type)) }, retryDuration, tick) @@ -876,7 +826,7 @@ func TestServer_TestIDProviderInstanceReduces(t *testing.T) { retryDuration, tick = integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - azure, err := idpRepo.GetAzureAD(IAMCTX, pool, idpRepo.IDCondition(addOIDC.Id), instanceID, nil) + azure, err := idpRepo.GetAzureAD(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.IDCondition(addOIDC.Id)))) require.NoError(t, err) // event instance.idp.oidc.migrated.azure @@ -890,7 +840,7 @@ func TestServer_TestIDProviderInstanceReduces(t *testing.T) { assert.Equal(t, true, azure.AllowLinking) assert.Equal(t, true, azure.AllowCreation) assert.Equal(t, true, azure.AllowAutoUpdate) - assert.Equal(t, domain.IDPAutoLinkingFieldUserName, domain.IDPAutoLinkingField(*azure.AutoLinkingField)) + assert.Equal(t, domain.IDPAutoLinkingFieldUsername, domain.IDPAutoLinkingField(*azure.AutoLinkingField)) assert.WithinRange(t, azure.UpdatedAt, before, after) // oidc @@ -929,7 +879,7 @@ func TestServer_TestIDProviderInstanceReduces(t *testing.T) { var oidc *domain.IDPOIDC retryDuration, tick := integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - oidc, err = idpRepo.GetOIDC(IAMCTX, pool, idpRepo.IDCondition(addOIDC.Id), instanceID, nil) + oidc, err = idpRepo.GetOIDC(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.IDCondition(addOIDC.Id)))) require.NoError(t, err) assert.Equal(t, domain.IDPTypeOIDC, domain.IDPType(*oidc.Type)) }, retryDuration, tick) @@ -958,7 +908,7 @@ func TestServer_TestIDProviderInstanceReduces(t *testing.T) { retryDuration, tick = integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - google, err := idpRepo.GetGoogle(IAMCTX, pool, idpRepo.IDCondition(addOIDC.Id), instanceID, nil) + google, err := idpRepo.GetGoogle(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.IDCondition(addOIDC.Id)))) require.NoError(t, err) // event instance.idp.oidc.migrated.google @@ -972,7 +922,7 @@ func TestServer_TestIDProviderInstanceReduces(t *testing.T) { assert.Equal(t, true, google.AllowLinking) assert.Equal(t, true, google.AllowCreation) assert.Equal(t, true, google.AllowAutoUpdate) - assert.Equal(t, domain.IDPAutoLinkingFieldUserName, domain.IDPAutoLinkingField(*google.AutoLinkingField)) + assert.Equal(t, domain.IDPAutoLinkingFieldUsername, domain.IDPAutoLinkingField(*google.AutoLinkingField)) assert.WithinRange(t, google.UpdatedAt, before, after) // oidc @@ -1009,7 +959,7 @@ func TestServer_TestIDProviderInstanceReduces(t *testing.T) { // check values for jwt retryDuration, tick := integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - jwt, err := idpRepo.GetJWT(IAMCTX, pool, idpRepo.IDCondition(addJWT.Id), instanceID, nil) + jwt, err := idpRepo.GetJWT(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.IDCondition(addJWT.Id)))) require.NoError(t, err) // event instance.idp.jwt.added @@ -1080,7 +1030,7 @@ func TestServer_TestIDProviderInstanceReduces(t *testing.T) { // check values for jwt retryDuration, tick := integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - jwt, err := idpRepo.GetJWT(IAMCTX, pool, idpRepo.IDCondition(addJWT.Id), instanceID, nil) + jwt, err := idpRepo.GetJWT(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.IDCondition(addJWT.Id)))) require.NoError(t, err) // event instance.idp.jwt.added @@ -1093,7 +1043,7 @@ func TestServer_TestIDProviderInstanceReduces(t *testing.T) { assert.Equal(t, true, jwt.AllowLinking) assert.Equal(t, true, jwt.AllowCreation) assert.Equal(t, true, jwt.AllowAutoUpdate) - assert.Equal(t, domain.IDPAutoLinkingFieldUserName, domain.IDPAutoLinkingField(*jwt.AutoLinkingField)) + assert.Equal(t, domain.IDPAutoLinkingFieldUsername, domain.IDPAutoLinkingField(*jwt.AutoLinkingField)) assert.WithinRange(t, jwt.UpdatedAt, before, after) // jwt @@ -1136,7 +1086,7 @@ func TestServer_TestIDProviderInstanceReduces(t *testing.T) { // check values for azure retryDuration, tick := integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - azure, err := idpRepo.GetAzureAD(IAMCTX, pool, idpRepo.IDCondition(addAzure.Id), instanceID, nil) + azure, err := idpRepo.GetAzureAD(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.IDCondition(addAzure.Id)))) require.NoError(t, err) // event instance.idp.azure.added @@ -1149,7 +1099,7 @@ func TestServer_TestIDProviderInstanceReduces(t *testing.T) { assert.Equal(t, true, azure.AllowLinking) assert.Equal(t, true, azure.AllowCreation) assert.Equal(t, true, azure.AllowAutoUpdate) - assert.Equal(t, domain.IDPAutoLinkingFieldUserName, domain.IDPAutoLinkingField(*azure.AutoLinkingField)) + assert.Equal(t, domain.IDPAutoLinkingFieldUsername, domain.IDPAutoLinkingField(*azure.AutoLinkingField)) assert.WithinRange(t, azure.UpdatedAt, before, after) // azure @@ -1191,7 +1141,7 @@ func TestServer_TestIDProviderInstanceReduces(t *testing.T) { var azure *domain.IDPAzureAD retryDuration, tick := integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - azure, err = idpRepo.GetAzureAD(IAMCTX, pool, idpRepo.IDCondition(addAzure.Id), instanceID, nil) + azure, err = idpRepo.GetAzureAD(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.IDCondition(addAzure.Id)))) require.NoError(t, err) assert.Equal(t, addAzure.Id, azure.ID) }, retryDuration, tick) @@ -1225,7 +1175,7 @@ func TestServer_TestIDProviderInstanceReduces(t *testing.T) { // check values for azure retryDuration, tick = integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - updateAzure, err := idpRepo.GetAzureAD(IAMCTX, pool, idpRepo.IDCondition(addAzure.Id), instanceID, nil) + updateAzure, err := idpRepo.GetAzureAD(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.IDCondition(addAzure.Id)))) require.NoError(t, err) // event instance.idp.azure.changed @@ -1276,7 +1226,7 @@ func TestServer_TestIDProviderInstanceReduces(t *testing.T) { // check values for github retryDuration, tick := integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - github, err := idpRepo.GetGithub(IAMCTX, pool, idpRepo.IDCondition(addGithub.Id), instanceID, nil) + github, err := idpRepo.GetGithub(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.IDCondition(addGithub.Id)))) require.NoError(t, err) // event instance.idp.github.added @@ -1289,7 +1239,7 @@ func TestServer_TestIDProviderInstanceReduces(t *testing.T) { assert.Equal(t, false, github.AllowLinking) assert.Equal(t, false, github.AllowCreation) assert.Equal(t, false, github.AllowAutoUpdate) - assert.Equal(t, domain.IDPAutoLinkingFieldUserName, domain.IDPAutoLinkingField(*github.AutoLinkingField)) + assert.Equal(t, domain.IDPAutoLinkingFieldUsername, domain.IDPAutoLinkingField(*github.AutoLinkingField)) assert.WithinRange(t, github.UpdatedAt, before, after) assert.Equal(t, "clientId", github.ClientID) @@ -1322,7 +1272,7 @@ func TestServer_TestIDProviderInstanceReduces(t *testing.T) { var github *domain.IDPGithub retryDuration, tick := integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - github, err = idpRepo.GetGithub(IAMCTX, pool, idpRepo.IDCondition(addGithub.Id), instanceID, nil) + github, err = idpRepo.GetGithub(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.IDCondition(addGithub.Id)))) require.NoError(t, err) assert.Equal(t, addGithub.Id, github.ID) }, retryDuration, tick) @@ -1350,7 +1300,7 @@ func TestServer_TestIDProviderInstanceReduces(t *testing.T) { // check values for azure retryDuration, tick = integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - updateGithub, err := idpRepo.GetGithub(IAMCTX, pool, idpRepo.IDCondition(addGithub.Id), instanceID, nil) + updateGithub, err := idpRepo.GetGithub(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.IDCondition(addGithub.Id)))) require.NoError(t, err) // event instance.idp.github.changed @@ -1363,7 +1313,7 @@ func TestServer_TestIDProviderInstanceReduces(t *testing.T) { assert.Equal(t, true, updateGithub.AllowLinking) assert.Equal(t, true, updateGithub.AllowCreation) assert.Equal(t, true, updateGithub.AllowAutoUpdate) - assert.Equal(t, domain.IDPAutoLinkingFieldUserName, domain.IDPAutoLinkingField(*updateGithub.AutoLinkingField)) + assert.Equal(t, domain.IDPAutoLinkingFieldUsername, domain.IDPAutoLinkingField(*updateGithub.AutoLinkingField)) assert.WithinRange(t, updateGithub.UpdatedAt, before, after) // github @@ -1402,7 +1352,7 @@ func TestServer_TestIDProviderInstanceReduces(t *testing.T) { // check values for github enterprise retryDuration, tick := integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - githubEnterprise, err := idpRepo.GetGithubEnterprise(IAMCTX, pool, idpRepo.IDCondition(addGithubEnterprise.Id), instanceID, nil) + githubEnterprise, err := idpRepo.GetGithubEnterprise(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.IDCondition(addGithubEnterprise.Id)))) require.NoError(t, err) // event instance.idp.github_enterprise.added @@ -1456,7 +1406,7 @@ func TestServer_TestIDProviderInstanceReduces(t *testing.T) { var githubEnterprise *domain.IDPGithubEnterprise retryDuration, tick := integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - githubEnterprise, err = idpRepo.GetGithubEnterprise(IAMCTX, pool, idpRepo.IDCondition(addGithubEnterprise.Id), instanceID, nil) + githubEnterprise, err = idpRepo.GetGithubEnterprise(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.IDCondition(addGithubEnterprise.Id)))) require.NoError(t, err) assert.Equal(t, addGithubEnterprise.Id, githubEnterprise.ID) }, retryDuration, tick) @@ -1487,7 +1437,7 @@ func TestServer_TestIDProviderInstanceReduces(t *testing.T) { // check values for azure retryDuration, tick = integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - updateGithubEnterprise, err := idpRepo.GetGithubEnterprise(IAMCTX, pool, idpRepo.IDCondition(addGithubEnterprise.Id), instanceID, nil) + updateGithubEnterprise, err := idpRepo.GetGithubEnterprise(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.IDCondition(addGithubEnterprise.Id)))) require.NoError(t, err) // event instance.idp.github_enterprise.changed @@ -1539,7 +1489,7 @@ func TestServer_TestIDProviderInstanceReduces(t *testing.T) { // check values for gitlab retryDuration, tick := integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - gitlab, err := idpRepo.GetGitlab(IAMCTX, pool, idpRepo.IDCondition(addGithub.Id), instanceID, nil) + gitlab, err := idpRepo.GetGitlab(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.IDCondition(addGithub.Id)))) require.NoError(t, err) // event instance.idp.gitlab.added @@ -1587,7 +1537,7 @@ func TestServer_TestIDProviderInstanceReduces(t *testing.T) { var githlab *domain.IDPGitlab retryDuration, tick := integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - githlab, err = idpRepo.GetGitlab(IAMCTX, pool, idpRepo.IDCondition(addGitlab.Id), instanceID, nil) + githlab, err = idpRepo.GetGitlab(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.IDCondition(addGitlab.Id)))) require.NoError(t, err) assert.Equal(t, addGitlab.Id, githlab.ID) }, retryDuration, tick) @@ -1615,7 +1565,7 @@ func TestServer_TestIDProviderInstanceReduces(t *testing.T) { // check values for gitlab retryDuration, tick = integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - updateGitlab, err := idpRepo.GetGitlab(IAMCTX, pool, idpRepo.IDCondition(addGitlab.Id), instanceID, nil) + updateGitlab, err := idpRepo.GetGitlab(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.IDCondition(addGitlab.Id)))) require.NoError(t, err) // event instance.idp.gitlab.changed @@ -1627,7 +1577,7 @@ func TestServer_TestIDProviderInstanceReduces(t *testing.T) { assert.Equal(t, true, updateGitlab.AllowLinking) assert.Equal(t, true, updateGitlab.AllowCreation) assert.Equal(t, true, updateGitlab.AllowAutoUpdate) - assert.Equal(t, domain.IDPAutoLinkingFieldUserName, domain.IDPAutoLinkingField(*updateGitlab.AutoLinkingField)) + assert.Equal(t, domain.IDPAutoLinkingFieldUsername, domain.IDPAutoLinkingField(*updateGitlab.AutoLinkingField)) assert.WithinRange(t, updateGitlab.UpdatedAt, before, after) // gitlab @@ -1665,7 +1615,7 @@ func TestServer_TestIDProviderInstanceReduces(t *testing.T) { // check values for gitlab self hosted retryDuration, tick := integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - gitlabSelfHosted, err := idpRepo.GetGitlabSelfHosting(IAMCTX, pool, idpRepo.IDCondition(addGitlabSelfHosted.Id), instanceID, nil) + gitlabSelfHosted, err := idpRepo.GetGitlabSelfHosted(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.IDCondition(addGitlabSelfHosted.Id)))) require.NoError(t, err) // event instance.idp.gitlab_self_hosted.added @@ -1712,10 +1662,10 @@ func TestServer_TestIDProviderInstanceReduces(t *testing.T) { idpRepo := repository.IDProviderRepository() - var githlabSelfHosted *domain.IDPGitlabSelfHosting + var githlabSelfHosted *domain.IDPGitlabSelfHosted retryDuration, tick := integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - githlabSelfHosted, err = idpRepo.GetGitlabSelfHosting(IAMCTX, pool, idpRepo.IDCondition(addGitlabSelfHosted.Id), instanceID, nil) + githlabSelfHosted, err = idpRepo.GetGitlabSelfHosted(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.IDCondition(addGitlabSelfHosted.Id)))) require.NoError(t, err) assert.Equal(t, addGitlabSelfHosted.Id, githlabSelfHosted.ID) }, retryDuration, tick) @@ -1744,7 +1694,7 @@ func TestServer_TestIDProviderInstanceReduces(t *testing.T) { // check values for gitlab self hosted retryDuration, tick = integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - updateGitlabSelfHosted, err := idpRepo.GetGitlabSelfHosting(IAMCTX, pool, idpRepo.IDCondition(addGitlabSelfHosted.Id), instanceID, nil) + updateGitlabSelfHosted, err := idpRepo.GetGitlabSelfHosted(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.IDCondition(addGitlabSelfHosted.Id)))) require.NoError(t, err) // event instance.idp.gitlab_self_hosted.changed @@ -1757,7 +1707,7 @@ func TestServer_TestIDProviderInstanceReduces(t *testing.T) { assert.Equal(t, true, updateGitlabSelfHosted.AllowLinking) assert.Equal(t, true, updateGitlabSelfHosted.AllowCreation) assert.Equal(t, true, updateGitlabSelfHosted.AllowAutoUpdate) - assert.Equal(t, domain.IDPAutoLinkingFieldUserName, domain.IDPAutoLinkingField(*updateGitlabSelfHosted.AutoLinkingField)) + assert.Equal(t, domain.IDPAutoLinkingFieldUsername, domain.IDPAutoLinkingField(*updateGitlabSelfHosted.AutoLinkingField)) assert.WithinRange(t, updateGitlabSelfHosted.UpdatedAt, before, after) // gitlab self hosted @@ -1794,7 +1744,7 @@ func TestServer_TestIDProviderInstanceReduces(t *testing.T) { // check values for google retryDuration, tick := integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - google, err := idpRepo.GetGoogle(IAMCTX, pool, idpRepo.IDCondition(addGoogle.Id), instanceID, nil) + google, err := idpRepo.GetGoogle(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.IDCondition(addGoogle.Id)))) require.NoError(t, err) // event instance.idp.google.added @@ -1842,7 +1792,7 @@ func TestServer_TestIDProviderInstanceReduces(t *testing.T) { var google *domain.IDPGoogle retryDuration, tick := integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - google, err = idpRepo.GetGoogle(IAMCTX, pool, idpRepo.IDCondition(addGoogle.Id), instanceID, nil) + google, err = idpRepo.GetGoogle(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.IDCondition(addGoogle.Id)))) require.NoError(t, err) assert.Equal(t, addGoogle.Id, google.ID) }, retryDuration, tick) @@ -1870,7 +1820,7 @@ func TestServer_TestIDProviderInstanceReduces(t *testing.T) { // check values for google retryDuration, tick = integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - updateGoogle, err := idpRepo.GetGoogle(IAMCTX, pool, idpRepo.IDCondition(addGoogle.Id), instanceID, nil) + updateGoogle, err := idpRepo.GetGoogle(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.IDCondition(addGoogle.Id)))) require.NoError(t, err) // event instance.idp.google.changed @@ -1883,7 +1833,7 @@ func TestServer_TestIDProviderInstanceReduces(t *testing.T) { assert.Equal(t, true, updateGoogle.AllowLinking) assert.Equal(t, true, updateGoogle.AllowCreation) assert.Equal(t, true, updateGoogle.AllowAutoUpdate) - assert.Equal(t, domain.IDPAutoLinkingFieldUserName, domain.IDPAutoLinkingField(*updateGoogle.AutoLinkingField)) + assert.Equal(t, domain.IDPAutoLinkingFieldUsername, domain.IDPAutoLinkingField(*updateGoogle.AutoLinkingField)) assert.WithinRange(t, updateGoogle.UpdatedAt, before, after) // google @@ -1906,7 +1856,7 @@ func TestServer_TestIDProviderInstanceReduces(t *testing.T) { retryDuration, tick := integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - ldap, err := idpRepo.GetLDAP(IAMCTX, pool, idpRepo.IDCondition(addLdap.Id), instanceID, nil) + ldap, err := idpRepo.GetLDAP(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.IDCondition(addLdap.Id)))) require.NoError(t, err) // event instance.idp.ldap.v2.added @@ -1961,7 +1911,7 @@ func TestServer_TestIDProviderInstanceReduces(t *testing.T) { var ldap *domain.IDPLDAP retryDuration, tick := integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - ldap, err = idpRepo.GetLDAP(IAMCTX, pool, idpRepo.IDCondition(addLdap.Id), instanceID, nil) + ldap, err = idpRepo.GetLDAP(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.IDCondition(addLdap.Id)))) require.NoError(t, err) assert.Equal(t, addLdap.Id, ldap.ID) }, retryDuration, tick) @@ -2010,7 +1960,7 @@ func TestServer_TestIDProviderInstanceReduces(t *testing.T) { // check values for ldap retryDuration, tick = integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - updateLdap, err := idpRepo.GetLDAP(IAMCTX, pool, idpRepo.IDCondition(addLdap.Id), instanceID, nil) + updateLdap, err := idpRepo.GetLDAP(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.IDCondition(addLdap.Id)))) require.NoError(t, err) // event instance.idp.ldap.v2.changed @@ -2023,7 +1973,7 @@ func TestServer_TestIDProviderInstanceReduces(t *testing.T) { assert.Equal(t, true, updateLdap.AllowLinking) assert.Equal(t, true, updateLdap.AllowCreation) assert.Equal(t, true, updateLdap.AllowAutoUpdate) - assert.Equal(t, domain.IDPAutoLinkingFieldUserName, domain.IDPAutoLinkingField(*updateLdap.AutoLinkingField)) + assert.Equal(t, domain.IDPAutoLinkingFieldUsername, domain.IDPAutoLinkingField(*updateLdap.AutoLinkingField)) assert.WithinRange(t, updateLdap.UpdatedAt, before, after) // ldap @@ -2079,7 +2029,7 @@ func TestServer_TestIDProviderInstanceReduces(t *testing.T) { retryDuration, tick := integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - apple, err := idpRepo.GetApple(IAMCTX, pool, idpRepo.IDCondition(addApple.Id), instanceID, nil) + apple, err := idpRepo.GetApple(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.IDCondition(addApple.Id)))) require.NoError(t, err) // event instance.idp.apple.added @@ -2131,7 +2081,7 @@ func TestServer_TestIDProviderInstanceReduces(t *testing.T) { var apple *domain.IDPApple retryDuration, tick := integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - apple, err = idpRepo.GetApple(IAMCTX, pool, idpRepo.IDCondition(addApple.Id), instanceID, nil) + apple, err = idpRepo.GetApple(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.IDCondition(addApple.Id)))) require.NoError(t, err) assert.Equal(t, addApple.Id, apple.ID) }, retryDuration, tick) @@ -2161,7 +2111,7 @@ func TestServer_TestIDProviderInstanceReduces(t *testing.T) { // check values for apple retryDuration, tick = integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - updateApple, err := idpRepo.GetApple(IAMCTX, pool, idpRepo.IDCondition(addApple.Id), instanceID, nil) + updateApple, err := idpRepo.GetApple(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.IDCondition(addApple.Id)))) require.NoError(t, err) // event nstance.idp.apple.changed @@ -2174,7 +2124,7 @@ func TestServer_TestIDProviderInstanceReduces(t *testing.T) { assert.Equal(t, true, updateApple.AllowLinking) assert.Equal(t, true, updateApple.AllowCreation) assert.Equal(t, true, updateApple.AllowAutoUpdate) - assert.Equal(t, domain.IDPAutoLinkingFieldUserName, domain.IDPAutoLinkingField(*updateApple.AutoLinkingField)) + assert.Equal(t, domain.IDPAutoLinkingFieldUsername, domain.IDPAutoLinkingField(*updateApple.AutoLinkingField)) assert.WithinRange(t, updateApple.UpdatedAt, before, after) // apple @@ -2200,7 +2150,7 @@ func TestServer_TestIDProviderInstanceReduces(t *testing.T) { retryDuration, tick := integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - saml, err := idpRepo.GetSAML(IAMCTX, pool, idpRepo.IDCondition(addSAML.Id), instanceID, nil) + saml, err := idpRepo.GetSAML(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.IDCondition(addSAML.Id)))) require.NoError(t, err) // event instance.idp.saml.added @@ -2243,7 +2193,7 @@ func TestServer_TestIDProviderInstanceReduces(t *testing.T) { var saml *domain.IDPSAML retryDuration, tick := integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - saml, err = idpRepo.GetSAML(IAMCTX, pool, idpRepo.IDCondition(addSAML.Id), instanceID, nil) + saml, err = idpRepo.GetSAML(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.IDCondition(addSAML.Id)))) require.NoError(t, err) assert.Equal(t, addSAML.Id, saml.ID) }, retryDuration, tick) @@ -2278,7 +2228,7 @@ func TestServer_TestIDProviderInstanceReduces(t *testing.T) { // check values for apple retryDuration, tick = integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - updateSAML, err := idpRepo.GetSAML(IAMCTX, pool, idpRepo.IDCondition(addSAML.Id), instanceID, nil) + updateSAML, err := idpRepo.GetSAML(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.IDCondition(addSAML.Id)))) require.NoError(t, err) // event instance.idp.saml.changed @@ -2291,7 +2241,7 @@ func TestServer_TestIDProviderInstanceReduces(t *testing.T) { assert.Equal(t, true, updateSAML.AllowLinking) assert.Equal(t, true, updateSAML.AllowCreation) assert.Equal(t, true, updateSAML.AllowAutoUpdate) - assert.Equal(t, domain.IDPAutoLinkingFieldUserName, domain.IDPAutoLinkingField(*updateSAML.AutoLinkingField)) + assert.Equal(t, domain.IDPAutoLinkingFieldUsername, domain.IDPAutoLinkingField(*updateSAML.AutoLinkingField)) assert.WithinRange(t, updateSAML.UpdatedAt, before, after) // saml @@ -2312,7 +2262,7 @@ func TestServer_TestIDProviderInstanceReduces(t *testing.T) { name := gofakeit.Name() // add idp - addOIDC, err := AdminClient.AddOIDCIDP(IAMCTX, defaultOIDCIDReq(name, true)) + addOIDC, err := AdminClient.AddOIDCIDP(IAMCTX, defaultOIDCIDPReq(name, true)) require.NoError(t, err) idpRepo := repository.IDProviderRepository() @@ -2320,11 +2270,7 @@ func TestServer_TestIDProviderInstanceReduces(t *testing.T) { // check idp exists retryDuration, tick := integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - _, err := idpRepo.Get(IAMCTX, pool, - idpRepo.IDCondition(addOIDC.IdpId), - instanceID, - nil, - ) + _, err := idpRepo.Get(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.IDCondition(addOIDC.IdpId)))) require.NoError(t, err) }, retryDuration, tick) @@ -2337,11 +2283,7 @@ func TestServer_TestIDProviderInstanceReduces(t *testing.T) { // check idp is removed retryDuration, tick = integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - _, err := idpRepo.Get(IAMCTX, pool, - idpRepo.IDCondition(addOIDC.IdpId), - instanceID, - nil, - ) + _, err := idpRepo.Get(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.IDCondition(addOIDC.IdpId)))) require.ErrorIs(t, &database.NoRowFoundError{}, err) }, retryDuration, tick) }) @@ -2369,7 +2311,7 @@ func defaultInstanceSAMLRequest(name string, federatedLogoutEnable *bool) *admin } } -func defaultOIDCIDReq(name string, autoRegister bool) *admin.AddOIDCIDPRequest { +func defaultOIDCIDPReq(name string, autoRegister bool) *admin.AddOIDCIDPRequest { return &admin.AddOIDCIDPRequest{ Name: name, StylingType: idp_grpc.IDPStylingType_STYLING_TYPE_GOOGLE, diff --git a/backend/v3/storage/database/events_testing/id_provider_org_test.go b/backend/v3/storage/database/events_testing/id_provider_org_test.go index 34ed2a203..cc12a131b 100644 --- a/backend/v3/storage/database/events_testing/id_provider_org_test.go +++ b/backend/v3/storage/database/events_testing/id_provider_org_test.go @@ -25,7 +25,7 @@ func TestServer_TestIDProviderOrgReduces(t *testing.T) { instanceID := Instance.ID() orgID := Instance.DefaultOrg.Id - t.Run("test instance idp add reduces", func(t *testing.T) { + t.Run("test org idp add reduces", func(t *testing.T) { name := gofakeit.Name() before := time.Now() @@ -47,11 +47,7 @@ func TestServer_TestIDProviderOrgReduces(t *testing.T) { retryDuration, tick := integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - idp, err := idpRepo.Get(IAMCTX, pool, - idpRepo.NameCondition(name), - instanceID, - &orgID, - ) + idp, err := idpRepo.Get(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.OrgIDCondition(&orgID), idpRepo.NameCondition(database.TextOperationEqual, name)))) require.NoError(t, err) // event org.idp.config.added @@ -60,18 +56,16 @@ func TestServer_TestIDProviderOrgReduces(t *testing.T) { assert.Equal(t, addOIDC.IdpId, idp.ID) assert.Equal(t, domain.IDPStateActive, idp.State) assert.Equal(t, name, idp.Name) - assert.Equal(t, true, idp.AutoRegister) assert.Equal(t, true, idp.AllowCreation) assert.Equal(t, false, idp.AllowAutoUpdate) assert.Equal(t, true, idp.AllowLinking) assert.Nil(t, idp.AutoLinkingField) - assert.Equal(t, int16(idp_grpc.IDPStylingType_STYLING_TYPE_GOOGLE), *idp.StylingType) assert.WithinRange(t, idp.UpdatedAt, before, after) assert.WithinRange(t, idp.CreatedAt, before, after) }, retryDuration, tick) }) - // t.Run("test instance idp update reduces", func(t *testing.T) { + // t.Run("test org idp update reduces", func(t *testing.T) { // name := gofakeit.Name() // addOIDC, err := MgmtClient.AddOrgOIDCIDP(IAMCTX, &management.AddOrgOIDCIDPRequest{ @@ -119,7 +113,7 @@ func TestServer_TestIDProviderOrgReduces(t *testing.T) { // }, retryDuration, tick) // }) - t.Run("test instance idp deactivate reduces", func(t *testing.T) { + t.Run("test org idp deactivate reduces", func(t *testing.T) { name := gofakeit.Name() addOIDC, err := MgmtClient.AddOrgOIDCIDP(IAMCTX, &management.AddOrgOIDCIDPRequest{ @@ -147,11 +141,7 @@ func TestServer_TestIDProviderOrgReduces(t *testing.T) { retryDuration, tick := integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - idp, err := idpRepo.Get(IAMCTX, pool, - idpRepo.IDCondition(addOIDC.IdpId), - instanceID, - &orgID, - ) + idp, err := idpRepo.Get(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.OrgIDCondition(&orgID), idpRepo.IDCondition(addOIDC.IdpId)))) require.NoError(t, err) // event org.idp.config.deactivated @@ -161,7 +151,7 @@ func TestServer_TestIDProviderOrgReduces(t *testing.T) { }, retryDuration, tick) }) - t.Run("test instance idp reactivate reduces", func(t *testing.T) { + t.Run("test org idp reactivate reduces", func(t *testing.T) { name := gofakeit.Name() addOIDC, err := MgmtClient.AddOrgOIDCIDP(IAMCTX, &management.AddOrgOIDCIDPRequest{ @@ -187,11 +177,7 @@ func TestServer_TestIDProviderOrgReduces(t *testing.T) { // wait for idp to be deactivated retryDuration, tick := integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - idp, err := idpRepo.Get(IAMCTX, pool, - idpRepo.IDCondition(addOIDC.IdpId), - instanceID, - &orgID, - ) + idp, err := idpRepo.Get(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.OrgIDCondition(&orgID), idpRepo.IDCondition(addOIDC.IdpId)))) require.NoError(t, err) assert.Equal(t, addOIDC.IdpId, idp.ID) @@ -208,11 +194,7 @@ func TestServer_TestIDProviderOrgReduces(t *testing.T) { retryDuration, tick = integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - idp, err := idpRepo.Get(IAMCTX, pool, - idpRepo.IDCondition(addOIDC.IdpId), - instanceID, - &orgID, - ) + idp, err := idpRepo.Get(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.OrgIDCondition(&orgID), idpRepo.IDCondition(addOIDC.IdpId)))) require.NoError(t, err) // event org.idp.config.reactivated @@ -222,7 +204,7 @@ func TestServer_TestIDProviderOrgReduces(t *testing.T) { }, retryDuration, tick) }) - t.Run("test instance idp remove reduces", func(t *testing.T) { + t.Run("test org idp remove reduces", func(t *testing.T) { name := gofakeit.Name() // add idp @@ -241,26 +223,31 @@ func TestServer_TestIDProviderOrgReduces(t *testing.T) { idpRepo := repository.IDProviderRepository() + // wait until idp is added + retryDuration, tick := integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) + assert.EventuallyWithT(t, func(t *assert.CollectT) { + _, err := idpRepo.Get(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.OrgIDCondition(&orgID), idpRepo.IDCondition(addOIDC.IdpId)))) + + // event org.idp.config.remove + require.NoError(t, err) + }, retryDuration, tick) + // remove idp _, err = MgmtClient.RemoveOrgIDP(IAMCTX, &management.RemoveOrgIDPRequest{ IdpId: addOIDC.IdpId, }) require.NoError(t, err) - retryDuration, tick := integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) + retryDuration, tick = integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - _, err := idpRepo.Get(IAMCTX, pool, - idpRepo.IDCondition(addOIDC.IdpId), - instanceID, - &orgID, - ) + _, err := idpRepo.Get(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.OrgIDCondition(&orgID), idpRepo.IDCondition(addOIDC.IdpId)))) // event org.idp.config.remove require.ErrorIs(t, &database.NoRowFoundError{}, err) }, retryDuration, tick) }) - t.Run("test instance idp oidc added reduces", func(t *testing.T) { + t.Run("test org idp oidc added reduces", func(t *testing.T) { name := gofakeit.Name() // add oidc @@ -281,11 +268,7 @@ func TestServer_TestIDProviderOrgReduces(t *testing.T) { retryDuration, tick := integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - oidc, err := idpRepo.GetOIDC(IAMCTX, pool, - idpRepo.IDCondition(addOIDC.IdpId), - instanceID, - &orgID, - ) + oidc, err := idpRepo.GetOIDC(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.OrgIDCondition(&orgID), idpRepo.IDCondition(addOIDC.IdpId)))) require.NoError(t, err) // event org.idp.oidc.config.added @@ -300,14 +283,12 @@ func TestServer_TestIDProviderOrgReduces(t *testing.T) { assert.Equal(t, "issuer", oidc.Issuer) assert.Equal(t, "clientID", oidc.ClientID) assert.Equal(t, []string{"scope"}, oidc.Scopes) - assert.Equal(t, int16(idp_grpc.IDPStylingType_STYLING_TYPE_GOOGLE), *oidc.StylingType) - assert.Equal(t, false, oidc.AutoRegister) assert.Equal(t, domain.OIDCMappingField(idp.OIDCMappingField_OIDC_MAPPING_FIELD_EMAIL), oidc.IDPDisplayNameMapping) assert.Equal(t, domain.OIDCMappingField(idp.OIDCMappingField_OIDC_MAPPING_FIELD_EMAIL), oidc.UserNameMapping) }, retryDuration, tick) }) - t.Run("test instance idp oidc changed reduces", func(t *testing.T) { + t.Run("test org idp oidc changed reduces", func(t *testing.T) { name := gofakeit.Name() // add oidc @@ -330,7 +311,7 @@ func TestServer_TestIDProviderOrgReduces(t *testing.T) { var oidc *domain.IDPOIDC retryDuration, tick := integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - oidc, err = idpRepo.GetOIDC(IAMCTX, pool, idpRepo.IDCondition(addOIDC.IdpId), instanceID, &orgID) + oidc, err = idpRepo.GetOIDC(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.OrgIDCondition(&orgID), idpRepo.IDCondition(addOIDC.IdpId)))) require.NoError(t, err) assert.Equal(t, addOIDC.IdpId, oidc.ID) }, retryDuration, tick) @@ -350,11 +331,7 @@ func TestServer_TestIDProviderOrgReduces(t *testing.T) { retryDuration, tick = integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - updateOIDC, err := idpRepo.GetOIDC(IAMCTX, pool, - idpRepo.IDCondition(addOIDC.IdpId), - instanceID, - &orgID, - ) + updateOIDC, err := idpRepo.GetOIDC(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.OrgIDCondition(&orgID), idpRepo.IDCondition(addOIDC.IdpId)))) require.NoError(t, err) // event org.idp.oidc.config.changed @@ -379,7 +356,7 @@ func TestServer_TestIDProviderOrgReduces(t *testing.T) { }, retryDuration, tick) }) - t.Run("test instance idp jwt added reduces", func(t *testing.T) { + t.Run("test org idp jwt added reduces", func(t *testing.T) { name := gofakeit.Name() // add jwt @@ -398,11 +375,7 @@ func TestServer_TestIDProviderOrgReduces(t *testing.T) { retryDuration, tick := integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - jwt, err := idpRepo.GetJWT(IAMCTX, pool, - idpRepo.IDCondition(addJWT.IdpId), - instanceID, - &orgID, - ) + jwt, err := idpRepo.GetJWT(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.OrgIDCondition(&orgID), idpRepo.IDCondition(addJWT.IdpId)))) require.NoError(t, err) // event org.idp.jwt.config.added @@ -412,7 +385,6 @@ func TestServer_TestIDProviderOrgReduces(t *testing.T) { assert.Equal(t, name, jwt.Name) assert.Equal(t, addJWT.IdpId, jwt.ID) assert.Equal(t, domain.IDPTypeJWT, domain.IDPType(*jwt.Type)) - assert.Equal(t, int16(idp_grpc.IDPStylingType_STYLING_TYPE_GOOGLE), *jwt.StylingType) // jwt assert.Equal(t, "jwtEndpoint", jwt.JWTEndpoint) @@ -422,7 +394,7 @@ func TestServer_TestIDProviderOrgReduces(t *testing.T) { }, retryDuration, tick) }) - t.Run("test instance idp jwt changed reduces", func(t *testing.T) { + t.Run("test org idp jwt changed reduces", func(t *testing.T) { name := gofakeit.Name() // add jwt @@ -452,11 +424,7 @@ func TestServer_TestIDProviderOrgReduces(t *testing.T) { retryDuration, tick := integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - updateJWT, err := idpRepo.GetJWT(IAMCTX, pool, - idpRepo.IDCondition(addJWT.IdpId), - instanceID, - &orgID, - ) + updateJWT, err := idpRepo.GetJWT(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.OrgIDCondition(&orgID), idpRepo.IDCondition(addJWT.IdpId)))) require.NoError(t, err) // event org.idp.jwt.config.changed @@ -506,7 +474,7 @@ func TestServer_TestIDProviderOrgReduces(t *testing.T) { var oauth *domain.IDPOAuth retryDuration, tick := integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - oauth, err = idpRepo.GetOAuth(IAMCTX, pool, idpRepo.IDCondition(addOAuth.Id), instanceID, &orgID) + oauth, err = idpRepo.GetOAuth(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.OrgIDCondition(&orgID), idpRepo.IDCondition(addOAuth.Id)))) require.NoError(t, err) // event org.idp.oauth.added @@ -519,7 +487,9 @@ func TestServer_TestIDProviderOrgReduces(t *testing.T) { assert.Equal(t, false, oauth.AllowLinking) assert.Equal(t, false, oauth.AllowCreation) assert.Equal(t, false, oauth.AllowAutoUpdate) - assert.Equal(t, domain.IDPAutoLinkingFieldEmail, domain.IDPAutoLinkingField(*oauth.AutoLinkingField)) + if assert.NotNil(t, oauth.AutoLinkingField) { + assert.Equal(t, domain.IDPAutoLinkingFieldEmail, domain.IDPAutoLinkingField(*oauth.AutoLinkingField)) + } assert.WithinRange(t, oauth.CreatedAt, before, after) assert.WithinRange(t, oauth.UpdatedAt, before, after) @@ -565,7 +535,7 @@ func TestServer_TestIDProviderOrgReduces(t *testing.T) { var oauth *domain.IDPOAuth retryDuration, tick := integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - oauth, err = idpRepo.GetOAuth(IAMCTX, pool, idpRepo.IDCondition(addOAuth.Id), instanceID, &orgID) + oauth, err = idpRepo.GetOAuth(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.OrgIDCondition(&orgID), idpRepo.IDCondition(addOAuth.Id)))) require.NoError(t, err) assert.Equal(t, addOAuth.Id, oauth.ID) }, retryDuration, tick) @@ -596,11 +566,7 @@ func TestServer_TestIDProviderOrgReduces(t *testing.T) { retryDuration, tick = integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - updateOauth, err := idpRepo.GetOAuth(IAMCTX, pool, - idpRepo.IDCondition(addOAuth.Id), - instanceID, - &orgID, - ) + updateOauth, err := idpRepo.GetOAuth(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.OrgIDCondition(&orgID), idpRepo.IDCondition(addOAuth.Id)))) require.NoError(t, err) // event org.idp.oauth.changed @@ -613,7 +579,7 @@ func TestServer_TestIDProviderOrgReduces(t *testing.T) { assert.Equal(t, true, updateOauth.AllowLinking) assert.Equal(t, true, updateOauth.AllowCreation) assert.Equal(t, true, updateOauth.AllowAutoUpdate) - assert.Equal(t, domain.IDPAutoLinkingFieldUserName, domain.IDPAutoLinkingField(*updateOauth.AutoLinkingField)) + assert.Equal(t, domain.IDPAutoLinkingFieldUsername, domain.IDPAutoLinkingField(*updateOauth.AutoLinkingField)) assert.Equal(t, true, updateOauth.UsePKCE) assert.WithinRange(t, updateOauth.UpdatedAt, before, after) @@ -656,7 +622,7 @@ func TestServer_TestIDProviderOrgReduces(t *testing.T) { // check values for oidc retryDuration, tick := integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - oidc, err := idpRepo.GetOIDC(IAMCTX, pool, idpRepo.IDCondition(addOIDC.Id), instanceID, &orgID) + oidc, err := idpRepo.GetOIDC(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.OrgIDCondition(&orgID), idpRepo.IDCondition(addOIDC.Id)))) require.NoError(t, err) // event org.idp.oidc added @@ -711,7 +677,7 @@ func TestServer_TestIDProviderOrgReduces(t *testing.T) { var oidc *domain.IDPOIDC retryDuration, tick := integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - oidc, err = idpRepo.GetOIDC(IAMCTX, pool, idpRepo.IDCondition(addOIDC.Id), instanceID, &orgID) + oidc, err = idpRepo.GetOIDC(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.OrgIDCondition(&orgID), idpRepo.IDCondition(addOIDC.Id)))) require.NoError(t, err) }, retryDuration, tick) @@ -739,11 +705,7 @@ func TestServer_TestIDProviderOrgReduces(t *testing.T) { retryDuration, tick = integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - updateOIDC, err := idpRepo.GetOIDC(IAMCTX, pool, - idpRepo.IDCondition(addOIDC.Id), - instanceID, - &orgID, - ) + updateOIDC, err := idpRepo.GetOIDC(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.OrgIDCondition(&orgID), idpRepo.IDCondition(addOIDC.Id)))) require.NoError(t, err) // event org.idp.oidc.changed @@ -756,7 +718,7 @@ func TestServer_TestIDProviderOrgReduces(t *testing.T) { assert.Equal(t, true, updateOIDC.AllowLinking) assert.Equal(t, true, updateOIDC.AllowCreation) assert.Equal(t, true, updateOIDC.AllowAutoUpdate) - assert.Equal(t, domain.IDPAutoLinkingFieldUserName, domain.IDPAutoLinkingField(*updateOIDC.AutoLinkingField)) + assert.Equal(t, domain.IDPAutoLinkingFieldUsername, domain.IDPAutoLinkingField(*updateOIDC.AutoLinkingField)) assert.WithinRange(t, updateOIDC.UpdatedAt, before, after) // oidc @@ -795,7 +757,7 @@ func TestServer_TestIDProviderOrgReduces(t *testing.T) { var oidc *domain.IDPOIDC retryDuration, tick := integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - oidc, err = idpRepo.GetOIDC(IAMCTX, pool, idpRepo.IDCondition(addOIDC.Id), instanceID, &orgID) + oidc, err = idpRepo.GetOIDC(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.OrgIDCondition(&orgID), idpRepo.IDCondition(addOIDC.Id)))) require.NoError(t, err) assert.Equal(t, domain.IDPTypeOIDC, domain.IDPType(*oidc.Type)) }, retryDuration, tick) @@ -830,7 +792,7 @@ func TestServer_TestIDProviderOrgReduces(t *testing.T) { retryDuration, tick = integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - azure, err := idpRepo.GetAzureAD(IAMCTX, pool, idpRepo.IDCondition(addOIDC.Id), instanceID, &orgID) + azure, err := idpRepo.GetAzureAD(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.OrgIDCondition(&orgID), idpRepo.IDCondition(addOIDC.Id)))) require.NoError(t, err) // event org.idp.oidc.migrated.azure @@ -844,7 +806,7 @@ func TestServer_TestIDProviderOrgReduces(t *testing.T) { assert.Equal(t, true, azure.AllowLinking) assert.Equal(t, true, azure.AllowCreation) assert.Equal(t, true, azure.AllowAutoUpdate) - assert.Equal(t, domain.IDPAutoLinkingFieldUserName, domain.IDPAutoLinkingField(*azure.AutoLinkingField)) + assert.Equal(t, domain.IDPAutoLinkingFieldUsername, domain.IDPAutoLinkingField(*azure.AutoLinkingField)) assert.WithinRange(t, azure.UpdatedAt, before, after) // oidc @@ -883,7 +845,7 @@ func TestServer_TestIDProviderOrgReduces(t *testing.T) { var oidc *domain.IDPOIDC retryDuration, tick := integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - oidc, err = idpRepo.GetOIDC(IAMCTX, pool, idpRepo.IDCondition(addOIDC.Id), instanceID, &orgID) + oidc, err = idpRepo.GetOIDC(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.OrgIDCondition(&orgID), idpRepo.IDCondition(addOIDC.Id)))) require.NoError(t, err) assert.Equal(t, domain.IDPTypeOIDC, domain.IDPType(*oidc.Type)) }, retryDuration, tick) @@ -912,7 +874,7 @@ func TestServer_TestIDProviderOrgReduces(t *testing.T) { retryDuration, tick = integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - google, err := idpRepo.GetGoogle(IAMCTX, pool, idpRepo.IDCondition(addOIDC.Id), instanceID, &orgID) + google, err := idpRepo.GetGoogle(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.OrgIDCondition(&orgID), idpRepo.IDCondition(addOIDC.Id)))) require.NoError(t, err) // event org.idp.oidc.migrated.google @@ -926,7 +888,7 @@ func TestServer_TestIDProviderOrgReduces(t *testing.T) { assert.Equal(t, true, google.AllowLinking) assert.Equal(t, true, google.AllowCreation) assert.Equal(t, true, google.AllowAutoUpdate) - assert.Equal(t, domain.IDPAutoLinkingFieldUserName, domain.IDPAutoLinkingField(*google.AutoLinkingField)) + assert.Equal(t, domain.IDPAutoLinkingFieldUsername, domain.IDPAutoLinkingField(*google.AutoLinkingField)) assert.WithinRange(t, google.UpdatedAt, before, after) // oidc @@ -963,7 +925,7 @@ func TestServer_TestIDProviderOrgReduces(t *testing.T) { // check values for jwt retryDuration, tick := integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - jwt, err := idpRepo.GetJWT(IAMCTX, pool, idpRepo.IDCondition(addJWT.Id), instanceID, &orgID) + jwt, err := idpRepo.GetJWT(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.OrgIDCondition(&orgID), idpRepo.IDCondition(addJWT.Id)))) require.NoError(t, err) // event org.idp.jwt.added @@ -1034,7 +996,7 @@ func TestServer_TestIDProviderOrgReduces(t *testing.T) { // check values for jwt retryDuration, tick := integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - updateJwt, err := idpRepo.GetJWT(IAMCTX, pool, idpRepo.IDCondition(addJWT.Id), instanceID, &orgID) + updateJwt, err := idpRepo.GetJWT(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.OrgIDCondition(&orgID), idpRepo.IDCondition(addJWT.Id)))) require.NoError(t, err) // event org.idp.jwt.added @@ -1047,7 +1009,7 @@ func TestServer_TestIDProviderOrgReduces(t *testing.T) { assert.Equal(t, true, updateJwt.AllowLinking) assert.Equal(t, true, updateJwt.AllowCreation) assert.Equal(t, true, updateJwt.AllowAutoUpdate) - assert.Equal(t, domain.IDPAutoLinkingFieldUserName, domain.IDPAutoLinkingField(*updateJwt.AutoLinkingField)) + assert.Equal(t, domain.IDPAutoLinkingFieldUsername, domain.IDPAutoLinkingField(*updateJwt.AutoLinkingField)) assert.WithinRange(t, updateJwt.UpdatedAt, before, after) // jwt @@ -1090,7 +1052,7 @@ func TestServer_TestIDProviderOrgReduces(t *testing.T) { // check values for azure retryDuration, tick := integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - azure, err := idpRepo.GetAzureAD(IAMCTX, pool, idpRepo.IDCondition(addAzure.Id), instanceID, &orgID) + azure, err := idpRepo.GetAzureAD(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.OrgIDCondition(&orgID), idpRepo.IDCondition(addAzure.Id)))) require.NoError(t, err) // event org.idp.azure.added @@ -1103,7 +1065,7 @@ func TestServer_TestIDProviderOrgReduces(t *testing.T) { assert.Equal(t, true, azure.AllowLinking) assert.Equal(t, true, azure.AllowCreation) assert.Equal(t, true, azure.AllowAutoUpdate) - assert.Equal(t, domain.IDPAutoLinkingFieldUserName, domain.IDPAutoLinkingField(*azure.AutoLinkingField)) + assert.Equal(t, domain.IDPAutoLinkingFieldUsername, domain.IDPAutoLinkingField(*azure.AutoLinkingField)) assert.WithinRange(t, azure.UpdatedAt, before, after) // azure @@ -1145,7 +1107,7 @@ func TestServer_TestIDProviderOrgReduces(t *testing.T) { var azure *domain.IDPAzureAD retryDuration, tick := integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - azure, err = idpRepo.GetAzureAD(IAMCTX, pool, idpRepo.IDCondition(addAzure.Id), instanceID, &orgID) + azure, err = idpRepo.GetAzureAD(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.OrgIDCondition(&orgID), idpRepo.IDCondition(addAzure.Id)))) require.NoError(t, err) assert.Equal(t, addAzure.Id, azure.ID) }, retryDuration, tick) @@ -1179,7 +1141,7 @@ func TestServer_TestIDProviderOrgReduces(t *testing.T) { // check values for azure retryDuration, tick = integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - updateAzure, err := idpRepo.GetAzureAD(IAMCTX, pool, idpRepo.IDCondition(addAzure.Id), instanceID, &orgID) + updateAzure, err := idpRepo.GetAzureAD(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.OrgIDCondition(&orgID), idpRepo.IDCondition(addAzure.Id)))) require.NoError(t, err) // event org.idp.azure.changed @@ -1230,7 +1192,7 @@ func TestServer_TestIDProviderOrgReduces(t *testing.T) { // check values for github retryDuration, tick := integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - github, err := idpRepo.GetGithub(IAMCTX, pool, idpRepo.IDCondition(addGithub.Id), instanceID, &orgID) + github, err := idpRepo.GetGithub(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.OrgIDCondition(&orgID), idpRepo.IDCondition(addGithub.Id)))) require.NoError(t, err) // event org.idp.github.added @@ -1243,7 +1205,7 @@ func TestServer_TestIDProviderOrgReduces(t *testing.T) { assert.Equal(t, false, github.AllowLinking) assert.Equal(t, false, github.AllowCreation) assert.Equal(t, false, github.AllowAutoUpdate) - assert.Equal(t, domain.IDPAutoLinkingFieldUserName, domain.IDPAutoLinkingField(*github.AutoLinkingField)) + assert.Equal(t, domain.IDPAutoLinkingFieldUsername, domain.IDPAutoLinkingField(*github.AutoLinkingField)) assert.WithinRange(t, github.UpdatedAt, before, after) assert.Equal(t, "clientId", github.ClientID) @@ -1276,7 +1238,7 @@ func TestServer_TestIDProviderOrgReduces(t *testing.T) { var github *domain.IDPGithub retryDuration, tick := integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - github, err = idpRepo.GetGithub(IAMCTX, pool, idpRepo.IDCondition(addGithub.Id), instanceID, &orgID) + github, err = idpRepo.GetGithub(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.OrgIDCondition(&orgID), idpRepo.IDCondition(addGithub.Id)))) require.NoError(t, err) assert.Equal(t, addGithub.Id, github.ID) }, retryDuration, tick) @@ -1304,7 +1266,7 @@ func TestServer_TestIDProviderOrgReduces(t *testing.T) { // check values for azure retryDuration, tick = integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - updateGithub, err := idpRepo.GetGithub(IAMCTX, pool, idpRepo.IDCondition(addGithub.Id), instanceID, &orgID) + updateGithub, err := idpRepo.GetGithub(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.OrgIDCondition(&orgID), idpRepo.IDCondition(addGithub.Id)))) require.NoError(t, err) // event org.idp.github.changed @@ -1317,7 +1279,7 @@ func TestServer_TestIDProviderOrgReduces(t *testing.T) { assert.Equal(t, true, updateGithub.AllowLinking) assert.Equal(t, true, updateGithub.AllowCreation) assert.Equal(t, true, updateGithub.AllowAutoUpdate) - assert.Equal(t, domain.IDPAutoLinkingFieldUserName, domain.IDPAutoLinkingField(*updateGithub.AutoLinkingField)) + assert.Equal(t, domain.IDPAutoLinkingFieldUsername, domain.IDPAutoLinkingField(*updateGithub.AutoLinkingField)) assert.WithinRange(t, updateGithub.UpdatedAt, before, after) // github @@ -1356,7 +1318,7 @@ func TestServer_TestIDProviderOrgReduces(t *testing.T) { // check values for github enterprise retryDuration, tick := integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - githubEnterprise, err := idpRepo.GetGithubEnterprise(IAMCTX, pool, idpRepo.IDCondition(addGithubEnterprise.Id), instanceID, &orgID) + githubEnterprise, err := idpRepo.GetGithubEnterprise(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.OrgIDCondition(&orgID), idpRepo.IDCondition(addGithubEnterprise.Id)))) require.NoError(t, err) // event org.idp.github_enterprise.added @@ -1410,7 +1372,7 @@ func TestServer_TestIDProviderOrgReduces(t *testing.T) { var githubEnterprise *domain.IDPGithubEnterprise retryDuration, tick := integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - githubEnterprise, err = idpRepo.GetGithubEnterprise(IAMCTX, pool, idpRepo.IDCondition(addGithubEnterprise.Id), instanceID, &orgID) + githubEnterprise, err = idpRepo.GetGithubEnterprise(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.OrgIDCondition(&orgID), idpRepo.IDCondition(addGithubEnterprise.Id)))) require.NoError(t, err) assert.Equal(t, addGithubEnterprise.Id, githubEnterprise.ID) }, retryDuration, tick) @@ -1441,7 +1403,7 @@ func TestServer_TestIDProviderOrgReduces(t *testing.T) { // check values for azure retryDuration, tick = integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - updateGithubEnterprise, err := idpRepo.GetGithubEnterprise(IAMCTX, pool, idpRepo.IDCondition(addGithubEnterprise.Id), instanceID, &orgID) + updateGithubEnterprise, err := idpRepo.GetGithubEnterprise(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.OrgIDCondition(&orgID), idpRepo.IDCondition(addGithubEnterprise.Id)))) require.NoError(t, err) // event org.idp.github_enterprise.changed @@ -1493,7 +1455,7 @@ func TestServer_TestIDProviderOrgReduces(t *testing.T) { // check values for gitlab retryDuration, tick := integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - gitlab, err := idpRepo.GetGitlab(IAMCTX, pool, idpRepo.IDCondition(addGithub.Id), instanceID, &orgID) + gitlab, err := idpRepo.GetGitlab(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.OrgIDCondition(&orgID), idpRepo.IDCondition(addGithub.Id)))) require.NoError(t, err) // event org.idp.gitlab.added @@ -1541,7 +1503,7 @@ func TestServer_TestIDProviderOrgReduces(t *testing.T) { var gitlab *domain.IDPGitlab retryDuration, tick := integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - gitlab, err = idpRepo.GetGitlab(IAMCTX, pool, idpRepo.IDCondition(addGitlab.Id), instanceID, &orgID) + gitlab, err = idpRepo.GetGitlab(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.OrgIDCondition(&orgID), idpRepo.IDCondition(addGitlab.Id)))) require.NoError(t, err) assert.Equal(t, addGitlab.Id, gitlab.ID) }, retryDuration, tick) @@ -1569,7 +1531,7 @@ func TestServer_TestIDProviderOrgReduces(t *testing.T) { // check values for gitlab retryDuration, tick = integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - updateGitlab, err := idpRepo.GetGitlab(IAMCTX, pool, idpRepo.IDCondition(addGitlab.Id), instanceID, &orgID) + updateGitlab, err := idpRepo.GetGitlab(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.OrgIDCondition(&orgID), idpRepo.IDCondition(addGitlab.Id)))) require.NoError(t, err) // event org.idp.gitlab.changed @@ -1581,7 +1543,7 @@ func TestServer_TestIDProviderOrgReduces(t *testing.T) { assert.Equal(t, true, updateGitlab.AllowLinking) assert.Equal(t, true, updateGitlab.AllowCreation) assert.Equal(t, true, updateGitlab.AllowAutoUpdate) - assert.Equal(t, domain.IDPAutoLinkingFieldUserName, domain.IDPAutoLinkingField(*updateGitlab.AutoLinkingField)) + assert.Equal(t, domain.IDPAutoLinkingFieldUsername, domain.IDPAutoLinkingField(*updateGitlab.AutoLinkingField)) assert.WithinRange(t, updateGitlab.UpdatedAt, before, after) // gitlab @@ -1619,7 +1581,7 @@ func TestServer_TestIDProviderOrgReduces(t *testing.T) { // check values for gitlab self hosted retryDuration, tick := integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - gitlabSelfHosted, err := idpRepo.GetGitlabSelfHosting(IAMCTX, pool, idpRepo.IDCondition(addGitlabSelfHosted.Id), instanceID, &orgID) + gitlabSelfHosted, err := idpRepo.GetGitlabSelfHosted(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.OrgIDCondition(&orgID), idpRepo.IDCondition(addGitlabSelfHosted.Id)))) require.NoError(t, err) // event org.idp.gitlab_self_hosted.added @@ -1666,10 +1628,10 @@ func TestServer_TestIDProviderOrgReduces(t *testing.T) { idpRepo := repository.IDProviderRepository() - var githlabSelfHosted *domain.IDPGitlabSelfHosting + var githlabSelfHosted *domain.IDPGitlabSelfHosted retryDuration, tick := integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - githlabSelfHosted, err = idpRepo.GetGitlabSelfHosting(IAMCTX, pool, idpRepo.IDCondition(addGitlabSelfHosted.Id), instanceID, &orgID) + githlabSelfHosted, err = idpRepo.GetGitlabSelfHosted(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.OrgIDCondition(&orgID), idpRepo.IDCondition(addGitlabSelfHosted.Id)))) require.NoError(t, err) assert.Equal(t, addGitlabSelfHosted.Id, githlabSelfHosted.ID) }, retryDuration, tick) @@ -1698,7 +1660,7 @@ func TestServer_TestIDProviderOrgReduces(t *testing.T) { // check values for gitlab self hosted retryDuration, tick = integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - updateGitlabSelfHosted, err := idpRepo.GetGitlabSelfHosting(IAMCTX, pool, idpRepo.IDCondition(addGitlabSelfHosted.Id), instanceID, &orgID) + updateGitlabSelfHosted, err := idpRepo.GetGitlabSelfHosted(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.OrgIDCondition(&orgID), idpRepo.IDCondition(addGitlabSelfHosted.Id)))) require.NoError(t, err) // event org.idp.gitlab_self_hosted.changed @@ -1711,7 +1673,7 @@ func TestServer_TestIDProviderOrgReduces(t *testing.T) { assert.Equal(t, true, updateGitlabSelfHosted.AllowLinking) assert.Equal(t, true, updateGitlabSelfHosted.AllowCreation) assert.Equal(t, true, updateGitlabSelfHosted.AllowAutoUpdate) - assert.Equal(t, domain.IDPAutoLinkingFieldUserName, domain.IDPAutoLinkingField(*updateGitlabSelfHosted.AutoLinkingField)) + assert.Equal(t, domain.IDPAutoLinkingFieldUsername, domain.IDPAutoLinkingField(*updateGitlabSelfHosted.AutoLinkingField)) assert.WithinRange(t, updateGitlabSelfHosted.UpdatedAt, before, after) // gitlab self hosted @@ -1748,7 +1710,7 @@ func TestServer_TestIDProviderOrgReduces(t *testing.T) { // check values for google retryDuration, tick := integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - google, err := idpRepo.GetGoogle(IAMCTX, pool, idpRepo.IDCondition(addGoogle.Id), instanceID, &orgID) + google, err := idpRepo.GetGoogle(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.OrgIDCondition(&orgID), idpRepo.IDCondition(addGoogle.Id)))) require.NoError(t, err) // event org.idp.google.added @@ -1796,7 +1758,7 @@ func TestServer_TestIDProviderOrgReduces(t *testing.T) { var google *domain.IDPGoogle retryDuration, tick := integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - google, err = idpRepo.GetGoogle(IAMCTX, pool, idpRepo.IDCondition(addGoogle.Id), instanceID, &orgID) + google, err = idpRepo.GetGoogle(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.OrgIDCondition(&orgID), idpRepo.IDCondition(addGoogle.Id)))) require.NoError(t, err) assert.Equal(t, addGoogle.Id, google.ID) }, retryDuration, tick) @@ -1824,7 +1786,7 @@ func TestServer_TestIDProviderOrgReduces(t *testing.T) { // check values for google retryDuration, tick = integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - updateGoogle, err := idpRepo.GetGoogle(IAMCTX, pool, idpRepo.IDCondition(addGoogle.Id), instanceID, &orgID) + updateGoogle, err := idpRepo.GetGoogle(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.OrgIDCondition(&orgID), idpRepo.IDCondition(addGoogle.Id)))) require.NoError(t, err) // event org.idp.google.changed @@ -1837,7 +1799,7 @@ func TestServer_TestIDProviderOrgReduces(t *testing.T) { assert.Equal(t, true, updateGoogle.AllowLinking) assert.Equal(t, true, updateGoogle.AllowCreation) assert.Equal(t, true, updateGoogle.AllowAutoUpdate) - assert.Equal(t, domain.IDPAutoLinkingFieldUserName, domain.IDPAutoLinkingField(*updateGoogle.AutoLinkingField)) + assert.Equal(t, domain.IDPAutoLinkingFieldUsername, domain.IDPAutoLinkingField(*updateGoogle.AutoLinkingField)) assert.WithinRange(t, updateGoogle.UpdatedAt, before, after) // google @@ -1860,7 +1822,7 @@ func TestServer_TestIDProviderOrgReduces(t *testing.T) { retryDuration, tick := integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - ldap, err := idpRepo.GetLDAP(IAMCTX, pool, idpRepo.IDCondition(addLdap.Id), instanceID, &orgID) + ldap, err := idpRepo.GetLDAP(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.OrgIDCondition(&orgID), idpRepo.IDCondition(addLdap.Id)))) require.NoError(t, err) // event org.idp.ldap.v2.added @@ -1915,7 +1877,7 @@ func TestServer_TestIDProviderOrgReduces(t *testing.T) { var ldap *domain.IDPLDAP retryDuration, tick := integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - ldap, err = idpRepo.GetLDAP(IAMCTX, pool, idpRepo.IDCondition(addLdap.Id), instanceID, &orgID) + ldap, err = idpRepo.GetLDAP(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.OrgIDCondition(&orgID), idpRepo.IDCondition(addLdap.Id)))) require.NoError(t, err) assert.Equal(t, addLdap.Id, ldap.ID) }, retryDuration, tick) @@ -1964,7 +1926,7 @@ func TestServer_TestIDProviderOrgReduces(t *testing.T) { // check values for ldap retryDuration, tick = integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - updateLdap, err := idpRepo.GetLDAP(IAMCTX, pool, idpRepo.IDCondition(addLdap.Id), instanceID, &orgID) + updateLdap, err := idpRepo.GetLDAP(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.OrgIDCondition(&orgID), idpRepo.IDCondition(addLdap.Id)))) require.NoError(t, err) // event org.idp.ldap.v2.changed @@ -1977,7 +1939,7 @@ func TestServer_TestIDProviderOrgReduces(t *testing.T) { assert.Equal(t, true, updateLdap.AllowLinking) assert.Equal(t, true, updateLdap.AllowCreation) assert.Equal(t, true, updateLdap.AllowAutoUpdate) - assert.Equal(t, domain.IDPAutoLinkingFieldUserName, domain.IDPAutoLinkingField(*updateLdap.AutoLinkingField)) + assert.Equal(t, domain.IDPAutoLinkingFieldUsername, domain.IDPAutoLinkingField(*updateLdap.AutoLinkingField)) assert.WithinRange(t, updateLdap.UpdatedAt, before, after) // ldap @@ -2033,7 +1995,7 @@ func TestServer_TestIDProviderOrgReduces(t *testing.T) { retryDuration, tick := integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - apple, err := idpRepo.GetApple(IAMCTX, pool, idpRepo.IDCondition(addApple.Id), instanceID, &orgID) + apple, err := idpRepo.GetApple(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.OrgIDCondition(&orgID), idpRepo.IDCondition(addApple.Id)))) require.NoError(t, err) // event org.idp.apple.added @@ -2085,7 +2047,7 @@ func TestServer_TestIDProviderOrgReduces(t *testing.T) { var apple *domain.IDPApple retryDuration, tick := integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - apple, err = idpRepo.GetApple(IAMCTX, pool, idpRepo.IDCondition(addApple.Id), instanceID, &orgID) + apple, err = idpRepo.GetApple(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.OrgIDCondition(&orgID), idpRepo.IDCondition(addApple.Id)))) require.NoError(t, err) assert.Equal(t, addApple.Id, apple.ID) }, retryDuration, tick) @@ -2115,7 +2077,7 @@ func TestServer_TestIDProviderOrgReduces(t *testing.T) { // check values for apple retryDuration, tick = integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - updateApple, err := idpRepo.GetApple(IAMCTX, pool, idpRepo.IDCondition(addApple.Id), instanceID, &orgID) + updateApple, err := idpRepo.GetApple(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.OrgIDCondition(&orgID), idpRepo.IDCondition(addApple.Id)))) require.NoError(t, err) // event nstance.idp.apple.changed @@ -2128,7 +2090,7 @@ func TestServer_TestIDProviderOrgReduces(t *testing.T) { assert.Equal(t, true, updateApple.AllowLinking) assert.Equal(t, true, updateApple.AllowCreation) assert.Equal(t, true, updateApple.AllowAutoUpdate) - assert.Equal(t, domain.IDPAutoLinkingFieldUserName, domain.IDPAutoLinkingField(*updateApple.AutoLinkingField)) + assert.Equal(t, domain.IDPAutoLinkingFieldUsername, domain.IDPAutoLinkingField(*updateApple.AutoLinkingField)) assert.WithinRange(t, updateApple.UpdatedAt, before, after) // apple @@ -2172,7 +2134,7 @@ func TestServer_TestIDProviderOrgReduces(t *testing.T) { retryDuration, tick := integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - saml, err := idpRepo.GetSAML(IAMCTX, pool, idpRepo.IDCondition(addSAML.Id), instanceID, &orgID) + saml, err := idpRepo.GetSAML(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.OrgIDCondition(&orgID), idpRepo.IDCondition(addSAML.Id)))) require.NoError(t, err) // event org.idp.saml.added @@ -2233,7 +2195,7 @@ func TestServer_TestIDProviderOrgReduces(t *testing.T) { var saml *domain.IDPSAML retryDuration, tick := integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - saml, err = idpRepo.GetSAML(IAMCTX, pool, idpRepo.IDCondition(addSAML.Id), instanceID, &orgID) + saml, err = idpRepo.GetSAML(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.OrgIDCondition(&orgID), idpRepo.IDCondition(addSAML.Id)))) require.NoError(t, err) assert.Equal(t, addSAML.Id, saml.ID) }, retryDuration, tick) @@ -2268,7 +2230,7 @@ func TestServer_TestIDProviderOrgReduces(t *testing.T) { // check values for apple retryDuration, tick = integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - updateSAML, err := idpRepo.GetSAML(IAMCTX, pool, idpRepo.IDCondition(addSAML.Id), instanceID, &orgID) + updateSAML, err := idpRepo.GetSAML(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.OrgIDCondition(&orgID), idpRepo.IDCondition(addSAML.Id)))) require.NoError(t, err) // event org.idp.saml.changed @@ -2281,7 +2243,7 @@ func TestServer_TestIDProviderOrgReduces(t *testing.T) { assert.Equal(t, true, updateSAML.AllowLinking) assert.Equal(t, true, updateSAML.AllowCreation) assert.Equal(t, true, updateSAML.AllowAutoUpdate) - assert.Equal(t, domain.IDPAutoLinkingFieldUserName, domain.IDPAutoLinkingField(*updateSAML.AutoLinkingField)) + assert.Equal(t, domain.IDPAutoLinkingFieldUsername, domain.IDPAutoLinkingField(*updateSAML.AutoLinkingField)) assert.WithinRange(t, updateSAML.UpdatedAt, before, after) // saml @@ -2320,11 +2282,7 @@ func TestServer_TestIDProviderOrgReduces(t *testing.T) { // check idp exists retryDuration, tick := integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - _, err := idpRepo.Get(IAMCTX, pool, - idpRepo.IDCondition(addOIDC.IdpId), - instanceID, - &orgID, - ) + _, err := idpRepo.Get(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.OrgIDCondition(&orgID), idpRepo.IDCondition(addOIDC.IdpId)))) require.NoError(t, err) }, retryDuration, tick) @@ -2337,11 +2295,7 @@ func TestServer_TestIDProviderOrgReduces(t *testing.T) { // check idp is removed retryDuration, tick = integration.WaitForAndTickWithMaxDuration(IAMCTX, time.Minute) assert.EventuallyWithT(t, func(t *assert.CollectT) { - _, err := idpRepo.Get(IAMCTX, pool, - idpRepo.IDCondition(addOIDC.IdpId), - instanceID, - &orgID, - ) + _, err := idpRepo.Get(IAMCTX, pool, database.WithCondition(database.And(idpRepo.InstanceIDCondition(instanceID), idpRepo.OrgIDCondition(&orgID), idpRepo.IDCondition(addOIDC.IdpId)))) require.ErrorIs(t, &database.NoRowFoundError{}, err) }, retryDuration, tick) }) diff --git a/backend/v3/storage/database/repository/id_provider.go b/backend/v3/storage/database/repository/id_provider.go index 930246b6f..4b2a7b97a 100644 --- a/backend/v3/storage/database/repository/id_provider.go +++ b/backend/v3/storage/database/repository/id_provider.go @@ -17,49 +17,54 @@ func IDProviderRepository() domain.IDProviderRepository { return new(idProvider) } -const queryIDProviderStmt = `SELECT instance_id, org_id, id, state, name, type, auto_register, allow_creation, allow_auto_creation,` + - ` allow_auto_update, allow_linking, auto_linking_field, styling_type, payload, created_at, updated_at` + +func (idProvider) qualifiedTableName() string { + return "zitadel.identity_providers" +} + +const queryIDProviderStmt = `SELECT instance_id, org_id, id, state, name, type, allow_creation, allow_auto_creation,` + + ` allow_auto_update, allow_linking, auto_linking_field, payload, created_at, updated_at` + ` FROM zitadel.identity_providers` -func (i *idProvider) Get(ctx context.Context, client database.QueryExecutor, id domain.IDPIdentifierCondition, instanceID string, orgID *string) (*domain.IdentityProvider, error) { - builder := database.StatementBuilder{} - - builder.WriteString(queryIDProviderStmt) - - conditions := []database.Condition{id, i.InstanceIDCondition(instanceID), i.OrgIDCondition(orgID)} - - writeCondition(&builder, database.And(conditions...)) - - return scanIDProvider(ctx, client, &builder) -} - -func (i *idProvider) List(ctx context.Context, client database.QueryExecutor, conditions ...database.Condition) ([]*domain.IdentityProvider, error) { - builder := database.StatementBuilder{} - - builder.WriteString(queryIDProviderStmt) - - if conditions != nil { - writeCondition(&builder, database.And(conditions...)) +func (i *idProvider) Get(ctx context.Context, client database.QueryExecutor, opts ...database.QueryOption) (*domain.IdentityProvider, error) { + options := new(database.QueryOpts) + for _, opt := range opts { + opt(options) + } + if err := checkRestrictingColumns(options.Condition, i.InstanceIDColumn()); err != nil { + return nil, err } - orderBy := database.OrderBy(i.CreatedAtColumn()) - orderBy.Write(&builder) + builder := database.NewStatementBuilder(queryIDProviderStmt) + options.Write(builder) - return scanIDProviders(ctx, client, &builder) + return scanIDProvider(ctx, client, builder) } -const createIDProviderStmtStart = `INSERT INTO zitadel.identity_providers` + - ` (instance_id, org_id, id, state, name, type, allow_creation, allow_auto_creation,` + - ` allow_auto_update, allow_linking, styling_type, payload) VALUES (` +func (i *idProvider) List(ctx context.Context, client database.QueryExecutor, opts ...database.QueryOption) ([]*domain.IdentityProvider, error) { + options := new(database.QueryOpts) + for _, opt := range opts { + opt(options) + } -const createIDProviderStmtEnd = `) RETURNING created_at, updated_at` + if err := checkRestrictingColumns(options.Condition, i.InstanceIDColumn()); err != nil { + return nil, err + } + + builder := database.NewStatementBuilder(queryIDProviderStmt) + options.Write(builder) + + return scanIDProviders(ctx, client, builder) +} + +const ( + createIDProviderStmtStart = `INSERT INTO zitadel.identity_providers` + + ` (instance_id, org_id, id, state, name, type, allow_creation, allow_auto_creation,` + + ` allow_auto_update, allow_linking, auto_linking_field, payload, created_at, updated_at) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, ` + createIDProviderStmtEnd = `) RETURNING created_at, updated_at` +) func (i *idProvider) Create(ctx context.Context, client database.QueryExecutor, idp *domain.IdentityProvider) error { - builder := database.StatementBuilder{} - - builder.WriteString(createIDProviderStmtStart) - - builder.WriteArgs( + builder := database.NewStatementBuilder(createIDProviderStmtStart, idp.InstanceID, idp.OrgID, idp.ID, @@ -70,364 +75,227 @@ func (i *idProvider) Create(ctx context.Context, client database.QueryExecutor, idp.AllowAutoCreation, idp.AllowAutoUpdate, idp.AllowLinking, - idp.StylingType, - string(idp.Payload)) - + idp.AutoLinkingField, + idp.Payload, + ) + var createdAt any = database.NowInstruction + if !idp.CreatedAt.IsZero() { + createdAt = idp.CreatedAt + } + builder.WriteArgs(createdAt, createdAt) builder.WriteString(createIDProviderStmtEnd) - err := client.QueryRow(ctx, builder.String(), builder.Args()...).Scan(&idp.CreatedAt, &idp.UpdatedAt) - return err + return client.QueryRow(ctx, builder.String(), builder.Args()...).Scan(&idp.CreatedAt, &idp.UpdatedAt) } -func (i *idProvider) Update(ctx context.Context, client database.QueryExecutor, id domain.IDPIdentifierCondition, instanceID string, orgID *string, changes ...database.Change) (int64, error) { +func (i *idProvider) Update(ctx context.Context, client database.QueryExecutor, condition database.Condition, changes ...database.Change) (int64, error) { if changes == nil { return 0, database.ErrNoChanges } - changes = append(changes, i.SetUpdatedAt(nil)) - builder := database.StatementBuilder{} - builder.WriteString(`UPDATE zitadel.identity_providers SET `) - - conditions := []database.Condition{ - id, - i.InstanceIDCondition(instanceID), - i.OrgIDCondition(orgID), + if err := checkRestrictingColumns(condition, i.InstanceIDColumn(), i.IDColumn()); err != nil { + return 0, err } - err := database.Changes(changes).Write(&builder) + dbChanges := database.Changes(changes) + if !dbChanges.IsOnColumn(i.UpdatedAtColumn()) { + dbChanges = append(dbChanges, i.SetUpdatedAt(nil)) + } + + builder := database.NewStatementBuilder(`UPDATE zitadel.identity_providers SET `) + err := dbChanges.Write(builder) if err != nil { return 0, err } - writeCondition(&builder, database.And(conditions...)) - - stmt := builder.String() - - return client.Exec(ctx, stmt, builder.Args()...) -} - -func (i *idProvider) Delete(ctx context.Context, client database.QueryExecutor, id domain.IDPIdentifierCondition, instanceID string, orgID *string) (int64, error) { - builder := database.StatementBuilder{} - - builder.WriteString(`DELETE FROM zitadel.identity_providers`) - - conditions := []database.Condition{ - id, - i.InstanceIDCondition(instanceID), - i.OrgIDCondition(orgID), - } - writeCondition(&builder, database.And(conditions...)) + writeCondition(builder, condition) return client.Exec(ctx, builder.String(), builder.Args()...) } -func (i *idProvider) GetOIDC(ctx context.Context, client database.QueryExecutor, id domain.IDPIdentifierCondition, instanceID string, orgID *string) (*domain.IDPOIDC, error) { - idpOIDC := &domain.IDPOIDC{} - var err error - - idpOIDC.IdentityProvider, err = i.Get(ctx, client, id, instanceID, orgID) - if err != nil { - return nil, err +func (i *idProvider) Delete(ctx context.Context, client database.QueryExecutor, condition database.Condition) (int64, error) { + if err := checkRestrictingColumns(condition, i.InstanceIDColumn(), i.IDColumn()); err != nil { + return 0, err } - var idpType domain.IDPType - if idpOIDC.Type != nil { - idpType = *idpOIDC.Type - } + builder := database.NewStatementBuilder(`DELETE FROM `) + builder.WriteString(i.qualifiedTableName()) + writeCondition(builder, condition) - if idpType != domain.IDPTypeOIDC { - return nil, domain.NewIDPWrongTypeError(domain.IDPTypeOIDC, idpType) - } - - err = json.Unmarshal(idpOIDC.Payload, idpOIDC) - if err != nil { - return nil, err - } - - return idpOIDC, nil + return client.Exec(ctx, builder.String(), builder.Args()...) } -func (i *idProvider) GetJWT(ctx context.Context, client database.QueryExecutor, id domain.IDPIdentifierCondition, instanceID string, orgID *string) (*domain.IDPJWT, error) { - idpJWT := &domain.IDPJWT{} - var err error - - idpJWT.IdentityProvider, err = i.Get(ctx, client, id, instanceID, orgID) +func (i *idProvider) GetOIDC(ctx context.Context, client database.QueryExecutor, opts ...database.QueryOption) (*domain.IDPOIDC, error) { + idp, typ, err := getIDP[domain.OIDC](ctx, client, i, domain.IDPTypeOIDC, opts...) if err != nil { return nil, err } - var idpType domain.IDPType - if idpJWT.Type != nil { - idpType = *idpJWT.Type - } - - if idpType != domain.IDPTypeJWT { - return nil, domain.NewIDPWrongTypeError(domain.IDPTypeJWT, idpType) - } - - err = json.Unmarshal(idpJWT.Payload, idpJWT) - if err != nil { - return nil, err - } - - return idpJWT, nil + return &domain.IDPOIDC{ + IdentityProvider: idp, + OIDC: typ, + }, nil } -func (i *idProvider) GetOAuth(ctx context.Context, client database.QueryExecutor, id domain.IDPIdentifierCondition, instanceID string, orgID *string) (*domain.IDPOAuth, error) { - idpOAuth := &domain.IDPOAuth{} - var err error - - idpOAuth.IdentityProvider, err = i.Get(ctx, client, id, instanceID, orgID) +func (i *idProvider) GetJWT(ctx context.Context, client database.QueryExecutor, opts ...database.QueryOption) (*domain.IDPJWT, error) { + idp, typ, err := getIDP[domain.JWT](ctx, client, i, domain.IDPTypeJWT, opts...) if err != nil { return nil, err } - var idpType domain.IDPType - if idpOAuth.Type != nil { - idpType = *idpOAuth.Type - } - - if idpType != domain.IDPTypeOAuth { - return nil, domain.NewIDPWrongTypeError(domain.IDPTypeOAuth, idpType) - } - - err = json.Unmarshal(idpOAuth.Payload, idpOAuth) - if err != nil { - return nil, err - } - - return idpOAuth, nil + return &domain.IDPJWT{ + IdentityProvider: idp, + JWT: typ, + }, nil } -func (i *idProvider) GetAzureAD(ctx context.Context, client database.QueryExecutor, id domain.IDPIdentifierCondition, instanceID string, orgID *string) (*domain.IDPAzureAD, error) { - idpAzure := &domain.IDPAzureAD{} - var err error - - idpAzure.IdentityProvider, err = i.Get(ctx, client, id, instanceID, orgID) +func (i *idProvider) GetOAuth(ctx context.Context, client database.QueryExecutor, opts ...database.QueryOption) (*domain.IDPOAuth, error) { + idp, typ, err := getIDP[domain.OAuth](ctx, client, i, domain.IDPTypeOAuth, opts...) if err != nil { return nil, err } - var idpType domain.IDPType - if idpAzure.Type != nil { - idpType = *idpAzure.Type - } - - if idpType != domain.IDPTypeAzure { - return nil, domain.NewIDPWrongTypeError(domain.IDPTypeAzure, idpType) - } - - err = json.Unmarshal(idpAzure.Payload, idpAzure) - if err != nil { - return nil, err - } - - return idpAzure, nil + return &domain.IDPOAuth{ + IdentityProvider: idp, + OAuth: typ, + }, nil } -func (i *idProvider) GetGoogle(ctx context.Context, client database.QueryExecutor, id domain.IDPIdentifierCondition, instanceID string, orgID *string) (*domain.IDPGoogle, error) { - idpGoogle := &domain.IDPGoogle{} - var err error - - idpGoogle.IdentityProvider, err = i.Get(ctx, client, id, instanceID, orgID) +func (i *idProvider) GetAzureAD(ctx context.Context, client database.QueryExecutor, opts ...database.QueryOption) (*domain.IDPAzureAD, error) { + idp, typ, err := getIDP[domain.Azure](ctx, client, i, domain.IDPTypeAzure, opts...) if err != nil { return nil, err } - var idpType domain.IDPType - if idpGoogle.Type != nil { - idpType = *idpGoogle.Type - } - - if idpType != domain.IDPTypeGoogle { - return nil, domain.NewIDPWrongTypeError(domain.IDPTypeGoogle, idpType) - } - - err = json.Unmarshal(idpGoogle.Payload, idpGoogle) - if err != nil { - return nil, err - } - - return idpGoogle, nil + return &domain.IDPAzureAD{ + IdentityProvider: idp, + Azure: typ, + }, nil } -func (i *idProvider) GetGithub(ctx context.Context, client database.QueryExecutor, id domain.IDPIdentifierCondition, instanceID string, orgID *string) (*domain.IDPGithub, error) { - idpGithub := &domain.IDPGithub{} - var err error - - idpGithub.IdentityProvider, err = i.Get(ctx, client, id, instanceID, orgID) +func (i *idProvider) GetGoogle(ctx context.Context, client database.QueryExecutor, opts ...database.QueryOption) (*domain.IDPGoogle, error) { + idp, typ, err := getIDP[domain.Google](ctx, client, i, domain.IDPTypeGoogle, opts...) if err != nil { return nil, err } - var idpType domain.IDPType - if idpGithub.Type != nil { - idpType = *idpGithub.Type - } - - if idpType != domain.IDPTypeGitHub { - return nil, domain.NewIDPWrongTypeError(domain.IDPTypeGitHub, idpType) - } - - err = json.Unmarshal(idpGithub.Payload, idpGithub) - if err != nil { - return nil, err - } - - return idpGithub, nil + return &domain.IDPGoogle{ + IdentityProvider: idp, + Google: typ, + }, nil } -func (i *idProvider) GetGithubEnterprise(ctx context.Context, client database.QueryExecutor, id domain.IDPIdentifierCondition, instanceID string, orgID *string) (*domain.IDPGithubEnterprise, error) { - idpGithubEnterprise := &domain.IDPGithubEnterprise{} - var err error - - idpGithubEnterprise.IdentityProvider, err = i.Get(ctx, client, id, instanceID, orgID) +func (i *idProvider) GetGithub(ctx context.Context, client database.QueryExecutor, opts ...database.QueryOption) (*domain.IDPGithub, error) { + idp, typ, err := getIDP[domain.Github](ctx, client, i, domain.IDPTypeGitHub, opts...) if err != nil { return nil, err } - var idpType domain.IDPType - if idpGithubEnterprise.Type != nil { - idpType = *idpGithubEnterprise.Type - } - - if idpType != domain.IDPTypeGitHubEnterprise { - return nil, domain.NewIDPWrongTypeError(domain.IDPTypeGitHubEnterprise, idpType) - } - - err = json.Unmarshal(idpGithubEnterprise.Payload, idpGithubEnterprise) - if err != nil { - return nil, err - } - - return idpGithubEnterprise, nil + return &domain.IDPGithub{ + IdentityProvider: idp, + Github: typ, + }, nil } -func (i *idProvider) GetGitlab(ctx context.Context, client database.QueryExecutor, id domain.IDPIdentifierCondition, instanceID string, orgID *string) (*domain.IDPGitlab, error) { - idpGitlab := &domain.IDPGitlab{} - var err error - - idpGitlab.IdentityProvider, err = i.Get(ctx, client, id, instanceID, orgID) +func (i *idProvider) GetGithubEnterprise(ctx context.Context, client database.QueryExecutor, opts ...database.QueryOption) (*domain.IDPGithubEnterprise, error) { + idp, typ, err := getIDP[domain.GithubEnterprise](ctx, client, i, domain.IDPTypeGitHubEnterprise, opts...) if err != nil { return nil, err } - var idpType domain.IDPType - if idpGitlab.Type != nil { - idpType = *idpGitlab.Type - } - - if idpType != domain.IDPTypeGitLab { - return nil, domain.NewIDPWrongTypeError(domain.IDPTypeGitLab, idpType) - } - - err = json.Unmarshal(idpGitlab.Payload, idpGitlab) - if err != nil { - return nil, err - } - - return idpGitlab, nil + return &domain.IDPGithubEnterprise{ + IdentityProvider: idp, + GithubEnterprise: typ, + }, nil } -func (i *idProvider) GetGitlabSelfHosting(ctx context.Context, client database.QueryExecutor, id domain.IDPIdentifierCondition, instanceID string, orgID *string) (*domain.IDPGitlabSelfHosting, error) { - idpGitlabSelfHosting := &domain.IDPGitlabSelfHosting{} - var err error - - idpGitlabSelfHosting.IdentityProvider, err = i.Get(ctx, client, id, instanceID, orgID) +func (i *idProvider) GetGitlab(ctx context.Context, client database.QueryExecutor, opts ...database.QueryOption) (*domain.IDPGitlab, error) { + idp, typ, err := getIDP[domain.Gitlab](ctx, client, i, domain.IDPTypeGitLab, opts...) if err != nil { return nil, err } - var idpType domain.IDPType - if idpGitlabSelfHosting.Type != nil { - idpType = *idpGitlabSelfHosting.Type - } - - if idpType != domain.IDPTypeGitLabSelfHosted { - return nil, domain.NewIDPWrongTypeError(domain.IDPTypeGitLabSelfHosted, idpType) - } - - err = json.Unmarshal(idpGitlabSelfHosting.Payload, idpGitlabSelfHosting) - if err != nil { - return nil, err - } - - return idpGitlabSelfHosting, nil + return &domain.IDPGitlab{ + IdentityProvider: idp, + Gitlab: typ, + }, nil } -func (i *idProvider) GetLDAP(ctx context.Context, client database.QueryExecutor, id domain.IDPIdentifierCondition, instanceID string, orgID *string) (*domain.IDPLDAP, error) { - ldap := &domain.IDPLDAP{} - var err error - - ldap.IdentityProvider, err = i.Get(ctx, client, id, instanceID, orgID) +func (i *idProvider) GetGitlabSelfHosted(ctx context.Context, client database.QueryExecutor, opts ...database.QueryOption) (*domain.IDPGitlabSelfHosted, error) { + idp, typ, err := getIDP[domain.GitlabSelfHosted](ctx, client, i, domain.IDPTypeGitLabSelfHosted, opts...) if err != nil { return nil, err } - var idpType domain.IDPType - if ldap.Type != nil { - idpType = *ldap.Type - } - - if idpType != domain.IDPTypeLDAP { - return nil, domain.NewIDPWrongTypeError(domain.IDPTypeLDAP, idpType) - } - - err = json.Unmarshal(ldap.Payload, ldap) - if err != nil { - return nil, err - } - - return ldap, nil + return &domain.IDPGitlabSelfHosted{ + IdentityProvider: idp, + GitlabSelfHosted: typ, + }, nil } -func (i *idProvider) GetApple(ctx context.Context, client database.QueryExecutor, id domain.IDPIdentifierCondition, instanceID string, orgID *string) (*domain.IDPApple, error) { - apple := &domain.IDPApple{} - var err error - - apple.IdentityProvider, err = i.Get(ctx, client, id, instanceID, orgID) +func (i *idProvider) GetLDAP(ctx context.Context, client database.QueryExecutor, opts ...database.QueryOption) (*domain.IDPLDAP, error) { + idp, typ, err := getIDP[domain.LDAP](ctx, client, i, domain.IDPTypeLDAP, opts...) if err != nil { return nil, err } - var idpType domain.IDPType - if apple.Type != nil { - idpType = *apple.Type - } - - if idpType != domain.IDPTypeApple { - return nil, domain.NewIDPWrongTypeError(domain.IDPTypeApple, idpType) - } - - err = json.Unmarshal(apple.Payload, apple) - if err != nil { - return nil, err - } - - return apple, nil + return &domain.IDPLDAP{ + IdentityProvider: idp, + LDAP: typ, + }, nil } -func (i *idProvider) GetSAML(ctx context.Context, client database.QueryExecutor, id domain.IDPIdentifierCondition, instanceID string, orgID *string) (*domain.IDPSAML, error) { - saml := &domain.IDPSAML{} - var err error - - saml.IdentityProvider, err = i.Get(ctx, client, id, instanceID, orgID) +func (i *idProvider) GetApple(ctx context.Context, client database.QueryExecutor, opts ...database.QueryOption) (*domain.IDPApple, error) { + idp, typ, err := getIDP[domain.Apple](ctx, client, i, domain.IDPTypeApple, opts...) if err != nil { return nil, err } + return &domain.IDPApple{ + IdentityProvider: idp, + Apple: typ, + }, nil +} + +func (i *idProvider) GetSAML(ctx context.Context, client database.QueryExecutor, opts ...database.QueryOption) (*domain.IDPSAML, error) { + idp, typ, err := getIDP[domain.SAML](ctx, client, i, domain.IDPTypeSAML, opts...) + if err != nil { + return nil, err + } + + return &domain.IDPSAML{ + IdentityProvider: idp, + SAML: typ, + }, nil +} + +type idpType interface { + domain.OIDC | domain.JWT | domain.OAuth | domain.Azure | domain.Google | + domain.Github | domain.GithubEnterprise | domain.Gitlab | domain.GitlabSelfHosted | + domain.LDAP | domain.Apple | domain.SAML +} + +func getIDP[T idpType](ctx context.Context, client database.QueryExecutor, repo *idProvider, expectedType domain.IDPType, opts ...database.QueryOption) (*domain.IdentityProvider, T, error) { + var zero T + idp, err := repo.Get(ctx, client, opts...) + if err != nil { + + return nil, zero, err + } + var idpType domain.IDPType - if saml.Type != nil { - idpType = *saml.Type + if idp.Type != nil { + idpType = *idp.Type } - if idpType != domain.IDPTypeSAML { - return nil, domain.NewIDPWrongTypeError(domain.IDPTypeSAML, idpType) + if idpType != expectedType { + return nil, zero, domain.NewIDPWrongTypeError(expectedType, idpType) } - err = json.Unmarshal(saml.Payload, saml) + var specificIDP T + err = json.Unmarshal(idp.Payload, &specificIDP) if err != nil { - return nil, err + return nil, zero, err } - return saml, nil + return idp, specificIDP, nil } // ------------------------------------------------------------- @@ -466,10 +334,6 @@ func (idProvider) TypeColumn() database.Column { return database.NewColumn("identity_providers", "type") } -func (idProvider) AutoRegisterColumn() database.Column { - return database.NewColumn("identity_providers", "auto_register") -} - func (idProvider) AllowCreationColumn() database.Column { return database.NewColumn("identity_providers", "allow_creation") } @@ -486,14 +350,10 @@ func (idProvider) AllowLinkingColumn() database.Column { return database.NewColumn("identity_providers", "allow_linking") } -func (idProvider) AllowAutoLinkingColumn() database.Column { +func (idProvider) AutoLinkingFieldColumn() database.Column { return database.NewColumn("identity_providers", "auto_linking_field") } -func (idProvider) StylingTypeColumn() database.Column { - return database.NewColumn("identity_providers", "styling_type") -} - func (idProvider) PayloadColumn() database.Column { return database.NewColumn("identity_providers", "payload") } @@ -528,7 +388,7 @@ func (i idProvider) OrgIDCondition(id *string) database.Condition { return database.NewTextCondition(i.OrgIDColumn(), database.TextOperationEqual, *id) } -func (i idProvider) IDCondition(id string) domain.IDPIdentifierCondition { +func (i idProvider) IDCondition(id string) database.Condition { return database.NewTextCondition(i.IDColumn(), database.TextOperationEqual, id) } @@ -536,46 +396,14 @@ func (i idProvider) StateCondition(state domain.IDPState) database.Condition { return database.NewTextCondition(i.StateColumn(), database.TextOperationEqual, state.String()) } -func (i idProvider) NameCondition(name string) domain.IDPIdentifierCondition { - return database.NewTextCondition(i.NameColumn(), database.TextOperationEqual, name) +func (i idProvider) NameCondition(op database.TextOperation, name string) database.Condition { + return database.NewTextCondition(i.NameColumn(), op, name) } func (i idProvider) TypeCondition(typ domain.IDPType) database.Condition { return database.NewNumberCondition(i.TypeColumn(), database.NumberOperationEqual, typ) } -func (i idProvider) AutoRegisterCondition(allow bool) database.Condition { - return database.NewBooleanCondition(i.AutoRegisterColumn(), allow) -} - -func (i idProvider) AllowCreationCondition(allow bool) database.Condition { - return database.NewBooleanCondition(i.AllowCreationColumn(), allow) -} - -func (i idProvider) AllowAutoCreationCondition(allow bool) database.Condition { - return database.NewBooleanCondition(i.AllowAutoCreationColumn(), allow) -} - -func (i idProvider) AllowAutoUpdateCondition(allow bool) database.Condition { - return database.NewBooleanCondition(i.AllowAutoUpdateColumn(), allow) -} - -func (i idProvider) AllowLinkingCondition(allow bool) database.Condition { - return database.NewBooleanCondition(i.AllowLinkingColumn(), allow) -} - -func (i idProvider) AllowAutoLinkingCondition(linkingType domain.IDPAutoLinkingField) database.Condition { - return database.NewTextCondition(i.AllowAutoLinkingColumn(), database.TextOperationEqual, linkingType.String()) -} - -func (i idProvider) StylingTypeCondition(style int16) database.Condition { - return database.NewNumberCondition(i.StylingTypeColumn(), database.NumberOperationEqual, style) -} - -func (i idProvider) PayloadCondition(payload string) database.Condition { - return database.NewTextCondition(i.PayloadColumn(), database.TextOperationEqual, payload) -} - // ------------------------------------------------------------- // changes // ------------------------------------------------------------- @@ -592,10 +420,6 @@ func (i idProvider) SetAllowCreation(allow bool) database.Change { return database.NewChange(i.AllowCreationColumn(), allow) } -func (i idProvider) SetAutoRegister(allow bool) database.Change { - return database.NewChange(i.AutoRegisterColumn(), allow) -} - func (i idProvider) SetAllowAutoCreation(allow bool) database.Change { return database.NewChange(i.AllowAutoCreationColumn(), allow) } @@ -608,12 +432,8 @@ func (i idProvider) SetAllowLinking(allow bool) database.Change { return database.NewChange(i.AllowLinkingColumn(), allow) } -func (i idProvider) SetAutoAllowLinking(allow bool) database.Change { - return database.NewChange(i.AllowAutoLinkingColumn(), allow) -} - -func (i idProvider) SetStylingType(stylingType int16) database.Change { - return database.NewChange(i.StylingTypeColumn(), stylingType) +func (i idProvider) SetAutoLinkingField(field *domain.IDPAutoLinkingField) database.Change { + return database.NewChangePtr(i.AutoLinkingFieldColumn(), field) } func (i idProvider) SetPayload(payload string) database.Change { @@ -624,6 +444,10 @@ func (i idProvider) SetUpdatedAt(updatedAt *time.Time) database.Change { return database.NewChangePtr(i.UpdatedAtColumn(), updatedAt) } +func (i idProvider) SetType(typee domain.IDPType) database.Change { + return database.NewChange(i.TypeColumn(), typee) +} + func scanIDProvider(ctx context.Context, querier database.Querier, builder *database.StatementBuilder) (*domain.IdentityProvider, error) { idp := &domain.IdentityProvider{} rows, err := querier.Query(ctx, builder.String(), builder.Args()...) diff --git a/backend/v3/storage/database/repository/id_provider_test.go b/backend/v3/storage/database/repository/id_provider_test.go index 68a11d8c0..05e50451d 100644 --- a/backend/v3/storage/database/repository/id_provider_test.go +++ b/backend/v3/storage/database/repository/id_provider_test.go @@ -14,8 +14,6 @@ import ( "github.com/zitadel/zitadel/backend/v3/storage/database/repository" ) -var stylingType int16 = 1 - func TestCreateIDProvider(t *testing.T) { beforeCreate := time.Now() tx, err := pool.Begin(t.Context(), nil) @@ -63,7 +61,6 @@ func TestCreateIDProvider(t *testing.T) { AllowAutoCreation: true, AllowAutoUpdate: true, AllowLinking: true, - StylingType: &stylingType, Payload: []byte("{}"), }, }, @@ -79,7 +76,6 @@ func TestCreateIDProvider(t *testing.T) { AllowAutoCreation: true, AllowAutoUpdate: true, AllowLinking: true, - StylingType: &stylingType, Payload: []byte("{}"), }, err: new(database.CheckError), @@ -100,7 +96,6 @@ func TestCreateIDProvider(t *testing.T) { AllowAutoCreation: true, AllowAutoUpdate: true, AllowLinking: true, - StylingType: &stylingType, Payload: []byte("{}"), } @@ -128,7 +123,6 @@ func TestCreateIDProvider(t *testing.T) { AllowAutoCreation: true, AllowAutoUpdate: true, AllowLinking: true, - StylingType: &stylingType, Payload: []byte("{}"), } @@ -185,7 +179,6 @@ func TestCreateIDProvider(t *testing.T) { AllowAutoCreation: true, AllowAutoUpdate: true, AllowLinking: true, - StylingType: &stylingType, Payload: []byte("{}"), } @@ -209,7 +202,6 @@ func TestCreateIDProvider(t *testing.T) { AllowAutoCreation: true, AllowAutoUpdate: true, AllowLinking: true, - StylingType: &stylingType, Payload: []byte("{}"), }, } @@ -226,7 +218,6 @@ func TestCreateIDProvider(t *testing.T) { AllowAutoCreation: true, AllowAutoUpdate: true, AllowLinking: true, - StylingType: &stylingType, Payload: []byte("{}"), }, err: new(database.CheckError), @@ -243,7 +234,6 @@ func TestCreateIDProvider(t *testing.T) { AllowAutoCreation: true, AllowAutoUpdate: true, AllowLinking: true, - StylingType: &stylingType, Payload: []byte("{}"), }, err: new(database.CheckError), @@ -259,7 +249,6 @@ func TestCreateIDProvider(t *testing.T) { AllowAutoCreation: true, AllowAutoUpdate: true, AllowLinking: true, - StylingType: &stylingType, Payload: []byte("{}"), }, err: new(database.IntegrityViolationError), @@ -277,7 +266,6 @@ func TestCreateIDProvider(t *testing.T) { AllowAutoCreation: true, AllowAutoUpdate: true, AllowLinking: true, - StylingType: &stylingType, Payload: []byte("{}"), }, err: new(database.ForeignKeyError), @@ -295,7 +283,6 @@ func TestCreateIDProvider(t *testing.T) { AllowAutoCreation: true, AllowAutoUpdate: true, AllowLinking: true, - StylingType: &stylingType, Payload: []byte("{}"), }, err: new(database.ForeignKeyError), @@ -330,11 +317,7 @@ func TestCreateIDProvider(t *testing.T) { afterCreate := time.Now() // check organization values - idp, err = idpRepo.Get(t.Context(), tx, - idpRepo.IDCondition(idp.ID), - idp.InstanceID, - idp.OrgID, - ) + idp, err = idpRepo.Get(t.Context(), tx, database.WithCondition(database.And(idpRepo.PrimaryKeyCondition(idp.InstanceID, idp.ID), idpRepo.OrgIDCondition(idp.OrgID)))) require.NoError(t, err) assert.Equal(t, tt.idp.InstanceID, idp.InstanceID) @@ -347,7 +330,6 @@ func TestCreateIDProvider(t *testing.T) { assert.Equal(t, tt.idp.AllowAutoCreation, idp.AllowAutoCreation) assert.Equal(t, tt.idp.AllowAutoUpdate, idp.AllowAutoUpdate) assert.Equal(t, tt.idp.AllowLinking, idp.AllowLinking) - assert.Equal(t, tt.idp.StylingType, idp.StylingType) assert.Equal(t, tt.idp.Payload, idp.Payload) assert.WithinRange(t, idp.CreatedAt, beforeCreate, afterCreate) assert.WithinRange(t, idp.UpdatedAt, beforeCreate, afterCreate) @@ -394,174 +376,27 @@ func TestUpdateIDProvider(t *testing.T) { AllowAutoCreation: true, AllowAutoUpdate: true, AllowLinking: true, - StylingType: &stylingType, Payload: []byte("{}"), } err := idpRepo.Create(t.Context(), tx, &idp) require.NoError(t, err) idp.Name = "new_name" - return &idp - }, - update: []database.Change{idpRepo.SetName("new_name")}, - rowsAffected: 1, - }, - { - name: "happy path update state", - testFunc: func(t *testing.T, tx database.QueryExecutor) *domain.IdentityProvider { - idp := domain.IdentityProvider{ - InstanceID: instanceId, - OrgID: &orgId, - ID: gofakeit.Name(), - State: domain.IDPStateActive, - Name: gofakeit.Name(), - Type: gu.Ptr(domain.IDPTypeOIDC), - AllowCreation: true, - AllowAutoCreation: true, - AllowAutoUpdate: true, - AllowLinking: true, - StylingType: &stylingType, - Payload: []byte("{}"), - } - - err := idpRepo.Create(t.Context(), tx, &idp) - require.NoError(t, err) idp.State = domain.IDPStateInactive - return &idp - }, - update: []database.Change{idpRepo.SetState(domain.IDPStateInactive)}, - rowsAffected: 1, - }, - { - name: "happy path update AllowCreation", - testFunc: func(t *testing.T, tx database.QueryExecutor) *domain.IdentityProvider { - idp := domain.IdentityProvider{ - InstanceID: instanceId, - OrgID: &orgId, - ID: gofakeit.Name(), - State: domain.IDPStateActive, - Name: gofakeit.Name(), - Type: gu.Ptr(domain.IDPTypeOIDC), - AllowCreation: true, - AllowAutoCreation: true, - AllowAutoUpdate: true, - AllowLinking: true, - StylingType: &stylingType, - Payload: []byte("{}"), - } - - err := idpRepo.Create(t.Context(), tx, &idp) - require.NoError(t, err) idp.AllowCreation = false - return &idp - }, - update: []database.Change{idpRepo.SetAllowCreation(false)}, - rowsAffected: 1, - }, - { - name: "happy path update AllowAutoCreation", - testFunc: func(t *testing.T, tx database.QueryExecutor) *domain.IdentityProvider { - idp := domain.IdentityProvider{ - InstanceID: instanceId, - OrgID: &orgId, - ID: gofakeit.Name(), - State: domain.IDPStateActive, - Name: gofakeit.Name(), - Type: gu.Ptr(domain.IDPTypeOIDC), - AllowCreation: true, - AllowAutoCreation: true, - AllowAutoUpdate: true, - AllowLinking: true, - StylingType: &stylingType, - Payload: []byte("{}"), - } - - err := idpRepo.Create(t.Context(), tx, &idp) - require.NoError(t, err) idp.AllowAutoCreation = false - return &idp - }, - update: []database.Change{idpRepo.SetAllowAutoCreation(false)}, - rowsAffected: 1, - }, - { - name: "happy path update AllowLinking", - testFunc: func(t *testing.T, tx database.QueryExecutor) *domain.IdentityProvider { - idp := domain.IdentityProvider{ - InstanceID: instanceId, - OrgID: &orgId, - ID: gofakeit.Name(), - State: domain.IDPStateActive, - Name: gofakeit.Name(), - Type: gu.Ptr(domain.IDPTypeOIDC), - AllowCreation: true, - AllowAutoCreation: true, - AllowAutoUpdate: true, - AllowLinking: true, - StylingType: &stylingType, - Payload: []byte("{}"), - } - - err := idpRepo.Create(t.Context(), tx, &idp) - require.NoError(t, err) idp.AllowLinking = false - return &idp - }, - update: []database.Change{idpRepo.SetAllowLinking(false)}, - rowsAffected: 1, - }, - { - name: "happy path update StylingType", - testFunc: func(t *testing.T, tx database.QueryExecutor) *domain.IdentityProvider { - idp := domain.IdentityProvider{ - InstanceID: instanceId, - OrgID: &orgId, - ID: gofakeit.Name(), - State: domain.IDPStateActive, - Name: gofakeit.Name(), - Type: gu.Ptr(domain.IDPTypeOIDC), - AllowCreation: true, - AllowAutoCreation: true, - AllowAutoUpdate: true, - AllowLinking: true, - StylingType: &stylingType, - Payload: []byte("{}"), - } - - err := idpRepo.Create(t.Context(), tx, &idp) - require.NoError(t, err) - newStyleType := int16(2) - idp.StylingType = &newStyleType - return &idp - }, - update: []database.Change{idpRepo.SetStylingType(2)}, - rowsAffected: 1, - }, - { - name: "happy path update Payload", - testFunc: func(t *testing.T, tx database.QueryExecutor) *domain.IdentityProvider { - idp := domain.IdentityProvider{ - InstanceID: instanceId, - OrgID: &orgId, - ID: gofakeit.Name(), - State: domain.IDPStateActive, - Name: gofakeit.Name(), - Type: gu.Ptr(domain.IDPTypeOIDC), - AllowCreation: true, - AllowAutoCreation: true, - AllowAutoUpdate: true, - AllowLinking: true, - StylingType: &stylingType, - Payload: []byte("{}"), - } - - err := idpRepo.Create(t.Context(), tx, &idp) - require.NoError(t, err) idp.Payload = []byte(`{"json": {}}`) return &idp }, - // update: []database.Change{idpRepo.SetPayload("{{}}")}, - update: []database.Change{idpRepo.SetPayload(`{"json": {}}`)}, + update: []database.Change{ + idpRepo.SetName("new_name"), + idpRepo.SetState(domain.IDPStateInactive), + idpRepo.SetAllowCreation(false), + idpRepo.SetAllowAutoCreation(false), + idpRepo.SetAllowLinking(false), + idpRepo.SetPayload(`{"json": {}}`), + }, rowsAffected: 1, }, } @@ -575,16 +410,19 @@ func TestUpdateIDProvider(t *testing.T) { t.Errorf("error rolling back savepoint: %v", err) } }() - // organizationRepo := repository.OrganizationRepository() idpRepo := repository.IDProviderRepository() createdIDP := tt.testFunc(t, tx) // update idp rowsAffected, err := idpRepo.Update(t.Context(), tx, - idpRepo.IDCondition(createdIDP.ID), - createdIDP.InstanceID, - createdIDP.OrgID, + database.And( + idpRepo.PrimaryKeyCondition( + createdIDP.InstanceID, + createdIDP.ID, + ), + idpRepo.OrgIDCondition(createdIDP.OrgID), + ), tt.update..., ) afterUpdate := time.Now() @@ -598,9 +436,10 @@ func TestUpdateIDProvider(t *testing.T) { // check idp values idp, err := idpRepo.Get(t.Context(), tx, - idpRepo.IDCondition(createdIDP.ID), - createdIDP.InstanceID, - createdIDP.OrgID, + database.WithCondition(database.And( + idpRepo.PrimaryKeyCondition(createdIDP.InstanceID, createdIDP.ID), + idpRepo.OrgIDCondition(createdIDP.OrgID), + )), ) require.NoError(t, err) @@ -614,7 +453,6 @@ func TestUpdateIDProvider(t *testing.T) { assert.Equal(t, createdIDP.AllowAutoCreation, idp.AllowAutoCreation) assert.Equal(t, createdIDP.AllowAutoUpdate, idp.AllowAutoUpdate) assert.Equal(t, createdIDP.AllowLinking, idp.AllowLinking) - assert.Equal(t, createdIDP.StylingType, idp.StylingType) assert.Equal(t, createdIDP.Payload, idp.Payload) assert.WithinRange(t, idp.UpdatedAt, beforeUpdate, afterUpdate) }) @@ -639,218 +477,75 @@ func TestGetIDProvider(t *testing.T) { createOrganization(t, tx, instanceId) idpRepo := repository.IDProviderRepository() + + idp := domain.IdentityProvider{ + InstanceID: instanceId, + OrgID: &orgId, + ID: gofakeit.UUID(), + State: domain.IDPStateActive, + Name: gofakeit.Name(), + Type: gu.Ptr(domain.IDPTypeOIDC), + AllowCreation: true, + AllowAutoCreation: true, + AllowAutoUpdate: true, + AllowLinking: true, + Payload: []byte("{}"), + } + err = idpRepo.Create(t.Context(), tx, &idp) + require.NoError(t, err) + type test struct { - name string - testFunc func(t *testing.T) *domain.IdentityProvider - idpIdentifierCondition database.Condition - err error + name string + condition database.Condition + err error } tests := []test{ - func() test { - id := gofakeit.Name() - return test{ - name: "happy path get using id", - testFunc: func(t *testing.T) *domain.IdentityProvider { - idp := domain.IdentityProvider{ - InstanceID: instanceId, - OrgID: &orgId, - ID: id, - State: domain.IDPStateActive, - Name: gofakeit.Name(), - Type: gu.Ptr(domain.IDPTypeOIDC), - AllowCreation: true, - AllowAutoCreation: true, - AllowAutoUpdate: true, - AllowLinking: true, - StylingType: &stylingType, - Payload: []byte("{}"), - } - - err := idpRepo.Create(t.Context(), tx, &idp) - require.NoError(t, err) - return &idp - }, - idpIdentifierCondition: idpRepo.IDCondition(id), - } - }(), - func() test { - name := gofakeit.Name() - return test{ - name: "happy path get using name", - testFunc: func(t *testing.T) *domain.IdentityProvider { - idp := domain.IdentityProvider{ - InstanceID: instanceId, - OrgID: &orgId, - ID: gofakeit.Name(), - State: domain.IDPStateActive, - Name: name, - Type: gu.Ptr(domain.IDPTypeOIDC), - AllowCreation: true, - AllowAutoCreation: true, - AllowAutoUpdate: true, - AllowLinking: true, - StylingType: &stylingType, - Payload: []byte("{}"), - } - - err := idpRepo.Create(t.Context(), tx, &idp) - require.NoError(t, err) - return &idp - }, - idpIdentifierCondition: idpRepo.NameCondition(name), - } - }(), { - name: "happy path using styling type", - testFunc: func(t *testing.T) *domain.IdentityProvider { - stylingType := 2 - idp := domain.IdentityProvider{ - InstanceID: instanceId, - OrgID: &orgId, - ID: gofakeit.Name(), - State: domain.IDPStateActive, - Name: gofakeit.Name(), - Type: gu.Ptr(domain.IDPTypeOIDC), - AllowCreation: true, - AllowAutoCreation: true, - AllowAutoUpdate: true, - AllowLinking: true, - StylingType: gu.Ptr(int16(stylingType)), - Payload: []byte("{}"), - } - - err := idpRepo.Create(t.Context(), tx, &idp) - require.NoError(t, err) - return &idp - }, - idpIdentifierCondition: idpRepo.StylingTypeCondition(2), + name: "happy path get using id", + condition: idpRepo.PrimaryKeyCondition(idp.InstanceID, idp.ID), }, { - name: "get using non existent id", - testFunc: func(t *testing.T) *domain.IdentityProvider { - idp := domain.IdentityProvider{ - InstanceID: instanceId, - OrgID: &orgId, - ID: gofakeit.Name(), - State: domain.IDPStateActive, - Name: gofakeit.Name(), - Type: gu.Ptr(domain.IDPTypeOIDC), - AllowCreation: true, - AllowAutoCreation: true, - AllowAutoUpdate: true, - AllowLinking: true, - StylingType: &stylingType, - Payload: []byte("{}"), - } - - err := idpRepo.Create(t.Context(), tx, &idp) - require.NoError(t, err) - return &idp - }, - idpIdentifierCondition: idpRepo.IDCondition("non-existent-id"), - err: new(database.NoRowFoundError), + name: "happy path get using name", + condition: database.And( + idpRepo.InstanceIDCondition(idp.InstanceID), + idpRepo.NameCondition(database.TextOperationEqual, idp.Name), + ), + }, + { + name: "get using non existent id", + condition: idpRepo.PrimaryKeyCondition(idp.InstanceID, "non-existent-id"), + err: new(database.NoRowFoundError), }, { name: "get using non existent name", - testFunc: func(t *testing.T) *domain.IdentityProvider { - idp := domain.IdentityProvider{ - InstanceID: instanceId, - OrgID: &orgId, - ID: gofakeit.Name(), - State: domain.IDPStateActive, - Name: gofakeit.Name(), - Type: gu.Ptr(domain.IDPTypeOIDC), - AllowCreation: true, - AllowAutoCreation: true, - AllowAutoUpdate: true, - AllowLinking: true, - StylingType: &stylingType, - Payload: []byte("{}"), - } - - err := idpRepo.Create(t.Context(), tx, &idp) - require.NoError(t, err) - return &idp - }, - idpIdentifierCondition: idpRepo.NameCondition("non-existent-name"), - err: new(database.NoRowFoundError), + condition: database.And( + idpRepo.InstanceIDCondition(idp.InstanceID), + idpRepo.NameCondition(database.TextOperationEqual, "non-existent-name"), + ), + err: new(database.NoRowFoundError), + }, + { + name: "non existent orgID", + condition: database.And( + idpRepo.PrimaryKeyCondition(idp.InstanceID, idp.ID), + idpRepo.OrgIDCondition(gu.Ptr("non-existing-org-id")), + ), + err: new(database.NoRowFoundError), + }, + { + name: "non existent instanceID", + condition: idpRepo.PrimaryKeyCondition("non-existent-instance-id", idp.ID), + err: new(database.NoRowFoundError), }, - func() test { - id := gofakeit.Name() - return test{ - name: "non existent orgID", - testFunc: func(t *testing.T) *domain.IdentityProvider { - idp := domain.IdentityProvider{ - InstanceID: instanceId, - OrgID: &orgId, - ID: id, - State: domain.IDPStateActive, - Name: gofakeit.Name(), - Type: gu.Ptr(domain.IDPTypeOIDC), - AllowCreation: true, - AllowAutoCreation: true, - AllowAutoUpdate: true, - AllowLinking: true, - StylingType: &stylingType, - Payload: []byte("{}"), - } - - err := idpRepo.Create(t.Context(), tx, &idp) - require.NoError(t, err) - idp.OrgID = gu.Ptr("non-existent-orgID") - return &idp - }, - idpIdentifierCondition: idpRepo.IDCondition(id), - err: new(database.NoRowFoundError), - } - }(), - func() test { - name := gofakeit.Name() - return test{ - name: "non existent instanceID", - testFunc: func(t *testing.T) *domain.IdentityProvider { - idp := domain.IdentityProvider{ - InstanceID: instanceId, - OrgID: &orgId, - ID: gofakeit.Name(), - State: domain.IDPStateActive, - Name: name, - Type: gu.Ptr(domain.IDPTypeOIDC), - AllowCreation: true, - AllowAutoCreation: true, - AllowAutoUpdate: true, - AllowLinking: true, - StylingType: &stylingType, - Payload: []byte("{}"), - } - - err := idpRepo.Create(t.Context(), tx, &idp) - require.NoError(t, err) - idp.InstanceID = "non-existent-instanceID" - return &idp - }, - idpIdentifierCondition: idpRepo.NameCondition(name), - err: new(database.NoRowFoundError), - } - }(), } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - var idp *domain.IdentityProvider - if tt.testFunc != nil { - idp = tt.testFunc(t) - } - // get idp - returnedIDP, err := idpRepo.Get(t.Context(), tx, - tt.idpIdentifierCondition, - idp.InstanceID, - idp.OrgID, - ) + returnedIDP, err := idpRepo.Get(t.Context(), tx, database.WithCondition(tt.condition)) + require.ErrorIs(t, err, tt.err) if err != nil { - require.ErrorIs(t, tt.err, err) return } @@ -864,7 +559,6 @@ func TestGetIDProvider(t *testing.T) { assert.Equal(t, returnedIDP.AllowAutoCreation, idp.AllowAutoCreation) assert.Equal(t, returnedIDP.AllowAutoUpdate, idp.AllowAutoUpdate) assert.Equal(t, returnedIDP.AllowLinking, idp.AllowLinking) - assert.Equal(t, returnedIDP.StylingType, idp.StylingType) assert.Equal(t, returnedIDP.Payload, idp.Payload) }) } @@ -873,7 +567,7 @@ func TestGetIDProvider(t *testing.T) { // gocognit linting fails due to number of test cases // and the fact that each test case has a testFunc() // -//nolint:gocognit + func TestListIDProvider(t *testing.T) { tx, err := pool.Begin(t.Context(), nil) require.NoError(t, err) @@ -922,7 +616,6 @@ func TestListIDProvider(t *testing.T) { AllowAutoCreation: true, AllowAutoUpdate: true, AllowLinking: true, - StylingType: &stylingType, Payload: []byte("{}"), } err := idpRepo.Create(t.Context(), tx, &idp) @@ -943,7 +636,6 @@ func TestListIDProvider(t *testing.T) { AllowAutoCreation: true, AllowAutoUpdate: true, AllowLinking: true, - StylingType: &stylingType, Payload: []byte("{}"), } @@ -986,7 +678,6 @@ func TestListIDProvider(t *testing.T) { AllowAutoCreation: true, AllowAutoUpdate: true, AllowLinking: true, - StylingType: &stylingType, Payload: []byte("{}"), } err := idpRepo.Create(t.Context(), tx, &idp) @@ -1007,7 +698,6 @@ func TestListIDProvider(t *testing.T) { AllowAutoCreation: true, AllowAutoUpdate: true, AllowLinking: true, - StylingType: &stylingType, Payload: []byte("{}"), } @@ -1039,7 +729,6 @@ func TestListIDProvider(t *testing.T) { AllowAutoCreation: true, AllowAutoUpdate: true, AllowLinking: true, - StylingType: &stylingType, Payload: []byte("{}"), } @@ -1070,7 +759,6 @@ func TestListIDProvider(t *testing.T) { AllowAutoCreation: true, AllowAutoUpdate: true, AllowLinking: true, - StylingType: &stylingType, Payload: []byte("{}"), } @@ -1102,7 +790,6 @@ func TestListIDProvider(t *testing.T) { AllowAutoCreation: true, AllowAutoUpdate: true, AllowLinking: true, - StylingType: &stylingType, Payload: []byte("{}"), } err := idpRepo.Create(t.Context(), tx, &idp) @@ -1123,7 +810,6 @@ func TestListIDProvider(t *testing.T) { AllowAutoCreation: true, AllowAutoUpdate: true, AllowLinking: true, - StylingType: &stylingType, Payload: []byte("{}"), } @@ -1156,7 +842,6 @@ func TestListIDProvider(t *testing.T) { AllowAutoCreation: true, AllowAutoUpdate: true, AllowLinking: true, - StylingType: &stylingType, Payload: []byte("{}"), } err := idpRepo.Create(t.Context(), tx, &idp) @@ -1178,7 +863,6 @@ func TestListIDProvider(t *testing.T) { AllowAutoCreation: true, AllowAutoUpdate: true, AllowLinking: true, - StylingType: &stylingType, Payload: []byte("{}"), } @@ -1211,7 +895,6 @@ func TestListIDProvider(t *testing.T) { AllowAutoCreation: true, AllowAutoUpdate: true, AllowLinking: true, - StylingType: &stylingType, Payload: []byte("{}"), } err := idpRepo.Create(t.Context(), tx, &idp) @@ -1232,7 +915,6 @@ func TestListIDProvider(t *testing.T) { AllowAutoCreation: true, AllowAutoUpdate: true, AllowLinking: true, - StylingType: &stylingType, Payload: []byte("{}"), } @@ -1244,7 +926,7 @@ func TestListIDProvider(t *testing.T) { return idps }, - conditionClauses: []database.Condition{idpRepo.NameCondition(name)}, + conditionClauses: []database.Condition{idpRepo.NameCondition(database.TextOperationEqual, name)}, } }(), { @@ -1264,7 +946,6 @@ func TestListIDProvider(t *testing.T) { AllowAutoCreation: true, AllowAutoUpdate: true, AllowLinking: true, - StylingType: &stylingType, Payload: []byte("{}"), } err := idpRepo.Create(t.Context(), tx, &idp) @@ -1286,7 +967,6 @@ func TestListIDProvider(t *testing.T) { AllowAutoCreation: true, AllowAutoUpdate: true, AllowLinking: true, - StylingType: &stylingType, Payload: []byte("{}"), } @@ -1300,329 +980,6 @@ func TestListIDProvider(t *testing.T) { }, conditionClauses: []database.Condition{idpRepo.TypeCondition(domain.IDPTypeLDAP)}, }, - { - name: "multiple idps filter on AllowCreation", - testFunc: func(t *testing.T) []*domain.IdentityProvider { - // create idp - // this idp is created as an additional idp which should NOT - // be returned in the results of this test case - idp := domain.IdentityProvider{ - InstanceID: instanceId, - OrgID: &orgId, - ID: gofakeit.Name(), - State: domain.IDPStateInactive, - Name: gofakeit.Name(), - Type: gu.Ptr(domain.IDPTypeOIDC), - AllowCreation: true, - AllowAutoCreation: true, - AllowAutoUpdate: true, - AllowLinking: true, - StylingType: &stylingType, - Payload: []byte("{}"), - } - err := idpRepo.Create(t.Context(), tx, &idp) - require.NoError(t, err) - - noOfIDPs := 5 - idps := make([]*domain.IdentityProvider, noOfIDPs) - for i := range noOfIDPs { - - idp := domain.IdentityProvider{ - InstanceID: instanceId, - OrgID: &orgId, - ID: gofakeit.Name(), - State: domain.IDPStateActive, - Name: gofakeit.Name(), - Type: gu.Ptr(domain.IDPTypeLDAP), - // AllowCreation set to false - AllowCreation: false, - AllowAutoCreation: true, - AllowAutoUpdate: true, - AllowLinking: true, - StylingType: &stylingType, - Payload: []byte("{}"), - } - - err := idpRepo.Create(t.Context(), tx, &idp) - require.NoError(t, err) - - idps[i] = &idp - } - - return idps - }, - conditionClauses: []database.Condition{idpRepo.AllowCreationCondition(false)}, - }, - { - name: "multiple idps filter on AllowAutoCreation", - testFunc: func(t *testing.T) []*domain.IdentityProvider { - // create idp - // this idp is created as an additional idp which should NOT - // be returned in the results of this test case - idp := domain.IdentityProvider{ - InstanceID: instanceId, - OrgID: &orgId, - ID: gofakeit.Name(), - State: domain.IDPStateInactive, - Name: gofakeit.Name(), - Type: gu.Ptr(domain.IDPTypeOIDC), - AllowCreation: true, - AllowAutoCreation: true, - AllowAutoUpdate: true, - AllowLinking: true, - StylingType: &stylingType, - Payload: []byte("{}"), - } - err := idpRepo.Create(t.Context(), tx, &idp) - require.NoError(t, err) - - noOfIDPs := 5 - idps := make([]*domain.IdentityProvider, noOfIDPs) - for i := range noOfIDPs { - - idp := domain.IdentityProvider{ - InstanceID: instanceId, - OrgID: &orgId, - ID: gofakeit.Name(), - State: domain.IDPStateActive, - Name: gofakeit.Name(), - Type: gu.Ptr(domain.IDPTypeLDAP), - AllowCreation: true, - // AllowAutoCreation set to false - AllowAutoCreation: false, - AllowAutoUpdate: true, - AllowLinking: true, - StylingType: &stylingType, - Payload: []byte("{}"), - } - - err := idpRepo.Create(t.Context(), tx, &idp) - require.NoError(t, err) - - idps[i] = &idp - } - - return idps - }, - conditionClauses: []database.Condition{idpRepo.AllowAutoCreationCondition(false)}, - }, - { - name: "multiple idps filter on AllowAutoUpdate", - testFunc: func(t *testing.T) []*domain.IdentityProvider { - // create idp - // this idp is created as an additional idp which should NOT - // be returned in the results of this test case - idp := domain.IdentityProvider{ - InstanceID: instanceId, - OrgID: &orgId, - ID: gofakeit.Name(), - // state inactive - State: domain.IDPStateInactive, - Name: gofakeit.Name(), - Type: gu.Ptr(domain.IDPTypeOIDC), - AllowCreation: true, - AllowAutoCreation: true, - AllowAutoUpdate: true, - AllowLinking: true, - StylingType: &stylingType, - Payload: []byte("{}"), - } - err := idpRepo.Create(t.Context(), tx, &idp) - require.NoError(t, err) - - noOfIDPs := 5 - idps := make([]*domain.IdentityProvider, noOfIDPs) - for i := range noOfIDPs { - - idp := domain.IdentityProvider{ - InstanceID: instanceId, - OrgID: &orgId, - ID: gofakeit.Name(), - State: domain.IDPStateActive, - Name: gofakeit.Name(), - Type: gu.Ptr(domain.IDPTypeLDAP), - AllowCreation: true, - AllowAutoCreation: true, - // AllowAutoUpdate set to false - AllowAutoUpdate: false, - AllowLinking: true, - StylingType: &stylingType, - Payload: []byte("{}"), - } - - err := idpRepo.Create(t.Context(), tx, &idp) - require.NoError(t, err) - - idps[i] = &idp - } - - return idps - }, - conditionClauses: []database.Condition{idpRepo.AllowAutoUpdateCondition(false)}, - }, - { - name: "multiple idps filter on AllowLinking", - testFunc: func(t *testing.T) []*domain.IdentityProvider { - // create idp - // this idp is created as an additional idp which should NOT - // be returned in the results of this test case - idp := domain.IdentityProvider{ - InstanceID: instanceId, - OrgID: &orgId, - ID: gofakeit.Name(), - // state inactive - State: domain.IDPStateInactive, - Name: gofakeit.Name(), - Type: gu.Ptr(domain.IDPTypeOIDC), - AllowCreation: true, - AllowAutoCreation: true, - AllowAutoUpdate: true, - AllowLinking: true, - StylingType: &stylingType, - Payload: []byte("{}"), - } - err := idpRepo.Create(t.Context(), tx, &idp) - require.NoError(t, err) - - noOfIDPs := 5 - idps := make([]*domain.IdentityProvider, noOfIDPs) - for i := range noOfIDPs { - - idp := domain.IdentityProvider{ - InstanceID: instanceId, - OrgID: &orgId, - ID: gofakeit.Name(), - State: domain.IDPStateActive, - Name: gofakeit.Name(), - Type: gu.Ptr(domain.IDPTypeLDAP), - AllowCreation: true, - AllowAutoCreation: true, - AllowAutoUpdate: true, - // AllowLinking set to false - AllowLinking: false, - StylingType: &stylingType, - Payload: []byte("{}"), - } - - err := idpRepo.Create(t.Context(), tx, &idp) - require.NoError(t, err) - - idps[i] = &idp - } - - return idps - }, - conditionClauses: []database.Condition{idpRepo.AllowLinkingCondition(false)}, - }, - { - name: "multiple idps filter on StylingType", - testFunc: func(t *testing.T) []*domain.IdentityProvider { - // create idp - // this idp is created as an additional idp which should NOT - // be returned in the results of this test case - idp := domain.IdentityProvider{ - InstanceID: instanceId, - OrgID: &orgId, - ID: gofakeit.Name(), - State: domain.IDPStateActive, - Name: gofakeit.Name(), - Type: gu.Ptr(domain.IDPTypeOIDC), - AllowCreation: true, - AllowAutoCreation: true, - AllowAutoUpdate: true, - AllowLinking: true, - StylingType: &stylingType, - Payload: []byte("{}"), - } - err := idpRepo.Create(t.Context(), tx, &idp) - require.NoError(t, err) - - var sytlingType int16 = 4 - noOfIDPs := 1 - idps := make([]*domain.IdentityProvider, noOfIDPs) - for i := range noOfIDPs { - - idp := domain.IdentityProvider{ - InstanceID: instanceId, - OrgID: &orgId, - ID: gofakeit.Name(), - State: domain.IDPStateActive, - Name: gofakeit.Name(), - Type: gu.Ptr(domain.IDPTypeOIDC), - AllowCreation: true, - AllowAutoCreation: true, - AllowAutoUpdate: true, - AllowLinking: true, - // StylingType set to 4 - StylingType: &sytlingType, - Payload: []byte("{}"), - } - - err := idpRepo.Create(t.Context(), tx, &idp) - require.NoError(t, err) - - idps[i] = &idp - } - - return idps - }, - conditionClauses: []database.Condition{idpRepo.StylingTypeCondition(4)}, - }, - func() test { - payload := []byte(`{"json": {}}`) - return test{ - name: "multiple idps filter on Payload", - testFunc: func(t *testing.T) []*domain.IdentityProvider { - // create idp - // this idp is created as an additional idp which should NOT - // be returned in the results of this test case - idp := domain.IdentityProvider{ - InstanceID: instanceId, - OrgID: &orgId, - ID: gofakeit.Name(), - State: domain.IDPStateActive, - Name: gofakeit.Name(), - Type: gu.Ptr(domain.IDPTypeOIDC), - AllowCreation: true, - AllowAutoCreation: true, - AllowAutoUpdate: true, - AllowLinking: true, - StylingType: &stylingType, - Payload: []byte("{}"), - } - err := idpRepo.Create(t.Context(), tx, &idp) - require.NoError(t, err) - - noOfIDPs := 1 - idps := make([]*domain.IdentityProvider, noOfIDPs) - for i := range noOfIDPs { - - idp := domain.IdentityProvider{ - InstanceID: instanceId, - OrgID: &orgId, - ID: gofakeit.Name(), - State: domain.IDPStateActive, - Name: gofakeit.Name(), - Type: gu.Ptr(domain.IDPTypeOIDC), - AllowCreation: true, - AllowAutoCreation: true, - AllowAutoUpdate: true, - AllowLinking: true, - StylingType: &stylingType, - Payload: payload, - } - - err := idpRepo.Create(t.Context(), tx, &idp) - require.NoError(t, err) - - idps[i] = &idp - } - - return idps - }, - conditionClauses: []database.Condition{idpRepo.PayloadCondition(string(payload))}, - } - }(), } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -1635,7 +992,7 @@ func TestListIDProvider(t *testing.T) { // check idp values returnedIDPs, err := idpRepo.List(t.Context(), tx, - tt.conditionClauses..., + database.WithCondition(database.And(append(tt.conditionClauses, idpRepo.InstanceIDCondition(instanceId))...)), ) require.NoError(t, err) if tt.noIDPsReturned { @@ -1644,21 +1001,7 @@ func TestListIDProvider(t *testing.T) { } assert.Equal(t, len(idps), len(returnedIDPs)) - for i, idp := range idps { - - assert.Equal(t, returnedIDPs[i].InstanceID, idp.InstanceID) - assert.Equal(t, returnedIDPs[i].OrgID, idp.OrgID) - assert.Equal(t, returnedIDPs[i].State, idp.State) - assert.Equal(t, returnedIDPs[i].ID, idp.ID) - assert.Equal(t, returnedIDPs[i].Name, idp.Name) - assert.Equal(t, returnedIDPs[i].Type, idp.Type) - assert.Equal(t, returnedIDPs[i].AllowCreation, idp.AllowCreation) - assert.Equal(t, returnedIDPs[i].AllowAutoCreation, idp.AllowAutoCreation) - assert.Equal(t, returnedIDPs[i].AllowAutoUpdate, idp.AllowAutoUpdate) - assert.Equal(t, returnedIDPs[i].AllowLinking, idp.AllowLinking) - assert.Equal(t, returnedIDPs[i].StylingType, idp.StylingType) - assert.Equal(t, returnedIDPs[i].Payload, idp.Payload) - } + assert.ElementsMatch(t, idps, returnedIDPs) }) } } @@ -1684,7 +1027,7 @@ func TestDeleteIDProvider(t *testing.T) { type test struct { name string testFunc func(t *testing.T) - idpIdentifierCondition domain.IDPIdentifierCondition + idpIdentifierCondition database.Condition noOfDeletedRows int64 } tests := []test{ @@ -1706,7 +1049,6 @@ func TestDeleteIDProvider(t *testing.T) { AllowAutoCreation: true, AllowAutoUpdate: true, AllowLinking: true, - StylingType: &stylingType, Payload: []byte("{}"), } @@ -1718,43 +1060,12 @@ func TestDeleteIDProvider(t *testing.T) { noOfDeletedRows: noOfIDPs, } }(), - func() test { - name := gofakeit.Name() - var noOfIDPs int64 = 1 - return test{ - name: "happy path delete idp filter name", - testFunc: func(t *testing.T) { - for range noOfIDPs { - idp := domain.IdentityProvider{ - InstanceID: instanceId, - OrgID: &orgId, - ID: gofakeit.Name(), - State: domain.IDPStateActive, - Name: name, - Type: gu.Ptr(domain.IDPTypeOIDC), - AllowCreation: true, - AllowAutoCreation: true, - AllowAutoUpdate: true, - AllowLinking: true, - StylingType: &stylingType, - Payload: []byte("{}"), - } - - err := idpRepo.Create(t.Context(), tx, &idp) - require.NoError(t, err) - - } - }, - idpIdentifierCondition: idpRepo.NameCondition(name), - noOfDeletedRows: noOfIDPs, - } - }(), { name: "delete non existent idp", - idpIdentifierCondition: idpRepo.NameCondition(gofakeit.Name()), + idpIdentifierCondition: idpRepo.IDCondition(gofakeit.UUID()), }, func() test { - name := gofakeit.Name() + id := gofakeit.UUID() return test{ name: "deleted already deleted idp", testFunc: func(t *testing.T) { @@ -1763,15 +1074,14 @@ func TestDeleteIDProvider(t *testing.T) { idp := domain.IdentityProvider{ InstanceID: instanceId, OrgID: &orgId, - ID: gofakeit.Name(), + ID: id, State: domain.IDPStateActive, - Name: name, + Name: gofakeit.Name(), Type: gu.Ptr(domain.IDPTypeOIDC), AllowCreation: true, AllowAutoCreation: true, AllowAutoUpdate: true, AllowLinking: true, - StylingType: &stylingType, Payload: []byte("{}"), } @@ -1781,14 +1091,16 @@ func TestDeleteIDProvider(t *testing.T) { // delete organization affectedRows, err := idpRepo.Delete(t.Context(), tx, - idpRepo.NameCondition(name), - instanceId, - &orgId, + database.And( + idpRepo.InstanceIDCondition(instanceId), + idpRepo.OrgIDCondition(&orgId), + idpRepo.IDCondition(id), + ), ) assert.Equal(t, int64(1), affectedRows) require.NoError(t, err) }, - idpIdentifierCondition: idpRepo.NameCondition(name), + idpIdentifierCondition: idpRepo.IDCondition(id), // this test should return 0 affected rows as the idp was already deleted noOfDeletedRows: 0, } @@ -1802,18 +1114,22 @@ func TestDeleteIDProvider(t *testing.T) { // delete idp noOfDeletedRows, err := idpRepo.Delete(t.Context(), tx, - tt.idpIdentifierCondition, - instanceId, - &orgId, + database.And( + tt.idpIdentifierCondition, + idpRepo.InstanceIDCondition(instanceId), + idpRepo.OrgIDCondition(&orgId), + ), ) require.NoError(t, err) assert.Equal(t, noOfDeletedRows, tt.noOfDeletedRows) // check idp was deleted organization, err := idpRepo.Get(t.Context(), tx, - tt.idpIdentifierCondition, - instanceId, - &orgId, + database.WithCondition(database.And( + tt.idpIdentifierCondition, + idpRepo.InstanceIDCondition(instanceId), + idpRepo.OrgIDCondition(&orgId), + )), ) require.ErrorIs(t, err, new(database.NoRowFoundError)) assert.Nil(t, organization) diff --git a/backend/v3/storage/database/repository/repository_test.go b/backend/v3/storage/database/repository/repository_test.go index 68c0ab8ad..0e2dc8194 100644 --- a/backend/v3/storage/database/repository/repository_test.go +++ b/backend/v3/storage/database/repository/repository_test.go @@ -199,7 +199,7 @@ func createIdentityProvider(t *testing.T, tx database.QueryExecutor, instanceID, AllowAutoCreation: true, AllowAutoUpdate: true, AllowLinking: true, - StylingType: &stylingType, + AutoLinkingField: gu.Ptr(domain.IDPAutoLinkingFieldEmail), Payload: []byte("{}"), } idpRepo := repository.IDProviderRepository() diff --git a/internal/query/projection/idp_template_relational.go b/internal/query/projection/idp_template_relational.go index 79d058fce..92b29c0c5 100644 --- a/internal/query/projection/idp_template_relational.go +++ b/internal/query/projection/idp_template_relational.go @@ -4,8 +4,12 @@ import ( "context" "database/sql" "encoding/json" + "slices" + + "github.com/muhlemmer/gu" "github.com/zitadel/zitadel/backend/v3/domain" + "github.com/zitadel/zitadel/backend/v3/storage/database" v3_sql "github.com/zitadel/zitadel/backend/v3/storage/database/dialect/sql" "github.com/zitadel/zitadel/backend/v3/storage/database/repository" internal_domain "github.com/zitadel/zitadel/internal/domain" @@ -18,19 +22,6 @@ import ( "github.com/zitadel/zitadel/internal/zerrors" ) -const ( - IDPRelationalTable = "zitadel.identity_providers" - IDPRelationalOrgIdCol = "org_id" - IDPRelationalAutoRegisterCol = "auto_register" - IDPRelationalPayloadCol = "payload" - IDPRelationalOrgId = "org_id" - IDPRelationalAllowCreationCol = "allow_creation" - IDPRelationalAllowLinkingCol = "allow_linking" - IDPRelationalAllowAutoCreationCol = "allow_auto_creation" - IDPRelationalAllowAutoUpdateCol = "allow_auto_update" - IDPRelationalAllowAutoLinkingCol = "auto_linking_field" -) - type idpTemplateRelationalProjection struct { idpRepo domain.IDProviderRepository } @@ -43,7 +34,7 @@ func newIDPTemplateRelationalProjection(ctx context.Context, config handler.Conf } func (*idpTemplateRelationalProjection) Name() string { - return IDPRelationalTable + return "zitadel.identity_providers" } func (p *idpTemplateRelationalProjection) Reducers() []handler.AggregateReducer { @@ -53,119 +44,119 @@ func (p *idpTemplateRelationalProjection) Reducers() []handler.AggregateReducer EventReducers: []handler.EventReducer{ { Event: instance.IDPConfigAddedEventType, - Reduce: p.reduceIDPRelationalAdded, + Reduce: p.reduceIDPAdded, }, { Event: instance.IDPConfigChangedEventType, - Reduce: p.reduceIDPRelationalChanged, + Reduce: p.reduceIDPChanged, }, { Event: instance.IDPConfigDeactivatedEventType, - Reduce: p.reduceIDRelationalPDeactivated, + Reduce: p.reduceIDPDeactivated, }, { Event: instance.IDPConfigReactivatedEventType, - Reduce: p.reduceIDPRelationalReactivated, + Reduce: p.reduceIDPReactivated, }, { Event: instance.IDPConfigRemovedEventType, - Reduce: p.reduceIDPRelationalRemoved, + Reduce: p.reduceIDPRemoved, }, { Event: instance.IDPOIDCConfigAddedEventType, - Reduce: p.reduceOIDCRelationalConfigAdded, + Reduce: p.reduceOIDCConfigAdded, }, { Event: instance.IDPOIDCConfigChangedEventType, - Reduce: p.reduceOIDCRelationalConfigChanged, + Reduce: p.reduceOIDCConfigChanged, }, { Event: instance.IDPJWTConfigAddedEventType, - Reduce: p.reduceJWTRelationalConfigAdded, + Reduce: p.reduceJWTConfigAdded, }, { Event: instance.IDPJWTConfigChangedEventType, - Reduce: p.reduceJWTRelationalConfigChanged, + Reduce: p.reduceJWTConfigChanged, }, { Event: instance.OAuthIDPAddedEventType, - Reduce: p.reduceOAuthIDPRelationalAdded, + Reduce: p.reduceOAuthIDPAdded, }, { Event: instance.OAuthIDPChangedEventType, - Reduce: p.reduceOAuthIDPRelationalChanged, + Reduce: p.reduceOAuthIDPChanged, }, { Event: instance.OIDCIDPAddedEventType, - Reduce: p.reduceOIDCIDPRelationalAdded, + Reduce: p.reduceOIDCIDPAdded, }, { Event: instance.OIDCIDPChangedEventType, - Reduce: p.reduceOIDCIDPRelationalChanged, + Reduce: p.reduceOIDCIDPChanged, }, { Event: instance.OIDCIDPMigratedAzureADEventType, - Reduce: p.reduceOIDCIDPRelationalMigratedAzureAD, + Reduce: p.reduceOIDCIDPMigratedAzureAD, }, { Event: instance.OIDCIDPMigratedGoogleEventType, - Reduce: p.reduceOIDCIDPRelationalMigratedGoogle, + Reduce: p.reduceOIDCIDPMigratedGoogle, }, { Event: instance.JWTIDPAddedEventType, - Reduce: p.reduceJWTIDPRelationalAdded, + Reduce: p.reduceJWTIDPAdded, }, { Event: instance.JWTIDPChangedEventType, - Reduce: p.reduceJWTIDPRelationalChanged, + Reduce: p.reduceJWTIDPChanged, }, { Event: instance.AzureADIDPAddedEventType, - Reduce: p.reduceAzureADIDPRelationalAdded, + Reduce: p.reduceAzureADIDPAdded, }, { Event: instance.AzureADIDPChangedEventType, - Reduce: p.reduceAzureADIDPRelationalChanged, + Reduce: p.reduceAzureADIDPChanged, }, { Event: instance.GitHubIDPAddedEventType, - Reduce: p.reduceGitHubIDPRelationalAdded, + Reduce: p.reduceGitHubIDPAdded, }, { Event: instance.GitHubIDPChangedEventType, - Reduce: p.reduceGitHubIDPRelationalChanged, + Reduce: p.reduceGitHubIDPChanged, }, { Event: instance.GitHubEnterpriseIDPAddedEventType, - Reduce: p.reduceGitHubEnterpriseIDPRelationalAdded, + Reduce: p.reduceGitHubEnterpriseIDPAdded, }, { Event: instance.GitHubEnterpriseIDPChangedEventType, - Reduce: p.reduceGitHubEnterpriseIDPRelationalChanged, + Reduce: p.reduceGitHubEnterpriseIDPChanged, }, { Event: instance.GitLabIDPAddedEventType, - Reduce: p.reduceGitLabIDPRelationalAdded, + Reduce: p.reduceGitLabIDPAdded, }, { Event: instance.GitLabIDPChangedEventType, - Reduce: p.reduceGitLabIDPRelationalChanged, + Reduce: p.reduceGitLabIDPChanged, }, { Event: instance.GitLabSelfHostedIDPAddedEventType, - Reduce: p.reduceGitLabSelfHostedIDPRelationalAdded, + Reduce: p.reduceGitLabSelfHostedIDPAdded, }, { Event: instance.GitLabSelfHostedIDPChangedEventType, - Reduce: p.reduceGitLabSelfHostedIDPRelationalChanged, + Reduce: p.reduceGitLabSelfHostedIDPChanged, }, { Event: instance.GoogleIDPAddedEventType, - Reduce: p.reduceGoogleIDPRelationalAdded, + Reduce: p.reduceGoogleIDPAdded, }, { Event: instance.GoogleIDPChangedEventType, - Reduce: p.reduceGoogleIDPRelationalChanged, + Reduce: p.reduceGoogleIDPChanged, }, { Event: instance.LDAPIDPAddedEventType, @@ -202,119 +193,119 @@ func (p *idpTemplateRelationalProjection) Reducers() []handler.AggregateReducer EventReducers: []handler.EventReducer{ { Event: org.IDPConfigAddedEventType, - Reduce: p.reduceIDPRelationalAdded, + Reduce: p.reduceIDPAdded, }, { Event: org.IDPConfigChangedEventType, - Reduce: p.reduceIDPRelationalChanged, + Reduce: p.reduceIDPChanged, }, { Event: org.IDPConfigDeactivatedEventType, - Reduce: p.reduceIDRelationalPDeactivated, + Reduce: p.reduceIDPDeactivated, }, { Event: org.IDPConfigReactivatedEventType, - Reduce: p.reduceIDPRelationalReactivated, + Reduce: p.reduceIDPReactivated, }, { Event: org.IDPConfigRemovedEventType, - Reduce: p.reduceIDPRelationalRemoved, + Reduce: p.reduceIDPRemoved, }, { Event: org.IDPOIDCConfigAddedEventType, - Reduce: p.reduceOIDCRelationalConfigAdded, + Reduce: p.reduceOIDCConfigAdded, }, { Event: org.IDPOIDCConfigChangedEventType, - Reduce: p.reduceOIDCRelationalConfigChanged, + Reduce: p.reduceOIDCConfigChanged, }, { Event: org.IDPJWTConfigAddedEventType, - Reduce: p.reduceJWTRelationalConfigAdded, + Reduce: p.reduceJWTConfigAdded, }, { Event: org.IDPJWTConfigChangedEventType, - Reduce: p.reduceJWTRelationalConfigChanged, + Reduce: p.reduceJWTConfigChanged, }, { Event: org.OAuthIDPAddedEventType, - Reduce: p.reduceOAuthIDPRelationalAdded, + Reduce: p.reduceOAuthIDPAdded, }, { Event: org.OAuthIDPChangedEventType, - Reduce: p.reduceOAuthIDPRelationalChanged, + Reduce: p.reduceOAuthIDPChanged, }, { Event: org.OIDCIDPAddedEventType, - Reduce: p.reduceOIDCIDPRelationalAdded, + Reduce: p.reduceOIDCIDPAdded, }, { Event: org.OIDCIDPChangedEventType, - Reduce: p.reduceOIDCIDPRelationalChanged, + Reduce: p.reduceOIDCIDPChanged, }, { Event: org.OIDCIDPMigratedAzureADEventType, - Reduce: p.reduceOIDCIDPRelationalMigratedAzureAD, + Reduce: p.reduceOIDCIDPMigratedAzureAD, }, { Event: org.OIDCIDPMigratedGoogleEventType, - Reduce: p.reduceOIDCIDPRelationalMigratedGoogle, + Reduce: p.reduceOIDCIDPMigratedGoogle, }, { Event: org.JWTIDPAddedEventType, - Reduce: p.reduceJWTIDPRelationalAdded, + Reduce: p.reduceJWTIDPAdded, }, { Event: org.JWTIDPChangedEventType, - Reduce: p.reduceJWTIDPRelationalChanged, + Reduce: p.reduceJWTIDPChanged, }, { Event: org.AzureADIDPAddedEventType, - Reduce: p.reduceAzureADIDPRelationalAdded, + Reduce: p.reduceAzureADIDPAdded, }, { Event: org.AzureADIDPChangedEventType, - Reduce: p.reduceAzureADIDPRelationalChanged, + Reduce: p.reduceAzureADIDPChanged, }, { Event: org.GitHubIDPAddedEventType, - Reduce: p.reduceGitHubIDPRelationalAdded, + Reduce: p.reduceGitHubIDPAdded, }, { Event: org.GitHubIDPChangedEventType, - Reduce: p.reduceGitHubIDPRelationalChanged, + Reduce: p.reduceGitHubIDPChanged, }, { Event: org.GitHubEnterpriseIDPAddedEventType, - Reduce: p.reduceGitHubEnterpriseIDPRelationalAdded, + Reduce: p.reduceGitHubEnterpriseIDPAdded, }, { Event: org.GitHubEnterpriseIDPChangedEventType, - Reduce: p.reduceGitHubEnterpriseIDPRelationalChanged, + Reduce: p.reduceGitHubEnterpriseIDPChanged, }, { Event: org.GitLabIDPAddedEventType, - Reduce: p.reduceGitLabIDPRelationalAdded, + Reduce: p.reduceGitLabIDPAdded, }, { Event: org.GitLabIDPChangedEventType, - Reduce: p.reduceGitLabIDPRelationalChanged, + Reduce: p.reduceGitLabIDPChanged, }, { Event: org.GitLabSelfHostedIDPAddedEventType, - Reduce: p.reduceGitLabSelfHostedIDPRelationalAdded, + Reduce: p.reduceGitLabSelfHostedIDPAdded, }, { Event: org.GitLabSelfHostedIDPChangedEventType, - Reduce: p.reduceGitLabSelfHostedIDPRelationalChanged, + Reduce: p.reduceGitLabSelfHostedIDPChanged, }, { Event: org.GoogleIDPAddedEventType, - Reduce: p.reduceGoogleIDPRelationalAdded, + Reduce: p.reduceGoogleIDPAdded, }, { Event: org.GoogleIDPChangedEventType, - Reduce: p.reduceGoogleIDPRelationalChanged, + Reduce: p.reduceGoogleIDPChanged, }, { Event: org.LDAPIDPAddedEventType, @@ -349,7 +340,7 @@ func (p *idpTemplateRelationalProjection) Reducers() []handler.AggregateReducer } } -func (p *idpTemplateRelationalProjection) reduceIDPRelationalAdded(event eventstore.Event) (*handler.Statement, error) { +func (p *idpTemplateRelationalProjection) reduceIDPAdded(event eventstore.Event) (*handler.Statement, error) { var orgId *string var idpEvent idpconfig.IDPConfigAddedEvent switch e := event.(type) { @@ -362,158 +353,200 @@ func (p *idpTemplateRelationalProjection) reduceIDPRelationalAdded(event eventst return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-YcUdQ", "reduce.wrong.event.type %v", []eventstore.EventType{org.IDPConfigAddedEventType, instance.IDPConfigAddedEventType}) } - return handler.NewCreateStatement( - &idpEvent, - []handler.Column{ - handler.NewCol(IDPInstanceIDCol, idpEvent.Aggregate().InstanceID), - handler.NewCol(IDPRelationalOrgIdCol, orgId), - handler.NewCol(IDPIDCol, idpEvent.ConfigID), - handler.NewCol(IDPStateCol, domain.IDPStateActive.String()), - handler.NewCol(IDPNameCol, idpEvent.Name), - handler.NewCol(IDPTemplateTypeCol, nil), - handler.NewCol(IDPRelationalAutoRegisterCol, idpEvent.AutoRegister), - handler.NewCol(IDPRelationalAllowCreationCol, true), - handler.NewCol(IDPRelationalAllowAutoUpdateCol, false), - handler.NewCol(IDPRelationalAllowLinkingCol, true), - handler.NewCol(IDPRelationalAllowAutoLinkingCol, nil), - handler.NewCol(IDPStylingTypeCol, idpEvent.StylingType), - handler.NewCol(CreatedAt, idpEvent.CreationDate()), - handler.NewCol(UpdatedAt, idpEvent.CreationDate()), - }, - ), nil + return handler.NewStatement(event, func(ctx context.Context, ex handler.Executer, _ string) error { + tx, ok := ex.(*sql.Tx) + if !ok { + return zerrors.ThrowInvalidArgumentf(nil, "HANDL-iZGH3", "reduce.wrong.db.pool %T", ex) + } + repo := repository.IDProviderRepository() + return repo.Create(ctx, v3_sql.SQLTx(tx), &domain.IdentityProvider{ + InstanceID: event.Aggregate().InstanceID, + OrgID: orgId, + ID: idpEvent.ConfigID, + State: domain.IDPStateActive, + Name: idpEvent.Name, + Type: mapIDPConfigType(idpEvent.Typ), + AllowAutoCreation: idpEvent.AutoRegister, + AllowLinking: true, + AllowCreation: true, + CreatedAt: event.CreatedAt(), + UpdatedAt: event.CreatedAt(), + }) + + }), nil } -func (p *idpTemplateRelationalProjection) reduceIDPRelationalChanged(event eventstore.Event) (*handler.Statement, error) { - var orgCond handler.Condition +func mapIDPConfigType(typ internal_domain.IDPConfigType) *domain.IDPType { + switch typ { + case internal_domain.IDPConfigTypeOIDC: + return gu.Ptr(domain.IDPTypeOIDC) + case internal_domain.IDPConfigTypeSAML: + return gu.Ptr(domain.IDPTypeSAML) + case internal_domain.IDPConfigTypeJWT: + return gu.Ptr(domain.IDPTypeJWT) + case internal_domain.IDPConfigTypeUnspecified: + fallthrough + default: + return nil + } +} + +func mapAutoLinkingField(option internal_domain.AutoLinkingOption) *domain.IDPAutoLinkingField { + switch option { + case internal_domain.AutoLinkingOptionEmail: + return gu.Ptr(domain.IDPAutoLinkingFieldEmail) + case internal_domain.AutoLinkingOptionUsername: + return gu.Ptr(domain.IDPAutoLinkingFieldUsername) + case internal_domain.AutoLinkingOptionUnspecified: + fallthrough + default: + return nil + } +} + +func idpScopedCondition(repo domain.IDProviderRepository, instanceID, id string, orgID *string) database.Condition { + return database.And( + repo.PrimaryKeyCondition(instanceID, id), + repo.OrgIDCondition(orgID), + ) +} + +func (p *idpTemplateRelationalProjection) reduceIDPChanged(event eventstore.Event) (*handler.Statement, error) { + var orgID *string var idpEvent idpconfig.IDPConfigChangedEvent switch e := event.(type) { case *org.IDPConfigChangedEvent: idpEvent = e.IDPConfigChangedEvent - orgCond = handler.NewCond(IDPRelationalOrgId, idpEvent.Aggregate().ResourceOwner) + orgID = gu.Ptr(idpEvent.Aggregate().ResourceOwner) case *instance.IDPConfigChangedEvent: idpEvent = e.IDPConfigChangedEvent - orgCond = handler.NewIsNullCond((IDPRelationalOrgId)) default: return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-YVvJD", "reduce.wrong.event.type %v", []eventstore.EventType{org.IDPConfigChangedEventType, instance.IDPConfigChangedEventType}) } - columns := make([]handler.Column, 0, 4) + repo := repository.IDProviderRepository() + + changes := make(database.Changes, 0, 3) if idpEvent.Name != nil { - columns = append(columns, handler.NewCol(IDPNameCol, *idpEvent.Name)) - } - if idpEvent.StylingType != nil { - columns = append(columns, handler.NewCol(IDPStylingTypeCol, *idpEvent.StylingType)) + changes = append(changes, repo.SetName(*idpEvent.Name)) } if idpEvent.AutoRegister != nil { - columns = append(columns, handler.NewCol(IDPRelationalAutoRegisterCol, *idpEvent.AutoRegister)) + changes = append(changes, repo.SetAllowAutoCreation(*idpEvent.AutoRegister)) } - if len(columns) == 0 { + if len(changes) == 0 { return handler.NewNoOpStatement(&idpEvent), nil } + changes = append(changes, repo.SetUpdatedAt(gu.Ptr(event.CreatedAt()))) - columns = append(columns, handler.NewCol(UpdatedAt, idpEvent.CreationDate())) + return handler.NewStatement(event, func(ctx context.Context, ex handler.Executer, _ string) error { + tx, ok := ex.(*sql.Tx) + if !ok { + return zerrors.ThrowInvalidArgumentf(nil, "HANDL-9sX8h", "reduce.wrong.db.pool %T", ex) + } - return handler.NewUpdateStatement( - &idpEvent, - columns, - []handler.Condition{ - handler.NewCond(IDPIDCol, idpEvent.ConfigID), - handler.NewCond(IDPInstanceIDCol, idpEvent.Aggregate().InstanceID), - orgCond, - }, - ), nil + _, err := repo.Update(ctx, v3_sql.SQLTx(tx), idpScopedCondition(repo, event.Aggregate().InstanceID, idpEvent.ConfigID, orgID), changes...) + return err + }), nil } -func (p *idpTemplateRelationalProjection) reduceIDRelationalPDeactivated(event eventstore.Event) (*handler.Statement, error) { - var orgCond handler.Condition +func (p *idpTemplateRelationalProjection) reduceIDPDeactivated(event eventstore.Event) (*handler.Statement, error) { + var orgID *string var idpEvent idpconfig.IDPConfigDeactivatedEvent switch e := event.(type) { case *org.IDPConfigDeactivatedEvent: idpEvent = e.IDPConfigDeactivatedEvent - orgCond = handler.NewCond(IDPRelationalOrgId, idpEvent.Aggregate().ResourceOwner) + orgID = gu.Ptr(idpEvent.Aggregate().ResourceOwner) case *instance.IDPConfigDeactivatedEvent: idpEvent = e.IDPConfigDeactivatedEvent - orgCond = handler.NewIsNullCond((IDPRelationalOrgId)) default: return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-Y4O5l", "reduce.wrong.event.type %v", []eventstore.EventType{org.IDPConfigDeactivatedEventType, instance.IDPConfigDeactivatedEventType}) } - return handler.NewUpdateStatement( - &idpEvent, - []handler.Column{ - handler.NewCol(IDPStateCol, domain.IDPStateInactive), - handler.NewCol(UpdatedAt, idpEvent.CreationDate()), - }, - []handler.Condition{ - handler.NewCond(IDPIDCol, idpEvent.ConfigID), - handler.NewCond(IDPInstanceIDCol, idpEvent.Aggregate().InstanceID), - orgCond, - }, - ), nil + return handler.NewStatement(event, func(ctx context.Context, ex handler.Executer, _ string) error { + tx, ok := ex.(*sql.Tx) + if !ok { + return zerrors.ThrowInvalidArgumentf(nil, "HANDL-9sX8h", "reduce.wrong.db.pool %T", ex) + } + + repo := repository.IDProviderRepository() + + _, err := repo.Update(ctx, v3_sql.SQLTx(tx), idpScopedCondition(repo, event.Aggregate().InstanceID, idpEvent.ConfigID, orgID), + repo.SetState(domain.IDPStateInactive), + repo.SetUpdatedAt(gu.Ptr(event.CreatedAt())), + ) + return err + }), nil } -func (p *idpTemplateRelationalProjection) reduceIDPRelationalReactivated(event eventstore.Event) (*handler.Statement, error) { - var orgCond handler.Condition +func (p *idpTemplateRelationalProjection) reduceIDPReactivated(event eventstore.Event) (*handler.Statement, error) { + var orgID *string var idpEvent idpconfig.IDPConfigReactivatedEvent switch e := event.(type) { case *org.IDPConfigReactivatedEvent: idpEvent = e.IDPConfigReactivatedEvent - orgCond = handler.NewCond(IDPRelationalOrgId, idpEvent.Aggregate().ResourceOwner) + orgID = gu.Ptr(idpEvent.Aggregate().ResourceOwner) case *instance.IDPConfigReactivatedEvent: idpEvent = e.IDPConfigReactivatedEvent - orgCond = handler.NewIsNullCond((IDPRelationalOrgId)) default: return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-Y8QyS", "reduce.wrong.event.type %v", []eventstore.EventType{org.IDPConfigReactivatedEventType, instance.IDPConfigReactivatedEventType}) } - return handler.NewUpdateStatement( - &idpEvent, - []handler.Column{ - handler.NewCol(IDPStateCol, domain.IDPStateActive.String()), - handler.NewCol(UpdatedAt, idpEvent.CreationDate()), - }, - []handler.Condition{ - handler.NewCond(IDPIDCol, idpEvent.ConfigID), - handler.NewCond(IDPInstanceIDCol, idpEvent.Aggregate().InstanceID), - orgCond, - }, - ), nil + return handler.NewStatement(event, func(ctx context.Context, ex handler.Executer, _ string) error { + tx, ok := ex.(*sql.Tx) + if !ok { + return zerrors.ThrowInvalidArgumentf(nil, "HANDL-2Db9P", "reduce.wrong.db.pool %T", ex) + } + + repo := repository.IDProviderRepository() + _, err := repo.Update(ctx, v3_sql.SQLTx(tx), idpScopedCondition(repo, idpEvent.Aggregate().InstanceID, idpEvent.ConfigID, orgID), + repo.SetState(domain.IDPStateActive), + repo.SetUpdatedAt(gu.Ptr(event.CreatedAt())), + ) + return err + }), nil } -func (p *idpTemplateRelationalProjection) reduceIDPRelationalRemoved(event eventstore.Event) (*handler.Statement, error) { - var orgCond handler.Condition - var idpEvent idpconfig.IDPConfigRemovedEvent +func (p *idpTemplateRelationalProjection) reduceIDPRemoved(event eventstore.Event) (*handler.Statement, error) { + var ( + orgID *string + idpID string + ) switch e := event.(type) { + case *org.IDPRemovedEvent: + idpID = e.ID + orgID = gu.Ptr(e.RemovedEvent.Aggregate().ResourceOwner) + case *instance.IDPRemovedEvent: + idpID = e.ID case *org.IDPConfigRemovedEvent: - idpEvent = e.IDPConfigRemovedEvent - orgCond = handler.NewCond(IDPRelationalOrgId, idpEvent.Aggregate().ResourceOwner) + idpID = e.ConfigID + orgID = gu.Ptr(e.IDPConfigRemovedEvent.Aggregate().ResourceOwner) case *instance.IDPConfigRemovedEvent: - idpEvent = e.IDPConfigRemovedEvent - orgCond = handler.NewIsNullCond((IDPRelationalOrgId)) + idpID = e.ConfigID default: - return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-Y4zy8", "reduce.wrong.event.type %v", []eventstore.EventType{org.IDPConfigRemovedEventType, instance.IDPConfigRemovedEventType}) + return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-Ybcvwin2", "reduce.wrong.event.type %v", []eventstore.EventType{org.IDPRemovedEventType, instance.IDPRemovedEventType, org.IDPConfigRemovedEventType, instance.IDPConfigRemovedEventType}) } - return handler.NewDeleteStatement( - &idpEvent, - []handler.Condition{ - handler.NewCond(IDPIDCol, idpEvent.ConfigID), - handler.NewCond(IDPInstanceIDCol, idpEvent.Aggregate().InstanceID), - orgCond, - }, - ), nil + return handler.NewStatement(event, func(ctx context.Context, ex handler.Executer, _ string) error { + tx, ok := ex.(*sql.Tx) + if !ok { + return zerrors.ThrowInvalidArgumentf(nil, "HANDL-PSj7F", "reduce.wrong.db.pool %T", ex) + } + + repo := repository.IDProviderRepository() + _, err := repo.Delete(ctx, v3_sql.SQLTx(tx), idpScopedCondition(repo, event.Aggregate().InstanceID, idpID, orgID)) + return err + }), nil } -func (p *idpTemplateRelationalProjection) reduceOIDCRelationalConfigAdded(event eventstore.Event) (*handler.Statement, error) { - var orgCond handler.Condition +func (p *idpTemplateRelationalProjection) reduceOIDCConfigAdded(event eventstore.Event) (*handler.Statement, error) { + var orgID *string var idpEvent idpconfig.OIDCConfigAddedEvent switch e := event.(type) { case *org.IDPOIDCConfigAddedEvent: idpEvent = e.OIDCConfigAddedEvent - orgCond = handler.NewCond(IDPRelationalOrgId, idpEvent.Aggregate().ResourceOwner) + orgID = gu.Ptr(idpEvent.Aggregate().ResourceOwner) case *instance.IDPOIDCConfigAddedEvent: idpEvent = e.OIDCConfigAddedEvent - orgCond = handler.NewIsNullCond((IDPRelationalOrgId)) default: return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-YFuAA", "reduce.wrong.event.type %v", []eventstore.EventType{org.IDPOIDCConfigAddedEventType, instance.IDPOIDCConfigAddedEventType}) } @@ -523,104 +556,108 @@ func (p *idpTemplateRelationalProjection) reduceOIDCRelationalConfigAdded(event return nil, err } - return handler.NewUpdateStatement( - &idpEvent, - []handler.Column{ - handler.NewCol(IDPRelationalPayloadCol, payloadJSON), - handler.NewCol(IDPTypeCol, domain.IDPTypeOIDC), - handler.NewCol(UpdatedAt, idpEvent.CreatedAt()), - }, - []handler.Condition{ - handler.NewCond(IDPIDCol, idpEvent.IDPConfigID), - handler.NewCond(IDPInstanceIDCol, idpEvent.Aggregate().InstanceID), - orgCond, - }, - ), nil + return handler.NewStatement(event, func(ctx context.Context, ex handler.Executer, _ string) error { + tx, ok := ex.(*sql.Tx) + if !ok { + return zerrors.ThrowInvalidArgumentf(nil, "HANDL-5cvzY", "reduce.wrong.db.pool %T", ex) + } + + repo := repository.IDProviderRepository() + _, err = repo.Update(ctx, v3_sql.SQLTx(tx), idpScopedCondition(repo, idpEvent.Aggregate().InstanceID, idpEvent.IDPConfigID, orgID), + repo.SetPayload(string(payloadJSON)), + repo.SetType(domain.IDPTypeOIDC), + repo.SetUpdatedAt(gu.Ptr(event.CreatedAt())), + ) + return err + }), nil } -func (p *idpTemplateRelationalProjection) reduceOIDCRelationalConfigChanged(event eventstore.Event) (*handler.Statement, error) { +func (p *idpTemplateRelationalProjection) reduceOIDCConfigChanged(event eventstore.Event) (*handler.Statement, error) { var orgId *string - var orgCond handler.Condition var idpEvent idpconfig.OIDCConfigChangedEvent switch e := event.(type) { case *org.IDPOIDCConfigChangedEvent: idpEvent = e.OIDCConfigChangedEvent orgId = &idpEvent.Aggregate().ResourceOwner - orgCond = handler.NewCond(IDPRelationalOrgId, orgId) case *instance.IDPOIDCConfigChangedEvent: idpEvent = e.OIDCConfigChangedEvent - orgCond = handler.NewIsNullCond((IDPRelationalOrgId)) default: return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-Y2IVI", "reduce.wrong.event.type %v", []eventstore.EventType{org.IDPOIDCConfigChangedEventType, instance.IDPOIDCConfigChangedEventType}) } - return handler.NewStatement(event, func(ctx context.Context, ex handler.Executer, projectionName string) error { + return handler.NewStatement(event, func(ctx context.Context, ex handler.Executer, _ string) error { tx, ok := ex.(*sql.Tx) if !ok { return zerrors.ThrowInternal(nil, "HANDL-sh6Lp", "unable to cast to tx executer") } - oidc, err := p.idpRepo.GetOIDC(ctx, v3_sql.SQLTx(tx), p.idpRepo.IDCondition(idpEvent.IDPConfigID), idpEvent.Agg.InstanceID, orgId) + idpRepo := repository.IDProviderRepository() + oidc, err := idpRepo.GetOIDC(ctx, v3_sql.SQLTx(tx), database.WithCondition(idpScopedCondition(idpRepo, idpEvent.Agg.InstanceID, idpEvent.IDPConfigID, orgId))) if err != nil { return err } - if idpEvent.ClientID != nil { + if idpEvent.ClientID != nil && *idpEvent.ClientID != oidc.ClientID { oidc.ClientID = *idpEvent.ClientID } if idpEvent.ClientSecret != nil { oidc.ClientSecret = idpEvent.ClientSecret } - if idpEvent.Issuer != nil { + if idpEvent.Issuer != nil && *idpEvent.Issuer != oidc.Issuer { oidc.Issuer = *idpEvent.Issuer } - if idpEvent.AuthorizationEndpoint != nil { + if idpEvent.AuthorizationEndpoint != nil && *idpEvent.AuthorizationEndpoint != oidc.AuthorizationEndpoint { oidc.AuthorizationEndpoint = *idpEvent.AuthorizationEndpoint } - if idpEvent.TokenEndpoint != nil { + if idpEvent.TokenEndpoint != nil && *idpEvent.TokenEndpoint != oidc.TokenEndpoint { oidc.TokenEndpoint = *idpEvent.TokenEndpoint } - if idpEvent.Scopes != nil { + if idpEvent.Scopes != nil && !slices.Equal(idpEvent.Scopes, oidc.Scopes) { oidc.Scopes = idpEvent.Scopes } if idpEvent.IDPDisplayNameMapping != nil { - oidc.IDPDisplayNameMapping = domain.OIDCMappingField(*idpEvent.IDPDisplayNameMapping) + oidc.IDPDisplayNameMapping = mapOIDCMappingField(*idpEvent.IDPDisplayNameMapping) } if idpEvent.UserNameMapping != nil { - oidc.UserNameMapping = domain.OIDCMappingField(*idpEvent.UserNameMapping) + oidc.UserNameMapping = mapOIDCMappingField(*idpEvent.UserNameMapping) } - payloadJSON, err := json.Marshal(idpEvent) + payloadJSON, err := json.Marshal(oidc.OIDC) if err != nil { return err } - return handler.NewUpdateStatement( - &idpEvent, - []handler.Column{ - handler.NewCol(IDPRelationalPayloadCol, payloadJSON), - handler.NewCol(IDPTypeCol, domain.IDPTypeOIDC), - handler.NewCol(UpdatedAt, idpEvent.CreationDate()), - }, - []handler.Condition{ - handler.NewCond(IDPIDCol, idpEvent.IDPConfigID), - handler.NewCond(IDPInstanceIDCol, idpEvent.Aggregate().InstanceID), - orgCond, - }, - ).Execute(ctx, ex, projectionName) + _, err = idpRepo.Update(ctx, v3_sql.SQLTx(tx), idpScopedCondition(idpRepo, idpEvent.Aggregate().InstanceID, idpEvent.IDPConfigID, orgId), + idpRepo.SetPayload(string(payloadJSON)), + idpRepo.SetType(domain.IDPTypeOIDC), + idpRepo.SetUpdatedAt(gu.Ptr(event.CreatedAt())), + ) + return err }), nil } -func (p *idpTemplateRelationalProjection) reduceJWTRelationalConfigAdded(event eventstore.Event) (*handler.Statement, error) { - var orgCond handler.Condition +func mapOIDCMappingField(field internal_domain.OIDCMappingField) domain.OIDCMappingField { + switch field { + case internal_domain.OIDCMappingFieldEmail: + return domain.OIDCMappingFieldEmail + case internal_domain.OIDCMappingFieldPreferredLoginName: + return domain.OIDCMappingFieldPreferredLoginName + case internal_domain.OIDCMappingFieldUnspecified: + fallthrough + default: + return domain.OIDCMappingFieldUnspecified + } +} + +func (p *idpTemplateRelationalProjection) reduceJWTConfigAdded(event eventstore.Event) (*handler.Statement, error) { + var orgID *string var idpEvent idpconfig.JWTConfigAddedEvent switch e := event.(type) { case *org.IDPJWTConfigAddedEvent: idpEvent = e.JWTConfigAddedEvent - orgCond = handler.NewCond(IDPRelationalOrgId, idpEvent.Aggregate().ResourceOwner) + orgID = gu.Ptr(idpEvent.Aggregate().ResourceOwner) case *instance.IDPJWTConfigAddedEvent: idpEvent = e.JWTConfigAddedEvent - orgCond = handler.NewIsNullCond((IDPRelationalOrgId)) default: return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-YvPdb", "reduce.wrong.event.type %v", []eventstore.EventType{org.IDPJWTConfigAddedEventType, instance.IDPJWTConfigAddedEventType}) } @@ -630,83 +667,75 @@ func (p *idpTemplateRelationalProjection) reduceJWTRelationalConfigAdded(event e return nil, err } - return handler.NewUpdateStatement( - &idpEvent, - []handler.Column{ - handler.NewCol(IDPRelationalPayloadCol, payloadJSON), - handler.NewCol(IDPTypeCol, domain.IDPTypeJWT), - handler.NewCol(UpdatedAt, idpEvent.CreatedAt()), - }, - []handler.Condition{ - handler.NewCond(IDPIDCol, idpEvent.IDPConfigID), - handler.NewCond(IDPInstanceIDCol, idpEvent.Aggregate().InstanceID), - orgCond, - }, - ), nil + return handler.NewStatement(event, func(ctx context.Context, ex handler.Executer, _ string) error { + tx, ok := ex.(*sql.Tx) + if !ok { + return zerrors.ThrowInvalidArgumentf(nil, "HANDL-tJQ8V", "reduce.wrong.db.pool %T", ex) + } + + repo := repository.IDProviderRepository() + _, err = repo.Update(ctx, v3_sql.SQLTx(tx), idpScopedCondition(repo, idpEvent.Aggregate().InstanceID, idpEvent.IDPConfigID, orgID), + repo.SetPayload(string(payloadJSON)), + repo.SetType(domain.IDPTypeJWT), + repo.SetUpdatedAt(gu.Ptr(event.CreatedAt())), + ) + return err + }), nil } -func (p *idpTemplateRelationalProjection) reduceJWTRelationalConfigChanged(event eventstore.Event) (*handler.Statement, error) { +func (p *idpTemplateRelationalProjection) reduceJWTConfigChanged(event eventstore.Event) (*handler.Statement, error) { var orgId *string - var orgCond handler.Condition var idpEvent idpconfig.JWTConfigChangedEvent switch e := event.(type) { case *org.IDPJWTConfigChangedEvent: idpEvent = e.JWTConfigChangedEvent orgId = &idpEvent.Aggregate().ResourceOwner - orgCond = handler.NewCond(IDPRelationalOrgId, orgId) case *instance.IDPJWTConfigChangedEvent: idpEvent = e.JWTConfigChangedEvent - orgCond = handler.NewIsNullCond((IDPRelationalOrgId)) default: return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-Y2IVI", "reduce.wrong.event.type %v", []eventstore.EventType{org.IDPJWTConfigChangedEventType, instance.IDPJWTConfigChangedEventType}) } - return handler.NewStatement(event, func(ctx context.Context, ex handler.Executer, projectionName string) error { + return handler.NewStatement(event, func(ctx context.Context, ex handler.Executer, _ string) error { tx, ok := ex.(*sql.Tx) if !ok { return zerrors.ThrowInternal(nil, "HANDL-sh6Lp", "unable to cast to tx executer") } - jwt, err := p.idpRepo.GetJWT(ctx, v3_sql.SQLTx(tx), p.idpRepo.IDCondition(idpEvent.IDPConfigID), idpEvent.Agg.InstanceID, orgId) + idpRepo := repository.IDProviderRepository() + jwt, err := idpRepo.GetJWT(ctx, v3_sql.SQLTx(tx), database.WithCondition(idpScopedCondition(idpRepo, idpEvent.Agg.InstanceID, idpEvent.IDPConfigID, orgId))) if err != nil { return err } - if idpEvent.JWTEndpoint != nil { + if idpEvent.JWTEndpoint != nil && *idpEvent.JWTEndpoint != jwt.JWTEndpoint { jwt.JWTEndpoint = *idpEvent.JWTEndpoint } - if idpEvent.Issuer != nil { + if idpEvent.Issuer != nil && *idpEvent.Issuer != jwt.Issuer { jwt.Issuer = *idpEvent.Issuer } - if idpEvent.KeysEndpoint != nil { + if idpEvent.KeysEndpoint != nil && *idpEvent.KeysEndpoint != jwt.KeysEndpoint { jwt.KeysEndpoint = *idpEvent.KeysEndpoint } - if idpEvent.HeaderName != nil { + if idpEvent.HeaderName != nil && *idpEvent.HeaderName != jwt.HeaderName { jwt.HeaderName = *idpEvent.HeaderName } - payloadJSON, err := json.Marshal(idpEvent) + payloadJSON, err := json.Marshal(jwt.JWT) if err != nil { return err } - return handler.NewUpdateStatement( - &idpEvent, - []handler.Column{ - handler.NewCol(IDPRelationalPayloadCol, payloadJSON), - handler.NewCol(IDPTypeCol, domain.IDPTypeJWT), - handler.NewCol(UpdatedAt, idpEvent.CreationDate()), - }, - []handler.Condition{ - handler.NewCond(IDPIDCol, idpEvent.IDPConfigID), - handler.NewCond(IDPInstanceIDCol, idpEvent.Aggregate().InstanceID), - orgCond, - }, - ).Execute(ctx, ex, projectionName) + _, err = idpRepo.Update(ctx, v3_sql.SQLTx(tx), idpScopedCondition(idpRepo, idpEvent.Aggregate().InstanceID, idpEvent.IDPConfigID, orgId), + idpRepo.SetPayload(string(payloadJSON)), + idpRepo.SetType(domain.IDPTypeJWT), + idpRepo.SetUpdatedAt(gu.Ptr(event.CreatedAt())), + ) + return err }), nil } -func (p *idpTemplateRelationalProjection) reduceOAuthIDPRelationalAdded(event eventstore.Event) (*handler.Statement, error) { +func (p *idpTemplateRelationalProjection) reduceOAuthIDPAdded(event eventstore.Event) (*handler.Statement, error) { var orgId *string var idpEvent idp.OAuthIDPAddedEvent switch e := event.(type) { @@ -735,86 +764,76 @@ func (p *idpTemplateRelationalProjection) reduceOAuthIDPRelationalAdded(event ev return nil, err } - return handler.NewCreateStatement( - &idpEvent, - []handler.Column{ - handler.NewCol(IDPTemplateIDCol, idpEvent.ID), - handler.NewCol(IDPRelationalOrgId, orgId), - handler.NewCol(IDPTemplateInstanceIDCol, idpEvent.Aggregate().InstanceID), - handler.NewCol(IDPTemplateStateCol, domain.IDPStateActive.String()), - handler.NewCol(IDPTemplateNameCol, idpEvent.Name), - handler.NewCol(IDPTemplateTypeCol, domain.IDPTypeOAuth), - handler.NewCol(IDPRelationalAllowCreationCol, idpEvent.IsCreationAllowed), - handler.NewCol(IDPRelationalAllowLinkingCol, idpEvent.IsLinkingAllowed), - handler.NewCol(IDPRelationalAllowAutoCreationCol, idpEvent.IsAutoCreation), - handler.NewCol(IDPRelationalAllowAutoUpdateCol, idpEvent.IsAutoUpdate), - handler.NewCol(IDPRelationalAllowAutoLinkingCol, func() any { - if idpEvent.AutoLinkingOption == internal_domain.AutoLinkingOptionUnspecified { - return nil - } - return domain.IDPAutoLinkingField(idpEvent.AutoLinkingOption) - }()), - handler.NewCol(IDPRelationalPayloadCol, payloadJSON), - handler.NewCol(CreatedAt, idpEvent.CreationDate()), - handler.NewCol(UpdatedAt, idpEvent.CreationDate()), - }, - ), nil + return handler.NewStatement(event, func(ctx context.Context, ex handler.Executer, _ string) error { + tx, ok := ex.(*sql.Tx) + if !ok { + return zerrors.ThrowInvalidArgumentf(nil, "HANDL-mB2hq", "reduce.wrong.db.pool %T", ex) + } + + repo := repository.IDProviderRepository() + return repo.Create(ctx, v3_sql.SQLTx(tx), &domain.IdentityProvider{ + InstanceID: idpEvent.Aggregate().InstanceID, + OrgID: orgId, + ID: idpEvent.ID, + State: domain.IDPStateActive, + Name: idpEvent.Name, + Type: gu.Ptr(domain.IDPTypeOAuth), + AllowCreation: idpEvent.IsCreationAllowed, + AllowLinking: idpEvent.IsLinkingAllowed, + AllowAutoCreation: idpEvent.IsAutoCreation, + AllowAutoUpdate: idpEvent.IsAutoUpdate, + AutoLinkingField: mapAutoLinkingField(idpEvent.AutoLinkingOption), + Payload: payloadJSON, + CreatedAt: event.CreatedAt(), + UpdatedAt: event.CreatedAt(), + }) + }), nil } -func (p *idpTemplateRelationalProjection) reduceOAuthIDPRelationalChanged(event eventstore.Event) (*handler.Statement, error) { +func (p *idpTemplateRelationalProjection) reduceOAuthIDPChanged(event eventstore.Event) (*handler.Statement, error) { var orgId *string - var orgCond handler.Condition var idpEvent idp.OAuthIDPChangedEvent switch e := event.(type) { case *org.OAuthIDPChangedEvent: idpEvent = e.OAuthIDPChangedEvent orgId = &idpEvent.Aggregate().ResourceOwner - orgCond = handler.NewCond(IDPRelationalOrgId, orgId) case *instance.OAuthIDPChangedEvent: idpEvent = e.OAuthIDPChangedEvent - orgCond = handler.NewIsNullCond((IDPRelationalOrgId)) default: return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-K1582ks", "reduce.wrong.event.type %v", []eventstore.EventType{org.OAuthIDPChangedEventType, instance.OAuthIDPChangedEventType}) } - return handler.NewStatement(event, func(ctx context.Context, ex handler.Executer, projectionName string) error { + return handler.NewStatement(event, func(ctx context.Context, ex handler.Executer, _ string) error { tx, ok := ex.(*sql.Tx) if !ok { return zerrors.ThrowInternal(nil, "HANDL-HX6ed", "unable to cast to tx executer") } - oauth, err := p.idpRepo.GetOAuth(ctx, v3_sql.SQLTx(tx), p.idpRepo.IDCondition(idpEvent.ID), idpEvent.Agg.InstanceID, orgId) + idpRepo := repository.IDProviderRepository() + oauth, err := idpRepo.GetOAuth(ctx, v3_sql.SQLTx(tx), database.WithCondition(idpScopedCondition(idpRepo, idpEvent.Agg.InstanceID, idpEvent.ID, orgId))) if err != nil { return err } - columns := reduceIDPRelationalChangedTemplateColumns(idpEvent.Name, idpEvent.OptionChanges) + changes := p.reduceIDPChangedTemplateColumns(idpRepo, idpEvent.Name, idpEvent.OptionChanges) payload := &oauth.OAuth - payloadChanged := reduceOAuthIDPRelationalChangedColumns(payload, &idpEvent) + payloadChanged := p.reduceOAuthIDPChangedColumns(payload, &idpEvent) if payloadChanged { payloadJSON, err := json.Marshal(payload) if err != nil { return err } - columns = append(columns, handler.NewCol(IDPRelationalPayloadCol, payloadJSON)) + changes = append(changes, idpRepo.SetPayload(string(payloadJSON))) } - columns = append(columns, handler.NewCol(UpdatedAt, idpEvent.CreationDate())) - - return handler.NewUpdateStatement( - &idpEvent, - columns, - []handler.Condition{ - handler.NewCond(IDPTemplateIDCol, idpEvent.ID), - handler.NewCond(IDPTemplateInstanceIDCol, idpEvent.Aggregate().InstanceID), - orgCond, - }, - ).Execute(ctx, ex, projectionName) + changes = append(changes, idpRepo.SetUpdatedAt(gu.Ptr(event.CreatedAt()))) + _, err = idpRepo.Update(ctx, v3_sql.SQLTx(tx), idpScopedCondition(idpRepo, idpEvent.Aggregate().InstanceID, idpEvent.ID, orgId), changes...) + return err }), nil } -func (p *idpTemplateRelationalProjection) reduceOIDCIDPRelationalAdded(event eventstore.Event) (*handler.Statement, error) { +func (p *idpTemplateRelationalProjection) reduceOIDCIDPAdded(event eventstore.Event) (*handler.Statement, error) { var orgId *string var idpEvent idp.OIDCIDPAddedEvent switch e := event.(type) { @@ -832,94 +851,83 @@ func (p *idpTemplateRelationalProjection) reduceOIDCIDPRelationalAdded(event eve return nil, err } - return handler.NewCreateStatement( - &idpEvent, - []handler.Column{ - handler.NewCol(IDPTemplateIDCol, idpEvent.ID), - handler.NewCol(IDPTemplateInstanceIDCol, idpEvent.Aggregate().InstanceID), - handler.NewCol(IDPRelationalOrgId, orgId), - handler.NewCol(IDPTemplateStateCol, domain.IDPStateActive), - handler.NewCol(IDPTemplateNameCol, idpEvent.Name), - handler.NewCol(IDPTemplateTypeCol, domain.IDPTypeOIDC), - handler.NewCol(IDPRelationalAllowCreationCol, idpEvent.IsCreationAllowed), - handler.NewCol(IDPRelationalAllowLinkingCol, idpEvent.IsLinkingAllowed), - handler.NewCol(IDPRelationalAllowAutoCreationCol, idpEvent.IsAutoCreation), - handler.NewCol(IDPRelationalAllowAutoUpdateCol, idpEvent.IsAutoUpdate), - handler.NewCol(IDPRelationalAllowAutoLinkingCol, func() any { - if idpEvent.AutoLinkingOption == internal_domain.AutoLinkingOptionUnspecified { - return nil - } - return domain.IDPAutoLinkingField(idpEvent.AutoLinkingOption) - }()), - handler.NewCol(IDPRelationalPayloadCol, payloadJSON), - handler.NewCol(CreatedAt, idpEvent.CreationDate()), - handler.NewCol(UpdatedAt, idpEvent.CreationDate()), - }, - ), nil + return handler.NewStatement(event, func(ctx context.Context, ex handler.Executer, _ string) error { + tx, ok := ex.(*sql.Tx) + if !ok { + return zerrors.ThrowInvalidArgumentf(nil, "HANDL-C9ju3", "reduce.wrong.db.pool %T", ex) + } + + repo := repository.IDProviderRepository() + return repo.Create(ctx, v3_sql.SQLTx(tx), &domain.IdentityProvider{ + InstanceID: idpEvent.Aggregate().InstanceID, + OrgID: orgId, + ID: idpEvent.ID, + State: domain.IDPStateActive, + Name: idpEvent.Name, + Type: gu.Ptr(domain.IDPTypeOIDC), + AllowCreation: idpEvent.IsCreationAllowed, + AllowAutoCreation: idpEvent.IsAutoCreation, + AllowAutoUpdate: idpEvent.IsAutoUpdate, + AllowLinking: idpEvent.IsLinkingAllowed, + AutoLinkingField: mapAutoLinkingField(idpEvent.AutoLinkingOption), + Payload: payloadJSON, + CreatedAt: event.CreatedAt(), + UpdatedAt: event.CreatedAt(), + }) + }), nil } -func (p *idpTemplateRelationalProjection) reduceOIDCIDPRelationalChanged(event eventstore.Event) (*handler.Statement, error) { +func (p *idpTemplateRelationalProjection) reduceOIDCIDPChanged(event eventstore.Event) (*handler.Statement, error) { var orgId *string - var orgCond handler.Condition var idpEvent idp.OIDCIDPChangedEvent switch e := event.(type) { case *org.OIDCIDPChangedEvent: idpEvent = e.OIDCIDPChangedEvent orgId = &idpEvent.Aggregate().ResourceOwner - orgCond = handler.NewCond(IDPRelationalOrgId, orgId) case *instance.OIDCIDPChangedEvent: idpEvent = e.OIDCIDPChangedEvent - orgCond = handler.NewIsNullCond((IDPRelationalOrgId)) default: return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-Y1K82ks", "reduce.wrong.event.type %v", []eventstore.EventType{org.OIDCIDPChangedEventType, instance.OIDCIDPChangedEventType}) } - return handler.NewStatement(event, func(ctx context.Context, ex handler.Executer, projectionName string) error { + return handler.NewStatement(event, func(ctx context.Context, ex handler.Executer, _ string) error { tx, ok := ex.(*sql.Tx) if !ok { return zerrors.ThrowInternal(nil, "HANDL-L8CQt", "unable to cast to tx executer") } - oidc, err := p.idpRepo.GetOIDC(ctx, v3_sql.SQLTx(tx), p.idpRepo.IDCondition(idpEvent.ID), idpEvent.Agg.InstanceID, orgId) + idpRepo := repository.IDProviderRepository() + oidc, err := idpRepo.GetOIDC(ctx, v3_sql.SQLTx(tx), database.WithCondition(idpScopedCondition(idpRepo, idpEvent.Agg.InstanceID, idpEvent.ID, orgId))) if err != nil { return err } - columns := reduceIDPRelationalChangedTemplateColumns(idpEvent.Name, idpEvent.OptionChanges) + changes := p.reduceIDPChangedTemplateColumns(idpRepo, idpEvent.Name, idpEvent.OptionChanges) payload := &oidc.OIDC - payloadChanged := reduceOIDCIDPRelationalChangedColumns(payload, &idpEvent) + payloadChanged := p.reduceOIDCIDPChangedColumns(payload, &idpEvent) if payloadChanged { payloadJSON, err := json.Marshal(payload) if err != nil { return err } - columns = append(columns, handler.NewCol(IDPRelationalPayloadCol, payloadJSON)) + changes = append(changes, idpRepo.SetPayload(string(payloadJSON))) } - columns = append(columns, handler.NewCol(UpdatedAt, idpEvent.CreationDate())) - - return handler.NewUpdateStatement( - &idpEvent, - columns, - []handler.Condition{ - handler.NewCond(IDPTemplateIDCol, idpEvent.ID), - handler.NewCond(IDPTemplateInstanceIDCol, idpEvent.Aggregate().InstanceID), - orgCond, - }, - ).Execute(ctx, ex, projectionName) + changes = append(changes, idpRepo.SetUpdatedAt(gu.Ptr(event.CreatedAt()))) + _, err = idpRepo.Update(ctx, v3_sql.SQLTx(tx), idpScopedCondition(idpRepo, idpEvent.Aggregate().InstanceID, idpEvent.ID, orgId), changes...) + return err }), nil } -func (p *idpTemplateRelationalProjection) reduceOIDCIDPRelationalMigratedAzureAD(event eventstore.Event) (*handler.Statement, error) { - var orgCond handler.Condition +func (p *idpTemplateRelationalProjection) reduceOIDCIDPMigratedAzureAD(event eventstore.Event) (*handler.Statement, error) { + var orgID *string var idpEvent idp.OIDCIDPMigratedAzureADEvent switch e := event.(type) { case *org.OIDCIDPMigratedAzureADEvent: idpEvent = e.OIDCIDPMigratedAzureADEvent - orgCond = handler.NewCond(IDPRelationalOrgId, idpEvent.Aggregate().ResourceOwner) + orgID = gu.Ptr(idpEvent.Aggregate().ResourceOwner) case *instance.OIDCIDPMigratedAzureADEvent: idpEvent = e.OIDCIDPMigratedAzureADEvent - orgCond = handler.NewIsNullCond((IDPRelationalOrgId)) default: return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-Yb582ks", "reduce.wrong.event.type %v", []eventstore.EventType{org.OIDCIDPMigratedAzureADEventType, instance.OIDCIDPMigratedAzureADEventType}) } @@ -942,42 +950,39 @@ func (p *idpTemplateRelationalProjection) reduceOIDCIDPRelationalMigratedAzureAD return nil, err } - return handler.NewUpdateStatement( - &idpEvent, - []handler.Column{ - handler.NewCol(IDPTemplateNameCol, idpEvent.Name), - handler.NewCol(IDPTemplateTypeCol, domain.IDPTypeAzure), - handler.NewCol(IDPRelationalAllowCreationCol, idpEvent.IsCreationAllowed), - handler.NewCol(IDPRelationalAllowLinkingCol, idpEvent.IsLinkingAllowed), - handler.NewCol(IDPRelationalAllowAutoCreationCol, idpEvent.IsAutoCreation), - handler.NewCol(IDPRelationalAllowAutoUpdateCol, idpEvent.IsAutoUpdate), - handler.NewCol(IDPRelationalAllowAutoLinkingCol, func() any { - if idpEvent.AutoLinkingOption == internal_domain.AutoLinkingOptionUnspecified { - return nil - } - return domain.IDPAutoLinkingField(idpEvent.AutoLinkingOption) - }()), - handler.NewCol(IDPRelationalPayloadCol, payloadJSON), - handler.NewCol(UpdatedAt, idpEvent.CreationDate()), - }, - []handler.Condition{ - handler.NewCond(IDPTemplateIDCol, idpEvent.ID), - handler.NewCond(IDPTemplateInstanceIDCol, idpEvent.Aggregate().InstanceID), - orgCond, - }, - ), nil + return handler.NewStatement(event, func(ctx context.Context, ex handler.Executer, _ string) error { + tx, ok := ex.(*sql.Tx) + if !ok { + return zerrors.ThrowInvalidArgumentf(nil, "HANDL-mj7LQ", "reduce.wrong.db.pool %T", ex) + } + + repo := repository.IDProviderRepository() + changes := database.Changes{ + repo.SetName(idpEvent.Name), + repo.SetType(domain.IDPTypeAzure), + repo.SetAllowCreation(idpEvent.IsCreationAllowed), + repo.SetAllowLinking(idpEvent.IsLinkingAllowed), + repo.SetAllowAutoCreation(idpEvent.IsAutoCreation), + repo.SetAllowAutoUpdate(idpEvent.IsAutoUpdate), + repo.SetAutoLinkingField(mapAutoLinkingField(idpEvent.AutoLinkingOption)), + repo.SetPayload(string(payloadJSON)), + repo.SetUpdatedAt(gu.Ptr(event.CreatedAt())), + } + + _, err = repo.Update(ctx, v3_sql.SQLTx(tx), idpScopedCondition(repo, idpEvent.Aggregate().InstanceID, idpEvent.ID, orgID), changes...) + return err + }), nil } -func (p *idpTemplateRelationalProjection) reduceOIDCIDPRelationalMigratedGoogle(event eventstore.Event) (*handler.Statement, error) { - var orgCond handler.Condition +func (p *idpTemplateRelationalProjection) reduceOIDCIDPMigratedGoogle(event eventstore.Event) (*handler.Statement, error) { + var orgID *string var idpEvent idp.OIDCIDPMigratedGoogleEvent switch e := event.(type) { case *org.OIDCIDPMigratedGoogleEvent: idpEvent = e.OIDCIDPMigratedGoogleEvent - orgCond = handler.NewCond(IDPRelationalOrgId, idpEvent.Aggregate().ResourceOwner) + orgID = gu.Ptr(idpEvent.Aggregate().ResourceOwner) case *instance.OIDCIDPMigratedGoogleEvent: idpEvent = e.OIDCIDPMigratedGoogleEvent - orgCond = handler.NewIsNullCond((IDPRelationalOrgId)) default: return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-Y1502hk", "reduce.wrong.event.type %v", []eventstore.EventType{org.OIDCIDPMigratedGoogleEventType, instance.OIDCIDPMigratedGoogleEventType}) } @@ -993,33 +998,31 @@ func (p *idpTemplateRelationalProjection) reduceOIDCIDPRelationalMigratedGoogle( return nil, err } - return handler.NewUpdateStatement( - &idpEvent, - []handler.Column{ - handler.NewCol(IDPTemplateNameCol, idpEvent.Name), - handler.NewCol(IDPTemplateTypeCol, domain.IDPTypeGoogle), - handler.NewCol(IDPRelationalAllowCreationCol, idpEvent.IsCreationAllowed), - handler.NewCol(IDPRelationalAllowLinkingCol, idpEvent.IsLinkingAllowed), - handler.NewCol(IDPRelationalAllowAutoCreationCol, idpEvent.IsAutoCreation), - handler.NewCol(IDPRelationalAllowAutoUpdateCol, idpEvent.IsAutoUpdate), - handler.NewCol(IDPRelationalAllowAutoLinkingCol, func() any { - if idpEvent.AutoLinkingOption == internal_domain.AutoLinkingOptionUnspecified { - return nil - } - return domain.IDPAutoLinkingField(idpEvent.AutoLinkingOption) - }()), - handler.NewCol(IDPRelationalPayloadCol, payloadJSON), - handler.NewCol(UpdatedAt, idpEvent.CreationDate()), - }, - []handler.Condition{ - handler.NewCond(IDPTemplateIDCol, idpEvent.ID), - handler.NewCond(IDPTemplateInstanceIDCol, idpEvent.Aggregate().InstanceID), - orgCond, - }, - ), nil + return handler.NewStatement(event, func(ctx context.Context, ex handler.Executer, _ string) error { + tx, ok := ex.(*sql.Tx) + if !ok { + return zerrors.ThrowInvalidArgumentf(nil, "HANDL-HDqk9", "reduce.wrong.db.pool %T", ex) + } + + repo := repository.IDProviderRepository() + changes := database.Changes{ + repo.SetName(idpEvent.Name), + repo.SetType(domain.IDPTypeGoogle), + repo.SetAllowCreation(idpEvent.IsCreationAllowed), + repo.SetAllowLinking(idpEvent.IsLinkingAllowed), + repo.SetAllowAutoCreation(idpEvent.IsAutoCreation), + repo.SetAllowAutoUpdate(idpEvent.IsAutoUpdate), + repo.SetAutoLinkingField(mapAutoLinkingField(idpEvent.AutoLinkingOption)), + repo.SetPayload(string(payloadJSON)), + repo.SetUpdatedAt(gu.Ptr(event.CreatedAt())), + } + + _, err = repo.Update(ctx, v3_sql.SQLTx(tx), idpScopedCondition(repo, idpEvent.Aggregate().InstanceID, idpEvent.ID, orgID), changes...) + return err + }), nil } -func (p *idpTemplateRelationalProjection) reduceJWTIDPRelationalAdded(event eventstore.Event) (*handler.Statement, error) { +func (p *idpTemplateRelationalProjection) reduceJWTIDPAdded(event eventstore.Event) (*handler.Statement, error) { var orgId *string var idpEvent idp.JWTIDPAddedEvent switch e := event.(type) { @@ -1044,85 +1047,75 @@ func (p *idpTemplateRelationalProjection) reduceJWTIDPRelationalAdded(event even return nil, err } - return handler.NewCreateStatement( - &idpEvent, - []handler.Column{ - handler.NewCol(IDPTemplateIDCol, idpEvent.ID), - handler.NewCol(IDPTemplateInstanceIDCol, idpEvent.Aggregate().InstanceID), - handler.NewCol(IDPRelationalOrgId, orgId), - handler.NewCol(IDPTemplateNameCol, idpEvent.Name), - handler.NewCol(IDPTemplateStateCol, domain.IDPStateActive.String()), - handler.NewCol(IDPTemplateTypeCol, domain.IDPTypeJWT), - handler.NewCol(IDPRelationalAllowCreationCol, idpEvent.IsCreationAllowed), - handler.NewCol(IDPRelationalAllowLinkingCol, idpEvent.IsLinkingAllowed), - handler.NewCol(IDPRelationalAllowAutoCreationCol, idpEvent.IsAutoCreation), - handler.NewCol(IDPRelationalAllowAutoUpdateCol, idpEvent.IsAutoUpdate), - handler.NewCol(IDPRelationalAllowAutoLinkingCol, func() any { - if idpEvent.AutoLinkingOption == internal_domain.AutoLinkingOptionUnspecified { - return nil - } - return domain.IDPAutoLinkingField(idpEvent.AutoLinkingOption) - }()), - handler.NewCol(IDPRelationalPayloadCol, payloadJSON), - handler.NewCol(CreatedAt, idpEvent.CreationDate()), - handler.NewCol(UpdatedAt, idpEvent.CreationDate()), - }, - ), nil + return handler.NewStatement(event, func(ctx context.Context, ex handler.Executer, _ string) error { + tx, ok := ex.(*sql.Tx) + if !ok { + return zerrors.ThrowInvalidArgumentf(nil, "HANDL-ZYYyQ", "reduce.wrong.db.pool %T", ex) + } + + repo := repository.IDProviderRepository() + return repo.Create(ctx, v3_sql.SQLTx(tx), &domain.IdentityProvider{ + InstanceID: idpEvent.Aggregate().InstanceID, + OrgID: orgId, + ID: idpEvent.ID, + State: domain.IDPStateActive, + Name: idpEvent.Name, + Type: gu.Ptr(domain.IDPTypeJWT), + AllowCreation: idpEvent.IsCreationAllowed, + AllowAutoCreation: idpEvent.IsAutoCreation, + AllowAutoUpdate: idpEvent.IsAutoUpdate, + AllowLinking: idpEvent.IsLinkingAllowed, + AutoLinkingField: mapAutoLinkingField(idpEvent.AutoLinkingOption), + Payload: payloadJSON, + CreatedAt: event.CreatedAt(), + UpdatedAt: event.CreatedAt(), + }) + }), nil } -func (p *idpTemplateRelationalProjection) reduceJWTIDPRelationalChanged(event eventstore.Event) (*handler.Statement, error) { +func (p *idpTemplateRelationalProjection) reduceJWTIDPChanged(event eventstore.Event) (*handler.Statement, error) { var orgId *string - var orgCond handler.Condition var idpEvent idp.JWTIDPChangedEvent switch e := event.(type) { case *org.JWTIDPChangedEvent: idpEvent = e.JWTIDPChangedEvent orgId = &idpEvent.Aggregate().ResourceOwner - orgCond = handler.NewCond(IDPRelationalOrgId, orgId) case *instance.JWTIDPChangedEvent: idpEvent = e.JWTIDPChangedEvent - orgCond = handler.NewIsNullCond((IDPRelationalOrgId)) default: return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-H15j2il", "reduce.wrong.event.type %v", []eventstore.EventType{org.JWTIDPChangedEventType, instance.JWTIDPChangedEventType}) } - return handler.NewStatement(event, func(ctx context.Context, ex handler.Executer, projectionName string) error { + return handler.NewStatement(event, func(ctx context.Context, ex handler.Executer, _ string) error { tx, ok := ex.(*sql.Tx) if !ok { return zerrors.ThrowInternal(nil, "HANDL-HX6ed", "unable to cast to tx executer") } - jwt, err := p.idpRepo.GetJWT(ctx, v3_sql.SQLTx(tx), p.idpRepo.IDCondition(idpEvent.ID), idpEvent.Agg.InstanceID, orgId) + idpRepo := repository.IDProviderRepository() + jwt, err := idpRepo.GetJWT(ctx, v3_sql.SQLTx(tx), database.WithCondition(idpScopedCondition(idpRepo, idpEvent.Agg.InstanceID, idpEvent.ID, orgId))) if err != nil { return err } - columns := reduceIDPRelationalChangedTemplateColumns(idpEvent.Name, idpEvent.OptionChanges) + changes := p.reduceIDPChangedTemplateColumns(idpRepo, idpEvent.Name, idpEvent.OptionChanges) payload := &jwt.JWT - payloadChanged := reduceJWTIDPRelationalChangedColumns(payload, &idpEvent) + payloadChanged := p.reduceJWTIDPChangedColumns(payload, &idpEvent) if payloadChanged { payloadJSON, err := json.Marshal(payload) if err != nil { return err } - columns = append(columns, handler.NewCol(IDPRelationalPayloadCol, payloadJSON)) + changes = append(changes, idpRepo.SetPayload(string(payloadJSON))) } - columns = append(columns, handler.NewCol(UpdatedAt, idpEvent.CreationDate())) - - return handler.NewUpdateStatement( - &idpEvent, - columns, - []handler.Condition{ - handler.NewCond(IDPTemplateIDCol, idpEvent.ID), - handler.NewCond(IDPTemplateInstanceIDCol, idpEvent.Aggregate().InstanceID), - orgCond, - }, - ).Execute(ctx, ex, projectionName) + changes = append(changes, idpRepo.SetUpdatedAt(gu.Ptr(event.CreatedAt()))) + _, err = idpRepo.Update(ctx, v3_sql.SQLTx(tx), idpScopedCondition(idpRepo, idpEvent.Aggregate().InstanceID, idpEvent.ID, orgId), changes...) + return err }), nil } -func (p *idpTemplateRelationalProjection) reduceAzureADIDPRelationalAdded(event eventstore.Event) (*handler.Statement, error) { +func (p *idpTemplateRelationalProjection) reduceAzureADIDPAdded(event eventstore.Event) (*handler.Statement, error) { var orgId *string var idpEvent idp.AzureADIDPAddedEvent switch e := event.(type) { @@ -1153,62 +1146,60 @@ func (p *idpTemplateRelationalProjection) reduceAzureADIDPRelationalAdded(event return nil, err } - return handler.NewCreateStatement( - &idpEvent, - []handler.Column{ - handler.NewCol(IDPTemplateIDCol, idpEvent.ID), - handler.NewCol(IDPTemplateInstanceIDCol, idpEvent.Aggregate().InstanceID), - handler.NewCol(IDPRelationalOrgId, orgId), - handler.NewCol(IDPTemplateNameCol, idpEvent.Name), - handler.NewCol(IDPTemplateTypeCol, domain.IDPTypeAzure), - handler.NewCol(IDPTemplateStateCol, domain.IDPStateActive.String()), - handler.NewCol(IDPRelationalAllowCreationCol, idpEvent.IsCreationAllowed), - handler.NewCol(IDPRelationalAllowLinkingCol, idpEvent.IsLinkingAllowed), - handler.NewCol(IDPRelationalAllowAutoCreationCol, idpEvent.IsAutoCreation), - handler.NewCol(IDPRelationalAllowAutoUpdateCol, idpEvent.IsAutoUpdate), - handler.NewCol(IDPRelationalAllowAutoLinkingCol, func() any { - if idpEvent.AutoLinkingOption == internal_domain.AutoLinkingOptionUnspecified { - return nil - } - return domain.IDPAutoLinkingField(idpEvent.AutoLinkingOption) - }()), - handler.NewCol(IDPRelationalPayloadCol, payloadJSON), - handler.NewCol(CreatedAt, idpEvent.CreationDate()), - handler.NewCol(UpdatedAt, idpEvent.CreationDate()), - }, - ), nil + return handler.NewStatement(event, func(ctx context.Context, ex handler.Executer, _ string) error { + tx, ok := ex.(*sql.Tx) + if !ok { + return zerrors.ThrowInvalidArgumentf(nil, "HANDL-GJ4Kb", "reduce.wrong.db.pool %T", ex) + } + + repo := repository.IDProviderRepository() + return repo.Create(ctx, v3_sql.SQLTx(tx), &domain.IdentityProvider{ + InstanceID: idpEvent.Aggregate().InstanceID, + OrgID: orgId, + ID: idpEvent.ID, + State: domain.IDPStateActive, + Name: idpEvent.Name, + Type: gu.Ptr(domain.IDPTypeAzure), + AllowCreation: idpEvent.IsCreationAllowed, + AllowAutoCreation: idpEvent.IsAutoCreation, + AllowAutoUpdate: idpEvent.IsAutoUpdate, + AllowLinking: idpEvent.IsLinkingAllowed, + AutoLinkingField: mapAutoLinkingField(idpEvent.AutoLinkingOption), + Payload: payloadJSON, + CreatedAt: event.CreatedAt(), + UpdatedAt: event.CreatedAt(), + }) + }), nil } -func (p *idpTemplateRelationalProjection) reduceAzureADIDPRelationalChanged(event eventstore.Event) (*handler.Statement, error) { +func (p *idpTemplateRelationalProjection) reduceAzureADIDPChanged(event eventstore.Event) (*handler.Statement, error) { var orgId *string - var orgCond handler.Condition var idpEvent idp.AzureADIDPChangedEvent switch e := event.(type) { case *org.AzureADIDPChangedEvent: idpEvent = e.AzureADIDPChangedEvent orgId = &idpEvent.Aggregate().ResourceOwner - orgCond = handler.NewCond(IDPRelationalOrgId, orgId) case *instance.AzureADIDPChangedEvent: idpEvent = e.AzureADIDPChangedEvent - orgCond = handler.NewIsNullCond((IDPRelationalOrgId)) default: return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-YZ5x25s", "reduce.wrong.event.type %v", []eventstore.EventType{org.AzureADIDPChangedEventType, instance.AzureADIDPChangedEventType}) } - return handler.NewStatement(event, func(ctx context.Context, ex handler.Executer, projectionName string) error { + return handler.NewStatement(event, func(ctx context.Context, ex handler.Executer, _ string) error { tx, ok := ex.(*sql.Tx) if !ok { return zerrors.ThrowInternal(nil, "HANDL-HX6ed", "unable to cast to tx executer") } - azure, err := p.idpRepo.GetAzureAD(ctx, v3_sql.SQLTx(tx), p.idpRepo.IDCondition(idpEvent.ID), idpEvent.Agg.InstanceID, orgId) + idpRepo := repository.IDProviderRepository() + azure, err := idpRepo.GetAzureAD(ctx, v3_sql.SQLTx(tx), database.WithCondition(idpScopedCondition(idpRepo, idpEvent.Agg.InstanceID, idpEvent.ID, orgId))) if err != nil { return err } - columns := reduceIDPRelationalChangedTemplateColumns(idpEvent.Name, idpEvent.OptionChanges) + changes := p.reduceIDPChangedTemplateColumns(idpRepo, idpEvent.Name, idpEvent.OptionChanges) payload := &azure.Azure - payloadChanged, err := reduceAzureADIDPRelationalChangedColumns(payload, &idpEvent) + payloadChanged, err := p.reduceAzureADIDPChangedColumns(payload, &idpEvent) if err != nil { return err } @@ -1218,25 +1209,17 @@ func (p *idpTemplateRelationalProjection) reduceAzureADIDPRelationalChanged(even if err != nil { return err } - columns = append(columns, handler.NewCol(IDPRelationalPayloadCol, payloadJSON)) + changes = append(changes, idpRepo.SetPayload(string(payloadJSON))) } - columns = append(columns, handler.NewCol(UpdatedAt, idpEvent.CreationDate())) - - return handler.NewUpdateStatement( - &idpEvent, - columns, - []handler.Condition{ - handler.NewCond(IDPTemplateIDCol, idpEvent.ID), - handler.NewCond(IDPTemplateInstanceIDCol, idpEvent.Aggregate().InstanceID), - orgCond, - }, - ).Execute(ctx, ex, projectionName) + changes = append(changes, idpRepo.SetUpdatedAt(gu.Ptr(event.CreatedAt()))) + _, err = idpRepo.Update(ctx, v3_sql.SQLTx(tx), idpScopedCondition(idpRepo, idpEvent.Aggregate().InstanceID, idpEvent.ID, orgId), changes...) + return err }), nil } -func (p *idpTemplateRelationalProjection) reduceGitHubIDPRelationalAdded(event eventstore.Event) (*handler.Statement, error) { +func (p *idpTemplateRelationalProjection) reduceGitHubIDPAdded(event eventstore.Event) (*handler.Statement, error) { var orgId *string var idpEvent idp.GitHubIDPAddedEvent switch e := event.(type) { @@ -1260,86 +1243,76 @@ func (p *idpTemplateRelationalProjection) reduceGitHubIDPRelationalAdded(event e return nil, err } - return handler.NewCreateStatement( - &idpEvent, - []handler.Column{ - handler.NewCol(IDPTemplateIDCol, idpEvent.ID), - handler.NewCol(IDPTemplateInstanceIDCol, idpEvent.Aggregate().InstanceID), - handler.NewCol(IDPRelationalOrgId, orgId), - handler.NewCol(IDPTemplateNameCol, idpEvent.Name), - handler.NewCol(IDPTemplateTypeCol, domain.IDPTypeGitHub), - handler.NewCol(IDPTemplateStateCol, domain.IDPStateActive.String()), - handler.NewCol(IDPRelationalAllowCreationCol, idpEvent.IsCreationAllowed), - handler.NewCol(IDPRelationalAllowLinkingCol, idpEvent.IsLinkingAllowed), - handler.NewCol(IDPRelationalAllowAutoCreationCol, idpEvent.IsAutoCreation), - handler.NewCol(IDPRelationalAllowAutoUpdateCol, idpEvent.IsAutoUpdate), - handler.NewCol(IDPRelationalAllowAutoLinkingCol, func() any { - if idpEvent.AutoLinkingOption == internal_domain.AutoLinkingOptionUnspecified { - return nil - } - return domain.IDPAutoLinkingField(idpEvent.AutoLinkingOption) - }()), - handler.NewCol(IDPRelationalPayloadCol, payloadJSON), - handler.NewCol(CreatedAt, idpEvent.CreationDate()), - handler.NewCol(UpdatedAt, idpEvent.CreationDate()), - }, - ), nil + return handler.NewStatement(event, func(ctx context.Context, ex handler.Executer, _ string) error { + tx, ok := ex.(*sql.Tx) + if !ok { + return zerrors.ThrowInvalidArgumentf(nil, "HANDL-HNpgd", "reduce.wrong.db.pool %T", ex) + } + + repo := repository.IDProviderRepository() + return repo.Create(ctx, v3_sql.SQLTx(tx), &domain.IdentityProvider{ + InstanceID: idpEvent.Aggregate().InstanceID, + OrgID: orgId, + ID: idpEvent.ID, + State: domain.IDPStateActive, + Name: idpEvent.Name, + Type: gu.Ptr(domain.IDPTypeGitHub), + AllowCreation: idpEvent.IsCreationAllowed, + AllowAutoCreation: idpEvent.IsAutoCreation, + AllowAutoUpdate: idpEvent.IsAutoUpdate, + AllowLinking: idpEvent.IsLinkingAllowed, + AutoLinkingField: mapAutoLinkingField(idpEvent.AutoLinkingOption), + Payload: payloadJSON, + CreatedAt: event.CreatedAt(), + UpdatedAt: event.CreatedAt(), + }) + }), nil } -func (p *idpTemplateRelationalProjection) reduceGitHubIDPRelationalChanged(event eventstore.Event) (*handler.Statement, error) { +func (p *idpTemplateRelationalProjection) reduceGitHubIDPChanged(event eventstore.Event) (*handler.Statement, error) { var orgId *string - var orgCond handler.Condition var idpEvent idp.GitHubIDPChangedEvent switch e := event.(type) { case *org.GitHubIDPChangedEvent: idpEvent = e.GitHubIDPChangedEvent orgId = &idpEvent.Aggregate().ResourceOwner - orgCond = handler.NewCond(IDPRelationalOrgId, orgId) case *instance.GitHubIDPChangedEvent: idpEvent = e.GitHubIDPChangedEvent - orgCond = handler.NewIsNullCond((IDPRelationalOrgId)) default: return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-L1U89ks", "reduce.wrong.event.type %v", []eventstore.EventType{org.GitHubIDPChangedEventType, instance.GitHubIDPChangedEventType}) } - return handler.NewStatement(event, func(ctx context.Context, ex handler.Executer, projectionName string) error { + return handler.NewStatement(event, func(ctx context.Context, ex handler.Executer, _ string) error { tx, ok := ex.(*sql.Tx) if !ok { return zerrors.ThrowInternal(nil, "HANDL-HX6ed", "unable to cast to tx executer") } - github, err := p.idpRepo.GetGithub(ctx, v3_sql.SQLTx(tx), p.idpRepo.IDCondition(idpEvent.ID), idpEvent.Agg.InstanceID, orgId) + idpRepo := repository.IDProviderRepository() + github, err := idpRepo.GetGithub(ctx, v3_sql.SQLTx(tx), database.WithCondition(idpScopedCondition(idpRepo, idpEvent.Agg.InstanceID, idpEvent.ID, orgId))) if err != nil { return err } - columns := reduceIDPRelationalChangedTemplateColumns(idpEvent.Name, idpEvent.OptionChanges) + changes := p.reduceIDPChangedTemplateColumns(idpRepo, idpEvent.Name, idpEvent.OptionChanges) payload := &github.Github - payloadChanged := reduceGitHubIDPRelationalChangedColumns(payload, &idpEvent) + payloadChanged := p.reduceGitHubIDPChangedColumns(payload, &idpEvent) if payloadChanged { payloadJSON, err := json.Marshal(payload) if err != nil { return err } - columns = append(columns, handler.NewCol(IDPRelationalPayloadCol, payloadJSON)) + changes = append(changes, idpRepo.SetPayload(string(payloadJSON))) } - columns = append(columns, handler.NewCol(UpdatedAt, idpEvent.CreationDate())) - - return handler.NewUpdateStatement( - &idpEvent, - columns, - []handler.Condition{ - handler.NewCond(IDPTemplateIDCol, idpEvent.ID), - handler.NewCond(IDPTemplateInstanceIDCol, idpEvent.Aggregate().InstanceID), - orgCond, - }, - ).Execute(ctx, ex, projectionName) + changes = append(changes, idpRepo.SetUpdatedAt(gu.Ptr(event.CreatedAt()))) + _, err = idpRepo.Update(ctx, v3_sql.SQLTx(tx), idpScopedCondition(idpRepo, idpEvent.Aggregate().InstanceID, idpEvent.ID, orgId), changes...) + return err }), nil } -func (p *idpTemplateRelationalProjection) reduceGitHubEnterpriseIDPRelationalAdded(event eventstore.Event) (*handler.Statement, error) { +func (p *idpTemplateRelationalProjection) reduceGitHubEnterpriseIDPAdded(event eventstore.Event) (*handler.Statement, error) { var orgId *string var idpEvent idp.GitHubEnterpriseIDPAddedEvent switch e := event.(type) { @@ -1366,86 +1339,76 @@ func (p *idpTemplateRelationalProjection) reduceGitHubEnterpriseIDPRelationalAdd return nil, err } - return handler.NewCreateStatement( - &idpEvent, - []handler.Column{ - handler.NewCol(IDPTemplateIDCol, idpEvent.ID), - handler.NewCol(IDPTemplateInstanceIDCol, idpEvent.Aggregate().InstanceID), - handler.NewCol(IDPRelationalOrgId, orgId), - handler.NewCol(IDPTemplateStateCol, domain.IDPStateActive.String()), - handler.NewCol(IDPTemplateNameCol, idpEvent.Name), - handler.NewCol(IDPTemplateTypeCol, domain.IDPTypeGitHubEnterprise), - handler.NewCol(IDPRelationalAllowCreationCol, idpEvent.IsCreationAllowed), - handler.NewCol(IDPRelationalAllowLinkingCol, idpEvent.IsLinkingAllowed), - handler.NewCol(IDPRelationalAllowAutoCreationCol, idpEvent.IsAutoCreation), - handler.NewCol(IDPRelationalAllowAutoUpdateCol, idpEvent.IsAutoUpdate), - handler.NewCol(IDPRelationalAllowAutoLinkingCol, func() any { - if idpEvent.AutoLinkingOption == internal_domain.AutoLinkingOptionUnspecified { - return nil - } - return domain.IDPAutoLinkingField(idpEvent.AutoLinkingOption) - }()), - handler.NewCol(IDPRelationalPayloadCol, payloadJSON), - handler.NewCol(CreatedAt, idpEvent.CreationDate()), - handler.NewCol(UpdatedAt, idpEvent.CreationDate()), - }, - ), nil + return handler.NewStatement(event, func(ctx context.Context, ex handler.Executer, _ string) error { + tx, ok := ex.(*sql.Tx) + if !ok { + return zerrors.ThrowInvalidArgumentf(nil, "HANDL-Kv4Fu", "reduce.wrong.db.pool %T", ex) + } + + repo := repository.IDProviderRepository() + return repo.Create(ctx, v3_sql.SQLTx(tx), &domain.IdentityProvider{ + InstanceID: idpEvent.Aggregate().InstanceID, + OrgID: orgId, + ID: idpEvent.ID, + State: domain.IDPStateActive, + Name: idpEvent.Name, + Type: gu.Ptr(domain.IDPTypeGitHubEnterprise), + AllowCreation: idpEvent.IsCreationAllowed, + AllowAutoCreation: idpEvent.IsAutoCreation, + AllowAutoUpdate: idpEvent.IsAutoUpdate, + AllowLinking: idpEvent.IsLinkingAllowed, + AutoLinkingField: mapAutoLinkingField(idpEvent.AutoLinkingOption), + Payload: payloadJSON, + CreatedAt: event.CreatedAt(), + UpdatedAt: event.CreatedAt(), + }) + }), nil } -func (p *idpTemplateRelationalProjection) reduceGitHubEnterpriseIDPRelationalChanged(event eventstore.Event) (*handler.Statement, error) { +func (p *idpTemplateRelationalProjection) reduceGitHubEnterpriseIDPChanged(event eventstore.Event) (*handler.Statement, error) { var orgId *string - var orgCond handler.Condition var idpEvent idp.GitHubEnterpriseIDPChangedEvent switch e := event.(type) { case *org.GitHubEnterpriseIDPChangedEvent: idpEvent = e.GitHubEnterpriseIDPChangedEvent orgId = &idpEvent.Aggregate().ResourceOwner - orgCond = handler.NewCond(IDPRelationalOrgId, orgId) case *instance.GitHubEnterpriseIDPChangedEvent: idpEvent = e.GitHubEnterpriseIDPChangedEvent - orgCond = handler.NewIsNullCond((IDPRelationalOrgId)) default: return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-YDg3g", "reduce.wrong.event.type %v", []eventstore.EventType{org.GitHubEnterpriseIDPChangedEventType, instance.GitHubEnterpriseIDPChangedEventType}) } - return handler.NewStatement(event, func(ctx context.Context, ex handler.Executer, projectionName string) error { + return handler.NewStatement(event, func(ctx context.Context, ex handler.Executer, _ string) error { tx, ok := ex.(*sql.Tx) if !ok { return zerrors.ThrowInternal(nil, "HANDL-HX6ed", "unable to cast to tx executer") } - githubEnterprise, err := p.idpRepo.GetGithubEnterprise(ctx, v3_sql.SQLTx(tx), p.idpRepo.IDCondition(idpEvent.ID), idpEvent.Agg.InstanceID, orgId) + idpRepo := repository.IDProviderRepository() + githubEnterprise, err := idpRepo.GetGithubEnterprise(ctx, v3_sql.SQLTx(tx), database.WithCondition(idpScopedCondition(idpRepo, idpEvent.Agg.InstanceID, idpEvent.ID, orgId))) if err != nil { return err } - columns := reduceIDPRelationalChangedTemplateColumns(idpEvent.Name, idpEvent.OptionChanges) + changes := p.reduceIDPChangedTemplateColumns(idpRepo, idpEvent.Name, idpEvent.OptionChanges) payload := &githubEnterprise.GithubEnterprise - payloadChanged := reduceGitHubEnterpriseIDPRelationalChangedColumns(payload, &idpEvent) + payloadChanged := p.reduceGitHubEnterpriseIDPChangedColumns(payload, &idpEvent) if payloadChanged { payloadJSON, err := json.Marshal(payload) if err != nil { return err } - columns = append(columns, handler.NewCol(IDPRelationalPayloadCol, payloadJSON)) + changes = append(changes, idpRepo.SetPayload(string(payloadJSON))) } - columns = append(columns, handler.NewCol(UpdatedAt, idpEvent.CreationDate())) - - return handler.NewUpdateStatement( - &idpEvent, - columns, - []handler.Condition{ - handler.NewCond(IDPTemplateIDCol, idpEvent.ID), - handler.NewCond(IDPTemplateInstanceIDCol, idpEvent.Aggregate().InstanceID), - orgCond, - }, - ).Execute(ctx, ex, projectionName) + changes = append(changes, idpRepo.SetUpdatedAt(gu.Ptr(event.CreatedAt()))) + _, err = idpRepo.Update(ctx, v3_sql.SQLTx(tx), idpScopedCondition(idpRepo, idpEvent.Aggregate().InstanceID, idpEvent.ID, orgId), changes...) + return err }), nil } -func (p *idpTemplateRelationalProjection) reduceGitLabIDPRelationalAdded(event eventstore.Event) (*handler.Statement, error) { +func (p *idpTemplateRelationalProjection) reduceGitLabIDPAdded(event eventstore.Event) (*handler.Statement, error) { var orgId *string var idpEvent idp.GitLabIDPAddedEvent switch e := event.(type) { @@ -1469,86 +1432,76 @@ func (p *idpTemplateRelationalProjection) reduceGitLabIDPRelationalAdded(event e return nil, err } - return handler.NewCreateStatement( - &idpEvent, - []handler.Column{ - handler.NewCol(IDPTemplateIDCol, idpEvent.ID), - handler.NewCol(IDPTemplateInstanceIDCol, idpEvent.Aggregate().InstanceID), - handler.NewCol(IDPRelationalOrgId, orgId), - handler.NewCol(IDPTemplateNameCol, idpEvent.Name), - handler.NewCol(IDPTemplateTypeCol, domain.IDPTypeGitLab), - handler.NewCol(IDPTemplateStateCol, domain.IDPStateActive.String()), - handler.NewCol(IDPRelationalAllowCreationCol, idpEvent.IsCreationAllowed), - handler.NewCol(IDPRelationalAllowLinkingCol, idpEvent.IsLinkingAllowed), - handler.NewCol(IDPRelationalAllowAutoCreationCol, idpEvent.IsAutoCreation), - handler.NewCol(IDPRelationalAllowAutoUpdateCol, idpEvent.IsAutoUpdate), - handler.NewCol(IDPRelationalAllowAutoLinkingCol, func() any { - if idpEvent.AutoLinkingOption == internal_domain.AutoLinkingOptionUnspecified { - return nil - } - return domain.IDPAutoLinkingField(idpEvent.AutoLinkingOption) - }()), - handler.NewCol(IDPRelationalPayloadCol, payloadJSON), - handler.NewCol(CreatedAt, idpEvent.CreationDate()), - handler.NewCol(UpdatedAt, idpEvent.CreationDate()), - }, - ), nil + return handler.NewStatement(event, func(ctx context.Context, ex handler.Executer, _ string) error { + tx, ok := ex.(*sql.Tx) + if !ok { + return zerrors.ThrowInvalidArgumentf(nil, "HANDL-kN8Qx", "reduce.wrong.db.pool %T", ex) + } + + repo := repository.IDProviderRepository() + return repo.Create(ctx, v3_sql.SQLTx(tx), &domain.IdentityProvider{ + InstanceID: idpEvent.Aggregate().InstanceID, + OrgID: orgId, + ID: idpEvent.ID, + State: domain.IDPStateActive, + Name: idpEvent.Name, + Type: gu.Ptr(domain.IDPTypeGitLab), + AllowCreation: idpEvent.IsCreationAllowed, + AllowAutoCreation: idpEvent.IsAutoCreation, + AllowAutoUpdate: idpEvent.IsAutoUpdate, + AllowLinking: idpEvent.IsLinkingAllowed, + AutoLinkingField: mapAutoLinkingField(idpEvent.AutoLinkingOption), + Payload: payloadJSON, + CreatedAt: event.CreatedAt(), + UpdatedAt: event.CreatedAt(), + }) + }), nil } -func (p *idpTemplateRelationalProjection) reduceGitLabIDPRelationalChanged(event eventstore.Event) (*handler.Statement, error) { +func (p *idpTemplateRelationalProjection) reduceGitLabIDPChanged(event eventstore.Event) (*handler.Statement, error) { var orgId *string - var orgCond handler.Condition var idpEvent idp.GitLabIDPChangedEvent switch e := event.(type) { case *org.GitLabIDPChangedEvent: idpEvent = e.GitLabIDPChangedEvent orgId = &idpEvent.Aggregate().ResourceOwner - orgCond = handler.NewCond(IDPRelationalOrgId, orgId) case *instance.GitLabIDPChangedEvent: idpEvent = e.GitLabIDPChangedEvent - orgCond = handler.NewIsNullCond((IDPRelationalOrgId)) default: return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-mT5827b", "reduce.wrong.event.type %v", []eventstore.EventType{org.GitLabIDPChangedEventType, instance.GitLabIDPChangedEventType}) } - return handler.NewStatement(event, func(ctx context.Context, ex handler.Executer, projectionName string) error { + return handler.NewStatement(event, func(ctx context.Context, ex handler.Executer, _ string) error { tx, ok := ex.(*sql.Tx) if !ok { return zerrors.ThrowInternal(nil, "HANDL-HX6ed", "unable to cast to tx executer") } - gitlab, err := p.idpRepo.GetGitlab(ctx, v3_sql.SQLTx(tx), p.idpRepo.IDCondition(idpEvent.ID), idpEvent.Agg.InstanceID, orgId) + idpRepo := repository.IDProviderRepository() + gitlab, err := idpRepo.GetGitlab(ctx, v3_sql.SQLTx(tx), database.WithCondition(idpScopedCondition(idpRepo, idpEvent.Agg.InstanceID, idpEvent.ID, orgId))) if err != nil { return err } - columns := reduceIDPRelationalChangedTemplateColumns(idpEvent.Name, idpEvent.OptionChanges) + changes := p.reduceIDPChangedTemplateColumns(idpRepo, idpEvent.Name, idpEvent.OptionChanges) payload := &gitlab.Gitlab - payloadChanged := reduceGitLabIDPRelationalChangedColumns(payload, &idpEvent) + payloadChanged := p.reduceGitLabIDPChangedColumns(payload, &idpEvent) if payloadChanged { payloadJSON, err := json.Marshal(payload) if err != nil { return err } - columns = append(columns, handler.NewCol(IDPRelationalPayloadCol, payloadJSON)) + changes = append(changes, idpRepo.SetPayload(string(payloadJSON))) } - columns = append(columns, handler.NewCol(UpdatedAt, idpEvent.CreationDate())) - - return handler.NewUpdateStatement( - &idpEvent, - columns, - []handler.Condition{ - handler.NewCond(IDPTemplateIDCol, idpEvent.ID), - handler.NewCond(IDPTemplateInstanceIDCol, idpEvent.Aggregate().InstanceID), - orgCond, - }, - ).Execute(ctx, ex, projectionName) + changes = append(changes, idpRepo.SetUpdatedAt(gu.Ptr(event.CreatedAt()))) + _, err = idpRepo.Update(ctx, v3_sql.SQLTx(tx), idpScopedCondition(idpRepo, idpEvent.Aggregate().InstanceID, idpEvent.ID, orgId), changes...) + return err }), nil } -func (p *idpTemplateRelationalProjection) reduceGitLabSelfHostedIDPRelationalAdded(event eventstore.Event) (*handler.Statement, error) { +func (p *idpTemplateRelationalProjection) reduceGitLabSelfHostedIDPAdded(event eventstore.Event) (*handler.Statement, error) { var orgId *string var idpEvent idp.GitLabSelfHostedIDPAddedEvent switch e := event.(type) { @@ -1561,98 +1514,88 @@ func (p *idpTemplateRelationalProjection) reduceGitLabSelfHostedIDPRelationalAdd return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-YAF3gw", "reduce.wrong.event.type %v", []eventstore.EventType{org.GitLabSelfHostedIDPAddedEventType, instance.GitLabSelfHostedIDPAddedEventType}) } - gitlabSelfHosting := domain.GitlabSelfHosting{ + gitlabSelfHosted := domain.GitlabSelfHosted{ Issuer: idpEvent.Issuer, ClientID: idpEvent.ClientID, ClientSecret: idpEvent.ClientSecret, Scopes: idpEvent.Scopes, } - payloadJSON, err := json.Marshal(gitlabSelfHosting) + payloadJSON, err := json.Marshal(gitlabSelfHosted) if err != nil { return nil, err } - return handler.NewCreateStatement( - &idpEvent, - []handler.Column{ - handler.NewCol(IDPTemplateIDCol, idpEvent.ID), - handler.NewCol(IDPTemplateInstanceIDCol, idpEvent.Aggregate().InstanceID), - handler.NewCol(IDPRelationalOrgId, orgId), - handler.NewCol(IDPTemplateNameCol, idpEvent.Name), - handler.NewCol(IDPTemplateTypeCol, domain.IDPTypeGitLabSelfHosted), - handler.NewCol(IDPTemplateStateCol, domain.IDPStateActive.String()), - handler.NewCol(IDPRelationalAllowCreationCol, idpEvent.IsCreationAllowed), - handler.NewCol(IDPRelationalAllowLinkingCol, idpEvent.IsLinkingAllowed), - handler.NewCol(IDPRelationalAllowAutoCreationCol, idpEvent.IsAutoCreation), - handler.NewCol(IDPRelationalAllowAutoUpdateCol, idpEvent.IsAutoUpdate), - handler.NewCol(IDPRelationalAllowAutoLinkingCol, func() any { - if idpEvent.AutoLinkingOption == internal_domain.AutoLinkingOptionUnspecified { - return nil - } - return domain.IDPAutoLinkingField(idpEvent.AutoLinkingOption) - }()), - handler.NewCol(IDPRelationalPayloadCol, payloadJSON), - handler.NewCol(CreatedAt, idpEvent.CreationDate()), - handler.NewCol(UpdatedAt, idpEvent.CreationDate()), - }, - ), nil + return handler.NewStatement(event, func(ctx context.Context, ex handler.Executer, _ string) error { + tx, ok := ex.(*sql.Tx) + if !ok { + return zerrors.ThrowInvalidArgumentf(nil, "HANDL-FQrtw", "reduce.wrong.db.pool %T", ex) + } + + repo := repository.IDProviderRepository() + return repo.Create(ctx, v3_sql.SQLTx(tx), &domain.IdentityProvider{ + InstanceID: idpEvent.Aggregate().InstanceID, + OrgID: orgId, + ID: idpEvent.ID, + State: domain.IDPStateActive, + Name: idpEvent.Name, + Type: gu.Ptr(domain.IDPTypeGitLabSelfHosted), + AllowCreation: idpEvent.IsCreationAllowed, + AllowAutoCreation: idpEvent.IsAutoCreation, + AllowAutoUpdate: idpEvent.IsAutoUpdate, + AllowLinking: idpEvent.IsLinkingAllowed, + AutoLinkingField: mapAutoLinkingField(idpEvent.AutoLinkingOption), + Payload: payloadJSON, + CreatedAt: event.CreatedAt(), + UpdatedAt: event.CreatedAt(), + }) + }), nil } -func (p *idpTemplateRelationalProjection) reduceGitLabSelfHostedIDPRelationalChanged(event eventstore.Event) (*handler.Statement, error) { +func (p *idpTemplateRelationalProjection) reduceGitLabSelfHostedIDPChanged(event eventstore.Event) (*handler.Statement, error) { var orgId *string - var orgCond handler.Condition var idpEvent idp.GitLabSelfHostedIDPChangedEvent switch e := event.(type) { case *org.GitLabSelfHostedIDPChangedEvent: idpEvent = e.GitLabSelfHostedIDPChangedEvent orgId = &idpEvent.Aggregate().ResourceOwner - orgCond = handler.NewCond(IDPRelationalOrgId, orgId) case *instance.GitLabSelfHostedIDPChangedEvent: idpEvent = e.GitLabSelfHostedIDPChangedEvent - orgCond = handler.NewIsNullCond((IDPRelationalOrgId)) default: return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-YAf3g2", "reduce.wrong.event.type %v", []eventstore.EventType{org.GitLabSelfHostedIDPChangedEventType, instance.GitLabSelfHostedIDPChangedEventType}) } - return handler.NewStatement(event, func(ctx context.Context, ex handler.Executer, projectionName string) error { + return handler.NewStatement(event, func(ctx context.Context, ex handler.Executer, _ string) error { tx, ok := ex.(*sql.Tx) if !ok { return zerrors.ThrowInternal(nil, "HANDL-HX6ed", "unable to cast to tx executer") } - gitlabSelfHosted, err := p.idpRepo.GetGitlabSelfHosting(ctx, v3_sql.SQLTx(tx), p.idpRepo.IDCondition(idpEvent.ID), idpEvent.Agg.InstanceID, orgId) + idpRepo := repository.IDProviderRepository() + gitlabSelfHosted, err := idpRepo.GetGitlabSelfHosted(ctx, v3_sql.SQLTx(tx), database.WithCondition(idpScopedCondition(idpRepo, idpEvent.Agg.InstanceID, idpEvent.ID, orgId))) if err != nil { return err } - columns := reduceIDPRelationalChangedTemplateColumns(idpEvent.Name, idpEvent.OptionChanges) + changes := p.reduceIDPChangedTemplateColumns(idpRepo, idpEvent.Name, idpEvent.OptionChanges) - payload := &gitlabSelfHosted.GitlabSelfHosting - payloadChanged := reduceGitLabSelfHostedIDPRelationalChangedColumns(payload, &idpEvent) + payload := &gitlabSelfHosted.GitlabSelfHosted + payloadChanged := p.reduceGitLabSelfHostedIDPChangedColumns(payload, &idpEvent) if payloadChanged { payloadJSON, err := json.Marshal(payload) if err != nil { return err } - columns = append(columns, handler.NewCol(IDPRelationalPayloadCol, payloadJSON)) + changes = append(changes, idpRepo.SetPayload(string(payloadJSON))) } - columns = append(columns, handler.NewCol(UpdatedAt, idpEvent.CreationDate())) - - return handler.NewUpdateStatement( - &idpEvent, - columns, - []handler.Condition{ - handler.NewCond(IDPTemplateIDCol, idpEvent.ID), - handler.NewCond(IDPTemplateInstanceIDCol, idpEvent.Aggregate().InstanceID), - orgCond, - }, - ).Execute(ctx, ex, projectionName) + changes = append(changes, idpRepo.SetUpdatedAt(gu.Ptr(event.CreatedAt()))) + _, err = idpRepo.Update(ctx, v3_sql.SQLTx(tx), idpScopedCondition(idpRepo, idpEvent.Aggregate().InstanceID, idpEvent.ID, orgId), changes...) + return err }), nil } -func (p *idpTemplateRelationalProjection) reduceGoogleIDPRelationalAdded(event eventstore.Event) (*handler.Statement, error) { +func (p *idpTemplateRelationalProjection) reduceGoogleIDPAdded(event eventstore.Event) (*handler.Statement, error) { var orgId *string var idpEvent idp.GoogleIDPAddedEvent switch e := event.(type) { @@ -1676,81 +1619,71 @@ func (p *idpTemplateRelationalProjection) reduceGoogleIDPRelationalAdded(event e return nil, err } - return handler.NewCreateStatement( - &idpEvent, - []handler.Column{ - handler.NewCol(IDPTemplateIDCol, idpEvent.ID), - handler.NewCol(IDPTemplateInstanceIDCol, idpEvent.Aggregate().InstanceID), - handler.NewCol(IDPRelationalOrgId, orgId), - handler.NewCol(IDPTemplateNameCol, idpEvent.Name), - handler.NewCol(IDPTemplateTypeCol, domain.IDPTypeGoogle), - handler.NewCol(IDPTemplateStateCol, domain.IDPStateActive.String()), - handler.NewCol(IDPRelationalAllowCreationCol, idpEvent.IsCreationAllowed), - handler.NewCol(IDPRelationalAllowLinkingCol, idpEvent.IsLinkingAllowed), - handler.NewCol(IDPRelationalAllowAutoCreationCol, idpEvent.IsAutoCreation), - handler.NewCol(IDPRelationalAllowAutoUpdateCol, idpEvent.IsAutoUpdate), - handler.NewCol(IDPRelationalAllowAutoLinkingCol, func() any { - if idpEvent.AutoLinkingOption == internal_domain.AutoLinkingOptionUnspecified { - return nil - } - return domain.IDPAutoLinkingField(idpEvent.AutoLinkingOption) - }()), - handler.NewCol(IDPRelationalPayloadCol, payloadJSON), - handler.NewCol(CreatedAt, idpEvent.CreationDate()), - handler.NewCol(UpdatedAt, idpEvent.CreationDate()), - }, - ), nil + return handler.NewStatement(event, func(ctx context.Context, ex handler.Executer, _ string) error { + tx, ok := ex.(*sql.Tx) + if !ok { + return zerrors.ThrowInvalidArgumentf(nil, "HANDL-B9SPm", "reduce.wrong.db.pool %T", ex) + } + + repo := repository.IDProviderRepository() + return repo.Create(ctx, v3_sql.SQLTx(tx), &domain.IdentityProvider{ + InstanceID: idpEvent.Aggregate().InstanceID, + OrgID: orgId, + ID: idpEvent.ID, + State: domain.IDPStateActive, + Name: idpEvent.Name, + Type: gu.Ptr(domain.IDPTypeGoogle), + AllowCreation: idpEvent.IsCreationAllowed, + AllowAutoCreation: idpEvent.IsAutoCreation, + AllowAutoUpdate: idpEvent.IsAutoUpdate, + AllowLinking: idpEvent.IsLinkingAllowed, + AutoLinkingField: mapAutoLinkingField(idpEvent.AutoLinkingOption), + Payload: payloadJSON, + CreatedAt: event.CreatedAt(), + UpdatedAt: event.CreatedAt(), + }) + }), nil } -func (p *idpTemplateRelationalProjection) reduceGoogleIDPRelationalChanged(event eventstore.Event) (*handler.Statement, error) { +func (p *idpTemplateRelationalProjection) reduceGoogleIDPChanged(event eventstore.Event) (*handler.Statement, error) { var orgId *string - var orgCond handler.Condition var idpEvent idp.GoogleIDPChangedEvent switch e := event.(type) { case *org.GoogleIDPChangedEvent: idpEvent = e.GoogleIDPChangedEvent orgId = &idpEvent.Aggregate().ResourceOwner - orgCond = handler.NewCond(IDPRelationalOrgId, orgId) case *instance.GoogleIDPChangedEvent: idpEvent = e.GoogleIDPChangedEvent - orgCond = handler.NewIsNullCond((IDPRelationalOrgId)) default: return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-YN58hml", "reduce.wrong.event.type %v", []eventstore.EventType{org.GoogleIDPChangedEventType, instance.GoogleIDPChangedEventType}) } - return handler.NewStatement(event, func(ctx context.Context, ex handler.Executer, projectionName string) error { + return handler.NewStatement(event, func(ctx context.Context, ex handler.Executer, _ string) error { tx, ok := ex.(*sql.Tx) if !ok { return zerrors.ThrowInternal(nil, "HANDL-HX6ed", "unable to cast to tx executer") } - google, err := p.idpRepo.GetGoogle(ctx, v3_sql.SQLTx(tx), p.idpRepo.IDCondition(idpEvent.ID), idpEvent.Agg.InstanceID, orgId) + idpRepo := repository.IDProviderRepository() + google, err := idpRepo.GetGoogle(ctx, v3_sql.SQLTx(tx), database.WithCondition(idpScopedCondition(idpRepo, idpEvent.Agg.InstanceID, idpEvent.ID, orgId))) if err != nil { return err } - columns := reduceIDPRelationalChangedTemplateColumns(idpEvent.Name, idpEvent.OptionChanges) + changes := p.reduceIDPChangedTemplateColumns(idpRepo, idpEvent.Name, idpEvent.OptionChanges) payload := &google.Google - payloadChanged := reduceGoogleIDPRelationalChangedColumns(payload, &idpEvent) + payloadChanged := p.reduceGoogleIDPChangedColumns(payload, &idpEvent) if payloadChanged { payloadJSON, err := json.Marshal(payload) if err != nil { return err } - columns = append(columns, handler.NewCol(IDPRelationalPayloadCol, payloadJSON)) + changes = append(changes, idpRepo.SetPayload(string(payloadJSON))) } - columns = append(columns, handler.NewCol(UpdatedAt, idpEvent.CreationDate())) - - return handler.NewUpdateStatement( - &idpEvent, - columns, - []handler.Condition{ - handler.NewCond(IDPTemplateIDCol, idpEvent.ID), - handler.NewCond(IDPTemplateInstanceIDCol, idpEvent.Aggregate().InstanceID), - orgCond, - }, - ).Execute(ctx, ex, projectionName) + changes = append(changes, idpRepo.SetUpdatedAt(gu.Ptr(event.CreatedAt()))) + _, err = idpRepo.Update(ctx, v3_sql.SQLTx(tx), idpScopedCondition(idpRepo, idpEvent.Aggregate().InstanceID, idpEvent.ID, orgId), changes...) + return err }), nil } @@ -1800,81 +1733,71 @@ func (p *idpTemplateRelationalProjection) reduceLDAPIDPAdded(event eventstore.Ev return nil, err } - return handler.NewCreateStatement( - &idpEvent, - []handler.Column{ - handler.NewCol(IDPTemplateIDCol, idpEvent.ID), - handler.NewCol(IDPTemplateInstanceIDCol, idpEvent.Aggregate().InstanceID), - handler.NewCol(IDPRelationalOrgId, orgId), - handler.NewCol(IDPTemplateNameCol, idpEvent.Name), - handler.NewCol(IDPTemplateTypeCol, domain.IDPTypeLDAP), - handler.NewCol(IDPTemplateStateCol, domain.IDPStateActive.String()), - handler.NewCol(IDPRelationalAllowCreationCol, idpEvent.IsCreationAllowed), - handler.NewCol(IDPRelationalAllowLinkingCol, idpEvent.IsLinkingAllowed), - handler.NewCol(IDPRelationalAllowAutoCreationCol, idpEvent.IsAutoCreation), - handler.NewCol(IDPRelationalAllowAutoUpdateCol, idpEvent.IsAutoUpdate), - handler.NewCol(IDPRelationalAllowAutoLinkingCol, func() any { - if idpEvent.AutoLinkingOption == internal_domain.AutoLinkingOptionUnspecified { - return nil - } - return domain.IDPAutoLinkingField(idpEvent.AutoLinkingOption) - }()), - handler.NewCol(IDPRelationalPayloadCol, payloadJSON), - handler.NewCol(CreatedAt, idpEvent.CreationDate()), - handler.NewCol(UpdatedAt, idpEvent.CreationDate()), - }, - ), nil + return handler.NewStatement(event, func(ctx context.Context, ex handler.Executer, _ string) error { + tx, ok := ex.(*sql.Tx) + if !ok { + return zerrors.ThrowInvalidArgumentf(nil, "HANDL-XCJ8w", "reduce.wrong.db.pool %T", ex) + } + + repo := repository.IDProviderRepository() + return repo.Create(ctx, v3_sql.SQLTx(tx), &domain.IdentityProvider{ + InstanceID: idpEvent.Aggregate().InstanceID, + OrgID: orgId, + ID: idpEvent.ID, + State: domain.IDPStateActive, + Name: idpEvent.Name, + Type: gu.Ptr(domain.IDPTypeLDAP), + AllowCreation: idpEvent.IsCreationAllowed, + AllowAutoCreation: idpEvent.IsAutoCreation, + AllowAutoUpdate: idpEvent.IsAutoUpdate, + AllowLinking: idpEvent.IsLinkingAllowed, + AutoLinkingField: mapAutoLinkingField(idpEvent.AutoLinkingOption), + Payload: payloadJSON, + CreatedAt: event.CreatedAt(), + UpdatedAt: event.CreatedAt(), + }) + }), nil } func (p *idpTemplateRelationalProjection) reduceLDAPIDPChanged(event eventstore.Event) (*handler.Statement, error) { var orgId *string - var orgCond handler.Condition var idpEvent idp.LDAPIDPChangedEvent switch e := event.(type) { case *org.LDAPIDPChangedEvent: idpEvent = e.LDAPIDPChangedEvent orgId = &idpEvent.Aggregate().ResourceOwner - orgCond = handler.NewCond(IDPRelationalOrgId, orgId) case *instance.LDAPIDPChangedEvent: idpEvent = e.LDAPIDPChangedEvent - orgCond = handler.NewIsNullCond((IDPRelationalOrgId)) default: return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-p1582ks", "reduce.wrong.event.type %v", []eventstore.EventType{org.LDAPIDPChangedEventType, instance.LDAPIDPChangedEventType}) } - return handler.NewStatement(event, func(ctx context.Context, ex handler.Executer, projectionName string) error { + return handler.NewStatement(event, func(ctx context.Context, ex handler.Executer, _ string) error { tx, ok := ex.(*sql.Tx) if !ok { return zerrors.ThrowInternal(nil, "HANDL-HX6ed", "unable to cast to tx executer") } - ldap, err := p.idpRepo.GetLDAP(ctx, v3_sql.SQLTx(tx), p.idpRepo.IDCondition(idpEvent.ID), idpEvent.Agg.InstanceID, orgId) + idpRepo := repository.IDProviderRepository() + ldap, err := idpRepo.GetLDAP(ctx, v3_sql.SQLTx(tx), database.WithCondition(idpScopedCondition(idpRepo, idpEvent.Agg.InstanceID, idpEvent.ID, orgId))) if err != nil { return err } - columns := reduceIDPRelationalChangedTemplateColumns(idpEvent.Name, idpEvent.OptionChanges) + changes := p.reduceIDPChangedTemplateColumns(idpRepo, idpEvent.Name, idpEvent.OptionChanges) payload := &ldap.LDAP - payloadChanged := reduceLDAPIDPRelationalChangedColumns(payload, &idpEvent) + payloadChanged := p.reduceLDAPIDPChangedColumns(payload, &idpEvent) if payloadChanged { payloadJSON, err := json.Marshal(payload) if err != nil { return err } - columns = append(columns, handler.NewCol(IDPRelationalPayloadCol, payloadJSON)) + changes = append(changes, idpRepo.SetPayload(string(payloadJSON))) } - columns = append(columns, handler.NewCol(UpdatedAt, idpEvent.CreationDate())) - - return handler.NewUpdateStatement( - &idpEvent, - columns, - []handler.Condition{ - handler.NewCond(IDPTemplateIDCol, idpEvent.ID), - handler.NewCond(IDPTemplateInstanceIDCol, idpEvent.Aggregate().InstanceID), - orgCond, - }, - ).Execute(ctx, ex, projectionName) + changes = append(changes, idpRepo.SetUpdatedAt(gu.Ptr(event.CreatedAt()))) + _, err = idpRepo.Update(ctx, v3_sql.SQLTx(tx), idpScopedCondition(idpRepo, idpEvent.Aggregate().InstanceID, idpEvent.ID, orgId), changes...) + return err }), nil } @@ -1905,81 +1828,71 @@ func (p *idpTemplateRelationalProjection) reduceAppleIDPAdded(event eventstore.E return nil, err } - return handler.NewCreateStatement( - &idpEvent, - []handler.Column{ - handler.NewCol(IDPTemplateIDCol, idpEvent.ID), - handler.NewCol(IDPTemplateInstanceIDCol, idpEvent.Aggregate().InstanceID), - handler.NewCol(IDPRelationalOrgId, orgId), - handler.NewCol(IDPTemplateNameCol, idpEvent.Name), - handler.NewCol(IDPTemplateTypeCol, domain.IDPTypeApple), - handler.NewCol(IDPTemplateStateCol, domain.IDPStateActive.String()), - handler.NewCol(IDPRelationalAllowCreationCol, idpEvent.IsCreationAllowed), - handler.NewCol(IDPRelationalAllowLinkingCol, idpEvent.IsLinkingAllowed), - handler.NewCol(IDPRelationalAllowAutoCreationCol, idpEvent.IsAutoCreation), - handler.NewCol(IDPRelationalAllowAutoUpdateCol, idpEvent.IsAutoUpdate), - handler.NewCol(IDPRelationalAllowAutoLinkingCol, func() any { - if idpEvent.AutoLinkingOption == internal_domain.AutoLinkingOptionUnspecified { - return nil - } - return domain.IDPAutoLinkingField(idpEvent.AutoLinkingOption) - }()), - handler.NewCol(IDPRelationalPayloadCol, payloadJSON), - handler.NewCol(CreatedAt, idpEvent.CreationDate()), - handler.NewCol(UpdatedAt, idpEvent.CreationDate()), - }, - ), nil + return handler.NewStatement(event, func(ctx context.Context, ex handler.Executer, _ string) error { + tx, ok := ex.(*sql.Tx) + if !ok { + return zerrors.ThrowInvalidArgumentf(nil, "HANDL-Ku2YB", "reduce.wrong.db.pool %T", ex) + } + + repo := repository.IDProviderRepository() + return repo.Create(ctx, v3_sql.SQLTx(tx), &domain.IdentityProvider{ + InstanceID: idpEvent.Aggregate().InstanceID, + OrgID: orgId, + ID: idpEvent.ID, + State: domain.IDPStateActive, + Name: idpEvent.Name, + Type: gu.Ptr(domain.IDPTypeApple), + AllowCreation: idpEvent.IsCreationAllowed, + AllowAutoCreation: idpEvent.IsAutoCreation, + AllowAutoUpdate: idpEvent.IsAutoUpdate, + AllowLinking: idpEvent.IsLinkingAllowed, + AutoLinkingField: mapAutoLinkingField(idpEvent.AutoLinkingOption), + Payload: payloadJSON, + CreatedAt: event.CreatedAt(), + UpdatedAt: event.CreatedAt(), + }) + }), nil } func (p *idpTemplateRelationalProjection) reduceAppleIDPChanged(event eventstore.Event) (*handler.Statement, error) { var orgId *string - var orgCond handler.Condition var idpEvent idp.AppleIDPChangedEvent switch e := event.(type) { case *org.AppleIDPChangedEvent: idpEvent = e.AppleIDPChangedEvent orgId = &idpEvent.Aggregate().ResourceOwner - orgCond = handler.NewCond(IDPRelationalOrgId, orgId) case *instance.AppleIDPChangedEvent: idpEvent = e.AppleIDPChangedEvent - orgCond = handler.NewIsNullCond((IDPRelationalOrgId)) default: return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-YBez3", "reduce.wrong.event.type %v", []eventstore.EventType{org.AppleIDPChangedEventType /*, instance.AppleIDPChangedEventType*/}) } - return handler.NewStatement(event, func(ctx context.Context, ex handler.Executer, projectionName string) error { + return handler.NewStatement(event, func(ctx context.Context, ex handler.Executer, _ string) error { tx, ok := ex.(*sql.Tx) if !ok { return zerrors.ThrowInternal(nil, "HANDL-HX6ed", "unable to cast to tx executer") } - apple, err := p.idpRepo.GetApple(ctx, v3_sql.SQLTx(tx), p.idpRepo.IDCondition(idpEvent.ID), idpEvent.Agg.InstanceID, orgId) + idpRepo := repository.IDProviderRepository() + apple, err := idpRepo.GetApple(ctx, v3_sql.SQLTx(tx), database.WithCondition(idpScopedCondition(idpRepo, idpEvent.Agg.InstanceID, idpEvent.ID, orgId))) if err != nil { return err } - columns := reduceIDPRelationalChangedTemplateColumns(idpEvent.Name, idpEvent.OptionChanges) + changes := p.reduceIDPChangedTemplateColumns(idpRepo, idpEvent.Name, idpEvent.OptionChanges) payload := &apple.Apple - payloadChanged := reduceAppleIDPRelationalChangedColumns(payload, &idpEvent) + payloadChanged := p.reduceAppleIDPChangedColumns(payload, &idpEvent) if payloadChanged { payloadJSON, err := json.Marshal(payload) if err != nil { return err } - columns = append(columns, handler.NewCol(IDPRelationalPayloadCol, payloadJSON)) + changes = append(changes, idpRepo.SetPayload(string(payloadJSON))) } - columns = append(columns, handler.NewCol(UpdatedAt, idpEvent.CreationDate())) - - return handler.NewUpdateStatement( - &idpEvent, - columns, - []handler.Condition{ - handler.NewCond(IDPTemplateIDCol, idpEvent.ID), - handler.NewCond(IDPTemplateInstanceIDCol, idpEvent.Aggregate().InstanceID), - orgCond, - }, - ).Execute(ctx, ex, projectionName) + changes = append(changes, idpRepo.SetUpdatedAt(gu.Ptr(event.CreatedAt()))) + _, err = idpRepo.Update(ctx, v3_sql.SQLTx(tx), idpScopedCondition(idpRepo, idpEvent.Aggregate().InstanceID, idpEvent.ID, orgId), changes...) + return err }), nil } @@ -2013,236 +1926,218 @@ func (p *idpTemplateRelationalProjection) reduceSAMLIDPAdded(event eventstore.Ev return nil, err } - return handler.NewCreateStatement( - &idpEvent, - []handler.Column{ - handler.NewCol(IDPTemplateIDCol, idpEvent.ID), - handler.NewCol(IDPTemplateInstanceIDCol, idpEvent.Aggregate().InstanceID), - handler.NewCol(IDPRelationalOrgId, orgId), - handler.NewCol(IDPTemplateNameCol, idpEvent.Name), - handler.NewCol(IDPTemplateTypeCol, domain.IDPTypeSAML), - handler.NewCol(IDPTemplateStateCol, domain.IDPStateActive.String()), - handler.NewCol(IDPRelationalAllowCreationCol, idpEvent.IsCreationAllowed), - handler.NewCol(IDPRelationalAllowLinkingCol, idpEvent.IsLinkingAllowed), - handler.NewCol(IDPRelationalAllowAutoCreationCol, idpEvent.IsAutoCreation), - handler.NewCol(IDPRelationalAllowAutoUpdateCol, idpEvent.IsAutoUpdate), - handler.NewCol(IDPRelationalAllowAutoLinkingCol, func() any { - if idpEvent.AutoLinkingOption == internal_domain.AutoLinkingOptionUnspecified { - return nil - } - return domain.IDPAutoLinkingField(idpEvent.AutoLinkingOption) - }()), - handler.NewCol(IDPRelationalPayloadCol, payloadJSON), - handler.NewCol(CreatedAt, idpEvent.CreationDate()), - handler.NewCol(UpdatedAt, idpEvent.CreationDate()), - }, - ), nil + return handler.NewStatement(event, func(ctx context.Context, ex handler.Executer, _ string) error { + tx, ok := ex.(*sql.Tx) + if !ok { + return zerrors.ThrowInvalidArgumentf(nil, "HANDL-ksJ3N", "reduce.wrong.db.pool %T", ex) + } + + repo := repository.IDProviderRepository() + return repo.Create(ctx, v3_sql.SQLTx(tx), &domain.IdentityProvider{ + InstanceID: idpEvent.Aggregate().InstanceID, + OrgID: orgId, + ID: idpEvent.ID, + State: domain.IDPStateActive, + Name: idpEvent.Name, + Type: gu.Ptr(domain.IDPTypeSAML), + AllowCreation: idpEvent.IsCreationAllowed, + AllowAutoCreation: idpEvent.IsAutoCreation, + AllowAutoUpdate: idpEvent.IsAutoUpdate, + AllowLinking: idpEvent.IsLinkingAllowed, + AutoLinkingField: mapAutoLinkingField(idpEvent.AutoLinkingOption), + Payload: payloadJSON, + CreatedAt: event.CreatedAt(), + UpdatedAt: event.CreatedAt(), + }) + }), nil } func (p *idpTemplateRelationalProjection) reduceSAMLIDPChanged(event eventstore.Event) (*handler.Statement, error) { var orgId *string - var orgCond handler.Condition var idpEvent idp.SAMLIDPChangedEvent switch e := event.(type) { case *org.SAMLIDPChangedEvent: idpEvent = e.SAMLIDPChangedEvent orgId = &idpEvent.Aggregate().ResourceOwner - orgCond = handler.NewCond(IDPRelationalOrgId, orgId) case *instance.SAMLIDPChangedEvent: idpEvent = e.SAMLIDPChangedEvent - orgCond = handler.NewIsNullCond((IDPRelationalOrgId)) default: return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-Y7c0fii4ad", "reduce.wrong.event.type %v", []eventstore.EventType{org.SAMLIDPChangedEventType, instance.SAMLIDPChangedEventType}) } - return handler.NewStatement(event, func(ctx context.Context, ex handler.Executer, projectionName string) error { + return handler.NewStatement(event, func(ctx context.Context, ex handler.Executer, _ string) error { tx, ok := ex.(*sql.Tx) if !ok { return zerrors.ThrowInternal(nil, "HANDL-HX6ed", "unable to cast to tx executer") } - saml, err := p.idpRepo.GetSAML(ctx, v3_sql.SQLTx(tx), p.idpRepo.IDCondition(idpEvent.ID), idpEvent.Agg.InstanceID, orgId) + idpRepo := repository.IDProviderRepository() + saml, err := idpRepo.GetSAML(ctx, v3_sql.SQLTx(tx), database.WithCondition(idpScopedCondition(idpRepo, idpEvent.Agg.InstanceID, idpEvent.ID, orgId))) if err != nil { return err } - columns := reduceIDPRelationalChangedTemplateColumns(idpEvent.Name, idpEvent.OptionChanges) + changes := p.reduceIDPChangedTemplateColumns(idpRepo, idpEvent.Name, idpEvent.OptionChanges) payload := &saml.SAML - payloadChanged := reduceSAMLIDPRelationalChangedColumns(payload, &idpEvent) + payloadChanged := p.reduceSAMLIDPChangedColumns(payload, &idpEvent) if payloadChanged { payloadJSON, err := json.Marshal(payload) if err != nil { return err } - columns = append(columns, handler.NewCol(IDPRelationalPayloadCol, payloadJSON)) + changes = append(changes, idpRepo.SetPayload(string(payloadJSON))) } - columns = append(columns, handler.NewCol(UpdatedAt, idpEvent.CreationDate())) - - return handler.NewUpdateStatement( - &idpEvent, - columns, - []handler.Condition{ - handler.NewCond(IDPTemplateIDCol, idpEvent.ID), - handler.NewCond(IDPTemplateInstanceIDCol, idpEvent.Aggregate().InstanceID), - orgCond, - }, - ).Execute(ctx, ex, projectionName) + changes = append(changes, idpRepo.SetUpdatedAt(gu.Ptr(event.CreatedAt()))) + _, err = idpRepo.Update(ctx, v3_sql.SQLTx(tx), idpScopedCondition(idpRepo, idpEvent.Aggregate().InstanceID, idpEvent.ID, orgId), changes...) + return err }), nil } -func (p *idpTemplateRelationalProjection) reduceIDPRemoved(event eventstore.Event) (*handler.Statement, error) { - var orgCond handler.Condition - var idpEvent idp.RemovedEvent - switch e := event.(type) { - case *org.IDPRemovedEvent: - idpEvent = e.RemovedEvent - orgCond = handler.NewCond(IDPRelationalOrgId, idpEvent.Aggregate().ResourceOwner) - case *instance.IDPRemovedEvent: - idpEvent = e.RemovedEvent - orgCond = handler.NewIsNullCond((IDPRelationalOrgId)) - default: - return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-Ybcvwin2", "reduce.wrong.event.type %v", []eventstore.EventType{org.IDPRemovedEventType, instance.IDPRemovedEventType}) - } - - return handler.NewDeleteStatement( - &idpEvent, - []handler.Condition{ - handler.NewCond(IDPTemplateIDCol, idpEvent.ID), - handler.NewCond(IDPTemplateInstanceIDCol, idpEvent.Aggregate().InstanceID), - orgCond, - }, - ), nil -} - -func reduceIDPRelationalChangedTemplateColumns(name *string, optionChanges idp.OptionChanges) []handler.Column { - cols := make([]handler.Column, 0, 8) +func (p *idpTemplateRelationalProjection) reduceIDPChangedTemplateColumns(repo domain.IDProviderRepository, name *string, optionChanges idp.OptionChanges) database.Changes { + changes := make(database.Changes, 0, 8) if name != nil { - cols = append(cols, handler.NewCol(IDPTemplateNameCol, *name)) + changes = append(changes, repo.SetName(*name)) } if optionChanges.IsCreationAllowed != nil { - cols = append(cols, handler.NewCol(IDPRelationalAllowCreationCol, *optionChanges.IsCreationAllowed)) + changes = append(changes, repo.SetAllowCreation(*optionChanges.IsCreationAllowed)) } if optionChanges.IsLinkingAllowed != nil { - cols = append(cols, handler.NewCol(IDPRelationalAllowLinkingCol, *optionChanges.IsLinkingAllowed)) + changes = append(changes, repo.SetAllowLinking(*optionChanges.IsLinkingAllowed)) } if optionChanges.IsAutoCreation != nil { - cols = append(cols, handler.NewCol(IDPRelationalAllowAutoCreationCol, *optionChanges.IsAutoCreation)) + changes = append(changes, repo.SetAllowAutoCreation(*optionChanges.IsAutoCreation)) } if optionChanges.IsAutoUpdate != nil { - cols = append(cols, handler.NewCol(IDPRelationalAllowAutoUpdateCol, *optionChanges.IsAutoUpdate)) + changes = append(changes, repo.SetAllowAutoUpdate(*optionChanges.IsAutoUpdate)) } - if optionChanges.AutoLinkingOption != nil && *optionChanges.AutoLinkingOption != internal_domain.AutoLinkingOptionUnspecified { - cols = append(cols, handler.NewCol(IDPRelationalAllowAutoLinkingCol, domain.IDPAutoLinkingField(*optionChanges.AutoLinkingOption))) + if optionChanges.AutoLinkingOption != nil { + changes = append(changes, repo.SetAutoLinkingField(mapAutoLinkingField(*optionChanges.AutoLinkingOption))) } - return cols + return changes } -func reduceOAuthIDPRelationalChangedColumns(payload *domain.OAuth, idpEvent *idp.OAuthIDPChangedEvent) bool { - payloadChange := false - if idpEvent.ClientID != nil { - payloadChange = true +func (p *idpTemplateRelationalProjection) reduceOAuthIDPChangedColumns(payload *domain.OAuth, idpEvent *idp.OAuthIDPChangedEvent) bool { + payloadChanged := false + if payload == nil || idpEvent == nil { + return payloadChanged + } + + if idpEvent.ClientID != nil && *idpEvent.ClientID != payload.ClientID { + payloadChanged = true payload.ClientID = *idpEvent.ClientID } if idpEvent.ClientSecret != nil { - payloadChange = true + payloadChanged = true payload.ClientSecret = idpEvent.ClientSecret } - if idpEvent.AuthorizationEndpoint != nil { - payloadChange = true + if idpEvent.AuthorizationEndpoint != nil && *idpEvent.AuthorizationEndpoint != payload.AuthorizationEndpoint { + payloadChanged = true payload.AuthorizationEndpoint = *idpEvent.AuthorizationEndpoint } - if idpEvent.TokenEndpoint != nil { - payloadChange = true + if idpEvent.TokenEndpoint != nil && *idpEvent.TokenEndpoint != payload.TokenEndpoint { + payloadChanged = true payload.TokenEndpoint = *idpEvent.TokenEndpoint } - if idpEvent.UserEndpoint != nil { - payloadChange = true + if idpEvent.UserEndpoint != nil && *idpEvent.UserEndpoint != payload.UserEndpoint { + payloadChanged = true payload.UserEndpoint = *idpEvent.UserEndpoint } - if idpEvent.Scopes != nil { - payloadChange = true + if idpEvent.Scopes != nil && !slices.Equal(idpEvent.Scopes, payload.Scopes) { + payloadChanged = true payload.Scopes = idpEvent.Scopes } - if idpEvent.IDAttribute != nil { - payloadChange = true + if idpEvent.IDAttribute != nil && *idpEvent.IDAttribute != payload.IDAttribute { + payloadChanged = true payload.IDAttribute = *idpEvent.IDAttribute } - if idpEvent.UsePKCE != nil { - payloadChange = true + if idpEvent.UsePKCE != nil && *idpEvent.UsePKCE != payload.UsePKCE { + payloadChanged = true payload.UsePKCE = *idpEvent.UsePKCE } - return payloadChange + return payloadChanged } -func reduceOIDCIDPRelationalChangedColumns(payload *domain.OIDC, idpEvent *idp.OIDCIDPChangedEvent) bool { - payloadChange := false - if idpEvent.ClientID != nil { - payloadChange = true +func (p *idpTemplateRelationalProjection) reduceOIDCIDPChangedColumns(payload *domain.OIDC, idpEvent *idp.OIDCIDPChangedEvent) bool { + payloadChanged := false + if payload == nil || idpEvent == nil { + return payloadChanged + } + + if idpEvent.ClientID != nil && *idpEvent.ClientID != payload.ClientID { + payloadChanged = true payload.ClientID = *idpEvent.ClientID } if idpEvent.ClientSecret != nil { - payloadChange = true + payloadChanged = true payload.ClientSecret = idpEvent.ClientSecret } - if idpEvent.Issuer != nil { - payloadChange = true + if idpEvent.Issuer != nil && *idpEvent.Issuer != payload.Issuer { + payloadChanged = true payload.Issuer = *idpEvent.Issuer } - if idpEvent.Scopes != nil { - payloadChange = true + if idpEvent.Scopes != nil && !slices.Equal(idpEvent.Scopes, payload.Scopes) { + payloadChanged = true payload.Scopes = idpEvent.Scopes } - if idpEvent.IsIDTokenMapping != nil { - payloadChange = true + if idpEvent.IsIDTokenMapping != nil && *idpEvent.IsIDTokenMapping != payload.IsIDTokenMapping { + payloadChanged = true payload.IsIDTokenMapping = *idpEvent.IsIDTokenMapping } - if idpEvent.UsePKCE != nil { - payloadChange = true + if idpEvent.UsePKCE != nil && *idpEvent.UsePKCE != payload.UsePKCE { + payloadChanged = true payload.UsePKCE = *idpEvent.UsePKCE } - return payloadChange + return payloadChanged } -func reduceJWTIDPRelationalChangedColumns(payload *domain.JWT, idpEvent *idp.JWTIDPChangedEvent) bool { - payloadChange := false - if idpEvent.JWTEndpoint != nil { - payloadChange = true +func (p *idpTemplateRelationalProjection) reduceJWTIDPChangedColumns(payload *domain.JWT, idpEvent *idp.JWTIDPChangedEvent) bool { + payloadChanged := false + if payload == nil || idpEvent == nil { + return payloadChanged + } + + if idpEvent.JWTEndpoint != nil && *idpEvent.JWTEndpoint != payload.JWTEndpoint { + payloadChanged = true payload.JWTEndpoint = *idpEvent.JWTEndpoint } - if idpEvent.KeysEndpoint != nil { - payloadChange = true + if idpEvent.KeysEndpoint != nil && *idpEvent.KeysEndpoint != payload.KeysEndpoint { + payloadChanged = true payload.KeysEndpoint = *idpEvent.KeysEndpoint } - if idpEvent.HeaderName != nil { - payloadChange = true + if idpEvent.HeaderName != nil && *idpEvent.HeaderName != payload.HeaderName { + payloadChanged = true payload.HeaderName = *idpEvent.HeaderName } - if idpEvent.Issuer != nil { - payloadChange = true + if idpEvent.Issuer != nil && *idpEvent.Issuer != payload.Issuer { + payloadChanged = true payload.Issuer = *idpEvent.Issuer } - return payloadChange + return payloadChanged } -func reduceAzureADIDPRelationalChangedColumns(payload *domain.Azure, idpEvent *idp.AzureADIDPChangedEvent) (bool, error) { - payloadChange := false - if idpEvent.ClientID != nil { - payloadChange = true +func (p *idpTemplateRelationalProjection) reduceAzureADIDPChangedColumns(payload *domain.Azure, idpEvent *idp.AzureADIDPChangedEvent) (bool, error) { + payloadChanged := false + if payload == nil || idpEvent == nil { + return payloadChanged, nil + } + + if idpEvent.ClientID != nil && *idpEvent.ClientID != payload.ClientID { + payloadChanged = true payload.ClientID = *idpEvent.ClientID } if idpEvent.ClientSecret != nil { - payloadChange = true + payloadChanged = true payload.ClientSecret = idpEvent.ClientSecret } - if idpEvent.Scopes != nil { - payloadChange = true + if idpEvent.Scopes != nil && !slices.Equal(idpEvent.Scopes, payload.Scopes) { + payloadChanged = true payload.Scopes = idpEvent.Scopes } - if idpEvent.Tenant != nil { - payloadChange = true + if idpEvent.Tenant != nil && *idpEvent.Tenant != payload.Tenant.String() { + payloadChanged = true azureTenant, err := domain.AzureTenantTypeString(*idpEvent.Tenant) if err != nil { @@ -2251,273 +2146,306 @@ func reduceAzureADIDPRelationalChangedColumns(payload *domain.Azure, idpEvent *i payload.Tenant = azureTenant } - if idpEvent.IsEmailVerified != nil { - payloadChange = true + if idpEvent.IsEmailVerified != nil && *idpEvent.IsEmailVerified != payload.IsEmailVerified { + payloadChanged = true payload.IsEmailVerified = *idpEvent.IsEmailVerified } - return payloadChange, nil + return payloadChanged, nil } -func reduceGitHubIDPRelationalChangedColumns(payload *domain.Github, idpEvent *idp.GitHubIDPChangedEvent) bool { - payloadChange := false - if idpEvent.ClientID != nil { - payloadChange = true +func (p *idpTemplateRelationalProjection) reduceGitHubIDPChangedColumns(payload *domain.Github, idpEvent *idp.GitHubIDPChangedEvent) bool { + payloadChanged := false + if payload == nil || idpEvent == nil { + return payloadChanged + } + + if idpEvent.ClientID != nil && *idpEvent.ClientID != payload.ClientID { + payloadChanged = true payload.ClientID = *idpEvent.ClientID } if idpEvent.ClientSecret != nil { - payloadChange = true + payloadChanged = true payload.ClientSecret = idpEvent.ClientSecret } - if idpEvent.Scopes != nil { - payloadChange = true + if idpEvent.Scopes != nil && !slices.Equal(idpEvent.Scopes, payload.Scopes) { + payloadChanged = true payload.Scopes = idpEvent.Scopes } - return payloadChange + return payloadChanged } -func reduceGitHubEnterpriseIDPRelationalChangedColumns(payload *domain.GithubEnterprise, idpEvent *idp.GitHubEnterpriseIDPChangedEvent) bool { - payloadChange := false - if idpEvent.ClientID != nil { - payloadChange = true +func (p *idpTemplateRelationalProjection) reduceGitHubEnterpriseIDPChangedColumns(payload *domain.GithubEnterprise, idpEvent *idp.GitHubEnterpriseIDPChangedEvent) bool { + payloadChanged := false + if payload == nil || idpEvent == nil { + return payloadChanged + } + + if idpEvent.ClientID != nil && *idpEvent.ClientID != payload.ClientID { + payloadChanged = true payload.ClientID = *idpEvent.ClientID } if idpEvent.ClientSecret != nil { - payloadChange = true + payloadChanged = true payload.ClientSecret = idpEvent.ClientSecret } - if idpEvent.AuthorizationEndpoint != nil { - payloadChange = true + if idpEvent.AuthorizationEndpoint != nil && *idpEvent.AuthorizationEndpoint != payload.AuthorizationEndpoint { + payloadChanged = true payload.AuthorizationEndpoint = *idpEvent.AuthorizationEndpoint } - if idpEvent.TokenEndpoint != nil { - payloadChange = true + if idpEvent.TokenEndpoint != nil && *idpEvent.TokenEndpoint != payload.TokenEndpoint { + payloadChanged = true payload.TokenEndpoint = *idpEvent.TokenEndpoint } - if idpEvent.UserEndpoint != nil { - payloadChange = true + if idpEvent.UserEndpoint != nil && *idpEvent.UserEndpoint != payload.UserEndpoint { + payloadChanged = true payload.UserEndpoint = *idpEvent.UserEndpoint } - if idpEvent.Scopes != nil { - payloadChange = true + if idpEvent.Scopes != nil && !slices.Equal(idpEvent.Scopes, payload.Scopes) { + payloadChanged = true payload.Scopes = idpEvent.Scopes } - return payloadChange + return payloadChanged } -func reduceGitLabIDPRelationalChangedColumns(payload *domain.Gitlab, idpEvent *idp.GitLabIDPChangedEvent) bool { - payloadChange := false - if idpEvent.ClientID != nil { - payloadChange = true +func (p *idpTemplateRelationalProjection) reduceGitLabIDPChangedColumns(payload *domain.Gitlab, idpEvent *idp.GitLabIDPChangedEvent) bool { + payloadChanged := false + if payload == nil || idpEvent == nil { + return payloadChanged + } + + if idpEvent.ClientID != nil && *idpEvent.ClientID != payload.ClientID { + payloadChanged = true payload.ClientID = *idpEvent.ClientID } if idpEvent.ClientSecret != nil { - payloadChange = true + payloadChanged = true payload.ClientSecret = idpEvent.ClientSecret } - if idpEvent.Scopes != nil { - payloadChange = true + if idpEvent.Scopes != nil && !slices.Equal(idpEvent.Scopes, payload.Scopes) { + payloadChanged = true payload.Scopes = idpEvent.Scopes } - return payloadChange + return payloadChanged } -func reduceGitLabSelfHostedIDPRelationalChangedColumns(payload *domain.GitlabSelfHosting, idpEvent *idp.GitLabSelfHostedIDPChangedEvent) bool { - payloadChange := false - if idpEvent.ClientID != nil { - payloadChange = true +func (p *idpTemplateRelationalProjection) reduceGitLabSelfHostedIDPChangedColumns(payload *domain.GitlabSelfHosted, idpEvent *idp.GitLabSelfHostedIDPChangedEvent) bool { + payloadChanged := false + if payload == nil || idpEvent == nil { + return payloadChanged + } + + if idpEvent.ClientID != nil && *idpEvent.ClientID != payload.ClientID { + payloadChanged = true payload.ClientID = *idpEvent.ClientID } if idpEvent.ClientSecret != nil { - payloadChange = true + payloadChanged = true payload.ClientSecret = idpEvent.ClientSecret } - if idpEvent.Issuer != nil { - payloadChange = true + if idpEvent.Issuer != nil && *idpEvent.Issuer != payload.Issuer { + payloadChanged = true payload.Issuer = *idpEvent.Issuer } - if idpEvent.Scopes != nil { - payloadChange = true + if idpEvent.Scopes != nil && !slices.Equal(idpEvent.Scopes, payload.Scopes) { + payloadChanged = true payload.Scopes = idpEvent.Scopes } - return payloadChange + return payloadChanged } -func reduceGoogleIDPRelationalChangedColumns(payload *domain.Google, idpEvent *idp.GoogleIDPChangedEvent) bool { - payloadChange := false - if idpEvent.ClientID != nil { - payloadChange = true +func (p *idpTemplateRelationalProjection) reduceGoogleIDPChangedColumns(payload *domain.Google, idpEvent *idp.GoogleIDPChangedEvent) bool { + payloadChanged := false + if payload == nil || idpEvent == nil { + return payloadChanged + } + + if idpEvent.ClientID != nil && *idpEvent.ClientID != payload.ClientID { + payloadChanged = true payload.ClientID = *idpEvent.ClientID } if idpEvent.ClientSecret != nil { - payloadChange = true + payloadChanged = true payload.ClientSecret = idpEvent.ClientSecret } - if idpEvent.Scopes != nil { - payloadChange = true + if idpEvent.Scopes != nil && !slices.Equal(idpEvent.Scopes, payload.Scopes) { + payloadChanged = true payload.Scopes = idpEvent.Scopes } - return payloadChange + return payloadChanged } -func reduceLDAPIDPRelationalChangedColumns(payload *domain.LDAP, idpEvent *idp.LDAPIDPChangedEvent) bool { - payloadChange := false - if idpEvent.Servers != nil { - payloadChange = true +//nolint:gocognit +func (p *idpTemplateRelationalProjection) reduceLDAPIDPChangedColumns(payload *domain.LDAP, idpEvent *idp.LDAPIDPChangedEvent) bool { + payloadChanged := false + if payload == nil || idpEvent == nil { + return payloadChanged + } + + if idpEvent.Servers != nil && !slices.Equal(idpEvent.Servers, payload.Servers) { + payloadChanged = true payload.Servers = idpEvent.Servers } - if idpEvent.StartTLS != nil { - payloadChange = true + if idpEvent.StartTLS != nil && *idpEvent.StartTLS != payload.StartTLS { + payloadChanged = true payload.StartTLS = *idpEvent.StartTLS } - if idpEvent.BaseDN != nil { - payloadChange = true + if idpEvent.BaseDN != nil && *idpEvent.BaseDN != payload.BaseDN { + payloadChanged = true payload.BaseDN = *idpEvent.BaseDN } - if idpEvent.BindDN != nil { - payloadChange = true + if idpEvent.BindDN != nil && *idpEvent.BindDN != payload.BindDN { + payloadChanged = true payload.BindDN = *idpEvent.BindDN } if idpEvent.BindPassword != nil { - payloadChange = true + payloadChanged = true payload.BindPassword = idpEvent.BindPassword } - if idpEvent.UserBase != nil { - payloadChange = true + if idpEvent.UserBase != nil && *idpEvent.UserBase != payload.UserBase { + payloadChanged = true payload.UserBase = *idpEvent.UserBase } - if idpEvent.UserObjectClasses != nil { - payloadChange = true + if idpEvent.UserObjectClasses != nil && !slices.Equal(idpEvent.UserObjectClasses, payload.UserObjectClasses) { + payloadChanged = true payload.UserObjectClasses = idpEvent.UserObjectClasses } - if idpEvent.UserFilters != nil { - payloadChange = true + if idpEvent.UserFilters != nil && !slices.Equal(idpEvent.UserFilters, payload.UserFilters) { + payloadChanged = true payload.UserFilters = idpEvent.UserFilters } - if idpEvent.Timeout != nil { - payloadChange = true + if idpEvent.Timeout != nil && *idpEvent.Timeout != payload.Timeout { + payloadChanged = true payload.Timeout = *idpEvent.Timeout } - if idpEvent.RootCA != nil { - payloadChange = true + if idpEvent.RootCA != nil && !slices.Equal(idpEvent.RootCA, payload.RootCA) { + payloadChanged = true payload.RootCA = idpEvent.RootCA } - if idpEvent.IDAttribute != nil { - payloadChange = true + if idpEvent.IDAttribute != nil && *idpEvent.IDAttribute != payload.IDAttribute { + payloadChanged = true payload.IDAttribute = *idpEvent.IDAttribute } - if idpEvent.FirstNameAttribute != nil { - payloadChange = true + if idpEvent.FirstNameAttribute != nil && *idpEvent.FirstNameAttribute != payload.FirstNameAttribute { + payloadChanged = true payload.FirstNameAttribute = *idpEvent.FirstNameAttribute } - if idpEvent.LastNameAttribute != nil { - payloadChange = true + if idpEvent.LastNameAttribute != nil && *idpEvent.LastNameAttribute != payload.LastNameAttribute { + payloadChanged = true payload.LastNameAttribute = *idpEvent.LastNameAttribute } - if idpEvent.DisplayNameAttribute != nil { - payloadChange = true + if idpEvent.DisplayNameAttribute != nil && *idpEvent.DisplayNameAttribute != payload.DisplayNameAttribute { + payloadChanged = true payload.DisplayNameAttribute = *idpEvent.DisplayNameAttribute } - if idpEvent.NickNameAttribute != nil { - payloadChange = true + if idpEvent.NickNameAttribute != nil && *idpEvent.NickNameAttribute != payload.NickNameAttribute { + payloadChanged = true payload.NickNameAttribute = *idpEvent.NickNameAttribute } - if idpEvent.PreferredUsernameAttribute != nil { - payloadChange = true + if idpEvent.PreferredUsernameAttribute != nil && *idpEvent.PreferredUsernameAttribute != payload.PreferredUsernameAttribute { + payloadChanged = true payload.PreferredUsernameAttribute = *idpEvent.PreferredUsernameAttribute } - if idpEvent.EmailAttribute != nil { - payloadChange = true + if idpEvent.EmailAttribute != nil && *idpEvent.EmailAttribute != payload.EmailAttribute { + payloadChanged = true payload.EmailAttribute = *idpEvent.EmailAttribute } - if idpEvent.EmailVerifiedAttribute != nil { - payloadChange = true + if idpEvent.EmailVerifiedAttribute != nil && *idpEvent.EmailVerifiedAttribute != payload.EmailVerifiedAttribute { + payloadChanged = true payload.EmailVerifiedAttribute = *idpEvent.EmailVerifiedAttribute } - if idpEvent.PhoneAttribute != nil { - payloadChange = true + if idpEvent.PhoneAttribute != nil && *idpEvent.PhoneAttribute != payload.PhoneAttribute { + payloadChanged = true payload.PhoneAttribute = *idpEvent.PhoneAttribute } - if idpEvent.PhoneVerifiedAttribute != nil { - payloadChange = true + if idpEvent.PhoneVerifiedAttribute != nil && *idpEvent.PhoneVerifiedAttribute != payload.PhoneVerifiedAttribute { + payloadChanged = true payload.PhoneVerifiedAttribute = *idpEvent.PhoneVerifiedAttribute } - if idpEvent.PreferredLanguageAttribute != nil { - payloadChange = true + if idpEvent.PreferredLanguageAttribute != nil && *idpEvent.PreferredLanguageAttribute != payload.PreferredLanguageAttribute { + payloadChanged = true payload.PreferredLanguageAttribute = *idpEvent.PreferredLanguageAttribute } - if idpEvent.AvatarURLAttribute != nil { - payloadChange = true + if idpEvent.AvatarURLAttribute != nil && *idpEvent.AvatarURLAttribute != payload.AvatarURLAttribute { + payloadChanged = true payload.AvatarURLAttribute = *idpEvent.AvatarURLAttribute } - if idpEvent.ProfileAttribute != nil { - payloadChange = true + if idpEvent.ProfileAttribute != nil && *idpEvent.ProfileAttribute != payload.ProfileAttribute { + payloadChanged = true payload.ProfileAttribute = *idpEvent.ProfileAttribute } - return payloadChange + return payloadChanged } -func reduceAppleIDPRelationalChangedColumns(payload *domain.Apple, idpEvent *idp.AppleIDPChangedEvent) bool { - payloadChange := false - if idpEvent.ClientID != nil { - payloadChange = true +func (p *idpTemplateRelationalProjection) reduceAppleIDPChangedColumns(payload *domain.Apple, idpEvent *idp.AppleIDPChangedEvent) bool { + payloadChanged := false + if payload == nil || idpEvent == nil { + return payloadChanged + } + + if idpEvent.ClientID != nil && *idpEvent.ClientID != payload.ClientID { + payloadChanged = true payload.ClientID = *idpEvent.ClientID } - if idpEvent.TeamID != nil { - payloadChange = true + if idpEvent.TeamID != nil && *idpEvent.TeamID != payload.TeamID { + payloadChanged = true payload.TeamID = *idpEvent.TeamID } - if idpEvent.KeyID != nil { - payloadChange = true + if idpEvent.KeyID != nil && *idpEvent.KeyID != payload.KeyID { + payloadChanged = true payload.KeyID = *idpEvent.KeyID } if idpEvent.PrivateKey != nil { - payloadChange = true + payloadChanged = true payload.PrivateKey = idpEvent.PrivateKey } - if idpEvent.Scopes != nil { - payloadChange = true + if idpEvent.Scopes != nil && !slices.Equal(idpEvent.Scopes, payload.Scopes) { + payloadChanged = true payload.Scopes = idpEvent.Scopes } - return payloadChange + return payloadChanged } -func reduceSAMLIDPRelationalChangedColumns(payload *domain.SAML, idpEvent *idp.SAMLIDPChangedEvent) bool { - payloadChange := false - if idpEvent.Metadata != nil { - payloadChange = true +func (p *idpTemplateRelationalProjection) reduceSAMLIDPChangedColumns(payload *domain.SAML, idpEvent *idp.SAMLIDPChangedEvent) bool { + payloadChanged := false + if payload == nil || idpEvent == nil { + return payloadChanged + } + + if idpEvent.Metadata != nil && !slices.Equal(idpEvent.Metadata, payload.Metadata) { + payloadChanged = true payload.Metadata = idpEvent.Metadata } if idpEvent.Key != nil { - payloadChange = true + payloadChanged = true payload.Key = idpEvent.Key } - if idpEvent.Certificate != nil { - payloadChange = true + if idpEvent.Certificate != nil && !slices.Equal(idpEvent.Certificate, payload.Certificate) { + payloadChanged = true payload.Certificate = idpEvent.Certificate } - if idpEvent.Binding != nil { - payloadChange = true + if idpEvent.Binding != nil && *idpEvent.Binding != payload.Binding { + payloadChanged = true payload.Binding = *idpEvent.Binding } - if idpEvent.WithSignedRequest != nil { - payloadChange = true + if idpEvent.WithSignedRequest != nil && *idpEvent.WithSignedRequest != payload.WithSignedRequest { + payloadChanged = true payload.WithSignedRequest = *idpEvent.WithSignedRequest } - if idpEvent.NameIDFormat != nil { - payloadChange = true + if idpEvent.NameIDFormat != nil && (payload.NameIDFormat == nil || *idpEvent.NameIDFormat != *payload.NameIDFormat) { + payloadChanged = true payload.NameIDFormat = idpEvent.NameIDFormat } - if idpEvent.TransientMappingAttributeName != nil { - payloadChange = true + if idpEvent.TransientMappingAttributeName != nil && *idpEvent.TransientMappingAttributeName != payload.TransientMappingAttributeName { + payloadChanged = true payload.TransientMappingAttributeName = *idpEvent.TransientMappingAttributeName } - if idpEvent.FederatedLogoutEnabled != nil { - payloadChange = true + if idpEvent.FederatedLogoutEnabled != nil && *idpEvent.FederatedLogoutEnabled != payload.FederatedLogoutEnabled { + payloadChanged = true payload.FederatedLogoutEnabled = *idpEvent.FederatedLogoutEnabled } - if idpEvent.SignatureAlgorithm != nil { - payloadChange = true + if idpEvent.SignatureAlgorithm != nil && *idpEvent.SignatureAlgorithm != payload.SignatureAlgorithm { + payloadChanged = true payload.SignatureAlgorithm = *idpEvent.SignatureAlgorithm } - return payloadChange + return payloadChanged }