MCPcopy Create free account
hub / github.com/gogs/gogs / DeletePublicKey

Function DeletePublicKey

internal/database/ssh_key.go:483–512  ·  view source on GitHub ↗

DeletePublicKey deletes SSH key information both in database and authorized_keys file.

(doer *User, id int64)

Source from the content-addressed store, hash-verified

481
482// DeletePublicKey deletes SSH key information both in database and authorized_keys file.
483func DeletePublicKey(doer *User, id int64) (err error) {
484 key, err := GetPublicKeyByID(id)
485 if err != nil {
486 if IsErrKeyNotExist(err) {
487 return nil
488 }
489 return errors.Newf("GetPublicKeyByID: %v", err)
490 }
491
492 // Check if user has access to delete this key.
493 if !doer.IsAdmin && doer.ID != key.OwnerID {
494 return ErrKeyAccessDenied{doer.ID, key.ID, "public"}
495 }
496
497 sess := x.NewSession()
498 defer sess.Close()
499 if err = sess.Begin(); err != nil {
500 return err
501 }
502
503 if err = deletePublicKeys(sess, id); err != nil {
504 return err
505 }
506
507 if err = sess.Commit(); err != nil {
508 return err
509 }
510
511 return RewriteAuthorizedKeys()
512}
513
514// RewriteAuthorizedKeys removes any authorized key and rewrite all keys from database again.
515// Note: x.Iterate does not get latest data after insert/delete, so we have to call this function

Callers 2

DeleteSSHKeyFunction · 0.92
DeletePublicKeyFunction · 0.92

Calls 4

GetPublicKeyByIDFunction · 0.85
IsErrKeyNotExistFunction · 0.85
deletePublicKeysFunction · 0.85
RewriteAuthorizedKeysFunction · 0.85

Tested by

no test coverage detected