zitadel/internal/v2/repository/iam/member.go

193 lines
4.1 KiB
Go
Raw Normal View History

2020-11-11 16:51:44 +00:00
package iam
import (
"context"
"github.com/caos/zitadel/internal/eventstore/v2"
"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-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)
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 {
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{
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:
if e.UserID != wm.WriteModel.UserID {
continue
}
wm.WriteModel.AppendEvents(&e.AddedEvent)
2020-11-23 10:36:58 +00:00
case *MemberChangedEvent:
if e.UserID != wm.WriteModel.UserID {
continue
}
wm.WriteModel.AppendEvents(&e.ChangedEvent)
2020-11-23 10:36:58 +00:00
case *MemberRemovedEvent:
if e.UserID != wm.WriteModel.UserID {
continue
}
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).
AggregateIDs(wm.WriteModel.AggregateID)
}
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{
AddedEvent: *member.NewAddedEvent(
2020-11-11 16:51:44 +00:00
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
}
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
),
&current.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
}
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
}
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{
RemovedEvent: *member.NewRemovedEvent(
2020-11-11 16:51:44 +00:00
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
}