diff --git a/README.md b/README.md index 367d07af1..c1f7b3d6b 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ Please see [Wiki](https://github.com/gogits/gogs/wiki) for project design. ### Dependencies - [Go Programming Language](http://golang.org): Main develop language. -- [libgit2](http://libgit2.github.com/): Git data manipulation. +- [libgit2](http://libgit2.github.com/)(cgo): Git data manipulation. ## Acknowledgments @@ -24,4 +24,4 @@ Please see [Wiki](https://github.com/gogits/gogs/wiki) for project design. ## Contributors -This project was launched by [Unknown](https://github.com/Unknwon) and [lunny](https://github.com/lunny). See [contributors page](https://github.com/gogits/gogs/graphs/contributors) for full list of contributors. \ No newline at end of file +This project was launched by [Unknown](https://github.com/Unknwon), [lunny](https://github.com/lunny) and [fuxiaohei](https://github.com/fuxiaohei). See [contributors page](https://github.com/gogits/gogs/graphs/contributors) for full list of contributors. \ No newline at end of file diff --git a/gogs.go b/gogs.go index 42b1f92ba..4c4d7da1f 100644 --- a/gogs.go +++ b/gogs.go @@ -19,7 +19,7 @@ import ( // Test that go1.1 tag above is included in builds. main.go refers to this definition. const go11tag = true -const APP_VER = "0.0.0.0306" +const APP_VER = "0.0.1.0306" func init() { runtime.GOMAXPROCS(runtime.NumCPU()) diff --git a/models/user.go b/models/user.go index 912b04a3e..a1ec293a7 100644 --- a/models/user.go +++ b/models/user.go @@ -192,7 +192,7 @@ func GetUserById(id int64) (*User, error) { // LoginUserPlain validates user by raw user name and password. func LoginUserPlain(name, passwd string) (*User, error) { - user := User{Name: name, Passwd: passwd} + user := User{LowerName: strings.ToLower(name), Passwd: passwd} if err := user.EncodePasswd(); err != nil { return nil, err } diff --git a/modules/auth/form.go b/modules/auth/form.go index 23c107c86..98c558399 100644 --- a/modules/auth/form.go +++ b/modules/auth/form.go @@ -28,7 +28,7 @@ type RegisterForm struct { RetypePasswd string `form:"retypepasswd"` } -func (r *RegisterForm) Name(field string) string { +func (f *RegisterForm) Name(field string) string { names := map[string]string{ "UserName": "Username", "Email": "E-mail address", @@ -38,6 +38,57 @@ func (r *RegisterForm) Name(field string) string { return names[field] } +func (f *RegisterForm) Validate(errors *binding.Errors, req *http.Request, context martini.Context) { + if req.Method == "GET" || errors.Count() == 0 { + return + } + + data := context.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData) + data["HasError"] = true + AssignForm(f, data) + + if len(errors.Overall) > 0 { + for _, err := range errors.Overall { + log.Error("RegisterForm.Validate: %v", err) + } + return + } + + validate(errors, data, f) +} + +type LogInForm struct { + UserName string `form:"username" binding:"Required;AlphaDash;MinSize(5);MaxSize(30)"` + Password string `form:"passwd" binding:"Required;MinSize(6);MaxSize(30)"` +} + +func (f *LogInForm) Name(field string) string { + names := map[string]string{ + "UserName": "Username", + "Password": "Password", + } + return names[field] +} + +func (f *LogInForm) Validate(errors *binding.Errors, req *http.Request, context martini.Context) { + if req.Method == "GET" || errors.Count() == 0 { + return + } + + data := context.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData) + data["HasError"] = true + AssignForm(f, data) + + if len(errors.Overall) > 0 { + for _, err := range errors.Overall { + log.Error("LogInForm.Validate: %v", err) + } + return + } + + validate(errors, data, f) +} + func getMinMaxSize(field reflect.StructField) string { for _, rule := range strings.Split(field.Tag.Get("binding"), ";") { if strings.HasPrefix(rule, "MinSize(") || strings.HasPrefix(rule, "MaxSize(") { @@ -86,25 +137,6 @@ func validate(errors *binding.Errors, data base.TmplData, form Form) { } } -func (r *RegisterForm) Validate(errors *binding.Errors, req *http.Request, context martini.Context) { - if req.Method == "GET" || errors.Count() == 0 { - return - } - - data := context.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData) - data["HasError"] = true - AssignForm(r, data) - - if len(errors.Overall) > 0 { - for _, err := range errors.Overall { - log.Error("RegisterForm.Validate: %v", err) - } - return - } - - validate(errors, data, r) -} - // AssignForm assign form values back to the template data. func AssignForm(form interface{}, data base.TmplData) { typ := reflect.TypeOf(form) diff --git a/routers/user/user.go b/routers/user/user.go index 0a85011a8..2e6cb3d59 100644 --- a/routers/user/user.go +++ b/routers/user/user.go @@ -79,36 +79,45 @@ func SignedInUser(session sessions.Session) *models.User { return user } -func SignIn(req *http.Request, r render.Render, session sessions.Session) { +func SignIn(form auth.LogInForm, data base.TmplData, req *http.Request, r render.Render, session sessions.Session) { // if logged, do not show login page if IsSignedIn(session) { r.Redirect("/") return } - var ( - errString string - account string - ) - // if post, do login action - if req.Method == "POST" { - account = req.FormValue("account") - user, err := models.LoginUserPlain(account, req.FormValue("passwd")) - if err == nil { - // login success - session.Set("userId", user.Id) - session.Set("userName", user.Name) - r.Redirect("/") + + data["Title"] = "Log In" + + if req.Method == "GET" { + r.HTML(200, "user/signin", data) + return + } + + if hasErr, ok := data["HasError"]; ok && hasErr.(bool) { + r.HTML(200, "user/signin", data) + return + } + + user, err := models.LoginUserPlain(form.UserName, form.Password) + if err != nil { + if err.Error() == models.ErrUserNotExist.Error() { + data["HasError"] = true + data["ErrorMsg"] = "Username or password is not correct" + auth.AssignForm(form, data) + r.HTML(200, "user/signin", data) return } - // login fail - errString = fmt.Sprintf("%v", err) - } - // if get or error post, show login page - r.HTML(200, "user/signin", map[string]interface{}{ - "Title": "Log In", - "Error": errString, - "Account": account, - }) + + data["ErrorMsg"] = err + log.Error("user.SignIn: %v", data) + r.HTML(500, "base/error", nil) + return + } + + // login success + session.Set("userId", user.Id) + session.Set("userName", user.Name) + r.Redirect("/") } func SignUp(form auth.RegisterForm, data base.TmplData, req *http.Request, r render.Render) { diff --git a/templates/user/signin.tmpl b/templates/user/signin.tmpl index f80b3199a..3aefefdcf 100644 --- a/templates/user/signin.tmpl +++ b/templates/user/signin.tmpl @@ -2,33 +2,35 @@ {{template "base/navbar" .}}
-

Log in

{{if .Error}} -
-
{{.Error}}
-
{{end}} -
- +

Log in

+
{{.ErrorMsg}}
+
+
- +
-
+ +
+ + + diff --git a/templates/user/signup.tmpl b/templates/user/signup.tmpl index 482cdc008..03e5bd344 100644 --- a/templates/user/signup.tmpl +++ b/templates/user/signup.tmpl @@ -4,12 +4,13 @@

Sign Up

{{.ErrorMsg}}
-
+
+
@@ -23,17 +24,20 @@
+
+
+
Already have an account? Sign in now! diff --git a/web.go b/web.go index 8f5c6c81b..6f8b902a6 100644 --- a/web.go +++ b/web.go @@ -58,8 +58,7 @@ func runWeb(*cli.Context) { // Routers. m.Get("/", routers.Home) - m.Any("/user/login", user.SignIn) - + m.Any("/user/login", binding.BindIgnErr(auth.LogInForm{}), user.SignIn) m.Any("/user/sign_up", binding.BindIgnErr(auth.RegisterForm{}), user.SignUp) m.Get("/user/profile", user.Profile) // should be /username