2021-02-08 10:30:30 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2021-02-23 14:13:04 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
2021-02-18 13:48:27 +00:00
|
|
|
"time"
|
|
|
|
|
2021-02-23 14:13:04 +00:00
|
|
|
"github.com/caos/zitadel/internal/domain"
|
|
|
|
"github.com/caos/zitadel/internal/repository/user"
|
2021-02-08 10:30:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type MachineKeyWriteModel struct {
|
|
|
|
eventstore.WriteModel
|
|
|
|
|
|
|
|
KeyID string
|
2021-02-22 11:27:47 +00:00
|
|
|
KeyType domain.AuthNKeyType
|
2021-02-08 10:30:30 +00: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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *MachineKeyWriteModel) AppendEvents(events ...eventstore.EventReader) {
|
|
|
|
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 {
|
|
|
|
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent, user.AggregateType).
|
|
|
|
AggregateIDs(wm.AggregateID).
|
|
|
|
ResourceOwner(wm.ResourceOwner).
|
|
|
|
EventTypes(
|
|
|
|
user.MachineKeyAddedEventType,
|
|
|
|
user.MachineKeyRemovedEventType,
|
|
|
|
user.UserRemovedType)
|
|
|
|
}
|
2021-02-18 13:48:27 +00:00
|
|
|
|
|
|
|
func (wm *MachineKeyWriteModel) Exists() bool {
|
|
|
|
return wm.State != domain.MachineKeyStateUnspecified && wm.State != domain.MachineKeyStateRemoved
|
|
|
|
}
|