2021-02-08 11:30:30 +01:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2021-02-18 14:48:27 +01:00
|
|
|
"time"
|
|
|
|
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
2024-04-03 13:43:43 +03:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/user"
|
2021-02-08 11:30:30 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type MachineKeyWriteModel struct {
|
|
|
|
eventstore.WriteModel
|
|
|
|
|
|
|
|
KeyID string
|
2021-02-22 12:27:47 +01:00
|
|
|
KeyType domain.AuthNKeyType
|
2021-02-08 11:30:30 +01:00
|
|
|
ExpirationDate time.Time
|
|
|
|
|
|
|
|
State domain.MachineKeyState
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewMachineKeyWriteModel(userID, keyID, resourceOwner string) *MachineKeyWriteModel {
|
|
|
|
return &MachineKeyWriteModel{
|
|
|
|
WriteModel: eventstore.WriteModel{
|
|
|
|
AggregateID: userID,
|
|
|
|
ResourceOwner: resourceOwner,
|
|
|
|
},
|
|
|
|
KeyID: keyID,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-03 09:19:07 +01:00
|
|
|
func (wm *MachineKeyWriteModel) AppendEvents(events ...eventstore.Event) {
|
2021-02-08 11:30:30 +01:00
|
|
|
for _, event := range events {
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *user.MachineKeyAddedEvent:
|
|
|
|
if wm.KeyID != e.KeyID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.WriteModel.AppendEvents(e)
|
|
|
|
case *user.MachineKeyRemovedEvent:
|
|
|
|
if wm.KeyID != e.KeyID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.WriteModel.AppendEvents(e)
|
|
|
|
case *user.UserRemovedEvent:
|
|
|
|
wm.WriteModel.AppendEvents(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *MachineKeyWriteModel) Reduce() error {
|
|
|
|
for _, event := range wm.Events {
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *user.MachineKeyAddedEvent:
|
|
|
|
wm.KeyID = e.KeyID
|
|
|
|
wm.KeyType = e.KeyType
|
|
|
|
wm.ExpirationDate = e.ExpirationDate
|
|
|
|
wm.State = domain.MachineKeyStateActive
|
|
|
|
case *user.MachineKeyRemovedEvent:
|
|
|
|
wm.State = domain.MachineKeyStateRemoved
|
|
|
|
case *user.UserRemovedEvent:
|
|
|
|
wm.State = domain.MachineKeyStateRemoved
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return wm.WriteModel.Reduce()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *MachineKeyWriteModel) Query() *eventstore.SearchQueryBuilder {
|
2021-07-06 13:55:57 +02:00
|
|
|
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent).
|
2021-02-08 11:30:30 +01:00
|
|
|
ResourceOwner(wm.ResourceOwner).
|
2021-07-06 13:55:57 +02:00
|
|
|
AddQuery().
|
|
|
|
AggregateTypes(user.AggregateType).
|
|
|
|
AggregateIDs(wm.AggregateID).
|
2021-02-08 11:30:30 +01:00
|
|
|
EventTypes(
|
|
|
|
user.MachineKeyAddedEventType,
|
|
|
|
user.MachineKeyRemovedEventType,
|
2021-07-06 13:55:57 +02:00
|
|
|
user.UserRemovedType).
|
|
|
|
Builder()
|
2021-02-08 11:30:30 +01:00
|
|
|
}
|
2021-02-18 14:48:27 +01:00
|
|
|
|
|
|
|
func (wm *MachineKeyWriteModel) Exists() bool {
|
|
|
|
return wm.State != domain.MachineKeyStateUnspecified && wm.State != domain.MachineKeyStateRemoved
|
|
|
|
}
|