2021-01-04 14:52:13 +01:00
|
|
|
package user
|
2020-12-10 16:18:52 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-01-03 09:19:07 +01:00
|
|
|
|
2023-02-08 09:06:34 +01:00
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
2023-10-19 12:19:10 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
2023-12-08 16:30:55 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2020-12-10 16:18:52 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-01-04 14:52:13 +01:00
|
|
|
machineEventPrefix = userEventTypePrefix + "machine."
|
2020-12-10 16:18:52 +01:00
|
|
|
MachineAddedEventType = machineEventPrefix + "added"
|
|
|
|
MachineChangedEventType = machineEventPrefix + "changed"
|
|
|
|
)
|
|
|
|
|
2021-01-04 14:52:13 +01:00
|
|
|
type MachineAddedEvent struct {
|
2020-12-10 16:18:52 +01:00
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
2021-01-21 10:49:38 +01:00
|
|
|
UserName string `json:"userName"`
|
2023-02-08 09:06:34 +01:00
|
|
|
userLoginMustBeDomain bool
|
2020-12-10 16:18:52 +01:00
|
|
|
|
2023-02-08 09:06:34 +01:00
|
|
|
Name string `json:"name,omitempty"`
|
|
|
|
Description string `json:"description,omitempty"`
|
|
|
|
AccessTokenType domain.OIDCTokenType `json:"accessTokenType,omitempty"`
|
2020-12-10 16:18:52 +01:00
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (e *MachineAddedEvent) Payload() interface{} {
|
2020-12-10 16:18:52 +01:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (e *MachineAddedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
|
|
|
return []*eventstore.UniqueConstraint{NewAddUsernameUniqueConstraint(e.UserName, e.Aggregate().ResourceOwner, e.userLoginMustBeDomain)}
|
2021-01-21 10:49:38 +01:00
|
|
|
}
|
|
|
|
|
2021-01-04 14:52:13 +01:00
|
|
|
func NewMachineAddedEvent(
|
2020-12-10 16:18:52 +01:00
|
|
|
ctx context.Context,
|
2021-02-18 14:48:27 +01:00
|
|
|
aggregate *eventstore.Aggregate,
|
2020-12-10 16:18:52 +01:00
|
|
|
userName,
|
|
|
|
name,
|
|
|
|
description string,
|
2021-01-21 10:49:38 +01:00
|
|
|
userLoginMustBeDomain bool,
|
2023-02-08 09:06:34 +01:00
|
|
|
accessTokenType domain.OIDCTokenType,
|
2021-01-04 14:52:13 +01:00
|
|
|
) *MachineAddedEvent {
|
|
|
|
return &MachineAddedEvent{
|
2020-12-10 16:18:52 +01:00
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
2021-02-18 14:48:27 +01:00
|
|
|
aggregate,
|
2020-12-10 16:18:52 +01:00
|
|
|
MachineAddedEventType,
|
|
|
|
),
|
2021-01-21 10:49:38 +01:00
|
|
|
UserName: userName,
|
|
|
|
Name: name,
|
|
|
|
Description: description,
|
2021-03-19 11:12:56 +01:00
|
|
|
userLoginMustBeDomain: userLoginMustBeDomain,
|
2023-02-08 09:06:34 +01:00
|
|
|
AccessTokenType: accessTokenType,
|
2020-12-10 16:18:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func MachineAddedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2021-01-04 14:52:13 +01:00
|
|
|
machineAdded := &MachineAddedEvent{
|
2020-12-10 16:18:52 +01:00
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
2023-10-19 12:19:10 +02:00
|
|
|
err := event.Unmarshal(machineAdded)
|
2020-12-10 16:18:52 +01:00
|
|
|
if err != nil {
|
2023-12-08 16:30:55 +02:00
|
|
|
return nil, zerrors.ThrowInternal(err, "USER-tMv9s", "unable to unmarshal machine added")
|
2020-12-10 16:18:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return machineAdded, nil
|
|
|
|
}
|
|
|
|
|
2021-01-04 14:52:13 +01:00
|
|
|
type MachineChangedEvent struct {
|
2020-12-10 16:18:52 +01:00
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
2023-02-08 09:06:34 +01:00
|
|
|
Name *string `json:"name,omitempty"`
|
|
|
|
Description *string `json:"description,omitempty"`
|
|
|
|
AccessTokenType *domain.OIDCTokenType `json:"accessTokenType,omitempty"`
|
2020-12-10 16:18:52 +01:00
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (e *MachineChangedEvent) Payload() interface{} {
|
2020-12-10 16:18:52 +01:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (e *MachineChangedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
2021-01-21 10:49:38 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-04 14:52:13 +01:00
|
|
|
func NewMachineChangedEvent(
|
2020-12-10 16:18:52 +01:00
|
|
|
ctx context.Context,
|
2021-02-18 14:48:27 +01:00
|
|
|
aggregate *eventstore.Aggregate,
|
2021-03-19 11:12:56 +01:00
|
|
|
changes []MachineChanges,
|
|
|
|
) (*MachineChangedEvent, error) {
|
|
|
|
if len(changes) == 0 {
|
2023-12-08 16:30:55 +02:00
|
|
|
return nil, zerrors.ThrowPreconditionFailed(nil, "USER-3M9fs", "Errors.NoChangesFound")
|
2021-03-19 11:12:56 +01:00
|
|
|
}
|
|
|
|
changeEvent := &MachineChangedEvent{
|
2020-12-10 16:18:52 +01:00
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
2021-02-18 14:48:27 +01:00
|
|
|
aggregate,
|
2020-12-10 16:18:52 +01:00
|
|
|
MachineChangedEventType,
|
|
|
|
),
|
|
|
|
}
|
2021-03-19 11:12:56 +01:00
|
|
|
for _, change := range changes {
|
|
|
|
change(changeEvent)
|
|
|
|
}
|
|
|
|
return changeEvent, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2020-12-10 16:18:52 +01:00
|
|
|
}
|
|
|
|
|
2023-02-08 09:06:34 +01:00
|
|
|
func ChangeAccessTokenType(accessTokenType domain.OIDCTokenType) func(event *MachineChangedEvent) {
|
|
|
|
return func(e *MachineChangedEvent) {
|
|
|
|
e.AccessTokenType = &accessTokenType
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func MachineChangedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2021-01-04 14:52:13 +01:00
|
|
|
machineChanged := &MachineChangedEvent{
|
2020-12-10 16:18:52 +01:00
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
2023-10-19 12:19:10 +02:00
|
|
|
err := event.Unmarshal(machineChanged)
|
2020-12-10 16:18:52 +01:00
|
|
|
if err != nil {
|
2023-12-08 16:30:55 +02:00
|
|
|
return nil, zerrors.ThrowInternal(err, "USER-4M9ds", "unable to unmarshal machine changed")
|
2020-12-10 16:18:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return machineChanged, nil
|
|
|
|
}
|