Browse Source

Finish register user

Unknown 11 years ago
parent
commit
b455478df8
5 changed files with 54 additions and 19 deletions
  1. 1 1
      gogs.go
  2. 9 14
      models/user.go
  3. 19 4
      routers/user/user.go
  4. 8 0
      utils/log/log.go
  5. 17 0
      utils/tool.go

+ 1 - 1
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() {
 

+ 9 - 14
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)

+ 19 - 4
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),
 	})

+ 8 - 0
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...)
+}

+ 17 - 0
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))
+}