summaryrefslogtreecommitdiffstats
path: root/models/gpg_key.go
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2021-09-19 19:49:59 +0800
committerGitHub <noreply@github.com>2021-09-19 19:49:59 +0800
commita4bfef265d9e512830350635a0489c2cdcd6508f (patch)
tree1e3c2ec94276dfcb2f8ba73a2ac075ba39c4a34a /models/gpg_key.go
parent462306e263db5a809dbe2cdf62e99307aeff28de (diff)
downloadgitea-a4bfef265d9e512830350635a0489c2cdcd6508f.tar.gz
gitea-a4bfef265d9e512830350635a0489c2cdcd6508f.zip
Move db related basic functions to models/db (#17075)
* Move db related basic functions to models/db * Fix lint * Fix lint * Fix test * Fix lint * Fix lint * revert unnecessary change * Fix test * Fix wrong replace string * Use *Context * Correct committer spelling and fix wrong replaced words Co-authored-by: zeripath <art27@cantab.net>
Diffstat (limited to 'models/gpg_key.go')
-rw-r--r--models/gpg_key.go27
1 files changed, 16 insertions, 11 deletions
diff --git a/models/gpg_key.go b/models/gpg_key.go
index cd286604a3..fabb3d5c91 100644
--- a/models/gpg_key.go
+++ b/models/gpg_key.go
@@ -9,6 +9,7 @@ import (
"strings"
"time"
+ "code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/timeutil"
@@ -43,6 +44,10 @@ type GPGKey struct {
CanCertify bool
}
+func init() {
+ db.RegisterModel(new(GPGKey))
+}
+
// BeforeInsert will be invoked by XORM before inserting a record
func (key *GPGKey) BeforeInsert() {
key.AddedUnix = timeutil.TimeStampNow()
@@ -58,10 +63,10 @@ func (key *GPGKey) AfterLoad(session *xorm.Session) {
// ListGPGKeys returns a list of public keys belongs to given user.
func ListGPGKeys(uid int64, listOptions ListOptions) ([]*GPGKey, error) {
- return listGPGKeys(x, uid, listOptions)
+ return listGPGKeys(db.DefaultContext().Engine(), uid, listOptions)
}
-func listGPGKeys(e Engine, uid int64, listOptions ListOptions) ([]*GPGKey, error) {
+func listGPGKeys(e db.Engine, uid int64, listOptions ListOptions) ([]*GPGKey, error) {
sess := e.Table(&GPGKey{}).Where("owner_id=? AND primary_key_id=''", uid)
if listOptions.Page != 0 {
sess = setSessionPagination(sess, &listOptions)
@@ -73,13 +78,13 @@ func listGPGKeys(e Engine, uid int64, listOptions ListOptions) ([]*GPGKey, error
// CountUserGPGKeys return number of gpg keys a user own
func CountUserGPGKeys(userID int64) (int64, error) {
- return x.Where("owner_id=? AND primary_key_id=''", userID).Count(&GPGKey{})
+ return db.DefaultContext().Engine().Where("owner_id=? AND primary_key_id=''", userID).Count(&GPGKey{})
}
// GetGPGKeyByID returns public key by given ID.
func GetGPGKeyByID(keyID int64) (*GPGKey, error) {
key := new(GPGKey)
- has, err := x.ID(keyID).Get(key)
+ has, err := db.DefaultContext().Engine().ID(keyID).Get(key)
if err != nil {
return nil, err
} else if !has {
@@ -91,7 +96,7 @@ func GetGPGKeyByID(keyID int64) (*GPGKey, error) {
// GetGPGKeysByKeyID returns public key by given ID.
func GetGPGKeysByKeyID(keyID string) ([]*GPGKey, error) {
keys := make([]*GPGKey, 0, 1)
- return keys, x.Where("key_id=?", keyID).Find(&keys)
+ return keys, db.DefaultContext().Engine().Where("key_id=?", keyID).Find(&keys)
}
// GPGKeyToEntity retrieve the imported key and the traducted entity
@@ -195,7 +200,7 @@ func parseGPGKey(ownerID int64, e *openpgp.Entity, verified bool) (*GPGKey, erro
}
// deleteGPGKey does the actual key deletion
-func deleteGPGKey(e *xorm.Session, keyID string) (int64, error) {
+func deleteGPGKey(e db.Engine, keyID string) (int64, error) {
if keyID == "" {
return 0, fmt.Errorf("empty KeyId forbidden") // Should never happen but just to be sure
}
@@ -222,17 +227,17 @@ func DeleteGPGKey(doer *User, id int64) (err error) {
return ErrGPGKeyAccessDenied{doer.ID, key.ID}
}
- sess := x.NewSession()
- defer sess.Close()
- if err = sess.Begin(); err != nil {
+ ctx, committer, err := db.TxContext()
+ if err != nil {
return err
}
+ defer committer.Close()
- if _, err = deleteGPGKey(sess, key.KeyID); err != nil {
+ if _, err = deleteGPGKey(ctx.Engine(), key.KeyID); err != nil {
return err
}
- return sess.Commit()
+ return committer.Commit()
}
func checkKeyEmails(email string, keys ...*GPGKey) (bool, string) {