2021-01-04 13:52:13 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-07-06 11:55:57 +00:00
|
|
|
|
2021-02-23 14:13:04 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
2021-01-12 11:59:51 +00:00
|
|
|
|
2021-02-23 14:13:04 +00:00
|
|
|
"github.com/caos/zitadel/internal/domain"
|
|
|
|
"github.com/caos/zitadel/internal/repository/iam"
|
|
|
|
"github.com/caos/zitadel/internal/repository/idpconfig"
|
2021-01-04 13:52:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type IAMIDPConfigWriteModel struct {
|
|
|
|
IDPConfigWriteModel
|
|
|
|
}
|
|
|
|
|
2021-01-12 11:59:51 +00:00
|
|
|
func NewIAMIDPConfigWriteModel(configID string) *IAMIDPConfigWriteModel {
|
2021-01-04 13:52:13 +00:00
|
|
|
return &IAMIDPConfigWriteModel{
|
|
|
|
IDPConfigWriteModel{
|
|
|
|
WriteModel: eventstore.WriteModel{
|
2021-01-12 11:59:51 +00:00
|
|
|
AggregateID: domain.IAMID,
|
|
|
|
ResourceOwner: domain.IAMID,
|
2021-01-04 13:52:13 +00:00
|
|
|
},
|
|
|
|
ConfigID: configID,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *IAMIDPConfigWriteModel) Query() *eventstore.SearchQueryBuilder {
|
2021-07-06 11:55:57 +00:00
|
|
|
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent).
|
2021-02-18 13:48:27 +00:00
|
|
|
ResourceOwner(wm.ResourceOwner).
|
2021-07-06 11:55:57 +00:00
|
|
|
AddQuery().
|
|
|
|
AggregateTypes(iam.AggregateType).
|
|
|
|
AggregateIDs(wm.AggregateID).
|
2021-02-18 13:48:27 +00:00
|
|
|
EventTypes(
|
|
|
|
iam.IDPConfigAddedEventType,
|
|
|
|
iam.IDPConfigChangedEventType,
|
|
|
|
iam.IDPConfigDeactivatedEventType,
|
|
|
|
iam.IDPConfigReactivatedEventType,
|
|
|
|
iam.IDPConfigRemovedEventType,
|
|
|
|
iam.IDPOIDCConfigAddedEventType,
|
2021-07-06 11:55:57 +00:00
|
|
|
iam.IDPOIDCConfigChangedEventType).
|
|
|
|
Builder()
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *IAMIDPConfigWriteModel) AppendEvents(events ...eventstore.EventReader) {
|
|
|
|
for _, event := range events {
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *iam.IDPConfigAddedEvent:
|
|
|
|
if wm.ConfigID != e.ConfigID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.IDPConfigWriteModel.AppendEvents(&e.IDPConfigAddedEvent)
|
|
|
|
case *iam.IDPConfigChangedEvent:
|
|
|
|
if wm.ConfigID != e.ConfigID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.IDPConfigWriteModel.AppendEvents(&e.IDPConfigChangedEvent)
|
|
|
|
case *iam.IDPConfigDeactivatedEvent:
|
|
|
|
if wm.ConfigID != e.ConfigID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.IDPConfigWriteModel.AppendEvents(&e.IDPConfigDeactivatedEvent)
|
|
|
|
case *iam.IDPConfigReactivatedEvent:
|
|
|
|
if wm.ConfigID != e.ConfigID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.IDPConfigWriteModel.AppendEvents(&e.IDPConfigReactivatedEvent)
|
|
|
|
case *iam.IDPConfigRemovedEvent:
|
|
|
|
if wm.ConfigID != e.ConfigID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.IDPConfigWriteModel.AppendEvents(&e.IDPConfigRemovedEvent)
|
|
|
|
case *iam.IDPOIDCConfigAddedEvent:
|
|
|
|
if wm.ConfigID != e.IDPConfigID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.IDPConfigWriteModel.AppendEvents(&e.OIDCConfigAddedEvent)
|
|
|
|
case *iam.IDPOIDCConfigChangedEvent:
|
|
|
|
if wm.ConfigID != e.IDPConfigID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.IDPConfigWriteModel.AppendEvents(&e.OIDCConfigChangedEvent)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *IAMIDPConfigWriteModel) Reduce() error {
|
|
|
|
return wm.IDPConfigWriteModel.Reduce()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *IAMIDPConfigWriteModel) AppendAndReduce(events ...eventstore.EventReader) error {
|
|
|
|
wm.AppendEvents(events...)
|
|
|
|
return wm.Reduce()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *IAMIDPConfigWriteModel) NewChangedEvent(
|
|
|
|
ctx context.Context,
|
2021-02-18 13:48:27 +00:00
|
|
|
aggregate *eventstore.Aggregate,
|
2021-01-04 13:52:13 +00:00
|
|
|
configID,
|
|
|
|
name string,
|
|
|
|
stylingType domain.IDPConfigStylingType,
|
|
|
|
) (*iam.IDPConfigChangedEvent, bool) {
|
|
|
|
|
2021-01-20 10:06:52 +00:00
|
|
|
changes := make([]idpconfig.IDPConfigChanges, 0)
|
2021-02-15 12:31:24 +00:00
|
|
|
oldName := ""
|
2021-01-04 13:52:13 +00:00
|
|
|
if wm.Name != name {
|
2021-02-15 12:31:24 +00:00
|
|
|
oldName = wm.Name
|
2021-01-20 10:06:52 +00:00
|
|
|
changes = append(changes, idpconfig.ChangeName(name))
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
|
|
|
if stylingType.Valid() && wm.StylingType != stylingType {
|
2021-01-20 10:06:52 +00:00
|
|
|
changes = append(changes, idpconfig.ChangeStyleType(stylingType))
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
2021-01-20 10:06:52 +00:00
|
|
|
if len(changes) == 0 {
|
|
|
|
return nil, false
|
|
|
|
}
|
2021-02-18 13:48:27 +00:00
|
|
|
changeEvent, err := iam.NewIDPConfigChangedEvent(ctx, aggregate, configID, oldName, changes)
|
2021-01-20 10:06:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
return changeEvent, true
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|