You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

account.go 7.2 kB

Better logging (#6038) (#6095) * Panic don't fatal on create new logger Fixes #5854 Signed-off-by: Andrew Thornton <art27@cantab.net> * partial broken * Update the logging infrastrcture Signed-off-by: Andrew Thornton <art27@cantab.net> * Reset the skip levels for Fatal and Error Signed-off-by: Andrew Thornton <art27@cantab.net> * broken ncsa * More log.Error fixes Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove nal * set log-levels to lowercase * Make console_test test all levels * switch to lowercased levels * OK now working * Fix vetting issues * Fix lint * Fix tests * change default logging to match current gitea * Improve log testing Signed-off-by: Andrew Thornton <art27@cantab.net> * reset error skip levels to 0 * Update documentation and access logger configuration * Redirect the router log back to gitea if redirect macaron log but also allow setting the log level - i.e. TRACE * Fix broken level caching * Refactor the router log * Add Router logger * Add colorizing options * Adjust router colors * Only create logger if they will be used * update app.ini.sample * rename Attribute ColorAttribute * Change from white to green for function * Set fatal/error levels * Restore initial trace logger * Fix Trace arguments in modules/auth/auth.go * Properly handle XORMLogger * Improve admin/config page * fix fmt * Add auto-compression of old logs * Update error log levels * Remove the unnecessary skip argument from Error, Fatal and Critical * Add stacktrace support * Fix tests * Remove x/sync from vendors? * Add stderr option to console logger * Use filepath.ToSlash to protect against Windows in tests * Remove prefixed underscores from names in colors.go * Remove not implemented database logger This was removed from Gogs on 4 Mar 2016 but left in the configuration since then. * Ensure that log paths are relative to ROOT_PATH * use path.Join * rename jsonConfig to logConfig * Rename "config" to "jsonConfig" to make it clearer * Requested changes * Requested changes: XormLogger * Try to color the windows terminal If successful default to colorizing the console logs * fixup * Colorize initially too * update vendor * Colorize logs on default and remove if this is not a colorizing logger * Fix documentation * fix test * Use go-isatty to detect if on windows we are on msys or cygwin * Fix spelling mistake * Add missing vendors * More changes * Rationalise the ANSI writer protection * Adjust colors on advice from @0x5c * Make Flags a comma separated list * Move to use the windows constant for ENABLE_VIRTUAL_TERMINAL_PROCESSING * Ensure matching is done on the non-colored message - to simpify EXPRESSION
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. "code.gitea.io/gitea/services/mailer"
  16. )
  17. const (
  18. tplSettingsAccount base.TplName = "user/settings/account"
  19. )
  20. // Account renders change user's password, user's email and user suicide page
  21. func Account(ctx *context.Context) {
  22. ctx.Data["Title"] = ctx.Tr("settings")
  23. ctx.Data["PageIsSettingsAccount"] = true
  24. ctx.Data["Email"] = ctx.User.Email
  25. ctx.Data["EmailNotificationsPreference"] = ctx.User.EmailNotifications()
  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 {
  45. var err error
  46. if ctx.User.Salt, err = models.GetUserSalt(); err != nil {
  47. ctx.ServerError("UpdateUser", err)
  48. return
  49. }
  50. ctx.User.HashPassword(form.Password)
  51. if err := models.UpdateUserCols(ctx.User, "salt", "passwd"); err != nil {
  52. ctx.ServerError("UpdateUser", err)
  53. return
  54. }
  55. log.Trace("User password updated: %s", ctx.User.Name)
  56. ctx.Flash.Success(ctx.Tr("settings.change_password_success"))
  57. }
  58. ctx.Redirect(setting.AppSubURL + "/user/settings/account")
  59. }
  60. // EmailPost response for change user's email
  61. func EmailPost(ctx *context.Context, form auth.AddEmailForm) {
  62. ctx.Data["Title"] = ctx.Tr("settings")
  63. ctx.Data["PageIsSettingsAccount"] = true
  64. // Make emailaddress primary.
  65. if ctx.Query("_method") == "PRIMARY" {
  66. if err := models.MakeEmailPrimary(&models.EmailAddress{ID: ctx.QueryInt64("id")}); err != nil {
  67. ctx.ServerError("MakeEmailPrimary", err)
  68. return
  69. }
  70. log.Trace("Email made primary: %s", ctx.User.Name)
  71. ctx.Redirect(setting.AppSubURL + "/user/settings/account")
  72. return
  73. }
  74. // Set Email Notification Preference
  75. if ctx.Query("_method") == "NOTIFICATION" {
  76. preference := ctx.Query("preference")
  77. if !(preference == models.EmailNotificationsEnabled ||
  78. preference == models.EmailNotificationsOnMention ||
  79. preference == models.EmailNotificationsDisabled) {
  80. log.Error("Email notifications preference change returned unrecognized option %s: %s", preference, ctx.User.Name)
  81. ctx.ServerError("SetEmailPreference", errors.New("option unrecognized"))
  82. return
  83. }
  84. if err := ctx.User.SetEmailNotifications(preference); err != nil {
  85. log.Error("Set Email Notifications failed: %v", err)
  86. ctx.ServerError("SetEmailNotifications", err)
  87. return
  88. }
  89. log.Trace("Email notifications preference made %s: %s", preference, ctx.User.Name)
  90. ctx.Redirect(setting.AppSubURL + "/user/settings/account")
  91. return
  92. }
  93. if ctx.HasError() {
  94. loadAccountData(ctx)
  95. ctx.HTML(200, tplSettingsAccount)
  96. return
  97. }
  98. email := &models.EmailAddress{
  99. UID: ctx.User.ID,
  100. Email: form.Email,
  101. IsActivated: !setting.Service.RegisterEmailConfirm,
  102. }
  103. if err := models.AddEmailAddress(email); err != nil {
  104. if models.IsErrEmailAlreadyUsed(err) {
  105. loadAccountData(ctx)
  106. ctx.RenderWithErr(ctx.Tr("form.email_been_used"), tplSettingsAccount, &form)
  107. return
  108. }
  109. ctx.ServerError("AddEmailAddress", err)
  110. return
  111. }
  112. // Send confirmation email
  113. if setting.Service.RegisterEmailConfirm {
  114. mailer.SendActivateEmailMail(ctx.Locale, ctx.User, email)
  115. if err := ctx.Cache.Put("MailResendLimit_"+ctx.User.LowerName, ctx.User.LowerName, 180); err != nil {
  116. log.Error("Set cache(MailResendLimit) fail: %v", err)
  117. }
  118. ctx.Flash.Info(ctx.Tr("settings.add_email_confirmation_sent", email.Email, timeutil.MinutesToFriendly(setting.Service.ActiveCodeLives, ctx.Locale.Language())))
  119. } else {
  120. ctx.Flash.Success(ctx.Tr("settings.add_email_success"))
  121. }
  122. log.Trace("Email address added: %s", email.Email)
  123. ctx.Redirect(setting.AppSubURL + "/user/settings/account")
  124. }
  125. // DeleteEmail response for delete user's email
  126. func DeleteEmail(ctx *context.Context) {
  127. if err := models.DeleteEmailAddress(&models.EmailAddress{ID: ctx.QueryInt64("id"), UID: ctx.User.ID}); err != nil {
  128. ctx.ServerError("DeleteEmail", err)
  129. return
  130. }
  131. log.Trace("Email address deleted: %s", ctx.User.Name)
  132. ctx.Flash.Success(ctx.Tr("settings.email_deletion_success"))
  133. ctx.JSON(200, map[string]interface{}{
  134. "redirect": setting.AppSubURL + "/user/settings/account",
  135. })
  136. }
  137. // DeleteAccount render user suicide page and response for delete user himself
  138. func DeleteAccount(ctx *context.Context) {
  139. ctx.Data["Title"] = ctx.Tr("settings")
  140. ctx.Data["PageIsSettingsAccount"] = true
  141. if _, err := models.UserSignIn(ctx.User.Name, ctx.Query("password")); err != nil {
  142. if models.IsErrUserNotExist(err) {
  143. loadAccountData(ctx)
  144. ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_password"), tplSettingsAccount, nil)
  145. } else {
  146. ctx.ServerError("UserSignIn", err)
  147. }
  148. return
  149. }
  150. if err := models.DeleteUser(ctx.User); err != nil {
  151. switch {
  152. case models.IsErrUserOwnRepos(err):
  153. ctx.Flash.Error(ctx.Tr("form.still_own_repo"))
  154. ctx.Redirect(setting.AppSubURL + "/user/settings/account")
  155. case models.IsErrUserHasOrgs(err):
  156. ctx.Flash.Error(ctx.Tr("form.still_has_org"))
  157. ctx.Redirect(setting.AppSubURL + "/user/settings/account")
  158. default:
  159. ctx.ServerError("DeleteUser", err)
  160. }
  161. } else {
  162. log.Trace("Account deleted: %s", ctx.User.Name)
  163. ctx.Redirect(setting.AppSubURL + "/")
  164. }
  165. }
  166. // UpdateUIThemePost is used to update users' specific theme
  167. func UpdateUIThemePost(ctx *context.Context, form auth.UpdateThemeForm) {
  168. ctx.Data["Title"] = ctx.Tr("settings")
  169. ctx.Data["PageIsSettingsAccount"] = true
  170. if ctx.HasError() {
  171. ctx.Redirect(setting.AppSubURL + "/user/settings/account")
  172. return
  173. }
  174. if !form.IsThemeExists() {
  175. ctx.Flash.Error(ctx.Tr("settings.theme_update_error"))
  176. ctx.Redirect(setting.AppSubURL + "/user/settings/account")
  177. return
  178. }
  179. if err := ctx.User.UpdateTheme(form.Theme); err != nil {
  180. ctx.Flash.Error(ctx.Tr("settings.theme_update_error"))
  181. ctx.Redirect(setting.AppSubURL + "/user/settings/account")
  182. return
  183. }
  184. log.Trace("Update user theme: %s", ctx.User.Name)
  185. ctx.Flash.Success(ctx.Tr("settings.theme_update_success"))
  186. ctx.Redirect(setting.AppSubURL + "/user/settings/account")
  187. }
  188. func loadAccountData(ctx *context.Context) {
  189. emails, err := models.GetEmailAddresses(ctx.User.ID)
  190. if err != nil {
  191. ctx.ServerError("GetEmailAddresses", err)
  192. return
  193. }
  194. ctx.Data["Emails"] = emails
  195. }