mirror of
https://github.com/zitadel/zitadel.git
synced 2024-12-12 19:14:23 +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>
123 lines
3.4 KiB
Go
123 lines
3.4 KiB
Go
package command
|
|
|
|
import (
|
|
"context"
|
|
"reflect"
|
|
|
|
"github.com/caos/zitadel/internal/crypto"
|
|
"github.com/caos/zitadel/internal/eventstore/v2"
|
|
"github.com/caos/zitadel/internal/v2/domain"
|
|
"github.com/caos/zitadel/internal/v2/repository/idpconfig"
|
|
"github.com/caos/zitadel/internal/v2/repository/org"
|
|
)
|
|
|
|
type IDPOIDCConfigWriteModel struct {
|
|
OIDCConfigWriteModel
|
|
}
|
|
|
|
func NewOrgIDPOIDCConfigWriteModel(idpConfigID, orgID string) *IDPOIDCConfigWriteModel {
|
|
return &IDPOIDCConfigWriteModel{
|
|
OIDCConfigWriteModel{
|
|
WriteModel: eventstore.WriteModel{
|
|
AggregateID: orgID,
|
|
ResourceOwner: orgID,
|
|
},
|
|
IDPConfigID: idpConfigID,
|
|
},
|
|
}
|
|
}
|
|
|
|
func (wm *IDPOIDCConfigWriteModel) AppendEvents(events ...eventstore.EventReader) {
|
|
for _, event := range events {
|
|
switch e := event.(type) {
|
|
case *org.IDPOIDCConfigAddedEvent:
|
|
if wm.IDPConfigID != e.IDPConfigID {
|
|
continue
|
|
}
|
|
wm.OIDCConfigWriteModel.AppendEvents(&e.OIDCConfigAddedEvent)
|
|
case *org.IDPOIDCConfigChangedEvent:
|
|
if wm.IDPConfigID != e.IDPConfigID {
|
|
continue
|
|
}
|
|
wm.OIDCConfigWriteModel.AppendEvents(&e.OIDCConfigChangedEvent)
|
|
case *org.IDPConfigReactivatedEvent:
|
|
if wm.IDPConfigID != e.ConfigID {
|
|
continue
|
|
}
|
|
wm.OIDCConfigWriteModel.AppendEvents(&e.IDPConfigReactivatedEvent)
|
|
case *org.IDPConfigDeactivatedEvent:
|
|
if wm.IDPConfigID != e.ConfigID {
|
|
continue
|
|
}
|
|
wm.OIDCConfigWriteModel.AppendEvents(&e.IDPConfigDeactivatedEvent)
|
|
case *org.IDPConfigRemovedEvent:
|
|
if wm.IDPConfigID != e.ConfigID {
|
|
continue
|
|
}
|
|
wm.OIDCConfigWriteModel.AppendEvents(&e.IDPConfigRemovedEvent)
|
|
default:
|
|
wm.OIDCConfigWriteModel.AppendEvents(e)
|
|
}
|
|
}
|
|
}
|
|
|
|
func (wm *IDPOIDCConfigWriteModel) Reduce() error {
|
|
if err := wm.OIDCConfigWriteModel.Reduce(); err != nil {
|
|
return err
|
|
}
|
|
return wm.WriteModel.Reduce()
|
|
}
|
|
|
|
func (wm *IDPOIDCConfigWriteModel) Query() *eventstore.SearchQueryBuilder {
|
|
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent, org.AggregateType).
|
|
AggregateIDs(wm.AggregateID).
|
|
ResourceOwner(wm.ResourceOwner)
|
|
}
|
|
|
|
func (wm *IDPOIDCConfigWriteModel) NewChangedEvent(
|
|
ctx context.Context,
|
|
idpConfigID,
|
|
clientID,
|
|
issuer,
|
|
clientSecretString string,
|
|
secretCrypto crypto.Crypto,
|
|
idpDisplayNameMapping,
|
|
userNameMapping domain.OIDCMappingField,
|
|
scopes ...string,
|
|
) (*org.IDPOIDCConfigChangedEvent, bool, error) {
|
|
|
|
changes := make([]idpconfig.OIDCConfigChanges, 0)
|
|
var clientSecret *crypto.CryptoValue
|
|
var err error
|
|
if clientSecretString != "" {
|
|
clientSecret, err = crypto.Crypt([]byte(clientSecretString), secretCrypto)
|
|
if err != nil {
|
|
return nil, false, err
|
|
}
|
|
changes = append(changes, idpconfig.ChangeClientSecret(clientSecret))
|
|
}
|
|
if wm.ClientID != clientID {
|
|
changes = append(changes, idpconfig.ChangeClientID(clientID))
|
|
}
|
|
if wm.Issuer != issuer {
|
|
changes = append(changes, idpconfig.ChangeIssuer(issuer))
|
|
}
|
|
if idpDisplayNameMapping.Valid() && wm.IDPDisplayNameMapping != idpDisplayNameMapping {
|
|
changes = append(changes, idpconfig.ChangeIDPDisplayNameMapping(idpDisplayNameMapping))
|
|
}
|
|
if userNameMapping.Valid() && wm.UserNameMapping != userNameMapping {
|
|
changes = append(changes, idpconfig.ChangeUserNameMapping(userNameMapping))
|
|
}
|
|
if reflect.DeepEqual(wm.Scopes, scopes) {
|
|
changes = append(changes, idpconfig.ChangeScopes(scopes))
|
|
}
|
|
if len(changes) == 0 {
|
|
return nil, false, nil
|
|
}
|
|
changeEvent, err := org.NewIDPOIDCConfigChangedEvent(ctx, idpConfigID, changes)
|
|
if err != nil {
|
|
return nil, false, err
|
|
}
|
|
return changeEvent, true, nil
|
|
}
|