Browse Source

Add updatePasswd

Unknown 11 years ago
parent
commit
c01f593daa
4 changed files with 69 additions and 1 deletions
  1. 2 1
      README.md
  2. 33 0
      modules/auth/user.go
  3. 33 0
      routers/user/setting.go
  4. 1 0
      web.go

+ 2 - 1
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

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

+ 33 - 0
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"
 

+ 1 - 0
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)