mirror of
https://github.com/zitadel/zitadel.git
synced 2024-12-13 19:44:21 +00:00
ebeedd1346
member not working atm
172 lines
3.6 KiB
Go
172 lines
3.6 KiB
Go
package iam
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/caos/zitadel/internal/eventstore/v2"
|
|
"github.com/caos/zitadel/internal/eventstore/v2/repository"
|
|
"github.com/caos/zitadel/internal/v2/repository/member"
|
|
)
|
|
|
|
var (
|
|
MemberAddedEventType = iamEventTypePrefix + member.AddedEventType
|
|
MemberChangedEventType = iamEventTypePrefix + member.ChangedEventType
|
|
MemberRemovedEventType = iamEventTypePrefix + member.RemovedEventType
|
|
)
|
|
|
|
type MemberReadModel struct {
|
|
member.ReadModel
|
|
}
|
|
|
|
func (rm *MemberReadModel) AppendEvents(events ...eventstore.EventReader) {
|
|
for _, event := range events {
|
|
switch e := event.(type) {
|
|
case *MemberAddedEvent:
|
|
rm.ReadModel.AppendEvents(&e.AddedEvent)
|
|
case *MemberChangedEvent:
|
|
rm.ReadModel.AppendEvents(&e.ChangedEvent)
|
|
case *member.AddedEvent, *member.ChangedEvent, *MemberRemovedEvent:
|
|
rm.ReadModel.AppendEvents(e)
|
|
}
|
|
}
|
|
}
|
|
|
|
type MemberWriteModel struct {
|
|
eventstore.WriteModel
|
|
Member member.WriteModel
|
|
|
|
userID string
|
|
iamID string
|
|
}
|
|
|
|
func NewMemberReadModel(iamID, userID string) *MemberWriteModel {
|
|
return &MemberWriteModel{
|
|
userID: userID,
|
|
iamID: iamID,
|
|
}
|
|
}
|
|
|
|
func (wm *MemberWriteModel) AppendEvents(events ...eventstore.EventReader) {
|
|
wm.WriteModel.AppendEvents(events...)
|
|
for _, event := range events {
|
|
switch e := event.(type) {
|
|
case *MemberAddedEvent:
|
|
if e.UserID != wm.userID {
|
|
continue
|
|
}
|
|
wm.Member.AppendEvents(&e.AddedEvent)
|
|
case *MemberChangedEvent:
|
|
if e.UserID != wm.userID {
|
|
continue
|
|
}
|
|
wm.Member.AppendEvents(&e.ChangedEvent)
|
|
case *MemberRemovedEvent:
|
|
if e.UserID != wm.userID {
|
|
continue
|
|
}
|
|
wm.Member.AppendEvents(&e.RemovedEvent)
|
|
}
|
|
}
|
|
}
|
|
|
|
func (wm *MemberWriteModel) Query() *eventstore.SearchQueryFactory {
|
|
return eventstore.NewSearchQueryFactory(eventstore.ColumnsEvent, AggregateType).
|
|
AggregateIDs(wm.iamID)
|
|
}
|
|
|
|
type MemberAddedEvent struct {
|
|
member.AddedEvent
|
|
}
|
|
|
|
func NewMemberAddedEvent(
|
|
ctx context.Context,
|
|
userID string,
|
|
roles ...string,
|
|
) *MemberAddedEvent {
|
|
|
|
return &MemberAddedEvent{
|
|
AddedEvent: *member.NewAddedEvent(
|
|
eventstore.NewBaseEventForPush(
|
|
ctx,
|
|
MemberAddedEventType,
|
|
),
|
|
userID,
|
|
roles...,
|
|
),
|
|
}
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
type MemberChangedEvent struct {
|
|
member.ChangedEvent
|
|
}
|
|
|
|
func MemberChangedEventFromExisting(
|
|
ctx context.Context,
|
|
current *MemberWriteModel,
|
|
roles ...string,
|
|
) (*MemberChangedEvent, error) {
|
|
|
|
event, err := member.ChangeEventFromExisting(
|
|
eventstore.NewBaseEventForPush(
|
|
ctx,
|
|
MemberChangedEventType,
|
|
),
|
|
¤t.Member,
|
|
roles...,
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &MemberChangedEvent{
|
|
ChangedEvent: *event,
|
|
}, nil
|
|
}
|
|
|
|
func MemberChangedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
|
|
e, err := member.ChangedEventMapper(event)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &MemberChangedEvent{ChangedEvent: *e.(*member.ChangedEvent)}, nil
|
|
}
|
|
|
|
type MemberRemovedEvent struct {
|
|
member.RemovedEvent
|
|
}
|
|
|
|
func NewMemberRemovedEvent(
|
|
ctx context.Context,
|
|
userID string,
|
|
) *MemberRemovedEvent {
|
|
|
|
return &MemberRemovedEvent{
|
|
RemovedEvent: *member.NewRemovedEvent(
|
|
eventstore.NewBaseEventForPush(
|
|
ctx,
|
|
MemberRemovedEventType,
|
|
),
|
|
userID,
|
|
),
|
|
}
|
|
}
|
|
|
|
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
|
|
}
|