2021-01-04 13:52:13 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-07-06 11:55:57 +00:00
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/api/authz"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
2021-01-12 11:59:51 +00:00
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/idpconfig"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/instance"
|
2021-01-04 13:52:13 +00:00
|
|
|
)
|
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
type InstanceIDPConfigWriteModel struct {
|
2021-01-04 13:52:13 +00:00
|
|
|
IDPConfigWriteModel
|
|
|
|
}
|
|
|
|
|
2022-04-05 05:58:09 +00:00
|
|
|
func NewInstanceIDPConfigWriteModel(ctx context.Context, configID string) *InstanceIDPConfigWriteModel {
|
2022-03-24 16:21:34 +00:00
|
|
|
return &InstanceIDPConfigWriteModel{
|
2021-01-04 13:52:13 +00:00
|
|
|
IDPConfigWriteModel{
|
|
|
|
WriteModel: eventstore.WriteModel{
|
2022-04-05 05:58:09 +00:00
|
|
|
AggregateID: authz.GetInstance(ctx).InstanceID(),
|
|
|
|
ResourceOwner: authz.GetInstance(ctx).InstanceID(),
|
2021-01-04 13:52:13 +00:00
|
|
|
},
|
|
|
|
ConfigID: configID,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
func (wm *InstanceIDPConfigWriteModel) 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().
|
2022-03-24 16:21:34 +00:00
|
|
|
AggregateTypes(instance.AggregateType).
|
2021-07-06 11:55:57 +00:00
|
|
|
AggregateIDs(wm.AggregateID).
|
2021-02-18 13:48:27 +00:00
|
|
|
EventTypes(
|
2022-03-24 16:21:34 +00:00
|
|
|
instance.IDPConfigAddedEventType,
|
|
|
|
instance.IDPConfigChangedEventType,
|
|
|
|
instance.IDPConfigDeactivatedEventType,
|
|
|
|
instance.IDPConfigReactivatedEventType,
|
|
|
|
instance.IDPConfigRemovedEventType,
|
|
|
|
instance.IDPOIDCConfigAddedEventType,
|
|
|
|
instance.IDPOIDCConfigChangedEventType).
|
2021-07-06 11:55:57 +00:00
|
|
|
Builder()
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
func (wm *InstanceIDPConfigWriteModel) AppendEvents(events ...eventstore.Event) {
|
2021-01-04 13:52:13 +00:00
|
|
|
for _, event := range events {
|
|
|
|
switch e := event.(type) {
|
2022-03-24 16:21:34 +00:00
|
|
|
case *instance.IDPConfigAddedEvent:
|
2021-01-04 13:52:13 +00:00
|
|
|
if wm.ConfigID != e.ConfigID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.IDPConfigWriteModel.AppendEvents(&e.IDPConfigAddedEvent)
|
2022-03-24 16:21:34 +00:00
|
|
|
case *instance.IDPConfigChangedEvent:
|
2021-01-04 13:52:13 +00:00
|
|
|
if wm.ConfigID != e.ConfigID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.IDPConfigWriteModel.AppendEvents(&e.IDPConfigChangedEvent)
|
2022-03-24 16:21:34 +00:00
|
|
|
case *instance.IDPConfigDeactivatedEvent:
|
2021-01-04 13:52:13 +00:00
|
|
|
if wm.ConfigID != e.ConfigID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.IDPConfigWriteModel.AppendEvents(&e.IDPConfigDeactivatedEvent)
|
2022-03-24 16:21:34 +00:00
|
|
|
case *instance.IDPConfigReactivatedEvent:
|
2021-01-04 13:52:13 +00:00
|
|
|
if wm.ConfigID != e.ConfigID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.IDPConfigWriteModel.AppendEvents(&e.IDPConfigReactivatedEvent)
|
2022-03-24 16:21:34 +00:00
|
|
|
case *instance.IDPConfigRemovedEvent:
|
2021-01-04 13:52:13 +00:00
|
|
|
if wm.ConfigID != e.ConfigID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.IDPConfigWriteModel.AppendEvents(&e.IDPConfigRemovedEvent)
|
2022-03-24 16:21:34 +00:00
|
|
|
case *instance.IDPOIDCConfigAddedEvent:
|
2021-01-04 13:52:13 +00:00
|
|
|
if wm.ConfigID != e.IDPConfigID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.IDPConfigWriteModel.AppendEvents(&e.OIDCConfigAddedEvent)
|
2022-03-24 16:21:34 +00:00
|
|
|
case *instance.IDPOIDCConfigChangedEvent:
|
2021-01-04 13:52:13 +00:00
|
|
|
if wm.ConfigID != e.IDPConfigID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.IDPConfigWriteModel.AppendEvents(&e.OIDCConfigChangedEvent)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
func (wm *InstanceIDPConfigWriteModel) Reduce() error {
|
2021-01-04 13:52:13 +00:00
|
|
|
return wm.IDPConfigWriteModel.Reduce()
|
|
|
|
}
|
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
func (wm *InstanceIDPConfigWriteModel) AppendAndReduce(events ...eventstore.Event) error {
|
2021-01-04 13:52:13 +00:00
|
|
|
wm.AppendEvents(events...)
|
|
|
|
return wm.Reduce()
|
|
|
|
}
|
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
func (wm *InstanceIDPConfigWriteModel) NewChangedEvent(
|
2021-01-04 13:52:13 +00:00
|
|
|
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,
|
2021-09-10 07:49:49 +00:00
|
|
|
autoRegister bool,
|
2022-03-24 16:21:34 +00:00
|
|
|
) (*instance.IDPConfigChangedEvent, bool) {
|
2021-01-04 13:52:13 +00:00
|
|
|
|
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-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
|
|
|
|
}
|
2022-03-24 16:21:34 +00:00
|
|
|
changeEvent, err := instance.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
|
|
|
}
|