You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

178 lines
5.3 KiB

  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Copyright 2018 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package setting
  6. import (
  7. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/modules/auth"
  9. "code.gitea.io/gitea/modules/base"
  10. "code.gitea.io/gitea/modules/context"
  11. "code.gitea.io/gitea/modules/log"
  12. "code.gitea.io/gitea/modules/setting"
  13. )
  14. const (
  15. tplSettingsAccount base.TplName = "user/settings/account"
  16. )
  17. // Account renders change user's password, user's email and user suicide page
  18. func Account(ctx *context.Context) {
  19. ctx.Data["Title"] = ctx.Tr("settings")
  20. ctx.Data["PageIsSettingsAccount"] = true
  21. ctx.Data["Email"] = ctx.User.Email
  22. loadAccountData(ctx)
  23. ctx.HTML(200, tplSettingsAccount)
  24. }
  25. // AccountPost response for change user's password
  26. func AccountPost(ctx *context.Context, form auth.ChangePasswordForm) {
  27. ctx.Data["Title"] = ctx.Tr("settings")
  28. ctx.Data["PageIsSettingsAccount"] = true
  29. if ctx.HasError() {
  30. loadAccountData(ctx)
  31. ctx.HTML(200, tplSettingsAccount)
  32. return
  33. }
  34. if len(form.Password) < setting.MinPasswordLength {
  35. ctx.Flash.Error(ctx.Tr("auth.password_too_short", setting.MinPasswordLength))
  36. } else if ctx.User.IsPasswordSet() && !ctx.User.ValidatePassword(form.OldPassword) {
  37. ctx.Flash.Error(ctx.Tr("settings.password_incorrect"))
  38. } else if form.Password != form.Retype {
  39. ctx.Flash.Error(ctx.Tr("form.password_not_match"))
  40. } else {
  41. var err error
  42. if ctx.User.Salt, err = models.GetUserSalt(); err != nil {
  43. ctx.ServerError("UpdateUser", err)
  44. return
  45. }
  46. ctx.User.HashPassword(form.Password)
  47. if err := models.UpdateUserCols(ctx.User, "salt", "passwd"); err != nil {
  48. ctx.ServerError("UpdateUser", err)
  49. return
  50. }
  51. log.Trace("User password updated: %s", ctx.User.Name)
  52. ctx.Flash.Success(ctx.Tr("settings.change_password_success"))
  53. }
  54. ctx.Redirect(setting.AppSubURL + "/user/settings/account")
  55. }
  56. // EmailPost response for change user's email
  57. func EmailPost(ctx *context.Context, form auth.AddEmailForm) {
  58. ctx.Data["Title"] = ctx.Tr("settings")
  59. ctx.Data["PageIsSettingsAccount"] = true
  60. // Make emailaddress primary.
  61. if ctx.Query("_method") == "PRIMARY" {
  62. if err := models.MakeEmailPrimary(&models.EmailAddress{ID: ctx.QueryInt64("id")}); err != nil {
  63. ctx.ServerError("MakeEmailPrimary", err)
  64. return
  65. }
  66. log.Trace("Email made primary: %s", ctx.User.Name)
  67. ctx.Redirect(setting.AppSubURL + "/user/settings/account")
  68. return
  69. }
  70. if ctx.HasError() {
  71. loadAccountData(ctx)
  72. ctx.HTML(200, tplSettingsAccount)
  73. return
  74. }
  75. email := &models.EmailAddress{
  76. UID: ctx.User.ID,
  77. Email: form.Email,
  78. IsActivated: !setting.Service.RegisterEmailConfirm,
  79. }
  80. if err := models.AddEmailAddress(email); err != nil {
  81. if models.IsErrEmailAlreadyUsed(err) {
  82. loadAccountData(ctx)
  83. ctx.RenderWithErr(ctx.Tr("form.email_been_used"), tplSettingsAccount, &form)
  84. return
  85. }
  86. ctx.ServerError("AddEmailAddress", err)
  87. return
  88. }
  89. // Send confirmation email
  90. if setting.Service.RegisterEmailConfirm {
  91. models.SendActivateEmailMail(ctx.Context, ctx.User, email)
  92. if err := ctx.Cache.Put("MailResendLimit_"+ctx.User.LowerName, ctx.User.LowerName, 180); err != nil {
  93. log.Error(4, "Set cache(MailResendLimit) fail: %v", err)
  94. }
  95. ctx.Flash.Info(ctx.Tr("settings.add_email_confirmation_sent", email.Email, base.MinutesToFriendly(setting.Service.ActiveCodeLives, ctx.Locale.Language())))
  96. } else {
  97. ctx.Flash.Success(ctx.Tr("settings.add_email_success"))
  98. }
  99. log.Trace("Email address added: %s", email.Email)
  100. ctx.Redirect(setting.AppSubURL + "/user/settings/account")
  101. }
  102. // DeleteEmail response for delete user's email
  103. func DeleteEmail(ctx *context.Context) {
  104. if err := models.DeleteEmailAddress(&models.EmailAddress{ID: ctx.QueryInt64("id"), UID: ctx.User.ID}); err != nil {
  105. ctx.ServerError("DeleteEmail", err)
  106. return
  107. }
  108. log.Trace("Email address deleted: %s", ctx.User.Name)
  109. ctx.Flash.Success(ctx.Tr("settings.email_deletion_success"))
  110. ctx.JSON(200, map[string]interface{}{
  111. "redirect": setting.AppSubURL + "/user/settings/account",
  112. })
  113. }
  114. // DeleteAccount render user suicide page and response for delete user himself
  115. func DeleteAccount(ctx *context.Context) {
  116. ctx.Data["Title"] = ctx.Tr("settings")
  117. ctx.Data["PageIsSettingsAccount"] = true
  118. if _, err := models.UserSignIn(ctx.User.Name, ctx.Query("password")); err != nil {
  119. if models.IsErrUserNotExist(err) {
  120. loadAccountData(ctx)
  121. ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_password"), tplSettingsAccount, nil)
  122. } else {
  123. ctx.ServerError("UserSignIn", err)
  124. }
  125. return
  126. }
  127. if err := models.DeleteUser(ctx.User); err != nil {
  128. switch {
  129. case models.IsErrUserOwnRepos(err):
  130. ctx.Flash.Error(ctx.Tr("form.still_own_repo"))
  131. ctx.Redirect(setting.AppSubURL + "/user/settings/account")
  132. case models.IsErrUserHasOrgs(err):
  133. ctx.Flash.Error(ctx.Tr("form.still_has_org"))
  134. ctx.Redirect(setting.AppSubURL + "/user/settings/account")
  135. default:
  136. ctx.ServerError("DeleteUser", err)
  137. }
  138. } else {
  139. log.Trace("Account deleted: %s", ctx.User.Name)
  140. ctx.Redirect(setting.AppSubURL + "/")
  141. }
  142. }
  143. func loadAccountData(ctx *context.Context) {
  144. emails, err := models.GetEmailAddresses(ctx.User.ID)
  145. if err != nil {
  146. ctx.ServerError("GetEmailAddresses", err)
  147. return
  148. }
  149. ctx.Data["Emails"] = emails
  150. }