Move almost all functions' parameter db.Engine to context.Context (#19748)
* Move almost all functions' parameter db.Engine to context.Context * remove some unnecessary wrap functions
This commit is contained in:
parent
d81e31ad78
commit
fd7d83ace6
232 changed files with 1463 additions and 2108 deletions
|
@ -75,7 +75,7 @@ func (key *PublicKey) AuthorizedString() string {
|
|||
return AuthorizedStringForKey(key)
|
||||
}
|
||||
|
||||
func addKey(e db.Engine, key *PublicKey) (err error) {
|
||||
func addKey(ctx context.Context, key *PublicKey) (err error) {
|
||||
if len(key.Fingerprint) == 0 {
|
||||
key.Fingerprint, err = calcFingerprint(key.Content)
|
||||
if err != nil {
|
||||
|
@ -84,7 +84,7 @@ func addKey(e db.Engine, key *PublicKey) (err error) {
|
|||
}
|
||||
|
||||
// Save SSH key.
|
||||
if _, err = e.Insert(key); err != nil {
|
||||
if err = db.Insert(ctx, key); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -105,14 +105,13 @@ func AddPublicKey(ownerID int64, name, content string, authSourceID int64) (*Pub
|
|||
return nil, err
|
||||
}
|
||||
defer committer.Close()
|
||||
sess := db.GetEngine(ctx)
|
||||
|
||||
if err := checkKeyFingerprint(sess, fingerprint); err != nil {
|
||||
if err := checkKeyFingerprint(ctx, fingerprint); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Key name of same user cannot be duplicated.
|
||||
has, err := sess.
|
||||
has, err := db.GetEngine(ctx).
|
||||
Where("owner_id = ? AND name = ?", ownerID, name).
|
||||
Get(new(PublicKey))
|
||||
if err != nil {
|
||||
|
@ -130,7 +129,7 @@ func AddPublicKey(ownerID int64, name, content string, authSourceID int64) (*Pub
|
|||
Type: KeyTypeUser,
|
||||
LoginSourceID: authSourceID,
|
||||
}
|
||||
if err = addKey(sess, key); err != nil {
|
||||
if err = addKey(ctx, key); err != nil {
|
||||
return nil, fmt.Errorf("addKey: %v", err)
|
||||
}
|
||||
|
||||
|
@ -151,29 +150,12 @@ func GetPublicKeyByID(keyID int64) (*PublicKey, error) {
|
|||
return key, nil
|
||||
}
|
||||
|
||||
func searchPublicKeyByContentWithEngine(e db.Engine, content string) (*PublicKey, error) {
|
||||
key := new(PublicKey)
|
||||
has, err := e.
|
||||
Where("content like ?", content+"%").
|
||||
Get(key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else if !has {
|
||||
return nil, ErrKeyNotExist{}
|
||||
}
|
||||
return key, nil
|
||||
}
|
||||
|
||||
// SearchPublicKeyByContent searches content as prefix (leak e-mail part)
|
||||
// and returns public key found.
|
||||
func SearchPublicKeyByContent(content string) (*PublicKey, error) {
|
||||
return searchPublicKeyByContentWithEngine(db.GetEngine(db.DefaultContext), content)
|
||||
}
|
||||
|
||||
func searchPublicKeyByContentExactWithEngine(e db.Engine, content string) (*PublicKey, error) {
|
||||
func SearchPublicKeyByContent(ctx context.Context, content string) (*PublicKey, error) {
|
||||
key := new(PublicKey)
|
||||
has, err := e.
|
||||
Where("content = ?", content).
|
||||
has, err := db.GetEngine(ctx).
|
||||
Where("content like ?", content+"%").
|
||||
Get(key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -185,8 +167,17 @@ func searchPublicKeyByContentExactWithEngine(e db.Engine, content string) (*Publ
|
|||
|
||||
// SearchPublicKeyByContentExact searches content
|
||||
// and returns public key found.
|
||||
func SearchPublicKeyByContentExact(content string) (*PublicKey, error) {
|
||||
return searchPublicKeyByContentExactWithEngine(db.GetEngine(db.DefaultContext), content)
|
||||
func SearchPublicKeyByContentExact(ctx context.Context, content string) (*PublicKey, error) {
|
||||
key := new(PublicKey)
|
||||
has, err := db.GetEngine(ctx).
|
||||
Where("content = ?", content).
|
||||
Get(key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else if !has {
|
||||
return nil, ErrKeyNotExist{}
|
||||
}
|
||||
return key, nil
|
||||
}
|
||||
|
||||
// SearchPublicKey returns a list of public keys matching the provided arguments.
|
||||
|
@ -335,12 +326,11 @@ func deleteKeysMarkedForDeletion(keys []string) (bool, error) {
|
|||
return false, err
|
||||
}
|
||||
defer committer.Close()
|
||||
sess := db.GetEngine(ctx)
|
||||
|
||||
// Delete keys marked for deletion
|
||||
var sshKeysNeedUpdate bool
|
||||
for _, KeyToDelete := range keys {
|
||||
key, err := searchPublicKeyByContentWithEngine(sess, KeyToDelete)
|
||||
key, err := SearchPublicKeyByContent(ctx, KeyToDelete)
|
||||
if err != nil {
|
||||
log.Error("SearchPublicKeyByContent: %v", err)
|
||||
continue
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue