event data search query

This commit is contained in:
adlerhurst 2020-11-23 19:31:12 +01:00
parent 6431fd2ec5
commit f8028f07d0
11 changed files with 148 additions and 37 deletions

View File

@ -48,6 +48,8 @@ const (
OperationLess OperationLess
//OperationIn checks if a stored value matches one of the passed value list //OperationIn checks if a stored value matches one of the passed value list
OperationIn OperationIn
//OperationJSONContains checks if a stored value matches the given json
OperationJSONContains
operationCount operationCount
) )
@ -70,6 +72,8 @@ const (
FieldEditorUser FieldEditorUser
//FieldEventType represents the event type field //FieldEventType represents the event type field
FieldEventType FieldEventType
//FieldEventData represents the event data field
FieldEventData
fieldCount fieldCount
) )

View File

@ -252,6 +252,8 @@ func (db *CRDB) columnName(col repository.Field) string {
return "editor_user" return "editor_user"
case repository.FieldEventType: case repository.FieldEventType:
return "event_type" return "event_type"
case repository.FieldEventData:
return "event_data"
default: default:
return "" return ""
} }
@ -272,6 +274,8 @@ func (db *CRDB) operation(operation repository.Operation) string {
return ">" return ">"
case repository.OperationLess: case repository.OperationLess:
return "<" return "<"
case repository.OperationJSONContains:
return "@>"
} }
return "" return ""
} }

View File

