zitadel/internal/iam/repository/view/model/privacy_policy.go
Fabi beb1c1604a
feat: Privacy policy (#1957)
* feat: command side privacy policy

* feat: add privacy policy to api

* feat: add privacy policy query side

* fix: add privacy policy to mgmt api

* fix: add privacy policy to auth and base data

* feat: use privacyPolicy in login gui

* feat: use privacyPolicy in login gui

* feat: test org fatures

* feat: typos

* feat: tos in register
2021-07-05 10:36:51 +02:00

99 lines
2.8 KiB
Go

package model
import (
"encoding/json"
"time"
"github.com/caos/zitadel/internal/domain"
org_es_model "github.com/caos/zitadel/internal/org/repository/eventsourcing/model"
es_model "github.com/caos/zitadel/internal/iam/repository/eventsourcing/model"
"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/iam/model"
)
const (
PrivacyKeyAggregateID = "aggregate_id"
)
type PrivacyPolicyView struct {
AggregateID string `json:"-" gorm:"column:aggregate_id;primary_key"`
CreationDate time.Time `json:"-" gorm:"column:creation_date"`
ChangeDate time.Time `json:"-" gorm:"column:change_date"`
State int32 `json:"-" gorm:"column:state"`
TOSLink string `json:"tosLink" gorm:"column:tos_link"`
PrivacyLink string `json:"privacyLink" gorm:"column:privacy_link"`
Default bool `json:"-" gorm:"-"`
Sequence uint64 `json:"-" gorm:"column:sequence"`
}
func PrivacyViewFromModel(policy *model.PrivacyPolicyView) *PrivacyPolicyView {
return &PrivacyPolicyView{
AggregateID: policy.AggregateID,
Sequence: policy.Sequence,
CreationDate: policy.CreationDate,
ChangeDate: policy.ChangeDate,
TOSLink: policy.TOSLink,
PrivacyLink: policy.PrivacyLink,
Default: policy.Default,
}
}
func PrivacyViewToModel(policy *PrivacyPolicyView) *model.PrivacyPolicyView {
return &model.PrivacyPolicyView{
AggregateID: policy.AggregateID,
Sequence: policy.Sequence,
CreationDate: policy.CreationDate,
ChangeDate: policy.ChangeDate,
TOSLink: policy.TOSLink,
PrivacyLink: policy.PrivacyLink,
Default: policy.Default,
}
}
func (p *PrivacyPolicyView) ToDomain() *domain.PrivacyPolicy {
return &domain.PrivacyPolicy{
ObjectRoot: models.ObjectRoot{
AggregateID: p.AggregateID,
CreationDate: p.CreationDate,
ChangeDate: p.ChangeDate,
Sequence: p.Sequence,
},
Default: p.Default,
TOSLink: p.TOSLink,
PrivacyLink: p.PrivacyLink,
}
}
func (i *PrivacyPolicyView) AppendEvent(event *models.Event) (err error) {
i.Sequence = event.Sequence
i.ChangeDate = event.CreationDate
switch event.Type {
case es_model.PrivacyPolicyAdded, org_es_model.PrivacyPolicyAdded:
i.setRootData(event)
i.CreationDate = event.CreationDate
err = i.SetData(event)
case es_model.PrivacyPolicyChanged, org_es_model.PrivacyPolicyChanged:
err = i.SetData(event)
}
return err
}
func (r *PrivacyPolicyView) setRootData(event *models.Event) {
r.AggregateID = event.AggregateID
}
func (r *PrivacyPolicyView) SetData(event *models.Event) error {
if err := json.Unmarshal(event.Data, r); err != nil {
logging.Log("EVEN-gHls0").WithError(err).Error("could not unmarshal event data")
return caos_errs.ThrowInternal(err, "MODEL-Hs8uf", "Could not unmarshal data")
}
return nil
}