Files
zitadel/internal/command/user_human_phone_model.go
T
6082e59d47 fix(eventstore): allow overwriting resource owner of events (#12261)
# 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]>
2026-06-15 11:24:37 +02:00

155 lines
4.2 KiB
Go

package command
import (
"context"
"time"
"github.com/zitadel/zitadel/internal/crypto"
"github.com/zitadel/zitadel/internal/domain"
"github.com/zitadel/zitadel/internal/eventstore"
"github.com/zitadel/zitadel/internal/repository/user"
)
type HumanPhoneWriteModel struct {
eventstore.WriteModel
Phone domain.PhoneNumber
IsPhoneVerified bool
Code *crypto.CryptoValue
CodeCreationDate time.Time
CodeExpiry time.Duration
GeneratorID string
VerificationID string
State domain.PhoneState
UserState domain.UserState
}
func NewHumanPhoneWriteModel(userID, resourceOwner string) *HumanPhoneWriteModel {
return &HumanPhoneWriteModel{
WriteModel: eventstore.WriteModel{
AggregateID: userID,
ResourceOwner: resourceOwner,
},
}
}
func (wm *HumanPhoneWriteModel) Reduce() error {
for _, event := range wm.Events {
switch e := event.(type) {
case *user.HumanAddedEvent:
wm.IsPhoneVerified = false
wm.Code = nil
wm.CodeCreationDate = time.Time{}
wm.CodeExpiry = 0
wm.GeneratorID = ""
wm.VerificationID = ""
wm.Phone = e.PhoneNumber
if wm.Phone != "" {
wm.State = domain.PhoneStateActive
} else {
wm.State = domain.PhoneStateUnspecified
}
wm.UserState = domain.UserStateActive
case *user.HumanRegisteredEvent:
wm.IsPhoneVerified = false
wm.Code = nil
wm.CodeCreationDate = time.Time{}
wm.CodeExpiry = 0
wm.GeneratorID = ""
wm.VerificationID = ""
wm.Phone = e.PhoneNumber
if wm.Phone != "" {
wm.State = domain.PhoneStateActive
} else {
wm.State = domain.PhoneStateUnspecified
}
wm.UserState = domain.UserStateActive
case *user.MachineAddedEvent:
wm.IsPhoneVerified = false
wm.Code = nil
wm.CodeCreationDate = time.Time{}
wm.CodeExpiry = 0
wm.GeneratorID = ""
wm.VerificationID = ""
wm.Phone = ""
wm.State = domain.PhoneStateUnspecified
wm.UserState = domain.UserStateUnspecified
case *user.HumanInitialCodeAddedEvent:
wm.UserState = domain.UserStateInitial
case *user.HumanInitializedCheckSucceededEvent:
wm.UserState = domain.UserStateActive
case *user.HumanPhoneChangedEvent:
wm.Phone = e.PhoneNumber
wm.IsPhoneVerified = false
wm.State = domain.PhoneStateActive
wm.Code = nil
case *user.HumanPhoneVerifiedEvent:
wm.IsPhoneVerified = true
wm.Code = nil
case *user.HumanPhoneCodeAddedEvent:
wm.Code = e.Code
wm.CodeCreationDate = e.CreationDate()
wm.CodeExpiry = e.Expiry
wm.GeneratorID = e.GeneratorID
case *user.HumanPhoneCodeSentEvent:
wm.GeneratorID = e.GeneratorInfo.GetID()
wm.VerificationID = e.GeneratorInfo.GetVerificationID()
case *user.HumanPhoneRemovedEvent:
wm.State = domain.PhoneStateRemoved
wm.IsPhoneVerified = false
wm.Phone = ""
case *user.UserRemovedEvent:
wm.IsPhoneVerified = false
wm.Code = nil
wm.CodeCreationDate = time.Time{}
wm.CodeExpiry = 0
wm.GeneratorID = ""
wm.VerificationID = ""
wm.UserState = domain.UserStateDeleted
wm.IsPhoneVerified = false
wm.Phone = ""
}
}
return wm.WriteModel.Reduce()
}
func (wm *HumanPhoneWriteModel) Query() *eventstore.SearchQueryBuilder {
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent).
ResourceOwner(wm.ResourceOwner).
AddQuery().
AggregateTypes(user.AggregateType).
AggregateIDs(wm.AggregateID).
EventTypes(user.HumanAddedType,
user.HumanRegisteredType,
user.HumanInitialCodeAddedType,
user.HumanInitializedCheckSucceededType,
user.HumanPhoneChangedType,
user.HumanPhoneVerifiedType,
user.HumanPhoneCodeAddedType,
user.HumanPhoneCodeSentType,
user.HumanPhoneRemovedType,
user.UserRemovedType,
user.UserV1AddedType,
user.UserV1RegisteredType,
user.UserV1InitialCodeAddedType,
user.UserV1InitializedCheckSucceededType,
user.UserV1PhoneCodeAddedType,
user.UserV1PhoneChangedType,
user.UserV1PhoneVerifiedType,
user.UserV1PhoneRemovedType).
Builder()
}
func (wm *HumanPhoneWriteModel) NewChangedEvent(
ctx context.Context,
aggregate *eventstore.Aggregate,
phone domain.PhoneNumber,
) (*user.HumanPhoneChangedEvent, bool) {
changedEvent := user.NewHumanPhoneChangedEvent(ctx, aggregate, phone)
return changedEvent, phone != wm.Phone
}