2021-01-04 14:52:13 +01:00
|
|
|
package query
|
2020-11-26 09:19:14 +01:00
|
|
|
|
|
|
|
import (
|
2021-02-23 15:13:04 +01:00
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
|
|
|
"github.com/caos/zitadel/internal/repository/idpconfig"
|
2020-11-26 09:19:14 +01:00
|
|
|
)
|
|
|
|
|
2021-01-04 14:52:13 +01:00
|
|
|
type IDPConfigsReadModel struct {
|
2020-11-26 09:19:14 +01:00
|
|
|
eventstore.ReadModel
|
|
|
|
|
2021-01-04 14:52:13 +01:00
|
|
|
Configs []*IDPConfigReadModel
|
2020-11-26 09:19:14 +01:00
|
|
|
}
|
|
|
|
|
2021-01-04 14:52:13 +01:00
|
|
|
func (rm *IDPConfigsReadModel) ConfigByID(id string) (idx int, config *IDPConfigReadModel) {
|
2020-11-26 13:14:07 +01:00
|
|
|
for idx, config = range rm.Configs {
|
|
|
|
if config.ConfigID == id {
|
|
|
|
return idx, config
|
2020-11-26 09:19:14 +01:00
|
|
|
}
|
|
|
|
}
|
2020-11-26 13:14:07 +01:00
|
|
|
return -1, nil
|
2020-11-26 09:19:14 +01:00
|
|
|
}
|
|
|
|
|
2021-01-04 14:52:13 +01:00
|
|
|
func (rm *IDPConfigsReadModel) AppendEvents(events ...eventstore.EventReader) {
|
2020-11-26 13:14:07 +01:00
|
|
|
for _, event := range events {
|
2020-11-26 09:19:14 +01:00
|
|
|
switch e := event.(type) {
|
2021-01-04 14:52:13 +01:00
|
|
|
case *idpconfig.IDPConfigAddedEvent:
|
|
|
|
config := NewIDPConfigReadModel(e.ConfigID)
|
2020-11-26 13:14:07 +01:00
|
|
|
rm.Configs = append(rm.Configs, config)
|
|
|
|
config.AppendEvents(event)
|
2021-01-04 14:52:13 +01:00
|
|
|
case *idpconfig.IDPConfigChangedEvent:
|
2020-11-26 13:14:07 +01:00
|
|
|
_, config := rm.ConfigByID(e.ConfigID)
|
|
|
|
config.AppendEvents(e)
|
2021-01-04 14:52:13 +01:00
|
|
|
case *idpconfig.IDPConfigDeactivatedEvent:
|
2020-11-26 13:14:07 +01:00
|
|
|
_, config := rm.ConfigByID(e.ConfigID)
|
|
|
|
config.AppendEvents(e)
|
2021-01-04 14:52:13 +01:00
|
|
|
case *idpconfig.IDPConfigReactivatedEvent:
|
2020-11-26 13:14:07 +01:00
|
|
|
_, config := rm.ConfigByID(e.ConfigID)
|
|
|
|
config.AppendEvents(e)
|
2021-01-04 14:52:13 +01:00
|
|
|
case *idpconfig.OIDCConfigAddedEvent:
|
2020-11-26 13:14:07 +01:00
|
|
|
_, config := rm.ConfigByID(e.IDPConfigID)
|
|
|
|
config.AppendEvents(e)
|
2021-01-04 14:52:13 +01:00
|
|
|
case *idpconfig.OIDCConfigChangedEvent:
|
2020-11-26 13:14:07 +01:00
|
|
|
_, config := rm.ConfigByID(e.IDPConfigID)
|
|
|
|
config.AppendEvents(e)
|
2021-01-04 14:52:13 +01:00
|
|
|
case *idpconfig.IDPConfigRemovedEvent:
|
2020-11-26 13:14:07 +01:00
|
|
|
idx, _ := rm.ConfigByID(e.ConfigID)
|
|
|
|
if idx < 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
copy(rm.Configs[idx:], rm.Configs[idx+1:])
|
|
|
|
rm.Configs[len(rm.Configs)-1] = nil
|
|
|
|
rm.Configs = rm.Configs[:len(rm.Configs)-1]
|
2020-11-26 09:19:14 +01:00
|
|
|
}
|
|
|
|
}
|
2020-11-26 13:14:07 +01:00
|
|
|
}
|
|
|
|
|
2021-01-04 14:52:13 +01:00
|
|
|
func (rm *IDPConfigsReadModel) Reduce() error {
|
2020-11-26 13:14:07 +01:00
|
|
|
for _, config := range rm.Configs {
|
|
|
|
if err := config.Reduce(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-26 09:19:14 +01:00
|
|
|
}
|
2020-11-26 13:14:07 +01:00
|
|
|
return nil
|
2020-11-26 09:19:14 +01:00
|
|
|
}
|