zitadel/internal/v2/repository/idp/config_read_model.go

94 lines
2.3 KiB
Go
Raw Normal View History

2020-11-25 19:04:32 +00:00
package idp
import (
"github.com/caos/zitadel/internal/eventstore/v2"
"github.com/caos/zitadel/internal/v2/repository/idp/oidc"
2020-12-15 11:00:53 +00:00
"github.com/caos/zitadel/internal/v2/repository/policy/login/idpprovider"
2020-11-25 19:04:32 +00:00
)
type ConfigReadModel struct {
eventstore.ReadModel
State ConfigState
ConfigID string
Name string
StylingType StylingType
2020-12-15 11:00:53 +00:00
ProviderType idpprovider.Type
2020-11-25 19:04:32 +00:00
OIDCConfig *oidc.ConfigReadModel
}
2020-11-26 12:14:07 +00:00
func NewConfigReadModel(configID string) *ConfigReadModel {
return &ConfigReadModel{
ConfigID: configID,
}
}
2020-11-25 19:04:32 +00:00
func (rm *ConfigReadModel) AppendEvents(events ...eventstore.EventReader) {
for _, event := range events {
2020-11-27 10:30:56 +00:00
switch e := event.(type) {
case *ConfigAddedEvent:
rm.ReadModel.AppendEvents(e)
case *ConfigChangedEvent:
rm.ReadModel.AppendEvents(e)
case *ConfigDeactivatedEvent:
rm.ReadModel.AppendEvents(e)
case *ConfigReactivatedEvent:
rm.ReadModel.AppendEvents(e)
case *ConfigRemovedEvent:
rm.ReadModel.AppendEvents(e)
2020-11-25 19:04:32 +00:00
case *oidc.ConfigAddedEvent:
rm.OIDCConfig = &oidc.ConfigReadModel{}
2020-11-27 10:30:56 +00:00
rm.ReadModel.AppendEvents(e)
2020-11-25 19:04:32 +00:00
rm.OIDCConfig.AppendEvents(event)
case *oidc.ConfigChangedEvent:
2020-11-27 10:30:56 +00:00
rm.ReadModel.AppendEvents(e)
2020-11-25 19:04:32 +00:00
rm.OIDCConfig.AppendEvents(event)
}
}
}
func (rm *ConfigReadModel) Reduce() error {
for _, event := range rm.Events {
switch e := event.(type) {
case *ConfigAddedEvent:
2020-11-27 10:30:56 +00:00
rm.reduceConfigAddedEvent(e)
2020-11-25 19:04:32 +00:00
case *ConfigChangedEvent:
2020-11-27 10:30:56 +00:00
rm.reduceConfigChangedEvent(e)
2020-11-25 19:04:32 +00:00
case *ConfigDeactivatedEvent:
2020-11-27 10:30:56 +00:00
rm.reduceConfigStateChanged(e.ConfigID, ConfigStateInactive)
2020-11-25 19:04:32 +00:00
case *ConfigReactivatedEvent:
2020-11-27 10:30:56 +00:00
rm.reduceConfigStateChanged(e.ConfigID, ConfigStateActive)
2020-11-25 19:04:32 +00:00
case *ConfigRemovedEvent:
2020-11-27 10:30:56 +00:00
rm.reduceConfigStateChanged(e.ConfigID, ConfigStateRemoved)
2020-11-25 19:04:32 +00:00
}
}
2020-11-26 12:14:07 +00:00
2020-11-27 10:30:56 +00:00
if rm.OIDCConfig != nil {
if err := rm.OIDCConfig.Reduce(); err != nil {
return err
}
2020-11-25 19:04:32 +00:00
}
return rm.ReadModel.Reduce()
}
2020-11-27 10:30:56 +00:00
func (rm *ConfigReadModel) reduceConfigAddedEvent(e *ConfigAddedEvent) {
rm.ConfigID = e.ConfigID
rm.Name = e.Name
rm.StylingType = e.StylingType
rm.State = ConfigStateActive
}
func (rm *ConfigReadModel) reduceConfigChangedEvent(e *ConfigChangedEvent) {
if e.Name != "" {
rm.Name = e.Name
}
if e.StylingType.Valid() {
rm.StylingType = e.StylingType
}
}
func (rm *ConfigReadModel) reduceConfigStateChanged(configID string, state ConfigState) {
rm.State = state
}