From bb9747923ce38b67273a32b88fef1d09940efb02 Mon Sep 17 00:00:00 2001 From: Fabi <38692350+fgerschwiler@users.noreply.github.com> Date: Mon, 19 Oct 2020 16:28:06 +0200 Subject: [PATCH] fix: cascade remove external login (#871) * fix: cascade remove external login * fix: tests --- .../repository/eventsourcing/eventstore_mock_test.go | 4 ++-- internal/user/repository/eventsourcing/user.go | 9 +++++---- internal/user/repository/eventsourcing/user_test.go | 5 +++-- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/internal/user/repository/eventsourcing/eventstore_mock_test.go b/internal/user/repository/eventsourcing/eventstore_mock_test.go index 44c8daff5c..ec0e0cc9e0 100644 --- a/internal/user/repository/eventsourcing/eventstore_mock_test.go +++ b/internal/user/repository/eventsourcing/eventstore_mock_test.go @@ -472,8 +472,8 @@ func GetMockManipulateUserWithExternalIDP(ctrl *gomock.Controller) *UserEventsto dataUser, _ := json.Marshal(user) dataIDP, _ := json.Marshal(externalIDP) events := []*es_models.Event{ - {AggregateID: "AggregateID", AggregateVersion: "v1", Sequence: 1, Type: model.UserAdded, Data: dataUser}, - {AggregateID: "AggregateID", AggregateVersion: "v1", Sequence: 1, Type: model.HumanExternalIDPAdded, Data: dataIDP}, + {AggregateID: "AggregateID", AggregateVersion: "v1", Sequence: 1, Type: model.UserAdded, Data: dataUser, ResourceOwner: "ResourceOwner"}, + {AggregateID: "AggregateID", AggregateVersion: "v1", Sequence: 1, Type: model.HumanExternalIDPAdded, Data: dataIDP, ResourceOwner: "ResourceOwner"}, } mockEs := mock.NewMockEventstore(ctrl) mockEs.EXPECT().FilterEvents(gomock.Any(), gomock.Any()).Return(events, nil) diff --git a/internal/user/repository/eventsourcing/user.go b/internal/user/repository/eventsourcing/user.go index 754991f1b6..3dcf50a0d4 100644 --- a/internal/user/repository/eventsourcing/user.go +++ b/internal/user/repository/eventsourcing/user.go @@ -836,7 +836,7 @@ func ExternalIDPRemovedAggregate(ctx context.Context, aggCreator *es_models.Aggr } aggregates := make([]*es_models.Aggregate, 0) - agg, err := UserAggregate(ctx, aggCreator, user) + agg, err := UserAggregateOverwriteContext(ctx, aggCreator, user, user.ResourceOwner, authz.GetCtxData(ctx).UserID) if err != nil { return nil, err } @@ -845,7 +845,7 @@ func ExternalIDPRemovedAggregate(ctx context.Context, aggCreator *es_models.Aggr } else { agg, err = agg.AppendEvent(model.HumanExternalIDPRemoved, externalIDP) } - uniqueReleasedAggregate, err := releasedUniqueExternalIDPAggregate(ctx, aggCreator, externalIDP) + uniqueReleasedAggregate, err := releasedUniqueExternalIDPAggregate(ctx, aggCreator, externalIDP, user.ResourceOwner) if err != nil { return nil, err } @@ -870,9 +870,10 @@ func reservedUniqueExternalIDPAggregate(ctx context.Context, aggCreator *es_mode return aggregate.SetPrecondition(UserExternalIDPUniqueQuery(uniqueExternlIDP), isEventValidation(aggregate, model.HumanExternalIDPReserved)), nil } -func releasedUniqueExternalIDPAggregate(ctx context.Context, aggCreator *es_models.AggregateCreator, externalIDP *model.ExternalIDP) (aggregate *es_models.Aggregate, err error) { +func releasedUniqueExternalIDPAggregate(ctx context.Context, aggCreator *es_models.AggregateCreator, externalIDP *model.ExternalIDP, resourceOwnerID string) (aggregate *es_models.Aggregate, err error) { uniqueExternlIDP := externalIDP.IDPConfigID + externalIDP.UserID - aggregate, err = aggCreator.NewAggregate(ctx, uniqueExternlIDP, model.UserExternalIDPAggregate, model.UserVersion, 0) + aggregate, err = aggCreator.NewAggregate(ctx, uniqueExternlIDP, model.UserExternalIDPAggregate, model.UserVersion, 0, es_models.OverwriteResourceOwner(resourceOwnerID), es_models.OverwriteEditorUser(authz.GetCtxData(ctx).UserID)) + if err != nil { return nil, err } diff --git a/internal/user/repository/eventsourcing/user_test.go b/internal/user/repository/eventsourcing/user_test.go index 7d4b978ef6..9e5917b150 100644 --- a/internal/user/repository/eventsourcing/user_test.go +++ b/internal/user/repository/eventsourcing/user_test.go @@ -2388,8 +2388,9 @@ func TestExternalIDPRemovedAggregates(t *testing.T) { aggCreator: models.NewAggregateCreator("test"), user: &model.User{ ObjectRoot: models.ObjectRoot{ - AggregateID: "AggregateID", - Sequence: 5, + AggregateID: "AggregateID", + Sequence: 5, + ResourceOwner: "ResourceOwner", }, }, externalIDP: &model.ExternalIDP{