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.2KB

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