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.

account.go 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. "errors"
  8. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/modules/auth"
  10. "code.gitea.io/gitea/modules/base"
  11. "code.gitea.io/gitea/modules/context"
  12. "code.gitea.io/gitea/modules/log"
  13. "code.gitea.io/gitea/modules/setting"
  14. "code.gitea.io/gitea/modules/timeutil"
  15. )
  16. const (
  17. tplSettingsAccount base.TplName = "user/settings/account"
  18. )
  19. // Account renders change user's password, user's email and user suicide page
  20. func Account(ctx *context.Context) {
  21. ctx.Data["Title"] = ctx.Tr("settings")
  22. ctx.Data["PageIsSettingsAccount"] = true
  23. ctx.Data["Email"] = ctx.User.Email
  24. ctx.Data["EmailNotificationsPreference"] = ctx.User.EmailNotifications()
  25. loadAccountData(ctx)
  26. ctx.HTML(200, tplSettingsAccount)
  27. }
  28. // AccountPost response for change user's password
  29. func AccountPost(ctx *context.Context, form auth.ChangePasswordForm) {
  30. ctx.Data["Title"] = ctx.Tr("settings")
  31. ctx.Data["PageIsSettingsAccount"] = true
  32. if ctx.HasError() {
  33. loadAccountData(ctx)
  34. ctx.HTML(200, tplSettingsAccount)
  35. return
  36. }
  37. if len(form.Password) < setting.MinPasswordLength {
  38. ctx.Flash.Error(ctx.Tr("auth.password_too_short", setting.MinPasswordLength))
  39. } else if ctx.User.IsPasswordSet() && !ctx.User.ValidatePassword(form.OldPassword) {
  40. ctx.Flash.Error(ctx.Tr("settings.password_incorrect"))
  41. } else if form.Password != form.Retype {
  42. ctx.Flash.Error(ctx.Tr("form.password_not_match"))
  43. } else {
  44. var err error
  45. if ctx.User.Salt, err = models.GetUserSalt(); err != nil {
  46. ctx.ServerError("UpdateUser", err)
  47. return
  48. }
  49. ctx.User.HashPassword(form.Password)
  50. if err := models.UpdateUserCols(ctx.User, "salt", "passwd"); err != nil {
  51. ctx.ServerError("UpdateUser", err)
  52. return
  53. }
  54. log.Trace("User password updated: %s", ctx.User.Name)
  55. ctx.Flash.Success(ctx.Tr("settings.change_password_success"))
  56. }
  57. ctx.Redirect(setting.AppSubURL + "/user/settings/account")
  58. }
  59. // EmailPost response for change user's email
  60. func EmailPost(ctx *context.Context, form auth.AddEmailForm) {
  61. ctx.Data["Title"] = ctx.Tr("settings")
  62. ctx.Data["PageIsSettingsAccount"] = true
  63. // Make emailaddress primary.
  64. if ctx.Query("_method") == "PRIMARY" {
  65. if err := models.MakeEmailPrimary(&models.EmailAddress{ID: ctx.QueryInt64("id")}); err != nil {
  66. ctx.ServerError("MakeEmailPrimary", err)
  67. return
  68. }
  69. log.Trace("Email made primary: %s", ctx.User.Name)
  70. ctx.Redirect(setting.AppSubURL + "/user/settings/account")
  71. return
  72. }
  73. // Set Email Notification Preference
  74. if ctx.Query("_method") == "NOTIFICATION" {
  75. preference := ctx.Query("preference")
  76. if !(preference == models.EmailNotificationsEnabled ||
  77. preference == models.EmailNotificationsOnMention ||
  78. preference == models.EmailNotificationsDisabled) {
  79. log.Error("Email notifications preference change returned unrecognized option %s: %s", preference, ctx.User.Name)
  80. ctx.ServerError("SetEmailPreference", errors.New("option unrecognized"))
  81. return
  82. }
  83. if err := ctx.User.SetEmailNotifications(preference); err != nil {
  84. log.Error("Set Email Notifications failed: %v", err)
  85. ctx.ServerError("SetEmailNotifications", err)
  86. return
  87. }
  88. log.Trace("Email notifications preference made %s: %s", preference, ctx.User.Name)
  89. ctx.Redirect(setting.AppSubURL + "/user/settings/account")
  90. return
  91. }
  92. if ctx.HasError() {
  93. loadAccountData(ctx)
  94. ctx.HTML(200, tplSettingsAccount)
  95. return
  96. }
  97. email := &models.EmailAddress{
  98. UID: ctx.User.ID,
  99. Email: form.Email,
  100. IsActivated: !setting.Service.RegisterEmailConfirm,
  101. }
  102. if err := models.AddEmailAddress(email); err != nil {
  103. if models.IsErrEmailAlreadyUsed(err) {
  104. loadAccountData(ctx)
  105. ctx.RenderWithErr(ctx.Tr("form.email_been_used"), tplSettingsAccount, &form)
  106. return
  107. }
  108. ctx.ServerError("AddEmailAddress", err)
  109. return
  110. }
  111. // Send confirmation email
  112. if setting.Service.RegisterEmailConfirm {
  113. models.SendActivateEmailMail(ctx.Context, ctx.User, email)
  114. if err := ctx.Cache.Put("MailResendLimit_"+ctx.User.LowerName, ctx.User.LowerName, 180); err != nil {
  115. log.Error("Set cache(MailResendLimit) fail: %v", err)
  116. }
  117. ctx.Flash.Info(ctx.Tr("settings.add_email_confirmation_sent", email.Email, timeutil.MinutesToFriendly(setting.Service.ActiveCodeLives, ctx.Locale.Language())))
  118. } else {
  119. ctx.Flash.Success(ctx.Tr("settings.add_email_success"))
  120. }
  121. log.Trace("Email address added: %s", email.Email)
  122. ctx.Redirect(setting.AppSubURL + "/user/settings/account")
  123. }
  124. // DeleteEmail response for delete user's email
  125. func DeleteEmail(ctx *context.Context) {
  126. if err := models.DeleteEmailAddress(&models.EmailAddress{ID: ctx.QueryInt64("id"), UID: ctx.User.ID}); err != nil {
  127. ctx.ServerError("DeleteEmail", err)
  128. return
  129. }
  130. log.Trace("Email address deleted: %s", ctx.User.Name)
  131. ctx.Flash.Success(ctx.Tr("settings.email_deletion_success"))
  132. ctx.JSON(200, map[string]interface{}{
  133. "redirect": setting.AppSubURL + "/user/settings/account",
  134. })
  135. }
  136. // DeleteAccount render user suicide page and response for delete user himself
  137. func DeleteAccount(ctx *context.Context) {
  138. ctx.Data["Title"] = ctx.Tr("settings")
  139. ctx.Data["PageIsSettingsAccount"] = true
  140. if _, err := models.UserSignIn(ctx.User.Name, ctx.Query("password")); err != nil {
  141. if models.IsErrUserNotExist(err) {
  142. loadAccountData(ctx)
  143. ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_password"), tplSettingsAccount, nil)
  144. } else {
  145. ctx.ServerError("UserSignIn", err)
  146. }
  147. return
  148. }
  149. if err := models.DeleteUser(ctx.User); err != nil {
  150. switch {
  151. case models.IsErrUserOwnRepos(err):
  152. ctx.Flash.Error(ctx.Tr("form.still_own_repo"))
  153. ctx.Redirect(setting.AppSubURL + "/user/settings/account")
  154. case models.IsErrUserHasOrgs(err):
  155. ctx.Flash.Error(ctx.Tr("form.still_has_org"))
  156. ctx.Redirect(setting.AppSubURL + "/user/settings/account")
  157. default:
  158. ctx.ServerError("DeleteUser", err)
  159. }
  160. } else {
  161. log.Trace("Account deleted: %s", ctx.User.Name)
  162. ctx.Redirect(setting.AppSubURL + "/")
  163. }
  164. }
  165. // UpdateUIThemePost is used to update users' specific theme
  166. func UpdateUIThemePost(ctx *context.Context, form auth.UpdateThemeForm) {
  167. ctx.Data["Title"] = ctx.Tr("settings")
  168. ctx.Data["PageIsSettingsAccount"] = true
  169. if ctx.HasError() {
  170. ctx.Redirect(setting.AppSubURL + "/user/settings/account")
  171. return
  172. }
  173. if !form.IsThemeExists() {
  174. ctx.Flash.Error(ctx.Tr("settings.theme_update_error"))
  175. ctx.Redirect(setting.AppSubURL + "/user/settings/account")
  176. return
  177. }
  178. if err := ctx.User.UpdateTheme(form.Theme); err != nil {
  179. ctx.Flash.Error(ctx.Tr("settings.theme_update_error"))
  180. ctx.Redirect(setting.AppSubURL + "/user/settings/account")
  181. return
  182. }
  183. log.Trace("Update user theme: %s", ctx.User.Name)
  184. ctx.Flash.Success(ctx.Tr("settings.theme_update_success"))
  185. ctx.Redirect(setting.AppSubURL + "/user/settings/account")
  186. }
  187. func loadAccountData(ctx *context.Context) {
  188. emails, err := models.GetEmailAddresses(ctx.User.ID)
  189. if err != nil {
  190. ctx.ServerError("GetEmailAddresses", err)
  191. return
  192. }
  193. ctx.Data["Emails"] = emails
  194. }