2021-12-02 08:31:44 +00:00
|
|
|
package projection
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/caos/logging"
|
|
|
|
|
|
|
|
"github.com/caos/zitadel/internal/errors"
|
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
|
|
|
"github.com/caos/zitadel/internal/eventstore/handler"
|
|
|
|
"github.com/caos/zitadel/internal/eventstore/handler/crdb"
|
|
|
|
"github.com/caos/zitadel/internal/repository/member"
|
2021-12-16 13:25:38 +00:00
|
|
|
"github.com/caos/zitadel/internal/repository/org"
|
2021-12-02 08:31:44 +00:00
|
|
|
"github.com/caos/zitadel/internal/repository/project"
|
2021-12-16 13:25:38 +00:00
|
|
|
"github.com/caos/zitadel/internal/repository/user"
|
2021-12-02 08:31:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ProjectGrantMemberProjection struct {
|
|
|
|
crdb.StatementHandler
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
ProjectGrantMemberProjectionTable = "zitadel.projections.project_grant_members"
|
|
|
|
)
|
|
|
|
|
|
|
|
func NewProjectGrantMemberProjection(ctx context.Context, config crdb.StatementHandlerConfig) *ProjectGrantMemberProjection {
|
2022-02-25 15:05:06 +00:00
|
|
|
p := new(ProjectGrantMemberProjection)
|
2021-12-02 08:31:44 +00:00
|
|
|
config.ProjectionName = ProjectGrantMemberProjectionTable
|
|
|
|
config.Reducers = p.reducers()
|
|
|
|
p.StatementHandler = crdb.NewStatementHandler(ctx, config)
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ProjectGrantMemberProjection) reducers() []handler.AggregateReducer {
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-12-02 08:31:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type ProjectGrantMemberColumn string
|
|
|
|
|
|
|
|
const (
|
|
|
|
ProjectGrantMemberProjectIDCol = "project_id"
|
|
|
|
ProjectGrantMemberGrantIDCol = "grant_id"
|
|
|
|
)
|
|
|
|
|
2022-01-03 08:19:07 +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 {
|
|
|
|
logging.LogWithFields("HANDL-csr8B", "seq", event.Sequence(), "expectedType", project.GrantMemberAddedType).Error("wrong event type")
|
|
|
|
return nil, errors.ThrowInvalidArgument(nil, "HANDL-0EBQf", "reduce.wrong.event.type")
|
|
|
|
}
|
|
|
|
return reduceMemberAdded(
|
|
|
|
*member.NewMemberAddedEvent(&e.BaseEvent, e.UserID, e.Roles...),
|
|
|
|
withMemberCol(ProjectGrantMemberProjectIDCol, e.Aggregate().ID),
|
|
|
|
withMemberCol(ProjectGrantMemberGrantIDCol, e.GrantID),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-01-03 08:19:07 +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 {
|
|
|
|
logging.LogWithFields("HANDL-ZubbI", "seq", event.Sequence(), "expectedType", project.GrantMemberChangedType).Error("wrong event type")
|
|
|
|
return nil, errors.ThrowInvalidArgument(nil, "HANDL-YX5Tk", "reduce.wrong.event.type")
|
|
|
|
}
|
|
|
|
return reduceMemberChanged(
|
|
|
|
*member.NewMemberChangedEvent(&e.BaseEvent, e.UserID, e.Roles...),
|
|
|
|
withMemberCond(ProjectGrantMemberProjectIDCol, e.Aggregate().ID),
|
|
|
|
withMemberCond(ProjectGrantMemberGrantIDCol, e.GrantID),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-01-03 08:19:07 +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 {
|
|
|
|
logging.LogWithFields("HANDL-azx7K", "seq", event.Sequence(), "expectedType", project.GrantMemberCascadeRemovedType).Error("wrong event type")
|
|
|
|
return nil, errors.ThrowInvalidArgument(nil, "HANDL-adnHG", "reduce.wrong.event.type")
|
|
|
|
}
|
|
|
|
return reduceMemberCascadeRemoved(
|
|
|
|
*member.NewCascadeRemovedEvent(&e.BaseEvent, e.UserID),
|
|
|
|
withMemberCond(ProjectGrantMemberProjectIDCol, e.Aggregate().ID),
|
|
|
|
withMemberCond(ProjectGrantMemberGrantIDCol, e.GrantID),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-01-03 08:19:07 +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 {
|
|
|
|
logging.LogWithFields("HANDL-6Z4dH", "seq", event.Sequence(), "expectedType", project.GrantMemberRemovedType).Error("wrong event type")
|
|
|
|
return nil, errors.ThrowInvalidArgument(nil, "HANDL-MGNnA", "reduce.wrong.event.type")
|
|
|
|
}
|
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-01-03 08:19:07 +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 {
|
|
|
|
logging.LogWithFields("HANDL-UVMmT", "seq", event.Sequence(), "expected", user.UserRemovedType).Error("wrong event type")
|
|
|
|
return nil, errors.ThrowInvalidArgument(nil, "HANDL-rufJr", "reduce.wrong.event.type")
|
|
|
|
}
|
|
|
|
return reduceMemberRemoved(e, withMemberCond(MemberUserIDCol, e.Aggregate().ID))
|
|
|
|
}
|
|
|
|
|
2022-01-03 08:19:07 +00:00
|
|
|
func (p *ProjectGrantMemberProjection) reduceOrgRemoved(event eventstore.Event) (*handler.Statement, error) {
|
2021-12-16 13:25:38 +00:00
|
|
|
//TODO: as soon as org deletion is implemented:
|
|
|
|
// Case: The user has resource owner A and project has resource owner B
|
|
|
|
// if org B deleted it works
|
|
|
|
// if org A is deleted, the membership wouldn't be deleted
|
|
|
|
e, ok := event.(*org.OrgRemovedEvent)
|
|
|
|
if !ok {
|
|
|
|
logging.LogWithFields("HANDL-Sq9FV", "seq", event.Sequence(), "expected", org.OrgRemovedEventType).Error("wrong event type")
|
|
|
|
return nil, errors.ThrowInvalidArgument(nil, "HANDL-Zzp6o", "reduce.wrong.event.type")
|
|
|
|
}
|
|
|
|
return reduceMemberRemoved(e, withMemberCond(MemberResourceOwner, e.Aggregate().ID))
|
|
|
|
}
|
|
|
|
|
2022-01-03 08:19:07 +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 {
|
|
|
|
logging.LogWithFields("HANDL-sGmCA", "seq", event.Sequence(), "expected", project.ProjectRemovedType).Error("wrong event type")
|
|
|
|
return nil, errors.ThrowInvalidArgument(nil, "HANDL-JLODy", "reduce.wrong.event.type")
|
|
|
|
}
|
|
|
|
return reduceMemberRemoved(e, withMemberCond(ProjectGrantMemberProjectIDCol, e.Aggregate().ID))
|
|
|
|
}
|
|
|
|
|
2022-01-03 08:19:07 +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 {
|
|
|
|
logging.LogWithFields("HANDL-sHabO", "seq", event.Sequence(), "expected", project.GrantRemovedType).Error("wrong event type")
|
|
|
|
return nil, errors.ThrowInvalidArgument(nil, "HANDL-D1J9R", "reduce.wrong.event.type")
|
|
|
|
}
|
|
|
|
return reduceMemberRemoved(e,
|
|
|
|
withMemberCond(ProjectGrantMemberGrantIDCol, e.GrantID),
|
|
|
|
withMemberCond(ProjectGrantMemberProjectIDCol, e.Aggregate().ID),
|
|
|
|
)
|
|
|
|
}
|