2018-05-17 13:05:00 +09:00
|
|
|
// Copyright 2014 The Gogs Authors. All rights reserved.
|
|
|
|
// Copyright 2018 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package setting
|
|
|
|
|
|
|
|
import (
|
2019-08-29 23:05:42 +09:00
|
|
|
"errors"
|
2021-04-06 00:30:52 +09:00
|
|
|
"net/http"
|
2021-01-18 05:48:38 +09:00
|
|
|
"time"
|
2019-08-29 23:05:42 +09:00
|
|
|
|
2018-05-17 13:05:00 +09:00
|
|
|
"code.gitea.io/gitea/models"
|
2021-11-23 00:21:55 +09:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-11-11 16:03:30 +09:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2018-05-17 13:05:00 +09:00
|
|
|
"code.gitea.io/gitea/modules/base"
|
|
|
|
"code.gitea.io/gitea/modules/context"
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
2019-10-15 00:24:26 +09:00
|
|
|
"code.gitea.io/gitea/modules/password"
|
2018-05-17 13:05:00 +09:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2019-08-15 23:46:21 +09:00
|
|
|
"code.gitea.io/gitea/modules/timeutil"
|
2021-01-27 00:36:53 +09:00
|
|
|
"code.gitea.io/gitea/modules/web"
|
2021-07-24 19:16:34 +09:00
|
|
|
"code.gitea.io/gitea/services/auth"
|
2021-04-07 04:44:05 +09:00
|
|
|
"code.gitea.io/gitea/services/forms"
|
2019-09-24 14:02:49 +09:00
|
|
|
"code.gitea.io/gitea/services/mailer"
|
2021-11-19 02:42:27 +09:00
|
|
|
"code.gitea.io/gitea/services/user"
|
2018-05-17 13:05:00 +09:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
tplSettingsAccount base.TplName = "user/settings/account"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Account renders change user's password, user's email and user suicide page
|
|
|
|
func Account(ctx *context.Context) {
|
|
|
|
ctx.Data["Title"] = ctx.Tr("settings")
|
|
|
|
ctx.Data["PageIsSettingsAccount"] = true
|
|
|
|
ctx.Data["Email"] = ctx.User.Email
|
|
|
|
|
2018-06-19 03:24:45 +09:00
|
|
|
loadAccountData(ctx)
|
2018-05-17 13:05:00 +09:00
|
|
|
|
2021-04-06 00:30:52 +09:00
|
|
|
ctx.HTML(http.StatusOK, tplSettingsAccount)
|
2018-05-17 13:05:00 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// AccountPost response for change user's password
|
2021-01-27 00:36:53 +09:00
|
|
|
func AccountPost(ctx *context.Context) {
|
2021-04-07 04:44:05 +09:00
|
|
|
form := web.GetForm(ctx).(*forms.ChangePasswordForm)
|
2018-05-17 13:05:00 +09:00
|
|
|
ctx.Data["Title"] = ctx.Tr("settings")
|
|
|
|
ctx.Data["PageIsSettingsAccount"] = true
|
|
|
|
|
|
|
|
if ctx.HasError() {
|
2018-06-19 03:24:45 +09:00
|
|
|
loadAccountData(ctx)
|
|
|
|
|
2021-04-06 00:30:52 +09:00
|
|
|
ctx.HTML(http.StatusOK, tplSettingsAccount)
|
2018-05-17 13:05:00 +09:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(form.Password) < setting.MinPasswordLength {
|
|
|
|
ctx.Flash.Error(ctx.Tr("auth.password_too_short", setting.MinPasswordLength))
|
|
|
|
} else if ctx.User.IsPasswordSet() && !ctx.User.ValidatePassword(form.OldPassword) {
|
|
|
|
ctx.Flash.Error(ctx.Tr("settings.password_incorrect"))
|
|
|
|
} else if form.Password != form.Retype {
|
|
|
|
ctx.Flash.Error(ctx.Tr("form.password_not_match"))
|
2019-10-15 00:24:26 +09:00
|
|
|
} else if !password.IsComplexEnough(form.Password) {
|
2019-11-20 07:44:58 +09:00
|
|
|
ctx.Flash.Error(password.BuildComplexityError(ctx))
|
2021-05-31 15:18:11 +09:00
|
|
|
} else if pwned, err := password.IsPwned(ctx, form.Password); pwned || err != nil {
|
2020-09-09 07:06:39 +09:00
|
|
|
errMsg := ctx.Tr("auth.password_pwned")
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err.Error())
|
|
|
|
errMsg = ctx.Tr("auth.password_pwned_err")
|
|
|
|
}
|
|
|
|
ctx.Flash.Error(errMsg)
|
2018-05-17 13:05:00 +09:00
|
|
|
} else {
|
|
|
|
var err error
|
2021-01-11 03:05:18 +09:00
|
|
|
if err = ctx.User.SetPassword(form.Password); err != nil {
|
2018-05-17 13:05:00 +09:00
|
|
|
ctx.ServerError("UpdateUser", err)
|
|
|
|
return
|
|
|
|
}
|
2021-11-24 18:49:20 +09:00
|
|
|
if err := user_model.UpdateUserCols(db.DefaultContext, ctx.User, "salt", "passwd_hash_algo", "passwd"); err != nil {
|
2018-05-17 13:05:00 +09:00
|
|
|
ctx.ServerError("UpdateUser", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Trace("User password updated: %s", ctx.User.Name)
|
|
|
|
ctx.Flash.Success(ctx.Tr("settings.change_password_success"))
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Redirect(setting.AppSubURL + "/user/settings/account")
|
|
|
|
}
|
|
|
|
|
|
|
|
// EmailPost response for change user's email
|
2021-01-27 00:36:53 +09:00
|
|
|
func EmailPost(ctx *context.Context) {
|
2021-04-07 04:44:05 +09:00
|
|
|
form := web.GetForm(ctx).(*forms.AddEmailForm)
|
2018-05-17 13:05:00 +09:00
|
|
|
ctx.Data["Title"] = ctx.Tr("settings")
|
|
|
|
ctx.Data["PageIsSettingsAccount"] = true
|
|
|
|
|
|
|
|
// Make emailaddress primary.
|
2021-08-11 09:31:13 +09:00
|
|
|
if ctx.FormString("_method") == "PRIMARY" {
|
2021-11-28 23:11:58 +09:00
|
|
|
if err := user_model.MakeEmailPrimary(&user_model.EmailAddress{ID: ctx.FormInt64("id")}); err != nil {
|
2018-05-17 13:05:00 +09:00
|
|
|
ctx.ServerError("MakeEmailPrimary", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Trace("Email made primary: %s", ctx.User.Name)
|
|
|
|
ctx.Redirect(setting.AppSubURL + "/user/settings/account")
|
|
|
|
return
|
|
|
|
}
|
2020-03-03 03:25:36 +09:00
|
|
|
// Send activation Email
|
2021-08-11 09:31:13 +09:00
|
|
|
if ctx.FormString("_method") == "SENDACTIVATION" {
|
2020-03-03 03:25:36 +09:00
|
|
|
var address string
|
|
|
|
if ctx.Cache.IsExist("MailResendLimit_" + ctx.User.LowerName) {
|
|
|
|
log.Error("Send activation: activation still pending")
|
|
|
|
ctx.Redirect(setting.AppSubURL + "/user/settings/account")
|
|
|
|
return
|
|
|
|
}
|
2021-07-14 05:59:27 +09:00
|
|
|
|
2021-07-29 10:42:15 +09:00
|
|
|
id := ctx.FormInt64("id")
|
2021-11-11 16:03:30 +09:00
|
|
|
email, err := user_model.GetEmailAddressByID(ctx.User.ID, id)
|
2021-07-14 05:59:27 +09:00
|
|
|
if err != nil {
|
|
|
|
log.Error("GetEmailAddressByID(%d,%d) error: %v", ctx.User.ID, id, err)
|
|
|
|
ctx.Redirect(setting.AppSubURL + "/user/settings/account")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if email == nil {
|
|
|
|
log.Warn("Send activation failed: EmailAddress[%d] not found for user: %-v", id, ctx.User)
|
|
|
|
ctx.Redirect(setting.AppSubURL + "/user/settings/account")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if email.IsActivated {
|
|
|
|
log.Debug("Send activation failed: email %s is already activated for user: %-v", email.Email, ctx.User)
|
|
|
|
ctx.Redirect(setting.AppSubURL + "/user/settings/account")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if email.IsPrimary {
|
|
|
|
if ctx.User.IsActive && !setting.Service.RegisterEmailConfirm {
|
|
|
|
log.Debug("Send activation failed: email %s is already activated for user: %-v", email.Email, ctx.User)
|
2020-03-03 03:25:36 +09:00
|
|
|
ctx.Redirect(setting.AppSubURL + "/user/settings/account")
|
|
|
|
return
|
|
|
|
}
|
2021-07-14 05:59:27 +09:00
|
|
|
// Only fired when the primary email is inactive (Wrong state)
|
2020-03-03 03:25:36 +09:00
|
|
|
mailer.SendActivateAccountMail(ctx.Locale, ctx.User)
|
|
|
|
} else {
|
2021-04-02 19:25:13 +09:00
|
|
|
mailer.SendActivateEmailMail(ctx.User, email)
|
2020-03-03 03:25:36 +09:00
|
|
|
}
|
2021-07-14 05:59:27 +09:00
|
|
|
address = email.Email
|
2020-03-03 03:25:36 +09:00
|
|
|
|
|
|
|
if err := ctx.Cache.Put("MailResendLimit_"+ctx.User.LowerName, ctx.User.LowerName, 180); err != nil {
|
|
|
|
log.Error("Set cache(MailResendLimit) fail: %v", err)
|
|
|
|
}
|
|
|
|
ctx.Flash.Info(ctx.Tr("settings.add_email_confirmation_sent", address, timeutil.MinutesToFriendly(setting.Service.ActiveCodeLives, ctx.Locale.Language())))
|
|
|
|
ctx.Redirect(setting.AppSubURL + "/user/settings/account")
|
|
|
|
return
|
|
|
|
}
|
2019-08-29 23:05:42 +09:00
|
|
|
// Set Email Notification Preference
|
2021-08-11 09:31:13 +09:00
|
|
|
if ctx.FormString("_method") == "NOTIFICATION" {
|
|
|
|
preference := ctx.FormString("preference")
|
2021-11-24 18:49:20 +09:00
|
|
|
if !(preference == user_model.EmailNotificationsEnabled ||
|
|
|
|
preference == user_model.EmailNotificationsOnMention ||
|
|
|
|
preference == user_model.EmailNotificationsDisabled) {
|
2019-08-29 23:05:42 +09:00
|
|
|
log.Error("Email notifications preference change returned unrecognized option %s: %s", preference, ctx.User.Name)
|
|
|
|
ctx.ServerError("SetEmailPreference", errors.New("option unrecognized"))
|
|
|
|
return
|
|
|
|
}
|
2021-11-24 18:49:20 +09:00
|
|
|
if err := user_model.SetEmailNotifications(ctx.User, preference); err != nil {
|
2019-08-29 23:05:42 +09:00
|
|
|
log.Error("Set Email Notifications failed: %v", err)
|
|
|
|
ctx.ServerError("SetEmailNotifications", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Trace("Email notifications preference made %s: %s", preference, ctx.User.Name)
|
2020-04-10 02:22:17 +09:00
|
|
|
ctx.Flash.Success(ctx.Tr("settings.email_preference_set_success"))
|
2019-08-29 23:05:42 +09:00
|
|
|
ctx.Redirect(setting.AppSubURL + "/user/settings/account")
|
|
|
|
return
|
|
|
|
}
|
2018-05-17 13:05:00 +09:00
|
|
|
|
|
|
|
if ctx.HasError() {
|
2018-06-19 03:24:45 +09:00
|
|
|
loadAccountData(ctx)
|
|
|
|
|
2021-04-06 00:30:52 +09:00
|
|
|
ctx.HTML(http.StatusOK, tplSettingsAccount)
|
2018-05-17 13:05:00 +09:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-11 16:03:30 +09:00
|
|
|
email := &user_model.EmailAddress{
|
2018-05-17 13:05:00 +09:00
|
|
|
UID: ctx.User.ID,
|
|
|
|
Email: form.Email,
|
|
|
|
IsActivated: !setting.Service.RegisterEmailConfirm,
|
|
|
|
}
|
2021-11-11 16:03:30 +09:00
|
|
|
if err := user_model.AddEmailAddress(email); err != nil {
|
|
|
|
if user_model.IsErrEmailAlreadyUsed(err) {
|
2018-06-19 03:24:45 +09:00
|
|
|
loadAccountData(ctx)
|
|
|
|
|
2018-05-17 13:05:00 +09:00
|
|
|
ctx.RenderWithErr(ctx.Tr("form.email_been_used"), tplSettingsAccount, &form)
|
|
|
|
return
|
2021-11-11 16:03:30 +09:00
|
|
|
} else if user_model.IsErrEmailInvalid(err) {
|
2020-11-15 01:53:43 +09:00
|
|
|
loadAccountData(ctx)
|
|
|
|
|
|
|
|
ctx.RenderWithErr(ctx.Tr("form.email_invalid"), tplSettingsAccount, &form)
|
|
|
|
return
|
2018-05-17 13:05:00 +09:00
|
|
|
}
|
|
|
|
ctx.ServerError("AddEmailAddress", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send confirmation email
|
|
|
|
if setting.Service.RegisterEmailConfirm {
|
2021-04-02 19:25:13 +09:00
|
|
|
mailer.SendActivateEmailMail(ctx.User, email)
|
2018-05-17 13:05:00 +09:00
|
|
|
if err := ctx.Cache.Put("MailResendLimit_"+ctx.User.LowerName, ctx.User.LowerName, 180); err != nil {
|
2019-04-02 16:48:31 +09:00
|
|
|
log.Error("Set cache(MailResendLimit) fail: %v", err)
|
2018-05-17 13:05:00 +09:00
|
|
|
}
|
2019-08-15 23:46:21 +09:00
|
|
|
ctx.Flash.Info(ctx.Tr("settings.add_email_confirmation_sent", email.Email, timeutil.MinutesToFriendly(setting.Service.ActiveCodeLives, ctx.Locale.Language())))
|
2018-05-17 13:05:00 +09:00
|
|
|
} else {
|
|
|
|
ctx.Flash.Success(ctx.Tr("settings.add_email_success"))
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Trace("Email address added: %s", email.Email)
|
|
|
|
ctx.Redirect(setting.AppSubURL + "/user/settings/account")
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteEmail response for delete user's email
|
|
|
|
func DeleteEmail(ctx *context.Context) {
|
2021-11-11 16:03:30 +09:00
|
|
|
if err := user_model.DeleteEmailAddress(&user_model.EmailAddress{ID: ctx.FormInt64("id"), UID: ctx.User.ID}); err != nil {
|
2018-05-17 13:05:00 +09:00
|
|
|
ctx.ServerError("DeleteEmail", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Trace("Email address deleted: %s", ctx.User.Name)
|
|
|
|
|
|
|
|
ctx.Flash.Success(ctx.Tr("settings.email_deletion_success"))
|
2021-04-06 00:30:52 +09:00
|
|
|
ctx.JSON(http.StatusOK, map[string]interface{}{
|
2018-05-17 13:05:00 +09:00
|
|
|
"redirect": setting.AppSubURL + "/user/settings/account",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteAccount render user suicide page and response for delete user himself
|
|
|
|
func DeleteAccount(ctx *context.Context) {
|
|
|
|
ctx.Data["Title"] = ctx.Tr("settings")
|
|
|
|
ctx.Data["PageIsSettingsAccount"] = true
|
|
|
|
|
2021-09-17 20:43:47 +09:00
|
|
|
if _, _, err := auth.UserSignIn(ctx.User.Name, ctx.FormString("password")); err != nil {
|
2021-11-24 18:49:20 +09:00
|
|
|
if user_model.IsErrUserNotExist(err) {
|
2018-06-19 03:24:45 +09:00
|
|
|
loadAccountData(ctx)
|
|
|
|
|
2018-05-17 13:05:00 +09:00
|
|
|
ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_password"), tplSettingsAccount, nil)
|
|
|
|
} else {
|
|
|
|
ctx.ServerError("UserSignIn", err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-19 02:42:27 +09:00
|
|
|
if err := user.DeleteUser(ctx.User); err != nil {
|
2018-05-17 13:05:00 +09:00
|
|
|
switch {
|
|
|
|
case models.IsErrUserOwnRepos(err):
|
|
|
|
ctx.Flash.Error(ctx.Tr("form.still_own_repo"))
|
|
|
|
ctx.Redirect(setting.AppSubURL + "/user/settings/account")
|
|
|
|
case models.IsErrUserHasOrgs(err):
|
|
|
|
ctx.Flash.Error(ctx.Tr("form.still_has_org"))
|
|
|
|
ctx.Redirect(setting.AppSubURL + "/user/settings/account")
|
|
|
|
default:
|
|
|
|
ctx.ServerError("DeleteUser", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Trace("Account deleted: %s", ctx.User.Name)
|
|
|
|
ctx.Redirect(setting.AppSubURL + "/")
|
|
|
|
}
|
|
|
|
}
|
2018-06-19 03:24:45 +09:00
|
|
|
|
|
|
|
func loadAccountData(ctx *context.Context) {
|
2021-11-11 16:03:30 +09:00
|
|
|
emlist, err := user_model.GetEmailAddresses(ctx.User.ID)
|
2018-06-19 03:24:45 +09:00
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("GetEmailAddresses", err)
|
|
|
|
return
|
|
|
|
}
|
2020-03-03 03:25:36 +09:00
|
|
|
type UserEmail struct {
|
2021-11-11 16:03:30 +09:00
|
|
|
user_model.EmailAddress
|
2020-03-03 03:25:36 +09:00
|
|
|
CanBePrimary bool
|
|
|
|
}
|
|
|
|
pendingActivation := ctx.Cache.IsExist("MailResendLimit_" + ctx.User.LowerName)
|
|
|
|
emails := make([]*UserEmail, len(emlist))
|
|
|
|
for i, em := range emlist {
|
|
|
|
var email UserEmail
|
|
|
|
email.EmailAddress = *em
|
|
|
|
email.CanBePrimary = em.IsActivated
|
|
|
|
emails[i] = &email
|
|
|
|
}
|
2018-06-19 03:24:45 +09:00
|
|
|
ctx.Data["Emails"] = emails
|
2019-10-19 18:01:33 +09:00
|
|
|
ctx.Data["EmailNotificationsPreference"] = ctx.User.EmailNotifications()
|
2020-03-03 03:25:36 +09:00
|
|
|
ctx.Data["ActivationsPending"] = pendingActivation
|
|
|
|
ctx.Data["CanAddEmails"] = !pendingActivation || !setting.Service.RegisterEmailConfirm
|
2021-01-18 05:48:38 +09:00
|
|
|
|
2021-01-22 11:56:19 +09:00
|
|
|
if setting.Service.UserDeleteWithCommentsMaxTime != 0 {
|
|
|
|
ctx.Data["UserDeleteWithCommentsMaxTime"] = setting.Service.UserDeleteWithCommentsMaxTime.String()
|
|
|
|
ctx.Data["UserDeleteWithComments"] = ctx.User.CreatedUnix.AsTime().Add(setting.Service.UserDeleteWithCommentsMaxTime).After(time.Now())
|
2021-01-18 05:48:38 +09:00
|
|
|
}
|
2018-06-19 03:24:45 +09:00
|
|
|
}
|