Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

account.go 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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(password.BuildComplexityError(ctx))
  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. // Send activation Email
  77. if ctx.Query("_method") == "SENDACTIVATION" {
  78. var address string
  79. if ctx.Cache.IsExist("MailResendLimit_" + ctx.User.LowerName) {
  80. log.Error("Send activation: activation still pending")
  81. ctx.Redirect(setting.AppSubURL + "/user/settings/account")
  82. return
  83. }
  84. if ctx.Query("id") == "PRIMARY" {
  85. if ctx.User.IsActive {
  86. log.Error("Send activation: email not set for activation")
  87. ctx.Redirect(setting.AppSubURL + "/user/settings/account")
  88. return
  89. }
  90. mailer.SendActivateAccountMail(ctx.Locale, ctx.User)
  91. address = ctx.User.Email
  92. } else {
  93. id := ctx.QueryInt64("id")
  94. email, err := models.GetEmailAddressByID(ctx.User.ID, id)
  95. if err != nil {
  96. log.Error("GetEmailAddressByID(%d,%d) error: %v", ctx.User.ID, id, err)
  97. ctx.Redirect(setting.AppSubURL + "/user/settings/account")
  98. return
  99. }
  100. if email == nil {
  101. log.Error("Send activation: EmailAddress not found; user:%d, id: %d", ctx.User.ID, id)
  102. ctx.Redirect(setting.AppSubURL + "/user/settings/account")
  103. return
  104. }
  105. if email.IsActivated {
  106. log.Error("Send activation: email not set for activation")
  107. ctx.Redirect(setting.AppSubURL + "/user/settings/account")
  108. return
  109. }
  110. mailer.SendActivateEmailMail(ctx.Locale, ctx.User, email)
  111. address = email.Email
  112. }
  113. if err := ctx.Cache.Put("MailResendLimit_"+ctx.User.LowerName, ctx.User.LowerName, 180); err != nil {
  114. log.Error("Set cache(MailResendLimit) fail: %v", err)
  115. }
  116. ctx.Flash.Info(ctx.Tr("settings.add_email_confirmation_sent", address, timeutil.MinutesToFriendly(setting.Service.ActiveCodeLives, ctx.Locale.Language())))
  117. ctx.Redirect(setting.AppSubURL + "/user/settings/account")
  118. return
  119. }
  120. // Set Email Notification Preference
  121. if ctx.Query("_method") == "NOTIFICATION" {
  122. preference := ctx.Query("preference")
  123. if !(preference == models.EmailNotificationsEnabled ||
  124. preference == models.EmailNotificationsOnMention ||
  125. preference == models.EmailNotificationsDisabled) {
  126. log.Error("Email notifications preference change returned unrecognized option %s: %s", preference, ctx.User.Name)
  127. ctx.ServerError("SetEmailPreference", errors.New("option unrecognized"))
  128. return
  129. }
  130. if err := ctx.User.SetEmailNotifications(preference); err != nil {
  131. log.Error("Set Email Notifications failed: %v", err)
  132. ctx.ServerError("SetEmailNotifications", err)
  133. return
  134. }
  135. log.Trace("Email notifications preference made %s: %s", preference, ctx.User.Name)
  136. ctx.Redirect(setting.AppSubURL + "/user/settings/account")
  137. return
  138. }
  139. if ctx.HasError() {
  140. loadAccountData(ctx)
  141. ctx.HTML(200, tplSettingsAccount)
  142. return
  143. }
  144. email := &models.EmailAddress{
  145. UID: ctx.User.ID,
  146. Email: form.Email,
  147. IsActivated: !setting.Service.RegisterEmailConfirm,
  148. }
  149. if err := models.AddEmailAddress(email); err != nil {
  150. if models.IsErrEmailAlreadyUsed(err) {
  151. loadAccountData(ctx)
  152. ctx.RenderWithErr(ctx.Tr("form.email_been_used"), tplSettingsAccount, &form)
  153. return
  154. }
  155. ctx.ServerError("AddEmailAddress", err)
  156. return
  157. }
  158. // Send confirmation email
  159. if setting.Service.RegisterEmailConfirm {
  160. mailer.SendActivateEmailMail(ctx.Locale, ctx.User, email)
  161. if err := ctx.Cache.Put("MailResendLimit_"+ctx.User.LowerName, ctx.User.LowerName, 180); err != nil {
  162. log.Error("Set cache(MailResendLimit) fail: %v", err)
  163. }
  164. ctx.Flash.Info(ctx.Tr("settings.add_email_confirmation_sent", email.Email, timeutil.MinutesToFriendly(setting.Service.ActiveCodeLives, ctx.Locale.Language())))
  165. } else {
  166. ctx.Flash.Success(ctx.Tr("settings.add_email_success"))
  167. }
  168. log.Trace("Email address added: %s", email.Email)
  169. ctx.Redirect(setting.AppSubURL + "/user/settings/account")
  170. }
  171. // DeleteEmail response for delete user's email
  172. func DeleteEmail(ctx *context.Context) {
  173. if err := models.DeleteEmailAddress(&models.EmailAddress{ID: ctx.QueryInt64("id"), UID: ctx.User.ID}); err != nil {
  174. ctx.ServerError("DeleteEmail", err)
  175. return
  176. }
  177. log.Trace("Email address deleted: %s", ctx.User.Name)
  178. ctx.Flash.Success(ctx.Tr("settings.email_deletion_success"))
  179. ctx.JSON(200, map[string]interface{}{
  180. "redirect": setting.AppSubURL + "/user/settings/account",
  181. })
  182. }
  183. // DeleteAccount render user suicide page and response for delete user himself
  184. func DeleteAccount(ctx *context.Context) {
  185. ctx.Data["Title"] = ctx.Tr("settings")
  186. ctx.Data["PageIsSettingsAccount"] = true
  187. if _, err := models.UserSignIn(ctx.User.Name, ctx.Query("password")); err != nil {
  188. if models.IsErrUserNotExist(err) {
  189. loadAccountData(ctx)
  190. ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_password"), tplSettingsAccount, nil)
  191. } else {
  192. ctx.ServerError("UserSignIn", err)
  193. }
  194. return
  195. }
  196. if err := models.DeleteUser(ctx.User); err != nil {
  197. switch {
  198. case models.IsErrUserOwnRepos(err):
  199. ctx.Flash.Error(ctx.Tr("form.still_own_repo"))
  200. ctx.Redirect(setting.AppSubURL + "/user/settings/account")
  201. case models.IsErrUserHasOrgs(err):
  202. ctx.Flash.Error(ctx.Tr("form.still_has_org"))
  203. ctx.Redirect(setting.AppSubURL + "/user/settings/account")
  204. default:
  205. ctx.ServerError("DeleteUser", err)
  206. }
  207. } else {
  208. log.Trace("Account deleted: %s", ctx.User.Name)
  209. ctx.Redirect(setting.AppSubURL + "/")
  210. }
  211. }
  212. // UpdateUIThemePost is used to update users' specific theme
  213. func UpdateUIThemePost(ctx *context.Context, form auth.UpdateThemeForm) {
  214. ctx.Data["Title"] = ctx.Tr("settings")
  215. ctx.Data["PageIsSettingsAccount"] = true
  216. if ctx.HasError() {
  217. ctx.Redirect(setting.AppSubURL + "/user/settings/account")
  218. return
  219. }
  220. if !form.IsThemeExists() {
  221. ctx.Flash.Error(ctx.Tr("settings.theme_update_error"))
  222. ctx.Redirect(setting.AppSubURL + "/user/settings/account")
  223. return
  224. }
  225. if err := ctx.User.UpdateTheme(form.Theme); err != nil {
  226. ctx.Flash.Error(ctx.Tr("settings.theme_update_error"))
  227. ctx.Redirect(setting.AppSubURL + "/user/settings/account")
  228. return
  229. }
  230. log.Trace("Update user theme: %s", ctx.User.Name)
  231. ctx.Flash.Success(ctx.Tr("settings.theme_update_success"))
  232. ctx.Redirect(setting.AppSubURL + "/user/settings/account")
  233. }
  234. func loadAccountData(ctx *context.Context) {
  235. emlist, err := models.GetEmailAddresses(ctx.User.ID)
  236. if err != nil {
  237. ctx.ServerError("GetEmailAddresses", err)
  238. return
  239. }
  240. type UserEmail struct {
  241. models.EmailAddress
  242. CanBePrimary bool
  243. }
  244. pendingActivation := ctx.Cache.IsExist("MailResendLimit_" + ctx.User.LowerName)
  245. emails := make([]*UserEmail, len(emlist))
  246. for i, em := range emlist {
  247. var email UserEmail
  248. email.EmailAddress = *em
  249. email.CanBePrimary = em.IsActivated
  250. emails[i] = &email
  251. }
  252. ctx.Data["Emails"] = emails
  253. ctx.Data["EmailNotificationsPreference"] = ctx.User.EmailNotifications()
  254. ctx.Data["ActivationsPending"] = pendingActivation
  255. ctx.Data["CanAddEmails"] = !pendingActivation || !setting.Service.RegisterEmailConfirm
  256. }