2020-08-26 09:56:23 +02:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/caos/logging"
|
|
|
|
|
|
|
|
"github.com/caos/zitadel/internal/eventstore/models"
|
|
|
|
es_models "github.com/caos/zitadel/internal/eventstore/models"
|
|
|
|
"github.com/caos/zitadel/internal/eventstore/spooler"
|
|
|
|
"github.com/caos/zitadel/internal/iam/repository/eventsourcing/model"
|
|
|
|
iam_model "github.com/caos/zitadel/internal/iam/repository/view/model"
|
|
|
|
)
|
|
|
|
|
|
|
|
type LoginPolicy struct {
|
|
|
|
handler
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
loginPolicyTable = "adminapi.login_policies"
|
|
|
|
)
|
|
|
|
|
2020-12-02 08:50:59 +01:00
|
|
|
func (p *LoginPolicy) ViewModel() string {
|
2020-08-26 09:56:23 +02:00
|
|
|
return loginPolicyTable
|
|
|
|
}
|
|
|
|
|
2020-12-02 08:50:59 +01:00
|
|
|
func (p *LoginPolicy) EventQuery() (*models.SearchQuery, error) {
|
|
|
|
sequence, err := p.view.GetLatestLoginPolicySequence()
|
2020-08-26 09:56:23 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return es_models.NewSearchQuery().
|
|
|
|
AggregateTypeFilter(model.IAMAggregate).
|
|
|
|
LatestSequenceFilter(sequence.CurrentSequence), nil
|
|
|
|
}
|
|
|
|
|
2020-12-02 08:50:59 +01:00
|
|
|
func (p *LoginPolicy) Reduce(event *models.Event) (err error) {
|
2020-08-26 09:56:23 +02:00
|
|
|
switch event.AggregateType {
|
|
|
|
case model.IAMAggregate:
|
2020-12-02 08:50:59 +01:00
|
|
|
err = p.processLoginPolicy(event)
|
2020-08-26 09:56:23 +02:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-12-02 08:50:59 +01:00
|
|
|
func (p *LoginPolicy) processLoginPolicy(event *models.Event) (err error) {
|
2020-08-26 09:56:23 +02:00
|
|
|
policy := new(iam_model.LoginPolicyView)
|
|
|
|
switch event.Type {
|
|
|
|
case model.LoginPolicyAdded:
|
|
|
|
err = policy.AppendEvent(event)
|
2020-11-04 11:26:10 +01:00
|
|
|
case model.LoginPolicyChanged,
|
|
|
|
model.LoginPolicySecondFactorAdded,
|
|
|
|
model.LoginPolicySecondFactorRemoved,
|
|
|
|
model.LoginPolicyMultiFactorAdded,
|
|
|
|
model.LoginPolicyMultiFactorRemoved:
|
2020-12-02 08:50:59 +01:00
|
|
|
policy, err = p.view.LoginPolicyByAggregateID(event.AggregateID)
|
2020-08-26 09:56:23 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = policy.AppendEvent(event)
|
|
|
|
default:
|
2020-12-02 08:50:59 +01:00
|
|
|
return p.view.ProcessedLoginPolicySequence(event.Sequence, event.CreationDate)
|
2020-08-26 09:56:23 +02:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-02 08:50:59 +01:00
|
|
|
return p.view.PutLoginPolicy(policy, policy.Sequence, event.CreationDate)
|
2020-08-26 09:56:23 +02:00
|
|
|
}
|
|
|
|
|
2020-12-02 08:50:59 +01:00
|
|
|
func (p *LoginPolicy) OnError(event *models.Event, err error) error {
|
2020-08-26 09:56:23 +02:00
|
|
|
logging.LogWithFields("SPOOL-Wj8sf", "id", event.AggregateID).WithError(err).Warn("something went wrong in login policy handler")
|
2020-12-02 08:50:59 +01:00
|
|
|
return spooler.HandleError(event, err, p.view.GetLatestLoginPolicyFailedEvent, p.view.ProcessedLoginPolicyFailedEvent, p.view.ProcessedLoginPolicySequence, p.errorCountUntilSkip)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *LoginPolicy) OnSuccess() error {
|
|
|
|
return spooler.HandleSuccess(p.view.UpdateLoginPolicySpoolerRunTimestamp)
|
2020-08-26 09:56:23 +02:00
|
|
|
}
|