2021-01-04 14:52:13 +01:00
|
|
|
package query
|
|
|
|
|
|
|
|
import (
|
2021-02-23 15:13:04 +01:00
|
|
|
"github.com/caos/zitadel/internal/domain"
|
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
|
|
|
"github.com/caos/zitadel/internal/repository/idpconfig"
|
2021-01-04 14:52:13 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type IDPConfigReadModel struct {
|
|
|
|
eventstore.ReadModel
|
|
|
|
|
|
|
|
State domain.IDPConfigState
|
|
|
|
ConfigID string
|
|
|
|
Name string
|
2021-09-10 09:49:49 +02:00
|
|
|
AutoRegister bool
|
2021-01-04 14:52:13 +01:00
|
|
|
StylingType domain.IDPConfigStylingType
|
|
|
|
ProviderType domain.IdentityProviderType
|
|
|
|
|
|
|
|
OIDCConfig *OIDCConfigReadModel
|
2021-09-14 15:15:01 +02:00
|
|
|
JWTConfig *JWTConfigReadModel
|
2021-01-04 14:52:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewIDPConfigReadModel(configID string) *IDPConfigReadModel {
|
|
|
|
return &IDPConfigReadModel{
|
|
|
|
ConfigID: configID,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-03 09:19:07 +01:00
|
|
|
func (rm *IDPConfigReadModel) AppendEvents(events ...eventstore.Event) {
|
2021-01-04 14:52:13 +01:00
|
|
|
for _, event := range events {
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *idpconfig.IDPConfigAddedEvent:
|
|
|
|
rm.ReadModel.AppendEvents(e)
|
|
|
|
case *idpconfig.IDPConfigChangedEvent:
|
|
|
|
rm.ReadModel.AppendEvents(e)
|
|
|
|
case *idpconfig.IDPConfigDeactivatedEvent:
|
|
|
|
rm.ReadModel.AppendEvents(e)
|
|
|
|
case *idpconfig.IDPConfigReactivatedEvent:
|
|
|
|
rm.ReadModel.AppendEvents(e)
|
|
|
|
case *idpconfig.IDPConfigRemovedEvent:
|
|
|
|
rm.ReadModel.AppendEvents(e)
|
|
|
|
case *idpconfig.OIDCConfigAddedEvent:
|
|
|
|
rm.OIDCConfig = &OIDCConfigReadModel{}
|
|
|
|
rm.ReadModel.AppendEvents(e)
|
|
|
|
rm.OIDCConfig.AppendEvents(event)
|
|
|
|
case *idpconfig.OIDCConfigChangedEvent:
|
|
|
|
rm.ReadModel.AppendEvents(e)
|
|
|
|
rm.OIDCConfig.AppendEvents(event)
|
2021-09-14 15:15:01 +02:00
|
|
|
case *idpconfig.JWTConfigAddedEvent:
|
|
|
|
rm.JWTConfig = &JWTConfigReadModel{}
|
|
|
|
rm.ReadModel.AppendEvents(e)
|
|
|
|
rm.JWTConfig.AppendEvents(event)
|
|
|
|
case *idpconfig.JWTConfigChangedEvent:
|
|
|
|
rm.ReadModel.AppendEvents(e)
|
|
|
|
rm.JWTConfig.AppendEvents(event)
|
2021-01-04 14:52:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rm *IDPConfigReadModel) Reduce() error {
|
|
|
|
for _, event := range rm.Events {
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *idpconfig.IDPConfigAddedEvent:
|
|
|
|
rm.reduceConfigAddedEvent(e)
|
|
|
|
case *idpconfig.IDPConfigChangedEvent:
|
|
|
|
rm.reduceConfigChangedEvent(e)
|
|
|
|
case *idpconfig.IDPConfigDeactivatedEvent:
|
|
|
|
rm.reduceConfigStateChanged(e.ConfigID, domain.IDPConfigStateInactive)
|
|
|
|
case *idpconfig.IDPConfigReactivatedEvent:
|
|
|
|
rm.reduceConfigStateChanged(e.ConfigID, domain.IDPConfigStateActive)
|
|
|
|
case *idpconfig.IDPConfigRemovedEvent:
|
|
|
|
rm.reduceConfigStateChanged(e.ConfigID, domain.IDPConfigStateRemoved)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if rm.OIDCConfig != nil {
|
|
|
|
if err := rm.OIDCConfig.Reduce(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2021-09-14 15:15:01 +02:00
|
|
|
if rm.JWTConfig != nil {
|
|
|
|
if err := rm.JWTConfig.Reduce(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2021-01-04 14:52:13 +01:00
|
|
|
return rm.ReadModel.Reduce()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rm *IDPConfigReadModel) reduceConfigAddedEvent(e *idpconfig.IDPConfigAddedEvent) {
|
|
|
|
rm.ConfigID = e.ConfigID
|
|
|
|
rm.Name = e.Name
|
|
|
|
rm.StylingType = e.StylingType
|
|
|
|
rm.State = domain.IDPConfigStateActive
|
2021-09-10 09:49:49 +02:00
|
|
|
rm.AutoRegister = e.AutoRegister
|
2021-01-04 14:52:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (rm *IDPConfigReadModel) reduceConfigChangedEvent(e *idpconfig.IDPConfigChangedEvent) {
|
2021-01-20 11:06:52 +01:00
|
|
|
if e.Name != nil {
|
|
|
|
rm.Name = *e.Name
|
2021-01-04 14:52:13 +01:00
|
|
|
}
|
2021-01-20 11:06:52 +01:00
|
|
|
if e.StylingType != nil && e.StylingType.Valid() {
|
|
|
|
rm.StylingType = *e.StylingType
|
2021-01-04 14:52:13 +01:00
|
|
|
}
|
2021-09-10 09:49:49 +02:00
|
|
|
if e.AutoRegister != nil {
|
|
|
|
rm.AutoRegister = *e.AutoRegister
|
|
|
|
}
|
2021-01-04 14:52:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (rm *IDPConfigReadModel) reduceConfigStateChanged(configID string, state domain.IDPConfigState) {
|
|
|
|
rm.State = state
|
|
|
|
}
|