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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. "net/http"
  9. "time"
  10. "code.gitea.io/gitea/models"
  11. "code.gitea.io/gitea/modules/base"
  12. "code.gitea.io/gitea/modules/context"
  13. "code.gitea.io/gitea/modules/log"
  14. "code.gitea.io/gitea/modules/password"
  15. "code.gitea.io/gitea/modules/setting"
  16. "code.gitea.io/gitea/modules/timeutil"
  17. "code.gitea.io/gitea/modules/web"
  18. "code.gitea.io/gitea/services/forms"
  19. "code.gitea.io/gitea/services/mailer"
  20. )
  21. const (
  22. tplSettingsAccount base.TplName = "user/settings/account"
  23. )
  24. // Account renders change user's password, user's email and user suicide page
  25. func Account(ctx *context.Context) {
  26. ctx.Data["Title"] = ctx.Tr("settings")
  27. ctx.Data["PageIsSettingsAccount"] = true
  28. ctx.Data["Email"] = ctx.User.Email
  29. loadAccountData(ctx)
  30. ctx.HTML(http.StatusOK, tplSettingsAccount)
  31. }
  32. // AccountPost response for change user's password
  33. func AccountPost(ctx *context.Context) {
  34. form := web.GetForm(ctx).(*forms.ChangePasswordForm)
  35. ctx.Data["Title"] = ctx.Tr("settings")
  36. ctx.Data["PageIsSettingsAccount"] = true
  37. if ctx.HasError() {
  38. loadAccountData(ctx)
  39. ctx.HTML(http.StatusOK, tplSettingsAccount)
  40. return
  41. }
  42. if len(form.Password) < setting.MinPasswordLength {
  43. ctx.Flash.Error(ctx.Tr("auth.password_too_short", setting.MinPasswordLength))
  44. } else if ctx.User.IsPasswordSet() && !ctx.User.ValidatePassword(form.OldPassword) {
  45. ctx.Flash.Error(ctx.Tr("settings.password_incorrect"))
  46. } else if form.Password != form.Retype {
  47. ctx.Flash.Error(ctx.Tr("form.password_not_match"))
  48. } else if !password.IsComplexEnough(form.Password) {
  49. ctx.Flash.Error(password.BuildComplexityError(ctx))
  50. } else if pwned, err := password.IsPwned(ctx, form.Password); pwned || err != nil {
  51. errMsg := ctx.Tr("auth.password_pwned")
  52. if err != nil {
  53. log.Error(err.Error())
  54. errMsg = ctx.Tr("auth.password_pwned_err")
  55. }
  56. ctx.Flash.Error(errMsg)
  57. } else {
  58. var err error
  59. if err = ctx.User.SetPassword(form.Password); err != nil {
  60. ctx.ServerError("UpdateUser", err)
  61. return
  62. }
  63. if err := models.UpdateUserCols(ctx.User, "salt", "passwd_hash_algo", "passwd"); err != nil {
  64. ctx.ServerError("UpdateUser", err)
  65. return
  66. }
  67. log.Trace("User password updated: %s", ctx.User.Name)
  68. ctx.Flash.Success(ctx.Tr("settings.change_password_success"))
  69. }
  70. ctx.Redirect(setting.AppSubURL + "/user/settings/account")
  71. }
  72. // EmailPost response for change user's email
  73. func EmailPost(ctx *context.Context) {
  74. form := web.GetForm(ctx).(*forms.AddEmailForm)
  75. ctx.Data["Title"] = ctx.Tr("settings")
  76. ctx.Data["PageIsSettingsAccount"] = true
  77. // Make emailaddress primary.
  78. if ctx.Query("_method") == "PRIMARY" {
  79. if err := models.MakeEmailPrimary(&models.EmailAddress{ID: ctx.QueryInt64("id")}); err != nil {
  80. ctx.ServerError("MakeEmailPrimary", err)
  81. return
  82. }
  83. log.Trace("Email made primary: %s", ctx.User.Name)
  84. ctx.Redirect(setting.AppSubURL + "/user/settings/account")
  85. return
  86. }
  87. // Send activation Email
  88. if ctx.Query("_method") == "SENDACTIVATION" {
  89. var address string
  90. if ctx.Cache.IsExist("MailResendLimit_" + ctx.User.LowerName) {
  91. log.Error("Send activation: activation still pending")
  92. ctx.Redirect(setting.AppSubURL + "/user/settings/account")
  93. return
  94. }
  95. if ctx.Query("id") == "PRIMARY" {
  96. if ctx.User.IsActive {
  97. log.Error("Send activation: email not set for activation")
  98. ctx.Redirect(setting.AppSubURL + "/user/settings/account")
  99. return
  100. }
  101. mailer.SendActivateAccountMail(ctx.Locale, ctx.User)
  102. address = ctx.User.Email
  103. } else {
  104. id := ctx.QueryInt64("id")
  105. email, err := models.GetEmailAddressByID(ctx.User.ID, id)
  106. if err != nil {
  107. log.Error("GetEmailAddressByID(%d,%d) error: %v", ctx.User.ID, id, err)
  108. ctx.Redirect(setting.AppSubURL + "/user/settings/account")
  109. return
  110. }
  111. if email == nil {
  112. log.Error("Send activation: EmailAddress not found; user:%d, id: %d", ctx.User.ID, id)
  113. ctx.Redirect(setting.AppSubURL + "/user/settings/account")
  114. return
  115. }
  116. if email.IsActivated {
  117. log.Error("Send activation: email not set for activation")
  118. ctx.Redirect(setting.AppSubURL + "/user/settings/account")
  119. return
  120. }
  121. mailer.SendActivateEmailMail(ctx.User, email)
  122. address = email.Email
  123. }
  124. if err := ctx.Cache.Put("MailResendLimit_"+ctx.User.LowerName, ctx.User.LowerName, 180); err != nil {
  125. log.Error("Set cache(MailResendLimit) fail: %v", err)
  126. }
  127. ctx.Flash.Info(ctx.Tr("settings.add_email_confirmation_sent", address, timeutil.MinutesToFriendly(setting.Service.ActiveCodeLives, ctx.Locale.Language())))
  128. ctx.Redirect(setting.AppSubURL + "/user/settings/account")
  129. return
  130. }
  131. // Set Email Notification Preference
  132. if ctx.Query("_method") == "NOTIFICATION" {
  133. preference := ctx.Query("preference")
  134. if !(preference == models.EmailNotificationsEnabled ||
  135. preference == models.EmailNotificationsOnMention ||
  136. preference == models.EmailNotificationsDisabled) {
  137. log.Error("Email notifications preference change returned unrecognized option %s: %s", preference, ctx.User.Name)
  138. ctx.ServerError("SetEmailPreference", errors.New("option unrecognized"))
  139. return
  140. }
  141. if err := ctx.User.SetEmailNotifications(preference); err != nil {
  142. log.Error("Set Email Notifications failed: %v", err)
  143. ctx.ServerError("SetEmailNotifications", err)
  144. return
  145. }
  146. log.Trace("Email notifications preference made %s: %s", preference, ctx.User.Name)
  147. ctx.Flash.Success(ctx.Tr("settings.email_preference_set_success"))
  148. ctx.Redirect(setting.AppSubURL + "/user/settings/account")
  149. return
  150. }
  151. if ctx.HasError() {
  152. loadAccountData(ctx)
  153. ctx.HTML(http.StatusOK, tplSettingsAccount)
  154. return
  155. }
  156. email := &models.EmailAddress{
  157. UID: ctx.User.ID,
  158. Email: form.Email,
  159. IsActivated: !setting.Service.RegisterEmailConfirm,
  160. }
  161. if err := models.AddEmailAddress(email); err != nil {
  162. if models.IsErrEmailAlreadyUsed(err) {
  163. loadAccountData(ctx)
  164. ctx.RenderWithErr(ctx.Tr("form.email_been_used"), tplSettingsAccount, &form)
  165. return
  166. } else if models.IsErrEmailInvalid(err) {
  167. loadAccountData(ctx)
  168. ctx.RenderWithErr(ctx.Tr("form.email_invalid"), tplSettingsAccount, &form)
  169. return
  170. }
  171. ctx.ServerError("AddEmailAddress", err)
  172. return
  173. }
  174. // Send confirmation email
  175. if setting.Service.RegisterEmailConfirm {
  176. mailer.SendActivateEmailMail(ctx.User, email)
  177. if err := ctx.Cache.Put("MailResendLimit_"+ctx.User.LowerName, ctx.User.LowerName, 180); err != nil {
  178. log.Error("Set cache(MailResendLimit) fail: %v", err)
  179. }
  180. ctx.Flash.Info(ctx.Tr("settings.add_email_confirmation_sent", email.Email, timeutil.MinutesToFriendly(setting.Service.ActiveCodeLives, ctx.Locale.Language())))
  181. } else {
  182. ctx.Flash.Success(ctx.Tr("settings.add_email_success"))
  183. }
  184. log.Trace("Email address added: %s", email.Email)
  185. ctx.Redirect(setting.AppSubURL + "/user/settings/account")
  186. }
  187. // DeleteEmail response for delete user's email
  188. func DeleteEmail(ctx *context.Context) {
  189. if err := models.DeleteEmailAddress(&models.EmailAddress{ID: ctx.QueryInt64("id"), UID: ctx.User.ID}); err != nil {
  190. ctx.ServerError("DeleteEmail", err)
  191. return
  192. }
  193. log.Trace("Email address deleted: %s", ctx.User.Name)
  194. ctx.Flash.Success(ctx.Tr("settings.email_deletion_success"))
  195. ctx.JSON(http.StatusOK, map[string]interface{}{
  196. "redirect": setting.AppSubURL + "/user/settings/account",
  197. })
  198. }
  199. // DeleteAccount render user suicide page and response for delete user himself
  200. func DeleteAccount(ctx *context.Context) {
  201. ctx.Data["Title"] = ctx.Tr("settings")
  202. ctx.Data["PageIsSettingsAccount"] = true
  203. if _, err := models.UserSignIn(ctx.User.Name, ctx.Query("password")); err != nil {
  204. if models.IsErrUserNotExist(err) {
  205. loadAccountData(ctx)
  206. ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_password"), tplSettingsAccount, nil)
  207. } else {
  208. ctx.ServerError("UserSignIn", err)
  209. }
  210. return
  211. }
  212. if err := models.DeleteUser(ctx.User); err != nil {
  213. switch {
  214. case models.IsErrUserOwnRepos(err):
  215. ctx.Flash.Error(ctx.Tr("form.still_own_repo"))
  216. ctx.Redirect(setting.AppSubURL + "/user/settings/account")
  217. case models.IsErrUserHasOrgs(err):
  218. ctx.Flash.Error(ctx.Tr("form.still_has_org"))
  219. ctx.Redirect(setting.AppSubURL + "/user/settings/account")
  220. default:
  221. ctx.ServerError("DeleteUser", err)
  222. }
  223. } else {
  224. log.Trace("Account deleted: %s", ctx.User.Name)
  225. ctx.Redirect(setting.AppSubURL + "/")
  226. }
  227. }
  228. // UpdateUIThemePost is used to update users' specific theme
  229. func UpdateUIThemePost(ctx *context.Context) {
  230. form := web.GetForm(ctx).(*forms.UpdateThemeForm)
  231. ctx.Data["Title"] = ctx.Tr("settings")
  232. ctx.Data["PageIsSettingsAccount"] = true
  233. if ctx.HasError() {
  234. ctx.Redirect(setting.AppSubURL + "/user/settings/account")
  235. return
  236. }
  237. if !form.IsThemeExists() {
  238. ctx.Flash.Error(ctx.Tr("settings.theme_update_error"))
  239. ctx.Redirect(setting.AppSubURL + "/user/settings/account")
  240. return
  241. }
  242. if err := ctx.User.UpdateTheme(form.Theme); err != nil {
  243. ctx.Flash.Error(ctx.Tr("settings.theme_update_error"))
  244. ctx.Redirect(setting.AppSubURL + "/user/settings/account")
  245. return
  246. }
  247. log.Trace("Update user theme: %s", ctx.User.Name)
  248. ctx.Flash.Success(ctx.Tr("settings.theme_update_success"))
  249. ctx.Redirect(setting.AppSubURL + "/user/settings/account")
  250. }
  251. func loadAccountData(ctx *context.Context) {
  252. emlist, err := models.GetEmailAddresses(ctx.User.ID)
  253. if err != nil {
  254. ctx.ServerError("GetEmailAddresses", err)
  255. return
  256. }
  257. type UserEmail struct {
  258. models.EmailAddress
  259. CanBePrimary bool
  260. }
  261. pendingActivation := ctx.Cache.IsExist("MailResendLimit_" + ctx.User.LowerName)
  262. emails := make([]*UserEmail, len(emlist))
  263. for i, em := range emlist {
  264. var email UserEmail
  265. email.EmailAddress = *em
  266. email.CanBePrimary = em.IsActivated
  267. emails[i] = &email
  268. }
  269. ctx.Data["Emails"] = emails
  270. ctx.Data["EmailNotificationsPreference"] = ctx.User.EmailNotifications()
  271. ctx.Data["ActivationsPending"] = pendingActivation
  272. ctx.Data["CanAddEmails"] = !pendingActivation || !setting.Service.RegisterEmailConfirm
  273. if setting.Service.UserDeleteWithCommentsMaxTime != 0 {
  274. ctx.Data["UserDeleteWithCommentsMaxTime"] = setting.Service.UserDeleteWithCommentsMaxTime.String()
  275. ctx.Data["UserDeleteWithComments"] = ctx.User.CreatedUnix.AsTime().Add(setting.Service.UserDeleteWithCommentsMaxTime).After(time.Now())
  276. }
  277. }