2023-10-25 13:42:00 +02:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-01-17 11:16:48 +01:00
|
|
|
"errors"
|
2023-10-25 13:42:00 +02:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"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"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/limits"
|
2023-12-08 16:30:55 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2023-10-25 13:42:00 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type SetLimits struct {
|
2023-11-22 10:29:38 +01:00
|
|
|
AuditLogRetention *time.Duration
|
2024-01-17 11:16:48 +01:00
|
|
|
Block *bool
|
2023-10-25 13:42:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetLimits creates new limits or updates existing limits.
|
|
|
|
func (c *Commands) SetLimits(
|
|
|
|
ctx context.Context,
|
|
|
|
setLimits *SetLimits,
|
|
|
|
) (*domain.ObjectDetails, error) {
|
|
|
|
instanceId := authz.GetInstance(ctx).InstanceID()
|
2024-01-17 11:16:48 +01:00
|
|
|
wm, err := c.getLimitsWriteModel(ctx, instanceId)
|
2023-10-25 13:42:00 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-01-17 11:16:48 +01:00
|
|
|
cmds, err := c.setLimitsCommands(ctx, wm, setLimits)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(cmds) > 0 {
|
|
|
|
events, err := c.eventstore.Push(ctx, cmds...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = AppendAndReduce(wm, events...)
|
2023-10-25 13:42:00 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2024-01-17 11:16:48 +01:00
|
|
|
return writeModelToObjectDetails(&wm.WriteModel), err
|
|
|
|
}
|
|
|
|
|
|
|
|
type SetInstanceLimitsBulk struct {
|
|
|
|
InstanceID string
|
|
|
|
SetLimits
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Commands) SetInstanceLimitsBulk(
|
|
|
|
ctx context.Context,
|
|
|
|
bulk []*SetInstanceLimitsBulk,
|
|
|
|
) (bulkDetails *domain.ObjectDetails, targetsDetails []*domain.ObjectDetails, err error) {
|
|
|
|
bulkWm, err := c.getBulkInstanceLimitsWriteModel(ctx, bulk)
|
2023-10-25 13:42:00 +02:00
|
|
|
if err != nil {
|
2024-01-17 11:16:48 +01:00
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
cmds := make([]eventstore.Command, 0)
|
|
|
|
for _, t := range bulk {
|
|
|
|
targetWM, ok := bulkWm.writeModels[t.InstanceID]
|
|
|
|
if !ok {
|
|
|
|
return nil, nil, zerrors.ThrowInternal(nil, "COMMAND-5HWA9", "Errors.Limits.NotFound")
|
|
|
|
}
|
|
|
|
targetCMDs, setErr := c.setLimitsCommands(ctx, targetWM, &t.SetLimits)
|
|
|
|
err = errors.Join(err, setErr)
|
|
|
|
cmds = append(cmds, targetCMDs...)
|
2023-10-25 13:42:00 +02:00
|
|
|
}
|
|
|
|
if err != nil {
|
2024-01-17 11:16:48 +01:00
|
|
|
return nil, nil, err
|
2023-10-25 13:42:00 +02:00
|
|
|
}
|
|
|
|
if len(cmds) > 0 {
|
|
|
|
events, err := c.eventstore.Push(ctx, cmds...)
|
|
|
|
if err != nil {
|
2024-01-17 11:16:48 +01:00
|
|
|
return nil, nil, err
|
2023-10-25 13:42:00 +02:00
|
|
|
}
|
2024-01-17 11:16:48 +01:00
|
|
|
err = AppendAndReduce(bulkWm, events...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
targetDetails := make([]*domain.ObjectDetails, len(bulk))
|
|
|
|
for i, t := range bulk {
|
|
|
|
targetDetails[i] = writeModelToObjectDetails(&bulkWm.writeModels[t.InstanceID].WriteModel)
|
|
|
|
}
|
|
|
|
details := writeModelToObjectDetails(&bulkWm.WriteModel)
|
|
|
|
details.ResourceOwner = ""
|
|
|
|
return details, targetDetails, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Commands) setLimitsCommands(ctx context.Context, wm *limitsWriteModel, setLimits *SetLimits) (cmds []eventstore.Command, err error) {
|
|
|
|
aggregateId := wm.AggregateID
|
|
|
|
if aggregateId == "" {
|
|
|
|
aggregateId, err = c.idGenerator.Next()
|
2023-10-25 13:42:00 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2024-01-17 11:16:48 +01:00
|
|
|
aggregate := limits.NewAggregate(aggregateId, wm.InstanceID)
|
|
|
|
createCmds, err := c.SetLimitsCommand(aggregate, wm, setLimits)()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
cmds, err = createCmds(ctx, nil)
|
|
|
|
return cmds, err
|
2023-10-25 13:42:00 +02:00
|
|
|
}
|
|
|
|
|
2024-01-17 11:16:48 +01:00
|
|
|
func (c *Commands) ResetLimits(ctx context.Context) (*domain.ObjectDetails, error) {
|
2023-10-25 13:42:00 +02:00
|
|
|
instanceId := authz.GetInstance(ctx).InstanceID()
|
2024-01-17 11:16:48 +01:00
|
|
|
wm, err := c.getLimitsWriteModel(ctx, instanceId)
|
2023-10-25 13:42:00 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if wm.AggregateID == "" {
|
2023-12-08 16:30:55 +02:00
|
|
|
return nil, zerrors.ThrowNotFound(nil, "COMMAND-9JToT", "Errors.Limits.NotFound")
|
2023-10-25 13:42:00 +02:00
|
|
|
}
|
2024-01-17 11:16:48 +01:00
|
|
|
aggregate := limits.NewAggregate(wm.AggregateID, instanceId)
|
2023-10-25 13:42:00 +02:00
|
|
|
events := []eventstore.Command{limits.NewResetEvent(ctx, &aggregate.Aggregate)}
|
|
|
|
pushedEvents, err := c.eventstore.Push(ctx, events...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = AppendAndReduce(wm, pushedEvents...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return writeModelToObjectDetails(&wm.WriteModel), nil
|
|
|
|
}
|
|
|
|
|
2024-01-17 11:16:48 +01:00
|
|
|
func (c *Commands) getLimitsWriteModel(ctx context.Context, instanceId string) (*limitsWriteModel, error) {
|
|
|
|
wm := newLimitsWriteModel(instanceId)
|
|
|
|
return wm, c.eventstore.FilterToQueryReducer(ctx, wm)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Commands) getBulkInstanceLimitsWriteModel(ctx context.Context, target []*SetInstanceLimitsBulk) (*limitsBulkWriteModel, error) {
|
|
|
|
wm := newLimitsBulkWriteModel()
|
|
|
|
for _, t := range target {
|
|
|
|
wm.addWriteModel(t.InstanceID)
|
|
|
|
}
|
2023-10-25 13:42:00 +02:00
|
|
|
return wm, c.eventstore.FilterToQueryReducer(ctx, wm)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Commands) SetLimitsCommand(a *limits.Aggregate, wm *limitsWriteModel, setLimits *SetLimits) preparation.Validation {
|
|
|
|
return func() (preparation.CreateCommands, error) {
|
2024-01-17 11:16:48 +01:00
|
|
|
if setLimits == nil || (setLimits.AuditLogRetention == nil && setLimits.Block == nil) {
|
2023-12-08 16:30:55 +02:00
|
|
|
return nil, zerrors.ThrowInvalidArgument(nil, "COMMAND-4M9vs", "Errors.Limits.NoneSpecified")
|
2023-10-25 13:42:00 +02:00
|
|
|
}
|
|
|
|
return func(ctx context.Context, _ preparation.FilterToQueryReducer) ([]eventstore.Command, error) {
|
|
|
|
changes := wm.NewChanges(setLimits)
|
|
|
|
if len(changes) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return []eventstore.Command{limits.NewSetEvent(
|
|
|
|
eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
|
|
|
&a.Aggregate,
|
|
|
|
limits.SetEventType,
|
|
|
|
),
|
|
|
|
changes...,
|
|
|
|
)}, nil
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
}
|