2020-06-05 05:50:04 +00:00
|
|
|
package model
|
2020-05-13 12:22:29 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2020-08-06 13:03:03 +00:00
|
|
|
|
2020-05-13 12:22:29 +00:00
|
|
|
"github.com/caos/zitadel/internal/errors"
|
|
|
|
es_models "github.com/caos/zitadel/internal/eventstore/models"
|
|
|
|
org_model "github.com/caos/zitadel/internal/org/model"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-06-05 05:50:04 +00:00
|
|
|
OrgVersion = "v1"
|
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
|
|
|
|
2020-06-16 09:40:18 +00:00
|
|
|
Domains []*OrgDomain `json:"-"`
|
|
|
|
Members []*OrgMember `json:"-"`
|
|
|
|
OrgIamPolicy *OrgIamPolicy `json:"-"`
|
2020-05-13 12:22:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func OrgFromModel(org *org_model.Org) *Org {
|
|
|
|
members := OrgMembersFromModel(org.Members)
|
2020-06-16 09:40:18 +00:00
|
|
|
domains := OrgDomainsFromModel(org.Domains)
|
|
|
|
converted := &Org{
|
2020-05-13 12:22:29 +00:00
|
|
|
ObjectRoot: org.ObjectRoot,
|
|
|
|
Name: org.Name,
|
|
|
|
State: int32(org.State),
|
2020-06-16 09:40:18 +00:00
|
|
|
Domains: domains,
|
2020-05-13 12:22:29 +00:00
|
|
|
Members: members,
|
|
|
|
}
|
2020-06-16 09:40:18 +00:00
|
|
|
if org.OrgIamPolicy != nil {
|
|
|
|
converted.OrgIamPolicy = OrgIamPolicyFromModel(org.OrgIamPolicy)
|
|
|
|
}
|
|
|
|
return converted
|
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
|
|
|
Members: OrgMembersToModel(org.Members),
|
|
|
|
}
|
2020-06-16 09:40:18 +00:00
|
|
|
if org.OrgIamPolicy != nil {
|
|
|
|
converted.OrgIamPolicy = OrgIamPolicyToModel(org.OrgIamPolicy)
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Org) AppendEvent(event *es_models.Event) error {
|
|
|
|
switch event.Type {
|
2020-06-05 05:50:04 +00:00
|
|
|
case OrgAdded:
|
2020-05-13 12:22:29 +00:00
|
|
|
*o = Org{}
|
|
|
|
err := o.setData(event)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-06-05 05:50:04 +00:00
|
|
|
case OrgChanged:
|
2020-05-13 12:22:29 +00:00
|
|
|
err := o.setData(event)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-06-05 05:50:04 +00:00
|
|
|
case OrgDeactivated:
|
2020-06-23 12:47:47 +00:00
|
|
|
o.State = int32(org_model.OrgStateInactive)
|
2020-06-05 05:50:04 +00:00
|
|
|
case OrgReactivated:
|
2020-06-23 12:47:47 +00:00
|
|
|
o.State = int32(org_model.OrgStateActive)
|
2020-06-05 05:50:04 +00:00
|
|
|
case OrgMemberAdded:
|
2020-05-13 12:22:29 +00:00
|
|
|
member, err := OrgMemberFromEvent(nil, event)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
member.CreationDate = event.CreationDate
|
|
|
|
|
|
|
|
o.setMember(member)
|
2020-06-05 05:50:04 +00:00
|
|
|
case OrgMemberChanged:
|
2020-05-13 12:22:29 +00:00
|
|
|
member, err := OrgMemberFromEvent(nil, event)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
existingMember := o.getMember(member.UserID)
|
|
|
|
member.CreationDate = existingMember.CreationDate
|
|
|
|
|
|
|
|
o.setMember(member)
|
2020-06-05 05:50:04 +00:00
|
|
|
case OrgMemberRemoved:
|
2020-05-13 12:22:29 +00:00
|
|
|
member, err := OrgMemberFromEvent(nil, event)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
o.removeMember(member.UserID)
|
2020-06-16 09:40:18 +00:00
|
|
|
case OrgDomainAdded:
|
|
|
|
o.appendAddDomainEvent(event)
|
2020-08-06 13:03:03 +00:00
|
|
|
case OrgDomainVerificationAdded:
|
|
|
|
o.appendVerificationDomainEvent(event)
|
2020-06-16 09:40:18 +00:00
|
|
|
case OrgDomainVerified:
|
|
|
|
o.appendVerifyDomainEvent(event)
|
|
|
|
case OrgDomainPrimarySet:
|
|
|
|
o.appendPrimaryDomainEvent(event)
|
|
|
|
case OrgDomainRemoved:
|
|
|
|
o.appendRemoveDomainEvent(event)
|
|
|
|
case OrgIamPolicyAdded:
|
|
|
|
o.appendAddOrgIamPolicyEvent(event)
|
|
|
|
case OrgIamPolicyChanged:
|
|
|
|
o.appendChangeOrgIamPolicyEvent(event)
|
|
|
|
case OrgIamPolicyRemoved:
|
|
|
|
o.appendRemoveOrgIamPolicyEvent()
|
2020-05-13 12:22:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
o.ObjectRoot.AppendEvent(event)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Org) setData(event *es_models.Event) error {
|
|
|
|
err := json.Unmarshal(event.Data, o)
|
|
|
|
if err != nil {
|
|
|
|
return errors.ThrowInternal(err, "EVENT-BpbQZ", "unable to unmarshal event")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Org) getMember(userID string) *OrgMember {
|
|
|
|
for _, member := range o.Members {
|
|
|
|
if member.UserID == userID {
|
|
|
|
return member
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Org) setMember(member *OrgMember) {
|
|
|
|
for i, existingMember := range o.Members {
|
|
|
|
if existingMember.UserID == member.UserID {
|
|
|
|
o.Members[i] = member
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
o.Members = append(o.Members, member)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *Org) removeMember(userID string) {
|
|
|
|
for i := len(o.Members) - 1; i >= 0; i-- {
|
|
|
|
if o.Members[i].UserID == userID {
|
|
|
|
copy(o.Members[i:], o.Members[i+1:])
|
|
|
|
o.Members[len(o.Members)-1] = nil
|
|
|
|
o.Members = o.Members[:len(o.Members)-1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|