2020-08-26 07:56:23 +00:00
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
org_es_model "github.com/caos/zitadel/internal/org/repository/eventsourcing/model"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
es_model "github.com/caos/zitadel/internal/iam/repository/eventsourcing/model"
|
|
|
|
|
|
|
|
"github.com/caos/logging"
|
|
|
|
caos_errs "github.com/caos/zitadel/internal/errors"
|
2021-02-23 14:13:04 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore/v1/models"
|
2020-08-26 07:56:23 +00:00
|
|
|
"github.com/caos/zitadel/internal/iam/model"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
IDPProviderKeyAggregateID = "aggregate_id"
|
|
|
|
IDPProviderKeyIdpConfigID = "idp_config_id"
|
2020-09-23 14:52:19 +00:00
|
|
|
IDPProviderKeyState = "idp_state"
|
2020-08-26 07:56:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type IDPProviderView struct {
|
|
|
|
AggregateID string `json:"-" gorm:"column:aggregate_id;primary_key"`
|
|
|
|
IDPConfigID string `json:"idpConfigID" gorm:"column:idp_config_id;primary_key"`
|
|
|
|
|
|
|
|
CreationDate time.Time `json:"-" gorm:"column:creation_date"`
|
|
|
|
ChangeDate time.Time `json:"-" gorm:"column:change_date"`
|
|
|
|
|
|
|
|
Name string `json:"-" gorm:"column:name"`
|
2020-10-19 15:10:02 +00:00
|
|
|
StylingType int32 `json:"-" gorm:"column:styling_type"`
|
2020-08-26 07:56:23 +00:00
|
|
|
IDPConfigType int32 `json:"-" gorm:"column:idp_config_type"`
|
|
|
|
IDPProviderType int32 `json:"idpProviderType" gorm:"column:idp_provider_type"`
|
2020-09-23 14:52:19 +00:00
|
|
|
IDPState int32 `json:"-" gorm:"column:idp_state"`
|
2020-08-26 07:56:23 +00:00
|
|
|
|
|
|
|
Sequence uint64 `json:"-" gorm:"column:sequence"`
|
|
|
|
}
|
|
|
|
|
2020-09-23 14:52:19 +00:00
|
|
|
func IDPProviderViewFromModel(provider *model.IDPProviderView) *IDPProviderView {
|
2020-08-26 07:56:23 +00:00
|
|
|
return &IDPProviderView{
|
2020-09-23 14:52:19 +00:00
|
|
|
AggregateID: provider.AggregateID,
|
|
|
|
Sequence: provider.Sequence,
|
|
|
|
CreationDate: provider.CreationDate,
|
|
|
|
ChangeDate: provider.ChangeDate,
|
|
|
|
Name: provider.Name,
|
2020-10-19 15:10:02 +00:00
|
|
|
StylingType: int32(provider.StylingType),
|
2020-09-23 14:52:19 +00:00
|
|
|
IDPConfigID: provider.IDPConfigID,
|
|
|
|
IDPConfigType: int32(provider.IDPConfigType),
|
|
|
|
IDPProviderType: int32(provider.IDPProviderType),
|
|
|
|
IDPState: int32(provider.IDPState),
|
2020-08-26 07:56:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-23 14:52:19 +00:00
|
|
|
func IDPProviderViewToModel(provider *IDPProviderView) *model.IDPProviderView {
|
2020-08-26 07:56:23 +00:00
|
|
|
return &model.IDPProviderView{
|
2020-09-23 14:52:19 +00:00
|
|
|
AggregateID: provider.AggregateID,
|
|
|
|
Sequence: provider.Sequence,
|
|
|
|
CreationDate: provider.CreationDate,
|
|
|
|
ChangeDate: provider.ChangeDate,
|
|
|
|
Name: provider.Name,
|
2020-10-19 15:10:02 +00:00
|
|
|
StylingType: model.IDPStylingType(provider.StylingType),
|
2020-09-23 14:52:19 +00:00
|
|
|
IDPConfigID: provider.IDPConfigID,
|
|
|
|
IDPConfigType: model.IdpConfigType(provider.IDPConfigType),
|
|
|
|
IDPProviderType: model.IDPProviderType(provider.IDPProviderType),
|
|
|
|
IDPState: model.IDPConfigState(provider.IDPState),
|
2020-08-26 07:56:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func IDPProviderViewsToModel(providers []*IDPProviderView) []*model.IDPProviderView {
|
|
|
|
result := make([]*model.IDPProviderView, len(providers))
|
|
|
|
for i, r := range providers {
|
|
|
|
result[i] = IDPProviderViewToModel(r)
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *IDPProviderView) AppendEvent(event *models.Event) (err error) {
|
|
|
|
i.Sequence = event.Sequence
|
|
|
|
i.ChangeDate = event.CreationDate
|
|
|
|
switch event.Type {
|
|
|
|
case es_model.LoginPolicyIDPProviderAdded, org_es_model.LoginPolicyIDPProviderAdded:
|
|
|
|
i.setRootData(event)
|
|
|
|
i.CreationDate = event.CreationDate
|
|
|
|
err = i.SetData(event)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *IDPProviderView) setRootData(event *models.Event) {
|
|
|
|
r.AggregateID = event.AggregateID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *IDPProviderView) SetData(event *models.Event) error {
|
|
|
|
if err := json.Unmarshal(event.Data, r); err != nil {
|
|
|
|
logging.Log("EVEN-Lso0d").WithError(err).Error("could not unmarshal event data")
|
|
|
|
return caos_errs.ThrowInternal(err, "MODEL-Hs8uf", "Could not unmarshal data")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|