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