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

145 lines
2.8 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 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
}
2020-11-23 10:36:58 +00:00
type MemberWriteModel struct {
member.WriteModel
}
func PrepareMemberWriteModel(iamID, userID string) *MemberWriteModel {
return &MemberWriteModel{
WriteModel: *member.PrepareWriteModel(userID, AggregateType, iamID),
}
}
func (wm *MemberWriteModel) AppendEvents(events ...eventstore.EventReader) (err error) {
for _, event := range events {
switch e := event.(type) {
case *MemberAddedEvent:
wm.WriteModel.AppendEvents(&e.AddedEvent)
case *MemberChangedEvent:
wm.WriteModel.AppendEvents(&e.ChangedEvent)
case *MemberRemovedEvent:
wm.WriteModel.AppendEvents(&e.RemovedEvent)
default:
wm.WriteModel.AppendEvents(e)
}
}
return nil
}
2020-11-11 16:51:44 +00:00
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...,
),
}
}
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-23 18:31:12 +00:00
m, 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-18 20:22:15 +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{
ChangedEvent: *m,
}, nil
2020-11-11 16:51:44 +00:00
}
2020-11-23 18:31:12 +00:00
func NewMemberChangedEvent(
ctx context.Context,
userID string,
roles ...string,
) *MemberChangedEvent {
return &MemberChangedEvent{
ChangedEvent: *member.NewChangedEvent(
eventstore.NewBaseEventForPush(
ctx,
MemberChangedEventType,
),
userID,
roles...,
),
}
}
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,
),
}
}