mirror of
https://github.com/zitadel/zitadel.git
synced 2025-05-06 09:16:49 +00:00
fix: unique constraints on instance domain events (#3635)
This commit is contained in:
parent
48fbf1a28e
commit
d401439427
@ -133,7 +133,7 @@ func TestCommandSide_AddInstanceDomain(t *testing.T) {
|
|||||||
newOIDCAppChangedEventInstanceDomain(context.Background(), "consoleApplicationID", "projectID", "org1"),
|
newOIDCAppChangedEventInstanceDomain(context.Background(), "consoleApplicationID", "projectID", "org1"),
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
uniqueConstraintsFromEventConstraintWithInstanceID("INSTANCE", instance.NewAddInstanceDomainUniqueConstraint("domain.ch")),
|
uniqueConstraintsFromEventConstraint(instance.NewAddInstanceDomainUniqueConstraint("domain.ch")),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
externalSecure: true,
|
externalSecure: true,
|
||||||
@ -348,7 +348,7 @@ func TestCommandSide_RemoveInstanceDomain(t *testing.T) {
|
|||||||
"domain.ch",
|
"domain.ch",
|
||||||
)),
|
)),
|
||||||
},
|
},
|
||||||
uniqueConstraintsFromEventConstraintWithInstanceID("INSTANCE", instance.NewRemoveInstanceDomainUniqueConstraint("domain.ch")),
|
uniqueConstraintsFromEventConstraint(instance.NewRemoveInstanceDomainUniqueConstraint("domain.ch")),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
|
@ -104,10 +104,14 @@ func commandsToRepository(instanceID string, cmds []Command) (events []*reposito
|
|||||||
func uniqueConstraintsToRepository(instanceID string, constraints []*EventUniqueConstraint) (uniqueConstraints []*repository.UniqueConstraint) {
|
func uniqueConstraintsToRepository(instanceID string, constraints []*EventUniqueConstraint) (uniqueConstraints []*repository.UniqueConstraint) {
|
||||||
uniqueConstraints = make([]*repository.UniqueConstraint, len(constraints))
|
uniqueConstraints = make([]*repository.UniqueConstraint, len(constraints))
|
||||||
for i, constraint := range constraints {
|
for i, constraint := range constraints {
|
||||||
|
var id string
|
||||||
|
if !constraint.IsGlobal {
|
||||||
|
id = instanceID
|
||||||
|
}
|
||||||
uniqueConstraints[i] = &repository.UniqueConstraint{
|
uniqueConstraints[i] = &repository.UniqueConstraint{
|
||||||
UniqueType: constraint.UniqueType,
|
UniqueType: constraint.UniqueType,
|
||||||
UniqueField: constraint.UniqueField,
|
UniqueField: constraint.UniqueField,
|
||||||
InstanceID: instanceID,
|
InstanceID: id,
|
||||||
Action: uniqueConstraintActionToRepository(constraint.Action),
|
Action: uniqueConstraintActionToRepository(constraint.Action),
|
||||||
ErrorMessage: constraint.ErrorMessage,
|
ErrorMessage: constraint.ErrorMessage,
|
||||||
}
|
}
|
||||||
|
@ -9,6 +9,8 @@ type EventUniqueConstraint struct {
|
|||||||
Action UniqueConstraintAction
|
Action UniqueConstraintAction
|
||||||
//ErrorMessage defines the translation file key for the error message
|
//ErrorMessage defines the translation file key for the error message
|
||||||
ErrorMessage string
|
ErrorMessage string
|
||||||
|
//IsGlobal defines if the unique constraint is globally unique or just within a single instance
|
||||||
|
IsGlobal bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type UniqueConstraintAction int32
|
type UniqueConstraintAction int32
|
||||||
@ -39,3 +41,27 @@ func NewRemoveEventUniqueConstraint(
|
|||||||
Action: UniqueConstraintRemove,
|
Action: UniqueConstraintRemove,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NewAddGlobalEventUniqueConstraint(
|
||||||
|
uniqueType,
|
||||||
|
uniqueField,
|
||||||
|
errMessage string) *EventUniqueConstraint {
|
||||||
|
return &EventUniqueConstraint{
|
||||||
|
UniqueType: uniqueType,
|
||||||
|
UniqueField: uniqueField,
|
||||||
|
ErrorMessage: errMessage,
|
||||||
|
IsGlobal: true,
|
||||||
|
Action: UniqueConstraintAdd,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewRemoveGlobalEventUniqueConstraint(
|
||||||
|
uniqueType,
|
||||||
|
uniqueField string) *EventUniqueConstraint {
|
||||||
|
return &EventUniqueConstraint{
|
||||||
|
UniqueType: uniqueType,
|
||||||
|
UniqueField: uniqueField,
|
||||||
|
IsGlobal: true,
|
||||||
|
Action: UniqueConstraintRemove,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -73,15 +73,15 @@ func (s *SetupStep) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
|||||||
switch s.Type() {
|
switch s.Type() {
|
||||||
case startedType:
|
case startedType:
|
||||||
return []*eventstore.EventUniqueConstraint{
|
return []*eventstore.EventUniqueConstraint{
|
||||||
eventstore.NewAddEventUniqueConstraint("migration_started", s.migration.String(), "Errors.Step.Started.AlreadyExists"),
|
eventstore.NewAddGlobalEventUniqueConstraint("migration_started", s.migration.String(), "Errors.Step.Started.AlreadyExists"),
|
||||||
}
|
}
|
||||||
case failedType:
|
case failedType:
|
||||||
return []*eventstore.EventUniqueConstraint{
|
return []*eventstore.EventUniqueConstraint{
|
||||||
eventstore.NewRemoveEventUniqueConstraint("migration_started", s.migration.String()),
|
eventstore.NewRemoveGlobalEventUniqueConstraint("migration_started", s.migration.String()),
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
return []*eventstore.EventUniqueConstraint{
|
return []*eventstore.EventUniqueConstraint{
|
||||||
eventstore.NewAddEventUniqueConstraint("migration_done", s.migration.String(), "Errors.Step.Done.AlreadyExists"),
|
eventstore.NewAddGlobalEventUniqueConstraint("migration_done", s.migration.String(), "Errors.Step.Done.AlreadyExists"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,14 +19,14 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func NewAddInstanceDomainUniqueConstraint(orgDomain string) *eventstore.EventUniqueConstraint {
|
func NewAddInstanceDomainUniqueConstraint(orgDomain string) *eventstore.EventUniqueConstraint {
|
||||||
return eventstore.NewAddEventUniqueConstraint(
|
return eventstore.NewAddGlobalEventUniqueConstraint(
|
||||||
UniqueInstanceDomain,
|
UniqueInstanceDomain,
|
||||||
orgDomain,
|
orgDomain,
|
||||||
"Errors.Instance.Domain.AlreadyExists")
|
"Errors.Instance.Domain.AlreadyExists")
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRemoveInstanceDomainUniqueConstraint(orgDomain string) *eventstore.EventUniqueConstraint {
|
func NewRemoveInstanceDomainUniqueConstraint(orgDomain string) *eventstore.EventUniqueConstraint {
|
||||||
return eventstore.NewRemoveEventUniqueConstraint(
|
return eventstore.NewRemoveGlobalEventUniqueConstraint(
|
||||||
UniqueInstanceDomain,
|
UniqueInstanceDomain,
|
||||||
orgDomain)
|
orgDomain)
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,6 @@ func RegisterEventMappers(es *eventstore.Eventstore) {
|
|||||||
RegisterFilterEventMapper(SMTPConfigAddedEventType, SMTPConfigAddedEventMapper).
|
RegisterFilterEventMapper(SMTPConfigAddedEventType, SMTPConfigAddedEventMapper).
|
||||||
RegisterFilterEventMapper(SMTPConfigChangedEventType, SMTPConfigChangedEventMapper).
|
RegisterFilterEventMapper(SMTPConfigChangedEventType, SMTPConfigChangedEventMapper).
|
||||||
RegisterFilterEventMapper(SMTPConfigPasswordChangedEventType, SMTPConfigPasswordChangedEventMapper).
|
RegisterFilterEventMapper(SMTPConfigPasswordChangedEventType, SMTPConfigPasswordChangedEventMapper).
|
||||||
RegisterFilterEventMapper(UniqueConstraintsMigratedEventType, MigrateUniqueConstraintEventMapper).
|
|
||||||
RegisterFilterEventMapper(SMSConfigTwilioAddedEventType, SMSConfigTwilioAddedEventMapper).
|
RegisterFilterEventMapper(SMSConfigTwilioAddedEventType, SMSConfigTwilioAddedEventMapper).
|
||||||
RegisterFilterEventMapper(SMSConfigTwilioChangedEventType, SMSConfigTwilioChangedEventMapper).
|
RegisterFilterEventMapper(SMSConfigTwilioChangedEventType, SMSConfigTwilioChangedEventMapper).
|
||||||
RegisterFilterEventMapper(SMSConfigTwilioTokenChangedEventType, SMSConfigTwilioTokenChangedEventMapper).
|
RegisterFilterEventMapper(SMSConfigTwilioTokenChangedEventType, SMSConfigTwilioTokenChangedEventMapper).
|
||||||
|
@ -1,59 +0,0 @@
|
|||||||
package instance
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
|
|
||||||
"github.com/zitadel/zitadel/internal/domain"
|
|
||||||
"github.com/zitadel/zitadel/internal/eventstore"
|
|
||||||
|
|
||||||
"github.com/zitadel/zitadel/internal/eventstore/repository"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
UniqueConstraintsMigratedEventType eventstore.EventType = "iam.unique.constraints.migrated"
|
|
||||||
)
|
|
||||||
|
|
||||||
type MigrateUniqueConstraintEvent struct {
|
|
||||||
eventstore.BaseEvent `json:"-"`
|
|
||||||
|
|
||||||
uniqueConstraintMigrations []*domain.UniqueConstraintMigration `json:"-"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewAddMigrateUniqueConstraint(uniqueMigration *domain.UniqueConstraintMigration) *eventstore.EventUniqueConstraint {
|
|
||||||
return eventstore.NewAddEventUniqueConstraint(
|
|
||||||
uniqueMigration.UniqueType,
|
|
||||||
uniqueMigration.UniqueField,
|
|
||||||
uniqueMigration.ErrorMessage)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *MigrateUniqueConstraintEvent) Data() interface{} {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *MigrateUniqueConstraintEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
|
||||||
constraints := make([]*eventstore.EventUniqueConstraint, len(e.uniqueConstraintMigrations))
|
|
||||||
for i, uniqueMigration := range e.uniqueConstraintMigrations {
|
|
||||||
constraints[i] = NewAddMigrateUniqueConstraint(uniqueMigration)
|
|
||||||
}
|
|
||||||
return constraints
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewMigrateUniqueConstraintEvent(
|
|
||||||
ctx context.Context,
|
|
||||||
aggregate *eventstore.Aggregate,
|
|
||||||
uniqueConstraintMigrations []*domain.UniqueConstraintMigration) *MigrateUniqueConstraintEvent {
|
|
||||||
return &MigrateUniqueConstraintEvent{
|
|
||||||
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
||||||
ctx,
|
|
||||||
aggregate,
|
|
||||||
UniqueConstraintsMigratedEventType,
|
|
||||||
),
|
|
||||||
uniqueConstraintMigrations: uniqueConstraintMigrations,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func MigrateUniqueConstraintEventMapper(event *repository.Event) (eventstore.Event, error) {
|
|
||||||
return &MigrateUniqueConstraintEvent{
|
|
||||||
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
||||||
}, nil
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user