aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--gogs.go2
-rw-r--r--models/user.go23
-rw-r--r--routers/user/user.go23
-rw-r--r--utils/log/log.go8
-rw-r--r--utils/tool.go17
5 files changed, 54 insertions, 19 deletions
diff --git a/gogs.go b/gogs.go
index 6cb3f37e..140ee651 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 44dadb9a..ec88edde 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 f9dc07f2..24c22a4f 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 c725c527..e5c0b21c 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 00000000..1d2db4bc
--- /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))
+}