Browse Source

Finish register user

pull/31/head
Unknown 11 years ago
parent
commit
94311e187f
  1. 9
      conf/app.ini
  2. 45
      models/models.go
  3. 7
      models/user.go
  4. 8
      utils/log/log.go

9
conf/app.ini

@ -2,4 +2,11 @@ APP_NAME = Gogs - Go Git Service
[server]
HTTP_ADDR =
HTTP_PORT = 3000
HTTP_PORT = 3000
[database]
DB_TYPE = mysql
HOST =
NAME = gogs
USER = root
PASSWD = root

45
models/models.go

@ -4,7 +4,16 @@
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 (
orm *xorm.Engine
@ -30,3 +39,37 @@ type PullRequest struct {
type Comment struct {
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))
}

7
models/user.go

@ -97,6 +97,7 @@ func RegisterUser(user *User) (err error) {
user.Avatar = utils.EncodeMd5(user.Email)
user.Created = time.Now()
user.Updated = time.Now()
user.EncodePasswd()
_, err = orm.Insert(user)
return err
}
@ -116,7 +117,7 @@ func DeleteUser(user *User) error {
}
// 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)
user.Passwd = fmt.Sprintf("%x", newPasswd)
return err
@ -124,8 +125,8 @@ func (user *User) EncodePasswd(pass string) error {
// LoginUserPlain validates user by raw user name and password.
func LoginUserPlain(name, passwd string) (*User, error) {
user := User{Name: name}
if err := user.EncodePasswd(passwd); err != nil {
user := User{Name: name, Passwd: passwd}
if err := user.EncodePasswd(); err != nil {
return nil, err
}

8
utils/log/log.go

@ -16,6 +16,10 @@ func init() {
logger.SetLogger("console", "")
}
func Trace(format string, v ...interface{}) {
logger.Trace(format, v...)
}
func Info(format string, v ...interface{}) {
logger.Info(format, v...)
}
@ -27,3 +31,7 @@ func Error(format string, v ...interface{}) {
func Warn(format string, v ...interface{}) {
logger.Warn(format, v...)
}
func Critical(format string, v ...interface{}) {
logger.Critical(format, v...)
}

Loading…
Cancel
Save