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 (
|
|
|
|
MemberAddedEventType = iamEventTypePrefix + member.AddedEventType
|
|
|
|
MemberChangedEventType = iamEventTypePrefix + member.ChangedEventType
|
|
|
|
MemberRemovedEventType = iamEventTypePrefix + member.RemovedEventType
|
|
|
|
)
|
|
|
|
|
2020-11-18 20:22:15 +00:00
|
|
|
type MemberReadModel struct {
|
|
|
|
member.ReadModel
|
|
|
|
}
|
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-11-23 10:36:58 +00:00
|
|
|
type MemberWriteModel struct {
|
2020-11-30 05:40:56 +00:00
|
|
|
eventstore.WriteModel
|
|
|
|
Member member.WriteModel
|
|
|
|
|
|
|
|
userID string
|
|
|
|
iamID string
|
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-11-30 05:40:56 +00:00
|
|
|
userID: userID,
|
|
|
|
iamID: iamID,
|
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-30 05:40:56 +00:00
|
|
|
wm.WriteModel.AppendEvents(events...)
|
2020-11-23 10:36:58 +00:00
|
|
|
for _, event := range events {
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *MemberAddedEvent:
|
2020-11-30 05:40:56 +00:00
|
|
|
if e.UserID != wm.userID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.Member.AppendEvents(&e.AddedEvent)
|
2020-11-23 10:36:58 +00:00
|
|
|
case *MemberChangedEvent:
|
2020-11-30 05:40:56 +00:00
|
|
|
if e.UserID != wm.userID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.Member.AppendEvents(&e.ChangedEvent)
|
2020-11-23 10:36:58 +00:00
|
|
|
case *MemberRemovedEvent:
|
2020-11-30 05:40:56 +00:00
|
|
|
if e.UserID != wm.userID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.Member.AppendEvents(&e.RemovedEvent)
|
2020-11-23 10:36:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-30 06:56:38 +00:00
|
|
|
func (wm *MemberWriteModel) Reduce() error {
|
|
|
|
if err := wm.Member.Reduce(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return wm.WriteModel.Reduce()
|
|
|
|
}
|
|
|
|
|
2020-11-30 05:40:56 +00:00
|
|
|
func (wm *MemberWriteModel) Query() *eventstore.SearchQueryFactory {
|
|
|
|
return eventstore.NewSearchQueryFactory(eventstore.ColumnsEvent, AggregateType).
|
|
|
|
AggregateIDs(wm.iamID)
|
|
|
|
}
|
|
|
|
|
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-11-30 05:40:56 +00:00
|
|
|
¤t.Member,
|
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
|
|
|
|
}
|