2020-08-26 07:56:23 +00:00
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/logging"
|
2022-03-23 08:02:39 +00:00
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
"github.com/zitadel/zitadel/internal/iam/model"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/instance"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/org"
|
2023-12-08 14:30:55 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2020-08-26 07:56:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2022-11-30 16:01:17 +00:00
|
|
|
IDPProviderKeyAggregateID = "aggregate_id"
|
|
|
|
IDPProviderKeyIdpConfigID = "idp_config_id"
|
|
|
|
IDPProviderKeyState = "idp_state"
|
|
|
|
IDPProviderKeyInstanceID = "instance_id"
|
|
|
|
IDPProviderKeyOwnerRemoved = "owner_removed"
|
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
|
|
|
|
2022-03-23 08:02:39 +00:00
|
|
|
Sequence uint64 `json:"-" gorm:"column:sequence"`
|
2022-04-19 06:26:12 +00:00
|
|
|
InstanceID string `json:"instanceID" gorm:"column:instance_id;primary_key"`
|
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
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (i *IDPProviderView) AppendEvent(event eventstore.Event) (err error) {
|
|
|
|
i.Sequence = event.Sequence()
|
|
|
|
i.ChangeDate = event.CreatedAt()
|
|
|
|
switch event.Type() {
|
2022-03-31 09:36:26 +00:00
|
|
|
case instance.LoginPolicyIDPProviderAddedEventType,
|
|
|
|
org.LoginPolicyIDPProviderAddedEventType:
|
2020-08-26 07:56:23 +00:00
|
|
|
i.setRootData(event)
|
2023-10-19 10:19:10 +00:00
|
|
|
i.CreationDate = event.CreatedAt()
|
2020-08-26 07:56:23 +00:00
|
|
|
err = i.SetData(event)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (r *IDPProviderView) setRootData(event eventstore.Event) {
|
|
|
|
r.AggregateID = event.Aggregate().ID
|
|
|
|
r.InstanceID = event.Aggregate().InstanceID
|
2020-08-26 07:56:23 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (r *IDPProviderView) SetData(event eventstore.Event) error {
|
|
|
|
if err := event.Unmarshal(r); err != nil {
|
2022-03-31 09:36:26 +00:00
|
|
|
logging.New().WithError(err).Error("could not unmarshal event data")
|
2023-12-08 14:30:55 +00:00
|
|
|
return zerrors.ThrowInternal(err, "MODEL-Hs8uf", "Could not unmarshal data")
|
2020-08-26 07:56:23 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|