2020-06-05 05:50:04 +00:00
|
|
|
package model
|
2020-05-13 12:22:29 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2020-10-20 17:10:23 +00:00
|
|
|
|
2020-05-13 12:22:29 +00:00
|
|
|
"github.com/caos/zitadel/internal/errors"
|
2022-03-31 09:36:26 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
2021-02-23 14:13:04 +00:00
|
|
|
es_models "github.com/caos/zitadel/internal/eventstore/v1/models"
|
2022-03-31 09:36:26 +00:00
|
|
|
iam_es_model "github.com/caos/zitadel/internal/iam/repository/eventsourcing/model"
|
2020-05-13 12:22:29 +00:00
|
|
|
org_model "github.com/caos/zitadel/internal/org/model"
|
2022-03-31 09:36:26 +00:00
|
|
|
"github.com/caos/zitadel/internal/repository/org"
|
2020-05-13 12:22:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Org struct {
|
|
|
|
es_models.ObjectRoot `json:"-"`
|
|
|
|
|
2020-06-16 09:40:18 +00:00
|
|
|
Name string `json:"name,omitempty"`
|
|
|
|
State int32 `json:"-"`
|
2020-05-13 12:22:29 +00:00
|
|
|
|
2022-03-31 09:36:26 +00:00
|
|
|
Domains []*OrgDomain `json:"-"`
|
|
|
|
DomainPolicy *iam_es_model.DomainPolicy `json:"-"`
|
2020-05-13 12:22:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func OrgToModel(org *Org) *org_model.Org {
|
2020-06-16 09:40:18 +00:00
|
|
|
converted := &org_model.Org{
|
2020-05-13 12:22:29 +00:00
|
|
|
ObjectRoot: org.ObjectRoot,
|
|
|
|
Name: org.Name,
|
|
|
|
State: org_model.OrgState(org.State),
|
2020-06-16 09:40:18 +00:00
|
|
|
Domains: OrgDomainsToModel(org.Domains),
|
2020-05-13 12:22:29 +00:00
|
|
|
}
|
2022-03-24 16:21:34 +00:00
|
|
|
if org.DomainPolicy != nil {
|
|
|
|
converted.DomainPolicy = iam_es_model.DomainPolicyToModel(org.DomainPolicy)
|
2020-08-26 07:56:23 +00:00
|
|
|
}
|
2020-06-16 09:40:18 +00:00
|
|
|
return converted
|
2020-05-13 12:22:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func OrgFromEvents(org *Org, events ...*es_models.Event) (*Org, error) {
|
|
|
|
if org == nil {
|
|
|
|
org = new(Org)
|
|
|
|
}
|
|
|
|
|
|
|
|
return org, org.AppendEvents(events...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Org) AppendEvents(events ...*es_models.Event) error {
|
|
|
|
for _, event := range events {
|
|
|
|
err := o.AppendEvent(event)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-26 07:56:23 +00:00
|
|
|
func (o *Org) AppendEvent(event *es_models.Event) (err error) {
|
2022-03-31 09:36:26 +00:00
|
|
|
switch eventstore.EventType(event.Type) {
|
|
|
|
case org.OrgAddedEventType:
|
2021-11-12 14:06:26 +00:00
|
|
|
err = o.SetData(event)
|
2020-05-13 12:22:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-03-31 09:36:26 +00:00
|
|
|
case org.OrgChangedEventType:
|
2021-11-12 14:06:26 +00:00
|
|
|
err = o.SetData(event)
|
2020-05-13 12:22:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-03-31 09:36:26 +00:00
|
|
|
case org.OrgDeactivatedEventType:
|
2020-06-23 12:47:47 +00:00
|
|
|
o.State = int32(org_model.OrgStateInactive)
|
2022-03-31 09:36:26 +00:00
|
|
|
case org.OrgReactivatedEventType:
|
2020-06-23 12:47:47 +00:00
|
|
|
o.State = int32(org_model.OrgStateActive)
|
2022-03-31 09:36:26 +00:00
|
|
|
case org.OrgDomainAddedEventType:
|
2020-08-26 07:56:23 +00:00
|
|
|
err = o.appendAddDomainEvent(event)
|
2022-03-31 09:36:26 +00:00
|
|
|
case org.OrgDomainVerificationAddedEventType:
|
2020-08-26 07:56:23 +00:00
|
|
|
err = o.appendVerificationDomainEvent(event)
|
2022-03-31 09:36:26 +00:00
|
|
|
case org.OrgDomainVerifiedEventType:
|
2020-08-26 07:56:23 +00:00
|
|
|
err = o.appendVerifyDomainEvent(event)
|
2022-03-31 09:36:26 +00:00
|
|
|
case org.OrgDomainPrimarySetEventType:
|
2020-08-26 07:56:23 +00:00
|
|
|
err = o.appendPrimaryDomainEvent(event)
|
2022-03-31 09:36:26 +00:00
|
|
|
case org.OrgDomainRemovedEventType:
|
2020-08-26 07:56:23 +00:00
|
|
|
err = o.appendRemoveDomainEvent(event)
|
2022-03-31 09:36:26 +00:00
|
|
|
case org.DomainPolicyAddedEventType:
|
2022-03-24 16:21:34 +00:00
|
|
|
err = o.appendAddDomainPolicyEvent(event)
|
2022-03-31 09:36:26 +00:00
|
|
|
case org.DomainPolicyChangedEventType:
|
2022-03-24 16:21:34 +00:00
|
|
|
err = o.appendChangeDomainPolicyEvent(event)
|
2022-03-31 09:36:26 +00:00
|
|
|
case org.DomainPolicyRemovedEventType:
|
2022-03-24 16:21:34 +00:00
|
|
|
o.appendRemoveDomainPolicyEvent()
|
2020-08-26 07:56:23 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-05-13 12:22:29 +00:00
|
|
|
}
|
|
|
|
o.ObjectRoot.AppendEvent(event)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-12 14:06:26 +00:00
|
|
|
func (o *Org) SetData(event *es_models.Event) error {
|
2020-05-13 12:22:29 +00:00
|
|
|
err := json.Unmarshal(event.Data, o)
|
|
|
|
if err != nil {
|
|
|
|
return errors.ThrowInternal(err, "EVENT-BpbQZ", "unable to unmarshal event")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Org) Changes(changed *Org) map[string]interface{} {
|
|
|
|
changes := make(map[string]interface{}, 2)
|
|
|
|
|
|
|
|
if changed.Name != "" && changed.Name != o.Name {
|
|
|
|
changes["name"] = changed.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
return changes
|
|
|
|
}
|