mirror of
https://github.com/zitadel/zitadel.git
synced 2024-12-12 11:04:25 +00:00
699fdaf68e
* feat: add machine tokens * fix test * rename to pat * fix merge and tests * fix scopes * fix migration version * fix test * Update internal/repository/user/personal_access_token.go Co-authored-by: Fabi <38692350+fgerschwiler@users.noreply.github.com> Co-authored-by: Fabi <38692350+fgerschwiler@users.noreply.github.com>
82 lines
2.1 KiB
Go
82 lines
2.1 KiB
Go
package command
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
|
|
|
"github.com/caos/zitadel/internal/domain"
|
|
"github.com/caos/zitadel/internal/repository/user"
|
|
)
|
|
|
|
type PersonalAccessTokenWriteModel struct {
|
|
eventstore.WriteModel
|
|
|
|
TokenID string
|
|
ExpirationDate time.Time
|
|
|
|
State domain.PersonalAccessTokenState
|
|
}
|
|
|
|
func NewPersonalAccessTokenWriteModel(userID, tokenID, resourceOwner string) *PersonalAccessTokenWriteModel {
|
|
return &PersonalAccessTokenWriteModel{
|
|
WriteModel: eventstore.WriteModel{
|
|
AggregateID: userID,
|
|
ResourceOwner: resourceOwner,
|
|
},
|
|
TokenID: tokenID,
|
|
}
|
|
}
|
|
|
|
func (wm *PersonalAccessTokenWriteModel) AppendEvents(events ...eventstore.Event) {
|
|
for _, event := range events {
|
|
switch e := event.(type) {
|
|
case *user.PersonalAccessTokenAddedEvent:
|
|
if wm.TokenID != e.TokenID {
|
|
continue
|
|
}
|
|
wm.WriteModel.AppendEvents(e)
|
|
case *user.PersonalAccessTokenRemovedEvent:
|
|
if wm.TokenID != e.TokenID {
|
|
continue
|
|
}
|
|
wm.WriteModel.AppendEvents(e)
|
|
case *user.UserRemovedEvent:
|
|
wm.WriteModel.AppendEvents(e)
|
|
}
|
|
}
|
|
}
|
|
|
|
func (wm *PersonalAccessTokenWriteModel) Reduce() error {
|
|
for _, event := range wm.Events {
|
|
switch e := event.(type) {
|
|
case *user.PersonalAccessTokenAddedEvent:
|
|
wm.TokenID = e.TokenID
|
|
wm.ExpirationDate = e.Expiration
|
|
wm.State = domain.PersonalAccessTokenStateActive
|
|
case *user.PersonalAccessTokenRemovedEvent:
|
|
wm.State = domain.PersonalAccessTokenStateRemoved
|
|
case *user.UserRemovedEvent:
|
|
wm.State = domain.PersonalAccessTokenStateRemoved
|
|
}
|
|
}
|
|
return wm.WriteModel.Reduce()
|
|
}
|
|
|
|
func (wm *PersonalAccessTokenWriteModel) Query() *eventstore.SearchQueryBuilder {
|
|
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent).
|
|
ResourceOwner(wm.ResourceOwner).
|
|
AddQuery().
|
|
AggregateTypes(user.AggregateType).
|
|
AggregateIDs(wm.AggregateID).
|
|
EventTypes(
|
|
user.PersonalAccessTokenAddedType,
|
|
user.PersonalAccessTokenRemovedType,
|
|
user.UserRemovedType).
|
|
Builder()
|
|
}
|
|
|
|
func (wm *PersonalAccessTokenWriteModel) Exists() bool {
|
|
return wm.State != domain.PersonalAccessTokenStateUnspecified && wm.State != domain.PersonalAccessTokenStateRemoved
|
|
}
|