2021-01-07 15:06:45 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/caos/zitadel/internal/crypto"
|
|
|
|
"github.com/caos/zitadel/internal/eventstore/v2"
|
|
|
|
"github.com/caos/zitadel/internal/v2/domain"
|
|
|
|
"github.com/caos/zitadel/internal/v2/repository/user"
|
|
|
|
)
|
|
|
|
|
|
|
|
type HumanPasswordWriteModel struct {
|
|
|
|
eventstore.WriteModel
|
|
|
|
|
|
|
|
Secret *crypto.CryptoValue
|
|
|
|
SecretChangeRequired bool
|
|
|
|
|
|
|
|
UserState domain.UserState
|
|
|
|
}
|
|
|
|
|
2021-01-12 11:59:51 +00:00
|
|
|
func NewHumanPasswordWriteModel(userID, resourceOwner string) *HumanPasswordWriteModel {
|
2021-01-07 15:06:45 +00:00
|
|
|
return &HumanPasswordWriteModel{
|
|
|
|
WriteModel: eventstore.WriteModel{
|
2021-01-12 11:59:51 +00:00
|
|
|
AggregateID: userID,
|
|
|
|
ResourceOwner: resourceOwner,
|
2021-01-07 15:06:45 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *HumanPasswordWriteModel) AppendEvents(events ...eventstore.EventReader) {
|
2021-01-15 08:32:59 +00:00
|
|
|
wm.WriteModel.AppendEvents(events...)
|
2021-01-07 15:06:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *HumanPasswordWriteModel) Reduce() error {
|
|
|
|
for _, event := range wm.Events {
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *user.HumanAddedEvent:
|
|
|
|
wm.Secret = e.Secret
|
|
|
|
wm.SecretChangeRequired = e.ChangeRequired
|
|
|
|
wm.UserState = domain.UserStateInitial
|
|
|
|
case *user.HumanRegisteredEvent:
|
|
|
|
wm.Secret = e.Secret
|
|
|
|
wm.SecretChangeRequired = e.ChangeRequired
|
|
|
|
wm.UserState = domain.UserStateActive
|
|
|
|
case *user.HumanPasswordChangedEvent:
|
|
|
|
wm.Secret = e.Secret
|
|
|
|
wm.SecretChangeRequired = e.ChangeRequired
|
|
|
|
case *user.HumanEmailVerifiedEvent:
|
|
|
|
if wm.UserState == domain.UserStateInitial {
|
|
|
|
wm.UserState = domain.UserStateActive
|
|
|
|
}
|
|
|
|
case *user.UserRemovedEvent:
|
|
|
|
wm.UserState = domain.UserStateDeleted
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return wm.WriteModel.Reduce()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *HumanPasswordWriteModel) Query() *eventstore.SearchQueryBuilder {
|
|
|
|
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent, user.AggregateType).
|
2021-01-12 11:59:51 +00:00
|
|
|
AggregateIDs(wm.AggregateID).
|
|
|
|
ResourceOwner(wm.ResourceOwner)
|
2021-01-07 15:06:45 +00:00
|
|
|
}
|