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

105 lines
1.9 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/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 MemberWriteModel struct {
member.WriteModel
}
type MemberReadModel struct {
member.ReadModel
}
2020-11-11 16:51:44 +00:00
func (rm *MemberReadModel) AppendEvents(events ...eventstore.EventReader) (err error) {
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
}
}
return nil
}
type MemberAddedEvent struct {
member.AddedEvent
}
type MemberChangedEvent struct {
member.ChangedEvent
}
type MemberRemovedEvent struct {
member.RemovedEvent
}
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 NewMemberChangedEvent(
ctx context.Context,
2020-11-18 20:22:15 +00:00
current,
changed *MemberWriteModel,
) (*MemberChangedEvent, error) {
2020-11-11 16:51:44 +00:00
2020-11-18 20:22:15 +00:00
m, err := member.NewChangedEvent(
eventstore.NewBaseEventForPush(
ctx,
MemberChangedEventType,
2020-11-11 16:51:44 +00:00
),
2020-11-18 20:22:15 +00:00
&current.WriteModel,
&changed.WriteModel,
)
if err != nil {
return nil, err
2020-11-11 16:51:44 +00:00
}
2020-11-18 20:22:15 +00:00
return &MemberChangedEvent{
ChangedEvent: *m,
}, nil
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,
),
}
}