2021-11-02 10:08:47 +01: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"
|
2021-12-16 14:25:38 +01:00
|
|
|
"github.com/caos/zitadel/internal/repository/iam"
|
|
|
|
"github.com/caos/zitadel/internal/repository/org"
|
2021-11-02 10:08:47 +01:00
|
|
|
"github.com/caos/zitadel/internal/repository/user"
|
|
|
|
)
|
|
|
|
|
|
|
|
type IDPUserLinkProjection struct {
|
|
|
|
crdb.StatementHandler
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewIDPUserLinkProjection(ctx context.Context, config crdb.StatementHandlerConfig) *IDPUserLinkProjection {
|
|
|
|
p := &IDPUserLinkProjection{}
|
|
|
|
config.ProjectionName = IDPUserLinkTable
|
|
|
|
config.Reducers = p.reducers()
|
|
|
|
p.StatementHandler = crdb.NewStatementHandler(ctx, config)
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *IDPUserLinkProjection) reducers() []handler.AggregateReducer {
|
|
|
|
return []handler.AggregateReducer{
|
|
|
|
{
|
|
|
|
Aggregate: user.AggregateType,
|
|
|
|
EventRedusers: []handler.EventReducer{
|
|
|
|
{
|
|
|
|
Event: user.UserIDPLinkAddedType,
|
|
|
|
Reduce: p.reduceAdded,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: user.UserIDPLinkCascadeRemovedType,
|
|
|
|
Reduce: p.reduceCascadeRemoved,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: user.UserIDPLinkRemovedType,
|
|
|
|
Reduce: p.reduceRemoved,
|
|
|
|
},
|
2021-12-16 14:25:38 +01:00
|
|
|
{
|
|
|
|
Event: user.UserRemovedType,
|
|
|
|
Reduce: p.reduceUserRemoved,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Aggregate: org.AggregateType,
|
|
|
|
EventRedusers: []handler.EventReducer{
|
|
|
|
{
|
|
|
|
Event: org.IDPConfigRemovedEventType,
|
|
|
|
Reduce: p.reduceIDPConfigRemoved,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: org.OrgRemovedEventType,
|
|
|
|
Reduce: p.reduceOrgRemoved,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Aggregate: iam.AggregateType,
|
|
|
|
EventRedusers: []handler.EventReducer{
|
|
|
|
{
|
|
|
|
Event: iam.IDPConfigRemovedEventType,
|
|
|
|
Reduce: p.reduceIDPConfigRemoved,
|
|
|
|
},
|
2021-11-02 10:08:47 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2021-12-07 08:33:52 +01:00
|
|
|
IDPUserLinkTable = "zitadel.projections.idp_user_links"
|
2021-11-02 10:08:47 +01:00
|
|
|
IDPUserLinkIDPIDCol = "idp_id"
|
|
|
|
IDPUserLinkUserIDCol = "user_id"
|
|
|
|
IDPUserLinkExternalUserIDCol = "external_user_id"
|
|
|
|
IDPUserLinkCreationDateCol = "creation_date"
|
|
|
|
IDPUserLinkChangeDateCol = "change_date"
|
|
|
|
IDPUserLinkSequenceCol = "sequence"
|
|
|
|
IDPUserLinkResourceOwnerCol = "resource_owner"
|
|
|
|
IDPUserLinkDisplayNameCol = "display_name"
|
|
|
|
)
|
|
|
|
|
2022-01-03 09:19:07 +01:00
|
|
|
func (p *IDPUserLinkProjection) reduceAdded(event eventstore.Event) (*handler.Statement, error) {
|
2021-11-02 10:08:47 +01:00
|
|
|
e, ok := event.(*user.UserIDPLinkAddedEvent)
|
|
|
|
if !ok {
|
|
|
|
logging.LogWithFields("HANDL-v2qC3", "seq", event.Sequence(), "expectedType", user.UserIDPLinkAddedType).Error("wrong event type")
|
|
|
|
return nil, errors.ThrowInvalidArgument(nil, "HANDL-DpmXq", "reduce.wrong.event.type")
|
|
|
|
}
|
2021-12-16 14:25:38 +01:00
|
|
|
|
2021-11-02 10:08:47 +01:00
|
|
|
return crdb.NewCreateStatement(e,
|
|
|
|
[]handler.Column{
|
|
|
|
handler.NewCol(IDPUserLinkIDPIDCol, e.IDPConfigID),
|
|
|
|
handler.NewCol(IDPUserLinkUserIDCol, e.Aggregate().ID),
|
|
|
|
handler.NewCol(IDPUserLinkExternalUserIDCol, e.ExternalUserID),
|
|
|
|
handler.NewCol(IDPUserLinkCreationDateCol, e.CreationDate()),
|
|
|
|
handler.NewCol(IDPUserLinkChangeDateCol, e.CreationDate()),
|
|
|
|
handler.NewCol(IDPUserLinkSequenceCol, e.Sequence()),
|
|
|
|
handler.NewCol(IDPUserLinkResourceOwnerCol, e.Aggregate().ResourceOwner),
|
|
|
|
handler.NewCol(IDPUserLinkDisplayNameCol, e.DisplayName),
|
|
|
|
},
|
|
|
|
), nil
|
|
|
|
}
|
|
|
|
|
2022-01-03 09:19:07 +01:00
|
|
|
func (p *IDPUserLinkProjection) reduceRemoved(event eventstore.Event) (*handler.Statement, error) {
|
2021-11-02 10:08:47 +01:00
|
|
|
e, ok := event.(*user.UserIDPLinkRemovedEvent)
|
|
|
|
if !ok {
|
|
|
|
logging.LogWithFields("HANDL-zX5m9", "seq", event.Sequence(), "expectedType", user.UserIDPLinkRemovedType).Error("wrong event type")
|
|
|
|
return nil, errors.ThrowInvalidArgument(nil, "HANDL-AZmfJ", "reduce.wrong.event.type")
|
|
|
|
}
|
2021-12-16 14:25:38 +01:00
|
|
|
|
2021-11-02 10:08:47 +01:00
|
|
|
return crdb.NewDeleteStatement(e,
|
|
|
|
[]handler.Condition{
|
|
|
|
handler.NewCond(IDPUserLinkIDPIDCol, e.IDPConfigID),
|
|
|
|
handler.NewCond(IDPUserLinkUserIDCol, e.Aggregate().ID),
|
|
|
|
handler.NewCond(IDPUserLinkExternalUserIDCol, e.ExternalUserID),
|
|
|
|
},
|
|
|
|
), nil
|
|
|
|
}
|
|
|
|
|
2022-01-03 09:19:07 +01:00
|
|
|
func (p *IDPUserLinkProjection) reduceCascadeRemoved(event eventstore.Event) (*handler.Statement, error) {
|
2021-11-02 10:08:47 +01:00
|
|
|
e, ok := event.(*user.UserIDPLinkCascadeRemovedEvent)
|
|
|
|
if !ok {
|
|
|
|
logging.LogWithFields("HANDL-I0s2H", "seq", event.Sequence(), "expectedType", user.UserIDPLinkCascadeRemovedType).Error("wrong event type")
|
|
|
|
return nil, errors.ThrowInvalidArgument(nil, "HANDL-jQpv9", "reduce.wrong.event.type")
|
|
|
|
}
|
2021-12-16 14:25:38 +01:00
|
|
|
|
2021-11-02 10:08:47 +01:00
|
|
|
return crdb.NewDeleteStatement(e,
|
|
|
|
[]handler.Condition{
|
|
|
|
handler.NewCond(IDPUserLinkIDPIDCol, e.IDPConfigID),
|
|
|
|
handler.NewCond(IDPUserLinkUserIDCol, e.Aggregate().ID),
|
|
|
|
handler.NewCond(IDPUserLinkExternalUserIDCol, e.ExternalUserID),
|
|
|
|
},
|
|
|
|
), nil
|
|
|
|
}
|
2021-12-16 14:25:38 +01:00
|
|
|
|
2022-01-03 09:19:07 +01:00
|
|
|
func (p *IDPUserLinkProjection) reduceOrgRemoved(event eventstore.Event) (*handler.Statement, error) {
|
2021-12-16 14:25:38 +01:00
|
|
|
e, ok := event.(*org.OrgRemovedEvent)
|
|
|
|
if !ok {
|
|
|
|
logging.LogWithFields("HANDL-zX5m9", "seq", event.Sequence(), "expectedType", org.OrgRemovedEventType).Error("wrong event type")
|
|
|
|
return nil, errors.ThrowInvalidArgument(nil, "HANDL-AZmfJ", "reduce.wrong.event.type")
|
|
|
|
}
|
|
|
|
|
|
|
|
return crdb.NewDeleteStatement(e,
|
|
|
|
[]handler.Condition{
|
|
|
|
handler.NewCond(IDPUserLinkResourceOwnerCol, e.Aggregate().ID),
|
|
|
|
},
|
|
|
|
), nil
|
|
|
|
}
|
|
|
|
|
2022-01-03 09:19:07 +01:00
|
|
|
func (p *IDPUserLinkProjection) reduceUserRemoved(event eventstore.Event) (*handler.Statement, error) {
|
2021-12-16 14:25:38 +01:00
|
|
|
e, ok := event.(*user.UserRemovedEvent)
|
|
|
|
if !ok {
|
|
|
|
logging.LogWithFields("HANDL-yM6u6", "seq", event.Sequence(), "expectedType", user.UserRemovedType).Error("wrong event type")
|
|
|
|
return nil, errors.ThrowInvalidArgument(nil, "HANDL-uwlWE", "reduce.wrong.event.type")
|
|
|
|
}
|
|
|
|
|
|
|
|
return crdb.NewDeleteStatement(e,
|
|
|
|
[]handler.Condition{
|
|
|
|
handler.NewCond(IDPUserLinkUserIDCol, e.Aggregate().ID),
|
|
|
|
},
|
|
|
|
), nil
|
|
|
|
}
|
|
|
|
|
2022-01-03 09:19:07 +01:00
|
|
|
func (p *IDPUserLinkProjection) reduceIDPConfigRemoved(event eventstore.Event) (*handler.Statement, error) {
|
2021-12-16 14:25:38 +01:00
|
|
|
var idpID string
|
|
|
|
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *org.IDPConfigRemovedEvent:
|
|
|
|
idpID = e.ConfigID
|
|
|
|
case *iam.IDPConfigRemovedEvent:
|
|
|
|
idpID = e.ConfigID
|
|
|
|
default:
|
|
|
|
logging.LogWithFields("HANDL-7lZaf", "seq", event.Sequence(), "expectedTypes", []eventstore.EventType{org.IDPConfigRemovedEventType, iam.IDPConfigRemovedEventType}).Error("wrong event type")
|
|
|
|
return nil, errors.ThrowInvalidArgument(nil, "HANDL-iCKSj", "reduce.wrong.event.type")
|
|
|
|
}
|
|
|
|
|
|
|
|
return crdb.NewDeleteStatement(event,
|
|
|
|
[]handler.Condition{
|
|
|
|
handler.NewCond(IDPUserLinkIDPIDCol, idpID),
|
|
|
|
handler.NewCond(IDPUserLinkResourceOwnerCol, event.Aggregate().ResourceOwner),
|
|
|
|
},
|
|
|
|
), nil
|
|
|
|
}
|