add activity events on PAT creation and deletion

This commit is contained in:
Pascal Fischer 2023-03-31 17:41:22 +02:00
parent 931c20c8fe
commit b2da0ae70f
2 changed files with 23 additions and 0 deletions

View File

@ -83,6 +83,10 @@ const (
AccountPeerLoginExpirationDisabled AccountPeerLoginExpirationDisabled
// AccountPeerLoginExpirationDurationUpdated indicates that a user updated peer login expiration duration for the account // AccountPeerLoginExpirationDurationUpdated indicates that a user updated peer login expiration duration for the account
AccountPeerLoginExpirationDurationUpdated AccountPeerLoginExpirationDurationUpdated
// PersonalAccessTokenCreated indicates that a user created a personal access token
PersonalAccessTokenCreated
// PersonalAccessTokenDeleted indicates that a user deleted a personal access token
PersonalAccessTokenDeleted
) )
const ( const (
@ -168,6 +172,10 @@ const (
AccountPeerLoginExpirationDisabledMessage string = "Peer login expiration disabled for the account" AccountPeerLoginExpirationDisabledMessage string = "Peer login expiration disabled for the account"
// AccountPeerLoginExpirationDurationUpdatedMessage is a human-readable text message of the AccountPeerLoginExpirationDurationUpdated activity // AccountPeerLoginExpirationDurationUpdatedMessage is a human-readable text message of the AccountPeerLoginExpirationDurationUpdated activity
AccountPeerLoginExpirationDurationUpdatedMessage string = "Peer login expiration duration updated" AccountPeerLoginExpirationDurationUpdatedMessage string = "Peer login expiration duration updated"
// PersonalAccessTokenCreatedMessage is a human-readable text message of the PersonalAccessTokenCreated activity
PersonalAccessTokenCreatedMessage string = "Personal access token created"
// PersonalAccessTokenDeletedMessage is a human-readable text message of the PersonalAccessTokenDeleted activity
PersonalAccessTokenDeletedMessage string = "Personal access token deleted"
) )
// Activity that triggered an Event // Activity that triggered an Event
@ -258,6 +266,10 @@ func (a Activity) Message() string {
return AccountPeerLoginExpirationDisabledMessage return AccountPeerLoginExpirationDisabledMessage
case AccountPeerLoginExpirationDurationUpdated: case AccountPeerLoginExpirationDurationUpdated:
return AccountPeerLoginExpirationDurationUpdatedMessage return AccountPeerLoginExpirationDurationUpdatedMessage
case PersonalAccessTokenCreated:
return PersonalAccessTokenCreatedMessage
case PersonalAccessTokenDeleted:
return PersonalAccessTokenDeletedMessage
default: default:
return "UNKNOWN_ACTIVITY" return "UNKNOWN_ACTIVITY"
} }
@ -348,6 +360,10 @@ func (a Activity) StringCode() string {
return "account.setting.peer.login.expiration.enable" return "account.setting.peer.login.expiration.enable"
case AccountPeerLoginExpirationDisabled: case AccountPeerLoginExpirationDisabled:
return "account.setting.peer.login.expiration.disable" return "account.setting.peer.login.expiration.disable"
case PersonalAccessTokenCreated:
return "personal.access.token.create"
case PersonalAccessTokenDeleted:
return "personal.access.token.delete"
default: default:
return "UNKNOWN_ACTIVITY" return "UNKNOWN_ACTIVITY"
} }

View File

@ -232,6 +232,9 @@ func (am *DefaultAccountManager) CreatePAT(accountID string, executingUserID str
return nil, status.Errorf(status.Internal, "failed to save account: %v", err) return nil, status.Errorf(status.Internal, "failed to save account: %v", err)
} }
meta := map[string]any{"name": pat.Name}
am.storeEvent(executingUserID, targetUserId, accountID, activity.PersonalAccessTokenCreated, meta)
return pat, nil return pat, nil
} }
@ -267,6 +270,10 @@ func (am *DefaultAccountManager) DeletePAT(accountID string, executingUserID str
if err != nil { if err != nil {
return status.Errorf(status.Internal, "Failed to delete hashed token index: %s", err) return status.Errorf(status.Internal, "Failed to delete hashed token index: %s", err)
} }
meta := map[string]any{"name": pat.Name}
am.storeEvent(executingUserID, targetUserID, accountID, activity.PersonalAccessTokenDeleted, meta)
delete(user.PATs, tokenID) delete(user.PATs, tokenID)
err = am.Store.SaveAccount(account) err = am.Store.SaveAccount(account)