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.

470 lines
14 KiB

10 years ago
10 years ago
9 years ago
10 years ago
10 years ago
10 years ago
Add support for federated avatars (#3320) * Add support for federated avatars Fixes #3105 Removes avatar fetching duplication code Adds an "Enable Federated Avatar" checkbox in user settings (defaults to unchecked) Moves avatar settings all in the same form, making local and remote avatars mutually exclusive Renames UploadAvatarForm to AvatarForm as it's not anymore only for uploading * Run gofmt on all modified files * Move Avatar form in its own page * Add go-libravatar dependency to vendor/ dir Hopefully helps with accepting the contribution. See also #3214 * Revert "Add go-libravatar dependency to vendor/ dir" This reverts commit a8cb93ae640bbb90f7d25012fc257bda9fae9b82. * Make federated avatar setting a global configuration Removes the per-user setting * Move avatar handling back to base tool, disable federated avatar in offline mode * Format, handle error * Properly set fallback host * Use unsupported github.com mirror for importing go-libravatar * Remove comment showing life exists outside of github.com ... pity, but contribution would not be accepted otherwise * Use Combo for Get and Post methods over /avatar * FEDERATED_AVATAR -> ENABLE_FEDERATED_AVATAR * Fix persistance of federated avatar lookup checkbox at install time * Federated Avatars -> Enable Federated Avatars * Use len(string) == 0 instead of string == "" * Move import line where it belong See https://github.com/Unknwon/go-code-convention/blob/master/en-US/import_packages.md Pity the import url is still the unofficial one, but oh well... * Save a line (and waste much more expensive time) * Remove redundant parens * Remove an empty line * Remove empty lines * Reorder lines to make diff smaller * Remove another newline Unknwon review got me start a fight against newlines * Move DISABLE_GRAVATAR and ENABLE_FEDERATED_AVATAR after OFFLINE_MODE On re-reading the diff I figured what Unknwon meant here: https://github.com/gogits/gogs/pull/3320/files#r73741106 * Remove newlines that weren't there before my intervention
8 years ago
8 years ago
Add support for federated avatars (#3320) * Add support for federated avatars Fixes #3105 Removes avatar fetching duplication code Adds an "Enable Federated Avatar" checkbox in user settings (defaults to unchecked) Moves avatar settings all in the same form, making local and remote avatars mutually exclusive Renames UploadAvatarForm to AvatarForm as it's not anymore only for uploading * Run gofmt on all modified files * Move Avatar form in its own page * Add go-libravatar dependency to vendor/ dir Hopefully helps with accepting the contribution. See also #3214 * Revert "Add go-libravatar dependency to vendor/ dir" This reverts commit a8cb93ae640bbb90f7d25012fc257bda9fae9b82. * Make federated avatar setting a global configuration Removes the per-user setting * Move avatar handling back to base tool, disable federated avatar in offline mode * Format, handle error * Properly set fallback host * Use unsupported github.com mirror for importing go-libravatar * Remove comment showing life exists outside of github.com ... pity, but contribution would not be accepted otherwise * Use Combo for Get and Post methods over /avatar * FEDERATED_AVATAR -> ENABLE_FEDERATED_AVATAR * Fix persistance of federated avatar lookup checkbox at install time * Federated Avatars -> Enable Federated Avatars * Use len(string) == 0 instead of string == "" * Move import line where it belong See https://github.com/Unknwon/go-code-convention/blob/master/en-US/import_packages.md Pity the import url is still the unofficial one, but oh well... * Save a line (and waste much more expensive time) * Remove redundant parens * Remove an empty line * Remove empty lines * Reorder lines to make diff smaller * Remove another newline Unknwon review got me start a fight against newlines * Move DISABLE_GRAVATAR and ENABLE_FEDERATED_AVATAR after OFFLINE_MODE On re-reading the diff I figured what Unknwon meant here: https://github.com/gogits/gogs/pull/3320/files#r73741106 * Remove newlines that weren't there before my intervention
8 years ago
10 years ago
Add support for federated avatars (#3320) * Add support for federated avatars Fixes #3105 Removes avatar fetching duplication code Adds an "Enable Federated Avatar" checkbox in user settings (defaults to unchecked) Moves avatar settings all in the same form, making local and remote avatars mutually exclusive Renames UploadAvatarForm to AvatarForm as it's not anymore only for uploading * Run gofmt on all modified files * Move Avatar form in its own page * Add go-libravatar dependency to vendor/ dir Hopefully helps with accepting the contribution. See also #3214 * Revert "Add go-libravatar dependency to vendor/ dir" This reverts commit a8cb93ae640bbb90f7d25012fc257bda9fae9b82. * Make federated avatar setting a global configuration Removes the per-user setting * Move avatar handling back to base tool, disable federated avatar in offline mode * Format, handle error * Properly set fallback host * Use unsupported github.com mirror for importing go-libravatar * Remove comment showing life exists outside of github.com ... pity, but contribution would not be accepted otherwise * Use Combo for Get and Post methods over /avatar * FEDERATED_AVATAR -> ENABLE_FEDERATED_AVATAR * Fix persistance of federated avatar lookup checkbox at install time * Federated Avatars -> Enable Federated Avatars * Use len(string) == 0 instead of string == "" * Move import line where it belong See https://github.com/Unknwon/go-code-convention/blob/master/en-US/import_packages.md Pity the import url is still the unofficial one, but oh well... * Save a line (and waste much more expensive time) * Remove redundant parens * Remove an empty line * Remove empty lines * Reorder lines to make diff smaller * Remove another newline Unknwon review got me start a fight against newlines * Move DISABLE_GRAVATAR and ENABLE_FEDERATED_AVATAR after OFFLINE_MODE On re-reading the diff I figured what Unknwon meant here: https://github.com/gogits/gogs/pull/3320/files#r73741106 * Remove newlines that weren't there before my intervention
8 years ago
10 years ago
10 years ago
Add support for federated avatars (#3320) * Add support for federated avatars Fixes #3105 Removes avatar fetching duplication code Adds an "Enable Federated Avatar" checkbox in user settings (defaults to unchecked) Moves avatar settings all in the same form, making local and remote avatars mutually exclusive Renames UploadAvatarForm to AvatarForm as it's not anymore only for uploading * Run gofmt on all modified files * Move Avatar form in its own page * Add go-libravatar dependency to vendor/ dir Hopefully helps with accepting the contribution. See also #3214 * Revert "Add go-libravatar dependency to vendor/ dir" This reverts commit a8cb93ae640bbb90f7d25012fc257bda9fae9b82. * Make federated avatar setting a global configuration Removes the per-user setting * Move avatar handling back to base tool, disable federated avatar in offline mode * Format, handle error * Properly set fallback host * Use unsupported github.com mirror for importing go-libravatar * Remove comment showing life exists outside of github.com ... pity, but contribution would not be accepted otherwise * Use Combo for Get and Post methods over /avatar * FEDERATED_AVATAR -> ENABLE_FEDERATED_AVATAR * Fix persistance of federated avatar lookup checkbox at install time * Federated Avatars -> Enable Federated Avatars * Use len(string) == 0 instead of string == "" * Move import line where it belong See https://github.com/Unknwon/go-code-convention/blob/master/en-US/import_packages.md Pity the import url is still the unofficial one, but oh well... * Save a line (and waste much more expensive time) * Remove redundant parens * Remove an empty line * Remove empty lines * Reorder lines to make diff smaller * Remove another newline Unknwon review got me start a fight against newlines * Move DISABLE_GRAVATAR and ENABLE_FEDERATED_AVATAR after OFFLINE_MODE On re-reading the diff I figured what Unknwon meant here: https://github.com/gogits/gogs/pull/3320/files#r73741106 * Remove newlines that weren't there before my intervention
8 years ago
Add support for federated avatars (#3320) * Add support for federated avatars Fixes #3105 Removes avatar fetching duplication code Adds an "Enable Federated Avatar" checkbox in user settings (defaults to unchecked) Moves avatar settings all in the same form, making local and remote avatars mutually exclusive Renames UploadAvatarForm to AvatarForm as it's not anymore only for uploading * Run gofmt on all modified files * Move Avatar form in its own page * Add go-libravatar dependency to vendor/ dir Hopefully helps with accepting the contribution. See also #3214 * Revert "Add go-libravatar dependency to vendor/ dir" This reverts commit a8cb93ae640bbb90f7d25012fc257bda9fae9b82. * Make federated avatar setting a global configuration Removes the per-user setting * Move avatar handling back to base tool, disable federated avatar in offline mode * Format, handle error * Properly set fallback host * Use unsupported github.com mirror for importing go-libravatar * Remove comment showing life exists outside of github.com ... pity, but contribution would not be accepted otherwise * Use Combo for Get and Post methods over /avatar * FEDERATED_AVATAR -> ENABLE_FEDERATED_AVATAR * Fix persistance of federated avatar lookup checkbox at install time * Federated Avatars -> Enable Federated Avatars * Use len(string) == 0 instead of string == "" * Move import line where it belong See https://github.com/Unknwon/go-code-convention/blob/master/en-US/import_packages.md Pity the import url is still the unofficial one, but oh well... * Save a line (and waste much more expensive time) * Remove redundant parens * Remove an empty line * Remove empty lines * Reorder lines to make diff smaller * Remove another newline Unknwon review got me start a fight against newlines * Move DISABLE_GRAVATAR and ENABLE_FEDERATED_AVATAR after OFFLINE_MODE On re-reading the diff I figured what Unknwon meant here: https://github.com/gogits/gogs/pull/3320/files#r73741106 * Remove newlines that weren't there before my intervention
8 years ago
Add support for federated avatars (#3320) * Add support for federated avatars Fixes #3105 Removes avatar fetching duplication code Adds an "Enable Federated Avatar" checkbox in user settings (defaults to unchecked) Moves avatar settings all in the same form, making local and remote avatars mutually exclusive Renames UploadAvatarForm to AvatarForm as it's not anymore only for uploading * Run gofmt on all modified files * Move Avatar form in its own page * Add go-libravatar dependency to vendor/ dir Hopefully helps with accepting the contribution. See also #3214 * Revert "Add go-libravatar dependency to vendor/ dir" This reverts commit a8cb93ae640bbb90f7d25012fc257bda9fae9b82. * Make federated avatar setting a global configuration Removes the per-user setting * Move avatar handling back to base tool, disable federated avatar in offline mode * Format, handle error * Properly set fallback host * Use unsupported github.com mirror for importing go-libravatar * Remove comment showing life exists outside of github.com ... pity, but contribution would not be accepted otherwise * Use Combo for Get and Post methods over /avatar * FEDERATED_AVATAR -> ENABLE_FEDERATED_AVATAR * Fix persistance of federated avatar lookup checkbox at install time * Federated Avatars -> Enable Federated Avatars * Use len(string) == 0 instead of string == "" * Move import line where it belong See https://github.com/Unknwon/go-code-convention/blob/master/en-US/import_packages.md Pity the import url is still the unofficial one, but oh well... * Save a line (and waste much more expensive time) * Remove redundant parens * Remove an empty line * Remove empty lines * Reorder lines to make diff smaller * Remove another newline Unknwon review got me start a fight against newlines * Move DISABLE_GRAVATAR and ENABLE_FEDERATED_AVATAR after OFFLINE_MODE On re-reading the diff I figured what Unknwon meant here: https://github.com/gogits/gogs/pull/3320/files#r73741106 * Remove newlines that weren't there before my intervention
8 years ago
10 years ago
10 years ago
Add support for federated avatars (#3320) * Add support for federated avatars Fixes #3105 Removes avatar fetching duplication code Adds an "Enable Federated Avatar" checkbox in user settings (defaults to unchecked) Moves avatar settings all in the same form, making local and remote avatars mutually exclusive Renames UploadAvatarForm to AvatarForm as it's not anymore only for uploading * Run gofmt on all modified files * Move Avatar form in its own page * Add go-libravatar dependency to vendor/ dir Hopefully helps with accepting the contribution. See also #3214 * Revert "Add go-libravatar dependency to vendor/ dir" This reverts commit a8cb93ae640bbb90f7d25012fc257bda9fae9b82. * Make federated avatar setting a global configuration Removes the per-user setting * Move avatar handling back to base tool, disable federated avatar in offline mode * Format, handle error * Properly set fallback host * Use unsupported github.com mirror for importing go-libravatar * Remove comment showing life exists outside of github.com ... pity, but contribution would not be accepted otherwise * Use Combo for Get and Post methods over /avatar * FEDERATED_AVATAR -> ENABLE_FEDERATED_AVATAR * Fix persistance of federated avatar lookup checkbox at install time * Federated Avatars -> Enable Federated Avatars * Use len(string) == 0 instead of string == "" * Move import line where it belong See https://github.com/Unknwon/go-code-convention/blob/master/en-US/import_packages.md Pity the import url is still the unofficial one, but oh well... * Save a line (and waste much more expensive time) * Remove redundant parens * Remove an empty line * Remove empty lines * Reorder lines to make diff smaller * Remove another newline Unknwon review got me start a fight against newlines * Move DISABLE_GRAVATAR and ENABLE_FEDERATED_AVATAR after OFFLINE_MODE On re-reading the diff I figured what Unknwon meant here: https://github.com/gogits/gogs/pull/3320/files#r73741106 * Remove newlines that weren't there before my intervention
8 years ago
Add support for federated avatars (#3320) * Add support for federated avatars Fixes #3105 Removes avatar fetching duplication code Adds an "Enable Federated Avatar" checkbox in user settings (defaults to unchecked) Moves avatar settings all in the same form, making local and remote avatars mutually exclusive Renames UploadAvatarForm to AvatarForm as it's not anymore only for uploading * Run gofmt on all modified files * Move Avatar form in its own page * Add go-libravatar dependency to vendor/ dir Hopefully helps with accepting the contribution. See also #3214 * Revert "Add go-libravatar dependency to vendor/ dir" This reverts commit a8cb93ae640bbb90f7d25012fc257bda9fae9b82. * Make federated avatar setting a global configuration Removes the per-user setting * Move avatar handling back to base tool, disable federated avatar in offline mode * Format, handle error * Properly set fallback host * Use unsupported github.com mirror for importing go-libravatar * Remove comment showing life exists outside of github.com ... pity, but contribution would not be accepted otherwise * Use Combo for Get and Post methods over /avatar * FEDERATED_AVATAR -> ENABLE_FEDERATED_AVATAR * Fix persistance of federated avatar lookup checkbox at install time * Federated Avatars -> Enable Federated Avatars * Use len(string) == 0 instead of string == "" * Move import line where it belong See https://github.com/Unknwon/go-code-convention/blob/master/en-US/import_packages.md Pity the import url is still the unofficial one, but oh well... * Save a line (and waste much more expensive time) * Remove redundant parens * Remove an empty line * Remove empty lines * Reorder lines to make diff smaller * Remove another newline Unknwon review got me start a fight against newlines * Move DISABLE_GRAVATAR and ENABLE_FEDERATED_AVATAR after OFFLINE_MODE On re-reading the diff I figured what Unknwon meant here: https://github.com/gogits/gogs/pull/3320/files#r73741106 * Remove newlines that weren't there before my intervention
8 years ago
10 years ago
10 years ago
10 years ago
9 years ago
9 years ago
10 years ago
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package user
  5. import (
  6. "errors"
  7. "fmt"
  8. "io/ioutil"
  9. "strings"
  10. "github.com/Unknwon/com"
  11. "code.gitea.io/gitea/models"
  12. "code.gitea.io/gitea/modules/auth"
  13. "code.gitea.io/gitea/modules/base"
  14. "code.gitea.io/gitea/modules/context"
  15. "code.gitea.io/gitea/modules/log"
  16. "code.gitea.io/gitea/modules/setting"
  17. )
  18. const (
  19. tplSettingsProfile base.TplName = "user/settings/profile"
  20. tplSettingsAvatar base.TplName = "user/settings/avatar"
  21. tplSettingsPassword base.TplName = "user/settings/password"
  22. tplSettingsEmails base.TplName = "user/settings/email"
  23. tplSettingsSSHKeys base.TplName = "user/settings/sshkeys"
  24. tplSettingsSocial base.TplName = "user/settings/social"
  25. tplSettingsApplications base.TplName = "user/settings/applications"
  26. tplSettingsDelete base.TplName = "user/settings/delete"
  27. tplNotification base.TplName = "user/notification"
  28. tplSecurity base.TplName = "user/security"
  29. )
  30. // Settings render user's profile page
  31. func Settings(ctx *context.Context) {
  32. ctx.Data["Title"] = ctx.Tr("settings")
  33. ctx.Data["PageIsSettingsProfile"] = true
  34. ctx.HTML(200, tplSettingsProfile)
  35. }
  36. func handleUsernameChange(ctx *context.Context, newName string) {
  37. // Non-local users are not allowed to change their username.
  38. if len(newName) == 0 || !ctx.User.IsLocal() {
  39. return
  40. }
  41. // Check if user name has been changed
  42. if ctx.User.LowerName != strings.ToLower(newName) {
  43. if err := models.ChangeUserName(ctx.User, newName); err != nil {
  44. switch {
  45. case models.IsErrUserAlreadyExist(err):
  46. ctx.Flash.Error(ctx.Tr("newName_been_taken"))
  47. ctx.Redirect(setting.AppSubUrl + "/user/settings")
  48. case models.IsErrEmailAlreadyUsed(err):
  49. ctx.Flash.Error(ctx.Tr("form.email_been_used"))
  50. ctx.Redirect(setting.AppSubUrl + "/user/settings")
  51. case models.IsErrNameReserved(err):
  52. ctx.Flash.Error(ctx.Tr("user.newName_reserved"))
  53. ctx.Redirect(setting.AppSubUrl + "/user/settings")
  54. case models.IsErrNamePatternNotAllowed(err):
  55. ctx.Flash.Error(ctx.Tr("user.newName_pattern_not_allowed"))
  56. ctx.Redirect(setting.AppSubUrl + "/user/settings")
  57. default:
  58. ctx.Handle(500, "ChangeUserName", err)
  59. }
  60. return
  61. }
  62. log.Trace("User name changed: %s -> %s", ctx.User.Name, newName)
  63. }
  64. // In case it's just a case change
  65. ctx.User.Name = newName
  66. ctx.User.LowerName = strings.ToLower(newName)
  67. }
  68. // SettingsPost response for change user's profile
  69. func SettingsPost(ctx *context.Context, form auth.UpdateProfileForm) {
  70. ctx.Data["Title"] = ctx.Tr("settings")
  71. ctx.Data["PageIsSettingsProfile"] = true
  72. if ctx.HasError() {
  73. ctx.HTML(200, tplSettingsProfile)
  74. return
  75. }
  76. handleUsernameChange(ctx, form.Name)
  77. if ctx.Written() {
  78. return
  79. }
  80. ctx.User.FullName = form.FullName
  81. ctx.User.Email = form.Email
  82. ctx.User.Website = form.Website
  83. ctx.User.Location = form.Location
  84. if err := models.UpdateUser(ctx.User); err != nil {
  85. ctx.Handle(500, "UpdateUser", err)
  86. return
  87. }
  88. log.Trace("User settings updated: %s", ctx.User.Name)
  89. ctx.Flash.Success(ctx.Tr("settings.update_profile_success"))
  90. ctx.Redirect(setting.AppSubUrl + "/user/settings")
  91. }
  92. // UpdateAvatarSetting update user's avatar
  93. // FIXME: limit size.
  94. func UpdateAvatarSetting(ctx *context.Context, form auth.AvatarForm, ctxUser *models.User) error {
  95. ctxUser.UseCustomAvatar = form.Source == auth.AvatarLocal
  96. if len(form.Gravatar) > 0 {
  97. ctxUser.Avatar = base.EncodeMD5(form.Gravatar)
  98. ctxUser.AvatarEmail = form.Gravatar
  99. }
  100. if form.Avatar != nil {
  101. fr, err := form.Avatar.Open()
  102. if err != nil {
  103. return fmt.Errorf("Avatar.Open: %v", err)
  104. }
  105. defer fr.Close()
  106. data, err := ioutil.ReadAll(fr)
  107. if err != nil {
  108. return fmt.Errorf("ioutil.ReadAll: %v", err)
  109. }
  110. if !base.IsImageFile(data) {
  111. return errors.New(ctx.Tr("settings.uploaded_avatar_not_a_image"))
  112. }
  113. if err = ctxUser.UploadAvatar(data); err != nil {
  114. return fmt.Errorf("UploadAvatar: %v", err)
  115. }
  116. } else {
  117. // No avatar is uploaded but setting has been changed to enable,
  118. // generate a random one when needed.
  119. if ctxUser.UseCustomAvatar && !com.IsFile(ctxUser.CustomAvatarPath()) {
  120. if err := ctxUser.GenerateRandomAvatar(); err != nil {
  121. log.Error(4, "GenerateRandomAvatar[%d]: %v", ctxUser.ID, err)
  122. }
  123. }
  124. }
  125. if err := models.UpdateUser(ctxUser); err != nil {
  126. return fmt.Errorf("UpdateUser: %v", err)
  127. }
  128. return nil
  129. }
  130. // SettingsAvatar render user avatar page
  131. func SettingsAvatar(ctx *context.Context) {
  132. ctx.Data["Title"] = ctx.Tr("settings")
  133. ctx.Data["PageIsSettingsAvatar"] = true
  134. ctx.HTML(200, tplSettingsAvatar)
  135. }
  136. // SettingsAvatarPost response for change user's avatar request
  137. func SettingsAvatarPost(ctx *context.Context, form auth.AvatarForm) {
  138. if err := UpdateAvatarSetting(ctx, form, ctx.User); err != nil {
  139. ctx.Flash.Error(err.Error())
  140. } else {
  141. ctx.Flash.Success(ctx.Tr("settings.update_avatar_success"))
  142. }
  143. ctx.Redirect(setting.AppSubUrl + "/user/settings/avatar")
  144. }
  145. // SettingsDeleteAvatar render delete avatar page
  146. func SettingsDeleteAvatar(ctx *context.Context) {
  147. if err := ctx.User.DeleteAvatar(); err != nil {
  148. ctx.Flash.Error(err.Error())
  149. }
  150. ctx.Redirect(setting.AppSubUrl + "/user/settings/avatar")
  151. }
  152. // SettingsPassword render change user's password page
  153. func SettingsPassword(ctx *context.Context) {
  154. ctx.Data["Title"] = ctx.Tr("settings")
  155. ctx.Data["PageIsSettingsPassword"] = true
  156. ctx.HTML(200, tplSettingsPassword)
  157. }
  158. // SettingsPasswordPost response for change user's password
  159. func SettingsPasswordPost(ctx *context.Context, form auth.ChangePasswordForm) {
  160. ctx.Data["Title"] = ctx.Tr("settings")
  161. ctx.Data["PageIsSettingsPassword"] = true
  162. if ctx.HasError() {
  163. ctx.HTML(200, tplSettingsPassword)
  164. return
  165. }
  166. if !ctx.User.ValidatePassword(form.OldPassword) {
  167. ctx.Flash.Error(ctx.Tr("settings.password_incorrect"))
  168. } else if form.Password != form.Retype {
  169. ctx.Flash.Error(ctx.Tr("form.password_not_match"))
  170. } else {
  171. ctx.User.Passwd = form.Password
  172. ctx.User.Salt = models.GetUserSalt()
  173. ctx.User.EncodePasswd()
  174. if err := models.UpdateUser(ctx.User); err != nil {
  175. ctx.Handle(500, "UpdateUser", err)
  176. return
  177. }
  178. log.Trace("User password updated: %s", ctx.User.Name)
  179. ctx.Flash.Success(ctx.Tr("settings.change_password_success"))
  180. }
  181. ctx.Redirect(setting.AppSubUrl + "/user/settings/password")
  182. }
  183. // SettingsEmails render user's emails page
  184. func SettingsEmails(ctx *context.Context) {
  185. ctx.Data["Title"] = ctx.Tr("settings")
  186. ctx.Data["PageIsSettingsEmails"] = true
  187. emails, err := models.GetEmailAddresses(ctx.User.ID)
  188. if err != nil {
  189. ctx.Handle(500, "GetEmailAddresses", err)
  190. return
  191. }
  192. ctx.Data["Emails"] = emails
  193. ctx.HTML(200, tplSettingsEmails)
  194. }
  195. // SettingsEmailPost response for change user's email
  196. func SettingsEmailPost(ctx *context.Context, form auth.AddEmailForm) {
  197. ctx.Data["Title"] = ctx.Tr("settings")
  198. ctx.Data["PageIsSettingsEmails"] = true
  199. // Make emailaddress primary.
  200. if ctx.Query("_method") == "PRIMARY" {
  201. if err := models.MakeEmailPrimary(&models.EmailAddress{ID: ctx.QueryInt64("id")}); err != nil {
  202. ctx.Handle(500, "MakeEmailPrimary", err)
  203. return
  204. }
  205. log.Trace("Email made primary: %s", ctx.User.Name)
  206. ctx.Redirect(setting.AppSubUrl + "/user/settings/email")
  207. return
  208. }
  209. // Add Email address.
  210. emails, err := models.GetEmailAddresses(ctx.User.ID)
  211. if err != nil {
  212. ctx.Handle(500, "GetEmailAddresses", err)
  213. return
  214. }
  215. ctx.Data["Emails"] = emails
  216. if ctx.HasError() {
  217. ctx.HTML(200, tplSettingsEmails)
  218. return
  219. }
  220. email := &models.EmailAddress{
  221. UID: ctx.User.ID,
  222. Email: form.Email,
  223. IsActivated: !setting.Service.RegisterEmailConfirm,
  224. }
  225. if err := models.AddEmailAddress(email); err != nil {
  226. if models.IsErrEmailAlreadyUsed(err) {
  227. ctx.RenderWithErr(ctx.Tr("form.email_been_used"), tplSettingsEmails, &form)
  228. return
  229. }
  230. ctx.Handle(500, "AddEmailAddress", err)
  231. return
  232. }
  233. // Send confirmation email
  234. if setting.Service.RegisterEmailConfirm {
  235. models.SendActivateEmailMail(ctx.Context, ctx.User, email)
  236. if err := ctx.Cache.Put("MailResendLimit_"+ctx.User.LowerName, ctx.User.LowerName, 180); err != nil {
  237. log.Error(4, "Set cache(MailResendLimit) fail: %v", err)
  238. }
  239. ctx.Flash.Info(ctx.Tr("settings.add_email_confirmation_sent", email.Email, setting.Service.ActiveCodeLives/60))
  240. } else {
  241. ctx.Flash.Success(ctx.Tr("settings.add_email_success"))
  242. }
  243. log.Trace("Email address added: %s", email.Email)
  244. ctx.Redirect(setting.AppSubUrl + "/user/settings/email")
  245. }
  246. // DeleteEmail reponse for delete user's email
  247. func DeleteEmail(ctx *context.Context) {
  248. if err := models.DeleteEmailAddress(&models.EmailAddress{ID: ctx.QueryInt64("id")}); err != nil {
  249. ctx.Handle(500, "DeleteEmail", err)
  250. return
  251. }
  252. log.Trace("Email address deleted: %s", ctx.User.Name)
  253. ctx.Flash.Success(ctx.Tr("settings.email_deletion_success"))
  254. ctx.JSON(200, map[string]interface{}{
  255. "redirect": setting.AppSubUrl + "/user/settings/email",
  256. })
  257. }
  258. // SettingsSSHKeys render user's SSH public keys page
  259. func SettingsSSHKeys(ctx *context.Context) {
  260. ctx.Data["Title"] = ctx.Tr("settings")
  261. ctx.Data["PageIsSettingsSSHKeys"] = true
  262. keys, err := models.ListPublicKeys(ctx.User.ID)
  263. if err != nil {
  264. ctx.Handle(500, "ListPublicKeys", err)
  265. return
  266. }
  267. ctx.Data["Keys"] = keys
  268. ctx.HTML(200, tplSettingsSSHKeys)
  269. }
  270. // SettingsSSHKeysPost response for change user's SSH keys
  271. func SettingsSSHKeysPost(ctx *context.Context, form auth.AddSSHKeyForm) {
  272. ctx.Data["Title"] = ctx.Tr("settings")
  273. ctx.Data["PageIsSettingsSSHKeys"] = true
  274. keys, err := models.ListPublicKeys(ctx.User.ID)
  275. if err != nil {
  276. ctx.Handle(500, "ListPublicKeys", err)
  277. return
  278. }
  279. ctx.Data["Keys"] = keys
  280. if ctx.HasError() {
  281. ctx.HTML(200, tplSettingsSSHKeys)
  282. return
  283. }
  284. content, err := models.CheckPublicKeyString(form.Content)
  285. if err != nil {
  286. if models.IsErrKeyUnableVerify(err) {
  287. ctx.Flash.Info(ctx.Tr("form.unable_verify_ssh_key"))
  288. } else {
  289. ctx.Flash.Error(ctx.Tr("form.invalid_ssh_key", err.Error()))
  290. ctx.Redirect(setting.AppSubUrl + "/user/settings/ssh")
  291. return
  292. }
  293. }
  294. if _, err = models.AddPublicKey(ctx.User.ID, form.Title, content); err != nil {
  295. ctx.Data["HasError"] = true
  296. switch {
  297. case models.IsErrKeyAlreadyExist(err):
  298. ctx.Data["Err_Content"] = true
  299. ctx.RenderWithErr(ctx.Tr("settings.ssh_key_been_used"), tplSettingsSSHKeys, &form)
  300. case models.IsErrKeyNameAlreadyUsed(err):
  301. ctx.Data["Err_Title"] = true
  302. ctx.RenderWithErr(ctx.Tr("settings.ssh_key_name_used"), tplSettingsSSHKeys, &form)
  303. default:
  304. ctx.Handle(500, "AddPublicKey", err)
  305. }
  306. return
  307. }
  308. ctx.Flash.Success(ctx.Tr("settings.add_key_success", form.Title))
  309. ctx.Redirect(setting.AppSubUrl + "/user/settings/ssh")
  310. }
  311. // DeleteSSHKey response for delete user's SSH key
  312. func DeleteSSHKey(ctx *context.Context) {
  313. if err := models.DeletePublicKey(ctx.User, ctx.QueryInt64("id")); err != nil {
  314. ctx.Flash.Error("DeletePublicKey: " + err.Error())
  315. } else {
  316. ctx.Flash.Success(ctx.Tr("settings.ssh_key_deletion_success"))
  317. }
  318. ctx.JSON(200, map[string]interface{}{
  319. "redirect": setting.AppSubUrl + "/user/settings/ssh",
  320. })
  321. }
  322. // SettingsApplications render user's access tokens page
  323. func SettingsApplications(ctx *context.Context) {
  324. ctx.Data["Title"] = ctx.Tr("settings")
  325. ctx.Data["PageIsSettingsApplications"] = true
  326. tokens, err := models.ListAccessTokens(ctx.User.ID)
  327. if err != nil {
  328. ctx.Handle(500, "ListAccessTokens", err)
  329. return
  330. }
  331. ctx.Data["Tokens"] = tokens
  332. ctx.HTML(200, tplSettingsApplications)
  333. }
  334. // SettingsApplicationsPost response for add user's access token
  335. func SettingsApplicationsPost(ctx *context.Context, form auth.NewAccessTokenForm) {
  336. ctx.Data["Title"] = ctx.Tr("settings")
  337. ctx.Data["PageIsSettingsApplications"] = true
  338. if ctx.HasError() {
  339. tokens, err := models.ListAccessTokens(ctx.User.ID)
  340. if err != nil {
  341. ctx.Handle(500, "ListAccessTokens", err)
  342. return
  343. }
  344. ctx.Data["Tokens"] = tokens
  345. ctx.HTML(200, tplSettingsApplications)
  346. return
  347. }
  348. t := &models.AccessToken{
  349. UID: ctx.User.ID,
  350. Name: form.Name,
  351. }
  352. if err := models.NewAccessToken(t); err != nil {
  353. ctx.Handle(500, "NewAccessToken", err)
  354. return
  355. }
  356. ctx.Flash.Success(ctx.Tr("settings.generate_token_succees"))
  357. ctx.Flash.Info(t.Sha1)
  358. ctx.Redirect(setting.AppSubUrl + "/user/settings/applications")
  359. }
  360. // SettingsDeleteApplication response for delete user access token
  361. func SettingsDeleteApplication(ctx *context.Context) {
  362. if err := models.DeleteAccessTokenByID(ctx.QueryInt64("id")); err != nil {
  363. ctx.Flash.Error("DeleteAccessTokenByID: " + err.Error())
  364. } else {
  365. ctx.Flash.Success(ctx.Tr("settings.delete_token_success"))
  366. }
  367. ctx.JSON(200, map[string]interface{}{
  368. "redirect": setting.AppSubUrl + "/user/settings/applications",
  369. })
  370. }
  371. // SettingsDelete render user suicide page and response for delete user himself
  372. func SettingsDelete(ctx *context.Context) {
  373. ctx.Data["Title"] = ctx.Tr("settings")
  374. ctx.Data["PageIsSettingsDelete"] = true
  375. if ctx.Req.Method == "POST" {
  376. if _, err := models.UserSignIn(ctx.User.Name, ctx.Query("password")); err != nil {
  377. if models.IsErrUserNotExist(err) {
  378. ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_password"), tplSettingsDelete, nil)
  379. } else {
  380. ctx.Handle(500, "UserSignIn", err)
  381. }
  382. return
  383. }
  384. if err := models.DeleteUser(ctx.User); err != nil {
  385. switch {
  386. case models.IsErrUserOwnRepos(err):
  387. ctx.Flash.Error(ctx.Tr("form.still_own_repo"))
  388. ctx.Redirect(setting.AppSubUrl + "/user/settings/delete")
  389. case models.IsErrUserHasOrgs(err):
  390. ctx.Flash.Error(ctx.Tr("form.still_has_org"))
  391. ctx.Redirect(setting.AppSubUrl + "/user/settings/delete")
  392. default:
  393. ctx.Handle(500, "DeleteUser", err)
  394. }
  395. } else {
  396. log.Trace("Account deleted: %s", ctx.User.Name)
  397. ctx.Redirect(setting.AppSubUrl + "/")
  398. }
  399. return
  400. }
  401. ctx.HTML(200, tplSettingsDelete)
  402. }