2021-01-07 15:06:45 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/user"
|
2021-01-07 15:06:45 +00:00
|
|
|
)
|
|
|
|
|
2021-11-02 09:08:47 +00:00
|
|
|
type UserIDPLinkWriteModel struct {
|
2021-01-07 15:06:45 +00:00
|
|
|
eventstore.WriteModel
|
|
|
|
|
|
|
|
IDPConfigID string
|
|
|
|
ExternalUserID string
|
|
|
|
DisplayName string
|
|
|
|
|
2021-11-02 09:08:47 +00:00
|
|
|
State domain.UserIDPLinkState
|
2021-01-07 15:06:45 +00:00
|
|
|
}
|
|
|
|
|
2021-11-02 09:08:47 +00:00
|
|
|
func NewUserIDPLinkWriteModel(userID, idpConfigID, externalUserID, resourceOwner string) *UserIDPLinkWriteModel {
|
|
|
|
return &UserIDPLinkWriteModel{
|
2021-01-07 15:06:45 +00:00
|
|
|
WriteModel: eventstore.WriteModel{
|
2021-01-12 11:59:51 +00:00
|
|
|
AggregateID: userID,
|
|
|
|
ResourceOwner: resourceOwner,
|
2021-01-07 15:06:45 +00:00
|
|
|
},
|
|
|
|
IDPConfigID: idpConfigID,
|
|
|
|
ExternalUserID: externalUserID,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-03 08:19:07 +00:00
|
|
|
func (wm *UserIDPLinkWriteModel) AppendEvents(events ...eventstore.Event) {
|
2021-08-13 12:27:24 +00:00
|
|
|
for _, event := range events {
|
|
|
|
switch e := event.(type) {
|
2021-11-02 09:08:47 +00:00
|
|
|
case *user.UserIDPLinkAddedEvent:
|
2023-06-21 06:30:39 +00:00
|
|
|
if e.IDPConfigID != wm.IDPConfigID || e.ExternalUserID != wm.ExternalUserID {
|
2021-08-13 12:27:24 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.WriteModel.AppendEvents(e)
|
2023-08-04 09:35:36 +00:00
|
|
|
case *user.UserIDPExternalIDMigratedEvent:
|
|
|
|
if e.IDPConfigID != wm.IDPConfigID || e.PreviousID != wm.ExternalUserID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.WriteModel.AppendEvents(e)
|
2021-11-02 09:08:47 +00:00
|
|
|
case *user.UserIDPLinkRemovedEvent:
|
2023-06-21 06:30:39 +00:00
|
|
|
if e.IDPConfigID != wm.IDPConfigID || e.ExternalUserID != wm.ExternalUserID {
|
2021-08-13 12:27:24 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.WriteModel.AppendEvents(e)
|
2021-11-02 09:08:47 +00:00
|
|
|
case *user.UserIDPLinkCascadeRemovedEvent:
|
2023-06-21 06:30:39 +00:00
|
|
|
if e.IDPConfigID != wm.IDPConfigID || e.ExternalUserID != wm.ExternalUserID {
|
2021-08-13 12:27:24 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.WriteModel.AppendEvents(e)
|
|
|
|
case *user.UserRemovedEvent:
|
|
|
|
wm.WriteModel.AppendEvents(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-02 09:08:47 +00:00
|
|
|
func (wm *UserIDPLinkWriteModel) Reduce() error {
|
2021-01-07 15:06:45 +00:00
|
|
|
for _, event := range wm.Events {
|
|
|
|
switch e := event.(type) {
|
2021-11-02 09:08:47 +00:00
|
|
|
case *user.UserIDPLinkAddedEvent:
|
2021-01-07 15:06:45 +00:00
|
|
|
wm.IDPConfigID = e.IDPConfigID
|
|
|
|
wm.DisplayName = e.DisplayName
|
2021-02-15 12:31:24 +00:00
|
|
|
wm.ExternalUserID = e.ExternalUserID
|
2021-11-02 09:08:47 +00:00
|
|
|
wm.State = domain.UserIDPLinkStateActive
|
2023-08-04 09:35:36 +00:00
|
|
|
case *user.UserIDPExternalIDMigratedEvent:
|
|
|
|
wm.ExternalUserID = e.NewID
|
2021-11-02 09:08:47 +00:00
|
|
|
case *user.UserIDPLinkRemovedEvent:
|
|
|
|
wm.State = domain.UserIDPLinkStateRemoved
|
|
|
|
case *user.UserIDPLinkCascadeRemovedEvent:
|
|
|
|
wm.State = domain.UserIDPLinkStateRemoved
|
2021-01-07 15:06:45 +00:00
|
|
|
case *user.UserRemovedEvent:
|
2021-11-02 09:08:47 +00:00
|
|
|
wm.State = domain.UserIDPLinkStateRemoved
|
2021-01-07 15:06:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return wm.WriteModel.Reduce()
|
|
|
|
}
|
|
|
|
|
2021-11-02 09:08:47 +00:00
|
|
|
func (wm *UserIDPLinkWriteModel) Query() *eventstore.SearchQueryBuilder {
|
2021-07-06 11:55:57 +00:00
|
|
|
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent).
|
2021-02-18 13:48:27 +00:00
|
|
|
ResourceOwner(wm.ResourceOwner).
|
2021-07-06 11:55:57 +00:00
|
|
|
AddQuery().
|
|
|
|
AggregateTypes(user.AggregateType).
|
|
|
|
AggregateIDs(wm.AggregateID).
|
2021-11-02 09:08:47 +00:00
|
|
|
EventTypes(user.UserIDPLinkAddedType,
|
2023-08-04 09:35:36 +00:00
|
|
|
user.UserIDPExternalIDMigratedType,
|
2021-11-02 09:08:47 +00:00
|
|
|
user.UserIDPLinkRemovedType,
|
|
|
|
user.UserIDPLinkCascadeRemovedType,
|
2021-07-06 11:55:57 +00:00
|
|
|
user.UserRemovedType).
|
|
|
|
Builder()
|
2021-01-07 15:06:45 +00:00
|
|
|
}
|