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
12 years ago
10 years ago
12 years ago
12 years ago
12 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
12 years ago
9 years ago
12 years ago
12 years ago
10 years ago
10 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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.Website = form.Website
  82. ctx.User.Location = form.Location
  83. if err := models.UpdateUser(ctx.User); err != nil {
  84. ctx.Handle(500, "UpdateUser", err)
  85. return
  86. }
  87. log.Trace("User settings updated: %s", ctx.User.Name)
  88. ctx.Flash.Success(ctx.Tr("settings.update_profile_success"))
  89. ctx.Redirect(setting.AppSubURL + "/user/settings")
  90. }
  91. // UpdateAvatarSetting update user's avatar
  92. // FIXME: limit size.
  93. func UpdateAvatarSetting(ctx *context.Context, form auth.AvatarForm, ctxUser *models.User) error {
  94. ctxUser.UseCustomAvatar = form.Source == auth.AvatarLocal
  95. if len(form.Gravatar) > 0 {
  96. ctxUser.Avatar = base.EncodeMD5(form.Gravatar)
  97. ctxUser.AvatarEmail = form.Gravatar
  98. }
  99. if form.Avatar != nil {
  100. fr, err := form.Avatar.Open()
  101. if err != nil {
  102. return fmt.Errorf("Avatar.Open: %v", err)
  103. }
  104. defer fr.Close()
  105. data, err := ioutil.ReadAll(fr)
  106. if err != nil {
  107. return fmt.Errorf("ioutil.ReadAll: %v", err)
  108. }
  109. if !base.IsImageFile(data) {
  110. return errors.New(ctx.Tr("settings.uploaded_avatar_not_a_image"))
  111. }
  112. if err = ctxUser.UploadAvatar(data); err != nil {
  113. return fmt.Errorf("UploadAvatar: %v", err)
  114. }
  115. } else {
  116. // No avatar is uploaded but setting has been changed to enable,
  117. // generate a random one when needed.
  118. if ctxUser.UseCustomAvatar && !com.IsFile(ctxUser.CustomAvatarPath()) {
  119. if err := ctxUser.GenerateRandomAvatar(); err != nil {
  120. log.Error(4, "GenerateRandomAvatar[%d]: %v", ctxUser.ID, err)
  121. }
  122. }
  123. }
  124. if err := models.UpdateUser(ctxUser); err != nil {
  125. return fmt.Errorf("UpdateUser: %v", err)
  126. }
  127. return nil
  128. }
  129. // SettingsAvatar render user avatar page
  130. func SettingsAvatar(ctx *context.Context) {
  131. ctx.Data["Title"] = ctx.Tr("settings")
  132. ctx.Data["PageIsSettingsAvatar"] = true
  133. ctx.HTML(200, tplSettingsAvatar)
  134. }
  135. // SettingsAvatarPost response for change user's avatar request
  136. func SettingsAvatarPost(ctx *context.Context, form auth.AvatarForm) {
  137. if err := UpdateAvatarSetting(ctx, form, ctx.User); err != nil {
  138. ctx.Flash.Error(err.Error())
  139. } else {
  140. ctx.Flash.Success(ctx.Tr("settings.update_avatar_success"))
  141. }
  142. ctx.Redirect(setting.AppSubURL + "/user/settings/avatar")
  143. }
  144. // SettingsDeleteAvatar render delete avatar page
  145. func SettingsDeleteAvatar(ctx *context.Context) {
  146. if err := ctx.User.DeleteAvatar(); err != nil {
  147. ctx.Flash.Error(err.Error())
  148. }
  149. ctx.Redirect(setting.AppSubURL + "/user/settings/avatar")
  150. }
  151. // SettingsPassword render change user's password page
  152. func SettingsPassword(ctx *context.Context) {
  153. ctx.Data["Title"] = ctx.Tr("settings")
  154. ctx.Data["PageIsSettingsPassword"] = true
  155. ctx.HTML(200, tplSettingsPassword)
  156. }
  157. // SettingsPasswordPost response for change user's password
  158. func SettingsPasswordPost(ctx *context.Context, form auth.ChangePasswordForm) {
  159. ctx.Data["Title"] = ctx.Tr("settings")
  160. ctx.Data["PageIsSettingsPassword"] = true
  161. if ctx.HasError() {
  162. ctx.HTML(200, tplSettingsPassword)
  163. return
  164. }
  165. if !ctx.User.ValidatePassword(form.OldPassword) {
  166. ctx.Flash.Error(ctx.Tr("settings.password_incorrect"))
  167. } else if form.Password != form.Retype {
  168. ctx.Flash.Error(ctx.Tr("form.password_not_match"))
  169. } else {
  170. ctx.User.Passwd = form.Password
  171. var err error
  172. if ctx.User.Salt, err = models.GetUserSalt(); err != nil {
  173. ctx.Handle(500, "UpdateUser", err)
  174. return
  175. }
  176. ctx.User.EncodePasswd()
  177. if err := models.UpdateUser(ctx.User); err != nil {
  178. ctx.Handle(500, "UpdateUser", err)
  179. return
  180. }
  181. log.Trace("User password updated: %s", ctx.User.Name)
  182. ctx.Flash.Success(ctx.Tr("settings.change_password_success"))
  183. }
  184. ctx.Redirect(setting.AppSubURL + "/user/settings/password")
  185. }
  186. // SettingsEmails render user's emails page
  187. func SettingsEmails(ctx *context.Context) {
  188. ctx.Data["Title"] = ctx.Tr("settings")
  189. ctx.Data["PageIsSettingsEmails"] = true
  190. emails, err := models.GetEmailAddresses(ctx.User.ID)
  191. if err != nil {
  192. ctx.Handle(500, "GetEmailAddresses", err)
  193. return
  194. }
  195. ctx.Data["Emails"] = emails
  196. ctx.HTML(200, tplSettingsEmails)
  197. }
  198. // SettingsEmailPost response for change user's email
  199. func SettingsEmailPost(ctx *context.Context, form auth.AddEmailForm) {
  200. ctx.Data["Title"] = ctx.Tr("settings")
  201. ctx.Data["PageIsSettingsEmails"] = true
  202. // Make emailaddress primary.
  203. if ctx.Query("_method") == "PRIMARY" {
  204. if err := models.MakeEmailPrimary(&models.EmailAddress{ID: ctx.QueryInt64("id")}); err != nil {
  205. ctx.Handle(500, "MakeEmailPrimary", err)
  206. return
  207. }
  208. log.Trace("Email made primary: %s", ctx.User.Name)
  209. ctx.Redirect(setting.AppSubURL + "/user/settings/email")
  210. return
  211. }
  212. // Add Email address.
  213. emails, err := models.GetEmailAddresses(ctx.User.ID)
  214. if err != nil {
  215. ctx.Handle(500, "GetEmailAddresses", err)
  216. return
  217. }
  218. ctx.Data["Emails"] = emails
  219. if ctx.HasError() {
  220. ctx.HTML(200, tplSettingsEmails)
  221. return
  222. }
  223. email := &models.EmailAddress{
  224. UID: ctx.User.ID,
  225. Email: form.Email,
  226. IsActivated: !setting.Service.RegisterEmailConfirm,
  227. }
  228. if err := models.AddEmailAddress(email); err != nil {
  229. if models.IsErrEmailAlreadyUsed(err) {
  230. ctx.RenderWithErr(ctx.Tr("form.email_been_used"), tplSettingsEmails, &form)
  231. return
  232. }
  233. ctx.Handle(500, "AddEmailAddress", err)
  234. return
  235. }
  236. // Send confirmation email
  237. if setting.Service.RegisterEmailConfirm {
  238. models.SendActivateEmailMail(ctx.Context, ctx.User, email)
  239. if err := ctx.Cache.Put("MailResendLimit_"+ctx.User.LowerName, ctx.User.LowerName, 180); err != nil {
  240. log.Error(4, "Set cache(MailResendLimit) fail: %v", err)
  241. }
  242. ctx.Flash.Info(ctx.Tr("settings.add_email_confirmation_sent", email.Email, setting.Service.ActiveCodeLives/60))
  243. } else {
  244. ctx.Flash.Success(ctx.Tr("settings.add_email_success"))
  245. }
  246. log.Trace("Email address added: %s", email.Email)
  247. ctx.Redirect(setting.AppSubURL + "/user/settings/email")
  248. }
  249. // DeleteEmail response for delete user's email
  250. func DeleteEmail(ctx *context.Context) {
  251. if err := models.DeleteEmailAddress(&models.EmailAddress{ID: ctx.QueryInt64("id"), UID: ctx.User.ID}); err != nil {
  252. ctx.Handle(500, "DeleteEmail", err)
  253. return
  254. }
  255. log.Trace("Email address deleted: %s", ctx.User.Name)
  256. ctx.Flash.Success(ctx.Tr("settings.email_deletion_success"))
  257. ctx.JSON(200, map[string]interface{}{
  258. "redirect": setting.AppSubURL + "/user/settings/email",
  259. })
  260. }
  261. // SettingsSSHKeys render user's SSH public keys page
  262. func SettingsSSHKeys(ctx *context.Context) {
  263. ctx.Data["Title"] = ctx.Tr("settings")
  264. ctx.Data["PageIsSettingsSSHKeys"] = true
  265. keys, err := models.ListPublicKeys(ctx.User.ID)
  266. if err != nil {
  267. ctx.Handle(500, "ListPublicKeys", err)
  268. return
  269. }
  270. ctx.Data["Keys"] = keys
  271. ctx.HTML(200, tplSettingsSSHKeys)
  272. }
  273. // SettingsSSHKeysPost response for change user's SSH keys
  274. func SettingsSSHKeysPost(ctx *context.Context, form auth.AddSSHKeyForm) {
  275. ctx.Data["Title"] = ctx.Tr("settings")
  276. ctx.Data["PageIsSettingsSSHKeys"] = true
  277. keys, err := models.ListPublicKeys(ctx.User.ID)
  278. if err != nil {
  279. ctx.Handle(500, "ListPublicKeys", err)
  280. return
  281. }
  282. ctx.Data["Keys"] = keys
  283. if ctx.HasError() {
  284. ctx.HTML(200, tplSettingsSSHKeys)
  285. return
  286. }
  287. content, err := models.CheckPublicKeyString(form.Content)
  288. if err != nil {
  289. if models.IsErrKeyUnableVerify(err) {
  290. ctx.Flash.Info(ctx.Tr("form.unable_verify_ssh_key"))
  291. } else {
  292. ctx.Flash.Error(ctx.Tr("form.invalid_ssh_key", err.Error()))
  293. ctx.Redirect(setting.AppSubURL + "/user/settings/ssh")
  294. return
  295. }
  296. }
  297. if _, err = models.AddPublicKey(ctx.User.ID, form.Title, content); err != nil {
  298. ctx.Data["HasError"] = true
  299. switch {
  300. case models.IsErrKeyAlreadyExist(err):
  301. ctx.Data["Err_Content"] = true
  302. ctx.RenderWithErr(ctx.Tr("settings.ssh_key_been_used"), tplSettingsSSHKeys, &form)
  303. case models.IsErrKeyNameAlreadyUsed(err):
  304. ctx.Data["Err_Title"] = true
  305. ctx.RenderWithErr(ctx.Tr("settings.ssh_key_name_used"), tplSettingsSSHKeys, &form)
  306. default:
  307. ctx.Handle(500, "AddPublicKey", err)
  308. }
  309. return
  310. }
  311. ctx.Flash.Success(ctx.Tr("settings.add_key_success", form.Title))
  312. ctx.Redirect(setting.AppSubURL + "/user/settings/ssh")
  313. }
  314. // DeleteSSHKey response for delete user's SSH key
  315. func DeleteSSHKey(ctx *context.Context) {
  316. if err := models.DeletePublicKey(ctx.User, ctx.QueryInt64("id")); err != nil {
  317. ctx.Flash.Error("DeletePublicKey: " + err.Error())
  318. } else {
  319. ctx.Flash.Success(ctx.Tr("settings.ssh_key_deletion_success"))
  320. }
  321. ctx.JSON(200, map[string]interface{}{
  322. "redirect": setting.AppSubURL + "/user/settings/ssh",
  323. })
  324. }
  325. // SettingsApplications render user's access tokens page
  326. func SettingsApplications(ctx *context.Context) {
  327. ctx.Data["Title"] = ctx.Tr("settings")
  328. ctx.Data["PageIsSettingsApplications"] = true
  329. tokens, err := models.ListAccessTokens(ctx.User.ID)
  330. if err != nil {
  331. ctx.Handle(500, "ListAccessTokens", err)
  332. return
  333. }
  334. ctx.Data["Tokens"] = tokens
  335. ctx.HTML(200, tplSettingsApplications)
  336. }
  337. // SettingsApplicationsPost response for add user's access token
  338. func SettingsApplicationsPost(ctx *context.Context, form auth.NewAccessTokenForm) {
  339. ctx.Data["Title"] = ctx.Tr("settings")
  340. ctx.Data["PageIsSettingsApplications"] = true
  341. if ctx.HasError() {
  342. tokens, err := models.ListAccessTokens(ctx.User.ID)
  343. if err != nil {
  344. ctx.Handle(500, "ListAccessTokens", err)
  345. return
  346. }
  347. ctx.Data["Tokens"] = tokens
  348. ctx.HTML(200, tplSettingsApplications)
  349. return
  350. }
  351. t := &models.AccessToken{
  352. UID: ctx.User.ID,
  353. Name: form.Name,
  354. }
  355. if err := models.NewAccessToken(t); err != nil {
  356. ctx.Handle(500, "NewAccessToken", err)
  357. return
  358. }
  359. ctx.Flash.Success(ctx.Tr("settings.generate_token_succees"))
  360. ctx.Flash.Info(t.Sha1)
  361. ctx.Redirect(setting.AppSubURL + "/user/settings/applications")
  362. }
  363. // SettingsDeleteApplication response for delete user access token
  364. func SettingsDeleteApplication(ctx *context.Context) {
  365. if err := models.DeleteAccessTokenByID(ctx.QueryInt64("id"), ctx.User.ID); err != nil {
  366. ctx.Flash.Error("DeleteAccessTokenByID: " + err.Error())
  367. } else {
  368. ctx.Flash.Success(ctx.Tr("settings.delete_token_success"))
  369. }
  370. ctx.JSON(200, map[string]interface{}{
  371. "redirect": setting.AppSubURL + "/user/settings/applications",
  372. })
  373. }
  374. // SettingsDelete render user suicide page and response for delete user himself
  375. func SettingsDelete(ctx *context.Context) {
  376. ctx.Data["Title"] = ctx.Tr("settings")
  377. ctx.Data["PageIsSettingsDelete"] = true
  378. if ctx.Req.Method == "POST" {
  379. if _, err := models.UserSignIn(ctx.User.Name, ctx.Query("password")); err != nil {
  380. if models.IsErrUserNotExist(err) {
  381. ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_password"), tplSettingsDelete, nil)
  382. } else {
  383. ctx.Handle(500, "UserSignIn", err)
  384. }
  385. return
  386. }
  387. if err := models.DeleteUser(ctx.User); err != nil {
  388. switch {
  389. case models.IsErrUserOwnRepos(err):
  390. ctx.Flash.Error(ctx.Tr("form.still_own_repo"))
  391. ctx.Redirect(setting.AppSubURL + "/user/settings/delete")
  392. case models.IsErrUserHasOrgs(err):
  393. ctx.Flash.Error(ctx.Tr("form.still_has_org"))
  394. ctx.Redirect(setting.AppSubURL + "/user/settings/delete")
  395. default:
  396. ctx.Handle(500, "DeleteUser", err)
  397. }
  398. } else {
  399. log.Trace("Account deleted: %s", ctx.User.Name)
  400. ctx.Redirect(setting.AppSubURL + "/")
  401. }
  402. return
  403. }
  404. ctx.HTML(200, tplSettingsDelete)
  405. }