1
1
mirror of https://github.com/go-gitea/gitea synced 2025-07-22 18:28:37 +00:00

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>
This commit is contained in:
Lunny Xiao
2021-09-19 19:49:59 +08:00
committed by GitHub
parent 462306e263
commit a4bfef265d
335 changed files with 4191 additions and 3654 deletions

View File

@@ -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 {