2021-01-06 10:12:56 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-02-18 13:48:27 +00:00
|
|
|
|
2021-02-23 14:13:04 +00:00
|
|
|
"github.com/caos/zitadel/internal/domain"
|
2021-01-06 10:12:56 +00:00
|
|
|
caos_errs "github.com/caos/zitadel/internal/errors"
|
2021-02-23 14:13:04 +00:00
|
|
|
"github.com/caos/zitadel/internal/repository/user"
|
2021-01-06 10:12:56 +00:00
|
|
|
"github.com/caos/zitadel/internal/telemetry/tracing"
|
|
|
|
)
|
|
|
|
|
2021-01-15 08:32:59 +00:00
|
|
|
func (r *CommandSide) AddMachine(ctx context.Context, orgID string, machine *domain.Machine) (*domain.Machine, error) {
|
2021-01-06 10:12:56 +00:00
|
|
|
if !machine.IsValid() {
|
2021-01-15 08:32:59 +00:00
|
|
|
return nil, caos_errs.ThrowInvalidArgument(nil, "COMMAND-bm9Ds", "Errors.User.Invalid")
|
2021-01-06 10:12:56 +00:00
|
|
|
}
|
2021-02-18 13:48:27 +00:00
|
|
|
|
2021-01-06 10:12:56 +00:00
|
|
|
userID, err := r.idGenerator.Next()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
machine.AggregateID = userID
|
2021-02-18 13:48:27 +00:00
|
|
|
|
2021-01-08 10:33:45 +00:00
|
|
|
orgIAMPolicy, err := r.getOrgIAMPolicy(ctx, orgID)
|
2021-01-06 10:12:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-02-18 13:48:27 +00:00
|
|
|
//TODO: adlerhurst are no machines allowed in global org? or what if I create an org which allowes all suffixes?
|
2021-01-06 10:12:56 +00:00
|
|
|
if !orgIAMPolicy.UserLoginMustBeDomain {
|
|
|
|
return nil, caos_errs.ThrowInvalidArgument(nil, "COMMAND-6M0ds", "Errors.User.Invalid")
|
|
|
|
}
|
|
|
|
|
2021-01-12 11:59:51 +00:00
|
|
|
addedMachine := NewMachineWriteModel(machine.AggregateID, orgID)
|
2021-01-06 10:12:56 +00:00
|
|
|
userAgg := UserAggregateFromWriteModel(&addedMachine.WriteModel)
|
2021-02-18 13:48:27 +00:00
|
|
|
events, err := r.eventstore.PushEvents(ctx, user.NewMachineAddedEvent(
|
|
|
|
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 10:12:56 +00:00
|
|
|
return writeModelToMachine(addedMachine), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *CommandSide) ChangeMachine(ctx context.Context, machine *domain.Machine) (*domain.Machine, error) {
|
2021-02-18 13:48:27 +00:00
|
|
|
existingMachine, err := r.machineWriteModelByID(ctx, machine.AggregateID, machine.ResourceOwner)
|
2021-01-06 10:12:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-02-18 13:48:27 +00:00
|
|
|
if !isUserStateExists(existingMachine.UserState) {
|
2021-01-07 15:06:45 +00:00
|
|
|
return nil, caos_errs.ThrowNotFound(nil, "COMMAND-5M0od", "Errors.User.NotFound")
|
2021-01-06 10:12:56 +00:00
|
|
|
}
|
|
|
|
|
2021-02-18 13:48:27 +00:00
|
|
|
userAgg := UserAggregateFromWriteModel(&existingMachine.WriteModel)
|
|
|
|
changedEvent, hasChanged := existingMachine.NewChangedEvent(ctx, userAgg, machine.Name, machine.Description)
|
2021-01-06 10:12:56 +00:00
|
|
|
if !hasChanged {
|
2021-02-18 13:48:27 +00:00
|
|
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "COMMAND-2n8vs", "Errors.User.NotChanged")
|
2021-01-06 10:12:56 +00:00
|
|
|
}
|
|
|
|
|
2021-02-18 13:48:27 +00:00
|
|
|
events, err := r.eventstore.PushEvents(ctx, changedEvent)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = AppendAndReduce(existingMachine, events...)
|
2021-01-06 10:12:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-02-18 13:48:27 +00:00
|
|
|
return writeModelToMachine(existingMachine), nil
|
2021-01-06 10:12:56 +00:00
|
|
|
}
|
|
|
|
|
2021-02-18 13:48:27 +00:00
|
|
|
//TODO: adlerhurst we should check userID on the same level, in user.go userID is checked in public funcs
|
2021-01-12 11:59:51 +00:00
|
|
|
func (r *CommandSide) machineWriteModelByID(ctx context.Context, userID, resourceOwner string) (writeModel *MachineWriteModel, err error) {
|
2021-01-06 10:12:56 +00:00
|
|
|
if userID == "" {
|
2021-01-15 08:32:59 +00:00
|
|
|
return nil, caos_errs.ThrowInvalidArgument(nil, "COMMAND-0Plof", "Errors.User.UserIDMissing")
|
2021-01-06 10:12:56 +00:00
|
|
|
}
|
|
|
|
ctx, span := tracing.NewSpan(ctx)
|
|
|
|
defer func() { span.EndWithError(err) }()
|
|
|
|
|
2021-01-12 11:59:51 +00:00
|
|
|
writeModel = NewMachineWriteModel(userID, resourceOwner)
|
2021-01-06 10:12:56 +00:00
|
|
|
err = r.eventstore.FilterToQueryReducer(ctx, writeModel)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return writeModel, nil
|
|
|
|
}
|