Fabi 7451ed58f2
feat: User metadata (#2025)
* feat: user meta data events

* feat: user meta data set tests

* feat: user meta data tests

* feat: user meta data in protos

* feat: user meta data command api

* feat: user meta data query side

* feat: proto correct order, fix handlers

* feat: proto correct order

* feat: fixes of pr comments

* feat: fixes of pr comments

* feat: value as byte array

* feat: metadata feature

* Update internal/auth/repository/eventsourcing/handler/meta_data.go

Co-authored-by: Silvan <silvan.reusser@gmail.com>

* Update internal/command/user_meta_data.go

Co-authored-by: Silvan <silvan.reusser@gmail.com>

* Update proto/zitadel/metadata.proto

Co-authored-by: Silvan <silvan.reusser@gmail.com>

* Update proto/zitadel/metadata.proto

Co-authored-by: Silvan <silvan.reusser@gmail.com>

* fix: rename metadata files and table

* fix: rename meta data to metadat in protos

* Update internal/domain/metadata.go

Co-authored-by: Silvan <silvan.reusser@gmail.com>

* fix: rename vars

* fix: rebiuld docs

* Update internal/iam/repository/view/metadata_view.go

Co-authored-by: Silvan <silvan.reusser@gmail.com>

Co-authored-by: Silvan <silvan.reusser@gmail.com>
2021-08-09 08:01:20 +02:00

125 lines
3.2 KiB
Go

package handler
import (
"github.com/caos/logging"
caos_errs "github.com/caos/zitadel/internal/errors"
"github.com/caos/zitadel/internal/eventstore/v1"
es_models "github.com/caos/zitadel/internal/eventstore/v1/models"
"github.com/caos/zitadel/internal/eventstore/v1/query"
"github.com/caos/zitadel/internal/eventstore/v1/spooler"
iam_model "github.com/caos/zitadel/internal/iam/repository/view/model"
usr_model "github.com/caos/zitadel/internal/user/repository/eventsourcing/model"
)
type Metadata struct {
handler
subscription *v1.Subscription
}
func newMetadata(handler handler) *Metadata {
h := &Metadata{
handler: handler,
}
h.subscribe()
return h
}
func (m *Metadata) subscribe() {
m.subscription = m.es.Subscribe(m.AggregateTypes()...)
go func() {
for event := range m.subscription.Events {
query.ReduceEvent(m, event)
}
}()
}
const (
metadataTable = "management.metadata"
)
func (m *Metadata) ViewModel() string {
return metadataTable
}
func (m *Metadata) Subscription() *v1.Subscription {
return m.subscription
}
func (_ *Metadata) AggregateTypes() []es_models.AggregateType {
return []es_models.AggregateType{usr_model.UserAggregate}
}
func (p *Metadata) CurrentSequence() (uint64, error) {
sequence, err := p.view.GetLatestMetadataSequence()
if err != nil {
return 0, err
}
return sequence.CurrentSequence, nil
}
func (m *Metadata) EventQuery() (*es_models.SearchQuery, error) {
sequence, err := m.view.GetLatestMetadataSequence()
if err != nil {
return nil, err
}
return es_models.NewSearchQuery().
AggregateTypeFilter(m.AggregateTypes()...).
LatestSequenceFilter(sequence.CurrentSequence), nil
}
func (m *Metadata) Reduce(event *es_models.Event) (err error) {
switch event.AggregateType {
case usr_model.UserAggregate:
err = m.processMetadata(event)
}
return err
}
func (m *Metadata) processMetadata(event *es_models.Event) (err error) {
metadata := new(iam_model.MetadataView)
switch event.Type {
case usr_model.UserMetadataSet:
err = metadata.SetData(event)
if err != nil {
return err
}
metadata, err = m.view.MetadataByKey(event.AggregateID, metadata.Key)
if err != nil && !caos_errs.IsNotFound(err) {
return err
}
if caos_errs.IsNotFound(err) {
err = nil
metadata = new(iam_model.MetadataView)
metadata.CreationDate = event.CreationDate
}
err = metadata.AppendEvent(event)
case usr_model.UserMetadataRemoved:
data := new(iam_model.MetadataView)
err = data.SetData(event)
if err != nil {
return err
}
return m.view.DeleteMetadata(event.AggregateID, data.Key, event)
case usr_model.UserRemoved:
return m.view.DeleteMetadataByAggregateID(event.AggregateID, event)
default:
return m.view.ProcessedMetadataSequence(event)
}
if err != nil {
return err
}
return m.view.PutMetadata(metadata, event)
}
func (m *Metadata) OnError(event *es_models.Event, err error) error {
logging.LogWithFields("SPOOL-3m912", "id", event.AggregateID).WithError(err).Warn("something went wrong in custom text handler")
return spooler.HandleError(event, err, m.view.GetLatestMetadataFailedEvent, m.view.ProcessedMetadataFailedEvent, m.view.ProcessedMetadataSequence, m.errorCountUntilSkip)
}
func (o *Metadata) OnSuccess() error {
return spooler.HandleSuccess(o.view.UpdateMetadataSpoolerRunTimestamp)
}