Skip to content

Commit 5c0bee9

Browse files
authored
refactor update ssh key use time (#1466)
1 parent d9db188 commit 5c0bee9

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

cmd/serv.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -316,13 +316,7 @@ func runServ(c *cli.Context) error {
316316

317317
// Update user key activity.
318318
if keyID > 0 {
319-
key, err := models.GetPublicKeyByID(keyID)
320-
if err != nil {
321-
fail("Internal error", "GetPublicKeyById: %v", err)
322-
}
323-
324-
key.Updated = time.Now()
325-
if err = models.UpdatePublicKey(key); err != nil {
319+
if err = models.UpdatePublicKeyUpdated(keyID); err != nil {
326320
fail("Internal error", "UpdatePublicKey: %v", err)
327321
}
328322
}

models/ssh_key.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,20 @@ func UpdatePublicKey(key *PublicKey) error {
500500
return err
501501
}
502502

503+
// UpdatePublicKeyUpdated updates public key use time.
504+
func UpdatePublicKeyUpdated(id int64) error {
505+
cnt, err := x.ID(id).Cols("updated").Update(&PublicKey{
506+
Updated: time.Now(),
507+
})
508+
if err != nil {
509+
return err
510+
}
511+
if cnt != 1 {
512+
return ErrKeyNotExist{id}
513+
}
514+
return nil
515+
}
516+
503517
// deletePublicKeys does the actual key deletion but does not update authorized_keys file.
504518
func deletePublicKeys(e *xorm.Session, keyIDs ...int64) error {
505519
if len(keyIDs) == 0 {

0 commit comments

Comments
 (0)