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"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/org"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/policy"
|
2021-01-18 10:24:15 +00:00
|
|
|
)
|
|
|
|
|
2021-08-11 06:36:32 +00:00
|
|
|
type OrgLockoutPolicyWriteModel struct {
|
|
|
|
LockoutPolicyWriteModel
|
2021-01-18 10:24:15 +00:00
|
|
|
}
|
|
|
|
|
2021-08-11 06:36:32 +00:00
|
|
|
func NewOrgLockoutPolicyWriteModel(orgID string) *OrgLockoutPolicyWriteModel {
|
|
|
|
return &OrgLockoutPolicyWriteModel{
|
|
|
|
LockoutPolicyWriteModel{
|
2021-01-18 10:24:15 +00:00
|
|
|
WriteModel: eventstore.WriteModel{
|
|
|
|
AggregateID: orgID,
|
|
|
|
ResourceOwner: orgID,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-03 08:19:07 +00:00
|
|
|
func (wm *OrgLockoutPolicyWriteModel) AppendEvents(events ...eventstore.Event) {
|
2021-01-18 10:24:15 +00:00
|
|
|
for _, event := range events {
|
|
|
|
switch e := event.(type) {
|
2021-08-11 06:36:32 +00:00
|
|
|
case *org.LockoutPolicyAddedEvent:
|
|
|
|
wm.LockoutPolicyWriteModel.AppendEvents(&e.LockoutPolicyAddedEvent)
|
|
|
|
case *org.LockoutPolicyChangedEvent:
|
|
|
|
wm.LockoutPolicyWriteModel.AppendEvents(&e.LockoutPolicyChangedEvent)
|
|
|
|
case *org.LockoutPolicyRemovedEvent:
|
|
|
|
wm.LockoutPolicyWriteModel.AppendEvents(&e.LockoutPolicyRemovedEvent)
|
2021-01-18 10:24:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-11 06:36:32 +00:00
|
|
|
func (wm *OrgLockoutPolicyWriteModel) Reduce() error {
|
|
|
|
return wm.LockoutPolicyWriteModel.Reduce()
|
2021-01-18 10:24:15 +00:00
|
|
|
}
|
|
|
|
|
2021-08-11 06:36:32 +00:00
|
|
|
func (wm *OrgLockoutPolicyWriteModel) 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).
|
2021-08-11 06:36:32 +00:00
|
|
|
AggregateIDs(wm.LockoutPolicyWriteModel.AggregateID).
|
|
|
|
EventTypes(org.LockoutPolicyAddedEventType,
|
|
|
|
org.LockoutPolicyChangedEventType,
|
|
|
|
org.LockoutPolicyRemovedEventType).
|
2021-07-06 11:55:57 +00:00
|
|
|
Builder()
|
2021-01-18 10:24:15 +00:00
|
|
|
}
|
|
|
|
|
2021-08-11 06:36:32 +00:00
|
|
|
func (wm *OrgLockoutPolicyWriteModel) NewChangedEvent(
|
2021-02-18 13:48:27 +00:00
|
|
|
ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
|
|
|
maxAttempts uint64,
|
2021-08-11 06:36:32 +00:00
|
|
|
showLockoutFailure bool) (*org.LockoutPolicyChangedEvent, bool) {
|
|
|
|
changes := make([]policy.LockoutPolicyChanges, 0)
|
|
|
|
if wm.MaxPasswordAttempts != maxAttempts {
|
2021-01-18 10:24:15 +00:00
|
|
|
changes = append(changes, policy.ChangeMaxAttempts(maxAttempts))
|
|
|
|
}
|
|
|
|
if wm.ShowLockOutFailures != showLockoutFailure {
|
|
|
|
changes = append(changes, policy.ChangeShowLockOutFailures(showLockoutFailure))
|
|
|
|
}
|
|
|
|
if len(changes) == 0 {
|
|
|
|
return nil, false
|
|
|
|
}
|
2021-08-11 06:36:32 +00:00
|
|
|
changedEvent, err := org.NewLockoutPolicyChangedEvent(ctx, aggregate, changes)
|
2021-01-18 10:24:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
return changedEvent, true
|
|
|
|
}
|