2021-01-18 10:24:15 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-07-06 11:55:57 +00:00
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
2021-01-18 10:24:15 +00:00
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/org"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/policy"
|
2021-01-18 10:24:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type OrgPasswordAgePolicyWriteModel struct {
|
|
|
|
PasswordAgePolicyWriteModel
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewOrgPasswordAgePolicyWriteModel(orgID string) *OrgPasswordAgePolicyWriteModel {
|
|
|
|
return &OrgPasswordAgePolicyWriteModel{
|
|
|
|
PasswordAgePolicyWriteModel{
|
|
|
|
WriteModel: eventstore.WriteModel{
|
|
|
|
AggregateID: orgID,
|
|
|
|
ResourceOwner: orgID,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-03 08:19:07 +00:00
|
|
|
func (wm *OrgPasswordAgePolicyWriteModel) AppendEvents(events ...eventstore.Event) {
|
2021-01-18 10:24:15 +00: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 11:55:57 +00:00
|
|
|
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent).
|
2021-02-18 13:48:27 +00:00
|
|
|
ResourceOwner(wm.ResourceOwner).
|
2021-07-06 11:55:57 +00:00
|
|
|
AddQuery().
|
|
|
|
AggregateTypes(org.AggregateType).
|
|
|
|
AggregateIDs(wm.PasswordAgePolicyWriteModel.AggregateID).
|
2021-02-18 13:48:27 +00:00
|
|
|
EventTypes(
|
|
|
|
org.PasswordAgePolicyAddedEventType,
|
|
|
|
org.PasswordAgePolicyChangedEventType,
|
2021-07-06 11:55:57 +00:00
|
|
|
org.PasswordAgePolicyRemovedEventType).
|
|
|
|
Builder()
|
2021-01-18 10:24:15 +00:00
|
|
|
}
|
|
|
|
|
2021-02-18 13:48:27 +00:00
|
|
|
func (wm *OrgPasswordAgePolicyWriteModel) NewChangedEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
|
|
|
expireWarnDays,
|
|
|
|
maxAgeDays uint64) (*org.PasswordAgePolicyChangedEvent, bool) {
|
2021-01-18 10:24:15 +00: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 13:48:27 +00:00
|
|
|
changedEvent, err := org.NewPasswordAgePolicyChangedEvent(ctx, aggregate, changes)
|
2021-01-18 10:24:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
return changedEvent, true
|
|
|
|
}
|