summaryrefslogtreecommitdiffstats
path: root/models/repo.go
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2021-12-10 16:14:24 +0800
committerGitHub <noreply@github.com>2021-12-10 16:14:24 +0800
commit3ca5dc7e32b372d14ff80d96f14b8f6a805862f1 (patch)
tree50d193ed0dacf2888d57b193a9b0d36065aff205 /models/repo.go
parent0a9fcf63a49799ad3b0f146c54879161bac61e10 (diff)
downloadgitea-3ca5dc7e32b372d14ff80d96f14b8f6a805862f1.tar.gz
gitea-3ca5dc7e32b372d14ff80d96f14b8f6a805862f1.zip
Move keys to models/asymkey (#17917)
* Move keys to models/keys * Rename models/keys -> models/asymkey * change the missed package name * Fix package alias * Fix test * Fix docs * Fix test * Fix test * merge
Diffstat (limited to 'models/repo.go')
-rw-r--r--models/repo.go61
1 files changed, 59 insertions, 2 deletions
diff --git a/models/repo.go b/models/repo.go
index 6bdc4c20d2..adc62c9528 100644
--- a/models/repo.go
+++ b/models/repo.go
@@ -20,6 +20,7 @@ import (
_ "image/jpeg" // Needed for jpeg support
admin_model "code.gitea.io/gitea/models/admin"
+ asymkey_model "code.gitea.io/gitea/models/asymkey"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/perm"
repo_model "code.gitea.io/gitea/models/repo"
@@ -856,12 +857,13 @@ func DeleteRepository(doer *user_model.User, uid, repoID int64) error {
}
// Delete Deploy Keys
- deployKeys, err := listDeployKeys(sess, &ListDeployKeysOptions{RepoID: repoID})
+ deployKeys, err := asymkey_model.ListDeployKeys(ctx, &asymkey_model.ListDeployKeysOptions{RepoID: repoID})
if err != nil {
return fmt.Errorf("listDeployKeys: %v", err)
}
+ var needRewriteKeysFile = len(deployKeys) > 0
for _, dKey := range deployKeys {
- if err := deleteDeployKey(ctx, doer, dKey.ID); err != nil {
+ if err := DeleteDeployKey(ctx, doer, dKey.ID); err != nil {
return fmt.Errorf("deleteDeployKeys: %v", err)
}
}
@@ -1049,6 +1051,12 @@ func DeleteRepository(doer *user_model.User, uid, repoID int64) error {
committer.Close()
+ if needRewriteKeysFile {
+ if err := asymkey_model.RewriteAllPublicKeys(); err != nil {
+ log.Error("RewriteAllPublicKeys failed: %v", err)
+ }
+ }
+
// We should always delete the files after the database transaction succeed. If
// we delete the file but the database rollback, the repository will be broken.
@@ -1407,3 +1415,52 @@ func LinkedRepository(a *repo_model.Attachment) (*repo_model.Repository, unit.Ty
}
return nil, -1, nil
}
+
+// DeleteDeployKey delete deploy keys
+func DeleteDeployKey(ctx context.Context, doer *user_model.User, id int64) error {
+ key, err := asymkey_model.GetDeployKeyByID(ctx, id)
+ if err != nil {
+ if asymkey_model.IsErrDeployKeyNotExist(err) {
+ return nil
+ }
+ return fmt.Errorf("GetDeployKeyByID: %v", err)
+ }
+
+ sess := db.GetEngine(ctx)
+
+ // Check if user has access to delete this key.
+ if !doer.IsAdmin {
+ repo, err := repo_model.GetRepositoryByIDCtx(ctx, key.RepoID)
+ if err != nil {
+ return fmt.Errorf("GetRepositoryByID: %v", err)
+ }
+ has, err := isUserRepoAdmin(sess, repo, doer)
+ if err != nil {
+ return fmt.Errorf("GetUserRepoPermission: %v", err)
+ } else if !has {
+ return asymkey_model.ErrKeyAccessDenied{
+ UserID: doer.ID,
+ KeyID: key.ID,
+ Note: "deploy",
+ }
+ }
+ }
+
+ if _, err = sess.ID(key.ID).Delete(new(asymkey_model.DeployKey)); err != nil {
+ return fmt.Errorf("delete deploy key [%d]: %v", key.ID, err)
+ }
+
+ // Check if this is the last reference to same key content.
+ has, err := sess.
+ Where("key_id = ?", key.KeyID).
+ Get(new(asymkey_model.DeployKey))
+ if err != nil {
+ return err
+ } else if !has {
+ if err = asymkey_model.DeletePublicKeys(ctx, key.KeyID); err != nil {
+ return err
+ }
+ }
+
+ return nil
+}