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/domain"
|
2024-04-03 10:43:43 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/user"
|
2021-01-06 10:12:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type MachineWriteModel struct {
|
|
|
|
eventstore.WriteModel
|
|
|
|
|
|
|
|
UserName string
|
|
|
|
|
2023-02-08 08:06:34 +00:00
|
|
|
Name string
|
|
|
|
Description string
|
|
|
|
UserState domain.UserState
|
|
|
|
AccessTokenType domain.OIDCTokenType
|
2024-04-05 09:35:49 +00:00
|
|
|
HashedSecret string
|
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
|
2023-02-08 08:06:34 +00:00
|
|
|
wm.AccessTokenType = e.AccessTokenType
|
2021-01-06 10:12:56 +00:00
|
|
|
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
|
|
|
|
}
|
2023-02-08 08:06:34 +00:00
|
|
|
if e.AccessTokenType != nil {
|
|
|
|
wm.AccessTokenType = *e.AccessTokenType
|
|
|
|
}
|
2021-01-06 10:12:56 +00: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 19:52:47 +00:00
|
|
|
case *user.MachineSecretSetEvent:
|
2024-04-05 09:35:49 +00:00
|
|
|
wm.HashedSecret = crypto.SecretOrEncodedHash(e.ClientSecret, e.HashedSecret)
|
2023-01-31 19:52:47 +00:00
|
|
|
case *user.MachineSecretRemovedEvent:
|
2024-04-05 09:35:49 +00:00
|
|
|
wm.HashedSecret = ""
|
|
|
|
case *user.MachineSecretHashUpdatedEvent:
|
|
|
|
wm.HashedSecret = e.HashedSecret
|
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,
|
2024-04-05 09:35:49 +00:00
|
|
|
user.MachineSecretRemovedType,
|
|
|
|
user.MachineSecretHashUpdatedType,
|
|
|
|
).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,
|
2023-02-08 08:06:34 +00:00
|
|
|
accessTokenType domain.OIDCTokenType,
|
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))
|
|
|
|
}
|
2023-02-08 08:06:34 +00:00
|
|
|
if wm.AccessTokenType != accessTokenType {
|
|
|
|
changes = append(changes, user.ChangeAccessTokenType(accessTokenType))
|
|
|
|
}
|
2021-03-19 10:12:56 +00: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 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
|
|
|
}
|