2021-01-20 10:06:52 +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-20 10:06:52 +00:00
|
|
|
|
2021-02-23 14:13:04 +00:00
|
|
|
"github.com/caos/zitadel/internal/domain"
|
|
|
|
"github.com/caos/zitadel/internal/repository/idpconfig"
|
|
|
|
"github.com/caos/zitadel/internal/repository/org"
|
2021-01-20 10:06:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type OrgIDPConfigWriteModel struct {
|
|
|
|
IDPConfigWriteModel
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewOrgIDPConfigWriteModel(configID, orgID string) *OrgIDPConfigWriteModel {
|
|
|
|
return &OrgIDPConfigWriteModel{
|
|
|
|
IDPConfigWriteModel{
|
|
|
|
WriteModel: eventstore.WriteModel{
|
|
|
|
AggregateID: orgID,
|
|
|
|
ResourceOwner: orgID,
|
|
|
|
},
|
|
|
|
ConfigID: configID,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *OrgIDPConfigWriteModel) 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(org.AggregateType).
|
|
|
|
AggregateIDs(wm.AggregateID).
|
2021-02-18 13:48:27 +00:00
|
|
|
EventTypes(
|
|
|
|
org.IDPConfigAddedEventType,
|
|
|
|
org.IDPConfigChangedEventType,
|
|
|
|
org.IDPConfigDeactivatedEventType,
|
|
|
|
org.IDPConfigReactivatedEventType,
|
|
|
|
org.IDPConfigRemovedEventType,
|
|
|
|
org.IDPOIDCConfigAddedEventType,
|
2021-07-06 11:55:57 +00:00
|
|
|
org.IDPOIDCConfigChangedEventType).
|
|
|
|
Builder()
|
2021-01-20 10:06:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *OrgIDPConfigWriteModel) AppendEvents(events ...eventstore.EventReader) {
|
|
|
|
for _, event := range events {
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *org.IDPConfigAddedEvent:
|
|
|
|
if wm.ConfigID != e.ConfigID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.IDPConfigWriteModel.AppendEvents(&e.IDPConfigAddedEvent)
|
|
|
|
case *org.IDPConfigChangedEvent:
|
|
|
|
if wm.ConfigID != e.ConfigID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.IDPConfigWriteModel.AppendEvents(&e.IDPConfigChangedEvent)
|
|
|
|
case *org.IDPConfigDeactivatedEvent:
|
|
|
|
if wm.ConfigID != e.ConfigID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.IDPConfigWriteModel.AppendEvents(&e.IDPConfigDeactivatedEvent)
|
|
|
|
case *org.IDPConfigReactivatedEvent:
|
|
|
|
if wm.ConfigID != e.ConfigID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.IDPConfigWriteModel.AppendEvents(&e.IDPConfigReactivatedEvent)
|
|
|
|
case *org.IDPConfigRemovedEvent:
|
|
|
|
if wm.ConfigID != e.ConfigID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.IDPConfigWriteModel.AppendEvents(&e.IDPConfigRemovedEvent)
|
|
|
|
case *org.IDPOIDCConfigAddedEvent:
|
|
|
|
if wm.ConfigID != e.IDPConfigID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.IDPConfigWriteModel.AppendEvents(&e.OIDCConfigAddedEvent)
|
|
|
|
case *org.IDPOIDCConfigChangedEvent:
|
|
|
|
if wm.ConfigID != e.IDPConfigID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.IDPConfigWriteModel.AppendEvents(&e.OIDCConfigChangedEvent)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *OrgIDPConfigWriteModel) Reduce() error {
|
|
|
|
return wm.IDPConfigWriteModel.Reduce()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *OrgIDPConfigWriteModel) AppendAndReduce(events ...eventstore.EventReader) error {
|
|
|
|
wm.AppendEvents(events...)
|
|
|
|
return wm.Reduce()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *OrgIDPConfigWriteModel) NewChangedEvent(
|
|
|
|
ctx context.Context,
|
2021-02-18 13:48:27 +00:00
|
|
|
aggregate *eventstore.Aggregate,
|
2021-01-20 10:06:52 +00:00
|
|
|
configID,
|
|
|
|
name string,
|
|
|
|
stylingType domain.IDPConfigStylingType,
|
2021-09-10 07:49:49 +00:00
|
|
|
autoRegister bool,
|
2021-01-20 10:06:52 +00:00
|
|
|
) (*org.IDPConfigChangedEvent, bool) {
|
|
|
|
|
|
|
|
changes := make([]idpconfig.IDPConfigChanges, 0)
|
2021-02-15 12:31:24 +00:00
|
|
|
oldName := ""
|
2021-01-20 10:06:52 +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))
|
|
|
|
}
|
|
|
|
if stylingType.Valid() && wm.StylingType != stylingType {
|
|
|
|
changes = append(changes, idpconfig.ChangeStyleType(stylingType))
|
|
|
|
}
|
2021-09-10 07:49:49 +00:00
|
|
|
if wm.AutoRegister != autoRegister {
|
|
|
|
changes = append(changes, idpconfig.ChangeAutoRegister(autoRegister))
|
|
|
|
}
|
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 := org.NewIDPConfigChangedEvent(ctx, aggregate, configID, oldName, changes)
|
2021-01-20 10:06:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
return changeEvent, true
|
|
|
|
}
|