2024-02-28 10:55:54 +02:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/zitadel/zitadel/internal/api/authz"
|
|
|
|
"github.com/zitadel/zitadel/internal/command/preparation"
|
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
2024-05-24 13:32:57 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/feature"
|
2024-02-28 10:55:54 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/feature/feature_v2"
|
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type InstanceFeatures struct {
|
|
|
|
LoginDefaultOrg *bool
|
|
|
|
TriggerIntrospectionProjections *bool
|
|
|
|
LegacyIntrospection *bool
|
2024-03-12 14:50:13 +01:00
|
|
|
UserSchema *bool
|
2024-03-20 12:18:46 +02:00
|
|
|
TokenExchange *bool
|
2024-04-09 19:21:21 +02:00
|
|
|
Actions *bool
|
2024-05-24 13:32:57 +02:00
|
|
|
ImprovedPerformance []feature.ImprovedPerformanceType
|
2024-02-28 10:55:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *InstanceFeatures) isEmpty() bool {
|
|
|
|
return m.LoginDefaultOrg == nil &&
|
|
|
|
m.TriggerIntrospectionProjections == nil &&
|
2024-03-12 14:50:13 +01:00
|
|
|
m.LegacyIntrospection == nil &&
|
2024-03-20 12:18:46 +02:00
|
|
|
m.UserSchema == nil &&
|
2024-04-09 19:21:21 +02:00
|
|
|
m.TokenExchange == nil &&
|
2024-05-24 13:32:57 +02:00
|
|
|
m.Actions == nil &&
|
|
|
|
// nil check to allow unset improvements
|
|
|
|
m.ImprovedPerformance == nil
|
2024-02-28 10:55:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Commands) SetInstanceFeatures(ctx context.Context, f *InstanceFeatures) (*domain.ObjectDetails, error) {
|
|
|
|
if f.isEmpty() {
|
|
|
|
return nil, zerrors.ThrowInvalidArgument(nil, "COMMAND-Vigh1", "Errors.NoChangesFound")
|
|
|
|
}
|
|
|
|
wm := NewInstanceFeaturesWriteModel(authz.GetInstance(ctx).InstanceID())
|
|
|
|
if err := c.eventstore.FilterToQueryReducer(ctx, wm); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-05-24 13:32:57 +02:00
|
|
|
commands := wm.setCommands(ctx, f)
|
|
|
|
if len(commands) == 0 {
|
2024-02-28 10:55:54 +02:00
|
|
|
return writeModelToObjectDetails(wm.WriteModel), nil
|
|
|
|
}
|
2024-05-24 13:32:57 +02:00
|
|
|
events, err := c.eventstore.Push(ctx, commands...)
|
2024-02-28 10:55:54 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return pushedEventsToObjectDetails(events), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func prepareSetFeatures(instanceID string, f *InstanceFeatures) preparation.Validation {
|
|
|
|
return func() (preparation.CreateCommands, error) {
|
|
|
|
return func(ctx context.Context, _ preparation.FilterToQueryReducer) ([]eventstore.Command, error) {
|
|
|
|
wm := NewInstanceFeaturesWriteModel(instanceID)
|
|
|
|
return wm.setCommands(ctx, f), nil
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Commands) ResetInstanceFeatures(ctx context.Context) (*domain.ObjectDetails, error) {
|
|
|
|
instanceID := authz.GetInstance(ctx).InstanceID()
|
|
|
|
wm := NewInstanceFeaturesWriteModel(instanceID)
|
|
|
|
if err := c.eventstore.FilterToQueryReducer(ctx, wm); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if wm.isEmpty() {
|
|
|
|
return writeModelToObjectDetails(wm.WriteModel), nil
|
|
|
|
}
|
|
|
|
aggregate := feature_v2.NewAggregate(instanceID, instanceID)
|
|
|
|
events, err := c.eventstore.Push(ctx, feature_v2.NewResetEvent(ctx, aggregate, feature_v2.InstanceResetEventType))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return pushedEventsToObjectDetails(events), nil
|
|
|
|
}
|