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