2021-12-02 08:31:44 +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-10-20 12:36:52 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/instance"
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/org"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/user"
|
2023-12-08 14:30:55 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2021-12-02 08:31:44 +00:00
|
|
|
)
|
|
|
|
|
2022-03-23 08:02:39 +00:00
|
|
|
const (
|
2023-11-20 15:21:08 +00:00
|
|
|
OrgMemberProjectionTable = "projections.org_members4"
|
2022-03-23 08:02:39 +00:00
|
|
|
OrgMemberOrgIDCol = "org_id"
|
|
|
|
)
|
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
type orgMemberProjection struct {
|
2023-10-19 10:19:10 +00:00
|
|
|
es handler.EventStore
|
2021-12-02 08:31:44 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func newOrgMemberProjection(ctx context.Context, config handler.Config) *handler.Handler {
|
|
|
|
return handler.NewHandler(ctx, &config, &orgMemberProjection{es: config.Eventstore})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*orgMemberProjection) Name() string {
|
|
|
|
return OrgMemberProjectionTable
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*orgMemberProjection) Init() *old_handler.Check {
|
|
|
|
return handler.NewTableCheck(
|
|
|
|
handler.NewTable(
|
|
|
|
append(memberColumns, handler.NewColumn(OrgMemberOrgIDCol, handler.ColumnTypeText)),
|
|
|
|
handler.NewPrimaryKey(MemberInstanceID, OrgMemberOrgIDCol, MemberUserIDCol),
|
|
|
|
handler.WithIndex(handler.NewIndex("user_id", []string{MemberUserIDCol})),
|
|
|
|
handler.WithIndex(
|
|
|
|
handler.NewIndex("om_instance", []string{MemberInstanceID},
|
|
|
|
handler.WithInclude(
|
|
|
|
MemberCreationDate,
|
|
|
|
MemberChangeDate,
|
|
|
|
MemberRolesCol,
|
|
|
|
MemberSequence,
|
|
|
|
MemberResourceOwner,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2022-03-23 08:02:39 +00:00
|
|
|
),
|
|
|
|
)
|
2021-12-02 08:31:44 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (p *orgMemberProjection) Reducers() []handler.AggregateReducer {
|
2021-12-02 08:31:44 +00:00
|
|
|
return []handler.AggregateReducer{
|
|
|
|
{
|
|
|
|
Aggregate: org.AggregateType,
|
2023-10-19 10:19:10 +00:00
|
|
|
EventReducers: []handler.EventReducer{
|
2021-12-02 08:31:44 +00:00
|
|
|
{
|
|
|
|
Event: org.MemberAddedEventType,
|
|
|
|
Reduce: p.reduceAdded,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: org.MemberChangedEventType,
|
|
|
|
Reduce: p.reduceChanged,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: org.MemberCascadeRemovedEventType,
|
|
|
|
Reduce: p.reduceCascadeRemoved,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: org.MemberRemovedEventType,
|
|
|
|
Reduce: p.reduceRemoved,
|
|
|
|
},
|
2022-01-03 10:50:38 +00:00
|
|
|
{
|
|
|
|
Event: org.OrgRemovedEventType,
|
|
|
|
Reduce: p.reduceOrgRemoved,
|
|
|
|
},
|
2021-12-02 08:31:44 +00:00
|
|
|
},
|
|
|
|
},
|
2021-12-16 13:25:38 +00:00
|
|
|
{
|
|
|
|
Aggregate: user.AggregateType,
|
2023-10-19 10:19:10 +00:00
|
|
|
EventReducers: []handler.EventReducer{
|
2021-12-16 13:25:38 +00:00
|
|
|
{
|
|
|
|
Event: user.UserRemovedType,
|
|
|
|
Reduce: p.reduceUserRemoved,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-10-20 12:36:52 +00:00
|
|
|
{
|
|
|
|
Aggregate: instance.AggregateType,
|
2023-10-19 10:19:10 +00:00
|
|
|
EventReducers: []handler.EventReducer{
|
2022-10-20 12:36:52 +00:00
|
|
|
{
|
|
|
|
Event: instance.InstanceRemovedEventType,
|
|
|
|
Reduce: reduceInstanceRemovedHelper(MemberInstanceID),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-12-02 08:31:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
func (p *orgMemberProjection) reduceAdded(event eventstore.Event) (*handler.Statement, error) {
|
2021-12-02 08:31:44 +00:00
|
|
|
e, ok := event.(*org.MemberAddedEvent)
|
|
|
|
if !ok {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-uYq4r", "reduce.wrong.event.type %s", org.MemberAddedEventType)
|
2021-12-02 08:31:44 +00:00
|
|
|
}
|
2022-11-30 16:01:17 +00:00
|
|
|
ctx := setMemberContext(e.Aggregate())
|
2024-01-08 15:26:30 +00:00
|
|
|
userOwner, err := getUserResourceOwner(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(OrgMemberOrgIDCol, e.Aggregate().ID))
|
2021-12-02 08:31:44 +00:00
|
|
|
}
|
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
func (p *orgMemberProjection) reduceChanged(event eventstore.Event) (*handler.Statement, error) {
|
2021-12-02 08:31:44 +00:00
|
|
|
e, ok := event.(*org.MemberChangedEvent)
|
|
|
|
if !ok {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-Bg8oM", "reduce.wrong.event.type %s", org.MemberChangedEventType)
|
2021-12-02 08:31:44 +00:00
|
|
|
}
|
|
|
|
return reduceMemberChanged(e.MemberChangedEvent, withMemberCond(OrgMemberOrgIDCol, e.Aggregate().ID))
|
|
|
|
}
|
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
func (p *orgMemberProjection) reduceCascadeRemoved(event eventstore.Event) (*handler.Statement, error) {
|
2021-12-02 08:31:44 +00:00
|
|
|
e, ok := event.(*org.MemberCascadeRemovedEvent)
|
|
|
|
if !ok {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-4twP2", "reduce.wrong.event.type %s", org.MemberCascadeRemovedEventType)
|
2021-12-02 08:31:44 +00:00
|
|
|
}
|
|
|
|
return reduceMemberCascadeRemoved(e.MemberCascadeRemovedEvent, withMemberCond(OrgMemberOrgIDCol, e.Aggregate().ID))
|
|
|
|
}
|
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
func (p *orgMemberProjection) reduceRemoved(event eventstore.Event) (*handler.Statement, error) {
|
2021-12-02 08:31:44 +00:00
|
|
|
e, ok := event.(*org.MemberRemovedEvent)
|
|
|
|
if !ok {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-avatH", "reduce.wrong.event.type %s", org.MemberRemovedEventType)
|
2021-12-02 08:31:44 +00:00
|
|
|
}
|
2021-12-16 13:25:38 +00:00
|
|
|
return reduceMemberRemoved(e,
|
|
|
|
withMemberCond(MemberUserIDCol, e.UserID),
|
|
|
|
withMemberCond(OrgMemberOrgIDCol, e.Aggregate().ID),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
func (p *orgMemberProjection) reduceUserRemoved(event eventstore.Event) (*handler.Statement, error) {
|
2021-12-16 13:25:38 +00:00
|
|
|
e, ok := event.(*user.UserRemovedEvent)
|
|
|
|
if !ok {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-eBMqH", "reduce.wrong.event.type %s", user.UserRemovedType)
|
2021-12-16 13:25:38 +00:00
|
|
|
}
|
|
|
|
return reduceMemberRemoved(e, withMemberCond(MemberUserIDCol, e.Aggregate().ID))
|
|
|
|
}
|
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
func (p *orgMemberProjection) reduceOrgRemoved(event eventstore.Event) (*handler.Statement, error) {
|
2021-12-16 13:25:38 +00:00
|
|
|
e, ok := event.(*org.OrgRemovedEvent)
|
|
|
|
if !ok {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-jnGAV", "reduce.wrong.event.type %s", org.OrgRemovedEventType)
|
2021-12-16 13:25:38 +00:00
|
|
|
}
|
2023-10-19 10:19:10 +00:00
|
|
|
return handler.NewMultiStatement(
|
2022-11-30 16:01:17 +00:00
|
|
|
e,
|
|
|
|
multiReduceMemberOwnerRemoved(e),
|
|
|
|
multiReduceMemberUserOwnerRemoved(e),
|
|
|
|
), nil
|
2021-12-02 08:31:44 +00:00
|
|
|
}
|