@ -3,6 +3,7 @@ package sql
import ( import (
"context" "context"
"database/sql" "database/sql"
"encoding/json"
"errors" "errors"
"fmt" "fmt"
"strings" "strings"
@ -137,6 +138,10 @@ func prepareCondition(criteria querier, filters []*repository.Filter) (clause st
switch value.(type) { switch value.(type) {
case []bool, []float64, []int64, []string, []repository.AggregateType, []repository.EventType, *[]bool, *[]float64, *[]int64, *[]string, *[]repository.AggregateType, *[]repository.EventType: case []bool, []float64, []int64, []string, []repository.AggregateType, []repository.EventType, *[]bool, *[]float64, *[]int64, *[]string, *[]repository.AggregateType, *[]repository.EventType:
value = pq.Array(value) value = pq.Array(value)
case map[string]interface{}:
var err error
value, err = json.Marshal(value)
logging.Log("SQL-BSsNy").OnError(err).Warn("unable to marshal search value")
} }
clauses[i] = getCondition(criteria, filter) clauses[i] = getCondition(criteria, filter)

View File

@ -15,6 +15,7 @@ type SearchQueryFactory struct {
aggregateIDs []string aggregateIDs []string
eventSequence uint64 eventSequence uint64
eventTypes []EventType eventTypes []EventType
eventData map[string]interface{}
resourceOwner string resourceOwner string
} }
@ -83,6 +84,11 @@ func (factory *SearchQueryFactory) OrderAsc() *SearchQueryFactory {
return factory return factory
} }
func (factory *SearchQueryFactory) EventData(query map[string]interface{}) *SearchQueryFactory {
factory.eventData = query
return factory
}
func (factory *SearchQueryFactory) build() (*repository.SearchQuery, error) { func (factory *SearchQueryFactory) build() (*repository.SearchQuery, error) {
if factory == nil || if factory == nil ||
len(factory.aggregateTypes) < 1 || len(factory.aggregateTypes) < 1 ||
@ -98,6 +104,7 @@ func (factory *SearchQueryFactory) build() (*repository.SearchQuery, error) {
factory.eventSequenceFilter, factory.eventSequenceFilter,
factory.eventTypeFilter, factory.eventTypeFilter,
factory.resourceOwnerFilter, factory.resourceOwnerFilter,
factory.eventDataFilter,
} { } {
if filter := f(); filter != nil { if filter := f(); filter != nil {
if err := filter.Validate(); err != nil { if err := filter.Validate(); err != nil {
@ -159,3 +166,10 @@ func (factory *SearchQueryFactory) resourceOwnerFilter() *repository.Filter {
} }
return repository.NewFilter(repository.FieldResourceOwner, factory.resourceOwner, repository.OperationEquals) return repository.NewFilter(repository.FieldResourceOwner, factory.resourceOwner, repository.OperationEquals)
} }
func (factory *SearchQueryFactory) eventDataFilter() *repository.Filter {
if len(factory.eventData) == 0 {
return nil
}
return repository.NewFilter(repository.FieldEventData, factory.eventData, repository.OperationJSONContains)
}

View File

@ -108,6 +108,16 @@ func readModelToObjectRoot(readModel eventstore.ReadModel) models.ObjectRoot {
} }
} }
func writeModelToObjectRoot(readModel eventstore.WriteModel) models.ObjectRoot {
return models.ObjectRoot{
AggregateID: readModel.AggregateID,
// ChangeDate: readModel.ChangeDate,
// CreationDate: readModel.CreationDate,
ResourceOwner: readModel.ResourceOwner,
Sequence: readModel.ProcessedSequence,
}
}
func readModelToMember(readModel *member.ReadModel) *model.IAMMember { func readModelToMember(readModel *member.ReadModel) *model.IAMMember {
return &model.IAMMember{ return &model.IAMMember{
ObjectRoot: readModelToObjectRoot(readModel.ReadModel), ObjectRoot: readModelToObjectRoot(readModel.ReadModel),
@ -115,3 +125,11 @@ func readModelToMember(readModel *member.ReadModel) *model.IAMMember {
UserID: readModel.UserID, UserID: readModel.UserID,
} }
} }
func writeModelToMember(writeModel *iam.MemberWriteModel) *model.IAMMember {
return &model.IAMMember{
ObjectRoot: writeModelToObjectRoot(writeModel.WriteModel.WriteModel),
Roles: writeModel.Roles,
UserID: writeModel.UserID,
}
}

View File

@ -5,7 +5,9 @@ import (
"github.com/caos/zitadel/internal/errors" "github.com/caos/zitadel/internal/errors"
caos_errs "github.com/caos/zitadel/internal/errors" caos_errs "github.com/caos/zitadel/internal/errors"
"github.com/caos/zitadel/internal/eventstore/v2"
iam_model "github.com/caos/zitadel/internal/iam/model" iam_model "github.com/caos/zitadel/internal/iam/model"
"github.com/caos/zitadel/internal/tracing"
iam_repo "github.com/caos/zitadel/internal/v2/repository/iam" iam_repo "github.com/caos/zitadel/internal/v2/repository/iam"
) )
@ -55,25 +57,20 @@ func (r *Repository) ChangeIAMMember(ctx context.Context, member *iam_model.IAMM
return nil, err return nil, err
} }
changedMember := *existingMember
changedMember.Roles = member.Roles
iam := iam_repo.AggregateFromWriteModel(&existingMember.WriteModel.WriteModel). iam := iam_repo.AggregateFromWriteModel(&existingMember.WriteModel.WriteModel).
PushMemberChanged(ctx, existingMember, &changedMember) PushMemberChangedFromExisting(ctx, existingMember, member.Roles...)
events, err := r.eventstore.PushAggregates(ctx, iam) _, err = r.eventstore.PushAggregates(ctx, iam)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if err = existingMember.AppendEvents(events...); err != nil { updatedMember, err := r.MemberByID(ctx, member.AggregateID, member.UserID)
return nil, err if err != nil {
}
if err = existingMember.Reduce(); err != nil {
return nil, err return nil, err
} }
return nil, nil return readModelToMember(&updatedMember.ReadModel), nil
} }
func (r *Repository) RemoveIAMMember(ctx context.Context, member *iam_model.IAMMember) error { func (r *Repository) RemoveIAMMember(ctx context.Context, member *iam_model.IAMMember) error {
@ -97,3 +94,22 @@ func (r *Repository) RemoveIAMMember(ctx context.Context, member *iam_model.IAMM
return iam.AppendAndReduce(events...) return iam.AppendAndReduce(events...)
} }
func (r *Repository) MemberByID(ctx context.Context, iamID, userID string) (member *iam_repo.MemberReadModel, err error) {
ctx, span := tracing.NewSpan(ctx)
defer func() { span.EndWithError(err) }()
query := eventstore.NewSearchQueryFactory(eventstore.ColumnsEvent, iam_repo.AggregateType).
AggregateIDs(iamID).
EventData(map[string]interface{}{
"userId": userID,
})
member = new(iam_repo.MemberReadModel)
err = r.eventstore.FilterToReducer(ctx, query, member)
if err != nil {
return nil, err
}
return member, nil
}

View File

@ -3,7 +3,6 @@ package iam
import ( import (
"context" "context"
"github.com/caos/logging"
"github.com/caos/zitadel/internal/eventstore/v2" "github.com/caos/zitadel/internal/eventstore/v2"
) )
@ -59,13 +58,16 @@ func (a *Aggregate) PushMemberAdded(ctx context.Context, userID string, roles ..
return a return a
} }
func (a *Aggregate) PushMemberChanged(ctx context.Context, current, changed *MemberWriteModel) *Aggregate { func (a *Aggregate) PushMemberChanged(ctx context.Context, changed *MemberWriteModel) *Aggregate {
e, err := NewMemberChangedEvent(ctx, current, changed) a.Aggregate = *a.PushEvents(NewMemberChangedEvent(ctx, changed.UserID, changed.Roles...))
return a
}
func (a *Aggregate) PushMemberChangedFromExisting(ctx context.Context, current *MemberWriteModel, roles ...string) *Aggregate {
e, err := MemberChangedEventFromExisting(ctx, current, roles...)
if err != nil { if err != nil {
logging.Log("IAM-KH21C").OnError(err).Warn("unable to push member changed")
return a return a
} }
a.Aggregate = *a.PushEvents(e) a.Aggregate = *a.PushEvents(e)
return a return a
} }

