2021-07-05 10:36:51 +02:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-01-03 09:19:07 +01:00
|
|
|
|
2022-05-24 11:28:17 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/api/authz"
|
|
|
|
"github.com/zitadel/zitadel/internal/command/preparation"
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/instance"
|
|
|
|
"github.com/zitadel/zitadel/internal/telemetry/tracing"
|
2023-12-08 16:30:55 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2021-07-05 10:36:51 +02:00
|
|
|
)
|
|
|
|
|
2023-03-28 21:36:52 +02:00
|
|
|
func (c *Commands) AddDefaultPrivacyPolicy(ctx context.Context, tosLink, privacyLink, helpLink string, supportEmail domain.EmailAddress) (*domain.ObjectDetails, error) {
|
2022-05-24 11:28:17 +02:00
|
|
|
instanceAgg := instance.NewAggregate(authz.GetInstance(ctx).InstanceID())
|
2023-03-28 21:36:52 +02:00
|
|
|
cmds, err := preparation.PrepareCommands(ctx, c.eventstore.Filter, prepareAddDefaultPrivacyPolicy(instanceAgg, tosLink, privacyLink, helpLink, supportEmail))
|
2021-07-05 10:36:51 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-05-24 11:28:17 +02:00
|
|
|
pushedEvents, err := c.eventstore.Push(ctx, cmds...)
|
2021-07-05 10:36:51 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-05-24 11:28:17 +02:00
|
|
|
return pushedEventsToObjectDetails(pushedEvents), nil
|
2021-07-05 10:36:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Commands) ChangeDefaultPrivacyPolicy(ctx context.Context, policy *domain.PrivacyPolicy) (*domain.PrivacyPolicy, error) {
|
2023-03-28 21:36:52 +02:00
|
|
|
if policy.SupportEmail != "" {
|
|
|
|
if err := policy.SupportEmail.Validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
policy.SupportEmail = policy.SupportEmail.Normalize()
|
|
|
|
}
|
|
|
|
|
2021-07-05 10:36:51 +02:00
|
|
|
existingPolicy, err := c.defaultPrivacyPolicyWriteModelByID(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if existingPolicy.State == domain.PolicyStateUnspecified || existingPolicy.State == domain.PolicyStateRemoved {
|
2023-12-08 16:30:55 +02:00
|
|
|
return nil, zerrors.ThrowNotFound(nil, "INSTANCE-0oPew", "Errors.IAM.PrivacyPolicy.NotFound")
|
2021-07-05 10:36:51 +02:00
|
|
|
}
|
|
|
|
|
2022-03-24 17:21:34 +01:00
|
|
|
instanceAgg := InstanceAggregateFromWriteModel(&existingPolicy.PrivacyPolicyWriteModel.WriteModel)
|
2023-03-28 21:36:52 +02:00
|
|
|
changedEvent, hasChanged := existingPolicy.NewChangedEvent(ctx, instanceAgg, policy.TOSLink, policy.PrivacyLink, policy.HelpLink, policy.SupportEmail)
|
2021-07-05 10:36:51 +02:00
|
|
|
if !hasChanged {
|
2023-12-08 16:30:55 +02:00
|
|
|
return nil, zerrors.ThrowPreconditionFailed(nil, "INSTANCE-9jJfs", "Errors.IAM.PrivacyPolicy.NotChanged")
|
2021-07-05 10:36:51 +02:00
|
|
|
}
|
2022-01-03 09:19:07 +01:00
|
|
|
pushedEvents, err := c.eventstore.Push(ctx, changedEvent)
|
2021-07-05 10:36:51 +02: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 17:21:34 +01:00
|
|
|
func (c *Commands) defaultPrivacyPolicyWriteModelByID(ctx context.Context) (policy *InstancePrivacyPolicyWriteModel, err error) {
|
2021-07-05 10:36:51 +02:00
|
|
|
ctx, span := tracing.NewSpan(ctx)
|
|
|
|
defer func() { span.EndWithError(err) }()
|
|
|
|
|
2022-04-05 07:58:09 +02:00
|
|
|
writeModel := NewInstancePrivacyPolicyWriteModel(ctx)
|
2021-07-05 10:36:51 +02:00
|
|
|
err = c.eventstore.FilterToQueryReducer(ctx, writeModel)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return writeModel, nil
|
|
|
|
}
|
2022-05-24 11:28:17 +02:00
|
|
|
|
|
|
|
func (c *Commands) getDefaultPrivacyPolicy(ctx context.Context) (*domain.PrivacyPolicy, error) {
|
|
|
|
policyWriteModel := NewInstancePrivacyPolicyWriteModel(ctx)
|
|
|
|
err := c.eventstore.FilterToQueryReducer(ctx, policyWriteModel)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !policyWriteModel.State.Exists() {
|
2023-12-08 16:30:55 +02:00
|
|
|
return nil, zerrors.ThrowInvalidArgument(nil, "INSTANCE-559os", "Errors.IAM.PrivacyPolicy.NotFound")
|
2022-05-24 11:28:17 +02:00
|
|
|
}
|
|
|
|
policy := writeModelToPrivacyPolicy(&policyWriteModel.PrivacyPolicyWriteModel)
|
|
|
|
policy.Default = true
|
|
|
|
return policy, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func prepareAddDefaultPrivacyPolicy(
|
|
|
|
a *instance.Aggregate,
|
|
|
|
tosLink,
|
|
|
|
privacyLink,
|
|
|
|
helpLink string,
|
2023-03-28 21:36:52 +02:00
|
|
|
supportEmail domain.EmailAddress,
|
2022-05-24 11:28:17 +02:00
|
|
|
) preparation.Validation {
|
|
|
|
return func() (preparation.CreateCommands, error) {
|
2023-03-28 21:36:52 +02:00
|
|
|
if supportEmail != "" {
|
|
|
|
if err := supportEmail.Validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
supportEmail = supportEmail.Normalize()
|
|
|
|
}
|
2022-05-24 11:28:17 +02:00
|
|
|
return func(ctx context.Context, filter preparation.FilterToQueryReducer) ([]eventstore.Command, error) {
|
|
|
|
writeModel := NewInstancePrivacyPolicyWriteModel(ctx)
|
|
|
|
events, err := filter(ctx, writeModel.Query())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
writeModel.AppendEvents(events...)
|
|
|
|
if err = writeModel.Reduce(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if writeModel.State == domain.PolicyStateActive {
|
2023-12-08 16:30:55 +02:00
|
|
|
return nil, zerrors.ThrowAlreadyExists(nil, "INSTANCE-M00rJ", "Errors.Instance.PrivacyPolicy.AlreadyExists")
|
2022-05-24 11:28:17 +02:00
|
|
|
}
|
|
|
|
return []eventstore.Command{
|
2023-03-28 21:36:52 +02:00
|
|
|
instance.NewPrivacyPolicyAddedEvent(ctx, &a.Aggregate, tosLink, privacyLink, helpLink, supportEmail),
|
2022-05-24 11:28:17 +02:00
|
|
|
}, nil
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
}
|