From c01f593daa994dddc208f853c1c116c56d2ea397 Mon Sep 17 00:00:00 2001 From: Unknown Date: Thu, 13 Mar 2014 04:06:35 -0400 Subject: [PATCH] Add updatePasswd --- README.md | 3 ++- modules/auth/user.go | 33 +++++++++++++++++++++++++++++++++ routers/user/setting.go | 33 +++++++++++++++++++++++++++++++++ web.go | 1 + 4 files changed, 69 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 05056e20a..0e354a9d4 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ Gogs - Go Git Service [![wercker status](https://app.wercker.com/status/ad0bdb0b Gogs(Go Git Service) is a GitHub-like clone in the Go Programming Language, it currently supports Linux and Max OS X, but Windows has **NOT** supported yet due to installation problem with [libgit2](http://libgit2.github.com/) in Windows. -##### Current version: 0.0.6 Alpha +##### Current version: 0.0.7 Alpha ## Purpose @@ -18,6 +18,7 @@ Please see [Wiki](https://github.com/gogits/gogs/wiki) for project design, devel - SSH protocal support. - Register/delete account. - Create/delete public repository. +- User/repository home page. - Git repository manipulation. ## Installation diff --git a/modules/auth/user.go b/modules/auth/user.go index e868fac23..6bc713062 100644 --- a/modules/auth/user.go +++ b/modules/auth/user.go @@ -128,3 +128,36 @@ func (f *UpdateProfileForm) Validate(errors *binding.Errors, req *http.Request, validate(errors, data, f) } + +type UpdatePasswdForm struct { + OldPasswd string `form:"oldpasswd" binding:"Required;MinSize(6);MaxSize(30)"` + NewPasswd string `form:"newpasswd" binding:"Required;MinSize(6);MaxSize(30)"` + RetypePasswd string `form:"retypepasswd"` +} + +func (f *UpdatePasswdForm) Name(field string) string { + names := map[string]string{ + "OldPasswd": "Old password", + "NewPasswd": "New password", + "RetypePasswd": "Re-type password", + } + return names[field] +} + +func (f *UpdatePasswdForm) 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 + + if len(errors.Overall) > 0 { + for _, err := range errors.Overall { + log.Error("UpdatePasswdForm.Validate: %v", err) + } + return + } + + validate(errors, data, f) +} diff --git a/routers/user/setting.go b/routers/user/setting.go index 5ec4c455e..02a214b2c 100644 --- a/routers/user/setting.go +++ b/routers/user/setting.go @@ -47,6 +47,39 @@ func Setting(form auth.UpdateProfileForm, r render.Render, data base.TmplData, r r.HTML(200, "user/setting", data) } +func UpdatePasswd(form auth.UpdatePasswdForm, r render.Render, data base.TmplData, req *http.Request, session sessions.Session) { + data["Title"] = "Setting" + data["PageIsUserSetting"] = true + + user := auth.SignedInUser(session) + newUser := &models.User{Passwd: form.OldPasswd} + if err := newUser.EncodePasswd(); err != nil { + data["ErrorMsg"] = err + log.Error("setting.UpdatePasswd: %v", err) + r.HTML(200, "base/error", data) + return + } + + if user.Passwd != newUser.Passwd { + data["HasError"] = true + data["ErrorMsg"] = "Old password is not correct" + } else if form.NewPasswd != form.RetypePasswd { + data["HasError"] = true + data["ErrorMsg"] = "New password and re-type password are not same" + } else { + user.Passwd = newUser.Passwd + if err := models.UpdateUser(user); err != nil { + data["ErrorMsg"] = err + log.Error("setting.Setting: %v", err) + r.HTML(200, "base/error", data) + return + } + } + + data["Owner"] = user + r.HTML(200, "user/setting", data) +} + func SettingSSHKeys(form auth.AddSSHKeyForm, r render.Render, data base.TmplData, req *http.Request, session sessions.Session) { data["Title"] = "SSH Keys" diff --git a/web.go b/web.go index 856d0f106..a80c4924b 100644 --- a/web.go +++ b/web.go @@ -64,6 +64,7 @@ func runWeb(*cli.Context) { m.Get("/user/feeds", binding.Bind(auth.FeedsForm{}), user.Feeds) m.Any("/user/setting", auth.SignInRequire(true), binding.BindIgnErr(auth.UpdateProfileForm{}), user.Setting) + m.Post("/user/setting/update_passwd", auth.SignInRequire(true), binding.BindIgnErr(auth.UpdatePasswdForm{}), user.UpdatePasswd) m.Any("/user/setting/ssh", auth.SignInRequire(true), binding.BindIgnErr(auth.AddSSHKeyForm{}), user.SettingSSHKeys) m.Get("/user/:username", auth.SignInRequire(false), user.Profile)