From 0780c23f7670f99492b71c038d57d8651568ff85 Mon Sep 17 00:00:00 2001 From: adlerhurst Date: Mon, 30 Nov 2020 07:56:38 +0100 Subject: [PATCH] member --- internal/eventstore/v2/eventstore.go | 5 -- internal/v2/business/iam/member.go | 46 ++++++------------- internal/v2/repository/iam/member.go | 9 +++- internal/v2/repository/member/write_model.go | 9 ++-- .../management/mock/management.proto.mock.go | 20 ++++++++ 5 files changed, 48 insertions(+), 41 deletions(-) diff --git a/internal/eventstore/v2/eventstore.go b/internal/eventstore/v2/eventstore.go index b28382235e..af036825ac 100644 --- a/internal/eventstore/v2/eventstore.go +++ b/internal/eventstore/v2/eventstore.go @@ -38,11 +38,6 @@ func (es *Eventstore) Health(ctx context.Context) error { //PushAggregate pushes the aggregate and reduces the new events on the aggregate func (es *Eventstore) PushAggregate(ctx context.Context, writeModel queryReducer, aggregate aggregater) error { - err := es.FilterToQueryReducer(ctx, writeModel) - if err != nil { - return err - } - events, err := es.PushAggregates(ctx, aggregate) if err != nil { return err diff --git a/internal/v2/business/iam/member.go b/internal/v2/business/iam/member.go index 421b5d1c0c..25df66b00c 100644 --- a/internal/v2/business/iam/member.go +++ b/internal/v2/business/iam/member.go @@ -18,33 +18,24 @@ func (r *Repository) AddMember(ctx context.Context, member *iam_model.IAMMember) return nil, caos_errs.ThrowPreconditionFailed(nil, "IAM-W8m4l", "Errors.IAM.MemberInvalid") } - iam, err := r.iamByID(ctx, member.AggregateID) + addedMember := iam_repo.NewMemberWriteModel(member.AggregateID, member.UserID) + err := r.eventstore.FilterToQueryReducer(ctx, addedMember) if err != nil { return nil, err } - - idx, _ := iam.Members.MemberByUserID(member.UserID) - if idx > -1 { - return nil, caos_errs.ThrowPreconditionFailed(nil, "IAM-GPhuz", "Errors.IAM.MemberAlreadyExisting") + if addedMember.Member.IsActive { + return nil, errors.ThrowAlreadyExists(nil, "IAM-PtXi1", "Errors.IAM.Member.AlreadyExists") } - iamAgg := iam_repo.AggregateFromReadModel(iam). + iamAgg := iam_repo.AggregateFromWriteModel(&addedMember.WriteModel). PushMemberAdded(ctx, member.UserID, member.Roles...) - events, err := r.eventstore.PushAggregates(ctx, iamAgg) + err = r.eventstore.PushAggregate(ctx, addedMember, iamAgg) if err != nil { return nil, err } - if err = iam.AppendAndReduce(events...); err != nil { - return nil, err - } - - _, addedMember := iam.Members.MemberByUserID(member.UserID) - if member == nil { - return nil, errors.ThrowInternal(nil, "IAM-nuoDN", "Errors.Internal") - } - return readModelToMember(addedMember), nil + return writeModelToMember(addedMember), nil } //ChangeMember updates an existing member @@ -60,7 +51,7 @@ func (r *Repository) ChangeMember(ctx context.Context, member *iam_model.IAMMemb return nil, err } - iam := iam_repo.AggregateFromWriteModel(&existingMember.Member.WriteModel). + iam := iam_repo.AggregateFromWriteModel(&existingMember.WriteModel). PushMemberChangedFromExisting(ctx, existingMember, member.Roles...) events, err := r.eventstore.PushAggregates(ctx, iam) @@ -77,25 +68,18 @@ func (r *Repository) ChangeMember(ctx context.Context, member *iam_model.IAMMemb } func (r *Repository) RemoveMember(ctx context.Context, member *iam_model.IAMMember) error { - iam, err := r.iamByID(ctx, member.AggregateID) - if err != nil { + m, err := r.memberWriteModelByID(ctx, member.AggregateID, member.UserID) + if err != nil && !errors.IsNotFound(err) { return err } - - i, _ := iam.Members.MemberByUserID(member.UserID) - if i == -1 { + if errors.IsNotFound(err) { return nil } - iamAgg := iam_repo.AggregateFromReadModel(iam). + iamAgg := iam_repo.AggregateFromWriteModel(&m.WriteModel). PushEvents(iam_repo.NewMemberRemovedEvent(ctx, member.UserID)) - events, err := r.eventstore.PushAggregates(ctx, iamAgg) - if err != nil { - return err - } - - return iam.AppendAndReduce(events...) + return r.eventstore.PushAggregate(ctx, m, iamAgg) } func (r *Repository) MemberByID(ctx context.Context, iamID, userID string) (member *iam_repo.MemberReadModel, err error) { @@ -124,13 +108,13 @@ func (r *Repository) memberWriteModelByID(ctx context.Context, iamID, userID str ctx, span := tracing.NewSpan(ctx) defer func() { span.EndWithError(err) }() - writeModel := iam_repo.NewMemberReadModel(iamID, userID) + writeModel := iam_repo.NewMemberWriteModel(iamID, userID) err = r.eventstore.FilterToQueryReducer(ctx, writeModel) if err != nil { return nil, err } - if writeModel.Member.IsRemoved { + if !writeModel.Member.IsActive { return nil, errors.ThrowNotFound(nil, "IAM-D8JxR", "Errors.NotFound") } diff --git a/internal/v2/repository/iam/member.go b/internal/v2/repository/iam/member.go index cc52d88dcc..0ab7b17688 100644 --- a/internal/v2/repository/iam/member.go +++ b/internal/v2/repository/iam/member.go @@ -39,7 +39,7 @@ type MemberWriteModel struct { iamID string } -func NewMemberReadModel(iamID, userID string) *MemberWriteModel { +func NewMemberWriteModel(iamID, userID string) *MemberWriteModel { return &MemberWriteModel{ userID: userID, iamID: iamID, @@ -69,6 +69,13 @@ func (wm *MemberWriteModel) AppendEvents(events ...eventstore.EventReader) { } } +func (wm *MemberWriteModel) Reduce() error { + if err := wm.Member.Reduce(); err != nil { + return err + } + return wm.WriteModel.Reduce() +} + func (wm *MemberWriteModel) Query() *eventstore.SearchQueryFactory { return eventstore.NewSearchQueryFactory(eventstore.ColumnsEvent, AggregateType). AggregateIDs(wm.iamID) diff --git a/internal/v2/repository/member/write_model.go b/internal/v2/repository/member/write_model.go index b92f115d2a..be0283a963 100644 --- a/internal/v2/repository/member/write_model.go +++ b/internal/v2/repository/member/write_model.go @@ -8,9 +8,9 @@ import "github.com/caos/zitadel/internal/eventstore/v2" type WriteModel struct { eventstore.WriteModel - UserID string - Roles []string - IsRemoved bool + UserID string + Roles []string + IsActive bool } func NewWriteModel(userID string) *WriteModel { @@ -26,11 +26,12 @@ func (wm *WriteModel) Reduce() error { case *AddedEvent: wm.UserID = e.UserID wm.Roles = e.Roles + wm.IsActive = true case *ChangedEvent: wm.Roles = e.Roles case *RemovedEvent: wm.Roles = nil - wm.IsRemoved = true + wm.IsActive = false } } return wm.WriteModel.Reduce() diff --git a/pkg/grpc/management/mock/management.proto.mock.go b/pkg/grpc/management/mock/management.proto.mock.go index b76d47eaf5..1c92530fee 100644 --- a/pkg/grpc/management/mock/management.proto.mock.go +++ b/pkg/grpc/management/mock/management.proto.mock.go @@ -1836,6 +1836,26 @@ func (mr *MockManagementServiceClientMockRecorder) RemoveLoginPolicy(arg0, arg1 return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "RemoveLoginPolicy", reflect.TypeOf((*MockManagementServiceClient)(nil).RemoveLoginPolicy), varargs...) } +// RemoveMfaOTP mocks base method +func (m *MockManagementServiceClient) RemoveMfaOTP(arg0 context.Context, arg1 *management.UserID, arg2 ...grpc.CallOption) (*emptypb.Empty, error) { + m.ctrl.T.Helper() + varargs := []interface{}{arg0, arg1} + for _, a := range arg2 { + varargs = append(varargs, a) + } + ret := m.ctrl.Call(m, "RemoveMfaOTP", varargs...) + ret0, _ := ret[0].(*emptypb.Empty) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// RemoveMfaOTP indicates an expected call of RemoveMfaOTP +func (mr *MockManagementServiceClientMockRecorder) RemoveMfaOTP(arg0, arg1 interface{}, arg2 ...interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + varargs := append([]interface{}{arg0, arg1}, arg2...) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "RemoveMfaOTP", reflect.TypeOf((*MockManagementServiceClient)(nil).RemoveMfaOTP), varargs...) +} + // RemoveMultiFactorFromLoginPolicy mocks base method func (m *MockManagementServiceClient) RemoveMultiFactorFromLoginPolicy(arg0 context.Context, arg1 *management.MultiFactor, arg2 ...grpc.CallOption) (*emptypb.Empty, error) { m.ctrl.T.Helper()