zitadel/internal/user/repository/view/model/external_idps.go
Livio Amstutz 56b916a2b0
feat: projections auto create their tables (#3324)
* begin init checks for projections

* first projection checks

* debug notification providers with query fixes

* more projections and first index

* more projections

* more projections

* finish projections

* fix tests (remove db name)

* create tables in setup

* fix logging / error handling

* add tenant to views

* rename tenant to instance_id

* add instance_id to all projections

* add instance_id to all queries

* correct instance_id on projections

* add instance_id to failed_events

* use separate context for instance

* implement features projection

* implement features projection

* remove unique constraint from setup when migration failed

* add error to failed setup event

* add instance_id to primary keys

* fix IAM projection

* remove old migrations folder

* fix keysFromYAML test
2022-03-23 09:02:39 +01:00

96 lines
3.2 KiB
Go

package model
import (
"encoding/json"
"time"
"github.com/caos/logging"
caos_errs "github.com/caos/zitadel/internal/errors"
"github.com/caos/zitadel/internal/eventstore/v1/models"
"github.com/caos/zitadel/internal/user/model"
es_model "github.com/caos/zitadel/internal/user/repository/eventsourcing/model"
)
const (
ExternalIDPKeyExternalUserID = "external_user_id"
ExternalIDPKeyUserID = "user_id"
ExternalIDPKeyIDPConfigID = "idp_config_id"
ExternalIDPKeyResourceOwner = "resource_owner"
)
type ExternalIDPView struct {
ExternalUserID string `json:"userID" gorm:"column:external_user_id;primary_key"`
IDPConfigID string `json:"idpConfigID" gorm:"column:idp_config_id;primary_key"`
UserID string `json:"-" gorm:"column:user_id"`
IDPName string `json:"-" gorm:"column:idp_name"`
UserDisplayName string `json:"displayName" gorm:"column:user_display_name"`
CreationDate time.Time `json:"-" gorm:"column:creation_date"`
ChangeDate time.Time `json:"-" gorm:"column:change_date"`
ResourceOwner string `json:"-" gorm:"column:resource_owner"`
Sequence uint64 `json:"-" gorm:"column:sequence"`
InstanceID string `json:"instanceID" gorm:"column:instance_id"`
}
func ExternalIDPViewFromModel(externalIDP *model.ExternalIDPView) *ExternalIDPView {
return &ExternalIDPView{
UserID: externalIDP.UserID,
IDPConfigID: externalIDP.IDPConfigID,
ExternalUserID: externalIDP.ExternalUserID,
IDPName: externalIDP.IDPName,
UserDisplayName: externalIDP.UserDisplayName,
Sequence: externalIDP.Sequence,
CreationDate: externalIDP.CreationDate,
ChangeDate: externalIDP.ChangeDate,
ResourceOwner: externalIDP.ResourceOwner,
}
}
func ExternalIDPViewToModel(externalIDP *ExternalIDPView) *model.ExternalIDPView {
return &model.ExternalIDPView{
UserID: externalIDP.UserID,
IDPConfigID: externalIDP.IDPConfigID,
ExternalUserID: externalIDP.ExternalUserID,
IDPName: externalIDP.IDPName,
UserDisplayName: externalIDP.UserDisplayName,
Sequence: externalIDP.Sequence,
CreationDate: externalIDP.CreationDate,
ChangeDate: externalIDP.ChangeDate,
ResourceOwner: externalIDP.ResourceOwner,
}
}
func ExternalIDPViewsToModel(externalIDPs []*ExternalIDPView) []*model.ExternalIDPView {
result := make([]*model.ExternalIDPView, len(externalIDPs))
for i, r := range externalIDPs {
result[i] = ExternalIDPViewToModel(r)
}
return result
}
func (i *ExternalIDPView) AppendEvent(event *models.Event) (err error) {
i.Sequence = event.Sequence
i.ChangeDate = event.CreationDate
switch event.Type {
case es_model.HumanExternalIDPAdded:
i.setRootData(event)
i.CreationDate = event.CreationDate
err = i.SetData(event)
}
return err
}
func (r *ExternalIDPView) setRootData(event *models.Event) {
r.UserID = event.AggregateID
r.ResourceOwner = event.ResourceOwner
r.InstanceID = event.InstanceID
}
func (r *ExternalIDPView) SetData(event *models.Event) error {
if err := json.Unmarshal(event.Data, r); err != nil {
logging.Log("EVEN-48sfs").WithError(err).Error("could not unmarshal event data")
return caos_errs.ThrowInternal(err, "MODEL-Hs8uf", "Could not unmarshal data")
}
return nil
}