2021-01-06 10:12:56 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
caos_errs "github.com/zitadel/zitadel/internal/errors"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/user"
|
|
|
|
"github.com/zitadel/zitadel/internal/telemetry/tracing"
|
2021-01-06 10:12:56 +00:00
|
|
|
)
|
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
func (c *Commands) 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
|
|
|
}
|
2022-03-28 08:05:09 +00:00
|
|
|
domainPolicy, err := c.getOrgDomainPolicy(ctx, orgID)
|
2021-01-06 10:12:56 +00:00
|
|
|
if err != nil {
|
2022-03-24 16:21:34 +00:00
|
|
|
return nil, caos_errs.ThrowPreconditionFailed(err, "COMMAND-3M9fs", "Errors.Org.DomainPolicy.NotFound")
|
2021-01-06 10:12:56 +00:00
|
|
|
}
|
2021-03-19 10:12:56 +00:00
|
|
|
userID, err := c.idGenerator.Next()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-07-28 13:42:35 +00:00
|
|
|
return c.addMachineWithID(ctx, orgID, userID, machine, domainPolicy)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Commands) AddMachineWithID(ctx context.Context, orgID string, userID string, machine *domain.Machine) (*domain.Machine, error) {
|
|
|
|
existingMachine, err := c.machineWriteModelByID(ctx, userID, orgID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if isUserStateExists(existingMachine.UserState) {
|
|
|
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "COMMAND-k2una", "Errors.User.AlreadyExisting")
|
|
|
|
}
|
|
|
|
domainPolicy, err := c.getOrgDomainPolicy(ctx, orgID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, caos_errs.ThrowPreconditionFailed(err, "COMMAND-3M9fs", "Errors.Org.DomainPolicy.NotFound")
|
|
|
|
}
|
|
|
|
if !domainPolicy.UserLoginMustBeDomain {
|
|
|
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "COMMAND-6M0dd", "Errors.User.Invalid")
|
|
|
|
}
|
|
|
|
return c.addMachineWithID(ctx, orgID, userID, machine, domainPolicy)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Commands) addMachineWithID(ctx context.Context, orgID string, userID string, machine *domain.Machine, domainPolicy *domain.DomainPolicy) (*domain.Machine, error) {
|
|
|
|
|
2021-03-19 10:12:56 +00:00
|
|
|
machine.AggregateID = userID
|
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)
|
2022-01-03 08:19:07 +00:00
|
|
|
events, err := c.eventstore.Push(ctx, user.NewMachineAddedEvent(
|
2021-02-18 13:48:27 +00:00
|
|
|
ctx,
|
|
|
|
userAgg,
|
|
|
|
machine.Username,
|
|
|
|
machine.Name,
|
|
|
|
machine.Description,
|
2022-03-28 08:05:09 +00:00
|
|
|
domainPolicy.UserLoginMustBeDomain,
|
2021-02-18 13:48:27 +00:00
|
|
|
))
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-02-24 10:17:39 +00: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 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)
|
2021-03-19 10:12:56 +00:00
|
|
|
changedEvent, hasChanged, err := existingMachine.NewChangedEvent(ctx, userAgg, machine.Name, machine.Description)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2022-01-03 08:19:07 +00:00
|
|
|
events, err := c.eventstore.Push(ctx, changedEvent)
|
2021-02-18 13:48:27 +00:00
|
|
|
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-24 10:17:39 +00:00
|
|
|
func (c *Commands) 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-02-24 10:17:39 +00:00
|
|
|
err = c.eventstore.FilterToQueryReducer(ctx, writeModel)
|
2021-01-06 10:12:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return writeModel, nil
|
|
|
|
}
|