1
0
Fork 0
forked from forgejo/forgejo

Reduce usage of db.DefaultContext (#27073)

Part of #27065

This reduces the usage of `db.DefaultContext`. I think I've got enough
files for the first PR. When this is merged, I will continue working on
this.

Considering how many files this PR affect, I hope it won't take to long
to merge, so I don't end up in the merge conflict hell.

---------

Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
JakobDev 2023-09-14 19:09:32 +02:00 committed by GitHub
parent 0de09d3afc
commit 76659b1114
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
83 changed files with 339 additions and 324 deletions

View file

@ -4,12 +4,12 @@
package ldap
import (
"context"
"fmt"
"strings"
asymkey_model "code.gitea.io/gitea/models/asymkey"
"code.gitea.io/gitea/models/auth"
"code.gitea.io/gitea/models/db"
user_model "code.gitea.io/gitea/models/user"
auth_module "code.gitea.io/gitea/modules/auth"
"code.gitea.io/gitea/modules/util"
@ -19,7 +19,7 @@ import (
// Authenticate queries if login/password is valid against the LDAP directory pool,
// and create a local user if success when enabled.
func (source *Source) Authenticate(user *user_model.User, userName, password string) (*user_model.User, error) {
func (source *Source) Authenticate(ctx context.Context, user *user_model.User, userName, password string) (*user_model.User, error) {
loginName := userName
if user != nil {
loginName = user.LoginName
@ -33,11 +33,11 @@ func (source *Source) Authenticate(user *user_model.User, userName, password str
isAttributeSSHPublicKeySet := len(strings.TrimSpace(source.AttributeSSHPublicKey)) > 0
// Update User admin flag if exist
if isExist, err := user_model.IsUserExist(db.DefaultContext, 0, sr.Username); err != nil {
if isExist, err := user_model.IsUserExist(ctx, 0, sr.Username); err != nil {
return nil, err
} else if isExist {
if user == nil {
user, err = user_model.GetUserByName(db.DefaultContext, sr.Username)
user, err = user_model.GetUserByName(ctx, sr.Username)
if err != nil {
return nil, err
}
@ -55,7 +55,7 @@ func (source *Source) Authenticate(user *user_model.User, userName, password str
cols = append(cols, "is_restricted")
}
if len(cols) > 0 {
err = user_model.UpdateUserCols(db.DefaultContext, user, cols...)
err = user_model.UpdateUserCols(ctx, user, cols...)
if err != nil {
return nil, err
}
@ -94,7 +94,7 @@ func (source *Source) Authenticate(user *user_model.User, userName, password str
IsActive: util.OptionalBoolTrue,
}
err := user_model.CreateUser(user, overwriteDefault)
err := user_model.CreateUser(ctx, user, overwriteDefault)
if err != nil {
return user, err
}
@ -116,7 +116,7 @@ func (source *Source) Authenticate(user *user_model.User, userName, password str
if err != nil {
return user, err
}
if err := source_service.SyncGroupsToTeams(db.DefaultContext, user, sr.Groups, groupTeamMapping, source.GroupTeamMapRemoval); err != nil {
if err := source_service.SyncGroupsToTeams(ctx, user, sr.Groups, groupTeamMapping, source.GroupTeamMapRemoval); err != nil {
return user, err
}
}