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