mirror of
https://github.com/zitadel/zitadel.git
synced 2025-07-15 19:28:35 +00:00

# Which Problems Are Solved This pull request addresses a significant gap in the user service v2 API, which currently lacks methods for managing machine users. # How the Problems Are Solved This PR adds new API endpoints to the user service v2 to manage machine users including their secret, keys and personal access tokens. Additionally, there's now a CreateUser and UpdateUser endpoints which allow to create either a human or machine user and update them. The existing `CreateHumanUser` endpoint has been deprecated along the corresponding management service endpoints. For details check the additional context section. # Additional Context - Closes https://github.com/zitadel/zitadel/issues/9349 ## More details - API changes: https://github.com/zitadel/zitadel/pull/9680 - Implementation: https://github.com/zitadel/zitadel/pull/9763 - Tests: https://github.com/zitadel/zitadel/pull/9771 ## Follow-ups - Metadata: support managing user metadata using resource API https://github.com/zitadel/zitadel/pull/10005 - Machine token type: support managing the machine token type (migrate to new enum with zero value unspecified?) --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Livio Spring <livio.a@gmail.com>
136 lines
3.5 KiB
Go
136 lines
3.5 KiB
Go
package user
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
|
)
|
|
|
|
const (
|
|
machineEventPrefix = userEventTypePrefix + "machine."
|
|
MachineAddedEventType = machineEventPrefix + "added"
|
|
MachineChangedEventType = machineEventPrefix + "changed"
|
|
)
|
|
|
|
type MachineAddedEvent struct {
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
UserName string `json:"userName"`
|
|
userLoginMustBeDomain bool
|
|
|
|
Name string `json:"name,omitempty"`
|
|
Description string `json:"description,omitempty"`
|
|
AccessTokenType domain.OIDCTokenType `json:"accessTokenType,omitempty"`
|
|
}
|
|
|
|
func (e *MachineAddedEvent) Payload() interface{} {
|
|
return e
|
|
}
|
|
|
|
func (e *MachineAddedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
|
return []*eventstore.UniqueConstraint{NewAddUsernameUniqueConstraint(e.UserName, e.Aggregate().ResourceOwner, e.userLoginMustBeDomain)}
|
|
}
|
|
|
|
func NewMachineAddedEvent(
|
|
ctx context.Context,
|
|
aggregate *eventstore.Aggregate,
|
|
userName,
|
|
name,
|
|
description string,
|
|
userLoginMustBeDomain bool,
|
|
accessTokenType domain.OIDCTokenType,
|
|
) *MachineAddedEvent {
|
|
return &MachineAddedEvent{
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
ctx,
|
|
aggregate,
|
|
MachineAddedEventType,
|
|
),
|
|
UserName: userName,
|
|
Name: name,
|
|
Description: description,
|
|
userLoginMustBeDomain: userLoginMustBeDomain,
|
|
AccessTokenType: accessTokenType,
|
|
}
|
|
}
|
|
|
|
func MachineAddedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
|
machineAdded := &MachineAddedEvent{
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
}
|
|
err := event.Unmarshal(machineAdded)
|
|
if err != nil {
|
|
return nil, zerrors.ThrowInternal(err, "USER-tMv9s", "unable to unmarshal machine added")
|
|
}
|
|
|
|
return machineAdded, nil
|
|
}
|
|
|
|
type MachineChangedEvent struct {
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
Name *string `json:"name,omitempty"`
|
|
Description *string `json:"description,omitempty"`
|
|
AccessTokenType *domain.OIDCTokenType `json:"accessTokenType,omitempty"`
|
|
}
|
|
|
|
func (e *MachineChangedEvent) Payload() interface{} {
|
|
return e
|
|
}
|
|
|
|
func (e *MachineChangedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
|
return nil
|
|
}
|
|
|
|
func NewMachineChangedEvent(
|
|
ctx context.Context,
|
|
aggregate *eventstore.Aggregate,
|
|
changes []MachineChanges,
|
|
) *MachineChangedEvent {
|
|
changeEvent := &MachineChangedEvent{
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
ctx,
|
|
aggregate,
|
|
MachineChangedEventType,
|
|
),
|
|
}
|
|
for _, change := range changes {
|
|
change(changeEvent)
|
|
}
|
|
return changeEvent
|
|
}
|
|
|
|
type MachineChanges func(event *MachineChangedEvent)
|
|
|
|
func ChangeName(name string) func(event *MachineChangedEvent) {
|
|
return func(e *MachineChangedEvent) {
|
|
e.Name = &name
|
|
}
|
|
}
|
|
|
|
func ChangeDescription(description string) func(event *MachineChangedEvent) {
|
|
return func(e *MachineChangedEvent) {
|
|
e.Description = &description
|
|
}
|
|
}
|
|
|
|
func ChangeAccessTokenType(accessTokenType domain.OIDCTokenType) func(event *MachineChangedEvent) {
|
|
return func(e *MachineChangedEvent) {
|
|
e.AccessTokenType = &accessTokenType
|
|
}
|
|
}
|
|
|
|
func MachineChangedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
|
machineChanged := &MachineChangedEvent{
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
}
|
|
err := event.Unmarshal(machineChanged)
|
|
if err != nil {
|
|
return nil, zerrors.ThrowInternal(err, "USER-4M9ds", "unable to unmarshal machine changed")
|
|
}
|
|
|
|
return machineChanged, nil
|
|
}
|