2020-05-18 09:32:16 +00:00
|
|
|
package eventsourcing
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"github.com/caos/zitadel/internal/errors"
|
|
|
|
es_models "github.com/caos/zitadel/internal/eventstore/models"
|
|
|
|
"github.com/caos/zitadel/internal/iam/repository/eventsourcing/model"
|
|
|
|
)
|
|
|
|
|
|
|
|
func IamByIDQuery(id string, latestSequence uint64) (*es_models.SearchQuery, error) {
|
|
|
|
if id == "" {
|
2020-06-22 11:51:44 +00:00
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-0soe4", "Errors.Iam.IDMissing")
|
2020-05-18 09:32:16 +00:00
|
|
|
}
|
|
|
|
return IamQuery(latestSequence).
|
|
|
|
AggregateIDFilter(id), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func IamQuery(latestSequence uint64) *es_models.SearchQuery {
|
|
|
|
return es_models.NewSearchQuery().
|
|
|
|
AggregateTypeFilter(model.IamAggregate).
|
|
|
|
LatestSequenceFilter(latestSequence)
|
|
|
|
}
|
|
|
|
|
|
|
|
func IamAggregate(ctx context.Context, aggCreator *es_models.AggregateCreator, iam *model.Iam) (*es_models.Aggregate, error) {
|
|
|
|
if iam == nil {
|
2020-06-22 11:51:44 +00:00
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-lo04e", "Errors.Internal")
|
2020-05-18 09:32:16 +00:00
|
|
|
}
|
|
|
|
return aggCreator.NewAggregate(ctx, iam.AggregateID, model.IamAggregate, model.IamVersion, iam.Sequence)
|
|
|
|
}
|
|
|
|
|
|
|
|
func IamAggregateOverwriteContext(ctx context.Context, aggCreator *es_models.AggregateCreator, iam *model.Iam, resourceOwnerID string, userID string) (*es_models.Aggregate, error) {
|
|
|
|
if iam == nil {
|
2020-06-22 11:51:44 +00:00
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-dis83", "Errors.Internal")
|
2020-05-18 09:32:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return aggCreator.NewAggregate(ctx, iam.AggregateID, model.IamAggregate, model.IamVersion, iam.Sequence, es_models.OverwriteResourceOwner(resourceOwnerID), es_models.OverwriteEditorUser(userID))
|
|
|
|
}
|
|
|
|
|
|
|
|
func IamSetupStartedAggregate(aggCreator *es_models.AggregateCreator, iam *model.Iam) func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
agg, err := IamAggregate(ctx, aggCreator, iam)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return agg.AppendEvent(model.IamSetupStarted, nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func IamSetupDoneAggregate(aggCreator *es_models.AggregateCreator, iam *model.Iam) func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
agg, err := IamAggregate(ctx, aggCreator, iam)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return agg.AppendEvent(model.IamSetupDone, nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func IamSetGlobalOrgAggregate(aggCreator *es_models.AggregateCreator, iam *model.Iam, globalOrg string) func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
if globalOrg == "" {
|
2020-06-22 11:51:44 +00:00
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-8siwa", "Errors.Iam.GlobalOrgMissing")
|
2020-05-18 09:32:16 +00:00
|
|
|
}
|
|
|
|
agg, err := IamAggregate(ctx, aggCreator, iam)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return agg.AppendEvent(model.GlobalOrgSet, &model.Iam{GlobalOrgID: globalOrg})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func IamSetIamProjectAggregate(aggCreator *es_models.AggregateCreator, iam *model.Iam, projectID string) func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
if projectID == "" {
|
2020-06-22 11:51:44 +00:00
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-sjuw3", "Errors.Iam.IamProjectIDMisisng")
|
2020-05-18 09:32:16 +00:00
|
|
|
}
|
|
|
|
agg, err := IamAggregate(ctx, aggCreator, iam)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return agg.AppendEvent(model.IamProjectSet, &model.Iam{IamProjectID: projectID})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func IamMemberAddedAggregate(aggCreator *es_models.AggregateCreator, existing *model.Iam, member *model.IamMember) func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
if member == nil {
|
2020-06-22 11:51:44 +00:00
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-9sope", "Errors.Internal")
|
2020-05-18 09:32:16 +00:00
|
|
|
}
|
|
|
|
agg, err := IamAggregate(ctx, aggCreator, existing)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return agg.AppendEvent(model.IamMemberAdded, member)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func IamMemberChangedAggregate(aggCreator *es_models.AggregateCreator, existing *model.Iam, member *model.IamMember) func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
if member == nil {
|
2020-06-22 11:51:44 +00:00
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-38skf", "Errors.Internal")
|
2020-05-18 09:32:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
agg, err := IamAggregate(ctx, aggCreator, existing)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return agg.AppendEvent(model.IamMemberChanged, member)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func IamMemberRemovedAggregate(aggCreator *es_models.AggregateCreator, existing *model.Iam, member *model.IamMember) func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
if member == nil {
|
2020-06-22 11:51:44 +00:00
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-90lsw", "Errors.Internal")
|
2020-05-18 09:32:16 +00:00
|
|
|
}
|
|
|
|
agg, err := IamAggregate(ctx, aggCreator, existing)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return agg.AppendEvent(model.IamMemberRemoved, member)
|
|
|
|
}
|
|
|
|
}
|