summaryrefslogtreecommitdiffstats
path: root/models/twofactor.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/twofactor.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/twofactor.go')
-rw-r--r--models/twofactor.go13
1 files changed, 9 insertions, 4 deletions
diff --git a/models/twofactor.go b/models/twofactor.go
index c19c5d120f..f2443e04ce 100644
--- a/models/twofactor.go
+++ b/models/twofactor.go
@@ -11,6 +11,7 @@ import (
"encoding/base64"
"fmt"
+ "code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/secret"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/timeutil"
@@ -32,6 +33,10 @@ type TwoFactor struct {
UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
}
+func init() {
+ db.RegisterModel(new(TwoFactor))
+}
+
// GenerateScratchToken recreates the scratch token the user is using.
func (t *TwoFactor) GenerateScratchToken() (string, error) {
token, err := util.RandomString(8)
@@ -88,13 +93,13 @@ func (t *TwoFactor) ValidateTOTP(passcode string) (bool, error) {
// NewTwoFactor creates a new two-factor authentication token.
func NewTwoFactor(t *TwoFactor) error {
- _, err := x.Insert(t)
+ _, err := db.DefaultContext().Engine().Insert(t)
return err
}
// UpdateTwoFactor updates a two-factor authentication token.
func UpdateTwoFactor(t *TwoFactor) error {
- _, err := x.ID(t.ID).AllCols().Update(t)
+ _, err := db.DefaultContext().Engine().ID(t.ID).AllCols().Update(t)
return err
}
@@ -102,7 +107,7 @@ func UpdateTwoFactor(t *TwoFactor) error {
// the user, if any.
func GetTwoFactorByUID(uid int64) (*TwoFactor, error) {
twofa := &TwoFactor{}
- has, err := x.Where("uid=?", uid).Get(twofa)
+ has, err := db.DefaultContext().Engine().Where("uid=?", uid).Get(twofa)
if err != nil {
return nil, err
} else if !has {
@@ -113,7 +118,7 @@ func GetTwoFactorByUID(uid int64) (*TwoFactor, error) {
// DeleteTwoFactorByID deletes two-factor authentication token by given ID.
func DeleteTwoFactorByID(id, userID int64) error {
- cnt, err := x.ID(id).Delete(&TwoFactor{
+ cnt, err := db.DefaultContext().Engine().ID(id).Delete(&TwoFactor{
UID: userID,
})
if err != nil {