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.

setting.go 14 kB

11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 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
9 years ago
9 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
9 years ago
11 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
9 years ago
11 years ago
11 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
9 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
9 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
9 years ago
11 years ago
11 years ago
11 years ago
9 years ago
11 years ago
11 years ago
10 years ago
10 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. tplSecurity base.TplName = "user/security"
  28. )
  29. // Settings render user's profile page
  30. func Settings(ctx *context.Context) {
  31. ctx.Data["Title"] = ctx.Tr("settings")
  32. ctx.Data["PageIsSettingsProfile"] = true
  33. ctx.HTML(200, tplSettingsProfile)
  34. }
  35. func handleUsernameChange(ctx *context.Context, newName string) {
  36. // Non-local users are not allowed to change their username.
  37. if len(newName) == 0 || !ctx.User.IsLocal() {
  38. return
  39. }
  40. // Check if user name has been changed
  41. if ctx.User.LowerName != strings.ToLower(newName) {
  42. if err := models.ChangeUserName(ctx.User, newName); err != nil {
  43. switch {
  44. case models.IsErrUserAlreadyExist(err):
  45. ctx.Flash.Error(ctx.Tr("newName_been_taken"))
  46. ctx.Redirect(setting.AppSubURL + "/user/settings")
  47. case models.IsErrEmailAlreadyUsed(err):
  48. ctx.Flash.Error(ctx.Tr("form.email_been_used"))
  49. ctx.Redirect(setting.AppSubURL + "/user/settings")
  50. case models.IsErrNameReserved(err):
  51. ctx.Flash.Error(ctx.Tr("user.newName_reserved"))
  52. ctx.Redirect(setting.AppSubURL + "/user/settings")
  53. case models.IsErrNamePatternNotAllowed(err):
  54. ctx.Flash.Error(ctx.Tr("user.newName_pattern_not_allowed"))
  55. ctx.Redirect(setting.AppSubURL + "/user/settings")
  56. default:
  57. ctx.Handle(500, "ChangeUserName", err)
  58. }
  59. return
  60. }
  61. log.Trace("User name changed: %s -> %s", ctx.User.Name, newName)
  62. }
  63. // In case it's just a case change
  64. ctx.User.Name = newName
  65. ctx.User.LowerName = strings.ToLower(newName)
  66. }
  67. // SettingsPost response for change user's profile
  68. func SettingsPost(ctx *context.Context, form auth.UpdateProfileForm) {
  69. ctx.Data["Title"] = ctx.Tr("settings")
  70. ctx.Data["PageIsSettingsProfile"] = true
  71. if ctx.HasError() {
  72. ctx.HTML(200, tplSettingsProfile)
  73. return
  74. }
  75. handleUsernameChange(ctx, form.Name)
  76. if ctx.Written() {
  77. return
  78. }
  79. ctx.User.FullName = form.FullName
  80. ctx.User.Email = form.Email
  81. ctx.User.KeepEmailPrivate = form.KeepEmailPrivate
  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. var err error
  173. if ctx.User.Salt, err = models.GetUserSalt(); err != nil {
  174. ctx.Handle(500, "UpdateUser", err)
  175. return
  176. }
  177. ctx.User.EncodePasswd()
  178. if err := models.UpdateUser(ctx.User); err != nil {
  179. ctx.Handle(500, "UpdateUser", err)
  180. return
  181. }
  182. log.Trace("User password updated: %s", ctx.User.Name)
  183. ctx.Flash.Success(ctx.Tr("settings.change_password_success"))
  184. }
  185. ctx.Redirect(setting.AppSubURL + "/user/settings/password")
  186. }
  187. // SettingsEmails render user's emails page
  188. func SettingsEmails(ctx *context.Context) {
  189. ctx.Data["Title"] = ctx.Tr("settings")
  190. ctx.Data["PageIsSettingsEmails"] = true
  191. emails, err := models.GetEmailAddresses(ctx.User.ID)
  192. if err != nil {
  193. ctx.Handle(500, "GetEmailAddresses", err)
  194. return
  195. }
  196. ctx.Data["Emails"] = emails
  197. ctx.HTML(200, tplSettingsEmails)
  198. }
  199. // SettingsEmailPost response for change user's email
  200. func SettingsEmailPost(ctx *context.Context, form auth.AddEmailForm) {
  201. ctx.Data["Title"] = ctx.Tr("settings")
  202. ctx.Data["PageIsSettingsEmails"] = true
  203. // Make emailaddress primary.
  204. if ctx.Query("_method") == "PRIMARY" {
  205. if err := models.MakeEmailPrimary(&models.EmailAddress{ID: ctx.QueryInt64("id")}); err != nil {
  206. ctx.Handle(500, "MakeEmailPrimary", err)
  207. return
  208. }
  209. log.Trace("Email made primary: %s", ctx.User.Name)
  210. ctx.Redirect(setting.AppSubURL + "/user/settings/email")
  211. return
  212. }
  213. // Add Email address.
  214. emails, err := models.GetEmailAddresses(ctx.User.ID)
  215. if err != nil {
  216. ctx.Handle(500, "GetEmailAddresses", err)
  217. return
  218. }
  219. ctx.Data["Emails"] = emails
  220. if ctx.HasError() {
  221. ctx.HTML(200, tplSettingsEmails)
  222. return
  223. }
  224. email := &models.EmailAddress{
  225. UID: ctx.User.ID,
  226. Email: form.Email,
  227. IsActivated: !setting.Service.RegisterEmailConfirm,
  228. }
  229. if err := models.AddEmailAddress(email); err != nil {
  230. if models.IsErrEmailAlreadyUsed(err) {
  231. ctx.RenderWithErr(ctx.Tr("form.email_been_used"), tplSettingsEmails, &form)
  232. return
  233. }
  234. ctx.Handle(500, "AddEmailAddress", err)
  235. return
  236. }
  237. // Send confirmation email
  238. if setting.Service.RegisterEmailConfirm {
  239. models.SendActivateEmailMail(ctx.Context, ctx.User, email)
  240. if err := ctx.Cache.Put("MailResendLimit_"+ctx.User.LowerName, ctx.User.LowerName, 180); err != nil {
  241. log.Error(4, "Set cache(MailResendLimit) fail: %v", err)
  242. }
  243. ctx.Flash.Info(ctx.Tr("settings.add_email_confirmation_sent", email.Email, setting.Service.ActiveCodeLives/60))
  244. } else {
  245. ctx.Flash.Success(ctx.Tr("settings.add_email_success"))
  246. }
  247. log.Trace("Email address added: %s", email.Email)
  248. ctx.Redirect(setting.AppSubURL + "/user/settings/email")
  249. }
  250. // DeleteEmail response for delete user's email
  251. func DeleteEmail(ctx *context.Context) {
  252. if err := models.DeleteEmailAddress(&models.EmailAddress{ID: ctx.QueryInt64("id"), UID: ctx.User.ID}); err != nil {
  253. ctx.Handle(500, "DeleteEmail", err)
  254. return
  255. }
  256. log.Trace("Email address deleted: %s", ctx.User.Name)
  257. ctx.Flash.Success(ctx.Tr("settings.email_deletion_success"))
  258. ctx.JSON(200, map[string]interface{}{
  259. "redirect": setting.AppSubURL + "/user/settings/email",
  260. })
  261. }
  262. // SettingsSSHKeys render user's SSH public keys page
  263. func SettingsSSHKeys(ctx *context.Context) {
  264. ctx.Data["Title"] = ctx.Tr("settings")
  265. ctx.Data["PageIsSettingsSSHKeys"] = true
  266. keys, err := models.ListPublicKeys(ctx.User.ID)
  267. if err != nil {
  268. ctx.Handle(500, "ListPublicKeys", err)
  269. return
  270. }
  271. ctx.Data["Keys"] = keys
  272. ctx.HTML(200, tplSettingsSSHKeys)
  273. }
  274. // SettingsSSHKeysPost response for change user's SSH keys
  275. func SettingsSSHKeysPost(ctx *context.Context, form auth.AddSSHKeyForm) {
  276. ctx.Data["Title"] = ctx.Tr("settings")
  277. ctx.Data["PageIsSettingsSSHKeys"] = true
  278. keys, err := models.ListPublicKeys(ctx.User.ID)
  279. if err != nil {
  280. ctx.Handle(500, "ListPublicKeys", err)
  281. return
  282. }
  283. ctx.Data["Keys"] = keys
  284. if ctx.HasError() {
  285. ctx.HTML(200, tplSettingsSSHKeys)
  286. return
  287. }
  288. content, err := models.CheckPublicKeyString(form.Content)
  289. if err != nil {
  290. if models.IsErrKeyUnableVerify(err) {
  291. ctx.Flash.Info(ctx.Tr("form.unable_verify_ssh_key"))
  292. } else {
  293. ctx.Flash.Error(ctx.Tr("form.invalid_ssh_key", err.Error()))
  294. ctx.Redirect(setting.AppSubURL + "/user/settings/ssh")
  295. return
  296. }
  297. }
  298. if _, err = models.AddPublicKey(ctx.User.ID, form.Title, content); err != nil {
  299. ctx.Data["HasError"] = true
  300. switch {
  301. case models.IsErrKeyAlreadyExist(err):
  302. ctx.Data["Err_Content"] = true
  303. ctx.RenderWithErr(ctx.Tr("settings.ssh_key_been_used"), tplSettingsSSHKeys, &form)
  304. case models.IsErrKeyNameAlreadyUsed(err):
  305. ctx.Data["Err_Title"] = true
  306. ctx.RenderWithErr(ctx.Tr("settings.ssh_key_name_used"), tplSettingsSSHKeys, &form)
  307. default:
  308. ctx.Handle(500, "AddPublicKey", err)
  309. }
  310. return
  311. }
  312. ctx.Flash.Success(ctx.Tr("settings.add_key_success", form.Title))
  313. ctx.Redirect(setting.AppSubURL + "/user/settings/ssh")
  314. }
  315. // DeleteSSHKey response for delete user's SSH key
  316. func DeleteSSHKey(ctx *context.Context) {
  317. if err := models.DeletePublicKey(ctx.User, ctx.QueryInt64("id")); err != nil {
  318. ctx.Flash.Error("DeletePublicKey: " + err.Error())
  319. } else {
  320. ctx.Flash.Success(ctx.Tr("settings.ssh_key_deletion_success"))
  321. }
  322. ctx.JSON(200, map[string]interface{}{
  323. "redirect": setting.AppSubURL + "/user/settings/ssh",
  324. })
  325. }
  326. // SettingsApplications render user's access tokens page
  327. func SettingsApplications(ctx *context.Context) {
  328. ctx.Data["Title"] = ctx.Tr("settings")
  329. ctx.Data["PageIsSettingsApplications"] = true
  330. tokens, err := models.ListAccessTokens(ctx.User.ID)
  331. if err != nil {
  332. ctx.Handle(500, "ListAccessTokens", err)
  333. return
  334. }
  335. ctx.Data["Tokens"] = tokens
  336. ctx.HTML(200, tplSettingsApplications)
  337. }
  338. // SettingsApplicationsPost response for add user's access token
  339. func SettingsApplicationsPost(ctx *context.Context, form auth.NewAccessTokenForm) {
  340. ctx.Data["Title"] = ctx.Tr("settings")
  341. ctx.Data["PageIsSettingsApplications"] = true
  342. if ctx.HasError() {
  343. tokens, err := models.ListAccessTokens(ctx.User.ID)
  344. if err != nil {
  345. ctx.Handle(500, "ListAccessTokens", err)
  346. return
  347. }
  348. ctx.Data["Tokens"] = tokens
  349. ctx.HTML(200, tplSettingsApplications)
  350. return
  351. }
  352. t := &models.AccessToken{
  353. UID: ctx.User.ID,
  354. Name: form.Name,
  355. }
  356. if err := models.NewAccessToken(t); err != nil {
  357. ctx.Handle(500, "NewAccessToken", err)
  358. return
  359. }
  360. ctx.Flash.Success(ctx.Tr("settings.generate_token_succees"))
  361. ctx.Flash.Info(t.Sha1)
  362. ctx.Redirect(setting.AppSubURL + "/user/settings/applications")
  363. }
  364. // SettingsDeleteApplication response for delete user access token
  365. func SettingsDeleteApplication(ctx *context.Context) {
  366. if err := models.DeleteAccessTokenByID(ctx.QueryInt64("id"), ctx.User.ID); err != nil {
  367. ctx.Flash.Error("DeleteAccessTokenByID: " + err.Error())
  368. } else {
  369. ctx.Flash.Success(ctx.Tr("settings.delete_token_success"))
  370. }
  371. ctx.JSON(200, map[string]interface{}{
  372. "redirect": setting.AppSubURL + "/user/settings/applications",
  373. })
  374. }
  375. // SettingsDelete render user suicide page and response for delete user himself
  376. func SettingsDelete(ctx *context.Context) {
  377. ctx.Data["Title"] = ctx.Tr("settings")
  378. ctx.Data["PageIsSettingsDelete"] = true
  379. if ctx.Req.Method == "POST" {
  380. if _, err := models.UserSignIn(ctx.User.Name, ctx.Query("password")); err != nil {
  381. if models.IsErrUserNotExist(err) {
  382. ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_password"), tplSettingsDelete, nil)
  383. } else {
  384. ctx.Handle(500, "UserSignIn", err)
  385. }
  386. return
  387. }
  388. if err := models.DeleteUser(ctx.User); err != nil {
  389. switch {
  390. case models.IsErrUserOwnRepos(err):
  391. ctx.Flash.Error(ctx.Tr("form.still_own_repo"))
  392. ctx.Redirect(setting.AppSubURL + "/user/settings/delete")
  393. case models.IsErrUserHasOrgs(err):
  394. ctx.Flash.Error(ctx.Tr("form.still_has_org"))
  395. ctx.Redirect(setting.AppSubURL + "/user/settings/delete")
  396. default:
  397. ctx.Handle(500, "DeleteUser", err)
  398. }
  399. } else {
  400. log.Trace("Account deleted: %s", ctx.User.Name)
  401. ctx.Redirect(setting.AppSubURL + "/")
  402. }
  403. return
  404. }
  405. ctx.HTML(200, tplSettingsDelete)
  406. }