diff --git a/cmd/web.go b/cmd/web.go index 2d80a89b7d..f81512554a 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -96,7 +96,7 @@ func runWeb(*cli.Context) { m.Get("/avatar/:hash", avt.ServeHTTP) m.Group("/user", func(r martini.Router) { - r.Get("/login", user.SignIn) // TODO + r.Get("/login", user.SignIn) r.Post("/login", bindIgnErr(auth.LogInForm{}), user.SignInPost) r.Get("/login/:name", user.SocialSignIn) r.Get("/sign_up", user.SignUp) @@ -130,7 +130,7 @@ func runWeb(*cli.Context) { m.Get("/user/:username", ignSignIn, user.Profile) m.Group("/repo", func(r martini.Router) { - r.Get("/create", repo.Create) + r.Get("/create", repo.Create) // TODO r.Post("/create", bindIgnErr(auth.CreateRepoForm{}), repo.CreatePost) r.Get("/migrate", repo.Migrate) r.Post("/migrate", bindIgnErr(auth.MigrateRepoForm{}), repo.MigratePost) diff --git a/models/oauth2.go b/models/oauth2.go index d1ae4611b8..97ba519c57 100644 --- a/models/oauth2.go +++ b/models/oauth2.go @@ -74,3 +74,9 @@ func GetOauthByUserId(uid int64) (oas []*Oauth2, err error) { err = orm.Find(&oas, Oauth2{Uid: uid}) return oas, err } + +// DeleteOauth2ById deletes a oauth2 by ID. +func DeleteOauth2ById(id int64) error { + _, err := orm.Delete(&Oauth2{Id: id}) + return err +} diff --git a/modules/auth/apiv1/miscellaneous.go b/modules/auth/apiv1/miscellaneous.go index c34bdfa43b..4d0d25659d 100644 --- a/modules/auth/apiv1/miscellaneous.go +++ b/modules/auth/apiv1/miscellaneous.go @@ -22,19 +22,12 @@ type MarkdownForm struct { Context string `form:"context"` } -func (f *MarkdownForm) Name(field string) string { - names := map[string]string{ - "Text": "text", - } - return names[field] -} - func (f *MarkdownForm) Validate(errs *binding.BindingErrors, req *http.Request, ctx martini.Context) { data := ctx.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData) validateApiReq(errs, data, f) } -func validateApiReq(errs *binding.BindingErrors, data base.TmplData, f auth.Form) { +func validateApiReq(errs *binding.BindingErrors, data base.TmplData, f interface{}) { if errs.Count() == 0 { return } else if len(errs.Overall) > 0 { @@ -64,22 +57,21 @@ func validateApiReq(errs *binding.BindingErrors, data base.TmplData, f auth.Form } if err, ok := errs.Fields[field.Name]; ok { - data["Err_"+field.Name] = true switch err { case binding.BindingRequireError: - data["ErrorMsg"] = f.Name(field.Name) + " cannot be empty" + data["ErrorMsg"] = fieldName + " cannot be empty" case binding.BindingAlphaDashError: - data["ErrorMsg"] = f.Name(field.Name) + " must be valid alpha or numeric or dash(-_) characters" + data["ErrorMsg"] = fieldName + " must be valid alpha or numeric or dash(-_) characters" case binding.BindingAlphaDashDotError: - data["ErrorMsg"] = f.Name(field.Name) + " must be valid alpha or numeric or dash(-_) or dot characters" + data["ErrorMsg"] = fieldName + " must be valid alpha or numeric or dash(-_) or dot characters" case binding.BindingMinSizeError: - data["ErrorMsg"] = f.Name(field.Name) + " must contain at least " + auth.GetMinMaxSize(field) + " characters" + data["ErrorMsg"] = fieldName + " must contain at least " + auth.GetMinMaxSize(field) + " characters" case binding.BindingMaxSizeError: - data["ErrorMsg"] = f.Name(field.Name) + " must contain at most " + auth.GetMinMaxSize(field) + " characters" + data["ErrorMsg"] = fieldName + " must contain at most " + auth.GetMinMaxSize(field) + " characters" case binding.BindingEmailError: - data["ErrorMsg"] = f.Name(field.Name) + " is not a valid e-mail address" + data["ErrorMsg"] = fieldName + " is not a valid e-mail address" case binding.BindingUrlError: - data["ErrorMsg"] = f.Name(field.Name) + " is not a valid URL" + data["ErrorMsg"] = fieldName + " is not a valid URL" default: data["ErrorMsg"] = "Unknown error: " + err } diff --git a/modules/auth/auth.go b/modules/auth/auth.go index 62728acceb..a0e00c10c5 100644 --- a/modules/auth/auth.go +++ b/modules/auth/auth.go @@ -39,29 +39,15 @@ func (f *RegisterForm) Name(field string) string { return names[field] } -func (f *RegisterForm) Validate(errors *binding.BindingErrors, 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) +func (f *RegisterForm) Validate(errs *binding.BindingErrors, req *http.Request, ctx martini.Context) { + data := ctx.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData) + validate(errs, data, f) } type LogInForm struct { UserName string `form:"username" binding:"Required;MaxSize(35)"` Password string `form:"passwd" binding:"Required;MinSize(6);MaxSize(30)"` - Remember string `form:"remember"` + Remember bool `form:"remember"` } func (f *LogInForm) Name(field string) string { @@ -72,23 +58,9 @@ func (f *LogInForm) Name(field string) string { return names[field] } -func (f *LogInForm) Validate(errors *binding.BindingErrors, 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 (f *LogInForm) Validate(errs *binding.BindingErrors, req *http.Request, ctx martini.Context) { + data := ctx.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData) + validate(errs, data, f) } func GetMinMaxSize(field reflect.StructField) string { @@ -100,9 +72,21 @@ func GetMinMaxSize(field reflect.StructField) string { return "" } -func validate(errors *binding.BindingErrors, data base.TmplData, form Form) { - typ := reflect.TypeOf(form) - val := reflect.ValueOf(form) +func validate(errs *binding.BindingErrors, data base.TmplData, f Form) { + if errs.Count() == 0 { + return + } else if len(errs.Overall) > 0 { + for _, err := range errs.Overall { + log.Error("%s: %v", reflect.TypeOf(f), err) + } + return + } + + data["HasError"] = true + AssignForm(f, data) + + typ := reflect.TypeOf(f) + val := reflect.ValueOf(f) if typ.Kind() == reflect.Ptr { typ = typ.Elem() @@ -118,23 +102,23 @@ func validate(errors *binding.BindingErrors, data base.TmplData, form Form) { continue } - if err, ok := errors.Fields[field.Name]; ok { + if err, ok := errs.Fields[field.Name]; ok { data["Err_"+field.Name] = true switch err { case binding.BindingRequireError: - data["ErrorMsg"] = form.Name(field.Name) + " cannot be empty" + data["ErrorMsg"] = f.Name(field.Name) + " cannot be empty" case binding.BindingAlphaDashError: - data["ErrorMsg"] = form.Name(field.Name) + " must be valid alpha or numeric or dash(-_) characters" + data["ErrorMsg"] = f.Name(field.Name) + " must be valid alpha or numeric or dash(-_) characters" case binding.BindingAlphaDashDotError: - data["ErrorMsg"] = form.Name(field.Name) + " must be valid alpha or numeric or dash(-_) or dot characters" + data["ErrorMsg"] = f.Name(field.Name) + " must be valid alpha or numeric or dash(-_) or dot characters" case binding.BindingMinSizeError: - data["ErrorMsg"] = form.Name(field.Name) + " must contain at least " + GetMinMaxSize(field) + " characters" + data["ErrorMsg"] = f.Name(field.Name) + " must contain at least " + GetMinMaxSize(field) + " characters" case binding.BindingMaxSizeError: - data["ErrorMsg"] = form.Name(field.Name) + " must contain at most " + GetMinMaxSize(field) + " characters" + data["ErrorMsg"] = f.Name(field.Name) + " must contain at most " + GetMinMaxSize(field) + " characters" case binding.BindingEmailError: - data["ErrorMsg"] = form.Name(field.Name) + " is not a valid e-mail address" + data["ErrorMsg"] = f.Name(field.Name) + " is not a valid e-mail address" case binding.BindingUrlError: - data["ErrorMsg"] = form.Name(field.Name) + " is not a valid URL" + data["ErrorMsg"] = f.Name(field.Name) + " is not a valid URL" default: data["ErrorMsg"] = "Unknown error: " + err } diff --git a/modules/auth/setting.go b/modules/auth/setting.go index 3316d7b9bc..7bae2451fb 100644 --- a/modules/auth/setting.go +++ b/modules/auth/setting.go @@ -7,12 +7,10 @@ package auth import ( "net/http" "reflect" - "strings" "github.com/go-martini/martini" "github.com/gogits/gogs/modules/base" - "github.com/gogits/gogs/modules/log" "github.com/gogits/gogs/modules/middleware/binding" ) @@ -31,24 +29,5 @@ func (f *AddSSHKeyForm) Name(field string) string { func (f *AddSSHKeyForm) Validate(errors *binding.BindingErrors, req *http.Request, context martini.Context) { data := context.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData) - AssignForm(f, data) - - if req.Method == "GET" || errors.Count() == 0 { - if req.Method == "POST" && - (len(f.KeyContent) < 100 || !strings.HasPrefix(f.KeyContent, "ssh-rsa")) { - data["HasError"] = true - data["ErrorMsg"] = "SSH key content is not valid" - } - return - } - - data["HasError"] = true - if len(errors.Overall) > 0 { - for _, err := range errors.Overall { - log.Error("AddSSHKeyForm.Validate: %v", err) - } - return - } - validate(errors, data, f) } diff --git a/modules/auth/user.go b/modules/auth/user.go index 0e591398c0..bc3216fe36 100644 --- a/modules/auth/user.go +++ b/modules/auth/user.go @@ -78,7 +78,7 @@ type UpdateProfileForm struct { UserName string `form:"username" binding:"Required;AlphaDash;MaxSize(30)"` FullName string `form:"fullname" binding:"MaxSize(40)"` Email string `form:"email" binding:"Required;Email;MaxSize(50)"` - Website string `form:"website" binding:"MaxSize(50)"` + Website string `form:"website" binding:"Url;MaxSize(50)"` Location string `form:"location" binding:"MaxSize(50)"` Avatar string `form:"avatar" binding:"Required;Email;MaxSize(50)"` } @@ -94,22 +94,9 @@ func (f *UpdateProfileForm) Name(field string) string { return names[field] } -func (f *UpdateProfileForm) Validate(errors *binding.BindingErrors, 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 - - if len(errors.Overall) > 0 { - for _, err := range errors.Overall { - log.Error("UpdateProfileForm.Validate: %v", err) - } - return - } - - validate(errors, data, f) +func (f *UpdateProfileForm) Validate(errs *binding.BindingErrors, req *http.Request, ctx martini.Context) { + data := ctx.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData) + validate(errs, data, f) } type UpdatePasswdForm struct { @@ -127,20 +114,7 @@ func (f *UpdatePasswdForm) Name(field string) string { return names[field] } -func (f *UpdatePasswdForm) Validate(errors *binding.BindingErrors, 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 - - if len(errors.Overall) > 0 { - for _, err := range errors.Overall { - log.Error("UpdatePasswdForm.Validate: %v", err) - } - return - } - - validate(errors, data, f) +func (f *UpdatePasswdForm) Validate(errs *binding.BindingErrors, req *http.Request, ctx martini.Context) { + data := ctx.Get(reflect.TypeOf(base.TmplData{})).Interface().(base.TmplData) + validate(errs, data, f) } diff --git a/modules/base/template.go b/modules/base/template.go index dd98df75b1..a69a54618d 100644 --- a/modules/base/template.go +++ b/modules/base/template.go @@ -93,6 +93,7 @@ var TemplateFuncs template.FuncMap = map[string]interface{}{ "DiffLineTypeToStr": DiffLineTypeToStr, "ShortSha": ShortSha, "Oauth2Icon": Oauth2Icon, + "Oauth2Name": Oauth2Name, } type Actioner interface { @@ -217,3 +218,19 @@ func Oauth2Icon(t int) string { } return "" } + +func Oauth2Name(t int) string { + switch t { + case 1: + return "GitHub" + case 2: + return "Google" + case 3: + return "Twitter" + case 4: + return "Tencent QQ" + case 5: + return "Weibo" + } + return "" +} diff --git a/public/js/app.js b/public/js/app.js index 59d4461350..6093d23b94 100644 --- a/public/js/app.js +++ b/public/js/app.js @@ -1,6 +1,4 @@ -var Gogits = { - "PageIsSignup": false -}; +var Gogits = {}; (function ($) { // extend jQuery ajax, set csrf token value @@ -333,32 +331,6 @@ function initCore() { Gogits.renderCodeView(); } -function initRegister() { - $.getScript("/js/jquery.validate.min.js", function () { - Gogits.validateForm("#login-card", { - rules: { - "username": { - required: true, - maxlength: 30 - }, - "email": { - required: true, - email: true - }, - "passwd": { - required: true, - minlength: 6, - maxlength: 30 - }, - "re-passwd": { - required: true, - equalTo: "input[name=passwd]" - } - } - }); - }); -} - function initUserSetting() { // ssh confirmation $('#ssh-keys .delete').confirmation({ @@ -626,9 +598,6 @@ function initRepoSetting() { $(function () { initCore(); var body = $("#body"); - if (body.data("page") == "user-signup") { - initRegister(); - } if (body.data("page") == "user") { initUserSetting(); } diff --git a/routers/user/setting.go b/routers/user/setting.go index f62e931051..8b8895a3b7 100644 --- a/routers/user/setting.go +++ b/routers/user/setting.go @@ -5,7 +5,7 @@ package user import ( - "strconv" + "strings" "github.com/gogits/gogs/models" "github.com/gogits/gogs/modules/auth" @@ -22,11 +22,10 @@ func Setting(ctx *middleware.Context) { ctx.HTML(200, "user/setting") } -// Render user setting page (email, website modify) func SettingPost(ctx *middleware.Context, form auth.UpdateProfileForm) { ctx.Data["Title"] = "Setting" - ctx.Data["PageIsUserSetting"] = true // For navbar arrow. - ctx.Data["IsUserPageSetting"] = true // For setting nav highlight. + ctx.Data["PageIsUserSetting"] = true + ctx.Data["IsUserPageSetting"] = true user := ctx.User ctx.Data["Owner"] = user @@ -74,9 +73,22 @@ func SettingSocial(ctx *middleware.Context) { ctx.Data["Title"] = "Social Account" ctx.Data["PageIsUserSetting"] = true ctx.Data["IsUserPageSettingSocial"] = true + + // Unbind social account. + remove, _ := base.StrTo(ctx.Query("remove")).Int64() + if remove > 0 { + if err := models.DeleteOauth2ById(remove); err != nil { + ctx.Handle(500, "user.SettingSocial(DeleteOauth2ById)", err) + return + } + ctx.Flash.Success("OAuth2 has been unbinded.") + ctx.Redirect("/user/settings/social") + return + } + socials, err := models.GetOauthByUserId(ctx.User.Id) if err != nil { - ctx.Handle(500, "user.SettingSocial", err) + ctx.Handle(500, "user.SettingSocial(GetOauthByUserId)", err) return } @@ -108,9 +120,9 @@ func SettingPasswordPost(ctx *middleware.Context, form auth.UpdatePasswdForm) { } tmpUser.EncodePasswd() if user.Passwd != tmpUser.Passwd { - ctx.Flash.Error("Old password is not correct") + ctx.Flash.Error("Old password is not correct.") } else if form.NewPasswd != form.RetypePasswd { - ctx.Flash.Error("New password and re-type password are not same") + ctx.Flash.Error("New password and re-type password are not same.") } else { user.Passwd = form.NewPasswd user.Salt = models.GetUserSalt() @@ -128,10 +140,12 @@ func SettingPasswordPost(ctx *middleware.Context, form auth.UpdatePasswdForm) { func SettingSSHKeys(ctx *middleware.Context, form auth.AddSSHKeyForm) { ctx.Data["Title"] = "SSH Keys" + ctx.Data["PageIsUserSetting"] = true + ctx.Data["IsUserPageSettingSSH"] = true // Delete SSH key. if ctx.Req.Method == "DELETE" || ctx.Query("_method") == "DELETE" { - id, err := strconv.ParseInt(ctx.Query("id"), 10, 64) + id, err := base.StrTo(ctx.Query("id")).Int64() if err != nil { log.Error("ssh.DelPublicKey: %v", err) ctx.JSON(200, map[string]interface{}{ @@ -160,6 +174,14 @@ func SettingSSHKeys(ctx *middleware.Context, form auth.AddSSHKeyForm) { return } + // List existed SSH keys. + keys, err := models.ListPublicKey(ctx.User.Id) + if err != nil { + ctx.Handle(500, "ssh.ListPublicKey", err) + return + } + ctx.Data["Keys"] = keys + // Add new SSH key. if ctx.Req.Method == "POST" { if ctx.HasError() { @@ -167,6 +189,12 @@ func SettingSSHKeys(ctx *middleware.Context, form auth.AddSSHKeyForm) { return } + if len(form.KeyContent) < 100 || !strings.HasPrefix(form.KeyContent, "ssh-rsa") { + ctx.Flash.Error("SSH key content is not valid.") + ctx.Redirect("/user/settings/ssh") + return + } + k := &models.PublicKey{ OwnerId: ctx.User.Id, Name: form.KeyName, @@ -188,16 +216,6 @@ func SettingSSHKeys(ctx *middleware.Context, form auth.AddSSHKeyForm) { } } - // List existed SSH keys. - keys, err := models.ListPublicKey(ctx.User.Id) - if err != nil { - ctx.Handle(200, "ssh.ListPublicKey", err) - return - } - - ctx.Data["PageIsUserSetting"] = true - ctx.Data["IsUserPageSettingSSH"] = true - ctx.Data["Keys"] = keys ctx.HTML(200, "user/publickey") } diff --git a/routers/user/user.go b/routers/user/user.go index 9cce4e719d..7af65fc585 100644 --- a/routers/user/user.go +++ b/routers/user/user.go @@ -49,14 +49,14 @@ func SignIn(ctx *middleware.Context) { user, err := models.GetUserByName(userName) if err != nil { - ctx.HTML(500, "user/signin") + ctx.Handle(500, "user.SignIn(GetUserByName)", err) return } secret := base.EncodeMd5(user.Rands + user.Passwd) value, _ := ctx.GetSecureCookie(secret, base.CookieRememberName) if value != user.Name { - ctx.HTML(500, "user/signin") + ctx.HTML(200, "user/signin") return } @@ -85,6 +85,7 @@ func SignInPost(ctx *middleware.Context, form auth.LogInForm) { } if ctx.HasError() { + println("shit") ctx.HTML(200, "user/signin") return } @@ -112,7 +113,7 @@ func SignInPost(ctx *middleware.Context, form auth.LogInForm) { return } - if form.Remember == "on" { + if form.Remember { secret := base.EncodeMd5(user.Rands + user.Passwd) days := 86400 * base.LogInRememberDays ctx.SetCookie(base.CookieUserName, user.Name, days) @@ -208,19 +209,18 @@ func SignUpPost(ctx *middleware.Context, form auth.RegisterForm) { ctx.Data["IsSocialLogin"] = true } - if form.Password != form.RetypePasswd { - ctx.Data["HasError"] = true - ctx.Data["Err_Password"] = true - ctx.Data["Err_RetypePasswd"] = true - ctx.Data["ErrorMsg"] = "Password and re-type password are not same" - auth.AssignForm(form, ctx.Data) - } - if ctx.HasError() { ctx.HTML(200, "user/signup") return } + if form.Password != form.RetypePasswd { + ctx.Data["Err_Password"] = true + ctx.Data["Err_RetypePasswd"] = true + ctx.RenderWithErr("Password and re-type password are not same", "user/signup", &form) + return + } + u := &models.User{ Name: form.UserName, Email: form.Email, @@ -415,7 +415,6 @@ func ResetPasswd(ctx *middleware.Context) { return } ctx.Data["Code"] = code - ctx.Data["IsResetForm"] = true ctx.HTML(200, "user/reset_passwd") } diff --git a/templates/repo/collaboration.tmpl b/templates/repo/collaboration.tmpl index 8ede9d4321..564c99eca2 100644 --- a/templates/repo/collaboration.tmpl +++ b/templates/repo/collaboration.tmpl @@ -10,6 +10,7 @@
Collaborators
+
+