2022-01-18 13:15:00 +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-11-30 16:01:17 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/org"
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/user"
|
2022-01-18 13:15:00 +00:00
|
|
|
)
|
|
|
|
|
2022-03-23 08:02:39 +00:00
|
|
|
const (
|
2022-11-30 16:01:17 +00:00
|
|
|
UserMetadataProjectionTable = "projections.user_metadata4"
|
2022-03-23 08:02:39 +00:00
|
|
|
|
|
|
|
UserMetadataColumnUserID = "user_id"
|
|
|
|
UserMetadataColumnCreationDate = "creation_date"
|
|
|
|
UserMetadataColumnChangeDate = "change_date"
|
|
|
|
UserMetadataColumnSequence = "sequence"
|
|
|
|
UserMetadataColumnResourceOwner = "resource_owner"
|
|
|
|
UserMetadataColumnInstanceID = "instance_id"
|
|
|
|
UserMetadataColumnKey = "key"
|
|
|
|
UserMetadataColumnValue = "value"
|
2022-11-30 16:01:17 +00:00
|
|
|
UserMetadataColumnOwnerRemoved = "owner_removed"
|
2022-03-23 08:02:39 +00:00
|
|
|
)
|
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
type userMetadataProjection struct {
|
2022-01-18 13:15:00 +00:00
|
|
|
crdb.StatementHandler
|
|
|
|
}
|
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
func newUserMetadataProjection(ctx context.Context, config crdb.StatementHandlerConfig) *userMetadataProjection {
|
|
|
|
p := new(userMetadataProjection)
|
2022-01-18 13:15:00 +00:00
|
|
|
config.ProjectionName = UserMetadataProjectionTable
|
|
|
|
config.Reducers = p.reducers()
|
2022-03-23 08:02:39 +00:00
|
|
|
config.InitCheck = crdb.NewTableCheck(
|
|
|
|
crdb.NewTable([]*crdb.Column{
|
|
|
|
crdb.NewColumn(UserMetadataColumnUserID, crdb.ColumnTypeText),
|
|
|
|
crdb.NewColumn(UserMetadataColumnCreationDate, crdb.ColumnTypeTimestamp),
|
|
|
|
crdb.NewColumn(UserMetadataColumnChangeDate, crdb.ColumnTypeTimestamp),
|
|
|
|
crdb.NewColumn(UserMetadataColumnSequence, crdb.ColumnTypeInt64),
|
|
|
|
crdb.NewColumn(UserMetadataColumnResourceOwner, crdb.ColumnTypeText),
|
|
|
|
crdb.NewColumn(UserMetadataColumnInstanceID, crdb.ColumnTypeText),
|
|
|
|
crdb.NewColumn(UserMetadataColumnKey, crdb.ColumnTypeText),
|
|
|
|
crdb.NewColumn(UserMetadataColumnValue, crdb.ColumnTypeBytes, crdb.Nullable()),
|
2022-11-30 16:01:17 +00:00
|
|
|
crdb.NewColumn(UserMetadataColumnOwnerRemoved, crdb.ColumnTypeBool, crdb.Default(false)),
|
2022-03-23 08:02:39 +00:00
|
|
|
},
|
2022-08-03 07:50:23 +00:00
|
|
|
crdb.NewPrimaryKey(UserMetadataColumnInstanceID, UserMetadataColumnUserID, UserMetadataColumnKey),
|
2022-11-30 16:01:17 +00:00
|
|
|
crdb.WithIndex(crdb.NewIndex("resource_owner", []string{UserGrantResourceOwner})),
|
|
|
|
crdb.WithIndex(crdb.NewIndex("owner_removed", []string{UserMetadataColumnOwnerRemoved})),
|
2022-03-23 08:02:39 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
|
2022-01-18 13:15:00 +00:00
|
|
|
p.StatementHandler = crdb.NewStatementHandler(ctx, config)
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
func (p *userMetadataProjection) reducers() []handler.AggregateReducer {
|
2022-01-18 13:15:00 +00:00
|
|
|
return []handler.AggregateReducer{
|
|
|
|
{
|
|
|
|
Aggregate: user.AggregateType,
|
|
|
|
EventRedusers: []handler.EventReducer{
|
|
|
|
{
|
|
|
|
Event: user.MetadataSetType,
|
|
|
|
Reduce: p.reduceMetadataSet,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: user.MetadataRemovedType,
|
|
|
|
Reduce: p.reduceMetadataRemoved,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: user.MetadataRemovedAllType,
|
|
|
|
Reduce: p.reduceMetadataRemovedAll,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: user.UserRemovedType,
|
|
|
|
Reduce: p.reduceMetadataRemovedAll,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-11-30 16:01:17 +00:00
|
|
|
{
|
|
|
|
Aggregate: org.AggregateType,
|
|
|
|
EventRedusers: []handler.EventReducer{
|
|
|
|
{
|
|
|
|
Event: org.OrgRemovedEventType,
|
|
|
|
Reduce: p.reduceOwnerRemoved,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-10-20 12:36:52 +00:00
|
|
|
{
|
|
|
|
Aggregate: instance.AggregateType,
|
|
|
|
EventRedusers: []handler.EventReducer{
|
|
|
|
{
|
|
|
|
Event: instance.InstanceRemovedEventType,
|
|
|
|
Reduce: reduceInstanceRemovedHelper(UserMetadataColumnInstanceID),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-01-18 13:15:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
func (p *userMetadataProjection) reduceMetadataSet(event eventstore.Event) (*handler.Statement, error) {
|
2022-01-18 13:15:00 +00:00
|
|
|
e, ok := event.(*user.MetadataSetEvent)
|
|
|
|
if !ok {
|
2022-03-23 08:02:39 +00:00
|
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-Ghn52", "reduce.wrong.event.type %s", user.MetadataSetType)
|
2022-01-18 13:15:00 +00:00
|
|
|
}
|
|
|
|
return crdb.NewUpsertStatement(
|
|
|
|
e,
|
|
|
|
[]handler.Column{
|
2022-08-31 07:52:43 +00:00
|
|
|
handler.NewCol(UserMetadataColumnInstanceID, nil),
|
|
|
|
handler.NewCol(UserMetadataColumnUserID, nil),
|
|
|
|
handler.NewCol(UserMetadataColumnKey, e.Key),
|
|
|
|
},
|
|
|
|
[]handler.Column{
|
|
|
|
handler.NewCol(UserMetadataColumnInstanceID, e.Aggregate().InstanceID),
|
2022-01-18 13:15:00 +00:00
|
|
|
handler.NewCol(UserMetadataColumnUserID, e.Aggregate().ID),
|
2022-08-31 07:52:43 +00:00
|
|
|
handler.NewCol(UserMetadataColumnKey, e.Key),
|
2022-01-18 13:15:00 +00:00
|
|
|
handler.NewCol(UserMetadataColumnResourceOwner, e.Aggregate().ResourceOwner),
|
|
|
|
handler.NewCol(UserMetadataColumnCreationDate, e.CreationDate()),
|
|
|
|
handler.NewCol(UserMetadataColumnChangeDate, e.CreationDate()),
|
|
|
|
handler.NewCol(UserMetadataColumnSequence, e.Sequence()),
|
|
|
|
handler.NewCol(UserMetadataColumnValue, e.Value),
|
|
|
|
},
|
|
|
|
), nil
|
|
|
|
}
|
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
func (p *userMetadataProjection) reduceMetadataRemoved(event eventstore.Event) (*handler.Statement, error) {
|
2022-01-18 13:15:00 +00:00
|
|
|
e, ok := event.(*user.MetadataRemovedEvent)
|
|
|
|
if !ok {
|
2022-03-23 08:02:39 +00:00
|
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-Bm542", "reduce.wrong.event.type %s", user.MetadataRemovedType)
|
2022-01-18 13:15:00 +00:00
|
|
|
}
|
|
|
|
return crdb.NewDeleteStatement(
|
|
|
|
e,
|
|
|
|
[]handler.Condition{
|
|
|
|
handler.NewCond(UserMetadataColumnUserID, e.Aggregate().ID),
|
|
|
|
handler.NewCond(UserMetadataColumnKey, e.Key),
|
2022-11-10 10:59:33 +00:00
|
|
|
handler.NewCond(UserAuthMethodInstanceIDCol, e.Aggregate().InstanceID),
|
2022-01-18 13:15:00 +00:00
|
|
|
},
|
|
|
|
), nil
|
|
|
|
}
|
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
func (p *userMetadataProjection) reduceMetadataRemovedAll(event eventstore.Event) (*handler.Statement, error) {
|
2022-01-18 13:15:00 +00:00
|
|
|
switch event.(type) {
|
|
|
|
case *user.MetadataRemovedAllEvent,
|
|
|
|
*user.UserRemovedEvent:
|
|
|
|
//ok
|
|
|
|
default:
|
2022-03-23 08:02:39 +00:00
|
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-Bmnf2", "reduce.wrong.event.type %v", []eventstore.EventType{user.MetadataRemovedAllType, user.UserRemovedType})
|
2022-01-18 13:15:00 +00:00
|
|
|
}
|
|
|
|
return crdb.NewDeleteStatement(
|
|
|
|
event,
|
|
|
|
[]handler.Condition{
|
|
|
|
handler.NewCond(UserMetadataColumnUserID, event.Aggregate().ID),
|
2022-11-10 10:59:33 +00:00
|
|
|
handler.NewCond(UserAuthMethodInstanceIDCol, event.Aggregate().InstanceID),
|
2022-01-18 13:15:00 +00:00
|
|
|
},
|
|
|
|
), nil
|
|
|
|
}
|
2022-11-30 16:01:17 +00:00
|
|
|
|
|
|
|
func (p *userMetadataProjection) reduceOwnerRemoved(event eventstore.Event) (*handler.Statement, error) {
|
|
|
|
e, ok := event.(*org.OrgRemovedEvent)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "PROJE-oqwul", "reduce.wrong.event.type %s", org.OrgRemovedEventType)
|
|
|
|
}
|
|
|
|
|
|
|
|
return crdb.NewUpdateStatement(
|
|
|
|
e,
|
|
|
|
[]handler.Column{
|
|
|
|
handler.NewCol(UserMetadataColumnChangeDate, e.CreationDate()),
|
|
|
|
handler.NewCol(UserMetadataColumnSequence, e.Sequence()),
|
|
|
|
handler.NewCol(UserMetadataColumnOwnerRemoved, true),
|
|
|
|
},
|
|
|
|
[]handler.Condition{
|
|
|
|
handler.NewCond(UserMetadataColumnInstanceID, e.Aggregate().InstanceID),
|
|
|
|
handler.NewCond(UserMetadataColumnResourceOwner, e.Aggregate().ID),
|
|
|
|
},
|
|
|
|
), nil
|
|
|
|
}
|