func (t *RegisterTest) TestRegisterPostWorks() { var user models.User defer engine.Delete(&user) form := url.Values{ "user.Name": []string{"testName"}, "user.Email": []string{"*****@*****.**"}, "user.Password": []string{"testtest"}, "user.ConfirmPassword": []string{"testtest"}, } t.PostForm("/account/postregister", form) user.Email = "*****@*****.**" has, _ := engine.Get(&user) t.Assert(has) }
//POST account/register func (c Account) PostRegister(user models.User) revel.Result { user.Validate(c.Validation) if c.Validation.HasErrors() { //把错误信息存到flash c.Validation.Keep() //把参数存到flash //c.FlashParams() return c.Redirect(routes.Account.Register()) } user.GravatarUrl = genGravatarUrl(user.Email) code := uuid.NewUUID() user.ActiveCode = code.String() user.ActiveCodeCreatedTime = time.Now() if !user.Save() { c.Flash.Error("Registered user failed") return c.Redirect(routes.Account.Register()) } c.Session[USERNAME] = user.Name subject := "activate password" content := fmt.Sprintf(`<h2><a href="http://%s:%s/account/activate/%s">`+ `activate account</a></h2>`, appAddr, appPort, user.ActiveCode) err := SendMail( subject, content, smtpConfig.Username, []string{user.Email}, smtpConfig, true) if err != nil { fmt.Println(err) } c.Flash.Success("please check email to make your account active") return c.Redirect(routes.Account.Notice()) }
//POST /account/login func (c *Account) PostLogin(user models.User) revel.Result { //check input legality c.Validation.Email(user.Email) c.Validation.Required(user.Password) if c.Validation.HasErrors() { c.Validation.Keep() //c.FlashParams() return c.Redirect(routes.Account.Login()) } if !user.LoginOk() { c.Validation.Keep() c.FlashParams() c.Flash.Error("account or password is incorrect") return c.Redirect(routes.Account.Login()) } else { c.Session[USERNAME] = user.Name } return c.Redirect("/code/status") }
//POST /account/edit func (c Account) PostEdit(user models.User) revel.Result { c.Validation.Required(user.Name).Message("用户名不能为空") if c.Validation.HasErrors() { c.Validation.Keep() c.FlashParams() return c.Redirect(routes.Account.Edit()) } if user.HasName() { return c.Redirect(routes.User.Profile()) } if user.Password != "" { if user.Password == user.ConfirmPassword { user.HashedPassword, user.Salt = models.GenHashPasswordAndSalt(user.Password) username := c.Session[USERNAME] u := models.GetCurrentUser(username) _, err := engine.Id(u.Id).Update(user) if err != nil { fmt.Println(err) } c.Session[USERNAME] = user.Name return c.Redirect(routes.User.Profile()) } else { c.Flash.Error("passwords not match") return c.Redirect(routes.Account.Notice()) } } else { username := c.Session[USERNAME] u := models.GetCurrentUser(username) _, err := engine.Id(u.Id).Cols("name").Update(user) if err != nil { fmt.Println(err) c.Flash.Error(err.Error()) } else { c.Session[USERNAME] = user.Name c.Flash.Success("modify sucess") } return c.Redirect(routes.User.Profile()) } }
//POST /account/send-reset-email func (c Account) SendResetEmail(email string) revel.Result { var user models.User code := uuid.NewUUID() user.Email = email user.ResetCode = code.String() user.ResetCodeCreatedTime = time.Now() if user.HasEmail() { _, err := engine.Where("email = ?", email). Cols("reset_code", "reset_code_created_time"). Update(&user) if err != nil { fmt.Println(err) } subject := "reset password" content := fmt.Sprintf( `<h2><a href="http://%s:%s/account/reset/%s">Reset Password</a></h2>`, appAddr, appPort, user.ResetCode) SendMail( subject, content, smtpConfig.Username, []string{email}, smtpConfig, true) c.Flash.Success("Email has been sent, pleas check it.") return c.Redirect(routes.Account.Notice()) } else { c.Flash.Error("Incorrect Email") return c.Redirect(routes.Account.Notice()) } }
//POST /account/reset func (c Account) PostReset(user models.User) revel.Result { username := c.Session[USERNAME] if user.Password == user.ConfirmPassword { user.HashedPassword, user.Salt = models.GenHashPasswordAndSalt(user.Password) user.ResetCode = "" _, err := engine.Where("name = ?", username). Cols("hashed_password", "salt", "reset_code"). Update(&user) if err != nil { fmt.Println(err) } c.Session[USERNAME] = username c.Flash.Success("reset success!") return c.Redirect(routes.Problem.Index(0)) } else { resetcode := c.Flash.Data[RESETCODE] c.Flash.Error("两次密码输入不一致") return c.Redirect(routes.Account.Reset(resetcode)) } }