zitadel/internal/v2/repository/iam/read_model.go

142 lines
4.1 KiB
Go
Raw Normal View History

2020-11-06 21:09:19 +00:00
package iam
import (
"github.com/caos/zitadel/internal/eventstore/v2"
iam_label "github.com/caos/zitadel/internal/v2/repository/iam/policy/label"
iam_login "github.com/caos/zitadel/internal/v2/repository/iam/policy/login"
iam_org_iam "github.com/caos/zitadel/internal/v2/repository/iam/policy/org_iam"
iam_password_age "github.com/caos/zitadel/internal/v2/repository/iam/policy/password_age"
iam_password_complexity "github.com/caos/zitadel/internal/v2/repository/iam/policy/password_complexity"
iam_password_lockout "github.com/caos/zitadel/internal/v2/repository/iam/policy/password_lockout"
2020-11-06 21:09:19 +00:00
"github.com/caos/zitadel/internal/v2/repository/member"
"github.com/caos/zitadel/internal/v2/repository/policy/label"
"github.com/caos/zitadel/internal/v2/repository/policy/login"
"github.com/caos/zitadel/internal/v2/repository/policy/org_iam"
"github.com/caos/zitadel/internal/v2/repository/policy/password_age"
"github.com/caos/zitadel/internal/v2/repository/policy/password_complexity"
"github.com/caos/zitadel/internal/v2/repository/policy/password_lockout"
2020-11-06 21:09:19 +00:00
)
type ReadModel struct {
eventstore.ReadModel
SetUpStarted Step
SetUpDone Step
2020-11-11 16:51:44 +00:00
Members MembersReadModel
2020-11-26 12:14:07 +00:00
IDPs IDPConfigsReadModel
2020-11-06 21:09:19 +00:00
GlobalOrgID string
ProjectID string
2020-11-11 16:51:44 +00:00
DefaultLoginPolicy iam_login.ReadModel
DefaultLabelPolicy iam_label.ReadModel
DefaultOrgIAMPolicy iam_org_iam.ReadModel
DefaultPasswordComplexityPolicy iam_password_complexity.ReadModel
DefaultPasswordAgePolicy iam_password_age.ReadModel
DefaultPasswordLockoutPolicy iam_password_lockout.ReadModel
2020-11-06 21:09:19 +00:00
}
2020-11-25 13:12:44 +00:00
func NewReadModel(id string) *ReadModel {
return &ReadModel{
ReadModel: eventstore.ReadModel{
AggregateID: id,
},
}
}
2020-11-26 12:14:07 +00:00
func (rm *ReadModel) IDPByID(idpID string) *IDPConfigReadModel {
_, config := rm.IDPs.ConfigByID(idpID)
if config == nil {
return nil
}
return &IDPConfigReadModel{ConfigReadModel: *config}
}
2020-11-25 13:12:44 +00:00
func (rm *ReadModel) AppendEvents(events ...eventstore.EventReader) {
2020-11-06 21:09:19 +00:00
rm.ReadModel.AppendEvents(events...)
for _, event := range events {
switch event.(type) {
2020-11-26 12:14:07 +00:00
case *member.AddedEvent,
*member.ChangedEvent,
*member.RemovedEvent:
2020-11-11 16:51:44 +00:00
rm.Members.AppendEvents(event)
2020-11-26 12:14:07 +00:00
case *IDPConfigAddedEvent,
*IDPConfigChangedEvent,
*IDPConfigDeactivatedEvent,
*IDPConfigReactivatedEvent,
*IDPConfigRemovedEvent,
*IDPOIDCConfigAddedEvent,
*IDPOIDCConfigChangedEvent:
rm.IDPs.AppendEvents(event)
case *label.AddedEvent,
*label.ChangedEvent:
2020-11-26 12:14:07 +00:00
2020-11-11 16:51:44 +00:00
rm.DefaultLabelPolicy.AppendEvents(event)
case *login.AddedEvent,
*login.ChangedEvent:
2020-11-26 12:14:07 +00:00
2020-11-11 16:51:44 +00:00
rm.DefaultLoginPolicy.AppendEvents(event)
case *org_iam.AddedEvent:
2020-11-11 16:51:44 +00:00
rm.DefaultOrgIAMPolicy.AppendEvents(event)
case *password_complexity.AddedEvent,
*password_complexity.ChangedEvent:
2020-11-26 12:14:07 +00:00
2020-11-11 16:51:44 +00:00
rm.DefaultPasswordComplexityPolicy.AppendEvents(event)
case *password_age.AddedEvent,
*password_age.ChangedEvent:
2020-11-26 12:14:07 +00:00
2020-11-11 16:51:44 +00:00
rm.DefaultPasswordAgePolicy.AppendEvents(event)
case *password_lockout.AddedEvent,
*password_lockout.ChangedEvent:
2020-11-26 12:14:07 +00:00
2020-11-11 16:51:44 +00:00
rm.DefaultPasswordLockoutPolicy.AppendEvents(event)
2020-11-06 21:09:19 +00:00
}
}
}
func (rm *ReadModel) Reduce() (err error) {
for _, event := range rm.Events {
switch e := event.(type) {
case *ProjectSetEvent:
rm.ProjectID = e.ProjectID
case *GlobalOrgSetEvent:
rm.GlobalOrgID = e.OrgID
case *SetupStepEvent:
if e.Done {
rm.SetUpDone = e.Step
} else {
rm.SetUpStarted = e.Step
}
}
}
2020-11-11 16:51:44 +00:00
for _, reduce := range []func() error{
rm.Members.Reduce,
2020-11-26 12:14:07 +00:00
rm.IDPs.Reduce,
2020-11-11 16:51:44 +00:00
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
}
2020-11-06 21:09:19 +00:00
}
2020-11-11 16:51:44 +00:00
return nil
2020-11-06 21:09:19 +00:00
}
func (rm *ReadModel) AppendAndReduce(events ...eventstore.EventReader) error {
2020-11-25 19:04:32 +00:00
rm.AppendEvents(events...)
return rm.Reduce()
}
2020-11-25 13:12:44 +00:00
2020-12-01 13:44:19 +00:00
func (rm *ReadModel) Query() *eventstore.SearchQueryBuilder {
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent, AggregateType).AggregateIDs(rm.AggregateID)
2020-11-25 13:12:44 +00:00
}