2020-11-11 16:51:44 +00:00
|
|
|
package iam
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/caos/zitadel/internal/eventstore/v2"
|
2020-11-30 05:40:56 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore/v2/repository"
|
2020-11-11 16:51:44 +00:00
|
|
|
"github.com/caos/zitadel/internal/v2/repository/member"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-12-11 14:49:19 +00:00
|
|
|
MemberAddedEventType = IamEventTypePrefix + member.AddedEventType
|
|
|
|
MemberChangedEventType = IamEventTypePrefix + member.ChangedEventType
|
|
|
|
MemberRemovedEventType = IamEventTypePrefix + member.RemovedEventType
|
2020-11-11 16:51:44 +00:00
|
|
|
)
|
|
|
|
|
2020-11-18 20:22:15 +00:00
|
|
|
type MemberReadModel struct {
|
|
|
|
member.ReadModel
|
2020-11-30 07:35:40 +00:00
|
|
|
|
|
|
|
userID string
|
|
|
|
iamID string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewMemberReadModel(iamID, userID string) *MemberReadModel {
|
|
|
|
return &MemberReadModel{
|
|
|
|
iamID: iamID,
|
|
|
|
userID: userID,
|
|
|
|
}
|
2020-11-18 20:22:15 +00:00
|
|
|
}
|
2020-11-11 16:51:44 +00:00
|
|
|
|
2020-11-25 13:12:44 +00:00
|
|
|
func (rm *MemberReadModel) AppendEvents(events ...eventstore.EventReader) {
|
2020-11-11 16:51:44 +00:00
|
|
|
for _, event := range events {
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *MemberAddedEvent:
|
|
|
|
rm.ReadModel.AppendEvents(&e.AddedEvent)
|
|
|
|
case *MemberChangedEvent:
|
|
|
|
rm.ReadModel.AppendEvents(&e.ChangedEvent)
|
2020-11-12 21:50:01 +00:00
|
|
|
case *member.AddedEvent, *member.ChangedEvent, *MemberRemovedEvent:
|
|
|
|
rm.ReadModel.AppendEvents(e)
|
2020-11-11 16:51:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-01 13:44:19 +00:00
|
|
|
func (rm *MemberReadModel) Query() *eventstore.SearchQueryBuilder {
|
|
|
|
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent, AggregateType).
|
2020-11-30 07:35:40 +00:00
|
|
|
AggregateIDs(rm.iamID).
|
|
|
|
EventData(map[string]interface{}{
|
|
|
|
"userId": rm.userID,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-11-23 10:36:58 +00:00
|
|
|
type MemberWriteModel struct {
|
2020-12-14 16:24:01 +00:00
|
|
|
member.WriteModel
|
2020-11-23 10:36:58 +00:00
|
|
|
}
|
|
|
|
|
2020-11-30 06:56:38 +00:00
|
|
|
func NewMemberWriteModel(iamID, userID string) *MemberWriteModel {
|
2020-11-23 10:36:58 +00:00
|
|
|
return &MemberWriteModel{
|
2020-12-14 16:24:01 +00:00
|
|
|
member.WriteModel{
|
|
|
|
WriteModel: eventstore.WriteModel{
|
|
|
|
AggregateID: iamID,
|
|
|
|
},
|
|
|
|
UserID: userID,
|
|
|
|
},
|
2020-11-23 10:36:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-25 13:12:44 +00:00
|
|
|
func (wm *MemberWriteModel) AppendEvents(events ...eventstore.EventReader) {
|
2020-11-23 10:36:58 +00:00
|
|
|
for _, event := range events {
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *MemberAddedEvent:
|
2020-12-14 16:24:01 +00:00
|
|
|
if e.UserID != wm.WriteModel.UserID {
|
2020-11-30 05:40:56 +00:00
|
|
|
continue
|
|
|
|
}
|
2020-12-14 16:24:01 +00:00
|
|
|
wm.WriteModel.AppendEvents(&e.AddedEvent)
|
2020-11-23 10:36:58 +00:00
|
|
|
case *MemberChangedEvent:
|
2020-12-14 16:24:01 +00:00
|
|
|
if e.UserID != wm.WriteModel.UserID {
|
2020-11-30 05:40:56 +00:00
|
|
|
continue
|
|
|
|
}
|
2020-12-14 16:24:01 +00:00
|
|
|
wm.WriteModel.AppendEvents(&e.ChangedEvent)
|
2020-11-23 10:36:58 +00:00
|
|
|
case *MemberRemovedEvent:
|
2020-12-14 16:24:01 +00:00
|
|
|
if e.UserID != wm.WriteModel.UserID {
|
2020-11-30 05:40:56 +00:00
|
|
|
continue
|
|
|
|
}
|
2020-12-14 16:24:01 +00:00
|
|
|
wm.WriteModel.AppendEvents(&e.RemovedEvent)
|
2020-11-23 10:36:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-30 06:56:38 +00:00
|
|
|
func (wm *MemberWriteModel) Reduce() error {
|
|
|
|
return wm.WriteModel.Reduce()
|
|
|
|
}
|
|
|
|
|
2020-12-01 13:44:19 +00:00
|
|
|
func (wm *MemberWriteModel) Query() *eventstore.SearchQueryBuilder {
|
|
|
|
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent, AggregateType).
|
2020-12-14 16:24:01 +00:00
|
|
|
AggregateIDs(wm.WriteModel.AggregateID)
|
2020-11-30 05:40:56 +00:00
|
|
|
}
|
|
|
|
|
2020-11-11 16:51:44 +00:00
|
|
|
type MemberAddedEvent struct {
|
|
|
|
member.AddedEvent
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewMemberAddedEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
userID string,
|
|
|
|
roles ...string,
|
|
|
|
) *MemberAddedEvent {
|
|
|
|
|
|
|
|
return &MemberAddedEvent{
|
2020-11-12 21:50:01 +00:00
|
|
|
AddedEvent: *member.NewAddedEvent(
|
2020-11-11 16:51:44 +00:00
|
|
|
eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
|
|
|
MemberAddedEventType,
|
|
|
|
),
|
|
|
|
userID,
|
|
|
|
roles...,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-30 05:40:56 +00:00
|
|
|
func MemberAddedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
|
|
|
|
e, err := member.AddedEventMapper(event)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &MemberAddedEvent{AddedEvent: *e.(*member.AddedEvent)}, nil
|
|
|
|
}
|
|
|
|
|
2020-11-25 13:12:44 +00:00
|
|
|
type MemberChangedEvent struct {
|
|
|
|
member.ChangedEvent
|
|
|
|
}
|
|
|
|
|
2020-11-23 18:31:12 +00:00
|
|
|
func MemberChangedEventFromExisting(
|
2020-11-11 16:51:44 +00:00
|
|
|
ctx context.Context,
|
2020-11-23 18:31:12 +00:00
|
|
|
current *MemberWriteModel,
|
|
|
|
roles ...string,
|
2020-11-18 20:22:15 +00:00
|
|
|
) (*MemberChangedEvent, error) {
|
2020-11-11 16:51:44 +00:00
|
|
|
|
2020-11-25 19:04:32 +00:00
|
|
|
event, err := member.ChangeEventFromExisting(
|
2020-11-18 20:22:15 +00:00
|
|
|
eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
|
|
|
MemberChangedEventType,
|
2020-11-11 16:51:44 +00:00
|
|
|
),
|
2020-12-14 16:24:01 +00:00
|
|
|
¤t.WriteModel,
|
2020-11-23 18:31:12 +00:00
|
|
|
roles...,
|
2020-11-18 20:22:15 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-11-11 16:51:44 +00:00
|
|
|
}
|
2020-11-18 20:22:15 +00:00
|
|
|
|
|
|
|
return &MemberChangedEvent{
|
2020-11-25 19:04:32 +00:00
|
|
|
ChangedEvent: *event,
|
2020-11-18 20:22:15 +00:00
|
|
|
}, nil
|
2020-11-11 16:51:44 +00:00
|
|
|
}
|
|
|
|
|
2020-11-30 05:40:56 +00:00
|
|
|
func MemberChangedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
|
|
|
|
e, err := member.ChangedEventMapper(event)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-11-23 18:31:12 +00:00
|
|
|
}
|
2020-11-30 05:40:56 +00:00
|
|
|
|
|
|
|
return &MemberChangedEvent{ChangedEvent: *e.(*member.ChangedEvent)}, nil
|
2020-11-23 18:31:12 +00:00
|
|
|
}
|
|
|
|
|
2020-11-25 13:12:44 +00:00
|
|
|
type MemberRemovedEvent struct {
|
|
|
|
member.RemovedEvent
|
|
|
|
}
|
|
|
|
|
2020-11-11 16:51:44 +00:00
|
|
|
func NewMemberRemovedEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
userID string,
|
|
|
|
) *MemberRemovedEvent {
|
|
|
|
|
|
|
|
return &MemberRemovedEvent{
|
2020-11-12 21:50:01 +00:00
|
|
|
RemovedEvent: *member.NewRemovedEvent(
|
2020-11-11 16:51:44 +00:00
|
|
|
eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
|
|
|
MemberRemovedEventType,
|
|
|
|
),
|
|
|
|
userID,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
2020-11-30 05:40:56 +00:00
|
|
|
|
|
|
|
func MemberRemovedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
|
|
|
|
e, err := member.RemovedEventMapper(event)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &MemberRemovedEvent{RemovedEvent: *e.(*member.RemovedEvent)}, nil
|
|
|
|
}
|