summaryrefslogtreecommitdiffstats
path: root/routers/user/setting.go
diff options
context:
space:
mode:
authorUnknwon <u@gogs.io>2015-09-10 11:40:34 -0400
committerUnknwon <u@gogs.io>2015-09-10 11:40:34 -0400
commit52ec80fa18bf991c6356b7aa972a1d3983aa20c3 (patch)
tree878773705e8d959598be88354317d1bbea80e0a8 /routers/user/setting.go
parentc8d92fad305f78f0207203b3f1ea955e0ef0309d (diff)
downloadgitea-52ec80fa18bf991c6356b7aa972a1d3983aa20c3.tar.gz
gitea-52ec80fa18bf991c6356b7aa972a1d3983aa20c3.zip
finish all new user settings UI
Diffstat (limited to 'routers/user/setting.go')
-rw-r--r--routers/user/setting.go149
1 files changed, 66 insertions, 83 deletions
diff --git a/routers/user/setting.go b/routers/user/setting.go
index c62a123d5b..a7a52093bc 100644
--- a/routers/user/setting.go
+++ b/routers/user/setting.go
@@ -132,21 +132,41 @@ func SettingsAvatar(ctx *middleware.Context, form auth.UploadAvatarForm) {
ctx.Redirect(setting.AppSubUrl + "/user/settings")
}
-func SettingsEmails(ctx *middleware.Context) {
+func SettingsPassword(ctx *middleware.Context) {
ctx.Data["Title"] = ctx.Tr("settings")
- ctx.Data["PageIsSettingsEmails"] = true
+ ctx.Data["PageIsSettingsPassword"] = true
+ ctx.HTML(200, SETTINGS_PASSWORD)
+}
- emails, err := models.GetEmailAddresses(ctx.User.Id)
- if err != nil {
- ctx.Handle(500, "GetEmailAddresses", err)
+func SettingsPasswordPost(ctx *middleware.Context, form auth.ChangePasswordForm) {
+ ctx.Data["Title"] = ctx.Tr("settings")
+ ctx.Data["PageIsSettingsPassword"] = true
+
+ if ctx.HasError() {
+ ctx.HTML(200, SETTINGS_PASSWORD)
return
}
- ctx.Data["Emails"] = emails
- ctx.HTML(200, SETTINGS_EMAILS)
+ if !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"))
+ } else {
+ ctx.User.Passwd = form.Password
+ ctx.User.Salt = models.GetUserSalt()
+ ctx.User.EncodePasswd()
+ if err := models.UpdateUser(ctx.User); err != nil {
+ ctx.Handle(500, "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/password")
}
-func SettingsEmailPost(ctx *middleware.Context, form auth.AddEmailForm) {
+func SettingsEmails(ctx *middleware.Context) {
ctx.Data["Title"] = ctx.Tr("settings")
ctx.Data["PageIsSettingsEmails"] = true
@@ -157,51 +177,43 @@ func SettingsEmailPost(ctx *middleware.Context, form auth.AddEmailForm) {
}
ctx.Data["Emails"] = emails
- // Delete E-mail address.
- if ctx.Query("_method") == "DELETE" {
- id := ctx.QueryInt64("id")
- if id <= 0 {
- return
- }
+ ctx.HTML(200, SETTINGS_EMAILS)
+}
- if err = models.DeleteEmailAddress(&models.EmailAddress{Id: id}); err != nil {
- ctx.Handle(500, "DeleteEmail", err)
- } else {
- log.Trace("Email address deleted: %s", ctx.User.Name)
- ctx.Redirect(setting.AppSubUrl + "/user/settings/email")
- }
- return
- }
+func SettingsEmailPost(ctx *middleware.Context, form auth.AddEmailForm) {
+ ctx.Data["Title"] = ctx.Tr("settings")
+ ctx.Data["PageIsSettingsEmails"] = true
// Make emailaddress primary.
if ctx.Query("_method") == "PRIMARY" {
- id := ctx.QueryInt64("id")
- if id <= 0 {
+ if err := models.MakeEmailPrimary(&models.EmailAddress{ID: ctx.QueryInt64("id")}); err != nil {
+ ctx.Handle(500, "MakeEmailPrimary", err)
return
}
- if err = models.MakeEmailPrimary(&models.EmailAddress{Id: id}); err != nil {
- ctx.Handle(500, "MakeEmailPrimary", err)
- } else {
- log.Trace("Email made primary: %s", ctx.User.Name)
- ctx.Redirect(setting.AppSubUrl + "/user/settings/email")
- }
+ log.Trace("Email made primary: %s", ctx.User.Name)
+ ctx.Redirect(setting.AppSubUrl + "/user/settings/email")
return
}
// Add Email address.
+ emails, err := models.GetEmailAddresses(ctx.User.Id)
+ if err != nil {
+ ctx.Handle(500, "GetEmailAddresses", err)
+ return
+ }
+ ctx.Data["Emails"] = emails
+
if ctx.HasError() {
ctx.HTML(200, SETTINGS_EMAILS)
return
}
- cleanEmail := strings.Replace(form.Email, "\n", "", -1)
e := &models.EmailAddress{
- Uid: ctx.User.Id,
- Email: cleanEmail,
+ UID: ctx.User.Id,
+ Email: strings.TrimSpace(form.Email),
IsActivated: !setting.Service.RegisterEmailConfirm,
}
-
if err := models.AddEmailAddress(e); err != nil {
if models.IsErrEmailAlreadyUsed(err) {
ctx.RenderWithErr(ctx.Tr("form.email_been_used"), SETTINGS_EMAILS, &form)
@@ -209,64 +221,35 @@ func SettingsEmailPost(ctx *middleware.Context, form auth.AddEmailForm) {
}
ctx.Handle(500, "AddEmailAddress", err)
return
- } else {
- // Send confirmation e-mail
- if setting.Service.RegisterEmailConfirm {
- mailer.SendActivateEmail(ctx.Render, ctx.User, e)
+ }
- if err := ctx.Cache.Put("MailResendLimit_"+ctx.User.LowerName, ctx.User.LowerName, 180); err != nil {
- log.Error(4, "Set cache(MailResendLimit) fail: %v", err)
- }
- ctx.Flash.Info(ctx.Tr("settings.add_email_confirmation_sent", cleanEmail, setting.Service.ActiveCodeLives/60))
- } else {
- ctx.Flash.Success(ctx.Tr("settings.add_email_success"))
- }
+ // Send confirmation e-mail
+ if setting.Service.RegisterEmailConfirm {
+ mailer.SendActivateEmail(ctx.Render, ctx.User, e)
- log.Trace("Email address added: %s", e.Email)
- ctx.Redirect(setting.AppSubUrl + "/user/settings/email")
- return
+ if err := ctx.Cache.Put("MailResendLimit_"+ctx.User.LowerName, ctx.User.LowerName, 180); err != nil {
+ log.Error(4, "Set cache(MailResendLimit) fail: %v", err)
+ }
+ ctx.Flash.Info(ctx.Tr("settings.add_email_confirmation_sent", e.Email, setting.Service.ActiveCodeLives/60))
+ } else {
+ ctx.Flash.Success(ctx.Tr("settings.add_email_success"))
}
- ctx.HTML(200, SETTINGS_EMAILS)
-}
-
-func SettingsPassword(ctx *middleware.Context) {
- ctx.Data["Title"] = ctx.Tr("settings")
- ctx.Data["PageIsSettingsPassword"] = true
- ctx.HTML(200, SETTINGS_PASSWORD)
+ log.Trace("Email address added: %s", e.Email)
+ ctx.Redirect(setting.AppSubUrl + "/user/settings/email")
}
-func SettingsPasswordPost(ctx *middleware.Context, form auth.ChangePasswordForm) {
- ctx.Data["Title"] = ctx.Tr("settings")
- ctx.Data["PageIsSettingsPassword"] = true
-
- if ctx.HasError() {
- ctx.HTML(200, SETTINGS_PASSWORD)
+func DeleteEmail(ctx *middleware.Context) {
+ if err := models.DeleteEmailAddress(&models.EmailAddress{ID: ctx.QueryInt64("id")}); err != nil {
+ ctx.Handle(500, "DeleteEmail", err)
return
}
+ log.Trace("Email address deleted: %s", ctx.User.Name)
- tmpUser := &models.User{
- Passwd: form.OldPassword,
- Salt: ctx.User.Salt,
- }
- tmpUser.EncodePasswd()
- if ctx.User.Passwd != tmpUser.Passwd {
- ctx.Flash.Error(ctx.Tr("settings.password_incorrect"))
- } else if form.Password != form.Retype {
- ctx.Flash.Error(ctx.Tr("form.password_not_match"))
- } else {
- ctx.User.Passwd = form.Password
- ctx.User.Salt = models.GetUserSalt()
- ctx.User.EncodePasswd()
- if err := models.UpdateUser(ctx.User); err != nil {
- ctx.Handle(500, "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/password")
+ ctx.Flash.Success(ctx.Tr("settings.email_deletion_success"))
+ ctx.JSON(200, map[string]interface{}{
+ "redirect": setting.AppSubUrl + "/user/settings/email",
+ })
}
func SettingsSSHKeys(ctx *middleware.Context) {