2020-09-18 13:26:28 +02:00
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2022-03-23 09:02:39 +01:00
|
|
|
"time"
|
|
|
|
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/logging"
|
2022-03-23 09:02:39 +01:00
|
|
|
|
2022-04-27 01:01:45 +02:00
|
|
|
caos_errs "github.com/zitadel/zitadel/internal/errors"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/v1/models"
|
|
|
|
user_repo "github.com/zitadel/zitadel/internal/repository/user"
|
2020-09-18 13:26:28 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
ExternalIDPKeyExternalUserID = "external_user_id"
|
|
|
|
ExternalIDPKeyUserID = "user_id"
|
|
|
|
ExternalIDPKeyIDPConfigID = "idp_config_id"
|
|
|
|
ExternalIDPKeyResourceOwner = "resource_owner"
|
2022-04-19 08:26:12 +02:00
|
|
|
ExternalIDPKeyInstanceID = "instance_id"
|
2022-11-30 17:01:17 +01:00
|
|
|
ExternalIDPKeyOwnerRemoved = "owner_removed"
|
2020-09-18 13:26:28 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
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"`
|
2022-04-19 08:26:12 +02:00
|
|
|
InstanceID string `json:"instanceID" gorm:"column:instance_id;primary_key"`
|
2020-09-18 13:26:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *ExternalIDPView) AppendEvent(event *models.Event) (err error) {
|
2023-10-19 12:19:10 +02:00
|
|
|
i.Sequence = event.Seq
|
2020-09-18 13:26:28 +02:00
|
|
|
i.ChangeDate = event.CreationDate
|
2023-10-19 12:19:10 +02:00
|
|
|
if event.Typ == user_repo.UserIDPLinkAddedType {
|
2020-09-18 13:26:28 +02:00
|
|
|
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
|
2022-03-23 09:02:39 +01:00
|
|
|
r.InstanceID = event.InstanceID
|
2020-09-18 13:26:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|