2021-01-04 13:52:13 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
caos_errs "github.com/caos/zitadel/internal/errors"
|
2021-02-18 13:48:27 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore/v2"
|
2021-01-04 13:52:13 +00:00
|
|
|
"github.com/caos/zitadel/internal/telemetry/tracing"
|
2021-01-05 08:33:45 +00:00
|
|
|
"github.com/caos/zitadel/internal/v2/domain"
|
2021-01-04 13:52:13 +00:00
|
|
|
iam_repo "github.com/caos/zitadel/internal/v2/repository/iam"
|
|
|
|
)
|
|
|
|
|
2021-01-05 08:33:45 +00:00
|
|
|
func (r *CommandSide) AddDefaultLabelPolicy(ctx context.Context, policy *domain.LabelPolicy) (*domain.LabelPolicy, error) {
|
2021-01-12 11:59:51 +00:00
|
|
|
addedPolicy := NewIAMLabelPolicyWriteModel()
|
2021-01-06 09:47:55 +00:00
|
|
|
iamAgg := IAMAggregateFromWriteModel(&addedPolicy.LabelPolicyWriteModel.WriteModel)
|
2021-02-18 13:48:27 +00:00
|
|
|
event, err := r.addDefaultLabelPolicy(ctx, iamAgg, addedPolicy, policy)
|
2021-01-04 13:52:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-02-18 13:48:27 +00:00
|
|
|
pushedEvents, err := r.eventstore.PushEvents(ctx, event)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = AppendAndReduce(addedPolicy, pushedEvents...)
|
2021-01-04 13:52:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-01-18 10:24:15 +00:00
|
|
|
return writeModelToLabelPolicy(&addedPolicy.LabelPolicyWriteModel), nil
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
|
|
|
|
2021-02-18 13:48:27 +00:00
|
|
|
func (r *CommandSide) addDefaultLabelPolicy(ctx context.Context, iamAgg *eventstore.Aggregate, addedPolicy *IAMLabelPolicyWriteModel, policy *domain.LabelPolicy) (eventstore.EventPusher, error) {
|
2021-01-06 09:47:55 +00:00
|
|
|
err := r.eventstore.FilterToQueryReducer(ctx, addedPolicy)
|
|
|
|
if err != nil {
|
2021-02-18 13:48:27 +00:00
|
|
|
return nil, err
|
2021-01-06 09:47:55 +00:00
|
|
|
}
|
2021-01-07 15:06:45 +00:00
|
|
|
if addedPolicy.State == domain.PolicyStateActive {
|
2021-02-18 13:48:27 +00:00
|
|
|
return nil, caos_errs.ThrowAlreadyExists(nil, "IAM-2B0ps", "Errors.IAM.LabelPolicy.AlreadyExists")
|
2021-01-06 09:47:55 +00:00
|
|
|
}
|
|
|
|
|
2021-02-18 13:48:27 +00:00
|
|
|
return iam_repo.NewLabelPolicyAddedEvent(ctx, iamAgg, policy.PrimaryColor, policy.SecondaryColor), nil
|
2021-01-06 09:47:55 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-01-05 08:33:45 +00:00
|
|
|
func (r *CommandSide) ChangeDefaultLabelPolicy(ctx context.Context, policy *domain.LabelPolicy) (*domain.LabelPolicy, error) {
|
2021-01-12 11:59:51 +00:00
|
|
|
existingPolicy, err := r.defaultLabelPolicyWriteModelByID(ctx)
|
2021-01-04 13:52:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-01-07 15:06:45 +00:00
|
|
|
if existingPolicy.State == domain.PolicyStateUnspecified || existingPolicy.State == domain.PolicyStateRemoved {
|
|
|
|
return nil, caos_errs.ThrowNotFound(nil, "IAM-0K9dq", "Errors.IAM.LabelPolicy.NotFound")
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
2021-02-18 13:48:27 +00:00
|
|
|
iamAgg := IAMAggregateFromWriteModel(&existingPolicy.LabelPolicyWriteModel.WriteModel)
|
|
|
|
changedEvent, hasChanged := existingPolicy.NewChangedEvent(ctx, iamAgg, policy.PrimaryColor, policy.SecondaryColor)
|
2021-01-04 13:52:13 +00:00
|
|
|
if !hasChanged {
|
2021-01-07 15:06:45 +00:00
|
|
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "IAM-4M9vs", "Errors.IAM.LabelPolicy.NotChanged")
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
|
|
|
|
2021-02-18 13:48:27 +00:00
|
|
|
pushedEvents, err := r.eventstore.PushEvents(ctx, changedEvent)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = AppendAndReduce(existingPolicy, pushedEvents...)
|
2021-01-04 13:52:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-01-18 10:24:15 +00:00
|
|
|
return writeModelToLabelPolicy(&existingPolicy.LabelPolicyWriteModel), nil
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
|
|
|
|
2021-01-12 11:59:51 +00:00
|
|
|
func (r *CommandSide) defaultLabelPolicyWriteModelByID(ctx context.Context) (policy *IAMLabelPolicyWriteModel, err error) {
|
2021-01-04 13:52:13 +00:00
|
|
|
ctx, span := tracing.NewSpan(ctx)
|
|
|
|
defer func() { span.EndWithError(err) }()
|
|
|
|
|
2021-01-12 11:59:51 +00:00
|
|
|
writeModel := NewIAMLabelPolicyWriteModel()
|
2021-01-04 13:52:13 +00:00
|
|
|
err = r.eventstore.FilterToQueryReducer(ctx, writeModel)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return writeModel, nil
|
|
|
|
}
|