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"
|
|
|
|
caos_errs "github.com/zitadel/zitadel/internal/errors"
|
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"
|
2021-01-06 11:12:56 +01:00
|
|
|
)
|
|
|
|
|
2022-10-26 09:39:56 +01:00
|
|
|
type Machine struct {
|
|
|
|
models.ObjectRoot
|
|
|
|
|
|
|
|
Username string
|
|
|
|
Name string
|
|
|
|
Description string
|
2022-07-28 15:42:35 +02:00
|
|
|
}
|
|
|
|
|
2022-10-26 09:39:56 +01:00
|
|
|
func (m *Machine) content() error {
|
|
|
|
if m.ResourceOwner == "" {
|
|
|
|
return caos_errs.ThrowInvalidArgument(nil, "COMMAND-xiown2", "Errors.ResourceOwnerMissing")
|
2022-07-28 15:42:35 +02:00
|
|
|
}
|
2022-10-26 09:39:56 +01:00
|
|
|
if m.AggregateID == "" {
|
|
|
|
return caos_errs.ThrowInvalidArgument(nil, "COMMAND-p0p2mi", "Errors.User.UserIDMissing")
|
2022-07-28 15:42:35 +02:00
|
|
|
}
|
2022-10-26 09:39:56 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
domainPolicy, err := c.getOrgDomainPolicy(ctx, machine.ResourceOwner)
|
2022-07-28 15:42:35 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, caos_errs.ThrowPreconditionFailed(err, "COMMAND-3M9fs", "Errors.Org.DomainPolicy.NotFound")
|
|
|
|
}
|
|
|
|
|
2022-10-26 09:39:56 +01:00
|
|
|
validation := prepareAddUserMachine(machine, domainPolicy)
|
|
|
|
cmds, err := preparation.PrepareCommands(ctx, c.eventstore.Filter, validation)
|
2021-02-18 14:48:27 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
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-10-26 09:39:56 +01: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 11:12:56 +01:00
|
|
|
}
|
|
|
|
|
2022-10-26 09:39:56 +01:00
|
|
|
func prepareAddUserMachine(machine *Machine, domainPolicy *domain.DomainPolicy) preparation.Validation {
|
|
|
|
return func() (_ preparation.CreateCommands, err error) {
|
|
|
|
if err := machine.content(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
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 := getMachineWriteModelByID(ctx, filter, machine.AggregateID, machine.ResourceOwner)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if isUserStateExists(writeModel.UserState) {
|
|
|
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "COMMAND-k2una", "Errors.User.AlreadyExisting")
|
|
|
|
}
|
|
|
|
return []eventstore.Command{
|
|
|
|
user.NewMachineAddedEvent(
|
|
|
|
ctx,
|
|
|
|
UserAggregateFromWriteModel(&writeModel.WriteModel),
|
|
|
|
machine.Username,
|
|
|
|
machine.Name,
|
|
|
|
machine.Description,
|
|
|
|
domainPolicy.UserLoginMustBeDomain,
|
|
|
|
),
|
|
|
|
}, 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
|
|
|
|
2022-10-26 09:39:56 +01:00
|
|
|
func (c *Commands) ChangeMachine(ctx context.Context, machine *Machine) (*domain.ObjectDetails, error) {
|
|
|
|
validation := prepareChangeUserMachine(machine)
|
|
|
|
cmds, err := preparation.PrepareCommands(ctx, c.eventstore.Filter, validation)
|
2021-02-18 14:48:27 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
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-10-26 09:39:56 +01: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 11:12:56 +01:00
|
|
|
}
|
|
|
|
|
2022-10-26 09:39:56 +01:00
|
|
|
func prepareChangeUserMachine(machine *Machine) preparation.Validation {
|
|
|
|
return func() (_ preparation.CreateCommands, err error) {
|
|
|
|
if err := machine.content(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return func(ctx context.Context, filter preparation.FilterToQueryReducer) ([]eventstore.Command, error) {
|
|
|
|
writeModel, err := getMachineWriteModelByID(ctx, filter, machine.AggregateID, machine.ResourceOwner)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !isUserStateExists(writeModel.UserState) {
|
|
|
|
return nil, caos_errs.ThrowNotFound(nil, "COMMAND-5M0od", "Errors.User.NotFound")
|
|
|
|
}
|
|
|
|
|
|
|
|
event, hasChanged, err := writeModel.NewChangedEvent(ctx, UserAggregateFromWriteModel(&writeModel.WriteModel), machine.Name, machine.Description)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !hasChanged {
|
|
|
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "COMMAND-2n8vs", "Errors.User.NotChanged")
|
|
|
|
}
|
|
|
|
|
|
|
|
return []eventstore.Command{
|
|
|
|
event,
|
|
|
|
}, 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
|
|
|
|
2022-10-26 09:39:56 +01:00
|
|
|
func getMachineWriteModelByID(ctx context.Context, filter preparation.FilterToQueryReducer, userID, resourceOwner string) (_ *MachineWriteModel, err error) {
|
|
|
|
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-10-26 09:39:56 +01:00
|
|
|
writeModel.AppendEvents(events...)
|
|
|
|
err = writeModel.Reduce()
|
|
|
|
return writeModel, err
|
2021-01-06 11:12:56 +01:00
|
|
|
}
|