fix: org member change and remove (#1014)

* fix: member

* fix: test

* fix: test

* fix: tests
This commit is contained in:
Silvan 2020-11-24 15:55:38 +01:00 committed by GitHub
parent 75bf0409c4
commit 78a1b8f019
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 39 additions and 21 deletions

View File

@ -85,13 +85,13 @@ func (o *Org) GetPrimaryDomain() *OrgDomain {
return nil return nil
} }
func (o *Org) ContainsMember(userID string) bool { func (o *Org) MemeberByUserID(userID string) (*OrgMember, int) {
for _, member := range o.Members { for i, member := range o.Members {
if member.UserID == userID { if member.UserID == userID {
return true return member, i
} }
} }
return false return nil, -1
} }
func (o *Org) nameForDomain(iamDomain string) string { func (o *Org) nameForDomain(iamDomain string) string {

View File

@ -429,16 +429,20 @@ func (es *OrgEventstore) ChangeOrgMember(ctx context.Context, member *org_model.
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-ara6l", "Errors.Org.InvalidMember") return nil, errors.ThrowPreconditionFailed(nil, "EVENT-ara6l", "Errors.Org.InvalidMember")
} }
existingMember, err := es.OrgMemberByIDs(ctx, member) org, err := es.OrgByID(ctx, &org_model.Org{ObjectRoot: es_models.ObjectRoot{AggregateID: member.AggregateID, Sequence: member.Sequence}})
if err != nil { if err != nil {
return nil, err return nil, err
} }
existingMember, _ := org.MemeberByUserID(member.UserID)
if existingMember == nil {
return nil, errors.ThrowNotFound(nil, "EVENT-VB2Pn", "Errors.Org.MemberNotExisting")
}
member.ObjectRoot = existingMember.ObjectRoot repoOrg := model.OrgFromModel(org)
repoMember := model.OrgMemberFromModel(member) repoMember := model.OrgMemberFromModel(member)
repoExistingMember := model.OrgMemberFromModel(existingMember) repoExistingMember := model.OrgMemberFromModel(existingMember)
orgAggregate := orgMemberChangedAggregate(es.Eventstore.AggregateCreator(), repoExistingMember, repoMember) orgAggregate := orgMemberChangedAggregate(es.Eventstore.AggregateCreator(), repoOrg, repoExistingMember, repoMember)
err = es_sdk.Push(ctx, es.PushAggregates, repoMember.AppendEvents, orgAggregate) err = es_sdk.Push(ctx, es.PushAggregates, repoMember.AppendEvents, orgAggregate)
if err != nil { if err != nil {
return nil, err return nil, err
@ -452,18 +456,19 @@ func (es *OrgEventstore) RemoveOrgMember(ctx context.Context, member *org_model.
return errors.ThrowInvalidArgument(nil, "EVENT-d43fs", "Errors.Org.UserIDMissing") return errors.ThrowInvalidArgument(nil, "EVENT-d43fs", "Errors.Org.UserIDMissing")
} }
existingMember, err := es.OrgMemberByIDs(ctx, member) org, err := es.OrgByID(ctx, &org_model.Org{ObjectRoot: es_models.ObjectRoot{AggregateID: member.AggregateID, Sequence: member.Sequence}})
if errors.IsNotFound(err) {
return nil
}
if err != nil { if err != nil {
return err return err
} }
existingMember, _ := org.MemeberByUserID(member.UserID)
if existingMember == nil {
return nil
}
member.ObjectRoot = existingMember.ObjectRoot repoOrg := model.OrgFromModel(org)
repoMember := model.OrgMemberFromModel(member) repoMember := model.OrgMemberFromModel(member)
orgAggregate := orgMemberRemovedAggregate(es.Eventstore.AggregateCreator(), repoMember) orgAggregate := orgMemberRemovedAggregate(es.Eventstore.AggregateCreator(), repoOrg, repoMember)
return es_sdk.Push(ctx, es.PushAggregates, repoMember.AppendEvents, orgAggregate) return es_sdk.Push(ctx, es.PushAggregates, repoMember.AppendEvents, orgAggregate)
} }

View File

