2021-01-04 14:52:13 +01:00
|
|
|
package command
|
2020-12-11 15:49:19 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/caos/zitadel/internal/eventstore/v2"
|
2021-01-04 14:52:13 +01:00
|
|
|
"github.com/caos/zitadel/internal/v2/domain"
|
|
|
|
"github.com/caos/zitadel/internal/v2/repository/policy"
|
2020-12-11 15:49:19 +01:00
|
|
|
)
|
|
|
|
|
2021-01-04 14:52:13 +01:00
|
|
|
type LoginPolicyWriteModel struct {
|
2020-12-11 15:49:19 +01:00
|
|
|
eventstore.WriteModel
|
|
|
|
|
|
|
|
AllowUserNamePassword bool
|
|
|
|
AllowRegister bool
|
|
|
|
AllowExternalIDP bool
|
|
|
|
ForceMFA bool
|
2021-01-04 14:52:13 +01:00
|
|
|
PasswordlessType domain.PasswordlessType
|
2021-01-07 16:06:45 +01:00
|
|
|
State domain.PolicyState
|
2020-12-11 15:49:19 +01:00
|
|
|
}
|
|
|
|
|
2021-01-04 14:52:13 +01:00
|
|
|
func (wm *LoginPolicyWriteModel) Reduce() error {
|
2020-12-11 15:49:19 +01:00
|
|
|
for _, event := range wm.Events {
|
|
|
|
switch e := event.(type) {
|
2021-01-04 14:52:13 +01:00
|
|
|
case *policy.LoginPolicyAddedEvent:
|
2020-12-11 15:49:19 +01:00
|
|
|
wm.AllowRegister = e.AllowRegister
|
|
|
|
wm.AllowUserNamePassword = e.AllowUserNamePassword
|
|
|
|
wm.AllowExternalIDP = e.AllowExternalIDP
|
|
|
|
wm.ForceMFA = e.ForceMFA
|
|
|
|
wm.PasswordlessType = e.PasswordlessType
|
2021-01-07 16:06:45 +01:00
|
|
|
wm.State = domain.PolicyStateActive
|
2021-01-04 14:52:13 +01:00
|
|
|
case *policy.LoginPolicyChangedEvent:
|
2021-01-06 11:12:56 +01:00
|
|
|
if e.AllowRegister != nil {
|
|
|
|
wm.AllowRegister = *e.AllowRegister
|
|
|
|
}
|
|
|
|
if e.AllowUserNamePassword != nil {
|
|
|
|
wm.AllowUserNamePassword = *e.AllowUserNamePassword
|
|
|
|
}
|
|
|
|
if e.AllowExternalIDP != nil {
|
|
|
|
wm.AllowExternalIDP = *e.AllowExternalIDP
|
|
|
|
}
|
|
|
|
if e.ForceMFA != nil {
|
|
|
|
wm.ForceMFA = *e.ForceMFA
|
|
|
|
}
|
|
|
|
if e.PasswordlessType != nil {
|
|
|
|
wm.PasswordlessType = *e.PasswordlessType
|
|
|
|
}
|
2021-01-04 14:52:13 +01:00
|
|
|
case *policy.LoginPolicyRemovedEvent:
|
2021-01-07 16:06:45 +01:00
|
|
|
wm.State = domain.PolicyStateRemoved
|
2020-12-11 15:49:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return wm.WriteModel.Reduce()
|
|
|
|
}
|