Sfoglia il codice sorgente

[db] Move custom functions to GIN file

internal/db/user.go file contains no custom function definitions now.
Achilleas Koutsou 4 anni fa
parent
commit
aa1c5beecf
2 ha cambiato i file con 37 aggiunte e 36 eliminazioni
  1. 37 3
      internal/db/db_gin.go
  2. 0 33
      internal/db/user.go

+ 37 - 3
internal/db/db_gin.go

@@ -1,6 +1,7 @@
 package db
 
 import (
+	"bufio"
 	"encoding/json"
 	"fmt"
 	"net/http"
@@ -8,10 +9,12 @@ import (
 	"path/filepath"
 	"strings"
 
+	"github.com/G-Node/git-module"
 	"github.com/G-Node/gogs/internal/conf"
 	"github.com/G-Node/libgin/libgin"
 	"github.com/G-Node/libgin/libgin/annex"
-	"github.com/gogs/git-module"
+	"github.com/unknwon/com"
+	"golang.org/x/crypto/bcrypt"
 	log "gopkg.in/clog.v1"
 )
 
@@ -150,9 +153,9 @@ func annexSync(path string) error {
 func annexAdd(repoPath string, all bool, files ...string) error {
 	cmd := git.NewCommand("annex", "add")
 	if all {
-		cmd.AddArgs(".")
+		cmd.AddArguments(".")
 	}
-	_, err := cmd.AddArgs(files...).RunInDir(repoPath)
+	_, err := cmd.AddArguments(files...).RunInDir(repoPath)
 	return err
 }
 
@@ -169,3 +172,34 @@ func annexUpload(repoPath, remote string) error {
 	}
 	return nil
 }
+
+func IsBlockedDomain(email string) bool {
+	// fpath := path.Join(setting.CustomPath, "blocklist")
+	fpath := filepath.Join(conf.CustomDir(), "blocklist")
+	if !com.IsExist(fpath) {
+		return false
+	}
+
+	f, err := os.Open(fpath)
+	if err != nil {
+		log.Error(2, "Failed to open file %q: %v", fpath, err)
+		return false
+	}
+	defer f.Close()
+
+	scanner := bufio.NewScanner(f)
+	for scanner.Scan() {
+		// Check provided email address against each line as suffix
+		if strings.HasSuffix(email, scanner.Text()) {
+			log.Trace("New user email matched blocked domain: %q", email)
+			return true
+		}
+	}
+
+	return false
+}
+
+func (u *User) OldGinVerifyPassword(plain string) bool {
+	err := bcrypt.CompareHashAndPassword([]byte(u.Passwd), []byte(plain))
+	return err == nil
+}

+ 0 - 33
internal/db/user.go

@@ -5,7 +5,6 @@
 package db
 
 import (
-	"bufio"
 	"bytes"
 	"crypto/sha256"
 	"crypto/subtle"
@@ -15,7 +14,6 @@ import (
 	_ "image/jpeg"
 	"image/png"
 	"os"
-	"path"
 	"path/filepath"
 	"strings"
 	"time"
@@ -36,7 +34,6 @@ import (
 	"github.com/G-Node/gogs/internal/errutil"
 	"github.com/G-Node/gogs/internal/strutil"
 	"github.com/G-Node/gogs/internal/tool"
-	"golang.org/x/crypto/bcrypt"
 )
 
 // USER_AVATAR_URL_PREFIX is used to identify a URL is to access user avatar.
@@ -329,11 +326,6 @@ func (u *User) EncodePassword() {
 	u.Passwd = fmt.Sprintf("%x", newPasswd)
 }
 
-func (u *User) OldGinVerifyPassword(plain string) bool {
-	err := bcrypt.CompareHashAndPassword([]byte(u.Passwd), []byte(plain))
-	return err == nil
-}
-
 // ValidatePassword checks if given password matches the one belongs to the user.
 func (u *User) ValidatePassword(passwd string) bool {
 	if u.OldGinVerifyPassword(passwd) {
@@ -494,31 +486,6 @@ func IsUserExist(uid int64, name string) (bool, error) {
 	return x.Where("id != ?", uid).Get(&User{LowerName: strings.ToLower(name)})
 }
 
-func IsBlockedDomain(email string) bool {
-	fpath := path.Join(conf.CustomDir(), "blocklist")
-	if !com.IsExist(fpath) {
-		return false
-	}
-
-	f, err := os.Open(fpath)
-	if err != nil {
-		log.Error("Failed to open file %q: %v", fpath, err)
-		return false
-	}
-	defer f.Close()
-
-	scanner := bufio.NewScanner(f)
-	for scanner.Scan() {
-		// Check provided email address against each line as suffix
-		if strings.HasSuffix(email, scanner.Text()) {
-			log.Trace("New user email matched blocked domain: %q", email)
-			return true
-		}
-	}
-
-	return false
-}
-
 // GetUserSalt returns a ramdom user salt token.
 func GetUserSalt() (string, error) {
 	return strutil.RandomChars(10)