diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2021-09-19 19:49:59 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-19 19:49:59 +0800 |
commit | a4bfef265d9e512830350635a0489c2cdcd6508f (patch) | |
tree | 1e3c2ec94276dfcb2f8ba73a2ac075ba39c4a34a /models/user_openid.go | |
parent | 462306e263db5a809dbe2cdf62e99307aeff28de (diff) | |
download | gitea-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/user_openid.go')
-rw-r--r-- | models/user_openid.go | 21 |
1 files changed, 13 insertions, 8 deletions
diff --git a/models/user_openid.go b/models/user_openid.go index 597f19d77d..4844b68c29 100644 --- a/models/user_openid.go +++ b/models/user_openid.go @@ -7,6 +7,7 @@ package models import ( "errors" + "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/auth/openid" "code.gitea.io/gitea/modules/log" ) @@ -22,10 +23,14 @@ type UserOpenID struct { Show bool `xorm:"DEFAULT false"` } +func init() { + db.RegisterModel(new(UserOpenID)) +} + // GetUserOpenIDs returns all openid addresses that belongs to given user. func GetUserOpenIDs(uid int64) ([]*UserOpenID, error) { openids := make([]*UserOpenID, 0, 5) - if err := x. + if err := db.DefaultContext().Engine(). Where("uid=?", uid). Asc("id"). Find(&openids); err != nil { @@ -36,7 +41,7 @@ func GetUserOpenIDs(uid int64) ([]*UserOpenID, error) { } // isOpenIDUsed returns true if the openid has been used. -func isOpenIDUsed(e Engine, uri string) (bool, error) { +func isOpenIDUsed(e db.Engine, uri string) (bool, error) { if len(uri) == 0 { return true, nil } @@ -45,7 +50,7 @@ func isOpenIDUsed(e Engine, uri string) (bool, error) { } // NOTE: make sure openid.URI is normalized already -func addUserOpenID(e Engine, openid *UserOpenID) error { +func addUserOpenID(e db.Engine, openid *UserOpenID) error { used, err := isOpenIDUsed(e, openid.URI) if err != nil { return err @@ -59,7 +64,7 @@ func addUserOpenID(e Engine, openid *UserOpenID) error { // AddUserOpenID adds an pre-verified/normalized OpenID URI to given user. func AddUserOpenID(openid *UserOpenID) error { - return addUserOpenID(x, openid) + return addUserOpenID(db.DefaultContext().Engine(), openid) } // DeleteUserOpenID deletes an openid address of given user. @@ -70,9 +75,9 @@ func DeleteUserOpenID(openid *UserOpenID) (err error) { UID: openid.UID, } if openid.ID > 0 { - deleted, err = x.ID(openid.ID).Delete(&address) + deleted, err = db.DefaultContext().Engine().ID(openid.ID).Delete(&address) } else { - deleted, err = x. + deleted, err = db.DefaultContext().Engine(). Where("openid=?", openid.URI). Delete(&address) } @@ -87,7 +92,7 @@ func DeleteUserOpenID(openid *UserOpenID) (err error) { // ToggleUserOpenIDVisibility toggles visibility of an openid address of given user. func ToggleUserOpenIDVisibility(id int64) (err error) { - _, err = x.Exec("update `user_open_id` set `show` = not `show` where `id` = ?", id) + _, err = db.DefaultContext().Engine().Exec("update `user_open_id` set `show` = not `show` where `id` = ?", id) return err } @@ -106,7 +111,7 @@ func GetUserByOpenID(uri string) (*User, error) { // Otherwise, check in openid table oid := &UserOpenID{} - has, err := x.Where("uri=?", uri).Get(oid) + has, err := db.DefaultContext().Engine().Where("uri=?", uri).Get(oid) if err != nil { return nil, err } |