2021-07-05 08:36:51 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-01-03 08:19:07 +00:00
|
|
|
|
2021-07-05 08:36:51 +00:00
|
|
|
"github.com/caos/zitadel/internal/domain"
|
|
|
|
caos_errs "github.com/caos/zitadel/internal/errors"
|
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
2022-03-24 16:21:34 +00:00
|
|
|
"github.com/caos/zitadel/internal/repository/instance"
|
2021-07-05 08:36:51 +00:00
|
|
|
"github.com/caos/zitadel/internal/telemetry/tracing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (c *Commands) getDefaultPrivacyPolicy(ctx context.Context) (*domain.PrivacyPolicy, error) {
|
2022-04-05 05:58:09 +00:00
|
|
|
policyWriteModel := NewInstancePrivacyPolicyWriteModel(ctx)
|
2021-07-05 08:36:51 +00:00
|
|
|
err := c.eventstore.FilterToQueryReducer(ctx, policyWriteModel)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !policyWriteModel.State.Exists() {
|
2022-03-24 16:21:34 +00:00
|
|
|
return nil, caos_errs.ThrowInvalidArgument(nil, "INSTANCE-559os", "Errors.IAM.PasswordAgePolicy.NotFound")
|
2021-07-05 08:36:51 +00:00
|
|
|
}
|
|
|
|
policy := writeModelToPrivacyPolicy(&policyWriteModel.PrivacyPolicyWriteModel)
|
|
|
|
policy.Default = true
|
|
|
|
return policy, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Commands) AddDefaultPrivacyPolicy(ctx context.Context, policy *domain.PrivacyPolicy) (*domain.PrivacyPolicy, error) {
|
2022-04-05 05:58:09 +00:00
|
|
|
addedPolicy := NewInstancePrivacyPolicyWriteModel(ctx)
|
2022-03-24 16:21:34 +00:00
|
|
|
instanceAgg := InstanceAggregateFromWriteModel(&addedPolicy.WriteModel)
|
|
|
|
events, err := c.addDefaultPrivacyPolicy(ctx, instanceAgg, addedPolicy, policy)
|
2021-07-05 08:36:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-01-03 08:19:07 +00:00
|
|
|
pushedEvents, err := c.eventstore.Push(ctx, events)
|
2021-07-05 08:36:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = AppendAndReduce(addedPolicy, pushedEvents...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return writeModelToPrivacyPolicy(&addedPolicy.PrivacyPolicyWriteModel), nil
|
|
|
|
}
|
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
func (c *Commands) addDefaultPrivacyPolicy(ctx context.Context, instanceAgg *eventstore.Aggregate, addedPolicy *InstancePrivacyPolicyWriteModel, policy *domain.PrivacyPolicy) (eventstore.Command, error) {
|
2021-07-05 08:36:51 +00:00
|
|
|
err := c.eventstore.FilterToQueryReducer(ctx, addedPolicy)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if addedPolicy.State == domain.PolicyStateActive {
|
2022-03-24 16:21:34 +00:00
|
|
|
return nil, caos_errs.ThrowAlreadyExists(nil, "INSTANCE-M00rJ", "Errors.IAM.PrivacyPolicy.AlreadyExists")
|
2021-07-05 08:36:51 +00:00
|
|
|
}
|
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
return instance.NewPrivacyPolicyAddedEvent(ctx, instanceAgg, policy.TOSLink, policy.PrivacyLink, policy.HelpLink), nil
|
2021-07-05 08:36:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Commands) ChangeDefaultPrivacyPolicy(ctx context.Context, policy *domain.PrivacyPolicy) (*domain.PrivacyPolicy, error) {
|
|
|
|
existingPolicy, err := c.defaultPrivacyPolicyWriteModelByID(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if existingPolicy.State == domain.PolicyStateUnspecified || existingPolicy.State == domain.PolicyStateRemoved {
|
2022-03-24 16:21:34 +00:00
|
|
|
return nil, caos_errs.ThrowNotFound(nil, "INSTANCE-0oPew", "Errors.IAM.PasswordAgePolicy.NotFound")
|
2021-07-05 08:36:51 +00:00
|
|
|
}
|
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
instanceAgg := InstanceAggregateFromWriteModel(&existingPolicy.PrivacyPolicyWriteModel.WriteModel)
|
|
|
|
changedEvent, hasChanged := existingPolicy.NewChangedEvent(ctx, instanceAgg, policy.TOSLink, policy.PrivacyLink, policy.HelpLink)
|
2021-07-05 08:36:51 +00:00
|
|
|
if !hasChanged {
|
2022-03-24 16:21:34 +00:00
|
|
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "INSTANCE-4M9vs", "Errors.IAM.LabelPolicy.NotChanged")
|
2021-07-05 08:36:51 +00:00
|
|
|
}
|
2022-01-03 08:19:07 +00:00
|
|
|
pushedEvents, err := c.eventstore.Push(ctx, changedEvent)
|
2021-07-05 08:36:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = AppendAndReduce(existingPolicy, pushedEvents...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return writeModelToPrivacyPolicy(&existingPolicy.PrivacyPolicyWriteModel), nil
|
|
|
|
}
|
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
func (c *Commands) defaultPrivacyPolicyWriteModelByID(ctx context.Context) (policy *InstancePrivacyPolicyWriteModel, err error) {
|
2021-07-05 08:36:51 +00:00
|
|
|
ctx, span := tracing.NewSpan(ctx)
|
|
|
|
defer func() { span.EndWithError(err) }()
|
|
|
|
|
2022-04-05 05:58:09 +00:00
|
|
|
writeModel := NewInstancePrivacyPolicyWriteModel(ctx)
|
2021-07-05 08:36:51 +00:00
|
|
|
err = c.eventstore.FilterToQueryReducer(ctx, writeModel)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return writeModel, nil
|
|
|
|
}
|