gitea源码

12345678910111213141516171819202122232425262728293031323334353637383940
  1. // Copyright 2021 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package asymkey
  4. import (
  5. "context"
  6. asymkey_model "code.gitea.io/gitea/models/asymkey"
  7. "code.gitea.io/gitea/models/db"
  8. user_model "code.gitea.io/gitea/models/user"
  9. )
  10. // DeletePublicKey deletes SSH key information both in database and authorized_keys file.
  11. func DeletePublicKey(ctx context.Context, doer *user_model.User, id int64) (err error) {
  12. key, err := asymkey_model.GetPublicKeyByID(ctx, id)
  13. if err != nil {
  14. return err
  15. }
  16. // Check if user has access to delete this key.
  17. if !doer.IsAdmin && doer.ID != key.OwnerID {
  18. return asymkey_model.ErrKeyAccessDenied{
  19. UserID: doer.ID,
  20. KeyID: key.ID,
  21. Note: "public",
  22. }
  23. }
  24. if _, err = db.DeleteByID[asymkey_model.PublicKey](ctx, id); err != nil {
  25. return err
  26. }
  27. if key.Type == asymkey_model.KeyTypePrincipal {
  28. return RewriteAllPrincipalKeys(ctx)
  29. }
  30. return RewriteAllPublicKeys(ctx)
  31. }