2021-01-04 13:52:13 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-02-18 13:48:27 +00:00
|
|
|
"time"
|
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/crypto"
|
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
2023-04-25 07:02:29 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/user"
|
2021-01-04 13:52:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type HumanEmailWriteModel struct {
|
|
|
|
eventstore.WriteModel
|
|
|
|
|
2023-03-14 19:20:38 +00:00
|
|
|
Email domain.EmailAddress
|
2021-01-04 13:52:13 +00:00
|
|
|
IsEmailVerified bool
|
|
|
|
|
2021-01-15 08:32:59 +00:00
|
|
|
Code *crypto.CryptoValue
|
|
|
|
CodeCreationDate time.Time
|
|
|
|
CodeExpiry time.Duration
|
2024-04-24 15:50:58 +00:00
|
|
|
AuthRequestID string
|
2021-01-15 08:32:59 +00:00
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
UserState domain.UserState
|
|
|
|
}
|
|
|
|
|
2021-01-12 11:59:51 +00:00
|
|
|
func NewHumanEmailWriteModel(userID, resourceOwner string) *HumanEmailWriteModel {
|
2021-01-04 13:52:13 +00:00
|
|
|
return &HumanEmailWriteModel{
|
|
|
|
WriteModel: eventstore.WriteModel{
|
2021-01-12 11:59:51 +00:00
|
|
|
AggregateID: userID,
|
|
|
|
ResourceOwner: resourceOwner,
|
2021-01-04 13:52:13 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *HumanEmailWriteModel) 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-01-04 13:52:13 +00:00
|
|
|
case *user.HumanRegisteredEvent:
|
|
|
|
wm.Email = e.EmailAddress
|
2021-03-22 13:40:25 +00:00
|
|
|
wm.UserState = domain.UserStateActive
|
|
|
|
case *user.HumanInitialCodeAddedEvent:
|
2021-01-07 15:06:45 +00:00
|
|
|
wm.UserState = domain.UserStateInitial
|
2021-03-22 13:40:25 +00:00
|
|
|
case *user.HumanInitializedCheckSucceededEvent:
|
|
|
|
wm.UserState = domain.UserStateActive
|
2021-01-04 13:52:13 +00:00
|
|
|
case *user.HumanEmailChangedEvent:
|
|
|
|
wm.Email = e.EmailAddress
|
|
|
|
wm.IsEmailVerified = false
|
2021-01-15 08:32:59 +00:00
|
|
|
wm.Code = nil
|
|
|
|
case *user.HumanEmailCodeAddedEvent:
|
|
|
|
wm.Code = e.Code
|
|
|
|
wm.CodeCreationDate = e.CreationDate()
|
|
|
|
wm.CodeExpiry = e.Expiry
|
2024-04-24 15:50:58 +00:00
|
|
|
wm.AuthRequestID = e.AuthRequestID
|
2021-01-04 13:52:13 +00:00
|
|
|
case *user.HumanEmailVerifiedEvent:
|
|
|
|
wm.IsEmailVerified = true
|
2021-01-15 08:32:59 +00:00
|
|
|
wm.Code = nil
|
2021-01-04 13:52:13 +00:00
|
|
|
case *user.UserRemovedEvent:
|
|
|
|
wm.UserState = domain.UserStateDeleted
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return wm.WriteModel.Reduce()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *HumanEmailWriteModel) Query() *eventstore.SearchQueryBuilder {
|
2021-07-06 11:55:57 +00:00
|
|
|
query := eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent).
|
|
|
|
AddQuery().
|
|
|
|
AggregateTypes(user.AggregateType).
|
2021-02-18 13:48:27 +00:00
|
|
|
AggregateIDs(wm.AggregateID).
|
2021-04-06 07:57:58 +00:00
|
|
|
EventTypes(user.UserV1AddedType,
|
|
|
|
user.HumanAddedType,
|
|
|
|
user.UserV1RegisteredType,
|
2021-02-18 13:48:27 +00:00
|
|
|
user.HumanRegisteredType,
|
2021-04-06 07:57:58 +00:00
|
|
|
user.UserV1InitialCodeAddedType,
|
2021-03-22 16:15:24 +00:00
|
|
|
user.HumanInitialCodeAddedType,
|
2021-04-06 07:57:58 +00:00
|
|
|
user.UserV1InitializedCheckSucceededType,
|
2021-03-22 16:15:24 +00:00
|
|
|
user.HumanInitializedCheckSucceededType,
|
2021-04-06 07:57:58 +00:00
|
|
|
user.UserV1EmailChangedType,
|
2021-02-18 13:48:27 +00:00
|
|
|
user.HumanEmailChangedType,
|
2021-04-06 07:57:58 +00:00
|
|
|
user.UserV1EmailCodeAddedType,
|
2021-02-18 13:48:27 +00:00
|
|
|
user.HumanEmailCodeAddedType,
|
2021-04-06 07:57:58 +00:00
|
|
|
user.UserV1EmailVerifiedType,
|
2021-02-18 13:48:27 +00:00
|
|
|
user.HumanEmailVerifiedType,
|
2021-07-06 11:55:57 +00:00
|
|
|
user.UserRemovedType).
|
|
|
|
Builder()
|
|
|
|
|
2021-02-08 10:30:30 +00:00
|
|
|
if wm.ResourceOwner != "" {
|
|
|
|
query.ResourceOwner(wm.ResourceOwner)
|
|
|
|
}
|
|
|
|
return query
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *HumanEmailWriteModel) NewChangedEvent(
|
|
|
|
ctx context.Context,
|
2021-02-18 13:48:27 +00:00
|
|
|
aggregate *eventstore.Aggregate,
|
2023-03-14 19:20:38 +00:00
|
|
|
email domain.EmailAddress,
|
2021-01-04 13:52:13 +00:00
|
|
|
) (*user.HumanEmailChangedEvent, bool) {
|
2021-03-19 10:12:56 +00:00
|
|
|
if wm.Email == email {
|
|
|
|
return nil, false
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
2021-03-19 10:12:56 +00:00
|
|
|
return user.NewHumanEmailChangedEvent(ctx, aggregate, email), true
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|