2022-03-24 16:21:34 +00:00
|
|
|
package projection
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
2023-10-19 10:19:10 +00:00
|
|
|
old_handler "github.com/zitadel/zitadel/internal/eventstore/handler"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/handler/v2"
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/instance"
|
2022-11-30 16:01:17 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/org"
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/user"
|
2023-12-08 14:30:55 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2022-03-24 16:21:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2023-11-20 15:21:08 +00:00
|
|
|
InstanceMemberProjectionTable = "projections.instance_members4"
|
2022-03-24 16:21:34 +00:00
|
|
|
|
2022-03-29 09:53:19 +00:00
|
|
|
InstanceMemberIAMIDCol = "id"
|
2022-03-24 16:21:34 +00:00
|
|
|
)
|
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
type instanceMemberProjection struct {
|
2023-10-19 10:19:10 +00:00
|
|
|
es handler.EventStore
|
2022-03-24 16:21:34 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func newInstanceMemberProjection(ctx context.Context, config handler.Config) *handler.Handler {
|
|
|
|
return handler.NewHandler(ctx, &config, &instanceMemberProjection{es: config.Eventstore})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*instanceMemberProjection) Name() string {
|
|
|
|
return InstanceMemberProjectionTable
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*instanceMemberProjection) Init() *old_handler.Check {
|
|
|
|
return handler.NewTableCheck(
|
|
|
|
handler.NewTable(
|
|
|
|
append(memberColumns, handler.NewColumn(InstanceColumnID, handler.ColumnTypeText)),
|
|
|
|
handler.NewPrimaryKey(MemberInstanceID, InstanceColumnID, MemberUserIDCol),
|
|
|
|
handler.WithIndex(handler.NewIndex("user_id", []string{MemberUserIDCol})),
|
|
|
|
handler.WithIndex(
|
|
|
|
handler.NewIndex("im_instance", []string{MemberInstanceID},
|
|
|
|
handler.WithInclude(
|
|
|
|
MemberCreationDate,
|
|
|
|
MemberChangeDate,
|
|
|
|
MemberRolesCol,
|
|
|
|
MemberSequence,
|
|
|
|
MemberResourceOwner,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2022-03-24 16:21:34 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (p *instanceMemberProjection) Reducers() []handler.AggregateReducer {
|
2022-03-24 16:21:34 +00:00
|
|
|
return []handler.AggregateReducer{
|
|
|
|
{
|
|
|
|
Aggregate: instance.AggregateType,
|
2023-10-19 10:19:10 +00:00
|
|
|
EventReducers: []handler.EventReducer{
|
2022-03-24 16:21:34 +00:00
|
|
|
{
|
|
|
|
Event: instance.MemberAddedEventType,
|
|
|
|
Reduce: p.reduceAdded,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: instance.MemberChangedEventType,
|
|
|
|
Reduce: p.reduceChanged,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: instance.MemberCascadeRemovedEventType,
|
|
|
|
Reduce: p.reduceCascadeRemoved,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: instance.MemberRemovedEventType,
|
|
|
|
Reduce: p.reduceRemoved,
|
|
|
|
},
|
2022-10-20 12:36:52 +00:00
|
|
|
{
|
|
|
|
Event: instance.InstanceRemovedEventType,
|
|
|
|
Reduce: reduceInstanceRemovedHelper(AppColumnInstanceID),
|
|
|
|
},
|
2022-03-24 16:21:34 +00:00
|
|
|
},
|
|
|
|
},
|
2022-11-30 16:01:17 +00:00
|
|
|
{
|
|
|
|
Aggregate: org.AggregateType,
|
2023-10-19 10:19:10 +00:00
|
|
|
EventReducers: []handler.EventReducer{
|
2022-11-30 16:01:17 +00:00
|
|
|
{
|
|
|
|
Event: org.OrgRemovedEventType,
|
|
|
|
Reduce: p.reduceUserOwnerRemoved,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-03-24 16:21:34 +00:00
|
|
|
{
|
|
|
|
Aggregate: user.AggregateType,
|
2023-10-19 10:19:10 +00:00
|
|
|
EventReducers: []handler.EventReducer{
|
2022-03-24 16:21:34 +00:00
|
|
|
{
|
|
|
|
Event: user.UserRemovedType,
|
|
|
|
Reduce: p.reduceUserRemoved,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
func (p *instanceMemberProjection) reduceAdded(event eventstore.Event) (*handler.Statement, error) {
|
2022-03-24 16:21:34 +00:00
|
|
|
e, ok := event.(*instance.MemberAddedEvent)
|
|
|
|
if !ok {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-pGNCu", "reduce.wrong.event.type %s", instance.MemberAddedEventType)
|
2022-03-24 16:21:34 +00:00
|
|
|
}
|
2022-11-30 16:01:17 +00:00
|
|
|
ctx := setMemberContext(e.Aggregate())
|
2023-10-19 10:19:10 +00:00
|
|
|
userOwner, err := getResourceOwnerOfUser(ctx, p.es, e.Aggregate().InstanceID, e.UserID)
|
2022-11-30 16:01:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return reduceMemberAdded(e.MemberAddedEvent, userOwner, withMemberCol(InstanceMemberIAMIDCol, e.Aggregate().ID))
|
2022-03-24 16:21:34 +00:00
|
|
|
}
|
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
func (p *instanceMemberProjection) reduceChanged(event eventstore.Event) (*handler.Statement, error) {
|
2022-03-24 16:21:34 +00:00
|
|
|
e, ok := event.(*instance.MemberChangedEvent)
|
|
|
|
if !ok {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-5WQcZ", "reduce.wrong.event.type %s", instance.MemberChangedEventType)
|
2022-03-24 16:21:34 +00:00
|
|
|
}
|
|
|
|
return reduceMemberChanged(e.MemberChangedEvent)
|
|
|
|
}
|
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
func (p *instanceMemberProjection) reduceCascadeRemoved(event eventstore.Event) (*handler.Statement, error) {
|
2022-03-24 16:21:34 +00:00
|
|
|
e, ok := event.(*instance.MemberCascadeRemovedEvent)
|
|
|
|
if !ok {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-Dmdf2", "reduce.wrong.event.type %s", instance.MemberCascadeRemovedEventType)
|
2022-03-24 16:21:34 +00:00
|
|
|
}
|
|
|
|
return reduceMemberCascadeRemoved(e.MemberCascadeRemovedEvent)
|
|
|
|
}
|
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
func (p *instanceMemberProjection) reduceRemoved(event eventstore.Event) (*handler.Statement, error) {
|
2022-03-24 16:21:34 +00:00
|
|
|
e, ok := event.(*instance.MemberRemovedEvent)
|
|
|
|
if !ok {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-exVqy", "reduce.wrong.event.type %s", instance.MemberRemovedEventType)
|
2022-03-24 16:21:34 +00:00
|
|
|
}
|
|
|
|
return reduceMemberRemoved(e, withMemberCond(MemberUserIDCol, e.UserID))
|
|
|
|
}
|
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
func (p *instanceMemberProjection) reduceUserRemoved(event eventstore.Event) (*handler.Statement, error) {
|
2022-03-24 16:21:34 +00:00
|
|
|
e, ok := event.(*user.UserRemovedEvent)
|
|
|
|
if !ok {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-mkDHF", "reduce.wrong.event.type %s", user.UserRemovedType)
|
2022-03-24 16:21:34 +00:00
|
|
|
}
|
|
|
|
return reduceMemberRemoved(e, withMemberCond(MemberUserIDCol, e.Aggregate().ID))
|
|
|
|
}
|
2022-11-30 16:01:17 +00:00
|
|
|
|
|
|
|
func (p *instanceMemberProjection) reduceUserOwnerRemoved(event eventstore.Event) (*handler.Statement, error) {
|
|
|
|
e, ok := event.(*org.OrgRemovedEvent)
|
|
|
|
if !ok {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-mkDHa", "reduce.wrong.event.type %s", org.OrgRemovedEventType)
|
2022-11-30 16:01:17 +00:00
|
|
|
}
|
|
|
|
return reduceMemberUserOwnerRemoved(e)
|
|
|
|
}
|