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/errors"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/handler"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/handler/crdb"
|
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/member"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/org"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/project"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/user"
|
2021-12-02 08:31:44 +00:00
|
|
|
)
|
|
|
|
|
2022-03-23 08:02:39 +00:00
|
|
|
const (
|
2022-11-30 16:01:17 +00:00
|
|
|
ProjectMemberProjectionTable = "projections.project_members3"
|
2022-03-23 08:02:39 +00:00
|
|
|
ProjectMemberProjectIDCol = "project_id"
|
|
|
|
)
|
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
type projectMemberProjection struct {
|
2021-12-02 08:31:44 +00:00
|
|
|
crdb.StatementHandler
|
|
|
|
}
|
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
func newProjectMemberProjection(ctx context.Context, config crdb.StatementHandlerConfig) *projectMemberProjection {
|
|
|
|
p := new(projectMemberProjection)
|
2021-12-02 08:31:44 +00:00
|
|
|
config.ProjectionName = ProjectMemberProjectionTable
|
|
|
|
config.Reducers = p.reducers()
|
2022-03-23 08:02:39 +00:00
|
|
|
config.InitCheck = crdb.NewTableCheck(
|
|
|
|
crdb.NewTable(
|
|
|
|
append(memberColumns,
|
|
|
|
crdb.NewColumn(ProjectMemberProjectIDCol, crdb.ColumnTypeText),
|
|
|
|
),
|
|
|
|
crdb.NewPrimaryKey(MemberInstanceID, ProjectMemberProjectIDCol, MemberUserIDCol),
|
2022-11-30 16:01:17 +00:00
|
|
|
crdb.WithIndex(crdb.NewIndex("user_id", []string{MemberUserIDCol})),
|
|
|
|
crdb.WithIndex(crdb.NewIndex("owner_removed", []string{MemberOwnerRemoved})),
|
|
|
|
crdb.WithIndex(crdb.NewIndex("user_owner_removed", []string{MemberUserOwnerRemoved})),
|
2022-03-23 08:02:39 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
|
2021-12-02 08:31:44 +00:00
|
|
|
p.StatementHandler = crdb.NewStatementHandler(ctx, config)
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
func (p *projectMemberProjection) reducers() []handler.AggregateReducer {
|
2021-12-02 08:31:44 +00:00
|
|
|
return []handler.AggregateReducer{
|
|
|
|
{
|
|
|
|
Aggregate: project.AggregateType,
|
|
|
|
EventRedusers: []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,
|
|
|
|
},
|
2022-01-03 10:50:38 +00:00
|
|
|
{
|
|
|
|
Event: project.ProjectRemovedType,
|
|
|
|
Reduce: p.reduceProjectRemoved,
|
|
|
|
},
|
2021-12-02 08:31:44 +00:00
|
|
|
},
|
|
|
|
},
|
2021-12-16 13:25:38 +00:00
|
|
|
{
|
|
|
|
Aggregate: user.AggregateType,
|
|
|
|
EventRedusers: []handler.EventReducer{
|
|
|
|
{
|
|
|
|
Event: user.UserRemovedType,
|
|
|
|
Reduce: p.reduceUserRemoved,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Aggregate: org.AggregateType,
|
|
|
|
EventRedusers: []handler.EventReducer{
|
|
|
|
{
|
|
|
|
Event: org.OrgRemovedEventType,
|
|
|
|
Reduce: p.reduceOrgRemoved,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-10-20 12:36:52 +00:00
|
|
|
{
|
|
|
|
Aggregate: instance.AggregateType,
|
|
|
|
EventRedusers: []handler.EventReducer{
|
|
|
|
{
|
|
|
|
Event: instance.InstanceRemovedEventType,
|
|
|
|
Reduce: reduceInstanceRemovedHelper(MemberInstanceID),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-12-02 08:31:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
func (p *projectMemberProjection) reduceAdded(event eventstore.Event) (*handler.Statement, error) {
|
2021-12-02 08:31:44 +00:00
|
|
|
e, ok := event.(*project.MemberAddedEvent)
|
|
|
|
if !ok {
|
2022-03-23 08:02:39 +00:00
|
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-bgx5Q", "reduce.wrong.event.type %s", project.MemberAddedType)
|
2021-12-02 08:31:44 +00:00
|
|
|
}
|
2022-11-30 16:01:17 +00:00
|
|
|
ctx := setMemberContext(e.Aggregate())
|
|
|
|
userOwner, err := getResourceOwnerOfUser(ctx, p.Eventstore, e.Aggregate().InstanceID, e.UserID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-12-02 08:31:44 +00:00
|
|
|
return reduceMemberAdded(
|
|
|
|
*member.NewMemberAddedEvent(&e.BaseEvent, e.UserID, e.Roles...),
|
2022-11-30 16:01:17 +00:00
|
|
|
userOwner,
|
2021-12-02 08:31:44 +00:00
|
|
|
withMemberCol(ProjectMemberProjectIDCol, e.Aggregate().ID),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
func (p *projectMemberProjection) reduceChanged(event eventstore.Event) (*handler.Statement, error) {
|
2021-12-02 08:31:44 +00:00
|
|
|
e, ok := event.(*project.MemberChangedEvent)
|
|
|
|
if !ok {
|
2022-03-23 08:02:39 +00:00
|
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-90WJ1", "reduce.wrong.event.type %s", project.MemberChangedType)
|
2021-12-02 08:31:44 +00:00
|
|
|
}
|
|
|
|
return reduceMemberChanged(
|
|
|
|
*member.NewMemberChangedEvent(&e.BaseEvent, e.UserID, e.Roles...),
|
|
|
|
withMemberCond(ProjectMemberProjectIDCol, e.Aggregate().ID),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
func (p *projectMemberProjection) reduceCascadeRemoved(event eventstore.Event) (*handler.Statement, error) {
|
2021-12-02 08:31:44 +00:00
|
|
|
e, ok := event.(*project.MemberCascadeRemovedEvent)
|
|
|
|
if !ok {
|
2022-03-23 08:02:39 +00:00
|
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-aGd43", "reduce.wrong.event.type %s", project.MemberCascadeRemovedType)
|
2021-12-02 08:31:44 +00:00
|
|
|
}
|
|
|
|
return reduceMemberCascadeRemoved(
|
|
|
|
*member.NewCascadeRemovedEvent(&e.BaseEvent, e.UserID),
|
|
|
|
withMemberCond(ProjectMemberProjectIDCol, e.Aggregate().ID),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
func (p *projectMemberProjection) reduceRemoved(event eventstore.Event) (*handler.Statement, error) {
|
2021-12-02 08:31:44 +00:00
|
|
|
e, ok := event.(*project.MemberRemovedEvent)
|
|
|
|
if !ok {
|
2022-03-23 08:02:39 +00:00
|
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-eJZPh", "reduce.wrong.event.type %s", project.MemberRemovedType)
|
2021-12-02 08:31:44 +00:00
|
|
|
}
|
2021-12-16 13:25:38 +00:00
|
|
|
return reduceMemberRemoved(e,
|
|
|
|
withMemberCond(MemberUserIDCol, e.UserID),
|
2021-12-02 08:31:44 +00:00
|
|
|
withMemberCond(ProjectMemberProjectIDCol, e.Aggregate().ID),
|
|
|
|
)
|
|
|
|
}
|
2021-12-16 13:25:38 +00:00
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
func (p *projectMemberProjection) reduceUserRemoved(event eventstore.Event) (*handler.Statement, error) {
|
2021-12-16 13:25:38 +00:00
|
|
|
e, ok := event.(*user.UserRemovedEvent)
|
|
|
|
if !ok {
|
2022-03-23 08:02:39 +00:00
|
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-aYA60", "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 *projectMemberProjection) reduceOrgRemoved(event eventstore.Event) (*handler.Statement, error) {
|
2021-12-16 13:25:38 +00:00
|
|
|
e, ok := event.(*org.OrgRemovedEvent)
|
|
|
|
if !ok {
|
2022-03-23 08:02:39 +00:00
|
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-NGUEL", "reduce.wrong.event.type %s", org.OrgRemovedEventType)
|
2021-12-16 13:25:38 +00:00
|
|
|
}
|
2022-11-30 16:01:17 +00:00
|
|
|
return crdb.NewMultiStatement(
|
|
|
|
e,
|
|
|
|
multiReduceMemberOwnerRemoved(e),
|
|
|
|
multiReduceMemberUserOwnerRemoved(e),
|
|
|
|
), nil
|
2021-12-16 13:25:38 +00:00
|
|
|
}
|
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
func (p *projectMemberProjection) reduceProjectRemoved(event eventstore.Event) (*handler.Statement, error) {
|
2021-12-16 13:25:38 +00:00
|
|
|
e, ok := event.(*project.ProjectRemovedEvent)
|
|
|
|
if !ok {
|
2022-03-23 08:02:39 +00:00
|
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-NGUEL", "reduce.wrong.event.type %s", project.ProjectRemovedType)
|
2021-12-16 13:25:38 +00:00
|
|
|
}
|
|
|
|
return reduceMemberRemoved(e, withMemberCond(ProjectMemberProjectIDCol, e.Aggregate().ID))
|
|
|
|
}
|