zitadel/internal/v2/repository/iam/idp_config.go

298 lines
7.0 KiB
Go
Raw Normal View History

2020-11-25 19:04:32 +00:00
package iam
import (
"context"
"github.com/caos/zitadel/internal/eventstore/v2"
2020-11-26 12:14:07 +00:00
"github.com/caos/zitadel/internal/eventstore/v2/repository"
2020-11-25 19:04:32 +00:00
"github.com/caos/zitadel/internal/v2/repository/idp"
)
const (
IDPConfigAddedEventType eventstore.EventType = "iam.idp.config.added"
IDPConfigChangedEventType eventstore.EventType = "iam.idp.config.changed"
IDPConfigRemovedEventType eventstore.EventType = "iam.idp.config.removed"
IDPConfigDeactivatedEventType eventstore.EventType = "iam.idp.config.deactivated"
IDPConfigReactivatedEventType eventstore.EventType = "iam.idp.config.reactivated"
)
type IDPConfigReadModel struct {
idp.ConfigReadModel
2020-11-27 10:30:56 +00:00
iamID string
configID string
}
func NewIDPConfigReadModel(iamID, configID string) *IDPConfigReadModel {
return &IDPConfigReadModel{
iamID: iamID,
configID: configID,
}
2020-11-25 19:04:32 +00:00
}
func (rm *IDPConfigReadModel) AppendEvents(events ...eventstore.EventReader) {
for _, event := range events {
switch e := event.(type) {
case *IDPConfigAddedEvent:
rm.ConfigReadModel.AppendEvents(&e.ConfigAddedEvent)
case *IDPConfigChangedEvent:
rm.ConfigReadModel.AppendEvents(&e.ConfigChangedEvent)
case *IDPConfigDeactivatedEvent:
rm.ConfigReadModel.AppendEvents(&e.ConfigDeactivatedEvent)
case *IDPConfigReactivatedEvent:
rm.ConfigReadModel.AppendEvents(&e.ConfigReactivatedEvent)
case *IDPConfigRemovedEvent:
rm.ConfigReadModel.AppendEvents(&e.ConfigRemovedEvent)
2020-11-26 12:14:07 +00:00
case *IDPOIDCConfigAddedEvent:
rm.ConfigReadModel.AppendEvents(&e.ConfigAddedEvent)
case *IDPOIDCConfigChangedEvent:
rm.ConfigReadModel.AppendEvents(&e.ConfigChangedEvent)
2020-11-25 19:04:32 +00:00
}
}
}
2020-11-27 10:30:56 +00:00
func (rm *IDPConfigReadModel) Query() *eventstore.SearchQueryFactory {
return eventstore.NewSearchQueryFactory(eventstore.ColumnsEvent, AggregateType).
AggregateIDs(rm.iamID).
EventData(map[string]interface{}{
"idpConfigId": rm.configID,
})
}
2020-11-25 19:04:32 +00:00
type IDPConfigWriteModel struct {
2020-11-27 10:30:56 +00:00
eventstore.WriteModel
2020-11-25 19:04:32 +00:00
idp.ConfigWriteModel
2020-11-27 10:30:56 +00:00
iamID string
configID string
}
func NewIDPConfigWriteModel(iamID, configID string) *IDPConfigWriteModel {
return &IDPConfigWriteModel{
iamID: iamID,
configID: configID,
}
2020-11-25 19:04:32 +00:00
}
2020-11-27 10:30:56 +00:00
func (wm *IDPConfigWriteModel) Query() *eventstore.SearchQueryFactory {
return eventstore.NewSearchQueryFactory(eventstore.ColumnsEvent, AggregateType).
AggregateIDs(wm.iamID)
}
func (wm *IDPConfigWriteModel) AppendEvents(events ...eventstore.EventReader) {
wm.WriteModel.AppendEvents(events...)
2020-11-25 19:04:32 +00:00
for _, event := range events {
switch e := event.(type) {
case *IDPConfigAddedEvent:
2020-11-27 10:30:56 +00:00
if wm.configID != e.ConfigID {
continue
}
wm.ConfigWriteModel.AppendEvents(&e.ConfigAddedEvent)
2020-11-25 19:04:32 +00:00
case *IDPConfigChangedEvent:
2020-11-27 10:30:56 +00:00
if wm.configID != e.ConfigID {
continue
}
wm.ConfigWriteModel.AppendEvents(&e.ConfigChangedEvent)
2020-11-25 19:04:32 +00:00
case *IDPConfigDeactivatedEvent:
2020-11-27 10:30:56 +00:00
if wm.configID != e.ConfigID {
continue
}
wm.ConfigWriteModel.AppendEvents(&e.ConfigDeactivatedEvent)
2020-11-25 19:04:32 +00:00
case *IDPConfigReactivatedEvent:
2020-11-27 10:30:56 +00:00
if wm.configID != e.ConfigID {
continue
}
wm.ConfigWriteModel.AppendEvents(&e.ConfigReactivatedEvent)
2020-11-25 19:04:32 +00:00
case *IDPConfigRemovedEvent:
2020-11-27 10:30:56 +00:00
if wm.configID != e.ConfigID {
continue
}
wm.ConfigWriteModel.AppendEvents(&e.ConfigRemovedEvent)
case *IDPOIDCConfigAddedEvent:
if wm.configID != e.IDPConfigID {
continue
}
wm.ConfigWriteModel.AppendEvents(&e.ConfigAddedEvent)
case *IDPOIDCConfigChangedEvent:
if wm.configID != e.IDPConfigID {
continue
}
wm.ConfigWriteModel.AppendEvents(&e.ConfigChangedEvent)
2020-11-25 19:04:32 +00:00
}
}
}
2020-11-27 10:30:56 +00:00
func (wm *IDPConfigWriteModel) Reduce() error {
if err := wm.ConfigWriteModel.Reduce(); err != nil {
return err
}
return wm.WriteModel.Reduce()
}
func (wm *IDPConfigWriteModel) AppendAndReduce(events ...eventstore.EventReader) error {
wm.AppendEvents(events...)
return wm.Reduce()
}
2020-11-25 19:04:32 +00:00
type IDPConfigAddedEvent struct {
idp.ConfigAddedEvent
}
func NewIDPConfigAddedEvent(
ctx context.Context,
configID string,
name string,
configType idp.ConfigType,
stylingType idp.StylingType,
) *IDPConfigAddedEvent {
return &IDPConfigAddedEvent{
ConfigAddedEvent: *idp.NewConfigAddedEvent(
eventstore.NewBaseEventForPush(
ctx,
IDPConfigAddedEventType,
),
configID,
name,
configType,
stylingType,
),
}
}
2020-11-26 12:14:07 +00:00
func IDPConfigAddedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
e, err := idp.ConfigAddedEventMapper(event)
if err != nil {
return nil, err
}
return &IDPConfigAddedEvent{ConfigAddedEvent: *e}, nil
}
2020-11-25 19:04:32 +00:00
type IDPConfigChangedEvent struct {
idp.ConfigChangedEvent
}
func NewIDPConfigChangedEvent(
ctx context.Context,
current *IDPConfigWriteModel,
configID string,
name string,
configType idp.ConfigType,
stylingType idp.StylingType,
) (*IDPConfigChangedEvent, error) {
event, err := idp.NewConfigChangedEvent(
eventstore.NewBaseEventForPush(
ctx,
IDPConfigChangedEventType,
),
&current.ConfigWriteModel,
name,
stylingType,
)
if err != nil {
return nil, err
}
return &IDPConfigChangedEvent{
ConfigChangedEvent: *event,
}, nil
}
2020-11-26 12:14:07 +00:00
func IDPConfigChangedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
e, err := idp.ConfigChangedEventMapper(event)
if err != nil {
return nil, err
}
return &IDPConfigChangedEvent{ConfigChangedEvent: *e}, nil
}
2020-11-25 19:04:32 +00:00
type IDPConfigRemovedEvent struct {
idp.ConfigRemovedEvent
}
func NewIDPConfigRemovedEvent(
ctx context.Context,
configID string,
) *IDPConfigRemovedEvent {
return &IDPConfigRemovedEvent{
ConfigRemovedEvent: *idp.NewConfigRemovedEvent(
eventstore.NewBaseEventForPush(
ctx,
IDPConfigRemovedEventType,
),
configID,
),
}
}
2020-11-26 12:14:07 +00:00
func IDPConfigRemovedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
e, err := idp.ConfigRemovedEventMapper(event)
if err != nil {
return nil, err
}
return &IDPConfigRemovedEvent{ConfigRemovedEvent: *e}, nil
}
2020-11-25 19:04:32 +00:00
type IDPConfigDeactivatedEvent struct {
idp.ConfigDeactivatedEvent
}
func NewIDPConfigDeactivatedEvent(
ctx context.Context,
configID string,
) *IDPConfigDeactivatedEvent {
return &IDPConfigDeactivatedEvent{
ConfigDeactivatedEvent: *idp.NewConfigDeactivatedEvent(
eventstore.NewBaseEventForPush(
ctx,
IDPConfigDeactivatedEventType,
),
configID,
),
}
}
2020-11-26 12:14:07 +00:00
func IDPConfigDeactivatedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
e, err := idp.ConfigDeactivatedEventMapper(event)
if err != nil {
return nil, err
}
return &IDPConfigDeactivatedEvent{ConfigDeactivatedEvent: *e}, nil
}
2020-11-25 19:04:32 +00:00
type IDPConfigReactivatedEvent struct {
idp.ConfigReactivatedEvent
}
func NewIDPConfigReactivatedEvent(
ctx context.Context,
configID string,
) *IDPConfigReactivatedEvent {
return &IDPConfigReactivatedEvent{
ConfigReactivatedEvent: *idp.NewConfigReactivatedEvent(
eventstore.NewBaseEventForPush(
ctx,
IDPConfigReactivatedEventType,
),
configID,
),
}
}
2020-11-26 12:14:07 +00:00
func IDPConfigReactivatedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
e, err := idp.ConfigReactivatedEventMapper(event)
if err != nil {
return nil, err
}
return &IDPConfigReactivatedEvent{ConfigReactivatedEvent: *e}, nil
}