2020-08-26 07:56:23 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/caos/logging"
|
|
|
|
iam_model "github.com/caos/zitadel/internal/iam/model"
|
|
|
|
|
|
|
|
"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_view_model "github.com/caos/zitadel/internal/iam/repository/view/model"
|
|
|
|
)
|
|
|
|
|
|
|
|
type IDPConfig struct {
|
|
|
|
handler
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
idpConfigTable = "adminapi.idp_configs"
|
|
|
|
)
|
|
|
|
|
2020-12-02 07:50:59 +00:00
|
|
|
func (i *IDPConfig) ViewModel() string {
|
2020-08-26 07:56:23 +00:00
|
|
|
return idpConfigTable
|
|
|
|
}
|
|
|
|
|
2020-12-02 07:50:59 +00:00
|
|
|
func (i *IDPConfig) EventQuery() (*models.SearchQuery, error) {
|
|
|
|
sequence, err := i.view.GetLatestIDPConfigSequence()
|
2020-08-26 07:56:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return es_models.NewSearchQuery().
|
|
|
|
AggregateTypeFilter(model.IAMAggregate).
|
|
|
|
LatestSequenceFilter(sequence.CurrentSequence), nil
|
|
|
|
}
|
|
|
|
|
2020-12-02 07:50:59 +00:00
|
|
|
func (i *IDPConfig) Reduce(event *models.Event) (err error) {
|
2020-08-26 07:56:23 +00:00
|
|
|
switch event.AggregateType {
|
|
|
|
case model.IAMAggregate:
|
2020-12-02 07:50:59 +00:00
|
|
|
err = i.processIDPConfig(event)
|
2020-08-26 07:56:23 +00:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-12-02 07:50:59 +00:00
|
|
|
func (i *IDPConfig) processIDPConfig(event *models.Event) (err error) {
|
2020-08-26 07:56:23 +00:00
|
|
|
idp := new(iam_view_model.IDPConfigView)
|
|
|
|
switch event.Type {
|
|
|
|
case model.IDPConfigAdded:
|
|
|
|
err = idp.AppendEvent(iam_model.IDPProviderTypeSystem, event)
|
|
|
|
case model.IDPConfigChanged,
|
|
|
|
model.OIDCIDPConfigAdded,
|
|
|
|
model.OIDCIDPConfigChanged:
|
|
|
|
err = idp.SetData(event)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-02 07:50:59 +00:00
|
|
|
idp, err = i.view.IDPConfigByID(idp.IDPConfigID)
|
2020-08-26 07:56:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = idp.AppendEvent(iam_model.IDPProviderTypeSystem, event)
|
|
|
|
case model.IDPConfigRemoved:
|
|
|
|
err = idp.SetData(event)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-02 07:50:59 +00:00
|
|
|
return i.view.DeleteIDPConfig(idp.IDPConfigID, event.Sequence, event.CreationDate)
|
2020-08-26 07:56:23 +00:00
|
|
|
default:
|
2020-12-02 07:50:59 +00:00
|
|
|
return i.view.ProcessedIDPConfigSequence(event.Sequence, event.CreationDate)
|
2020-08-26 07:56:23 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-02 07:50:59 +00:00
|
|
|
return i.view.PutIDPConfig(idp, idp.Sequence, event.CreationDate)
|
2020-08-26 07:56:23 +00:00
|
|
|
}
|
|
|
|
|
2020-12-02 07:50:59 +00:00
|
|
|
func (i *IDPConfig) OnError(event *models.Event, err error) error {
|
2020-08-26 07:56:23 +00:00
|
|
|
logging.LogWithFields("SPOOL-Mslo9", "id", event.AggregateID).WithError(err).Warn("something went wrong in idp config handler")
|
2020-12-02 07:50:59 +00:00
|
|
|
return spooler.HandleError(event, err, i.view.GetLatestIDPConfigFailedEvent, i.view.ProcessedIDPConfigFailedEvent, i.view.ProcessedIDPConfigSequence, i.errorCountUntilSkip)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *IDPConfig) OnSuccess() error {
|
|
|
|
return spooler.HandleSuccess(i.view.UpdateIDPConfigSpoolerRunTimestamp)
|
2020-08-26 07:56:23 +00:00
|
|
|
}
|