summaryrefslogtreecommitdiffstats
path: root/models/external_login_user.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/external_login_user.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/external_login_user.go')
-rw-r--r--models/external_login_user.go25
1 files changed, 15 insertions, 10 deletions
diff --git a/models/external_login_user.go b/models/external_login_user.go
index aa5da8134a..5bfdad6993 100644
--- a/models/external_login_user.go
+++ b/models/external_login_user.go
@@ -7,6 +7,7 @@ package models
import (
"time"
+ "code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/structs"
"github.com/markbates/goth"
@@ -34,15 +35,19 @@ type ExternalLoginUser struct {
ExpiresAt time.Time
}
+func init() {
+ db.RegisterModel(new(ExternalLoginUser))
+}
+
// GetExternalLogin checks if a externalID in loginSourceID scope already exists
func GetExternalLogin(externalLoginUser *ExternalLoginUser) (bool, error) {
- return x.Get(externalLoginUser)
+ return db.DefaultContext().Engine().Get(externalLoginUser)
}
// ListAccountLinks returns a map with the ExternalLoginUser and its LoginSource
func ListAccountLinks(user *User) ([]*ExternalLoginUser, error) {
externalAccounts := make([]*ExternalLoginUser, 0, 5)
- err := x.Where("user_id=?", user.ID).
+ err := db.DefaultContext().Engine().Where("user_id=?", user.ID).
Desc("login_source_id").
Find(&externalAccounts)
if err != nil {
@@ -54,7 +59,7 @@ func ListAccountLinks(user *User) ([]*ExternalLoginUser, error) {
// LinkExternalToUser link the external user to the user
func LinkExternalToUser(user *User, externalLoginUser *ExternalLoginUser) error {
- has, err := x.Where("external_id=? AND login_source_id=?", externalLoginUser.ExternalID, externalLoginUser.LoginSourceID).
+ has, err := db.DefaultContext().Engine().Where("external_id=? AND login_source_id=?", externalLoginUser.ExternalID, externalLoginUser.LoginSourceID).
NoAutoCondition().
Exist(externalLoginUser)
if err != nil {
@@ -63,13 +68,13 @@ func LinkExternalToUser(user *User, externalLoginUser *ExternalLoginUser) error
return ErrExternalLoginUserAlreadyExist{externalLoginUser.ExternalID, user.ID, externalLoginUser.LoginSourceID}
}
- _, err = x.Insert(externalLoginUser)
+ _, err = db.DefaultContext().Engine().Insert(externalLoginUser)
return err
}
// RemoveAccountLink will remove all external login sources for the given user
func RemoveAccountLink(user *User, loginSourceID int64) (int64, error) {
- deleted, err := x.Delete(&ExternalLoginUser{UserID: user.ID, LoginSourceID: loginSourceID})
+ deleted, err := db.DefaultContext().Engine().Delete(&ExternalLoginUser{UserID: user.ID, LoginSourceID: loginSourceID})
if err != nil {
return deleted, err
}
@@ -80,7 +85,7 @@ func RemoveAccountLink(user *User, loginSourceID int64) (int64, error) {
}
// removeAllAccountLinks will remove all external login sources for the given user
-func removeAllAccountLinks(e Engine, user *User) error {
+func removeAllAccountLinks(e db.Engine, user *User) error {
_, err := e.Delete(&ExternalLoginUser{UserID: user.ID})
return err
}
@@ -88,7 +93,7 @@ func removeAllAccountLinks(e Engine, user *User) error {
// GetUserIDByExternalUserID get user id according to provider and userID
func GetUserIDByExternalUserID(provider, userID string) (int64, error) {
var id int64
- _, err := x.Table("external_login_user").
+ _, err := db.DefaultContext().Engine().Table("external_login_user").
Select("user_id").
Where("provider=?", provider).
And("external_id=?", userID).
@@ -125,7 +130,7 @@ func UpdateExternalUser(user *User, gothUser goth.User) error {
ExpiresAt: gothUser.ExpiresAt,
}
- has, err := x.Where("external_id=? AND login_source_id=?", gothUser.UserID, loginSource.ID).
+ has, err := db.DefaultContext().Engine().Where("external_id=? AND login_source_id=?", gothUser.UserID, loginSource.ID).
NoAutoCondition().
Exist(externalLoginUser)
if err != nil {
@@ -134,7 +139,7 @@ func UpdateExternalUser(user *User, gothUser goth.User) error {
return ErrExternalLoginUserNotExist{user.ID, loginSource.ID}
}
- _, err = x.Where("external_id=? AND login_source_id=?", gothUser.UserID, loginSource.ID).AllCols().Update(externalLoginUser)
+ _, err = db.DefaultContext().Engine().Where("external_id=? AND login_source_id=?", gothUser.UserID, loginSource.ID).AllCols().Update(externalLoginUser)
return err
}
@@ -156,7 +161,7 @@ func (opts FindExternalUserOptions) toConds() builder.Cond {
// FindExternalUsersByProvider represents external users via provider
func FindExternalUsersByProvider(opts FindExternalUserOptions) ([]ExternalLoginUser, error) {
var users []ExternalLoginUser
- err := x.Where(opts.toConds()).
+ err := db.DefaultContext().Engine().Where(opts.toConds()).
Limit(opts.Limit, opts.Start).
OrderBy("login_source_id ASC, external_id ASC").
Find(&users)