diff --git a/gogs.go b/gogs.go index 6cb3f37ed..140ee6518 100644 --- a/gogs.go +++ b/gogs.go @@ -17,7 +17,7 @@ import ( "github.com/gogits/gogs/utils/log" ) -const APP_VER = "0.0.0.0217" +const APP_VER = "0.0.0.0218" func init() { diff --git a/models/user.go b/models/user.go index 44dadb9a5..ec88edde1 100644 --- a/models/user.go +++ b/models/user.go @@ -11,6 +11,8 @@ import ( "time" "github.com/dchest/scrypt" + + "github.com/gogits/gogs/utils" ) // User types. @@ -29,9 +31,9 @@ const ( type User struct { Id int64 LowerName string `xorm:"unique not null"` - Name string `xorm:"unique not null"` - Email string `xorm:"unique not null"` - Passwd string `xorm:"not null"` + Name string `xorm:"unique not null" valid:"Required"` + Email string `xorm:"unique not null" valid:"Email"` + Passwd string `xorm:"not null" valid:"MinSize(8)"` LoginType int Type int NumFollowers int @@ -82,24 +84,17 @@ func IsUserExist(name string) (bool, error) { return orm.Get(&User{LowerName: strings.ToLower(name)}) } -// validateUser checks if user exist. -func validateUser(name string) error { - isExist, err := IsUserExist(name) +// RegisterUser creates record of a new user. +func RegisterUser(user *User) (err error) { + isExist, err := IsUserExist(user.Name) if err != nil { return err } else if isExist { return ErrUserAlreadyExist } - return nil -} -// RegisterUser creates record of a new user. -func RegisterUser(user *User) (err error) { - if err = validateUser(user.Name); err != nil { - return err - } user.LowerName = strings.ToLower(user.Name) - // TODO: generate Avatar address. + user.Avatar = utils.EncodeMd5(user.Email) user.Created = time.Now() user.Updated = time.Now() _, err = orm.Insert(user) diff --git a/routers/user/user.go b/routers/user/user.go index f9dc07f26..24c22a4f4 100644 --- a/routers/user/user.go +++ b/routers/user/user.go @@ -10,8 +10,10 @@ import ( "github.com/martini-contrib/render" - //"github.com/gogits/gogs/utils/log" + "github.com/gogits/validation" + "github.com/gogits/gogs/models" + "github.com/gogits/gogs/utils/log" ) func SignIn(r render.Render) { @@ -26,12 +28,25 @@ func SignUp(req *http.Request, r render.Render) { return } - // TODO: validate form. - err := models.RegisterUser(&models.User{ + u := &models.User{ Name: req.FormValue("username"), Email: req.FormValue("email"), Passwd: req.FormValue("passwd"), - }) + } + valid := validation.Validation{} + ok, err := valid.Valid(u) + if err != nil { + log.Error("user.SignUp -> valid user: %v", err) + return + } + if !ok { + for _, err := range valid.Errors { + log.Warn("user.SignUp -> valid user: %v", err) + } + return + } + + err = models.RegisterUser(u) r.HTML(403, "status/403", map[string]interface{}{ "Title": fmt.Sprintf("%v", err), }) diff --git a/utils/log/log.go b/utils/log/log.go index c725c5276..e5c0b21c7 100644 --- a/utils/log/log.go +++ b/utils/log/log.go @@ -19,3 +19,11 @@ func init() { func Info(format string, v ...interface{}) { logger.Info(format, v...) } + +func Error(format string, v ...interface{}) { + logger.Error(format, v...) +} + +func Warn(format string, v ...interface{}) { + logger.Warn(format, v...) +} diff --git a/utils/tool.go b/utils/tool.go new file mode 100644 index 000000000..1d2db4bcf --- /dev/null +++ b/utils/tool.go @@ -0,0 +1,17 @@ +// Copyright 2014 The Gogs Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package utils + +import ( + "crypto/md5" + "encoding/hex" +) + +// Encode string to md5 hex value +func EncodeMd5(str string) string { + m := md5.New() + m.Write([]byte(str)) + return hex.EncodeToString(m.Sum(nil)) +}