mirror of
https://github.com/andwati/zitadel.git
synced 2026-10-18 21:52:54 +00:00
# Which Problems Are Solved The user service v2 didn't allow the specify or change the access token type for users of type machine and referred to using the management API, which in return was already deprecated and linked to the user service. # How the Problems Are Solved Added a possibility to specify the access token type in the creation and update request. # Additional Changes None # Additional Context - closes #10850 - requires backport to v4.x --------- Co-authored-by: Copilot <[email protected]>
102 lines
2.9 KiB
Go
102 lines
2.9 KiB
Go
package command
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
"github.com/zitadel/zitadel/internal/repository/user"
|
|
"github.com/zitadel/zitadel/internal/telemetry/tracing"
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
|
)
|
|
|
|
type ChangeMachine struct {
|
|
ID string
|
|
ResourceOwner string
|
|
Username *string
|
|
Name *string
|
|
Description *string
|
|
AccessTokenType *domain.OIDCTokenType
|
|
|
|
// Details are set after a successful execution of the command
|
|
Details *domain.ObjectDetails
|
|
}
|
|
|
|
func (h *ChangeMachine) Changed() bool {
|
|
if h.Username != nil {
|
|
return true
|
|
}
|
|
if h.Name != nil {
|
|
return true
|
|
}
|
|
if h.Description != nil {
|
|
return true
|
|
}
|
|
if h.AccessTokenType != nil {
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
func (c *Commands) ChangeUserMachine(ctx context.Context, machine *ChangeMachine) (err error) {
|
|
existingMachine, err := c.UserMachineWriteModel(
|
|
ctx,
|
|
machine.ID,
|
|
machine.ResourceOwner,
|
|
false,
|
|
)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if machine.Changed() {
|
|
if err := c.checkPermissionUpdateUser(ctx, existingMachine.ResourceOwner, existingMachine.AggregateID, true); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
cmds := make([]eventstore.Command, 0)
|
|
if machine.Username != nil {
|
|
cmds, err = c.changeUsername(ctx, cmds, existingMachine, *machine.Username)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
var machineChanges []user.MachineChanges
|
|
if machine.Name != nil && *machine.Name != existingMachine.Name {
|
|
machineChanges = append(machineChanges, user.ChangeName(*machine.Name))
|
|
}
|
|
if machine.Description != nil && *machine.Description != existingMachine.Description {
|
|
machineChanges = append(machineChanges, user.ChangeDescription(*machine.Description))
|
|
}
|
|
if machine.AccessTokenType != nil && *machine.AccessTokenType != existingMachine.AccessTokenType {
|
|
machineChanges = append(machineChanges, user.ChangeAccessTokenType(*machine.AccessTokenType))
|
|
}
|
|
if len(machineChanges) > 0 {
|
|
cmds = append(cmds, user.NewMachineChangedEvent(ctx, &existingMachine.Aggregate().Aggregate, machineChanges))
|
|
}
|
|
if len(cmds) == 0 {
|
|
machine.Details = writeModelToObjectDetails(&existingMachine.WriteModel)
|
|
return nil
|
|
}
|
|
err = c.pushAppendAndReduce(ctx, existingMachine, cmds...)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
machine.Details = writeModelToObjectDetails(&existingMachine.WriteModel)
|
|
return nil
|
|
}
|
|
|
|
func (c *Commands) UserMachineWriteModel(ctx context.Context, userID, resourceOwner string, metadataWM bool) (writeModel *UserV2WriteModel, err error) {
|
|
ctx, span := tracing.NewSpan(ctx)
|
|
defer func() { span.EndWithError(err) }()
|
|
writeModel = NewUserMachineWriteModel(userID, resourceOwner, metadataWM)
|
|
err = c.eventstore.FilterToQueryReducer(ctx, writeModel)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if !isUserStateExists(writeModel.UserState) {
|
|
return nil, zerrors.ThrowNotFound(nil, "COMMAND-ugjs0upun6", "Errors.User.NotFound")
|
|
}
|
|
return writeModel, nil
|
|
}
|