2021-02-08 10:30:30 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-02-23 14:13:04 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
2021-02-18 13:48:27 +00:00
|
|
|
"time"
|
|
|
|
|
2021-02-08 10:30:30 +00:00
|
|
|
"github.com/caos/zitadel/internal/crypto"
|
2021-02-23 14:13:04 +00:00
|
|
|
"github.com/caos/zitadel/internal/domain"
|
|
|
|
"github.com/caos/zitadel/internal/repository/user"
|
2021-02-08 10:30:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type HumanInitCodeWriteModel struct {
|
|
|
|
eventstore.WriteModel
|
|
|
|
|
|
|
|
Email string
|
|
|
|
IsEmailVerified bool
|
|
|
|
|
|
|
|
Code *crypto.CryptoValue
|
|
|
|
CodeCreationDate time.Time
|
|
|
|
CodeExpiry time.Duration
|
|
|
|
|
|
|
|
UserState domain.UserState
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewHumanInitCodeWriteModel(userID, resourceOwner string) *HumanInitCodeWriteModel {
|
|
|
|
return &HumanInitCodeWriteModel{
|
|
|
|
WriteModel: eventstore.WriteModel{
|
|
|
|
AggregateID: userID,
|
|
|
|
ResourceOwner: resourceOwner,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *HumanInitCodeWriteModel) Reduce() error {
|
|
|
|
for _, event := range wm.Events {
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *user.HumanAddedEvent:
|
|
|
|
wm.Email = e.EmailAddress
|
2021-03-22 13:40:25 +00:00
|
|
|
wm.UserState = domain.UserStateActive
|
2021-02-08 10:30:30 +00:00
|
|
|
case *user.HumanRegisteredEvent:
|
|
|
|
wm.Email = e.EmailAddress
|
2021-03-22 13:40:25 +00:00
|
|
|
wm.UserState = domain.UserStateActive
|
2021-02-08 10:30:30 +00:00
|
|
|
case *user.HumanEmailChangedEvent:
|
|
|
|
wm.Email = e.EmailAddress
|
|
|
|
wm.IsEmailVerified = false
|
|
|
|
case *user.HumanEmailVerifiedEvent:
|
|
|
|
wm.IsEmailVerified = true
|
|
|
|
wm.Code = nil
|
|
|
|
if wm.UserState == domain.UserStateInitial {
|
|
|
|
wm.UserState = domain.UserStateActive
|
|
|
|
}
|
|
|
|
case *user.HumanInitialCodeAddedEvent:
|
|
|
|
wm.Code = e.Code
|
|
|
|
wm.CodeCreationDate = e.CreationDate()
|
|
|
|
wm.CodeExpiry = e.Expiry
|
2021-03-22 13:40:25 +00:00
|
|
|
wm.UserState = domain.UserStateInitial
|
2021-02-08 10:30:30 +00:00
|
|
|
case *user.HumanInitializedCheckSucceededEvent:
|
|
|
|
wm.Code = nil
|
2021-03-22 13:40:25 +00:00
|
|
|
wm.UserState = domain.UserStateActive
|
2021-02-08 10:30:30 +00:00
|
|
|
case *user.UserRemovedEvent:
|
|
|
|
wm.UserState = domain.UserStateDeleted
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return wm.WriteModel.Reduce()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *HumanInitCodeWriteModel) Query() *eventstore.SearchQueryBuilder {
|
|
|
|
query := eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent, user.AggregateType).
|
2021-02-18 13:48:27 +00:00
|
|
|
AggregateIDs(wm.AggregateID).
|
|
|
|
EventTypes(user.HumanAddedType,
|
|
|
|
user.HumanRegisteredType,
|
|
|
|
user.HumanEmailChangedType,
|
|
|
|
user.HumanEmailVerifiedType,
|
|
|
|
user.HumanInitialCodeAddedType,
|
|
|
|
user.HumanInitializedCheckSucceededType,
|
|
|
|
user.UserRemovedType)
|
2021-02-08 10:30:30 +00:00
|
|
|
if wm.ResourceOwner != "" {
|
|
|
|
query.ResourceOwner(wm.ResourceOwner)
|
|
|
|
}
|
|
|
|
return query
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *HumanInitCodeWriteModel) NewChangedEvent(
|
|
|
|
ctx context.Context,
|
2021-02-18 13:48:27 +00:00
|
|
|
aggregate *eventstore.Aggregate,
|
2021-02-08 10:30:30 +00:00
|
|
|
email string,
|
|
|
|
) (*user.HumanEmailChangedEvent, bool) {
|
2021-03-19 10:12:56 +00:00
|
|
|
changedEvent := user.NewHumanEmailChangedEvent(ctx, aggregate, email)
|
|
|
|
return changedEvent, wm.Email != email
|
2021-02-08 10:30:30 +00:00
|
|
|
}
|