diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2021-11-21 23:41:00 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-21 23:41:00 +0800 |
commit | d710af6669654f27f02b69d7ef1ba563e7d58a90 (patch) | |
tree | 9727f468a570106293dc90beb70035180bbb7e8e /models/user.go | |
parent | 0add627182388ac63fd04b94cdf912fb87fd0326 (diff) | |
download | gitea-d710af6669654f27f02b69d7ef1ba563e7d58a90.tar.gz gitea-d710af6669654f27f02b69d7ef1ba563e7d58a90.zip |
Remove NewSession method from db.Engine interface (#17577)
* Remove NewSession method from db.Engine interface
* Fix bug
* Some improvements
* Fix bug
* Fix test
* Use XXXBean instead of XXXExample
Diffstat (limited to 'models/user.go')
-rw-r--r-- | models/user.go | 38 |
1 files changed, 21 insertions, 17 deletions
diff --git a/models/user.go b/models/user.go index db2345cb37..4231597d42 100644 --- a/models/user.go +++ b/models/user.go @@ -1105,11 +1105,13 @@ func updateUserCols(e db.Engine, u *User, cols ...string) error { // UpdateUserSetting updates user's settings. func UpdateUserSetting(u *User) (err error) { - sess := db.NewSession(db.DefaultContext) - defer sess.Close() - if err = sess.Begin(); err != nil { + ctx, committer, err := db.TxContext() + if err != nil { return err } + defer committer.Close() + sess := db.GetEngine(ctx) + if !u.IsOrganization() { if err = checkDupEmail(sess, u); err != nil { return err @@ -1118,7 +1120,7 @@ func UpdateUserSetting(u *User) (err error) { if err = updateUser(sess, u); err != nil { return err } - return sess.Commit() + return committer.Commit() } // deleteBeans deletes all given beans, beans should contain delete conditions. @@ -1533,7 +1535,7 @@ type SearchUserOptions struct { IsProhibitLogin util.OptionalBool } -func (opts *SearchUserOptions) toSearchQueryBase() (sess *xorm.Session) { +func (opts *SearchUserOptions) toSearchQueryBase() *xorm.Session { var cond builder.Cond = builder.Eq{"type": opts.Type} if len(opts.Keyword) > 0 { lowerKeyword := strings.ToLower(opts.Keyword) @@ -1599,19 +1601,21 @@ func (opts *SearchUserOptions) toSearchQueryBase() (sess *xorm.Session) { cond = cond.And(builder.Eq{"prohibit_login": opts.IsProhibitLogin.IsTrue()}) } - sess = db.NewSession(db.DefaultContext) - if !opts.IsTwoFactorEnabled.IsNone() { - // 2fa filter uses LEFT JOIN to check whether a user has a 2fa record - // TODO: bad performance here, maybe there will be a column "is_2fa_enabled" in the future - if opts.IsTwoFactorEnabled.IsTrue() { - cond = cond.And(builder.Expr("two_factor.uid IS NOT NULL")) - } else { - cond = cond.And(builder.Expr("two_factor.uid IS NULL")) - } - sess = sess.Join("LEFT OUTER", "two_factor", "two_factor.uid = `user`.id") + e := db.GetEngine(db.DefaultContext) + if opts.IsTwoFactorEnabled.IsNone() { + return e.Where(cond) } - sess = sess.Where(cond) - return sess + + // 2fa filter uses LEFT JOIN to check whether a user has a 2fa record + // TODO: bad performance here, maybe there will be a column "is_2fa_enabled" in the future + if opts.IsTwoFactorEnabled.IsTrue() { + cond = cond.And(builder.Expr("two_factor.uid IS NOT NULL")) + } else { + cond = cond.And(builder.Expr("two_factor.uid IS NULL")) + } + + return e.Join("LEFT OUTER", "two_factor", "two_factor.uid = `user`.id"). + Where(cond) } // SearchUsers takes options i.e. keyword and part of user name to search, |