zitadel/internal/query/projection/user_metadata.go

129 lines
4.3 KiB
Go
Raw Normal View History

package projection
import (
"context"
"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"
)
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"
)
type userMetadataProjection struct {
crdb.StatementHandler
}
func newUserMetadataProjection(ctx context.Context, config crdb.StatementHandlerConfig) *userMetadataProjection {
p := new(userMetadataProjection)
config.ProjectionName = UserMetadataProjectionTable
config.Reducers = p.reducers()
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),
feat(cli): setup (#3267) * commander * commander * selber! * move to packages * fix(errors): implement Is interface * test: command * test: commands * add init steps * setup tenant * add default step yaml * possibility to set password * merge v2 into v2-commander * fix: rename iam command side to instance * fix: rename iam command side to instance * fix: rename iam command side to instance * fix: rename iam command side to instance * fix: search query builder can filter events in memory * fix: filters for add member * fix(setup): add `ExternalSecure` to config * chore: name iam to instance * fix: matching * remove unsued func * base url * base url * test(command): filter funcs * test: commands * fix: rename orgiampolicy to domain policy * start from init * commands * config * fix indexes and add constraints * fixes * fix: merge conflicts * fix: protos * fix: md files * setup * add deprecated org iam policy again * typo * fix search query * fix filter * Apply suggestions from code review * remove custom org from org setup * add todos for verification * change apps creation * simplify package structure * fix error * move preparation helper for tests * fix unique constraints * fix config mapping in setup * fix error handling in encryption_keys.go * fix projection config * fix query from old views to projection * fix setup of mgmt api * set iam project and fix instance projection * imports Co-authored-by: Livio Amstutz <livio.a@gmail.com> Co-authored-by: fabi <fabienne.gerschwiler@gmail.com>
2022-03-28 08:05:09 +00:00
crdb.WithIndex(crdb.NewIndex("ro_idx", []string{UserGrantResourceOwner})),
),
)
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 {
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-Ghn52", "reduce.wrong.event.type %s", user.MetadataSetType)
}
return crdb.NewUpsertStatement(
e,
[]handler.Column{
handler.NewCol(UserMetadataColumnUserID, e.Aggregate().ID),
handler.NewCol(UserMetadataColumnResourceOwner, e.Aggregate().ResourceOwner),
handler.NewCol(UserMetadataColumnInstanceID, e.Aggregate().InstanceID),
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 {
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-Bm542", "reduce.wrong.event.type %s", user.MetadataRemovedType)
}
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:
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-Bmnf2", "reduce.wrong.event.type %v", []eventstore.EventType{user.MetadataRemovedAllType, user.UserRemovedType})
}
return crdb.NewDeleteStatement(
event,
[]handler.Condition{
handler.NewCond(UserMetadataColumnUserID, event.Aggregate().ID),
},
), nil
}