mirror of
https://github.com/zitadel/zitadel.git
synced 2024-12-12 11:04:25 +00:00
b5564572bc
This implementation increases parallel write capabilities of the eventstore. Please have a look at the technical advisories: [05](https://zitadel.com/docs/support/advisory/a10005) and [06](https://zitadel.com/docs/support/advisory/a10006). The implementation of eventstore.push is rewritten and stored events are migrated to a new table `eventstore.events2`. If you are using cockroach: make sure that the database user of ZITADEL has `VIEWACTIVITY` grant. This is used to query events.
195 lines
6.1 KiB
Go
195 lines
6.1 KiB
Go
package projection
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/zitadel/zitadel/internal/errors"
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
old_handler "github.com/zitadel/zitadel/internal/eventstore/handler"
|
|
"github.com/zitadel/zitadel/internal/eventstore/handler/v2"
|
|
"github.com/zitadel/zitadel/internal/repository/instance"
|
|
"github.com/zitadel/zitadel/internal/repository/member"
|
|
"github.com/zitadel/zitadel/internal/repository/org"
|
|
"github.com/zitadel/zitadel/internal/repository/project"
|
|
"github.com/zitadel/zitadel/internal/repository/user"
|
|
)
|
|
|
|
const (
|
|
ProjectMemberProjectionTable = "projections.project_members3"
|
|
ProjectMemberProjectIDCol = "project_id"
|
|
)
|
|
|
|
type projectMemberProjection struct {
|
|
es handler.EventStore
|
|
}
|
|
|
|
func newProjectMemberProjection(ctx context.Context, config handler.Config) *handler.Handler {
|
|
return handler.NewHandler(ctx, &config, &projectMemberProjection{es: config.Eventstore})
|
|
}
|
|
|
|
func (*projectMemberProjection) Name() string {
|
|
return ProjectMemberProjectionTable
|
|
}
|
|
|
|
func (*projectMemberProjection) Init() *old_handler.Check {
|
|
return handler.NewTableCheck(
|
|
handler.NewTable(
|
|
append(memberColumns,
|
|
handler.NewColumn(ProjectMemberProjectIDCol, handler.ColumnTypeText),
|
|
),
|
|
handler.NewPrimaryKey(MemberInstanceID, ProjectMemberProjectIDCol, MemberUserIDCol),
|
|
handler.WithIndex(handler.NewIndex("user_id", []string{MemberUserIDCol})),
|
|
handler.WithIndex(handler.NewIndex("owner_removed", []string{MemberOwnerRemoved})),
|
|
handler.WithIndex(handler.NewIndex("user_owner_removed", []string{MemberUserOwnerRemoved})),
|
|
handler.WithIndex(
|
|
handler.NewIndex("pm_instance", []string{MemberInstanceID},
|
|
handler.WithInclude(
|
|
MemberCreationDate,
|
|
MemberChangeDate,
|
|
MemberUserOwnerRemoved,
|
|
MemberRolesCol,
|
|
MemberSequence,
|
|
MemberResourceOwner,
|
|
MemberOwnerRemoved,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
)
|
|
}
|
|
|
|
func (p *projectMemberProjection) Reducers() []handler.AggregateReducer {
|
|
return []handler.AggregateReducer{
|
|
{
|
|
Aggregate: project.AggregateType,
|
|
EventReducers: []handler.EventReducer{
|
|
{
|
|
Event: project.MemberAddedType,
|
|
Reduce: p.reduceAdded,
|
|
},
|
|
{
|
|
Event: project.MemberChangedType,
|
|
Reduce: p.reduceChanged,
|
|
},
|
|
{
|
|
Event: project.MemberCascadeRemovedType,
|
|
Reduce: p.reduceCascadeRemoved,
|
|
},
|
|
{
|
|
Event: project.MemberRemovedType,
|
|
Reduce: p.reduceRemoved,
|
|
},
|
|
{
|
|
Event: project.ProjectRemovedType,
|
|
Reduce: p.reduceProjectRemoved,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
Aggregate: user.AggregateType,
|
|
EventReducers: []handler.EventReducer{
|
|
{
|
|
Event: user.UserRemovedType,
|
|
Reduce: p.reduceUserRemoved,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
Aggregate: org.AggregateType,
|
|
EventReducers: []handler.EventReducer{
|
|
{
|
|
Event: org.OrgRemovedEventType,
|
|
Reduce: p.reduceOrgRemoved,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
Aggregate: instance.AggregateType,
|
|
EventReducers: []handler.EventReducer{
|
|
{
|
|
Event: instance.InstanceRemovedEventType,
|
|
Reduce: reduceInstanceRemovedHelper(MemberInstanceID),
|
|
},
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func (p *projectMemberProjection) reduceAdded(event eventstore.Event) (*handler.Statement, error) {
|
|
e, ok := event.(*project.MemberAddedEvent)
|
|
if !ok {
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-bgx5Q", "reduce.wrong.event.type %s", project.MemberAddedType)
|
|
}
|
|
ctx := setMemberContext(e.Aggregate())
|
|
userOwner, err := getResourceOwnerOfUser(ctx, p.es, e.Aggregate().InstanceID, e.UserID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return reduceMemberAdded(
|
|
*member.NewMemberAddedEvent(&e.BaseEvent, e.UserID, e.Roles...),
|
|
userOwner,
|
|
withMemberCol(ProjectMemberProjectIDCol, e.Aggregate().ID),
|
|
)
|
|
}
|
|
|
|
func (p *projectMemberProjection) reduceChanged(event eventstore.Event) (*handler.Statement, error) {
|
|
e, ok := event.(*project.MemberChangedEvent)
|
|
if !ok {
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-90WJ1", "reduce.wrong.event.type %s", project.MemberChangedType)
|
|
}
|
|
return reduceMemberChanged(
|
|
*member.NewMemberChangedEvent(&e.BaseEvent, e.UserID, e.Roles...),
|
|
withMemberCond(ProjectMemberProjectIDCol, e.Aggregate().ID),
|
|
)
|
|
}
|
|
|
|
func (p *projectMemberProjection) reduceCascadeRemoved(event eventstore.Event) (*handler.Statement, error) {
|
|
e, ok := event.(*project.MemberCascadeRemovedEvent)
|
|
if !ok {
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-aGd43", "reduce.wrong.event.type %s", project.MemberCascadeRemovedType)
|
|
}
|
|
return reduceMemberCascadeRemoved(
|
|
*member.NewCascadeRemovedEvent(&e.BaseEvent, e.UserID),
|
|
withMemberCond(ProjectMemberProjectIDCol, e.Aggregate().ID),
|
|
)
|
|
}
|
|
|
|
func (p *projectMemberProjection) reduceRemoved(event eventstore.Event) (*handler.Statement, error) {
|
|
e, ok := event.(*project.MemberRemovedEvent)
|
|
if !ok {
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-eJZPh", "reduce.wrong.event.type %s", project.MemberRemovedType)
|
|
}
|
|
return reduceMemberRemoved(e,
|
|
withMemberCond(MemberUserIDCol, e.UserID),
|
|
withMemberCond(ProjectMemberProjectIDCol, e.Aggregate().ID),
|
|
)
|
|
}
|
|
|
|
func (p *projectMemberProjection) reduceUserRemoved(event eventstore.Event) (*handler.Statement, error) {
|
|
e, ok := event.(*user.UserRemovedEvent)
|
|
if !ok {
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-aYA60", "reduce.wrong.event.type %s", user.UserRemovedType)
|
|
}
|
|
return reduceMemberRemoved(e, withMemberCond(MemberUserIDCol, e.Aggregate().ID))
|
|
}
|
|
|
|
func (p *projectMemberProjection) reduceOrgRemoved(event eventstore.Event) (*handler.Statement, error) {
|
|
e, ok := event.(*org.OrgRemovedEvent)
|
|
if !ok {
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-NGUEL", "reduce.wrong.event.type %s", org.OrgRemovedEventType)
|
|
}
|
|
return handler.NewMultiStatement(
|
|
e,
|
|
multiReduceMemberOwnerRemoved(e),
|
|
multiReduceMemberUserOwnerRemoved(e),
|
|
), nil
|
|
}
|
|
|
|
func (p *projectMemberProjection) reduceProjectRemoved(event eventstore.Event) (*handler.Statement, error) {
|
|
e, ok := event.(*project.ProjectRemovedEvent)
|
|
if !ok {
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-NGUEL", "reduce.wrong.event.type %s", project.ProjectRemovedType)
|
|
}
|
|
return reduceMemberRemoved(e, withMemberCond(ProjectMemberProjectIDCol, e.Aggregate().ID))
|
|
}
|