2020-05-18 09:32:16 +00:00
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"github.com/caos/logging"
|
|
|
|
caos_errs "github.com/caos/zitadel/internal/errors"
|
|
|
|
es_models "github.com/caos/zitadel/internal/eventstore/models"
|
|
|
|
"github.com/caos/zitadel/internal/iam/model"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
IamVersion = "v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Iam struct {
|
|
|
|
es_models.ObjectRoot
|
|
|
|
SetUpStarted bool `json:"-"`
|
|
|
|
SetUpDone bool `json:"-"`
|
|
|
|
GlobalOrgID string `json:"globalOrgId,omitempty"`
|
|
|
|
IamProjectID string `json:"iamProjectId,omitempty"`
|
|
|
|
Members []*IamMember `json:"-"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func IamFromModel(iam *model.Iam) *Iam {
|
|
|
|
members := IamMembersFromModel(iam.Members)
|
|
|
|
converted := &Iam{
|
|
|
|
ObjectRoot: iam.ObjectRoot,
|
|
|
|
SetUpStarted: iam.SetUpStarted,
|
|
|
|
SetUpDone: iam.SetUpDone,
|
|
|
|
GlobalOrgID: iam.GlobalOrgID,
|
|
|
|
IamProjectID: iam.IamProjectID,
|
|
|
|
Members: members,
|
|
|
|
}
|
|
|
|
return converted
|
|
|
|
}
|
|
|
|
|
|
|
|
func IamToModel(iam *Iam) *model.Iam {
|
|
|
|
members := IamMembersToModel(iam.Members)
|
|
|
|
converted := &model.Iam{
|
|
|
|
ObjectRoot: iam.ObjectRoot,
|
|
|
|
SetUpStarted: iam.SetUpStarted,
|
|
|
|
SetUpDone: iam.SetUpDone,
|
|
|
|
GlobalOrgID: iam.GlobalOrgID,
|
|
|
|
IamProjectID: iam.IamProjectID,
|
|
|
|
Members: members,
|
|
|
|
}
|
|
|
|
return converted
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Iam) AppendEvents(events ...*es_models.Event) error {
|
|
|
|
for _, event := range events {
|
|
|
|
if err := i.AppendEvent(event); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Iam) AppendEvent(event *es_models.Event) (err error) {
|
|
|
|
i.ObjectRoot.AppendEvent(event)
|
|
|
|
switch event.Type {
|
|
|
|
case IamSetupStarted:
|
|
|
|
i.SetUpStarted = true
|
|
|
|
case IamSetupDone:
|
|
|
|
i.SetUpDone = true
|
|
|
|
case IamProjectSet,
|
|
|
|
GlobalOrgSet:
|
2020-06-05 05:50:04 +00:00
|
|
|
err = i.SetData(event)
|
2020-05-18 09:32:16 +00:00
|
|
|
case IamMemberAdded:
|
|
|
|
err = i.appendAddMemberEvent(event)
|
|
|
|
case IamMemberChanged:
|
|
|
|
err = i.appendChangeMemberEvent(event)
|
|
|
|
case IamMemberRemoved:
|
|
|
|
err = i.appendRemoveMemberEvent(event)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-06-05 05:50:04 +00:00
|
|
|
func (i *Iam) SetData(event *es_models.Event) error {
|
2020-05-18 09:32:16 +00:00
|
|
|
i.ObjectRoot.AppendEvent(event)
|
|
|
|
if err := json.Unmarshal(event.Data, i); err != nil {
|
|
|
|
logging.Log("EVEN-9sie4").WithError(err).Error("could not unmarshal event data")
|
|
|
|
return caos_errs.ThrowInternal(err, "MODEL-slwi3", "could not unmarshal event")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|