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