2021-01-06 10:12:56 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-10-26 08:39:56 +00:00
|
|
|
|
|
|
|
"github.com/zitadel/zitadel/internal/command/preparation"
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
caos_errs "github.com/zitadel/zitadel/internal/errors"
|
2022-10-26 08:39:56 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/v1/models"
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/user"
|
2021-01-06 10:12:56 +00:00
|
|
|
)
|
|
|
|
|
2022-12-09 13:04:33 +00:00
|
|
|
type AddMachine struct {
|
|
|
|
Machine *Machine
|
|
|
|
Pat *AddPat
|
|
|
|
MachineKey *AddMachineKey
|
|
|
|
}
|
|
|
|
|
2022-10-26 08:39:56 +00:00
|
|
|
type Machine struct {
|
|
|
|
models.ObjectRoot
|
|
|
|
|
2023-02-08 08:06:34 +00:00
|
|
|
Username string
|
|
|
|
Name string
|
|
|
|
Description string
|
|
|
|
AccessTokenType domain.OIDCTokenType
|
2022-07-28 13:42:35 +00: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 == "" {
|
|
|
|
return nil, caos_errs.ThrowInvalidArgument(nil, "COMMAND-xiown2", "Errors.ResourceOwnerMissing")
|
|
|
|
}
|
|
|
|
if a.ID == "" {
|
|
|
|
return nil, caos_errs.ThrowInvalidArgument(nil, "COMMAND-p0p2mi", "Errors.User.UserIDMissing")
|
|
|
|
}
|
|
|
|
if machine.Name == "" {
|
|
|
|
return nil, caos_errs.ThrowInvalidArgument(nil, "COMMAND-bs9Ds", "Errors.User.Invalid")
|
|
|
|
}
|
|
|
|
if machine.Username == "" {
|
|
|
|
return nil, caos_errs.ThrowInvalidArgument(nil, "COMMAND-bm9Ds", "Errors.User.Invalid")
|
|
|
|
}
|
|
|
|
return func(ctx context.Context, filter preparation.FilterToQueryReducer) ([]eventstore.Command, error) {
|
|
|
|
writeModel, err := getMachineWriteModel(ctx, a.ID, a.ResourceOwner, filter)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if isUserStateExists(writeModel.UserState) {
|
|
|
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "COMMAND-k2una", "Errors.User.AlreadyExisting")
|
|
|
|
}
|
2022-12-13 06:37:50 +00:00
|
|
|
domainPolicy, err := domainPolicyWriteModel(ctx, filter, a.ResourceOwner)
|
2022-12-09 13:04:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, caos_errs.ThrowPreconditionFailed(err, "COMMAND-3M9fs", "Errors.Org.DomainPolicy.NotFound")
|
|
|
|
}
|
|
|
|
return []eventstore.Command{
|
2023-02-08 08:06:34 +00: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 13:42:35 +00:00
|
|
|
}
|
2022-10-26 08:39:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Commands) AddMachine(ctx context.Context, machine *Machine) (*domain.ObjectDetails, error) {
|
|
|
|
if machine.AggregateID == "" {
|
|
|
|
userID, err := c.idGenerator.Next()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
machine.AggregateID = userID
|
|
|
|
}
|
2022-07-28 13:42:35 +00: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 13:48:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-12-09 13:04:33 +00:00
|
|
|
|
2022-10-26 08:39:56 +00:00
|
|
|
events, err := c.eventstore.Push(ctx, cmds...)
|
2021-02-18 13:48:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-12-09 13:04:33 +00:00
|
|
|
|
2022-10-26 08:39:56 +00:00
|
|
|
return &domain.ObjectDetails{
|
|
|
|
Sequence: events[len(events)-1].Sequence(),
|
|
|
|
EventDate: events[len(events)-1].CreationDate(),
|
|
|
|
ResourceOwner: events[len(events)-1].Aggregate().ResourceOwner,
|
|
|
|
}, nil
|
2021-01-06 10:12:56 +00:00
|
|
|
}
|
|
|
|
|
2022-10-26 08:39:56 +00: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 13:48:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-12-09 13:04:33 +00:00
|
|
|
|
2022-10-26 08:39:56 +00:00
|
|
|
events, err := c.eventstore.Push(ctx, cmds...)
|
2021-01-06 10:12:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-12-09 13:04:33 +00:00
|
|
|
|
2022-10-26 08:39:56 +00:00
|
|
|
return &domain.ObjectDetails{
|
|
|
|
Sequence: events[len(events)-1].Sequence(),
|
|
|
|
EventDate: events[len(events)-1].CreationDate(),
|
|
|
|
ResourceOwner: events[len(events)-1].Aggregate().ResourceOwner,
|
|
|
|
}, nil
|
2021-01-06 10:12:56 +00:00
|
|
|
}
|
|
|
|
|
2022-12-09 13:04:33 +00:00
|
|
|
func changeMachineCommand(a *user.Aggregate, machine *Machine) preparation.Validation {
|
2022-10-26 08:39:56 +00:00
|
|
|
return func() (_ preparation.CreateCommands, err error) {
|
2022-12-09 13:04:33 +00:00
|
|
|
if a.ResourceOwner == "" {
|
|
|
|
return nil, caos_errs.ThrowInvalidArgument(nil, "COMMAND-xiown3", "Errors.ResourceOwnerMissing")
|
|
|
|
}
|
|
|
|
if a.ID == "" {
|
|
|
|
return nil, caos_errs.ThrowInvalidArgument(nil, "COMMAND-p0p3mi", "Errors.User.UserIDMissing")
|
2022-10-26 08:39:56 +00: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 08:39:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !isUserStateExists(writeModel.UserState) {
|
|
|
|
return nil, caos_errs.ThrowNotFound(nil, "COMMAND-5M0od", "Errors.User.NotFound")
|
|
|
|
}
|
2023-02-08 08:06:34 +00:00
|
|
|
changedEvent, hasChanged, err := writeModel.NewChangedEvent(ctx, &a.Aggregate, machine.Name, machine.Description, machine.AccessTokenType)
|
2022-10-26 08:39:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !hasChanged {
|
|
|
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "COMMAND-2n8vs", "Errors.User.NotChanged")
|
|
|
|
}
|
|
|
|
|
|
|
|
return []eventstore.Command{
|
2022-12-09 13:04:33 +00:00
|
|
|
changedEvent,
|
2022-10-26 08:39:56 +00:00
|
|
|
}, nil
|
|
|
|
}, nil
|
2021-01-06 10:12:56 +00:00
|
|
|
}
|
2022-10-26 08:39:56 +00:00
|
|
|
}
|
2021-01-06 10:12:56 +00:00
|
|
|
|
2022-12-09 13:04:33 +00:00
|
|
|
func getMachineWriteModel(ctx context.Context, userID, resourceOwner string, filter preparation.FilterToQueryReducer) (*MachineWriteModel, error) {
|
2022-10-26 08:39:56 +00:00
|
|
|
writeModel := NewMachineWriteModel(userID, resourceOwner)
|
|
|
|
events, err := filter(ctx, writeModel.Query())
|
2021-01-06 10:12:56 +00: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 08:39:56 +00:00
|
|
|
writeModel.AppendEvents(events...)
|
|
|
|
err = writeModel.Reduce()
|
|
|
|
return writeModel, err
|
2021-01-06 10:12:56 +00:00
|
|
|
}
|