@ -2,6 +2,7 @@ package eventsourcing
import ( import (
"context" "context"
"github.com/caos/zitadel/internal/org/repository/eventsourcing/model" "github.com/caos/zitadel/internal/org/repository/eventsourcing/model"
"github.com/caos/zitadel/internal/errors" "github.com/caos/zitadel/internal/errors"
@ -31,9 +32,9 @@ func orgMemberAddedAggregate(ctx context.Context, aggCreator *es_models.Aggregat
return agg.SetPrecondition(validationQuery, validation).AppendEvent(model.OrgMemberAdded, member) return agg.SetPrecondition(validationQuery, validation).AppendEvent(model.OrgMemberAdded, member)
} }
func orgMemberChangedAggregate(aggCreator *es_models.AggregateCreator, existingMember *model.OrgMember, member *model.OrgMember) func(ctx context.Context) (*es_models.Aggregate, error) { func orgMemberChangedAggregate(aggCreator *es_models.AggregateCreator, org *model.Org, existingMember, member *model.OrgMember) func(ctx context.Context) (*es_models.Aggregate, error) {
return func(ctx context.Context) (*es_models.Aggregate, error) { return func(ctx context.Context) (*es_models.Aggregate, error) {
if member == nil || existingMember == nil { if member == nil || org == nil || existingMember == nil {
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-d34fs", "Errors.Internal") return nil, errors.ThrowPreconditionFailed(nil, "EVENT-d34fs", "Errors.Internal")
} }
@ -42,7 +43,7 @@ func orgMemberChangedAggregate(aggCreator *es_models.AggregateCreator, existingM
return nil, errors.ThrowInvalidArgument(nil, "EVENT-VLMGn", "Errors.NoChangesFound") return nil, errors.ThrowInvalidArgument(nil, "EVENT-VLMGn", "Errors.NoChangesFound")
} }
agg, err := OrgAggregate(ctx, aggCreator, existingMember.AggregateID, existingMember.Sequence) agg, err := OrgAggregate(ctx, aggCreator, org.AggregateID, org.Sequence)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -50,13 +51,13 @@ func orgMemberChangedAggregate(aggCreator *es_models.AggregateCreator, existingM
} }
} }
func orgMemberRemovedAggregate(aggCreator *es_models.AggregateCreator, member *model.OrgMember) func(ctx context.Context) (*es_models.Aggregate, error) { func orgMemberRemovedAggregate(aggCreator *es_models.AggregateCreator, org *model.Org, member *model.OrgMember) func(ctx context.Context) (*es_models.Aggregate, error) {
return func(ctx context.Context) (*es_models.Aggregate, error) { return func(ctx context.Context) (*es_models.Aggregate, error) {
if member == nil { if member == nil {
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-dieu7", "member must not be nil") return nil, errors.ThrowPreconditionFailed(nil, "EVENT-vNPVX", "Errors.Internal")
} }
agg, err := OrgAggregate(ctx, aggCreator, member.AggregateID, member.Sequence) agg, err := OrgAggregate(ctx, aggCreator, org.AggregateID, org.Sequence)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -78,6 +78,7 @@ func TestOrgMemberChangedAggregate(t *testing.T) {
} }
type args struct { type args struct {
aggCreator *es_models.AggregateCreator aggCreator *es_models.AggregateCreator
org *model.Org
existingMember *model.OrgMember existingMember *model.OrgMember
member *model.OrgMember member *model.OrgMember
ctx context.Context ctx context.Context
@ -92,6 +93,7 @@ func TestOrgMemberChangedAggregate(t *testing.T) {
args: args{ args: args{
aggCreator: es_models.NewAggregateCreator("test"), aggCreator: es_models.NewAggregateCreator("test"),
ctx: authz.NewMockContext("org", "user"), ctx: authz.NewMockContext("org", "user"),
org: &model.Org{},
member: nil, member: nil,
existingMember: &model.OrgMember{}, existingMember: &model.OrgMember{},
}, },
@ -104,6 +106,7 @@ func TestOrgMemberChangedAggregate(t *testing.T) {
args: args{ args: args{
aggCreator: es_models.NewAggregateCreator("test"), aggCreator: es_models.NewAggregateCreator("test"),
ctx: authz.NewMockContext("org", "user"), ctx: authz.NewMockContext("org", "user"),
org: &model.Org{},
existingMember: nil, existingMember: nil,
member: &model.OrgMember{}, member: &model.OrgMember{},
}, },
@ -122,6 +125,7 @@ func TestOrgMemberChangedAggregate(t *testing.T) {
existingMember: &model.OrgMember{ existingMember: &model.OrgMember{
ObjectRoot: es_models.ObjectRoot{AggregateID: "asdf", Sequence: 234}, ObjectRoot: es_models.ObjectRoot{AggregateID: "asdf", Sequence: 234},
}, },
org: &model.Org{},
}, },
res: res{ res: res{
isErr: errors.IsErrorInvalidArgument, isErr: errors.IsErrorInvalidArgument,
@ -140,6 +144,9 @@ func TestOrgMemberChangedAggregate(t *testing.T) {
ObjectRoot: es_models.ObjectRoot{AggregateID: "asdf", Sequence: 234}, ObjectRoot: es_models.ObjectRoot{AggregateID: "asdf", Sequence: 234},
Roles: []string{"asdf", "woeri"}, Roles: []string{"asdf", "woeri"},
}, },
org: &model.Org{
ObjectRoot: es_models.ObjectRoot{AggregateID: "asdf", Sequence: 234},
},
}, },
res: res{ res: res{
isErr: nil, isErr: nil,
@ -149,7 +156,7 @@ func TestOrgMemberChangedAggregate(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
aggregateCreator := orgMemberChangedAggregate(tt.args.aggCreator, tt.args.existingMember, tt.args.member) aggregateCreator := orgMemberChangedAggregate(tt.args.aggCreator, tt.args.org, tt.args.existingMember, tt.args.member)
aggregate, err := aggregateCreator(tt.args.ctx) aggregate, err := aggregateCreator(tt.args.ctx)
if tt.res.isErr == nil && err != nil { if tt.res.isErr == nil && err != nil {
t.Errorf("no error expected got: %v", err) t.Errorf("no error expected got: %v", err)
@ -174,6 +181,7 @@ func TestOrgMemberRemovedAggregate(t *testing.T) {
} }
type args struct { type args struct {
aggCreator *es_models.AggregateCreator aggCreator *es_models.AggregateCreator
org *model.Org
member *model.OrgMember member *model.OrgMember
ctx context.Context ctx context.Context
} }
@ -187,6 +195,7 @@ func TestOrgMemberRemovedAggregate(t *testing.T) {
args: args{ args: args{
aggCreator: es_models.NewAggregateCreator("test"), aggCreator: es_models.NewAggregateCreator("test"),
ctx: authz.NewMockContext("org", "user"), ctx: authz.NewMockContext("org", "user"),
org: &model.Org{},
member: nil, member: nil,
}, },
res: res{ res: res{
@ -198,6 +207,9 @@ func TestOrgMemberRemovedAggregate(t *testing.T) {
args: args{ args: args{
aggCreator: es_models.NewAggregateCreator("test"), aggCreator: es_models.NewAggregateCreator("test"),
ctx: authz.NewMockContext("org", "user"), ctx: authz.NewMockContext("org", "user"),
org: &model.Org{
ObjectRoot: es_models.ObjectRoot{AggregateID: "asdf", Sequence: 234},
},
member: &model.OrgMember{ member: &model.OrgMember{
ObjectRoot: es_models.ObjectRoot{AggregateID: "asdf", Sequence: 234}, ObjectRoot: es_models.ObjectRoot{AggregateID: "asdf", Sequence: 234},
}, },
@ -210,7 +222,7 @@ func TestOrgMemberRemovedAggregate(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
aggregateCreator := orgMemberRemovedAggregate(tt.args.aggCreator, tt.args.member) aggregateCreator := orgMemberRemovedAggregate(tt.args.aggCreator, tt.args.org, tt.args.member)
aggregate, err := aggregateCreator(tt.args.ctx) aggregate, err := aggregateCreator(tt.args.ctx)
if tt.res.isErr == nil && err != nil { if tt.res.isErr == nil && err != nil {
t.Errorf("no error expected got: %v", err) t.Errorf("no error expected got: %v", err)