mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-11-25 08:59:31 -05:00
Method for activating email addresses through verification email
This commit is contained in:
parent
6919c80f0b
commit
ec71d538fc
2 changed files with 54 additions and 0 deletions
|
@ -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)
|
||||
|
|
|
@ -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…
Reference in a new issue