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
|
|
|
ProjectGrantMemberProjectionTable = "projections.project_grant_members3"
|
|
|
|
ProjectGrantMemberProjectIDCol = "project_id"
|
|
|
|
ProjectGrantMemberGrantIDCol = "grant_id"
|
|
|
|
ProjectGrantMemberGrantedOrg = "granted_org"
|
|
|
|
ProjectGrantMemberGrantedOrgRemoved = "granted_org_removed"
|
2022-03-23 08:02:39 +00:00
|
|
|
)
|
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
type projectGrantMemberProjection struct {
|
2021-12-02 08:31:44 +00:00
|
|
|
crdb.StatementHandler
|
|
|
|
}
|
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
func newProjectGrantMemberProjection(ctx context.Context, config crdb.StatementHandlerConfig) *projectGrantMemberProjection {
|
|
|
|
p := new(projectGrantMemberProjection)
|
2021-12-02 08:31:44 +00:00
|
|
|
config.ProjectionName = ProjectGrantMemberProjectionTable
|
|
|
|
config.Reducers = p.reducers()
|
2022-03-23 08:02:39 +00:00
|
|
|
config.InitCheck = crdb.NewTableCheck(
|
|
|
|
crdb.NewTable(
|
|
|
|
append(memberColumns,
|
|
|
|
crdb.NewColumn(ProjectGrantMemberProjectIDCol, crdb.ColumnTypeText),
|
|
|
|
crdb.NewColumn(ProjectGrantMemberGrantIDCol, crdb.ColumnTypeText),
|
2022-11-30 16:01:17 +00:00
|
|
|
crdb.NewColumn(ProjectGrantMemberGrantedOrg, crdb.ColumnTypeText),
|
|
|
|
crdb.NewColumn(ProjectGrantMemberGrantedOrgRemoved, crdb.ColumnTypeBool, crdb.Default(false)),
|
2022-03-23 08:02:39 +00:00
|
|
|
),
|
|
|
|
crdb.NewPrimaryKey(MemberInstanceID, ProjectGrantMemberProjectIDCol, ProjectGrantMemberGrantIDCol, 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})),
|
|
|
|
crdb.WithIndex(crdb.NewIndex("granted_org_removed", []string{ProjectGrantMemberGrantedOrgRemoved})),
|
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 *projectGrantMemberProjection) reducers() []handler.AggregateReducer {
|
2021-12-02 08:31:44 +00:00
|
|
|
return []handler.AggregateReducer{
|
|
|
|
{
|
|
|
|
Aggregate: project.AggregateType,
|
|
|
|
EventRedusers: []handler.EventReducer{
|
|
|
|
{
|
|
|
|
Event: project.GrantMemberAddedType,
|
|
|
|
Reduce: p.reduceAdded,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: project.GrantMemberChangedType,
|
|
|
|
Reduce: p.reduceChanged,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: project.GrantMemberCascadeRemovedType,
|
|
|
|
Reduce: p.reduceCascadeRemoved,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: project.GrantMemberRemovedType,
|
|
|
|
Reduce: p.reduceRemoved,
|
|
|
|
},
|
2022-01-03 10:50:38 +00:00
|
|
|
{
|
|
|
|
Event: project.ProjectRemovedType,
|
|
|
|
Reduce: p.reduceProjectRemoved,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: project.GrantRemovedType,
|
|
|
|
Reduce: p.reduceProjectGrantRemoved,
|
|
|
|
},
|
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 *projectGrantMemberProjection) reduceAdded(event eventstore.Event) (*handler.Statement, error) {
|
2021-12-02 08:31:44 +00:00
|
|
|
e, ok := event.(*project.GrantMemberAddedEvent)
|
|
|
|
if !ok {
|
2022-03-23 08:02:39 +00:00
|
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-0EBQf", "reduce.wrong.event.type %s", project.GrantMemberAddedType)
|
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
|
|
|
|
}
|
|
|
|
grantedOrg, err := getGrantedOrgOfGrantedProject(ctx, p.Eventstore, e.Aggregate().InstanceID, e.Aggregate().ID, e.GrantID)
|
|
|
|
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(ProjectGrantMemberProjectIDCol, e.Aggregate().ID),
|
|
|
|
withMemberCol(ProjectGrantMemberGrantIDCol, e.GrantID),
|
2022-11-30 16:01:17 +00:00
|
|
|
withMemberCol(ProjectGrantMemberGrantedOrg, grantedOrg),
|
|
|
|
withMemberCol(ProjectGrantMemberGrantedOrgRemoved, false),
|
2021-12-02 08:31:44 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
func (p *projectGrantMemberProjection) reduceChanged(event eventstore.Event) (*handler.Statement, error) {
|
2021-12-02 08:31:44 +00:00
|
|
|
e, ok := event.(*project.GrantMemberChangedEvent)
|
|
|
|
if !ok {
|
2022-03-23 08:02:39 +00:00
|
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-YX5Tk", "reduce.wrong.event.type %s", project.GrantMemberChangedType)
|
2021-12-02 08:31:44 +00:00
|
|
|
}
|
|
|
|
return reduceMemberChanged(
|
|
|
|
*member.NewMemberChangedEvent(&e.BaseEvent, e.UserID, e.Roles...),
|
|
|
|
withMemberCond(ProjectGrantMemberProjectIDCol, e.Aggregate().ID),
|
|
|
|
withMemberCond(ProjectGrantMemberGrantIDCol, e.GrantID),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
func (p *projectGrantMemberProjection) reduceCascadeRemoved(event eventstore.Event) (*handler.Statement, error) {
|
2021-12-02 08:31:44 +00:00
|
|
|
e, ok := event.(*project.GrantMemberCascadeRemovedEvent)
|
|
|
|
if !ok {
|
2022-03-23 08:02:39 +00:00
|
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-adnHG", "reduce.wrong.event.type %s", project.GrantMemberCascadeRemovedType)
|
2021-12-02 08:31:44 +00:00
|
|
|
}
|
|
|
|
return reduceMemberCascadeRemoved(
|
|
|
|
*member.NewCascadeRemovedEvent(&e.BaseEvent, e.UserID),
|
|
|
|
withMemberCond(ProjectGrantMemberProjectIDCol, e.Aggregate().ID),
|
|
|
|
withMemberCond(ProjectGrantMemberGrantIDCol, e.GrantID),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
func (p *projectGrantMemberProjection) reduceRemoved(event eventstore.Event) (*handler.Statement, error) {
|
2021-12-02 08:31:44 +00:00
|
|
|
e, ok := event.(*project.GrantMemberRemovedEvent)
|
|
|
|
if !ok {
|
2022-03-23 08:02:39 +00:00
|
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-MGNnA", "reduce.wrong.event.type %s", project.GrantMemberRemovedType)
|
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(ProjectGrantMemberProjectIDCol, e.Aggregate().ID),
|
|
|
|
withMemberCond(ProjectGrantMemberGrantIDCol, e.GrantID),
|
|
|
|
)
|
|
|
|
}
|
2021-12-16 13:25:38 +00:00
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
func (p *projectGrantMemberProjection) 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-rufJr", "reduce.wrong.event.type %s", user.UserRemovedType)
|
2021-12-16 13:25:38 +00:00
|
|
|
}
|
|
|
|
return reduceMemberRemoved(e, withMemberCond(MemberUserIDCol, e.Aggregate().ID))
|
|
|
|
}
|
|
|
|
|
2022-10-20 12:36:52 +00:00
|
|
|
func (p *projectGrantMemberProjection) reduceInstanceRemoved(event eventstore.Event) (*handler.Statement, error) {
|
|
|
|
e, ok := event.(*instance.InstanceRemovedEvent)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-Z2p6o", "reduce.wrong.event.type %s", instance.InstanceRemovedEventType)
|
|
|
|
}
|
|
|
|
return reduceMemberRemoved(e, withMemberCond(MemberInstanceID, e.Aggregate().ID))
|
|
|
|
}
|
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
func (p *projectGrantMemberProjection) 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-Zzp6o", "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),
|
|
|
|
crdb.AddUpdateStatement(
|
|
|
|
[]handler.Column{
|
|
|
|
handler.NewCol(MemberChangeDate, e.CreationDate()),
|
|
|
|
handler.NewCol(MemberSequence, e.Sequence()),
|
|
|
|
handler.NewCol(ProjectGrantMemberGrantedOrgRemoved, true),
|
|
|
|
},
|
|
|
|
[]handler.Condition{
|
|
|
|
handler.NewCond(ProjectGrantColumnInstanceID, e.Aggregate().InstanceID),
|
|
|
|
handler.NewCond(ProjectGrantMemberGrantedOrg, e.Aggregate().ID),
|
|
|
|
},
|
|
|
|
),
|
|
|
|
), nil
|
2021-12-16 13:25:38 +00:00
|
|
|
}
|
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
func (p *projectGrantMemberProjection) 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-JLODy", "reduce.wrong.event.type %s", project.ProjectRemovedType)
|
2021-12-16 13:25:38 +00:00
|
|
|
}
|
|
|
|
return reduceMemberRemoved(e, withMemberCond(ProjectGrantMemberProjectIDCol, e.Aggregate().ID))
|
|
|
|
}
|
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
func (p *projectGrantMemberProjection) reduceProjectGrantRemoved(event eventstore.Event) (*handler.Statement, error) {
|
2021-12-16 13:25:38 +00:00
|
|
|
e, ok := event.(*project.GrantRemovedEvent)
|
|
|
|
if !ok {
|
2022-03-23 08:02:39 +00:00
|
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-D1J9R", "reduce.wrong.event.type %s", project.GrantRemovedType)
|
2021-12-16 13:25:38 +00:00
|
|
|
}
|
|
|
|
return reduceMemberRemoved(e,
|
|
|
|
withMemberCond(ProjectGrantMemberGrantIDCol, e.GrantID),
|
|
|
|
withMemberCond(ProjectGrantMemberProjectIDCol, e.Aggregate().ID),
|
|
|
|
)
|
|
|
|
}
|