fix: handle events of PATs correctly (#4024)

This commit is contained in:
Livio Spring 2022-07-22 13:47:55 +02:00 committed by GitHub
parent 3855ec2ab5
commit 9fc8a43642
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -66,7 +66,8 @@ func TokenViewToModel(token *TokenView) *usr_model.TokenView {
func (t *TokenView) AppendEventIfMyToken(event *es_models.Event) (err error) { func (t *TokenView) AppendEventIfMyToken(event *es_models.Event) (err error) {
view := new(TokenView) view := new(TokenView)
switch eventstore.EventType(event.Type) { switch eventstore.EventType(event.Type) {
case user_repo.UserTokenAddedType: case user_repo.UserTokenAddedType,
user_repo.PersonalAccessTokenAddedType:
view.setRootData(event) view.setRootData(event)
err = view.setData(event) err = view.setData(event)
case user_repo.UserTokenRemovedType: case user_repo.UserTokenRemovedType:
@ -94,6 +95,8 @@ func (t *TokenView) AppendEventIfMyToken(event *es_models.Event) (err error) {
t.Deactivated = false t.Deactivated = false
} }
return nil return nil
case user_repo.PersonalAccessTokenRemovedType:
return t.appendPATRemoved(event)
default: default:
return nil return nil
} }
@ -168,6 +171,17 @@ func (t *TokenView) appendRefreshTokenRemoved(event *es_models.Event) error {
return nil 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) { func eventToMap(event *es_models.Event) (map[string]interface{}, error) {
m := make(map[string]interface{}) m := make(map[string]interface{})
if err := json.Unmarshal(event.Data, &m); err != nil { if err := json.Unmarshal(event.Data, &m); err != nil {