This commit is contained in:
adlerhurst 2020-11-30 07:56:38 +01:00
parent c2400e1da5
commit 0780c23f76
5 changed files with 48 additions and 41 deletions

View File

@ -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

View File

@ -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")
}

View File

@ -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)

View File

@ -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()

View File

@ -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()