2021-01-06 11:12:56 +01:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-10-26 09:39:56 +01:00
|
|
|
|
|
|
|
"github.com/zitadel/zitadel/internal/command/preparation"
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
2022-10-26 09:39:56 +01:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/v1/models"
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/user"
|
2024-06-19 13:56:33 +03:00
|
|
|
"github.com/zitadel/zitadel/internal/telemetry/tracing"
|
2023-12-08 16:30:55 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2021-01-06 11:12:56 +01:00
|
|
|
)
|
|
|
|
|
2022-12-09 13:04:33 +00:00
|
|
|
type AddMachine struct {
|
|
|
|
Machine *Machine
|
|
|
|
Pat *AddPat
|
|
|
|
MachineKey *AddMachineKey
|
|
|
|
}
|
|
|
|
|
2022-10-26 09:39:56 +01:00
|
|
|
type Machine struct {
|
|
|
|
models.ObjectRoot
|
|
|
|
|
2023-02-08 09:06:34 +01:00
|
|
|
Username string
|
|
|
|
Name string
|
|
|
|
Description string
|
|
|
|
AccessTokenType domain.OIDCTokenType
|
2022-07-28 15:42:35 +02:00
|
|
|
}
|
|
|
|
|
2022-12-09 13:04:33 +00:00
|
|
|
func (m *Machine) IsZero() bool {
|
|
|
|
return m.Username == "" && m.Name == ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func AddMachineCommand(a *user.Aggregate, machine *Machine) preparation.Validation {
|
|
|
|
return func() (_ preparation.CreateCommands, err error) {
|
|
|
|
if a.ResourceOwner == "" {
|
2023-12-08 16:30:55 +02:00
|
|
|
return nil, zerrors.ThrowInvalidArgument(nil, "COMMAND-xiown2", "Errors.ResourceOwnerMissing")
|
2022-12-09 13:04:33 +00:00
|
|
|
}
|
|
|
|
if a.ID == "" {
|
2023-12-08 16:30:55 +02:00
|
|
|
return nil, zerrors.ThrowInvalidArgument(nil, "COMMAND-p0p2mi", "Errors.User.UserIDMissing")
|
2022-12-09 13:04:33 +00:00
|
|
|
}
|
|
|
|
if machine.Name == "" {
|
2023-12-08 16:30:55 +02:00
|
|
|
return nil, zerrors.ThrowInvalidArgument(nil, "COMMAND-bs9Ds", "Errors.User.Invalid")
|
2022-12-09 13:04:33 +00:00
|
|
|
}
|
|
|
|
if machine.Username == "" {
|
2023-12-08 16:30:55 +02:00
|
|
|
return nil, zerrors.ThrowInvalidArgument(nil, "COMMAND-bm9Ds", "Errors.User.Invalid")
|
2022-12-09 13:04:33 +00:00
|
|
|
}
|
|
|
|
return func(ctx context.Context, filter preparation.FilterToQueryReducer) ([]eventstore.Command, error) {
|
2024-06-19 13:56:33 +03:00
|
|
|
ctx, span := tracing.NewSpan(ctx)
|
|
|
|
defer func() { span.EndWithError(err) }()
|
|
|
|
|
2022-12-09 13:04:33 +00:00
|
|
|
writeModel, err := getMachineWriteModel(ctx, a.ID, a.ResourceOwner, filter)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if isUserStateExists(writeModel.UserState) {
|
2023-12-08 16:30:55 +02:00
|
|
|
return nil, zerrors.ThrowPreconditionFailed(nil, "COMMAND-k2una", "Errors.User.AlreadyExisting")
|
2022-12-09 13:04:33 +00:00
|
|
|
}
|
2022-12-13 07:37:50 +01:00
|
|
|
domainPolicy, err := domainPolicyWriteModel(ctx, filter, a.ResourceOwner)
|
2022-12-09 13:04:33 +00:00
|
|
|
if err != nil {
|
2023-12-08 16:30:55 +02:00
|
|
|
return nil, zerrors.ThrowPreconditionFailed(err, "COMMAND-3M9fs", "Errors.Org.DomainPolicy.NotFound")
|
2022-12-09 13:04:33 +00:00
|
|
|
}
|
|
|
|
return []eventstore.Command{
|
2023-02-08 09:06:34 +01:00
|
|
|
user.NewMachineAddedEvent(ctx, &a.Aggregate, machine.Username, machine.Name, machine.Description, domainPolicy.UserLoginMustBeDomain, machine.AccessTokenType),
|
2022-12-09 13:04:33 +00:00
|
|
|
}, nil
|
|
|
|
}, nil
|
2022-07-28 15:42:35 +02:00
|
|
|
}
|
2022-10-26 09:39:56 +01:00
|
|
|
}
|
|
|
|
|
2024-06-19 13:56:33 +03:00
|
|
|
func (c *Commands) AddMachine(ctx context.Context, machine *Machine) (_ *domain.ObjectDetails, err error) {
|
|
|
|
ctx, span := tracing.NewSpan(ctx)
|
|
|
|
defer func() { span.EndWithError(err) }()
|
|
|
|
|
2022-10-26 09:39:56 +01:00
|
|
|
if machine.AggregateID == "" {
|
|
|
|
userID, err := c.idGenerator.Next()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
machine.AggregateID = userID
|
|
|
|
}
|
2022-07-28 15:42:35 +02:00
|
|
|
|
2022-12-09 13:04:33 +00:00
|
|
|
agg := user.NewAggregate(machine.AggregateID, machine.ResourceOwner)
|
|
|
|
cmds, err := preparation.PrepareCommands(ctx, c.eventstore.Filter, AddMachineCommand(agg, machine))
|
2021-02-18 14:48:27 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-12-09 13:04:33 +00:00
|
|
|
|
2022-10-26 09:39:56 +01:00
|
|
|
events, err := c.eventstore.Push(ctx, cmds...)
|
2021-02-18 14:48:27 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-12-09 13:04:33 +00:00
|
|
|
|
2022-10-26 09:39:56 +01:00
|
|
|
return &domain.ObjectDetails{
|
|
|
|
Sequence: events[len(events)-1].Sequence(),
|
2023-10-19 12:19:10 +02:00
|
|
|
EventDate: events[len(events)-1].CreatedAt(),
|
2022-10-26 09:39:56 +01:00
|
|
|
ResourceOwner: events[len(events)-1].Aggregate().ResourceOwner,
|
|
|
|
}, nil
|
2021-01-06 11:12:56 +01:00
|
|
|
}
|
|
|
|
|
2022-10-26 09:39:56 +01:00
|
|
|
func (c *Commands) ChangeMachine(ctx context.Context, machine *Machine) (*domain.ObjectDetails, error) {
|
2022-12-09 13:04:33 +00:00
|
|
|
agg := user.NewAggregate(machine.AggregateID, machine.ResourceOwner)
|
|
|
|
cmds, err := preparation.PrepareCommands(ctx, c.eventstore.Filter, changeMachineCommand(agg, machine))
|
2021-02-18 14:48:27 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-12-09 13:04:33 +00:00
|
|
|
|
2022-10-26 09:39:56 +01:00
|
|
|
events, err := c.eventstore.Push(ctx, cmds...)
|
2021-01-06 11:12:56 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-12-09 13:04:33 +00:00
|
|
|
|
2022-10-26 09:39:56 +01:00
|
|
|
return &domain.ObjectDetails{
|
|
|
|
Sequence: events[len(events)-1].Sequence(),
|
2023-10-19 12:19:10 +02:00
|
|
|
EventDate: events[len(events)-1].CreatedAt(),
|
2022-10-26 09:39:56 +01:00
|
|
|
ResourceOwner: events[len(events)-1].Aggregate().ResourceOwner,
|
|
|
|
}, nil
|
2021-01-06 11:12:56 +01:00
|
|
|
}
|
|
|
|
|
2022-12-09 13:04:33 +00:00
|
|
|
func changeMachineCommand(a *user.Aggregate, machine *Machine) preparation.Validation {
|
2022-10-26 09:39:56 +01:00
|
|
|
return func() (_ preparation.CreateCommands, err error) {
|
2022-12-09 13:04:33 +00:00
|
|
|
if a.ResourceOwner == "" {
|
2023-12-08 16:30:55 +02:00
|
|
|
return nil, zerrors.ThrowInvalidArgument(nil, "COMMAND-xiown3", "Errors.ResourceOwnerMissing")
|
2022-12-09 13:04:33 +00:00
|
|
|
}
|
|
|
|
if a.ID == "" {
|
2023-12-08 16:30:55 +02:00
|
|
|
return nil, zerrors.ThrowInvalidArgument(nil, "COMMAND-p0p3mi", "Errors.User.UserIDMissing")
|
2022-10-26 09:39:56 +01:00
|
|
|
}
|
|
|
|
return func(ctx context.Context, filter preparation.FilterToQueryReducer) ([]eventstore.Command, error) {
|
2022-12-09 13:04:33 +00:00
|
|
|
writeModel, err := getMachineWriteModel(ctx, a.ID, a.ResourceOwner, filter)
|
2022-10-26 09:39:56 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !isUserStateExists(writeModel.UserState) {
|
2023-12-08 16:30:55 +02:00
|
|
|
return nil, zerrors.ThrowNotFound(nil, "COMMAND-5M0od", "Errors.User.NotFound")
|
2022-10-26 09:39:56 +01:00
|
|
|
}
|
2023-02-08 09:06:34 +01:00
|
|
|
changedEvent, hasChanged, err := writeModel.NewChangedEvent(ctx, &a.Aggregate, machine.Name, machine.Description, machine.AccessTokenType)
|
2022-10-26 09:39:56 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !hasChanged {
|
2023-12-08 16:30:55 +02:00
|
|
|
return nil, zerrors.ThrowPreconditionFailed(nil, "COMMAND-2n8vs", "Errors.User.NotChanged")
|
2022-10-26 09:39:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return []eventstore.Command{
|
2022-12-09 13:04:33 +00:00
|
|
|
changedEvent,
|
2022-10-26 09:39:56 +01:00
|
|
|
}, nil
|
|
|
|
}, nil
|
2021-01-06 11:12:56 +01:00
|
|
|
}
|
2022-10-26 09:39:56 +01:00
|
|
|
}
|
2021-01-06 11:12:56 +01:00
|
|
|
|
2024-06-19 13:56:33 +03:00
|
|
|
func getMachineWriteModel(ctx context.Context, userID, resourceOwner string, filter preparation.FilterToQueryReducer) (_ *MachineWriteModel, err error) {
|
|
|
|
ctx, span := tracing.NewSpan(ctx)
|
|
|
|
defer func() { span.EndWithError(err) }()
|
|
|
|
|
2022-10-26 09:39:56 +01:00
|
|
|
writeModel := NewMachineWriteModel(userID, resourceOwner)
|
|
|
|
events, err := filter(ctx, writeModel.Query())
|
2021-01-06 11:12:56 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-12-09 13:04:33 +00:00
|
|
|
if len(events) == 0 {
|
|
|
|
return writeModel, nil
|
|
|
|
}
|
2022-10-26 09:39:56 +01:00
|
|
|
writeModel.AppendEvents(events...)
|
|
|
|
err = writeModel.Reduce()
|
|
|
|
return writeModel, err
|
2021-01-06 11:12:56 +01:00
|
|
|
}
|