2021-01-04 13:52:13 +00:00
|
|
|
package query
|
|
|
|
|
|
|
|
import (
|
2021-02-23 14:13:04 +00:00
|
|
|
"github.com/caos/zitadel/internal/domain"
|
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
|
|
|
"github.com/caos/zitadel/internal/repository/iam"
|
|
|
|
"github.com/caos/zitadel/internal/repository/member"
|
|
|
|
"github.com/caos/zitadel/internal/repository/policy"
|
2021-01-04 13:52:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ReadModel struct {
|
|
|
|
eventstore.ReadModel
|
|
|
|
|
|
|
|
SetUpStarted domain.Step
|
|
|
|
SetUpDone domain.Step
|
|
|
|
|
|
|
|
Members IAMMembersReadModel
|
|
|
|
IDPs IAMIDPConfigsReadModel
|
|
|
|
|
|
|
|
GlobalOrgID string
|
|
|
|
ProjectID string
|
|
|
|
|
|
|
|
DefaultLoginPolicy IAMLoginPolicyReadModel
|
|
|
|
DefaultLabelPolicy IAMLabelPolicyReadModel
|
|
|
|
DefaultOrgIAMPolicy IAMOrgIAMPolicyReadModel
|
|
|
|
DefaultPasswordComplexityPolicy IAMPasswordComplexityPolicyReadModel
|
|
|
|
DefaultPasswordAgePolicy IAMPasswordAgePolicyReadModel
|
2021-08-11 06:36:32 +00:00
|
|
|
DefaultPasswordLockoutPolicy IAMLockoutPolicyReadModel
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewReadModel(id string) *ReadModel {
|
|
|
|
return &ReadModel{
|
|
|
|
ReadModel: eventstore.ReadModel{
|
|
|
|
AggregateID: id,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rm *ReadModel) IDPByID(idpID string) *IAMIDPConfigReadModel {
|
|
|
|
_, config := rm.IDPs.ConfigByID(idpID)
|
|
|
|
if config == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return &IAMIDPConfigReadModel{IDPConfigReadModel: *config}
|
|
|
|
}
|
|
|
|
|
2022-01-03 08:19:07 +00:00
|
|
|
func (rm *ReadModel) AppendEvents(events ...eventstore.Event) {
|
2021-01-04 13:52:13 +00:00
|
|
|
rm.ReadModel.AppendEvents(events...)
|
|
|
|
for _, event := range events {
|
|
|
|
switch event.(type) {
|
|
|
|
case *member.MemberAddedEvent,
|
|
|
|
*member.MemberChangedEvent,
|
|
|
|
*member.MemberRemovedEvent:
|
|
|
|
|
|
|
|
rm.Members.AppendEvents(event)
|
|
|
|
case *iam.IDPConfigAddedEvent,
|
|
|
|
*iam.IDPConfigChangedEvent,
|
|
|
|
*iam.IDPConfigDeactivatedEvent,
|
|
|
|
*iam.IDPConfigReactivatedEvent,
|
|
|
|
*iam.IDPConfigRemovedEvent,
|
|
|
|
*iam.IDPOIDCConfigAddedEvent,
|
|
|
|
*iam.IDPOIDCConfigChangedEvent:
|
|
|
|
|
|
|
|
rm.IDPs.AppendEvents(event)
|
|
|
|
case *policy.LabelPolicyAddedEvent,
|
|
|
|
*policy.LabelPolicyChangedEvent:
|
|
|
|
|
|
|
|
rm.DefaultLabelPolicy.AppendEvents(event)
|
|
|
|
case *policy.LoginPolicyAddedEvent,
|
|
|
|
*policy.LoginPolicyChangedEvent:
|
|
|
|
|
|
|
|
rm.DefaultLoginPolicy.AppendEvents(event)
|
|
|
|
case *policy.OrgIAMPolicyAddedEvent:
|
|
|
|
rm.DefaultOrgIAMPolicy.AppendEvents(event)
|
|
|
|
case *policy.PasswordComplexityPolicyAddedEvent,
|
|
|
|
*policy.PasswordComplexityPolicyChangedEvent:
|
|
|
|
|
|
|
|
rm.DefaultPasswordComplexityPolicy.AppendEvents(event)
|
|
|
|
case *policy.PasswordAgePolicyAddedEvent,
|
|
|
|
*policy.PasswordAgePolicyChangedEvent:
|
|
|
|
|
|
|
|
rm.DefaultPasswordAgePolicy.AppendEvents(event)
|
2021-08-11 06:36:32 +00:00
|
|
|
case *policy.LockoutPolicyAddedEvent,
|
|
|
|
*policy.LockoutPolicyChangedEvent:
|
2021-01-04 13:52:13 +00:00
|
|
|
|
|
|
|
rm.DefaultPasswordLockoutPolicy.AppendEvents(event)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rm *ReadModel) Reduce() (err error) {
|
|
|
|
for _, event := range rm.Events {
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *iam.ProjectSetEvent:
|
|
|
|
rm.ProjectID = e.ProjectID
|
|
|
|
case *iam.GlobalOrgSetEvent:
|
|
|
|
rm.GlobalOrgID = e.OrgID
|
|
|
|
case *iam.SetupStepEvent:
|
|
|
|
if e.Done {
|
|
|
|
rm.SetUpDone = e.Step
|
|
|
|
} else {
|
|
|
|
rm.SetUpStarted = e.Step
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, reduce := range []func() error{
|
|
|
|
rm.Members.Reduce,
|
|
|
|
rm.IDPs.Reduce,
|
|
|
|
rm.DefaultLoginPolicy.Reduce,
|
|
|
|
rm.DefaultLabelPolicy.Reduce,
|
|
|
|
rm.DefaultOrgIAMPolicy.Reduce,
|
|
|
|
rm.DefaultPasswordComplexityPolicy.Reduce,
|
|
|
|
rm.DefaultPasswordAgePolicy.Reduce,
|
|
|
|
rm.DefaultPasswordLockoutPolicy.Reduce,
|
|
|
|
rm.ReadModel.Reduce,
|
|
|
|
} {
|
|
|
|
if err = reduce(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-03 08:19:07 +00:00
|
|
|
func (rm *ReadModel) AppendAndReduce(events ...eventstore.Event) error {
|
2021-01-04 13:52:13 +00:00
|
|
|
rm.AppendEvents(events...)
|
|
|
|
return rm.Reduce()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rm *ReadModel) Query() *eventstore.SearchQueryBuilder {
|
2021-07-06 11:55:57 +00:00
|
|
|
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent).
|
|
|
|
AddQuery().
|
|
|
|
AggregateTypes(iam.AggregateType).
|
|
|
|
AggregateIDs(rm.AggregateID).
|
|
|
|
Builder()
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|