2021-01-04 13:52:13 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-02-23 14:13:04 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
2021-01-12 11:59:51 +00:00
|
|
|
|
2021-02-23 14:13:04 +00:00
|
|
|
"github.com/caos/zitadel/internal/domain"
|
|
|
|
"github.com/caos/zitadel/internal/repository/iam"
|
|
|
|
"github.com/caos/zitadel/internal/repository/policy"
|
2021-01-04 13:52:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type IAMPasswordComplexityPolicyWriteModel struct {
|
|
|
|
PasswordComplexityPolicyWriteModel
|
|
|
|
}
|
|
|
|
|
2021-01-12 11:59:51 +00:00
|
|
|
func NewIAMPasswordComplexityPolicyWriteModel() *IAMPasswordComplexityPolicyWriteModel {
|
2021-01-04 13:52:13 +00:00
|
|
|
return &IAMPasswordComplexityPolicyWriteModel{
|
|
|
|
PasswordComplexityPolicyWriteModel{
|
|
|
|
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 *IAMPasswordComplexityPolicyWriteModel) AppendEvents(events ...eventstore.EventReader) {
|
|
|
|
for _, event := range events {
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *iam.PasswordComplexityPolicyAddedEvent:
|
|
|
|
wm.PasswordComplexityPolicyWriteModel.AppendEvents(&e.PasswordComplexityPolicyAddedEvent)
|
|
|
|
case *iam.PasswordComplexityPolicyChangedEvent:
|
|
|
|
wm.PasswordComplexityPolicyWriteModel.AppendEvents(&e.PasswordComplexityPolicyChangedEvent)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *IAMPasswordComplexityPolicyWriteModel) Reduce() error {
|
|
|
|
return wm.PasswordComplexityPolicyWriteModel.Reduce()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *IAMPasswordComplexityPolicyWriteModel) Query() *eventstore.SearchQueryBuilder {
|
|
|
|
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent, iam.AggregateType).
|
2021-01-12 11:59:51 +00:00
|
|
|
AggregateIDs(wm.PasswordComplexityPolicyWriteModel.AggregateID).
|
2021-02-18 13:48:27 +00:00
|
|
|
ResourceOwner(wm.ResourceOwner).
|
|
|
|
EventTypes(
|
|
|
|
iam.PasswordComplexityPolicyAddedEventType,
|
|
|
|
iam.PasswordComplexityPolicyChangedEventType)
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *IAMPasswordComplexityPolicyWriteModel) NewChangedEvent(
|
|
|
|
ctx context.Context,
|
2021-02-18 13:48:27 +00:00
|
|
|
aggregate *eventstore.Aggregate,
|
2021-01-04 13:52:13 +00:00
|
|
|
minLength uint64,
|
|
|
|
hasLowercase,
|
|
|
|
hasUppercase,
|
|
|
|
hasNumber,
|
|
|
|
hasSymbol bool,
|
|
|
|
) (*iam.PasswordComplexityPolicyChangedEvent, bool) {
|
|
|
|
|
2021-01-18 10:24:15 +00:00
|
|
|
changes := make([]policy.PasswordComplexityPolicyChanges, 0)
|
2021-01-04 13:52:13 +00:00
|
|
|
if wm.MinLength != minLength {
|
2021-01-18 10:24:15 +00:00
|
|
|
changes = append(changes, policy.ChangeMinLength(minLength))
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
|
|
|
if wm.HasLowercase != hasLowercase {
|
2021-01-18 10:24:15 +00:00
|
|
|
changes = append(changes, policy.ChangeHasLowercase(hasLowercase))
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
2021-01-18 10:24:15 +00:00
|
|
|
if wm.HasUppercase != hasUppercase {
|
|
|
|
changes = append(changes, policy.ChangeHasUppercase(hasUppercase))
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
|
|
|
if wm.HasNumber != hasNumber {
|
2021-01-18 10:24:15 +00:00
|
|
|
changes = append(changes, policy.ChangeHasNumber(hasNumber))
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
|
|
|
if wm.HasSymbol != hasSymbol {
|
2021-01-18 10:24:15 +00:00
|
|
|
changes = append(changes, policy.ChangeHasSymbol(hasSymbol))
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
2021-01-18 10:24:15 +00:00
|
|
|
if len(changes) == 0 {
|
|
|
|
return nil, false
|
|
|
|
}
|
2021-02-18 13:48:27 +00:00
|
|
|
changedEvent, err := iam.NewPasswordComplexityPolicyChangedEvent(ctx, aggregate, changes)
|
2021-01-18 10:24:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
return changedEvent, true
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|