2021-01-04 13:52:13 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-01-03 08:19:07 +00:00
|
|
|
|
2022-05-24 09:28:17 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/api/authz"
|
|
|
|
"github.com/zitadel/zitadel/internal/command/preparation"
|
2022-04-26 23:01:45 +00: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 14:30:55 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2021-01-04 13:52:13 +00:00
|
|
|
)
|
|
|
|
|
2022-05-24 09:28:17 +00:00
|
|
|
func (c *Commands) AddDefaultPasswordAgePolicy(ctx context.Context, expireWarnDays, maxAgeDays uint64) (*domain.ObjectDetails, error) {
|
|
|
|
instanceAgg := instance.NewAggregate(authz.GetInstance(ctx).InstanceID())
|
|
|
|
cmds, err := preparation.PrepareCommands(ctx, c.eventstore.Filter, prepareAddDefaultPasswordAgePolicy(instanceAgg, expireWarnDays, maxAgeDays))
|
2021-01-04 13:52:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-05-24 09:28:17 +00:00
|
|
|
pushedEvents, err := c.eventstore.Push(ctx, cmds...)
|
2021-01-04 13:52:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-05-24 09:28:17 +00:00
|
|
|
return pushedEventsToObjectDetails(pushedEvents), nil
|
2021-01-06 09:47:55 +00:00
|
|
|
}
|
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
func (c *Commands) ChangeDefaultPasswordAgePolicy(ctx context.Context, policy *domain.PasswordAgePolicy) (*domain.PasswordAgePolicy, error) {
|
|
|
|
existingPolicy, err := c.defaultPasswordAgePolicyWriteModelByID(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 {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowNotFound(nil, "INSTANCE-0oPew", "Errors.IAM.PasswordAgePolicy.NotFound")
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
instanceAgg := InstanceAggregateFromWriteModel(&existingPolicy.PasswordAgePolicyWriteModel.WriteModel)
|
|
|
|
changedEvent, hasChanged := existingPolicy.NewChangedEvent(ctx, instanceAgg, policy.ExpireWarnDays, policy.MaxAgeDays)
|
2021-01-04 13:52:13 +00:00
|
|
|
if !hasChanged {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowPreconditionFailed(nil, "INSTANCE-180sf", "Errors.IAM.PasswordAgePolicy.NotChanged")
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
|
|
|
|
2022-01-03 08:19:07 +00:00
|
|
|
pushedEvents, err := c.eventstore.Push(ctx, changedEvent)
|
2021-02-18 13:48:27 +00:00
|
|
|
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 writeModelToPasswordAgePolicy(&existingPolicy.PasswordAgePolicyWriteModel), nil
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
func (c *Commands) defaultPasswordAgePolicyWriteModelByID(ctx context.Context) (policy *InstancePasswordAgePolicyWriteModel, err error) {
|
2021-01-04 13:52:13 +00:00
|
|
|
ctx, span := tracing.NewSpan(ctx)
|
|
|
|
defer func() { span.EndWithError(err) }()
|
|
|
|
|
2022-04-05 05:58:09 +00:00
|
|
|
writeModel := NewInstancePasswordAgePolicyWriteModel(ctx)
|
2021-02-24 10:17:39 +00:00
|
|
|
err = c.eventstore.FilterToQueryReducer(ctx, writeModel)
|
2021-01-04 13:52:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return writeModel, nil
|
|
|
|
}
|
2022-05-24 09:28:17 +00:00
|
|
|
|
|
|
|
func prepareAddDefaultPasswordAgePolicy(
|
|
|
|
a *instance.Aggregate,
|
|
|
|
expireWarnDays,
|
|
|
|
maxAgeDays uint64,
|
|
|
|
) preparation.Validation {
|
|
|
|
return func() (preparation.CreateCommands, error) {
|
|
|
|
return func(ctx context.Context, filter preparation.FilterToQueryReducer) ([]eventstore.Command, error) {
|
|
|
|
writeModel := NewInstancePasswordAgePolicyWriteModel(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 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowAlreadyExists(nil, "INSTANCE-Lk0dS", "Errors.Instance.PasswordAgePolicy.AlreadyExists")
|
2022-05-24 09:28:17 +00:00
|
|
|
}
|
|
|
|
return []eventstore.Command{
|
|
|
|
instance.NewPasswordAgePolicyAddedEvent(ctx, &a.Aggregate,
|
|
|
|
expireWarnDays,
|
|
|
|
maxAgeDays,
|
|
|
|
),
|
|
|
|
}, nil
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
}
|