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 (
|
2020-08-26 07:56:23 +00:00
|
|
|
IAMVersion = "v1"
|
2020-05-18 09:32:16 +00:00
|
|
|
)
|
|
|
|
|
2020-08-26 07:56:23 +00:00
|
|
|
type IAM struct {
|
2020-05-18 09:32:16 +00:00
|
|
|
es_models.ObjectRoot
|
2020-08-26 07:56:23 +00:00
|
|
|
SetUpStarted bool `json:"-"`
|
|
|
|
SetUpDone bool `json:"-"`
|
|
|
|
GlobalOrgID string `json:"globalOrgId,omitempty"`
|
|
|
|
IAMProjectID string `json:"iamProjectId,omitempty"`
|
|
|
|
Members []*IAMMember `json:"-"`
|
|
|
|
IDPs []*IDPConfig `json:"-"`
|
|
|
|
DefaultLoginPolicy *LoginPolicy `json:"-"`
|
2020-05-18 09:32:16 +00:00
|
|
|
}
|
|
|
|
|
2020-08-26 07:56:23 +00:00
|
|
|
func IAMFromModel(iam *model.IAM) *IAM {
|
|
|
|
members := IAMMembersFromModel(iam.Members)
|
|
|
|
idps := IDPConfigsFromModel(iam.IDPs)
|
|
|
|
converted := &IAM{
|
2020-05-18 09:32:16 +00:00
|
|
|
ObjectRoot: iam.ObjectRoot,
|
|
|
|
SetUpStarted: iam.SetUpStarted,
|
|
|
|
SetUpDone: iam.SetUpDone,
|
|
|
|
GlobalOrgID: iam.GlobalOrgID,
|
2020-08-26 07:56:23 +00:00
|
|
|
IAMProjectID: iam.IAMProjectID,
|
2020-05-18 09:32:16 +00:00
|
|
|
Members: members,
|
2020-08-26 07:56:23 +00:00
|
|
|
IDPs: idps,
|
|
|
|
}
|
|
|
|
if iam.DefaultLoginPolicy != nil {
|
|
|
|
converted.DefaultLoginPolicy = LoginPolicyFromModel(iam.DefaultLoginPolicy)
|
2020-05-18 09:32:16 +00:00
|
|
|
}
|
|
|
|
return converted
|
|
|
|
}
|
|
|
|
|
2020-08-26 07:56:23 +00:00
|
|
|
func IAMToModel(iam *IAM) *model.IAM {
|
|
|
|
members := IAMMembersToModel(iam.Members)
|
|
|
|
idps := IDPConfigsToModel(iam.IDPs)
|
|
|
|
converted := &model.IAM{
|
2020-05-18 09:32:16 +00:00
|
|
|
ObjectRoot: iam.ObjectRoot,
|
|
|
|
SetUpStarted: iam.SetUpStarted,
|
|
|
|
SetUpDone: iam.SetUpDone,
|
|
|
|
GlobalOrgID: iam.GlobalOrgID,
|
2020-08-26 07:56:23 +00:00
|
|
|
IAMProjectID: iam.IAMProjectID,
|
2020-05-18 09:32:16 +00:00
|
|
|
Members: members,
|
2020-08-26 07:56:23 +00:00
|
|
|
IDPs: idps,
|
|
|
|
}
|
|
|
|
if iam.DefaultLoginPolicy != nil {
|
|
|
|
converted.DefaultLoginPolicy = LoginPolicyToModel(iam.DefaultLoginPolicy)
|
2020-05-18 09:32:16 +00:00
|
|
|
}
|
|
|
|
return converted
|
|
|
|
}
|
|
|
|
|
2020-08-26 07:56:23 +00:00
|
|
|
func (i *IAM) AppendEvents(events ...*es_models.Event) error {
|
2020-05-18 09:32:16 +00:00
|
|
|
for _, event := range events {
|
|
|
|
if err := i.AppendEvent(event); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-26 07:56:23 +00:00
|
|
|
func (i *IAM) AppendEvent(event *es_models.Event) (err error) {
|
2020-05-18 09:32:16 +00:00
|
|
|
i.ObjectRoot.AppendEvent(event)
|
|
|
|
switch event.Type {
|
2020-08-26 07:56:23 +00:00
|
|
|
case IAMSetupStarted:
|
2020-05-18 09:32:16 +00:00
|
|
|
i.SetUpStarted = true
|
2020-08-26 07:56:23 +00:00
|
|
|
case IAMSetupDone:
|
2020-05-18 09:32:16 +00:00
|
|
|
i.SetUpDone = true
|
2020-08-26 07:56:23 +00:00
|
|
|
case IAMProjectSet,
|
2020-05-18 09:32:16 +00:00
|
|
|
GlobalOrgSet:
|
2020-06-05 05:50:04 +00:00
|
|
|
err = i.SetData(event)
|
2020-08-26 07:56:23 +00:00
|
|
|
case IAMMemberAdded:
|
2020-05-18 09:32:16 +00:00
|
|
|
err = i.appendAddMemberEvent(event)
|
2020-08-26 07:56:23 +00:00
|
|
|
case IAMMemberChanged:
|
2020-05-18 09:32:16 +00:00
|
|
|
err = i.appendChangeMemberEvent(event)
|
2020-08-26 07:56:23 +00:00
|
|
|
case IAMMemberRemoved:
|
2020-05-18 09:32:16 +00:00
|
|
|
err = i.appendRemoveMemberEvent(event)
|
2020-08-26 07:56:23 +00:00
|
|
|
case IDPConfigAdded:
|
|
|
|
return i.appendAddIDPConfigEvent(event)
|
|
|
|
case IDPConfigChanged:
|
|
|
|
return i.appendChangeIDPConfigEvent(event)
|
|
|
|
case IDPConfigRemoved:
|
|
|
|
return i.appendRemoveIDPConfigEvent(event)
|
|
|
|
case IDPConfigDeactivated:
|
|
|
|
return i.appendIDPConfigStateEvent(event, model.IDPConfigStateInactive)
|
|
|
|
case IDPConfigReactivated:
|
|
|
|
return i.appendIDPConfigStateEvent(event, model.IDPConfigStateActive)
|
|
|
|
case OIDCIDPConfigAdded:
|
|
|
|
return i.appendAddOIDCIDPConfigEvent(event)
|
|
|
|
case OIDCIDPConfigChanged:
|
|
|
|
return i.appendChangeOIDCIDPConfigEvent(event)
|
|
|
|
case LoginPolicyAdded:
|
|
|
|
return i.appendAddLoginPolicyEvent(event)
|
|
|
|
case LoginPolicyChanged:
|
|
|
|
return i.appendChangeLoginPolicyEvent(event)
|
|
|
|
case LoginPolicyIDPProviderAdded:
|
|
|
|
return i.appendAddIDPProviderToLoginPolicyEvent(event)
|
|
|
|
case LoginPolicyIDPProviderRemoved:
|
|
|
|
return i.appendRemoveIDPProviderFromLoginPolicyEvent(event)
|
2020-05-18 09:32:16 +00:00
|
|
|
}
|
2020-08-26 07:56:23 +00:00
|
|
|
|
2020-05-18 09:32:16 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-08-26 07:56:23 +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
|
|
|
|
}
|