2020-05-11 10:16:27 +02:00
|
|
|
package eventsourcing
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"github.com/caos/zitadel/internal/errors"
|
|
|
|
es_models "github.com/caos/zitadel/internal/eventstore/models"
|
2020-05-18 12:06:36 +02:00
|
|
|
es_sdk "github.com/caos/zitadel/internal/eventstore/sdk"
|
2020-05-11 10:16:27 +02:00
|
|
|
"github.com/caos/zitadel/internal/user/repository/eventsourcing/model"
|
|
|
|
)
|
|
|
|
|
|
|
|
func UserByIDQuery(id string, latestSequence uint64) (*es_models.SearchQuery, error) {
|
|
|
|
if id == "" {
|
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-d8isw", "id should be filled")
|
|
|
|
}
|
|
|
|
return UserQuery(latestSequence).
|
|
|
|
AggregateIDFilter(id), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func UserQuery(latestSequence uint64) *es_models.SearchQuery {
|
|
|
|
return es_models.NewSearchQuery().
|
|
|
|
AggregateTypeFilter(model.UserAggregate).
|
|
|
|
LatestSequenceFilter(latestSequence)
|
|
|
|
}
|
|
|
|
|
2020-05-28 06:49:22 +02:00
|
|
|
func UserUserNameUniqueQuery(userName string) *es_models.SearchQuery {
|
|
|
|
return es_models.NewSearchQuery().
|
|
|
|
AggregateTypeFilter(model.UserUserNameAggregate).
|
|
|
|
AggregateIDFilter(userName).
|
|
|
|
OrderDesc().
|
|
|
|
SetLimit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func UserEmailUniqueQuery(email string) *es_models.SearchQuery {
|
|
|
|
return es_models.NewSearchQuery().
|
|
|
|
AggregateTypeFilter(model.UserEmailAggregate).
|
|
|
|
AggregateIDFilter(email).
|
|
|
|
OrderDesc().
|
|
|
|
SetLimit(1)
|
|
|
|
}
|
|
|
|
|
2020-05-11 10:16:27 +02:00
|
|
|
func UserAggregate(ctx context.Context, aggCreator *es_models.AggregateCreator, user *model.User) (*es_models.Aggregate, error) {
|
|
|
|
if user == nil {
|
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-dis83", "existing user should not be nil")
|
|
|
|
}
|
|
|
|
return aggCreator.NewAggregate(ctx, user.AggregateID, model.UserAggregate, model.UserVersion, user.Sequence)
|
|
|
|
}
|
|
|
|
|
|
|
|
func UserAggregateOverwriteContext(ctx context.Context, aggCreator *es_models.AggregateCreator, user *model.User, resourceOwnerID string, userID string) (*es_models.Aggregate, error) {
|
|
|
|
if user == nil {
|
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-dis83", "existing user should not be nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
return aggCreator.NewAggregate(ctx, user.AggregateID, model.UserAggregate, model.UserVersion, user.Sequence, es_models.OverwriteResourceOwner(resourceOwnerID), es_models.OverwriteEditorUser(userID))
|
|
|
|
}
|
|
|
|
|
2020-05-28 06:49:22 +02:00
|
|
|
func UserCreateAggregate(ctx context.Context, aggCreator *es_models.AggregateCreator, user *model.User, initCode *model.InitUserCode, phoneCode *model.PhoneCode, resourceOwner string) (_ []*es_models.Aggregate, err error) {
|
2020-05-13 14:22:29 +02:00
|
|
|
if user == nil {
|
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-duxk2", "user should not be nil")
|
|
|
|
}
|
2020-05-11 10:16:27 +02:00
|
|
|
|
2020-05-28 06:49:22 +02:00
|
|
|
var agg *es_models.Aggregate
|
2020-05-13 14:22:29 +02:00
|
|
|
if resourceOwner != "" {
|
|
|
|
agg, err = UserAggregateOverwriteContext(ctx, aggCreator, user, user.AggregateID, resourceOwner)
|
|
|
|
} else {
|
|
|
|
agg, err = UserAggregate(ctx, aggCreator, user)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
agg, err = agg.AppendEvent(model.UserAdded, user)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if user.Email != nil && user.EmailAddress != "" && user.IsEmailVerified {
|
|
|
|
agg, err = agg.AppendEvent(model.UserEmailVerified, nil)
|
2020-05-11 10:16:27 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-13 14:22:29 +02:00
|
|
|
}
|
|
|
|
if user.Phone != nil && user.PhoneNumber != "" && user.IsPhoneVerified {
|
|
|
|
agg, err = agg.AppendEvent(model.UserPhoneVerified, nil)
|
2020-05-11 10:16:27 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2020-05-13 14:22:29 +02:00
|
|
|
if initCode != nil {
|
|
|
|
agg, err = agg.AppendEvent(model.InitializedUserCodeAdded, initCode)
|
2020-05-11 10:16:27 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-13 14:22:29 +02:00
|
|
|
}
|
|
|
|
if phoneCode != nil {
|
|
|
|
agg, err = agg.AppendEvent(model.UserPhoneCodeAdded, phoneCode)
|
2020-05-11 10:16:27 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2020-05-28 06:49:22 +02:00
|
|
|
uniqueAggregates, err := getUniqueUserAggregates(ctx, aggCreator, user, resourceOwner)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return []*es_models.Aggregate{
|
|
|
|
agg,
|
|
|
|
uniqueAggregates[0],
|
|
|
|
uniqueAggregates[1],
|
|
|
|
}, nil
|
2020-05-13 14:22:29 +02:00
|
|
|
}
|
|
|
|
|
2020-05-28 06:49:22 +02:00
|
|
|
func UserRegisterAggregate(ctx context.Context, aggCreator *es_models.AggregateCreator, user *model.User, resourceOwner string, emailCode *model.EmailCode) ([]*es_models.Aggregate, error) {
|
2020-05-13 14:22:29 +02:00
|
|
|
if user == nil || resourceOwner == "" || emailCode == nil {
|
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-duxk2", "user, resourceowner, emailcode should not be nothing")
|
|
|
|
}
|
|
|
|
|
|
|
|
agg, err := UserAggregateOverwriteContext(ctx, aggCreator, user, resourceOwner, user.AggregateID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
agg, err = agg.AppendEvent(model.UserRegistered, user)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-28 06:49:22 +02:00
|
|
|
agg, err = agg.AppendEvent(model.UserEmailCodeAdded, emailCode)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
uniqueAggregates, err := getUniqueUserAggregates(ctx, aggCreator, user, resourceOwner)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return []*es_models.Aggregate{
|
|
|
|
agg,
|
|
|
|
uniqueAggregates[0],
|
|
|
|
uniqueAggregates[1],
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getUniqueUserAggregates(ctx context.Context, aggCreator *es_models.AggregateCreator, user *model.User, resourceOwner string) ([]*es_models.Aggregate, error) {
|
|
|
|
userNameAggregate, err := reservedUniqueUserNameAggregate(ctx, aggCreator, resourceOwner, user.UserName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
emailAggregate, err := reservedUniqueEmailAggregate(ctx, aggCreator, resourceOwner, user.EmailAddress)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return []*es_models.Aggregate{
|
|
|
|
userNameAggregate,
|
|
|
|
emailAggregate,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
func reservedUniqueUserNameAggregate(ctx context.Context, aggCreator *es_models.AggregateCreator, resourceOwner, userName string) (*es_models.Aggregate, error) {
|
|
|
|
aggregate, err := aggCreator.NewAggregate(ctx, userName, model.UserUserNameAggregate, model.UserVersion, 0)
|
|
|
|
if resourceOwner != "" {
|
|
|
|
aggregate, err = aggCreator.NewAggregate(ctx, userName, model.UserUserNameAggregate, model.UserVersion, 0, es_models.OverwriteResourceOwner(resourceOwner))
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
aggregate, err = aggregate.AppendEvent(model.UserUserNameReserved, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return aggregate.SetPrecondition(UserUserNameUniqueQuery(userName), isEventValidation(aggregate, model.UserUserNameReserved)), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func reservedUniqueEmailAggregate(ctx context.Context, aggCreator *es_models.AggregateCreator, resourceOwner, email string) (aggregate *es_models.Aggregate, err error) {
|
|
|
|
aggregate, err = aggCreator.NewAggregate(ctx, email, model.UserEmailAggregate, model.UserVersion, 0)
|
|
|
|
if resourceOwner != "" {
|
|
|
|
aggregate, err = aggCreator.NewAggregate(ctx, email, model.UserEmailAggregate, model.UserVersion, 0, es_models.OverwriteResourceOwner(resourceOwner))
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
aggregate, err = aggregate.AppendEvent(model.UserEmailReserved, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return aggregate.SetPrecondition(UserEmailUniqueQuery(email), isEventValidation(aggregate, model.UserEmailReserved)), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func releasedUniqueEmailAggregate(ctx context.Context, aggCreator *es_models.AggregateCreator, resourceOwner, email string) (aggregate *es_models.Aggregate, err error) {
|
|
|
|
aggregate, err = aggCreator.NewAggregate(ctx, email, model.UserEmailAggregate, model.UserVersion, 0)
|
|
|
|
if resourceOwner != "" {
|
|
|
|
aggregate, err = aggCreator.NewAggregate(ctx, email, model.UserEmailAggregate, model.UserVersion, 0, es_models.OverwriteResourceOwner(resourceOwner))
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
aggregate, err = aggregate.AppendEvent(model.UserEmailReleased, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return aggregate.SetPrecondition(UserEmailUniqueQuery(email), isEventValidation(aggregate, model.UserEmailReleased)), nil
|
2020-05-11 10:16:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func UserDeactivateAggregate(aggCreator *es_models.AggregateCreator, user *model.User) func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
return userStateAggregate(aggCreator, user, model.UserDeactivated)
|
|
|
|
}
|
|
|
|
|
|
|
|
func UserReactivateAggregate(aggCreator *es_models.AggregateCreator, user *model.User) func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
return userStateAggregate(aggCreator, user, model.UserReactivated)
|
|
|
|
}
|
|
|
|
|
|
|
|
func UserLockAggregate(aggCreator *es_models.AggregateCreator, user *model.User) func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
return userStateAggregate(aggCreator, user, model.UserLocked)
|
|
|
|
}
|
|
|
|
|
|
|
|
func UserUnlockAggregate(aggCreator *es_models.AggregateCreator, user *model.User) func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
return userStateAggregate(aggCreator, user, model.UserUnlocked)
|
|
|
|
}
|
|
|
|
|
|
|
|
func userStateAggregate(aggCreator *es_models.AggregateCreator, user *model.User, state es_models.EventType) func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
agg, err := UserAggregate(ctx, aggCreator, user)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return agg.AppendEvent(state, nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func UserInitCodeAggregate(aggCreator *es_models.AggregateCreator, existing *model.User, code *model.InitUserCode) func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
if code == nil {
|
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-d8i23", "code should not be nil")
|
|
|
|
}
|
|
|
|
agg, err := UserAggregate(ctx, aggCreator, existing)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return agg.AppendEvent(model.InitializedUserCodeAdded, code)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-20 14:28:08 +02:00
|
|
|
func UserInitCodeSentAggregate(aggCreator *es_models.AggregateCreator, existing *model.User) func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
agg, err := UserAggregate(ctx, aggCreator, existing)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return agg.AppendEvent(model.InitializedUserCodeSent, nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-05 07:50:04 +02:00
|
|
|
func InitCodeVerifiedAggregate(aggCreator *es_models.AggregateCreator, existing *model.User, password *model.Password) func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
agg, err := UserAggregate(ctx, aggCreator, existing)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if existing.Email != nil && !existing.Email.IsEmailVerified {
|
|
|
|
agg, err = agg.AppendEvent(model.UserEmailVerified, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if password != nil {
|
|
|
|
agg, err = agg.AppendEvent(model.UserPasswordChanged, password)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return agg.AppendEvent(model.InitializedUserCheckSucceeded, nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func InitCodeCheckFailedAggregate(aggCreator *es_models.AggregateCreator, existing *model.User) func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
agg, err := UserAggregate(ctx, aggCreator, existing)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return agg.AppendEvent(model.InitializedUserCheckFailed, nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-11 10:16:27 +02:00
|
|
|
func SkipMfaAggregate(aggCreator *es_models.AggregateCreator, existing *model.User) func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
agg, err := UserAggregate(ctx, aggCreator, existing)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return agg.AppendEvent(model.MfaInitSkipped, nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func PasswordChangeAggregate(aggCreator *es_models.AggregateCreator, existing *model.User, password *model.Password) func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
if password == nil {
|
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-d9832", "password should not be nil")
|
|
|
|
}
|
|
|
|
agg, err := UserAggregate(ctx, aggCreator, existing)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return agg.AppendEvent(model.UserPasswordChanged, password)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-18 12:06:36 +02:00
|
|
|
func PasswordCheckSucceededAggregate(aggCreator *es_models.AggregateCreator, existing *model.User, check *model.AuthRequest) es_sdk.AggregateFunc {
|
|
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
agg, err := UserAggregate(ctx, aggCreator, existing)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return agg.AppendEvent(model.UserPasswordCheckSucceeded, check)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func PasswordCheckFailedAggregate(aggCreator *es_models.AggregateCreator, existing *model.User, check *model.AuthRequest) es_sdk.AggregateFunc {
|
|
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
agg, err := UserAggregate(ctx, aggCreator, existing)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return agg.AppendEvent(model.UserPasswordCheckFailed, check)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-11 10:16:27 +02:00
|
|
|
func RequestSetPassword(aggCreator *es_models.AggregateCreator, existing *model.User, request *model.PasswordCode) func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
if request == nil {
|
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-d8ei2", "password set request should not be nil")
|
|
|
|
}
|
|
|
|
agg, err := UserAggregate(ctx, aggCreator, existing)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return agg.AppendEvent(model.UserPasswordCodeAdded, request)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-20 14:28:08 +02:00
|
|
|
func PasswordCodeSentAggregate(aggCreator *es_models.AggregateCreator, existing *model.User) func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
agg, err := UserAggregate(ctx, aggCreator, existing)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return agg.AppendEvent(model.UserPasswordCodeSent, nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-11 10:16:27 +02:00
|
|
|
func ProfileChangeAggregate(aggCreator *es_models.AggregateCreator, existing *model.User, profile *model.Profile) func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
if profile == nil {
|
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-dhr74", "profile should not be nil")
|
|
|
|
}
|
|
|
|
agg, err := UserAggregate(ctx, aggCreator, existing)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
changes := existing.Profile.Changes(profile)
|
2020-05-20 14:28:08 +02:00
|
|
|
if len(changes) == 0 {
|
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-0spow", "no changes found")
|
|
|
|
}
|
2020-05-11 10:16:27 +02:00
|
|
|
return agg.AppendEvent(model.UserProfileChanged, changes)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-28 06:49:22 +02:00
|
|
|
func EmailChangeAggregate(ctx context.Context, aggCreator *es_models.AggregateCreator, existing *model.User, email *model.Email, code *model.EmailCode) ([]*es_models.Aggregate, error) {
|
|
|
|
if email == nil {
|
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-dki8s", "email should not be nil")
|
|
|
|
}
|
|
|
|
if (!email.IsEmailVerified && code == nil) || (email.IsEmailVerified && code != nil) {
|
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-id934", "email has to be verified or code must be sent")
|
|
|
|
}
|
|
|
|
changes := existing.Email.Changes(email)
|
|
|
|
if len(changes) == 0 {
|
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-s90pw", "no changes found")
|
|
|
|
}
|
|
|
|
aggregates := make([]*es_models.Aggregate, 0, 4)
|
|
|
|
reserveEmailAggregate, err := reservedUniqueEmailAggregate(ctx, aggCreator, "", email.EmailAddress)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
aggregates = append(aggregates, reserveEmailAggregate)
|
|
|
|
releaseEmailAggregate, err := releasedUniqueEmailAggregate(ctx, aggCreator, "", existing.EmailAddress)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
aggregates = append(aggregates, releaseEmailAggregate)
|
|
|
|
agg, err := UserAggregate(ctx, aggCreator, existing)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
agg, err = agg.AppendEvent(model.UserEmailChanged, changes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if existing.Email == nil {
|
|
|
|
existing.Email = new(model.Email)
|
|
|
|
}
|
|
|
|
if email.IsEmailVerified {
|
|
|
|
agg, err = agg.AppendEvent(model.UserEmailVerified, code)
|
2020-05-11 10:16:27 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-28 06:49:22 +02:00
|
|
|
}
|
|
|
|
if code != nil {
|
|
|
|
agg, err = agg.AppendEvent(model.UserEmailCodeAdded, code)
|
2020-05-11 10:16:27 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2020-05-28 06:49:22 +02:00
|
|
|
return append(aggregates, agg), nil
|
2020-05-11 10:16:27 +02:00
|
|
|
}
|
2020-05-28 06:49:22 +02:00
|
|
|
|
2020-06-05 07:50:04 +02:00
|
|
|
func EmailVerifiedAggregate(aggCreator *es_models.AggregateCreator, existing *model.User) es_sdk.AggregateFunc {
|
2020-05-11 10:16:27 +02:00
|
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
agg, err := UserAggregate(ctx, aggCreator, existing)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return agg.AppendEvent(model.UserEmailVerified, nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-05 07:50:04 +02:00
|
|
|
func EmailVerificationFailedAggregate(aggCreator *es_models.AggregateCreator, existing *model.User) es_sdk.AggregateFunc {
|
|
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
agg, err := UserAggregate(ctx, aggCreator, existing)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return agg.AppendEvent(model.UserEmailVerificationFailed, nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-11 10:16:27 +02:00
|
|
|
func EmailVerificationCodeAggregate(aggCreator *es_models.AggregateCreator, existing *model.User, code *model.EmailCode) func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
if code == nil {
|
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-dki8s", "code should not be nil")
|
|
|
|
}
|
|
|
|
agg, err := UserAggregate(ctx, aggCreator, existing)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return agg.AppendEvent(model.UserEmailCodeAdded, code)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-20 14:28:08 +02:00
|
|
|
func EmailCodeSentAggregate(aggCreator *es_models.AggregateCreator, existing *model.User) func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
agg, err := UserAggregate(ctx, aggCreator, existing)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return agg.AppendEvent(model.UserEmailCodeSent, nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-11 10:16:27 +02:00
|
|
|
func PhoneChangeAggregate(aggCreator *es_models.AggregateCreator, existing *model.User, phone *model.Phone, code *model.PhoneCode) func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
if phone == nil {
|
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-dkso3", "phone should not be nil")
|
|
|
|
}
|
|
|
|
if (!phone.IsPhoneVerified && code == nil) || (phone.IsPhoneVerified && code != nil) {
|
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-dksi8", "phone has to be verified or code must be sent")
|
|
|
|
}
|
|
|
|
agg, err := UserAggregate(ctx, aggCreator, existing)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if existing.Phone == nil {
|
|
|
|
existing.Phone = new(model.Phone)
|
|
|
|
}
|
|
|
|
changes := existing.Phone.Changes(phone)
|
2020-05-20 14:28:08 +02:00
|
|
|
if len(changes) == 0 {
|
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-sp0oc", "no changes found")
|
|
|
|
}
|
2020-05-11 10:16:27 +02:00
|
|
|
agg, err = agg.AppendEvent(model.UserPhoneChanged, changes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if phone.IsPhoneVerified {
|
|
|
|
return agg.AppendEvent(model.UserPhoneVerified, code)
|
|
|
|
}
|
|
|
|
if code != nil {
|
|
|
|
return agg.AppendEvent(model.UserPhoneCodeAdded, code)
|
|
|
|
}
|
|
|
|
return agg, nil
|
|
|
|
}
|
|
|
|
}
|
2020-06-05 07:50:04 +02:00
|
|
|
|
|
|
|
func PhoneVerifiedAggregate(aggCreator *es_models.AggregateCreator, existing *model.User) es_sdk.AggregateFunc {
|
2020-05-11 10:16:27 +02:00
|
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
agg, err := UserAggregate(ctx, aggCreator, existing)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return agg.AppendEvent(model.UserPhoneVerified, nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-05 07:50:04 +02:00
|
|
|
func PhoneVerificationFailedAggregate(aggCreator *es_models.AggregateCreator, existing *model.User) es_sdk.AggregateFunc {
|
|
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
agg, err := UserAggregate(ctx, aggCreator, existing)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return agg.AppendEvent(model.UserPhoneVerificationFailed, nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-11 10:16:27 +02:00
|
|
|
func PhoneVerificationCodeAggregate(aggCreator *es_models.AggregateCreator, existing *model.User, code *model.PhoneCode) func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
if code == nil {
|
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-dsue2", "code should not be nil")
|
|
|
|
}
|
|
|
|
agg, err := UserAggregate(ctx, aggCreator, existing)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return agg.AppendEvent(model.UserPhoneCodeAdded, code)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-20 14:28:08 +02:00
|
|
|
func PhoneCodeSentAggregate(aggCreator *es_models.AggregateCreator, existing *model.User) func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
agg, err := UserAggregate(ctx, aggCreator, existing)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return agg.AppendEvent(model.UserPhoneCodeSent, nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-11 10:16:27 +02:00
|
|
|
func AddressChangeAggregate(aggCreator *es_models.AggregateCreator, existing *model.User, address *model.Address) func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
if address == nil {
|
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-dkx9s", "address should not be nil")
|
|
|
|
}
|
|
|
|
agg, err := UserAggregate(ctx, aggCreator, existing)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if existing.Address == nil {
|
|
|
|
existing.Address = new(model.Address)
|
|
|
|
}
|
|
|
|
changes := existing.Address.Changes(address)
|
2020-05-20 14:28:08 +02:00
|
|
|
if len(changes) == 0 {
|
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-2tszw", "no changes found")
|
|
|
|
}
|
2020-05-11 10:16:27 +02:00
|
|
|
return agg.AppendEvent(model.UserAddressChanged, changes)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func MfaOTPAddAggregate(aggCreator *es_models.AggregateCreator, existing *model.User, otp *model.OTP) func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
if otp == nil {
|
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-dkx9s", "otp should not be nil")
|
|
|
|
}
|
|
|
|
agg, err := UserAggregate(ctx, aggCreator, existing)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return agg.AppendEvent(model.MfaOtpAdded, otp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func MfaOTPVerifyAggregate(aggCreator *es_models.AggregateCreator, existing *model.User) func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
agg, err := UserAggregate(ctx, aggCreator, existing)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return agg.AppendEvent(model.MfaOtpVerified, nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-18 12:06:36 +02:00
|
|
|
func MfaOTPCheckSucceededAggregate(aggCreator *es_models.AggregateCreator, existing *model.User, authReq *model.AuthRequest) es_sdk.AggregateFunc {
|
|
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
if authReq == nil {
|
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-sd5DA", "authReq must not be nil")
|
|
|
|
}
|
|
|
|
agg, err := UserAggregate(ctx, aggCreator, existing)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return agg.AppendEvent(model.MfaOtpCheckSucceeded, authReq)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func MfaOTPCheckFailedAggregate(aggCreator *es_models.AggregateCreator, existing *model.User, authReq *model.AuthRequest) es_sdk.AggregateFunc {
|
|
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
if authReq == nil {
|
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-64sd6", "authReq must not be nil")
|
|
|
|
}
|
|
|
|
agg, err := UserAggregate(ctx, aggCreator, existing)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return agg.AppendEvent(model.MfaOtpCheckFailed, authReq)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-11 10:16:27 +02:00
|
|
|
func MfaOTPRemoveAggregate(aggCreator *es_models.AggregateCreator, existing *model.User) func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
agg, err := UserAggregate(ctx, aggCreator, existing)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return agg.AppendEvent(model.MfaOtpRemoved, nil)
|
|
|
|
}
|
|
|
|
}
|
2020-05-18 12:06:36 +02:00
|
|
|
|
|
|
|
func SignOutAggregate(aggCreator *es_models.AggregateCreator, existing *model.User, agentID string) func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
|
|
agg, err := UserAggregate(ctx, aggCreator, existing)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return agg.AppendEvent(model.SignedOut, map[string]interface{}{"agentID": agentID})
|
|
|
|
}
|
|
|
|
}
|
2020-05-28 06:49:22 +02:00
|
|
|
|
|
|
|
func isEventValidation(aggregate *es_models.Aggregate, eventType es_models.EventType) func(...*es_models.Event) error {
|
|
|
|
return func(events ...*es_models.Event) error {
|
|
|
|
if len(events) == 0 {
|
|
|
|
aggregate.PreviousSequence = 0
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if events[0].Type == eventType {
|
|
|
|
return errors.ThrowPreconditionFailedf(nil, "EVENT-eJQqe", "user is already %v", eventType)
|
|
|
|
}
|
|
|
|
aggregate.PreviousSequence = events[0].Sequence
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|