2021-01-06 10:12:56 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-07-06 11:55:57 +00:00
|
|
|
|
2023-01-31 19:52:47 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/crypto"
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
2021-01-12 11:59:51 +00:00
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/user"
|
2021-01-06 10:12:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type MachineWriteModel struct {
|
|
|
|
eventstore.WriteModel
|
|
|
|
|
|
|
|
UserName string
|
|
|
|
|
|
|
|
Name string
|
|
|
|
Description string
|
|
|
|
UserState domain.UserState
|
2023-01-31 19:52:47 +00:00
|
|
|
|
|
|
|
ClientSecret *crypto.CryptoValue
|
2021-01-06 10:12:56 +00:00
|
|
|
}
|
|
|
|
|
2021-01-12 11:59:51 +00:00
|
|
|
func NewMachineWriteModel(userID, resourceOwner string) *MachineWriteModel {
|
2021-01-06 10:12:56 +00:00
|
|
|
return &MachineWriteModel{
|
|
|
|
WriteModel: eventstore.WriteModel{
|
2021-01-12 11:59:51 +00:00
|
|
|
AggregateID: userID,
|
|
|
|
ResourceOwner: resourceOwner,
|
2021-01-06 10:12:56 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *MachineWriteModel) Reduce() error {
|
|
|
|
for _, event := range wm.Events {
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *user.MachineAddedEvent:
|
|
|
|
wm.UserName = e.UserName
|
|
|
|
wm.Name = e.Name
|
|
|
|
wm.Description = e.Description
|
|
|
|
wm.UserState = domain.UserStateActive
|
2021-01-07 15:06:45 +00:00
|
|
|
case *user.UsernameChangedEvent:
|
|
|
|
wm.UserName = e.UserName
|
2021-01-06 10:12:56 +00:00
|
|
|
case *user.MachineChangedEvent:
|
|
|
|
if e.Name != nil {
|
|
|
|
wm.Name = *e.Name
|
|
|
|
}
|
|
|
|
if e.Description != nil {
|
|
|
|
wm.Description = *e.Description
|
|
|
|
}
|
|
|
|
case *user.UserLockedEvent:
|
|
|
|
if wm.UserState != domain.UserStateDeleted {
|
|
|
|
wm.UserState = domain.UserStateLocked
|
|
|
|
}
|
|
|
|
case *user.UserUnlockedEvent:
|
|
|
|
if wm.UserState != domain.UserStateDeleted {
|
|
|
|
wm.UserState = domain.UserStateActive
|
|
|
|
}
|
|
|
|
case *user.UserDeactivatedEvent:
|
|
|
|
if wm.UserState != domain.UserStateDeleted {
|
|
|
|
wm.UserState = domain.UserStateInactive
|
|
|
|
}
|
|
|
|
case *user.UserReactivatedEvent:
|
|
|
|
if wm.UserState != domain.UserStateDeleted {
|
|
|
|
wm.UserState = domain.UserStateActive
|
|
|
|
}
|
|
|
|
case *user.UserRemovedEvent:
|
|
|
|
wm.UserState = domain.UserStateDeleted
|
2023-01-31 19:52:47 +00:00
|
|
|
case *user.MachineSecretSetEvent:
|
|
|
|
wm.ClientSecret = e.ClientSecret
|
|
|
|
case *user.MachineSecretRemovedEvent:
|
|
|
|
wm.ClientSecret = nil
|
2021-01-06 10:12:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return wm.WriteModel.Reduce()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *MachineWriteModel) Query() *eventstore.SearchQueryBuilder {
|
2021-07-06 11:55:57 +00:00
|
|
|
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent).
|
2021-02-18 13:48:27 +00:00
|
|
|
ResourceOwner(wm.ResourceOwner).
|
2021-07-06 11:55:57 +00:00
|
|
|
AddQuery().
|
|
|
|
AggregateTypes(user.AggregateType).
|
|
|
|
AggregateIDs(wm.AggregateID).
|
2021-02-18 13:48:27 +00:00
|
|
|
EventTypes(user.MachineAddedEventType,
|
|
|
|
user.UserUserNameChangedType,
|
|
|
|
user.MachineChangedEventType,
|
|
|
|
user.UserLockedType,
|
|
|
|
user.UserUnlockedType,
|
|
|
|
user.UserDeactivatedType,
|
|
|
|
user.UserReactivatedType,
|
2023-01-31 19:52:47 +00:00
|
|
|
user.UserRemovedType,
|
|
|
|
user.MachineSecretSetType,
|
|
|
|
user.MachineSecretRemovedType).
|
2021-07-06 11:55:57 +00:00
|
|
|
Builder()
|
2021-01-06 10:12:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *MachineWriteModel) NewChangedEvent(
|
|
|
|
ctx context.Context,
|
2021-02-18 13:48:27 +00:00
|
|
|
aggregate *eventstore.Aggregate,
|
2021-01-06 10:12:56 +00:00
|
|
|
name,
|
|
|
|
description string,
|
2021-03-19 10:12:56 +00:00
|
|
|
) (*user.MachineChangedEvent, bool, error) {
|
|
|
|
changes := make([]user.MachineChanges, 0)
|
|
|
|
var err error
|
|
|
|
|
2021-01-06 10:12:56 +00:00
|
|
|
if wm.Name != name {
|
2021-03-19 10:12:56 +00:00
|
|
|
changes = append(changes, user.ChangeName(name))
|
2021-01-06 10:12:56 +00:00
|
|
|
}
|
|
|
|
if wm.Description != description {
|
2021-03-19 10:12:56 +00:00
|
|
|
changes = append(changes, user.ChangeDescription(description))
|
|
|
|
}
|
|
|
|
if len(changes) == 0 {
|
|
|
|
return nil, false, nil
|
|
|
|
}
|
|
|
|
changeEvent, err := user.NewMachineChangedEvent(ctx, aggregate, changes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
2021-01-06 10:12:56 +00:00
|
|
|
}
|
2021-03-19 10:12:56 +00:00
|
|
|
return changeEvent, true, nil
|
2021-01-06 10:12:56 +00:00
|
|
|
}
|