mirror of
https://github.com/zitadel/zitadel.git
synced 2025-08-11 21:27:42 +00:00
idp command side done
This commit is contained in:
@@ -109,13 +109,12 @@ func readModelToObjectRoot(readModel eventstore.ReadModel) models.ObjectRoot {
|
||||
}
|
||||
}
|
||||
|
||||
func writeModelToObjectRoot(readModel eventstore.WriteModel) models.ObjectRoot {
|
||||
func writeModelToObjectRoot(writeModel eventstore.WriteModel) models.ObjectRoot {
|
||||
return models.ObjectRoot{
|
||||
AggregateID: readModel.AggregateID,
|
||||
// ChangeDate: readModel.ChangeDate,
|
||||
// CreationDate: readModel.CreationDate,
|
||||
ResourceOwner: readModel.ResourceOwner,
|
||||
Sequence: readModel.ProcessedSequence,
|
||||
AggregateID: writeModel.AggregateID,
|
||||
ChangeDate: writeModel.ChangeDate,
|
||||
ResourceOwner: writeModel.ResourceOwner,
|
||||
Sequence: writeModel.ProcessedSequence,
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -30,20 +30,14 @@ func (r *Repository) AddIDPConfig(ctx context.Context, config *iam_model.IDPConf
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
wm := iam.NewIDPConfigWriteModel(config.AggregateID, idpConfigID)
|
||||
err = r.eventstore.FilterToQueryReducer(ctx, wm)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
clientSecret, err := crypto.Crypt([]byte(config.OIDCConfig.ClientSecretString), r.secretCrypto)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
aggregate := iam.AggregateFromWriteModel(&wm.WriteModel).
|
||||
PushIDPConfigAdded(ctx, idpConfigID, config.Name, idp.ConfigType(config.Type), idp.StylingType(config.StylingType)).
|
||||
PushIDPOIDCConfigAdded(
|
||||
writeModel, err := r.pushIDPWriteModel(ctx, config.AggregateID, idpConfigID, func(a *iam.Aggregate, _ *iam.IDPConfigWriteModel) *iam.Aggregate {
|
||||
return a.PushIDPOIDCConfigAdded(
|
||||
ctx,
|
||||
config.OIDCConfig.ClientID,
|
||||
idpConfigID,
|
||||
@@ -52,43 +46,77 @@ func (r *Repository) AddIDPConfig(ctx context.Context, config *iam_model.IDPConf
|
||||
oidc.MappingField(config.OIDCConfig.IDPDisplayNameMapping),
|
||||
oidc.MappingField(config.OIDCConfig.UsernameMapping),
|
||||
config.OIDCConfig.Scopes...)
|
||||
|
||||
events, err := r.eventstore.PushAggregates(ctx, aggregate)
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err = wm.AppendAndReduce(events...); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return writeModelToIDPConfig(wm), nil
|
||||
return writeModelToIDPConfig(writeModel), nil
|
||||
}
|
||||
|
||||
func (r *Repository) ChangeIDPConfig(ctx context.Context, config *iam_model.IDPConfig) (*iam_model.IDPConfig, error) {
|
||||
writeModel := iam.NewIDPConfigWriteModel(config.AggregateID, config.IDPConfigID)
|
||||
err := r.eventstore.FilterToQueryReducer(ctx, writeModel)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
aggregate := iam.AggregateFromWriteModel(&writeModel.WriteModel).
|
||||
PushIDPConfigChanged(
|
||||
writeModel, err := r.pushIDPWriteModel(ctx, config.AggregateID, config.IDPConfigID, func(a *iam.Aggregate, writeModel *iam.IDPConfigWriteModel) *iam.Aggregate {
|
||||
return a.PushIDPConfigChanged(
|
||||
ctx,
|
||||
writeModel,
|
||||
config.IDPConfigID,
|
||||
config.Name,
|
||||
idp.ConfigType(config.Type),
|
||||
idp.StylingType(config.StylingType))
|
||||
|
||||
events, err := r.eventstore.PushAggregates(ctx, aggregate)
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err = writeModel.AppendAndReduce(events...); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return writeModelToIDPConfig(writeModel), nil
|
||||
}
|
||||
|
||||
func (r *Repository) DeactivateIDPConfig(ctx context.Context, iamID, idpID string) (*iam_model.IDPConfig, error) {
|
||||
writeModel, err := r.pushIDPWriteModel(ctx, iamID, idpID, func(a *iam.Aggregate, _ *iam.IDPConfigWriteModel) *iam.Aggregate {
|
||||
return a.PushIDPConfigDeactivated(ctx, idpID)
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return writeModelToIDPConfig(writeModel), nil
|
||||
}
|
||||
|
||||
func (r *Repository) ReactivateIDPConfig(ctx context.Context, iamID, idpID string) (*iam_model.IDPConfig, error) {
|
||||
writeModel, err := r.pushIDPWriteModel(ctx, iamID, idpID, func(a *iam.Aggregate, _ *iam.IDPConfigWriteModel) *iam.Aggregate {
|
||||
return a.PushIDPConfigReactivated(ctx, idpID)
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return writeModelToIDPConfig(writeModel), nil
|
||||
}
|
||||
|
||||
func (r *Repository) RemoveIDPConfig(ctx context.Context, iamID, idpID string) (*iam_model.IDPConfig, error) {
|
||||
writeModel, err := r.pushIDPWriteModel(ctx, iamID, idpID, func(a *iam.Aggregate, _ *iam.IDPConfigWriteModel) *iam.Aggregate {
|
||||
return a.PushIDPConfigRemoved(ctx, idpID)
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return writeModelToIDPConfig(writeModel), nil
|
||||
}
|
||||
|
||||
func (r *Repository) pushIDPWriteModel(ctx context.Context, iamID, idpID string, eventSetter func(*iam.Aggregate, *iam.IDPConfigWriteModel) *iam.Aggregate) (*iam.IDPConfigWriteModel, error) {
|
||||
writeModel := iam.NewIDPConfigWriteModel(iamID, idpID)
|
||||
err := r.eventstore.FilterToQueryReducer(ctx, writeModel)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
aggregate := eventSetter(iam.AggregateFromWriteModel(&writeModel.WriteModel), writeModel)
|
||||
|
||||
err = r.eventstore.PushAggregate(ctx, writeModel, aggregate)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return writeModel, nil
|
||||
}
|
||||
|
@@ -26,7 +26,7 @@ func (r *Repository) ChangeIDPOIDCConfig(ctx context.Context, config *iam_model.
|
||||
}
|
||||
}
|
||||
|
||||
aggregate := iam.AggregateFromWriteModel(&writeModel.ConfigWriteModel.WriteModel).
|
||||
aggregate := iam.AggregateFromWriteModel(&writeModel.WriteModel).
|
||||
PushIDPOIDCConfigChanged(
|
||||
ctx,
|
||||
writeModel,
|
||||
|
@@ -15,6 +15,7 @@ const (
|
||||
)
|
||||
|
||||
type IDPOIDCConfigWriteModel struct {
|
||||
eventstore.WriteModel
|
||||
oidc.ConfigWriteModel
|
||||
|
||||
iamID string
|
||||
@@ -28,12 +29,8 @@ func NewIDPOIDCConfigWriteModel(iamID, idpConfigID string) *IDPOIDCConfigWriteMo
|
||||
}
|
||||
}
|
||||
|
||||
func (wm *IDPOIDCConfigWriteModel) Query() *eventstore.SearchQueryFactory {
|
||||
return eventstore.NewSearchQueryFactory(eventstore.ColumnsEvent, AggregateType).
|
||||
AggregateIDs(wm.iamID)
|
||||
}
|
||||
|
||||
func (wm *IDPOIDCConfigWriteModel) AppendEvents(events ...eventstore.EventReader) {
|
||||
wm.WriteModel.AppendEvents(events...)
|
||||
for _, event := range events {
|
||||
switch e := event.(type) {
|
||||
case *IDPOIDCConfigAddedEvent:
|
||||
@@ -52,6 +49,18 @@ func (wm *IDPOIDCConfigWriteModel) AppendEvents(events ...eventstore.EventReader
|
||||
}
|
||||
}
|
||||
|
||||
func (wm *IDPOIDCConfigWriteModel) Reduce() error {
|
||||
if err := wm.ConfigWriteModel.Reduce(); err != nil {
|
||||
return err
|
||||
}
|
||||
return wm.WriteModel.Reduce()
|
||||
}
|
||||
|
||||
func (wm *IDPOIDCConfigWriteModel) Query() *eventstore.SearchQueryFactory {
|
||||
return eventstore.NewSearchQueryFactory(eventstore.ColumnsEvent, AggregateType).
|
||||
AggregateIDs(wm.iamID)
|
||||
}
|
||||
|
||||
type IDPOIDCConfigAddedEvent struct {
|
||||
oidc.ConfigAddedEvent
|
||||
}
|
||||
|
Reference in New Issue
Block a user