Browse Source

Method for activating email addresses through verification email

pull/755/head
Peter 10 years ago
parent
commit
ec71d538fc
  1. 33
      modules/mailer/mail.go
  2. 21
      routers/user/auth.go

33
modules/mailer/mail.go

@ -21,6 +21,7 @@ import (
const (
AUTH_ACTIVE base.TplName = "mail/auth/active"
AUTH_ACTIVATE_EMAIL base.TplName = "mail/auth/activate_email"
AUTH_REGISTER_SUCCESS base.TplName = "mail/auth/register_success"
AUTH_RESET_PASSWORD base.TplName = "mail/auth/reset_passwd"
@ -64,6 +65,17 @@ func CreateUserActiveCode(u *models.User, startInf interface{}) string {
return code
}
// create a time limit code for user active
func CreateUserEmailActivateCode(u *models.User, e *models.EmailAddress, startInf interface{}) string {
minutes := setting.Service.ActiveCodeLives
data := com.ToStr(u.Id) + e.Email + u.LowerName + u.Passwd + u.Rands
code := base.CreateTimeLimitCode(data, minutes, startInf)
// add tail hex username
code += hex.EncodeToString([]byte(u.LowerName))
return code
}
// Send user register mail with active code
func SendRegisterMail(r macaron.Render, u *models.User) {
code := CreateUserActiveCode(u, nil)
@ -103,6 +115,27 @@ func SendActiveMail(r macaron.Render, u *models.User) {
SendAsync(&msg)
}
// Send email to verify secondary email.
func SendActivateEmail(r macaron.Render, user *models.User, email *models.EmailAddress) {
code := CreateUserEmailActivateCode(user, email, nil)
subject := "Verify your e-mail address"
data := GetMailTmplData(user)
data["Code"] = code
data["Email"] = email.Email
body, err := r.HTMLString(string(AUTH_ACTIVATE_EMAIL), data)
if err != nil {
log.Error(4, "mail.SendActiveMail(fail to render): %v", err)
return
}
msg := NewMailMessage([]string{email.Email}, subject, body)
msg.Info = fmt.Sprintf("UID: %d, send activate email to %s", user.Id, email.Email)
SendAsync(&msg)
}
// Send reset password email.
func SendResetPasswdMail(r macaron.Render, u *models.User) {
code := CreateUserActiveCode(u, nil)

21
routers/user/auth.go

@ -343,6 +343,27 @@ func Activate(ctx *middleware.Context) {
ctx.HTML(200, ACTIVATE)
}
func ActivateEmail(ctx *middleware.Context) {
code := ctx.Query("code")
email_string := ctx.Query("email")
// Verify code.
if email := models.VerifyActiveEmailCode(code, email_string); email != nil {
err := email.Activate()
if err != nil {
ctx.Handle(500, "ActivateEmail", err)
}
log.Trace("Email activated: %s", email.Email)
ctx.Flash.Success(ctx.Tr("settings.activate_email_success"))
}
ctx.Redirect(setting.AppSubUrl + "/user/settings/email")
return
}
func ForgotPasswd(ctx *middleware.Context) {
ctx.Data["Title"] = ctx.Tr("auth.forgot_password")

Loading…
Cancel
Save