2021-01-04 14:52:13 +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/api/authz"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/instance"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/policy"
|
2021-01-04 14:52:13 +01:00
|
|
|
)
|
|
|
|
|
2022-03-24 17:21:34 +01:00
|
|
|
type InstanceLockoutPolicyWriteModel struct {
|
2021-08-11 08:36:32 +02:00
|
|
|
LockoutPolicyWriteModel
|
2021-01-04 14:52:13 +01:00
|
|
|
}
|
|
|
|
|
2022-04-05 07:58:09 +02:00
|
|
|
func NewInstanceLockoutPolicyWriteModel(ctx context.Context) *InstanceLockoutPolicyWriteModel {
|
2022-03-24 17:21:34 +01:00
|
|
|
return &InstanceLockoutPolicyWriteModel{
|
2021-08-11 08:36:32 +02:00
|
|
|
LockoutPolicyWriteModel{
|
2021-01-04 14:52:13 +01:00
|
|
|
WriteModel: eventstore.WriteModel{
|
2022-04-05 07:58:09 +02:00
|
|
|
AggregateID: authz.GetInstance(ctx).InstanceID(),
|
|
|
|
ResourceOwner: authz.GetInstance(ctx).InstanceID(),
|
2021-01-04 14:52:13 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-24 17:21:34 +01:00
|
|
|
func (wm *InstanceLockoutPolicyWriteModel) AppendEvents(events ...eventstore.Event) {
|
2021-01-04 14:52:13 +01:00
|
|
|
for _, event := range events {
|
|
|
|
switch e := event.(type) {
|
2022-03-24 17:21:34 +01:00
|
|
|
case *instance.LockoutPolicyAddedEvent:
|
2021-08-11 08:36:32 +02:00
|
|
|
wm.LockoutPolicyWriteModel.AppendEvents(&e.LockoutPolicyAddedEvent)
|
2022-03-24 17:21:34 +01:00
|
|
|
case *instance.LockoutPolicyChangedEvent:
|
2021-08-11 08:36:32 +02:00
|
|
|
wm.LockoutPolicyWriteModel.AppendEvents(&e.LockoutPolicyChangedEvent)
|
2021-01-04 14:52:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-24 17:21:34 +01:00
|
|
|
func (wm *InstanceLockoutPolicyWriteModel) Reduce() error {
|
2021-08-11 08:36:32 +02:00
|
|
|
return wm.LockoutPolicyWriteModel.Reduce()
|
2021-01-04 14:52:13 +01:00
|
|
|
}
|
|
|
|
|
2022-03-24 17:21:34 +01:00
|
|
|
func (wm *InstanceLockoutPolicyWriteModel) 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().
|
2022-03-24 17:21:34 +01:00
|
|
|
AggregateTypes(instance.AggregateType).
|
2021-08-11 08:36:32 +02:00
|
|
|
AggregateIDs(wm.LockoutPolicyWriteModel.AggregateID).
|
2021-02-18 14:48:27 +01:00
|
|
|
EventTypes(
|
2022-03-24 17:21:34 +01:00
|
|
|
instance.LockoutPolicyAddedEventType,
|
|
|
|
instance.LockoutPolicyChangedEventType).
|
2021-07-06 13:55:57 +02:00
|
|
|
Builder()
|
2021-01-04 14:52:13 +01:00
|
|
|
}
|
|
|
|
|
2022-03-24 17:21:34 +01:00
|
|
|
func (wm *InstanceLockoutPolicyWriteModel) NewChangedEvent(
|
2021-02-18 14:48:27 +01:00
|
|
|
ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
2024-04-10 11:14:55 +02:00
|
|
|
maxPasswordAttempts,
|
|
|
|
maxOTPAttempts uint64,
|
2022-03-24 17:21:34 +01:00
|
|
|
showLockoutFailure bool) (*instance.LockoutPolicyChangedEvent, bool) {
|
2021-08-11 08:36:32 +02:00
|
|
|
changes := make([]policy.LockoutPolicyChanges, 0)
|
2024-04-10 11:14:55 +02:00
|
|
|
if wm.MaxPasswordAttempts != maxPasswordAttempts {
|
|
|
|
changes = append(changes, policy.ChangeMaxPasswordAttempts(maxPasswordAttempts))
|
|
|
|
}
|
|
|
|
if wm.MaxOTPAttempts != maxOTPAttempts {
|
|
|
|
changes = append(changes, policy.ChangeMaxOTPAttempts(maxOTPAttempts))
|
2021-01-04 14:52:13 +01:00
|
|
|
}
|
|
|
|
if wm.ShowLockOutFailures != showLockoutFailure {
|
2021-01-18 11:24:15 +01:00
|
|
|
changes = append(changes, policy.ChangeShowLockOutFailures(showLockoutFailure))
|
2021-01-04 14:52:13 +01:00
|
|
|
}
|
2021-01-18 11:24:15 +01:00
|
|
|
if len(changes) == 0 {
|
|
|
|
return nil, false
|
|
|
|
}
|
2022-03-24 17:21:34 +01:00
|
|
|
changedEvent, err := instance.NewLockoutPolicyChangedEvent(ctx, aggregate, changes)
|
2021-01-18 11:24:15 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
return changedEvent, true
|
2021-01-04 14:52:13 +01:00
|
|
|
}
|