2021-01-04 13:52:13 +00:00
|
|
|
package user
|
2020-12-10 15:18:52 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2021-02-23 14:13:04 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
2021-02-18 13:48:27 +00:00
|
|
|
|
2020-12-10 15:18:52 +00:00
|
|
|
"github.com/caos/zitadel/internal/errors"
|
2021-02-23 14:13:04 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore/repository"
|
2020-12-10 15:18:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-01-04 13:52:13 +00:00
|
|
|
machineEventPrefix = userEventTypePrefix + "machine."
|
2020-12-10 15:18:52 +00:00
|
|
|
MachineAddedEventType = machineEventPrefix + "added"
|
|
|
|
MachineChangedEventType = machineEventPrefix + "changed"
|
|
|
|
)
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
type MachineAddedEvent struct {
|
2020-12-10 15:18:52 +00:00
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
2021-01-21 09:49:38 +00:00
|
|
|
UserName string `json:"userName"`
|
|
|
|
UserLoginMustBeDomain bool
|
2020-12-10 15:18:52 +00:00
|
|
|
|
|
|
|
Name string `json:"name,omitempty"`
|
|
|
|
Description string `json:"description,omitempty"`
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
func (e *MachineAddedEvent) Data() interface{} {
|
2020-12-10 15:18:52 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2021-01-21 09:49:38 +00:00
|
|
|
func (e *MachineAddedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
2021-02-18 13:48:27 +00:00
|
|
|
return []*eventstore.EventUniqueConstraint{NewAddUsernameUniqueConstraint(e.UserName, e.Aggregate().ResourceOwner, e.UserLoginMustBeDomain)}
|
2021-01-21 09:49:38 +00:00
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
func NewMachineAddedEvent(
|
2020-12-10 15:18:52 +00:00
|
|
|
ctx context.Context,
|
2021-02-18 13:48:27 +00:00
|
|
|
aggregate *eventstore.Aggregate,
|
2020-12-10 15:18:52 +00:00
|
|
|
userName,
|
|
|
|
name,
|
|
|
|
description string,
|
2021-01-21 09:49:38 +00:00
|
|
|
userLoginMustBeDomain bool,
|
2021-01-04 13:52:13 +00:00
|
|
|
) *MachineAddedEvent {
|
|
|
|
return &MachineAddedEvent{
|
2020-12-10 15:18:52 +00:00
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
2021-02-18 13:48:27 +00:00
|
|
|
aggregate,
|
2020-12-10 15:18:52 +00:00
|
|
|
MachineAddedEventType,
|
|
|
|
),
|
2021-01-21 09:49:38 +00:00
|
|
|
UserName: userName,
|
|
|
|
Name: name,
|
|
|
|
Description: description,
|
|
|
|
UserLoginMustBeDomain: userLoginMustBeDomain,
|
2020-12-10 15:18:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
func MachineAddedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
|
|
|
|
machineAdded := &MachineAddedEvent{
|
2020-12-10 15:18:52 +00:00
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
|
|
|
err := json.Unmarshal(event.Data, machineAdded)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "USER-tMv9s", "unable to unmarshal machine added")
|
|
|
|
}
|
|
|
|
|
|
|
|
return machineAdded, nil
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
type MachineChangedEvent struct {
|
2020-12-10 15:18:52 +00:00
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
|
|
|
UserName string `json:"userName"`
|
|
|
|
|
2021-01-06 10:12:56 +00:00
|
|
|
Name *string `json:"name,omitempty"`
|
|
|
|
Description *string `json:"description,omitempty"`
|
2020-12-10 15:18:52 +00:00
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
func (e *MachineChangedEvent) Data() interface{} {
|
2020-12-10 15:18:52 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2021-01-21 09:49:38 +00:00
|
|
|
func (e *MachineChangedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
func NewMachineChangedEvent(
|
2020-12-10 15:18:52 +00:00
|
|
|
ctx context.Context,
|
2021-02-18 13:48:27 +00:00
|
|
|
aggregate *eventstore.Aggregate,
|
2021-01-04 13:52:13 +00:00
|
|
|
) *MachineChangedEvent {
|
|
|
|
return &MachineChangedEvent{
|
2020-12-10 15:18:52 +00:00
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
2021-02-18 13:48:27 +00:00
|
|
|
aggregate,
|
2020-12-10 15:18:52 +00:00
|
|
|
MachineChangedEventType,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
func MachineChangedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
|
|
|
|
machineChanged := &MachineChangedEvent{
|
2020-12-10 15:18:52 +00:00
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
|
|
|
err := json.Unmarshal(event.Data, machineChanged)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "USER-4M9ds", "unable to unmarshal machine changed")
|
|
|
|
}
|
|
|
|
|
|
|
|
return machineChanged, nil
|
|
|
|
}
|