2021-01-18 11:24:15 +01:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-07-06 13:55:57 +02:00
|
|
|
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/org"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/policy"
|
2021-01-18 11:24:15 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type OrgPasswordAgePolicyWriteModel struct {
|
|
|
|
PasswordAgePolicyWriteModel
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewOrgPasswordAgePolicyWriteModel(orgID string) *OrgPasswordAgePolicyWriteModel {
|
|
|
|
return &OrgPasswordAgePolicyWriteModel{
|
|
|
|
PasswordAgePolicyWriteModel{
|
|
|
|
WriteModel: eventstore.WriteModel{
|
|
|
|
AggregateID: orgID,
|
|
|
|
ResourceOwner: orgID,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-03 09:19:07 +01:00
|
|
|
func (wm *OrgPasswordAgePolicyWriteModel) AppendEvents(events ...eventstore.Event) {
|
2021-01-18 11:24:15 +01:00
|
|
|
for _, event := range events {
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *org.PasswordAgePolicyAddedEvent:
|
|
|
|
wm.PasswordAgePolicyWriteModel.AppendEvents(&e.PasswordAgePolicyAddedEvent)
|
|
|
|
case *org.PasswordAgePolicyChangedEvent:
|
|
|
|
wm.PasswordAgePolicyWriteModel.AppendEvents(&e.PasswordAgePolicyChangedEvent)
|
|
|
|
case *org.PasswordAgePolicyRemovedEvent:
|
|
|
|
wm.PasswordAgePolicyWriteModel.AppendEvents(&e.PasswordAgePolicyRemovedEvent)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *OrgPasswordAgePolicyWriteModel) Reduce() error {
|
|
|
|
return wm.PasswordAgePolicyWriteModel.Reduce()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *OrgPasswordAgePolicyWriteModel) Query() *eventstore.SearchQueryBuilder {
|
2021-07-06 13:55:57 +02:00
|
|
|
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent).
|
2021-02-18 14:48:27 +01:00
|
|
|
ResourceOwner(wm.ResourceOwner).
|
2021-07-06 13:55:57 +02:00
|
|
|
AddQuery().
|
|
|
|
AggregateTypes(org.AggregateType).
|
|
|
|
AggregateIDs(wm.PasswordAgePolicyWriteModel.AggregateID).
|
2021-02-18 14:48:27 +01:00
|
|
|
EventTypes(
|
|
|
|
org.PasswordAgePolicyAddedEventType,
|
|
|
|
org.PasswordAgePolicyChangedEventType,
|
2021-07-06 13:55:57 +02:00
|
|
|
org.PasswordAgePolicyRemovedEventType).
|
|
|
|
Builder()
|
2021-01-18 11:24:15 +01:00
|
|
|
}
|
|
|
|
|
2021-02-18 14:48:27 +01:00
|
|
|
func (wm *OrgPasswordAgePolicyWriteModel) NewChangedEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
|
|
|
expireWarnDays,
|
|
|
|
maxAgeDays uint64) (*org.PasswordAgePolicyChangedEvent, bool) {
|
2021-01-18 11:24:15 +01:00
|
|
|
changes := make([]policy.PasswordAgePolicyChanges, 0)
|
|
|
|
if wm.ExpireWarnDays != expireWarnDays {
|
|
|
|
changes = append(changes, policy.ChangeExpireWarnDays(expireWarnDays))
|
|
|
|
}
|
|
|
|
if wm.MaxAgeDays != maxAgeDays {
|
|
|
|
changes = append(changes, policy.ChangeMaxAgeDays(maxAgeDays))
|
|
|
|
}
|
|
|
|
if len(changes) == 0 {
|
|
|
|
return nil, false
|
|
|
|
}
|
2021-02-18 14:48:27 +01:00
|
|
|
changedEvent, err := org.NewPasswordAgePolicyChangedEvent(ctx, aggregate, changes)
|
2021-01-18 11:24:15 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
return changedEvent, true
|
|
|
|
}
|