2021-01-06 11:12:56 +01:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-02-18 14:48:27 +01:00
|
|
|
|
2021-02-23 15:13:04 +01:00
|
|
|
"github.com/caos/zitadel/internal/domain"
|
2021-01-06 11:12:56 +01:00
|
|
|
caos_errs "github.com/caos/zitadel/internal/errors"
|
2021-02-23 15:13:04 +01:00
|
|
|
"github.com/caos/zitadel/internal/repository/user"
|
2021-01-06 11:12:56 +01:00
|
|
|
"github.com/caos/zitadel/internal/telemetry/tracing"
|
|
|
|
)
|
|
|
|
|
2021-02-24 11:17:39 +01:00
|
|
|
func (c *Commands) AddMachine(ctx context.Context, orgID string, machine *domain.Machine) (*domain.Machine, error) {
|
2021-01-06 11:12:56 +01:00
|
|
|
if !machine.IsValid() {
|
2021-01-15 09:32:59 +01:00
|
|
|
return nil, caos_errs.ThrowInvalidArgument(nil, "COMMAND-bm9Ds", "Errors.User.Invalid")
|
2021-01-06 11:12:56 +01:00
|
|
|
}
|
2021-02-24 11:17:39 +01:00
|
|
|
orgIAMPolicy, err := c.getOrgIAMPolicy(ctx, orgID)
|
2021-01-06 11:12:56 +01:00
|
|
|
if err != nil {
|
2021-03-19 11:12:56 +01:00
|
|
|
return nil, caos_errs.ThrowPreconditionFailed(err, "COMMAND-3M9fs", "Errors.Org.OrgIAMPolicy.NotFound")
|
2021-01-06 11:12:56 +01:00
|
|
|
}
|
|
|
|
if !orgIAMPolicy.UserLoginMustBeDomain {
|
2021-03-19 11:12:56 +01:00
|
|
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "COMMAND-6M0ds", "Errors.User.Invalid")
|
2021-01-06 11:12:56 +01:00
|
|
|
}
|
2021-03-19 11:12:56 +01:00
|
|
|
userID, err := c.idGenerator.Next()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
machine.AggregateID = userID
|
2021-01-12 12:59:51 +01:00
|
|
|
addedMachine := NewMachineWriteModel(machine.AggregateID, orgID)
|
2021-01-06 11:12:56 +01:00
|
|
|
userAgg := UserAggregateFromWriteModel(&addedMachine.WriteModel)
|
2021-02-24 11:17:39 +01:00
|
|
|
events, err := c.eventstore.PushEvents(ctx, user.NewMachineAddedEvent(
|
2021-02-18 14:48:27 +01:00
|
|
|
ctx,
|
|
|
|
userAgg,
|
|
|
|
machine.Username,
|
|
|
|
machine.Name,
|
|
|
|
machine.Description,
|
|
|
|
orgIAMPolicy.UserLoginMustBeDomain,
|
|
|
|
))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = AppendAndReduce(addedMachine, events...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-01-06 11:12:56 +01:00
|
|
|
return writeModelToMachine(addedMachine), nil
|
|
|
|
}
|
|
|
|
|
2021-02-24 11:17:39 +01:00
|
|
|
func (c *Commands) ChangeMachine(ctx context.Context, machine *domain.Machine) (*domain.Machine, error) {
|
|
|
|
existingMachine, err := c.machineWriteModelByID(ctx, machine.AggregateID, machine.ResourceOwner)
|
2021-01-06 11:12:56 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-02-18 14:48:27 +01:00
|
|
|
if !isUserStateExists(existingMachine.UserState) {
|
2021-01-07 16:06:45 +01:00
|
|
|
return nil, caos_errs.ThrowNotFound(nil, "COMMAND-5M0od", "Errors.User.NotFound")
|
2021-01-06 11:12:56 +01:00
|
|
|
}
|
|
|
|
|
2021-02-18 14:48:27 +01:00
|
|
|
userAgg := UserAggregateFromWriteModel(&existingMachine.WriteModel)
|
2021-03-19 11:12:56 +01:00
|
|
|
changedEvent, hasChanged, err := existingMachine.NewChangedEvent(ctx, userAgg, machine.Name, machine.Description)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-01-06 11:12:56 +01:00
|
|
|
if !hasChanged {
|
2021-02-18 14:48:27 +01:00
|
|
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "COMMAND-2n8vs", "Errors.User.NotChanged")
|
2021-01-06 11:12:56 +01:00
|
|
|
}
|
|
|
|
|
2021-02-24 11:17:39 +01:00
|
|
|
events, err := c.eventstore.PushEvents(ctx, changedEvent)
|
2021-02-18 14:48:27 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = AppendAndReduce(existingMachine, events...)
|
2021-01-06 11:12:56 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-02-18 14:48:27 +01:00
|
|
|
return writeModelToMachine(existingMachine), nil
|
2021-01-06 11:12:56 +01:00
|
|
|
}
|
|
|
|
|
2021-02-24 11:17:39 +01:00
|
|
|
func (c *Commands) machineWriteModelByID(ctx context.Context, userID, resourceOwner string) (writeModel *MachineWriteModel, err error) {
|
2021-01-06 11:12:56 +01:00
|
|
|
if userID == "" {
|
2021-01-15 09:32:59 +01:00
|
|
|
return nil, caos_errs.ThrowInvalidArgument(nil, "COMMAND-0Plof", "Errors.User.UserIDMissing")
|
2021-01-06 11:12:56 +01:00
|
|
|
}
|
|
|
|
ctx, span := tracing.NewSpan(ctx)
|
|
|
|
defer func() { span.EndWithError(err) }()
|
|
|
|
|
2021-01-12 12:59:51 +01:00
|
|
|
writeModel = NewMachineWriteModel(userID, resourceOwner)
|
2021-02-24 11:17:39 +01:00
|
|
|
err = c.eventstore.FilterToQueryReducer(ctx, writeModel)
|
2021-01-06 11:12:56 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return writeModel, nil
|
|
|
|
}
|