From 9fc8a436420121783a785e1f97e3ad450d1a69e5 Mon Sep 17 00:00:00 2001 From: Livio Spring Date: Fri, 22 Jul 2022 13:47:55 +0200 Subject: [PATCH] fix: handle events of PATs correctly (#4024) --- internal/user/repository/view/model/token.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/internal/user/repository/view/model/token.go b/internal/user/repository/view/model/token.go index 38125cf2a7..972f707c10 100644 --- a/internal/user/repository/view/model/token.go +++ b/internal/user/repository/view/model/token.go @@ -66,7 +66,8 @@ func TokenViewToModel(token *TokenView) *usr_model.TokenView { func (t *TokenView) AppendEventIfMyToken(event *es_models.Event) (err error) { view := new(TokenView) switch eventstore.EventType(event.Type) { - case user_repo.UserTokenAddedType: + case user_repo.UserTokenAddedType, + user_repo.PersonalAccessTokenAddedType: view.setRootData(event) err = view.setData(event) case user_repo.UserTokenRemovedType: @@ -94,6 +95,8 @@ func (t *TokenView) AppendEventIfMyToken(event *es_models.Event) (err error) { t.Deactivated = false } return nil + case user_repo.PersonalAccessTokenRemovedType: + return t.appendPATRemoved(event) default: return nil } @@ -168,6 +171,17 @@ func (t *TokenView) appendRefreshTokenRemoved(event *es_models.Event) error { return nil } +func (t *TokenView) appendPATRemoved(event *es_models.Event) error { + pat, err := eventToMap(event) + if err != nil { + return err + } + if pat["tokenId"] == t.ID && t.IsPAT { + t.Deactivated = true + } + return nil +} + func eventToMap(event *es_models.Event) (map[string]interface{}, error) { m := make(map[string]interface{}) if err := json.Unmarshal(event.Data, &m); err != nil {