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
This commit is contained in:
Fabi
2021-07-05 10:36:51 +02:00
committed by GitHub
parent 91f1c88d4e
commit beb1c1604a
75 changed files with 3171 additions and 34 deletions

View File

@@ -67,6 +67,9 @@ const (
PasswordLockoutPolicyAdded models.EventType = "iam.policy.password.lockout.added"
PasswordLockoutPolicyChanged models.EventType = "iam.policy.password.lockout.changed"
PrivacyPolicyAdded models.EventType = "iam.policy.privacy.added"
PrivacyPolicyChanged models.EventType = "iam.policy.privacy.changed"
OrgIAMPolicyAdded models.EventType = "iam.policy.org.iam.added"
OrgIAMPolicyChanged models.EventType = "iam.policy.org.iam.changed"
)

View File

@@ -0,0 +1,98 @@
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
}

View File

@@ -0,0 +1,59 @@
package model
import (
"github.com/caos/zitadel/internal/domain"
iam_model "github.com/caos/zitadel/internal/iam/model"
"github.com/caos/zitadel/internal/view/repository"
)
type PrivacyPolicySearchRequest iam_model.PrivacyPolicySearchRequest
type PrivacyPolicySearchQuery iam_model.PrivacyPolicySearchQuery
type PrivacyPolicySearchKey iam_model.PrivacyPolicySearchKey
func (req PrivacyPolicySearchRequest) GetLimit() uint64 {
return req.Limit
}
func (req PrivacyPolicySearchRequest) GetOffset() uint64 {
return req.Offset
}
func (req PrivacyPolicySearchRequest) GetSortingColumn() repository.ColumnKey {
if req.SortingColumn == iam_model.PrivacyPolicySearchKeyUnspecified {
return nil
}
return PrivacyPolicySearchKey(req.SortingColumn)
}
func (req PrivacyPolicySearchRequest) GetAsc() bool {
return req.Asc
}
func (req PrivacyPolicySearchRequest) GetQueries() []repository.SearchQuery {
result := make([]repository.SearchQuery, len(req.Queries))
for i, q := range req.Queries {
result[i] = PrivacyPolicySearchQuery{Key: q.Key, Value: q.Value, Method: q.Method}
}
return result
}
func (req PrivacyPolicySearchQuery) GetKey() repository.ColumnKey {
return PrivacyPolicySearchKey(req.Key)
}
func (req PrivacyPolicySearchQuery) GetMethod() domain.SearchMethod {
return req.Method
}
func (req PrivacyPolicySearchQuery) GetValue() interface{} {
return req.Value
}
func (key PrivacyPolicySearchKey) ToColumnName() string {
switch iam_model.PrivacyPolicySearchKey(key) {
case iam_model.PrivacyPolicySearchKeyAggregateID:
return PrivacyKeyAggregateID
default:
return ""
}
}

View File

@@ -0,0 +1,32 @@
package view
import (
"github.com/caos/zitadel/internal/domain"
caos_errs "github.com/caos/zitadel/internal/errors"
iam_model "github.com/caos/zitadel/internal/iam/model"
"github.com/caos/zitadel/internal/iam/repository/view/model"
"github.com/caos/zitadel/internal/view/repository"
"github.com/jinzhu/gorm"
)
func GetPrivacyPolicyByAggregateID(db *gorm.DB, table, aggregateID string) (*model.PrivacyPolicyView, error) {
policy := new(model.PrivacyPolicyView)
aggregateIDQuery := &model.PrivacyPolicySearchQuery{Key: iam_model.PrivacyPolicySearchKeyAggregateID, Value: aggregateID, Method: domain.SearchMethodEquals}
query := repository.PrepareGetByQuery(table, aggregateIDQuery)
err := query(db, policy)
if caos_errs.IsNotFound(err) {
return nil, caos_errs.ThrowNotFound(nil, "VIEW-2N9fs", "Errors.IAM.PrivacyPolicy.NotExisting")
}
return policy, err
}
func PutPrivacyPolicy(db *gorm.DB, table string, policy *model.PrivacyPolicyView) error {
save := repository.PrepareSave(table)
return save(db, policy)
}
func DeletePrivacyPolicy(db *gorm.DB, table, aggregateID string) error {
delete := repository.PrepareDeleteByKey(table, model.PrivacyPolicySearchKey(iam_model.PrivacyPolicySearchKeyAggregateID), aggregateID)
return delete(db)
}