2021-01-04 13:52:13 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-01-12 11:59:51 +00:00
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore/v2"
|
2021-01-12 11:59:51 +00:00
|
|
|
"github.com/caos/zitadel/internal/v2/domain"
|
2021-01-04 13:52:13 +00:00
|
|
|
"github.com/caos/zitadel/internal/v2/repository/iam"
|
2021-01-18 10:24:15 +00:00
|
|
|
"github.com/caos/zitadel/internal/v2/repository/policy"
|
2021-01-04 13:52:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type IAMPasswordLockoutPolicyWriteModel struct {
|
|
|
|
PasswordLockoutPolicyWriteModel
|
|
|
|
}
|
|
|
|
|
2021-01-12 11:59:51 +00:00
|
|
|
func NewIAMPasswordLockoutPolicyWriteModel() *IAMPasswordLockoutPolicyWriteModel {
|
2021-01-04 13:52:13 +00:00
|
|
|
return &IAMPasswordLockoutPolicyWriteModel{
|
|
|
|
PasswordLockoutPolicyWriteModel{
|
|
|
|
WriteModel: eventstore.WriteModel{
|
2021-01-12 11:59:51 +00:00
|
|
|
AggregateID: domain.IAMID,
|
|
|
|
ResourceOwner: domain.IAMID,
|
2021-01-04 13:52:13 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *IAMPasswordLockoutPolicyWriteModel) AppendEvents(events ...eventstore.EventReader) {
|
|
|
|
for _, event := range events {
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *iam.PasswordLockoutPolicyAddedEvent:
|
|
|
|
wm.PasswordLockoutPolicyWriteModel.AppendEvents(&e.PasswordLockoutPolicyAddedEvent)
|
|
|
|
case *iam.PasswordLockoutPolicyChangedEvent:
|
|
|
|
wm.PasswordLockoutPolicyWriteModel.AppendEvents(&e.PasswordLockoutPolicyChangedEvent)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *IAMPasswordLockoutPolicyWriteModel) Reduce() error {
|
|
|
|
return wm.PasswordLockoutPolicyWriteModel.Reduce()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *IAMPasswordLockoutPolicyWriteModel) Query() *eventstore.SearchQueryBuilder {
|
|
|
|
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent, iam.AggregateType).
|
2021-01-12 11:59:51 +00:00
|
|
|
AggregateIDs(wm.PasswordLockoutPolicyWriteModel.AggregateID).
|
|
|
|
ResourceOwner(wm.ResourceOwner)
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *IAMPasswordLockoutPolicyWriteModel) NewChangedEvent(ctx context.Context, maxAttempts uint64, showLockoutFailure bool) (*iam.PasswordLockoutPolicyChangedEvent, bool) {
|
2021-01-18 10:24:15 +00:00
|
|
|
changes := make([]policy.PasswordLockoutPolicyChanges, 0)
|
2021-01-04 13:52:13 +00:00
|
|
|
if wm.MaxAttempts != maxAttempts {
|
2021-01-18 10:24:15 +00:00
|
|
|
changes = append(changes, policy.ChangeMaxAttempts(maxAttempts))
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
|
|
|
if wm.ShowLockOutFailures != showLockoutFailure {
|
2021-01-18 10:24:15 +00:00
|
|
|
changes = append(changes, policy.ChangeShowLockOutFailures(showLockoutFailure))
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
2021-01-18 10:24:15 +00:00
|
|
|
if len(changes) == 0 {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
changedEvent, err := iam.NewPasswordLockoutPolicyChangedEvent(ctx, changes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
return changedEvent, true
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|