2024-02-28 08:55:54 +00:00
|
|
|
package query
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/zitadel/zitadel/internal/api/authz"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
"github.com/zitadel/zitadel/internal/feature"
|
|
|
|
feature_v1 "github.com/zitadel/zitadel/internal/repository/feature"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/feature/feature_v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
type InstanceFeaturesReadModel struct {
|
|
|
|
*eventstore.ReadModel
|
|
|
|
system *SystemFeatures
|
|
|
|
instance *InstanceFeatures
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewInstanceFeaturesReadModel(ctx context.Context, system *SystemFeatures) *InstanceFeaturesReadModel {
|
|
|
|
instanceID := authz.GetInstance(ctx).InstanceID()
|
|
|
|
m := &InstanceFeaturesReadModel{
|
|
|
|
ReadModel: &eventstore.ReadModel{
|
|
|
|
AggregateID: instanceID,
|
|
|
|
ResourceOwner: instanceID,
|
|
|
|
},
|
|
|
|
instance: new(InstanceFeatures),
|
|
|
|
system: system,
|
|
|
|
}
|
|
|
|
m.populateFromSystem()
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *InstanceFeaturesReadModel) Reduce() (err error) {
|
|
|
|
for _, event := range m.Events {
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *feature_v2.ResetEvent:
|
|
|
|
m.reduceReset()
|
|
|
|
case *feature_v1.SetEvent[feature_v1.Boolean]:
|
2024-05-24 11:32:57 +00:00
|
|
|
err = reduceInstanceFeatureSet(
|
|
|
|
m.instance,
|
2024-02-28 08:55:54 +00:00
|
|
|
feature_v1.DefaultLoginInstanceEventToV2(e),
|
|
|
|
)
|
|
|
|
case *feature_v2.SetEvent[bool]:
|
2024-05-24 11:32:57 +00:00
|
|
|
err = reduceInstanceFeatureSet(m.instance, e)
|
|
|
|
case *feature_v2.SetEvent[[]feature.ImprovedPerformanceType]:
|
|
|
|
err = reduceInstanceFeatureSet(m.instance, e)
|
2024-02-28 08:55:54 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return m.ReadModel.Reduce()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *InstanceFeaturesReadModel) Query() *eventstore.SearchQueryBuilder {
|
|
|
|
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent).
|
|
|
|
AwaitOpenTransactions().
|
|
|
|
AddQuery().
|
|
|
|
AggregateTypes(feature_v2.AggregateType).
|
|
|
|
EventTypes(
|
|
|
|
feature_v1.DefaultLoginInstanceEventType,
|
|
|
|
feature_v2.InstanceResetEventType,
|
|
|
|
feature_v2.InstanceLoginDefaultOrgEventType,
|
|
|
|
feature_v2.InstanceTriggerIntrospectionProjectionsEventType,
|
|
|
|
feature_v2.InstanceLegacyIntrospectionEventType,
|
2024-03-12 13:50:13 +00:00
|
|
|
feature_v2.InstanceUserSchemaEventType,
|
2024-03-20 10:18:46 +00:00
|
|
|
feature_v2.InstanceTokenExchangeEventType,
|
2024-04-09 17:21:21 +00:00
|
|
|
feature_v2.InstanceActionsEventType,
|
2024-05-24 11:32:57 +00:00
|
|
|
feature_v2.InstanceImprovedPerformanceEventType,
|
2024-08-14 14:18:14 +00:00
|
|
|
feature_v2.InstanceWebKeyEventType,
|
2024-08-20 06:45:24 +00:00
|
|
|
feature_v2.InstanceDebugOIDCParentErrorEventType,
|
2024-09-04 10:14:50 +00:00
|
|
|
feature_v2.InstanceOIDCSingleV1SessionTerminationEventType,
|
2024-09-26 13:55:41 +00:00
|
|
|
feature_v2.InstanceDisableUserTokenEvent,
|
2024-02-28 08:55:54 +00:00
|
|
|
).
|
|
|
|
Builder().ResourceOwner(m.ResourceOwner)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *InstanceFeaturesReadModel) reduceReset() {
|
|
|
|
if m.populateFromSystem() {
|
|
|
|
return
|
|
|
|
}
|
2024-05-24 11:32:57 +00:00
|
|
|
m.instance = nil
|
|
|
|
m.instance = new(InstanceFeatures)
|
2024-02-28 08:55:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *InstanceFeaturesReadModel) populateFromSystem() bool {
|
|
|
|
if m.system == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
m.instance.LoginDefaultOrg = m.system.LoginDefaultOrg
|
|
|
|
m.instance.TriggerIntrospectionProjections = m.system.TriggerIntrospectionProjections
|
|
|
|
m.instance.LegacyIntrospection = m.system.LegacyIntrospection
|
2024-03-12 13:50:13 +00:00
|
|
|
m.instance.UserSchema = m.system.UserSchema
|
2024-03-20 10:18:46 +00:00
|
|
|
m.instance.TokenExchange = m.system.TokenExchange
|
2024-04-09 17:21:21 +00:00
|
|
|
m.instance.Actions = m.system.Actions
|
2024-05-24 11:32:57 +00:00
|
|
|
m.instance.ImprovedPerformance = m.system.ImprovedPerformance
|
2024-09-04 10:14:50 +00:00
|
|
|
m.instance.OIDCSingleV1SessionTermination = m.system.OIDCSingleV1SessionTermination
|
2024-09-26 13:55:41 +00:00
|
|
|
m.instance.DisableUserTokenEvent = m.system.DisableUserTokenEvent
|
2024-02-28 08:55:54 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2024-05-24 11:32:57 +00:00
|
|
|
func reduceInstanceFeatureSet[T any](features *InstanceFeatures, event *feature_v2.SetEvent[T]) error {
|
2024-02-28 08:55:54 +00:00
|
|
|
level, key, err := event.FeatureInfo()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
switch key {
|
|
|
|
case feature.KeyUnspecified:
|
|
|
|
return nil
|
|
|
|
case feature.KeyLoginDefaultOrg:
|
2024-05-24 11:32:57 +00:00
|
|
|
features.LoginDefaultOrg.set(level, event.Value)
|
2024-02-28 08:55:54 +00:00
|
|
|
case feature.KeyTriggerIntrospectionProjections:
|
2024-05-24 11:32:57 +00:00
|
|
|
features.TriggerIntrospectionProjections.set(level, event.Value)
|
2024-02-28 08:55:54 +00:00
|
|
|
case feature.KeyLegacyIntrospection:
|
2024-05-24 11:32:57 +00:00
|
|
|
features.LegacyIntrospection.set(level, event.Value)
|
2024-03-12 13:50:13 +00:00
|
|
|
case feature.KeyUserSchema:
|
2024-05-24 11:32:57 +00:00
|
|
|
features.UserSchema.set(level, event.Value)
|
2024-03-20 10:18:46 +00:00
|
|
|
case feature.KeyTokenExchange:
|
2024-05-24 11:32:57 +00:00
|
|
|
features.TokenExchange.set(level, event.Value)
|
2024-04-09 17:21:21 +00:00
|
|
|
case feature.KeyActions:
|
2024-05-24 11:32:57 +00:00
|
|
|
features.Actions.set(level, event.Value)
|
|
|
|
case feature.KeyImprovedPerformance:
|
|
|
|
features.ImprovedPerformance.set(level, event.Value)
|
2024-08-14 14:18:14 +00:00
|
|
|
case feature.KeyWebKey:
|
|
|
|
features.WebKey.set(level, event.Value)
|
2024-08-20 06:45:24 +00:00
|
|
|
case feature.KeyDebugOIDCParentError:
|
|
|
|
features.DebugOIDCParentError.set(level, event.Value)
|
2024-09-04 10:14:50 +00:00
|
|
|
case feature.KeyOIDCSingleV1SessionTermination:
|
|
|
|
features.OIDCSingleV1SessionTermination.set(level, event.Value)
|
2024-09-26 13:55:41 +00:00
|
|
|
case feature.KeyDisableUserTokenEvent:
|
|
|
|
features.DisableUserTokenEvent.set(level, event.Value)
|
2024-02-28 08:55:54 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|