Finish register user
This commit is contained in:
parent
b455478df8
commit
94311e187f
4 changed files with 64 additions and 5 deletions
|
@ -2,4 +2,11 @@ APP_NAME = Gogs - Go Git Service
|
||||||
|
|
||||||
[server]
|
[server]
|
||||||
HTTP_ADDR =
|
HTTP_ADDR =
|
||||||
HTTP_PORT = 3000
|
HTTP_PORT = 3000
|
||||||
|
|
||||||
|
[database]
|
||||||
|
DB_TYPE = mysql
|
||||||
|
HOST =
|
||||||
|
NAME = gogs
|
||||||
|
USER = root
|
||||||
|
PASSWD = root
|
|
@ -4,7 +4,16 @@
|
||||||
|
|
||||||
package models
|
package models
|
||||||
|
|
||||||
import "github.com/lunny/xorm"
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
_ "github.com/go-sql-driver/mysql"
|
||||||
|
"github.com/lunny/xorm"
|
||||||
|
|
||||||
|
"github.com/gogits/gogs/utils"
|
||||||
|
"github.com/gogits/gogs/utils/log"
|
||||||
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
orm *xorm.Engine
|
orm *xorm.Engine
|
||||||
|
@ -30,3 +39,37 @@ type PullRequest struct {
|
||||||
type Comment struct {
|
type Comment struct {
|
||||||
Id int64
|
Id int64
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func setEngine() {
|
||||||
|
dbType := utils.Cfg.MustValue("database", "DB_TYPE")
|
||||||
|
dbHost := utils.Cfg.MustValue("database", "HOST")
|
||||||
|
dbName := utils.Cfg.MustValue("database", "NAME")
|
||||||
|
dbUser := utils.Cfg.MustValue("database", "USER")
|
||||||
|
dbPwd := utils.Cfg.MustValue("database", "PASSWD")
|
||||||
|
|
||||||
|
var err error
|
||||||
|
switch dbType {
|
||||||
|
case "mysql":
|
||||||
|
orm, err = xorm.NewEngine("mysql", fmt.Sprintf("%v:%v@%v/%v?charset=utf8",
|
||||||
|
dbUser, dbPwd, dbHost, dbName))
|
||||||
|
default:
|
||||||
|
log.Critical("Unknown database type: %s", dbType)
|
||||||
|
os.Exit(2)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Critical("models.init -> Conntect database: %s", dbType)
|
||||||
|
os.Exit(2)
|
||||||
|
}
|
||||||
|
|
||||||
|
//x.ShowDebug = true
|
||||||
|
orm.ShowErr = true
|
||||||
|
//x.ShowSQL = true
|
||||||
|
|
||||||
|
log.Trace("Initialized database -> %s", dbName)
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
setEngine()
|
||||||
|
orm.Sync(new(User))
|
||||||
|
}
|
||||||
|
|
|
@ -97,6 +97,7 @@ func RegisterUser(user *User) (err error) {
|
||||||
user.Avatar = utils.EncodeMd5(user.Email)
|
user.Avatar = utils.EncodeMd5(user.Email)
|
||||||
user.Created = time.Now()
|
user.Created = time.Now()
|
||||||
user.Updated = time.Now()
|
user.Updated = time.Now()
|
||||||
|
user.EncodePasswd()
|
||||||
_, err = orm.Insert(user)
|
_, err = orm.Insert(user)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -116,7 +117,7 @@ func DeleteUser(user *User) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// EncodePasswd encodes password to safe format.
|
// EncodePasswd encodes password to safe format.
|
||||||
func (user *User) EncodePasswd(pass string) error {
|
func (user *User) EncodePasswd() error {
|
||||||
newPasswd, err := scrypt.Key([]byte(user.Passwd), []byte("!#@FDEWREWR&*("), 16384, 8, 1, 64)
|
newPasswd, err := scrypt.Key([]byte(user.Passwd), []byte("!#@FDEWREWR&*("), 16384, 8, 1, 64)
|
||||||
user.Passwd = fmt.Sprintf("%x", newPasswd)
|
user.Passwd = fmt.Sprintf("%x", newPasswd)
|
||||||
return err
|
return err
|
||||||
|
@ -124,8 +125,8 @@ func (user *User) EncodePasswd(pass string) error {
|
||||||
|
|
||||||
// LoginUserPlain validates user by raw user name and password.
|
// LoginUserPlain validates user by raw user name and password.
|
||||||
func LoginUserPlain(name, passwd string) (*User, error) {
|
func LoginUserPlain(name, passwd string) (*User, error) {
|
||||||
user := User{Name: name}
|
user := User{Name: name, Passwd: passwd}
|
||||||
if err := user.EncodePasswd(passwd); err != nil {
|
if err := user.EncodePasswd(); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -16,6 +16,10 @@ func init() {
|
||||||
logger.SetLogger("console", "")
|
logger.SetLogger("console", "")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Trace(format string, v ...interface{}) {
|
||||||
|
logger.Trace(format, v...)
|
||||||
|
}
|
||||||
|
|
||||||
func Info(format string, v ...interface{}) {
|
func Info(format string, v ...interface{}) {
|
||||||
logger.Info(format, v...)
|
logger.Info(format, v...)
|
||||||
}
|
}
|
||||||
|
@ -27,3 +31,7 @@ func Error(format string, v ...interface{}) {
|
||||||
func Warn(format string, v ...interface{}) {
|
func Warn(format string, v ...interface{}) {
|
||||||
logger.Warn(format, v...)
|
logger.Warn(format, v...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Critical(format string, v ...interface{}) {
|
||||||
|
logger.Critical(format, v...)
|
||||||
|
}
|
||||||
|
|
Reference in a new issue