View File

@ -86,19 +86,19 @@ func NewMemberAddedEvent(
} }
} }
func NewMemberChangedEvent( func MemberChangedEventFromExisting(
ctx context.Context, ctx context.Context,
current, current *MemberWriteModel,
changed *MemberWriteModel, roles ...string,
) (*MemberChangedEvent, error) { ) (*MemberChangedEvent, error) {
m, err := member.NewChangedEvent( m, err := member.ChangeEventFromExisting(
eventstore.NewBaseEventForPush( eventstore.NewBaseEventForPush(
ctx, ctx,
MemberChangedEventType, MemberChangedEventType,
), ),
&current.WriteModel, &current.WriteModel,
&changed.WriteModel, roles...,
) )
if err != nil { if err != nil {
return nil, err return nil, err
@ -109,6 +109,24 @@ func NewMemberChangedEvent(
}, nil }, nil
} }
func NewMemberChangedEvent(
ctx context.Context,
userID string,
roles ...string,
) *MemberChangedEvent {
return &MemberChangedEvent{
ChangedEvent: *member.NewChangedEvent(
eventstore.NewBaseEventForPush(
ctx,
MemberChangedEventType,
),
userID,
roles...,
),
}
}
func NewMemberRemovedEvent( func NewMemberRemovedEvent(
ctx context.Context, ctx context.Context,
userID string, userID string,

View File

@ -31,25 +31,21 @@ func (e *ChangedEvent) Data() interface{} {
return e return e
} }
func NewChangedEvent( func ChangeEventFromExisting(
base *eventstore.BaseEvent, base *eventstore.BaseEvent,
current, current *WriteModel,
changed *WriteModel, roles ...string,
) (*ChangedEvent, error) { ) (*ChangedEvent, error) {
change := &ChangedEvent{ change := &ChangedEvent{
BaseEvent: *base, BaseEvent: *base,
} UserID: current.UserID,
if current.UserID != changed.UserID {
change.UserID = changed.UserID
change.hasChanged = true
} }
sort.Strings(current.Roles) sort.Strings(current.Roles)
sort.Strings(changed.Roles) sort.Strings(roles)
if !reflect.DeepEqual(current.Roles, changed.Roles) { if !reflect.DeepEqual(current.Roles, roles) {
change.Roles = changed.Roles change.Roles = roles
change.hasChanged = true change.hasChanged = true
} }
@ -58,7 +54,19 @@ func NewChangedEvent(
} }
return change, nil return change, nil
}
func NewChangedEvent(
base *eventstore.BaseEvent,
userID string,
roles ...string,
) *ChangedEvent {
return &ChangedEvent{
BaseEvent: *base,
UserID: userID,
Roles: roles,
}
} }
func ChangedEventMapper(event *repository.Event) (eventstore.EventReader, error) { func ChangedEventMapper(event *repository.Event) (eventstore.EventReader, error) {

View File

@ -48,6 +48,9 @@ func (wm *WriteModel) Reduce() error {
wm.UserID = e.UserID wm.UserID = e.UserID
wm.Roles = e.Roles wm.Roles = e.Roles
case *RemovedEvent: case *RemovedEvent:
if e.UserID != wm.userID {
continue
}
wm.Roles = nil wm.Roles = nil
wm.IsRemoved = true wm.IsRemoved = true
} }

View File

@ -92,28 +92,47 @@ func NewMemberAddedEvent(
} }
} }
func NewMemberChangedEvent( func MemberChangedEventFromExisting(
ctx context.Context, ctx context.Context,
current, current *MemberWriteModel,
changed *MemberWriteModel, roles ...string,
) (*MemberChangedEvent, error) { ) (*MemberChangedEvent, error) {
event, err := member.NewChangedEvent( m, err := member.ChangeEventFromExisting(
eventstore.NewBaseEventForPush( eventstore.NewBaseEventForPush(
ctx, ctx,
MemberChangedEventType, MemberChangedEventType,
), ),
&current.WriteModel, &current.WriteModel,
&changed.WriteModel, roles...,
) )
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &MemberChangedEvent{ return &MemberChangedEvent{
ChangedEvent: *event, ChangedEvent: *m,
}, nil }, nil
} }
func NewMemberChangedEvent(
ctx context.Context,
userID string,
roles ...string,
) *MemberChangedEvent {
return &MemberChangedEvent{
ChangedEvent: *member.NewChangedEvent(
eventstore.NewBaseEventForPush(
ctx,
MemberChangedEventType,
),
userID,
roles...,
),
}
}
func NewMemberRemovedEvent( func NewMemberRemovedEvent(
ctx context.Context, ctx context.Context,
userID string, userID string,