mirror of
https://github.com/zitadel/zitadel.git
synced 2024-12-13 11:34:26 +00:00
c2e6e782a8
* add setup steps * refactoring * omitempty * cleanup * begin org * create org * setup org * setup org * merge * fixes * fixes * fixes * add project * add oidc application * fix app creation * add resourceOwner to writemodels * resource owner * cleanup * global org, iam project and iam member in setup * logs * logs * logs * cleanup * Update internal/v2/command/project.go Co-authored-by: Fabi <38692350+fgerschwiler@users.noreply.github.com> * check project state * add org domain commands * add org status changes and member commands * fixes * policies * login policy * fix iam project event * mapper * label policy * change to command * fix * fix * handle change event differently and lot of fixes * idps * fixes * fixes * fixes * changedEvent handling * fix change events * remove creation date Co-authored-by: Fabi <38692350+fgerschwiler@users.noreply.github.com>
147 lines
4.8 KiB
Go
147 lines
4.8 KiB
Go
package command
|
|
|
|
import (
|
|
"context"
|
|
|
|
caos_errs "github.com/caos/zitadel/internal/errors"
|
|
"github.com/caos/zitadel/internal/telemetry/tracing"
|
|
"github.com/caos/zitadel/internal/v2/domain"
|
|
|
|
"github.com/caos/zitadel/internal/crypto"
|
|
"github.com/caos/zitadel/internal/errors"
|
|
"github.com/caos/zitadel/internal/v2/repository/org"
|
|
org_repo "github.com/caos/zitadel/internal/v2/repository/org"
|
|
)
|
|
|
|
func (r *CommandSide) AddIDPConfig(ctx context.Context, config *domain.IDPConfig) (*domain.IDPConfig, error) {
|
|
if config.OIDCConfig == nil {
|
|
return nil, errors.ThrowInvalidArgument(nil, "Org-eUpQU", "Errors.idp.config.notset")
|
|
}
|
|
|
|
idpConfigID, err := r.idGenerator.Next()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
//TODO: check name unique on aggregate
|
|
addedConfig := NewOrgIDPConfigWriteModel(idpConfigID, config.AggregateID)
|
|
|
|
clientSecret, err := crypto.Crypt([]byte(config.OIDCConfig.ClientSecretString), r.idpConfigSecretCrypto)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
orgAgg := OrgAggregateFromWriteModel(&addedConfig.WriteModel)
|
|
orgAgg.PushEvents(
|
|
org_repo.NewIDPConfigAddedEvent(
|
|
ctx,
|
|
idpConfigID,
|
|
config.Name,
|
|
config.Type,
|
|
config.StylingType,
|
|
),
|
|
)
|
|
orgAgg.PushEvents(
|
|
org_repo.NewIDPOIDCConfigAddedEvent(
|
|
ctx, config.OIDCConfig.ClientID,
|
|
idpConfigID,
|
|
config.OIDCConfig.Issuer,
|
|
clientSecret,
|
|
config.OIDCConfig.IDPDisplayNameMapping,
|
|
config.OIDCConfig.UsernameMapping,
|
|
config.OIDCConfig.Scopes...,
|
|
),
|
|
)
|
|
err = r.eventstore.PushAggregate(ctx, addedConfig, orgAgg)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return writeModelToIDPConfig(&addedConfig.IDPConfigWriteModel), nil
|
|
}
|
|
|
|
func (r *CommandSide) ChangeIDPConfig(ctx context.Context, config *domain.IDPConfig) (*domain.IDPConfig, error) {
|
|
existingIDP, err := r.orgIDPConfigWriteModelByID(ctx, config.IDPConfigID, config.AggregateID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if existingIDP.State == domain.IDPConfigStateRemoved || existingIDP.State == domain.IDPConfigStateUnspecified {
|
|
return nil, caos_errs.ThrowNotFound(nil, "Org-4M9so", "Errors.Org.IDPConfig.NotExisting")
|
|
}
|
|
|
|
changedEvent, hasChanged := existingIDP.NewChangedEvent(ctx, config.IDPConfigID, config.Name, config.StylingType)
|
|
if !hasChanged {
|
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "Org-4M9vs", "Errors.Org.LabelPolicy.NotChanged")
|
|
}
|
|
orgAgg := OrgAggregateFromWriteModel(&existingIDP.WriteModel)
|
|
orgAgg.PushEvents(changedEvent)
|
|
|
|
err = r.eventstore.PushAggregate(ctx, existingIDP, orgAgg)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return writeModelToIDPConfig(&existingIDP.IDPConfigWriteModel), nil
|
|
}
|
|
|
|
func (r *CommandSide) DeactivateIDPConfig(ctx context.Context, idpID, orgID string) error {
|
|
existingIDP, err := r.orgIDPConfigWriteModelByID(ctx, idpID, orgID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if existingIDP.State != domain.IDPConfigStateActive {
|
|
return caos_errs.ThrowPreconditionFailed(nil, "Org-4M9so", "Errors.Org.IDPConfig.NotActive")
|
|
}
|
|
orgAgg := OrgAggregateFromWriteModel(&existingIDP.WriteModel)
|
|
orgAgg.PushEvents(org_repo.NewIDPConfigDeactivatedEvent(ctx, idpID))
|
|
|
|
return r.eventstore.PushAggregate(ctx, existingIDP, orgAgg)
|
|
}
|
|
|
|
func (r *CommandSide) ReactivateIDPConfig(ctx context.Context, idpID, orgID string) error {
|
|
existingIDP, err := r.orgIDPConfigWriteModelByID(ctx, idpID, orgID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if existingIDP.State != domain.IDPConfigStateInactive {
|
|
return caos_errs.ThrowPreconditionFailed(nil, "Org-5Mo0d", "Errors.Org.IDPConfig.NotInactive")
|
|
}
|
|
orgAgg := OrgAggregateFromWriteModel(&existingIDP.WriteModel)
|
|
orgAgg.PushEvents(org_repo.NewIDPConfigReactivatedEvent(ctx, idpID))
|
|
|
|
return r.eventstore.PushAggregate(ctx, existingIDP, orgAgg)
|
|
}
|
|
|
|
func (r *CommandSide) RemoveIDPConfig(ctx context.Context, idpID, orgID string) error {
|
|
_, err := r.pushIDPWriteModel(ctx, idpID, orgID, func(a *org.Aggregate, _ *OrgIDPConfigWriteModel) *org.Aggregate {
|
|
a.Aggregate = *a.PushEvents(org_repo.NewIDPConfigRemovedEvent(ctx, idpID))
|
|
return a
|
|
})
|
|
return err
|
|
}
|
|
|
|
func (r *CommandSide) pushIDPWriteModel(ctx context.Context, idpID, orgID string, eventSetter func(*org.Aggregate, *OrgIDPConfigWriteModel) *org.Aggregate) (*OrgIDPConfigWriteModel, error) {
|
|
writeModel := NewOrgIDPConfigWriteModel(idpID, orgID)
|
|
err := r.eventstore.FilterToQueryReducer(ctx, writeModel)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
aggregate := eventSetter(OrgAggregateFromWriteModel(&writeModel.WriteModel), writeModel)
|
|
err = r.eventstore.PushAggregate(ctx, writeModel, aggregate)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return writeModel, nil
|
|
}
|
|
|
|
func (r *CommandSide) orgIDPConfigWriteModelByID(ctx context.Context, idpID, orgID string) (policy *OrgIDPConfigWriteModel, err error) {
|
|
ctx, span := tracing.NewSpan(ctx)
|
|
defer func() { span.EndWithError(err) }()
|
|
|
|
writeModel := NewOrgIDPConfigWriteModel(idpID, orgID)
|
|
err = r.eventstore.FilterToQueryReducer(ctx, writeModel)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return writeModel, nil
|
|
}
|