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"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/user"
|
2022-01-18 13:15:00 +00:00
|
|
|
)
|
|
|
|
|
2022-03-23 08:02:39 +00:00
|
|
|
const (
|
|
|
|
UserMetadataProjectionTable = "projections.user_metadata"
|
|
|
|
|
|
|
|
UserMetadataColumnUserID = "user_id"
|
|
|
|
UserMetadataColumnCreationDate = "creation_date"
|
|
|
|
UserMetadataColumnChangeDate = "change_date"
|
|
|
|
UserMetadataColumnSequence = "sequence"
|
|
|
|
UserMetadataColumnResourceOwner = "resource_owner"
|
|
|
|
UserMetadataColumnInstanceID = "instance_id"
|
|
|
|
UserMetadataColumnKey = "key"
|
|
|
|
UserMetadataColumnValue = "value"
|
|
|
|
)
|
|
|
|
|
2022-01-18 13:15:00 +00:00
|
|
|
type UserMetadataProjection struct {
|
|
|
|
crdb.StatementHandler
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewUserMetadataProjection(ctx context.Context, config crdb.StatementHandlerConfig) *UserMetadataProjection {
|
2022-02-25 15:05:06 +00:00
|
|
|
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()),
|
|
|
|
},
|
|
|
|
crdb.NewPrimaryKey(UserMetadataColumnInstanceID, UserMetadataColumnUserID),
|
2022-03-28 08:05:09 +00:00
|
|
|
crdb.WithIndex(crdb.NewIndex("ro_idx", []string{UserGrantResourceOwner})),
|
2022-03-23 08:02:39 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
|
2022-01-18 13:15:00 +00:00
|
|
|
p.StatementHandler = crdb.NewStatementHandler(ctx, config)
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *UserMetadataProjection) reducers() []handler.AggregateReducer {
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *UserMetadataProjection) reduceMetadataSet(event eventstore.Event) (*handler.Statement, error) {
|
|
|
|
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{
|
|
|
|
handler.NewCol(UserMetadataColumnUserID, e.Aggregate().ID),
|
|
|
|
handler.NewCol(UserMetadataColumnResourceOwner, e.Aggregate().ResourceOwner),
|
2022-03-23 08:02:39 +00:00
|
|
|
handler.NewCol(UserMetadataColumnInstanceID, e.Aggregate().InstanceID),
|
2022-01-18 13:15:00 +00:00
|
|
|
handler.NewCol(UserMetadataColumnCreationDate, e.CreationDate()),
|
|
|
|
handler.NewCol(UserMetadataColumnChangeDate, e.CreationDate()),
|
|
|
|
handler.NewCol(UserMetadataColumnSequence, e.Sequence()),
|
|
|
|
handler.NewCol(UserMetadataColumnKey, e.Key),
|
|
|
|
handler.NewCol(UserMetadataColumnValue, e.Value),
|
|
|
|
},
|
|
|
|
), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *UserMetadataProjection) reduceMetadataRemoved(event eventstore.Event) (*handler.Statement, error) {
|
|
|
|
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),
|
|
|
|
},
|
|
|
|
), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *UserMetadataProjection) reduceMetadataRemovedAll(event eventstore.Event) (*handler.Statement, error) {
|
|
|
|
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),
|
|
|
|
},
|
|
|
|
), nil
|
|
|
|
}
|