mirror of
https://github.com/andwati/zitadel.git
synced 2026-10-18 13:52:51 +00:00
# Which Problems Are Solved - The eventstore did not support intentionally overwriting the resource owner when creating events for aggregates that may be reused across owners. - Resource owner handling was implicit and could not be controlled per command/event type. - We needed a safe way to distinguish between: - keeping the existing aggregate owner, and - explicitly setting a new owner for specific create-like events. # How the Problems Are Solved - Introduced a new eventstore command type with an explicit enforce_owner flag. - Updated eventstore.commands_to_events and eventstore.push so owner assignment is now explicit: - if enforce_owner is true, the command owner is written - if enforce_owner is false, the existing aggregate owner is retained when present - Added EnforceResourceOwnerCommand and wiring so command types can opt in to enforced owner behavior. - Wired the new behavior through the v3 eventstore push path, including compatibility fallback for older command type mapping. - Added migration/setup changes to register and use the new command type and SQL functions. - Added and updated tests for owner overwrite and aggregate ID reuse scenarios. # Additional Changes - Added small migration/setup robustness improvements related to eventstore setup ordering and helper reuse. - Added focused test coverage for enforced owner behavior and sequencing. - Events that currently allow owner changes (implement EnforceResourceOwner) are: - AddedEvent (action) - GroupAddedEvent - StartedEvent (idp intent) - ProjectAddedEvent - HumanAddedEvent - HumanRegisteredEvent - MachineAddedEvent - CreatedEvent (schema user) # Additional Context - Follow-up for eventstore owner-handling correctness in create flows and aggregate ID reuse cases. - No additional issue link was attached for this change. --------- Co-authored-by: abhishek kumar gupta <[email protected]>
137 lines
3.3 KiB
Go
137 lines
3.3 KiB
Go
package command
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
"github.com/zitadel/zitadel/internal/repository/user"
|
|
)
|
|
|
|
type HumanAddressWriteModel struct {
|
|
eventstore.WriteModel
|
|
|
|
Country string
|
|
Locality string
|
|
PostalCode string
|
|
Region string
|
|
StreetAddress string
|
|
|
|
State domain.AddressState
|
|
}
|
|
|
|
func NewHumanAddressWriteModel(userID, resourceOwner string) *HumanAddressWriteModel {
|
|
return &HumanAddressWriteModel{
|
|
WriteModel: eventstore.WriteModel{
|
|
AggregateID: userID,
|
|
ResourceOwner: resourceOwner,
|
|
},
|
|
}
|
|
}
|
|
|
|
func (wm *HumanAddressWriteModel) Reduce() error {
|
|
for _, event := range wm.Events {
|
|
switch e := event.(type) {
|
|
case *user.HumanAddedEvent:
|
|
wm.Country = e.Country
|
|
wm.Locality = e.Locality
|
|
wm.PostalCode = e.PostalCode
|
|
wm.Region = e.Region
|
|
wm.StreetAddress = e.StreetAddress
|
|
wm.State = domain.AddressStateActive
|
|
case *user.HumanRegisteredEvent:
|
|
wm.Country = e.Country
|
|
wm.Locality = e.Locality
|
|
wm.PostalCode = e.PostalCode
|
|
wm.Region = e.Region
|
|
wm.StreetAddress = e.StreetAddress
|
|
wm.State = domain.AddressStateActive
|
|
case *user.HumanAddressChangedEvent:
|
|
if e.Country != nil {
|
|
wm.Country = *e.Country
|
|
}
|
|
if e.Locality != nil {
|
|
wm.Locality = *e.Locality
|
|
}
|
|
if e.PostalCode != nil {
|
|
wm.PostalCode = *e.PostalCode
|
|
}
|
|
if e.Region != nil {
|
|
wm.Region = *e.Region
|
|
}
|
|
if e.StreetAddress != nil {
|
|
wm.StreetAddress = *e.StreetAddress
|
|
}
|
|
case *user.UserRemovedEvent:
|
|
wm.Country = ""
|
|
wm.Locality = ""
|
|
wm.PostalCode = ""
|
|
wm.Region = ""
|
|
wm.StreetAddress = ""
|
|
wm.State = domain.AddressStateRemoved
|
|
case *user.MachineAddedEvent:
|
|
wm.Country = ""
|
|
wm.Locality = ""
|
|
wm.PostalCode = ""
|
|
wm.Region = ""
|
|
wm.StreetAddress = ""
|
|
wm.State = domain.AddressStateUnspecified
|
|
}
|
|
}
|
|
return wm.WriteModel.Reduce()
|
|
}
|
|
|
|
func (wm *HumanAddressWriteModel) Query() *eventstore.SearchQueryBuilder {
|
|
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent).
|
|
ResourceOwner(wm.ResourceOwner).
|
|
AddQuery().
|
|
AggregateTypes(user.AggregateType).
|
|
AggregateIDs(wm.AggregateID).
|
|
EventTypes(user.UserV1AddedType,
|
|
user.UserV1RegisteredType,
|
|
user.UserV1AddressChangedType,
|
|
user.HumanAddedType,
|
|
user.HumanRegisteredType,
|
|
user.MachineAddedEventType,
|
|
user.HumanAddressChangedType,
|
|
user.UserRemovedType).
|
|
Builder()
|
|
}
|
|
|
|
func (wm *HumanAddressWriteModel) NewChangedEvent(
|
|
ctx context.Context,
|
|
aggregate *eventstore.Aggregate,
|
|
country,
|
|
locality,
|
|
postalCode,
|
|
region,
|
|
streetAddress string,
|
|
) (*user.HumanAddressChangedEvent, bool, error) {
|
|
changes := make([]user.AddressChanges, 0)
|
|
var err error
|
|
|
|
if wm.Country != country {
|
|
changes = append(changes, user.ChangeCountry(country))
|
|
}
|
|
if wm.Locality != locality {
|
|
changes = append(changes, user.ChangeLocality(locality))
|
|
}
|
|
if wm.PostalCode != postalCode {
|
|
changes = append(changes, user.ChangePostalCode(postalCode))
|
|
}
|
|
if wm.Region != region {
|
|
changes = append(changes, user.ChangeRegion(region))
|
|
}
|
|
if wm.StreetAddress != streetAddress {
|
|
changes = append(changes, user.ChangeStreetAddress(streetAddress))
|
|
}
|
|
if len(changes) == 0 {
|
|
return nil, false, nil
|
|
}
|
|
changeEvent, err := user.NewAddressChangedEvent(ctx, aggregate, changes)
|
|
if err != nil {
|
|
return nil, false, err
|
|
}
|
|
return changeEvent, true, nil
|
|
}
|