2021-01-06 11:12:56 +01:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-07-06 13:55:57 +02:00
|
|
|
|
2023-01-31 20:52:47 +01:00
|
|
|
"github.com/zitadel/zitadel/internal/crypto"
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
2021-01-12 12:59:51 +01:00
|
|
|
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/user"
|
2021-01-06 11:12:56 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type MachineWriteModel struct {
|
|
|
|
eventstore.WriteModel
|
|
|
|
|
|
|
|
UserName string
|
|
|
|
|
2023-02-08 09:06:34 +01:00
|
|
|
Name string
|
|
|
|
Description string
|
|
|
|
UserState domain.UserState
|
|
|
|
AccessTokenType domain.OIDCTokenType
|
2023-01-31 20:52:47 +01:00
|
|
|
|
|
|
|
ClientSecret *crypto.CryptoValue
|
2021-01-06 11:12:56 +01:00
|
|
|
}
|
|
|
|
|
2021-01-12 12:59:51 +01:00
|
|
|
func NewMachineWriteModel(userID, resourceOwner string) *MachineWriteModel {
|
2021-01-06 11:12:56 +01:00
|
|
|
return &MachineWriteModel{
|
|
|
|
WriteModel: eventstore.WriteModel{
|
2021-01-12 12:59:51 +01:00
|
|
|
AggregateID: userID,
|
|
|
|
ResourceOwner: resourceOwner,
|
2021-01-06 11:12:56 +01: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
|
2023-02-08 09:06:34 +01:00
|
|
|
wm.AccessTokenType = e.AccessTokenType
|
2021-01-06 11:12:56 +01:00
|
|
|
wm.UserState = domain.UserStateActive
|
2021-01-07 16:06:45 +01:00
|
|
|
case *user.UsernameChangedEvent:
|
|
|
|
wm.UserName = e.UserName
|
2021-01-06 11:12:56 +01:00
|
|
|
case *user.MachineChangedEvent:
|
|
|
|
if e.Name != nil {
|
|
|
|
wm.Name = *e.Name
|
|
|
|
}
|
|
|
|
if e.Description != nil {
|
|
|
|
wm.Description = *e.Description
|
|
|
|
}
|
2023-02-08 09:06:34 +01:00
|
|
|
if e.AccessTokenType != nil {
|
|
|
|
wm.AccessTokenType = *e.AccessTokenType
|
|
|
|
}
|
2021-01-06 11:12:56 +01:00
|
|
|
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 20:52:47 +01:00
|
|
|
case *user.MachineSecretSetEvent:
|
|
|
|
wm.ClientSecret = e.ClientSecret
|
|
|
|
case *user.MachineSecretRemovedEvent:
|
|
|
|
wm.ClientSecret = nil
|
2021-01-06 11:12:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return wm.WriteModel.Reduce()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *MachineWriteModel) Query() *eventstore.SearchQueryBuilder {
|
2021-07-06 13:55:57 +02:00
|
|
|
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent).
|
2021-02-18 14:48:27 +01:00
|
|
|
ResourceOwner(wm.ResourceOwner).
|
2021-07-06 13:55:57 +02:00
|
|
|
AddQuery().
|
|
|
|
AggregateTypes(user.AggregateType).
|
|
|
|
AggregateIDs(wm.AggregateID).
|
2021-02-18 14:48:27 +01:00
|
|
|
EventTypes(user.MachineAddedEventType,
|
|
|
|
user.UserUserNameChangedType,
|
|
|
|
user.MachineChangedEventType,
|
|
|
|
user.UserLockedType,
|
|
|
|
user.UserUnlockedType,
|
|
|
|
user.UserDeactivatedType,
|
|
|
|
user.UserReactivatedType,
|
2023-01-31 20:52:47 +01:00
|
|
|
user.UserRemovedType,
|
|
|
|
user.MachineSecretSetType,
|
|
|
|
user.MachineSecretRemovedType).
|
2021-07-06 13:55:57 +02:00
|
|
|
Builder()
|
2021-01-06 11:12:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *MachineWriteModel) NewChangedEvent(
|
|
|
|
ctx context.Context,
|
2021-02-18 14:48:27 +01:00
|
|
|
aggregate *eventstore.Aggregate,
|
2021-01-06 11:12:56 +01:00
|
|
|
name,
|
|
|
|
description string,
|
2023-02-08 09:06:34 +01:00
|
|
|
accessTokenType domain.OIDCTokenType,
|
2021-03-19 11:12:56 +01:00
|
|
|
) (*user.MachineChangedEvent, bool, error) {
|
|
|
|
changes := make([]user.MachineChanges, 0)
|
|
|
|
var err error
|
|
|
|
|
2021-01-06 11:12:56 +01:00
|
|
|
if wm.Name != name {
|
2021-03-19 11:12:56 +01:00
|
|
|
changes = append(changes, user.ChangeName(name))
|
2021-01-06 11:12:56 +01:00
|
|
|
}
|
|
|
|
if wm.Description != description {
|
2021-03-19 11:12:56 +01:00
|
|
|
changes = append(changes, user.ChangeDescription(description))
|
|
|
|
}
|
2023-02-08 09:06:34 +01:00
|
|
|
if wm.AccessTokenType != accessTokenType {
|
|
|
|
changes = append(changes, user.ChangeAccessTokenType(accessTokenType))
|
|
|
|
}
|
2021-03-19 11:12:56 +01:00
|
|
|
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 11:12:56 +01:00
|
|
|
}
|
2021-03-19 11:12:56 +01:00
|
|
|
return changeEvent, true, nil
|
2021-01-06 11:12:56 +01:00
|
|
|
}
|