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 24 kB

11 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
9 years ago
12 years ago
10 years ago
12 years ago
12 years ago
12 years ago
12 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
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
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
9 years ago
12 years ago
9 years ago
12 years ago
12 years ago
Oauth2 consumer (#679) * initial stuff for oauth2 login, fails on: * login button on the signIn page to start the OAuth2 flow and a callback for each provider Only GitHub is implemented for now * show login button only when the OAuth2 consumer is configured (and activated) * create macaron group for oauth2 urls * prevent net/http in modules (other then oauth2) * use a new data sessions oauth2 folder for storing the oauth2 session data * add missing 2FA when this is enabled on the user * add password option for OAuth2 user , for use with git over http and login to the GUI * add tip for registering a GitHub OAuth application * at startup of Gitea register all configured providers and also on adding/deleting of new providers * custom handling of errors in oauth2 request init + show better tip * add ExternalLoginUser model and migration script to add it to database * link a external account to an existing account (still need to handle wrong login and signup) and remove if user is removed * remove the linked external account from the user his settings * if user is unknown we allow him to register a new account or link it to some existing account * sign up with button on signin page (als change OAuth2Provider structure so we can store basic stuff about providers) * from gorilla/sessions docs: "Important Note: If you aren't using gorilla/mux, you need to wrap your handlers with context.ClearHandler as or else you will leak memory!" (we're using gorilla/sessions for storing oauth2 sessions) * use updated goth lib that now supports getting the OAuth2 user if the AccessToken is still valid instead of re-authenticating (prevent flooding the OAuth2 provider)
9 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  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. "bytes"
  7. "errors"
  8. "fmt"
  9. "io/ioutil"
  10. "strings"
  11. "github.com/Unknwon/com"
  12. "github.com/pquerna/otp"
  13. "github.com/pquerna/otp/totp"
  14. "encoding/base64"
  15. "html/template"
  16. "image/png"
  17. "code.gitea.io/gitea/models"
  18. "code.gitea.io/gitea/modules/auth"
  19. "code.gitea.io/gitea/modules/base"
  20. "code.gitea.io/gitea/modules/context"
  21. "code.gitea.io/gitea/modules/log"
  22. "code.gitea.io/gitea/modules/setting"
  23. )
  24. const (
  25. tplSettingsProfile base.TplName = "user/settings/profile"
  26. tplSettingsAvatar base.TplName = "user/settings/avatar"
  27. tplSettingsPassword base.TplName = "user/settings/password"
  28. tplSettingsEmails base.TplName = "user/settings/email"
  29. tplSettingsKeys base.TplName = "user/settings/keys"
  30. tplSettingsSocial base.TplName = "user/settings/social"
  31. tplSettingsApplications base.TplName = "user/settings/applications"
  32. tplSettingsTwofa base.TplName = "user/settings/twofa"
  33. tplSettingsTwofaEnroll base.TplName = "user/settings/twofa_enroll"
  34. tplSettingsAccountLink base.TplName = "user/settings/account_link"
  35. tplSettingsOrganization base.TplName = "user/settings/organization"
  36. tplSettingsRepositories base.TplName = "user/settings/repos"
  37. tplSettingsDelete base.TplName = "user/settings/delete"
  38. tplSecurity base.TplName = "user/security"
  39. )
  40. // Settings render user's profile page
  41. func Settings(ctx *context.Context) {
  42. ctx.Data["Title"] = ctx.Tr("settings")
  43. ctx.Data["PageIsSettingsProfile"] = true
  44. ctx.HTML(200, tplSettingsProfile)
  45. }
  46. func handleUsernameChange(ctx *context.Context, newName string) {
  47. // Non-local users are not allowed to change their username.
  48. if len(newName) == 0 || !ctx.User.IsLocal() {
  49. return
  50. }
  51. // Check if user name has been changed
  52. if ctx.User.LowerName != strings.ToLower(newName) {
  53. if err := models.ChangeUserName(ctx.User, newName); err != nil {
  54. switch {
  55. case models.IsErrUserAlreadyExist(err):
  56. ctx.Flash.Error(ctx.Tr("newName_been_taken"))
  57. ctx.Redirect(setting.AppSubURL + "/user/settings")
  58. case models.IsErrEmailAlreadyUsed(err):
  59. ctx.Flash.Error(ctx.Tr("form.email_been_used"))
  60. ctx.Redirect(setting.AppSubURL + "/user/settings")
  61. case models.IsErrNameReserved(err):
  62. ctx.Flash.Error(ctx.Tr("user.newName_reserved"))
  63. ctx.Redirect(setting.AppSubURL + "/user/settings")
  64. case models.IsErrNamePatternNotAllowed(err):
  65. ctx.Flash.Error(ctx.Tr("user.newName_pattern_not_allowed"))
  66. ctx.Redirect(setting.AppSubURL + "/user/settings")
  67. default:
  68. ctx.Handle(500, "ChangeUserName", err)
  69. }
  70. return
  71. }
  72. log.Trace("User name changed: %s -> %s", ctx.User.Name, newName)
  73. }
  74. // In case it's just a case change
  75. ctx.User.Name = newName
  76. ctx.User.LowerName = strings.ToLower(newName)
  77. }
  78. // SettingsPost response for change user's profile
  79. func SettingsPost(ctx *context.Context, form auth.UpdateProfileForm) {
  80. ctx.Data["Title"] = ctx.Tr("settings")
  81. ctx.Data["PageIsSettingsProfile"] = true
  82. if ctx.HasError() {
  83. ctx.HTML(200, tplSettingsProfile)
  84. return
  85. }
  86. handleUsernameChange(ctx, form.Name)
  87. if ctx.Written() {
  88. return
  89. }
  90. ctx.User.FullName = form.FullName
  91. ctx.User.Email = form.Email
  92. ctx.User.KeepEmailPrivate = form.KeepEmailPrivate
  93. ctx.User.Website = form.Website
  94. ctx.User.Location = form.Location
  95. if err := models.UpdateUserSetting(ctx.User); err != nil {
  96. if _, ok := err.(models.ErrEmailAlreadyUsed); ok {
  97. ctx.Flash.Error(ctx.Tr("form.email_been_used"))
  98. ctx.Redirect(setting.AppSubURL + "/user/settings")
  99. return
  100. }
  101. ctx.Handle(500, "UpdateUser", err)
  102. return
  103. }
  104. log.Trace("User settings updated: %s", ctx.User.Name)
  105. ctx.Flash.Success(ctx.Tr("settings.update_profile_success"))
  106. ctx.Redirect(setting.AppSubURL + "/user/settings")
  107. }
  108. // UpdateAvatarSetting update user's avatar
  109. // FIXME: limit size.
  110. func UpdateAvatarSetting(ctx *context.Context, form auth.AvatarForm, ctxUser *models.User) error {
  111. ctxUser.UseCustomAvatar = form.Source == auth.AvatarLocal
  112. if len(form.Gravatar) > 0 {
  113. ctxUser.Avatar = base.EncodeMD5(form.Gravatar)
  114. ctxUser.AvatarEmail = form.Gravatar
  115. }
  116. if form.Avatar != nil {
  117. fr, err := form.Avatar.Open()
  118. if err != nil {
  119. return fmt.Errorf("Avatar.Open: %v", err)
  120. }
  121. defer fr.Close()
  122. data, err := ioutil.ReadAll(fr)
  123. if err != nil {
  124. return fmt.Errorf("ioutil.ReadAll: %v", err)
  125. }
  126. if !base.IsImageFile(data) {
  127. return errors.New(ctx.Tr("settings.uploaded_avatar_not_a_image"))
  128. }
  129. if err = ctxUser.UploadAvatar(data); err != nil {
  130. return fmt.Errorf("UploadAvatar: %v", err)
  131. }
  132. } else {
  133. // No avatar is uploaded but setting has been changed to enable,
  134. // generate a random one when needed.
  135. if ctxUser.UseCustomAvatar && !com.IsFile(ctxUser.CustomAvatarPath()) {
  136. if err := ctxUser.GenerateRandomAvatar(); err != nil {
  137. log.Error(4, "GenerateRandomAvatar[%d]: %v", ctxUser.ID, err)
  138. }
  139. }
  140. }
  141. if err := models.UpdateUserCols(ctxUser, "avatar", "avatar_email", "use_custom_avatar"); err != nil {
  142. return fmt.Errorf("UpdateUser: %v", err)
  143. }
  144. return nil
  145. }
  146. // SettingsAvatar render user avatar page
  147. func SettingsAvatar(ctx *context.Context) {
  148. ctx.Data["Title"] = ctx.Tr("settings")
  149. ctx.Data["PageIsSettingsAvatar"] = true
  150. ctx.HTML(200, tplSettingsAvatar)
  151. }
  152. // SettingsAvatarPost response for change user's avatar request
  153. func SettingsAvatarPost(ctx *context.Context, form auth.AvatarForm) {
  154. if err := UpdateAvatarSetting(ctx, form, ctx.User); err != nil {
  155. ctx.Flash.Error(err.Error())
  156. } else {
  157. ctx.Flash.Success(ctx.Tr("settings.update_avatar_success"))
  158. }
  159. ctx.Redirect(setting.AppSubURL + "/user/settings/avatar")
  160. }
  161. // SettingsDeleteAvatar render delete avatar page
  162. func SettingsDeleteAvatar(ctx *context.Context) {
  163. if err := ctx.User.DeleteAvatar(); err != nil {
  164. ctx.Flash.Error(err.Error())
  165. }
  166. ctx.Redirect(setting.AppSubURL + "/user/settings/avatar")
  167. }
  168. // SettingsPassword render change user's password page
  169. func SettingsPassword(ctx *context.Context) {
  170. ctx.Data["Title"] = ctx.Tr("settings")
  171. ctx.Data["PageIsSettingsPassword"] = true
  172. ctx.Data["Email"] = ctx.User.Email
  173. ctx.HTML(200, tplSettingsPassword)
  174. }
  175. // SettingsPasswordPost response for change user's password
  176. func SettingsPasswordPost(ctx *context.Context, form auth.ChangePasswordForm) {
  177. ctx.Data["Title"] = ctx.Tr("settings")
  178. ctx.Data["PageIsSettingsPassword"] = true
  179. ctx.Data["PageIsSettingsDelete"] = true
  180. if ctx.HasError() {
  181. ctx.HTML(200, tplSettingsPassword)
  182. return
  183. }
  184. if ctx.User.IsPasswordSet() && !ctx.User.ValidatePassword(form.OldPassword) {
  185. ctx.Flash.Error(ctx.Tr("settings.password_incorrect"))
  186. } else if form.Password != form.Retype {
  187. ctx.Flash.Error(ctx.Tr("form.password_not_match"))
  188. } else {
  189. ctx.User.Passwd = form.Password
  190. var err error
  191. if ctx.User.Salt, err = models.GetUserSalt(); err != nil {
  192. ctx.Handle(500, "UpdateUser", err)
  193. return
  194. }
  195. ctx.User.EncodePasswd()
  196. if err := models.UpdateUserCols(ctx.User, "salt", "passwd"); err != nil {
  197. ctx.Handle(500, "UpdateUser", err)
  198. return
  199. }
  200. log.Trace("User password updated: %s", ctx.User.Name)
  201. ctx.Flash.Success(ctx.Tr("settings.change_password_success"))
  202. }
  203. ctx.Redirect(setting.AppSubURL + "/user/settings/password")
  204. }
  205. // SettingsEmails render user's emails page
  206. func SettingsEmails(ctx *context.Context) {
  207. ctx.Data["Title"] = ctx.Tr("settings")
  208. ctx.Data["PageIsSettingsEmails"] = true
  209. emails, err := models.GetEmailAddresses(ctx.User.ID)
  210. if err != nil {
  211. ctx.Handle(500, "GetEmailAddresses", err)
  212. return
  213. }
  214. ctx.Data["Emails"] = emails
  215. ctx.HTML(200, tplSettingsEmails)
  216. }
  217. // SettingsEmailPost response for change user's email
  218. func SettingsEmailPost(ctx *context.Context, form auth.AddEmailForm) {
  219. ctx.Data["Title"] = ctx.Tr("settings")
  220. ctx.Data["PageIsSettingsEmails"] = true
  221. // Make emailaddress primary.
  222. if ctx.Query("_method") == "PRIMARY" {
  223. if err := models.MakeEmailPrimary(&models.EmailAddress{ID: ctx.QueryInt64("id")}); err != nil {
  224. ctx.Handle(500, "MakeEmailPrimary", err)
  225. return
  226. }
  227. log.Trace("Email made primary: %s", ctx.User.Name)
  228. ctx.Redirect(setting.AppSubURL + "/user/settings/email")
  229. return
  230. }
  231. // Add Email address.
  232. emails, err := models.GetEmailAddresses(ctx.User.ID)
  233. if err != nil {
  234. ctx.Handle(500, "GetEmailAddresses", err)
  235. return
  236. }
  237. ctx.Data["Emails"] = emails
  238. if ctx.HasError() {
  239. ctx.HTML(200, tplSettingsEmails)
  240. return
  241. }
  242. email := &models.EmailAddress{
  243. UID: ctx.User.ID,
  244. Email: form.Email,
  245. IsActivated: !setting.Service.RegisterEmailConfirm,
  246. }
  247. if err := models.AddEmailAddress(email); err != nil {
  248. if models.IsErrEmailAlreadyUsed(err) {
  249. ctx.RenderWithErr(ctx.Tr("form.email_been_used"), tplSettingsEmails, &form)
  250. return
  251. }
  252. ctx.Handle(500, "AddEmailAddress", err)
  253. return
  254. }
  255. // Send confirmation email
  256. if setting.Service.RegisterEmailConfirm {
  257. models.SendActivateEmailMail(ctx.Context, ctx.User, email)
  258. if err := ctx.Cache.Put("MailResendLimit_"+ctx.User.LowerName, ctx.User.LowerName, 180); err != nil {
  259. log.Error(4, "Set cache(MailResendLimit) fail: %v", err)
  260. }
  261. ctx.Flash.Info(ctx.Tr("settings.add_email_confirmation_sent", email.Email, base.MinutesToFriendly(setting.Service.ActiveCodeLives, ctx.Locale.Language())))
  262. } else {
  263. ctx.Flash.Success(ctx.Tr("settings.add_email_success"))
  264. }
  265. log.Trace("Email address added: %s", email.Email)
  266. ctx.Redirect(setting.AppSubURL + "/user/settings/email")
  267. }
  268. // DeleteEmail response for delete user's email
  269. func DeleteEmail(ctx *context.Context) {
  270. if err := models.DeleteEmailAddress(&models.EmailAddress{ID: ctx.QueryInt64("id"), UID: ctx.User.ID}); err != nil {
  271. ctx.Handle(500, "DeleteEmail", err)
  272. return
  273. }
  274. log.Trace("Email address deleted: %s", ctx.User.Name)
  275. ctx.Flash.Success(ctx.Tr("settings.email_deletion_success"))
  276. ctx.JSON(200, map[string]interface{}{
  277. "redirect": setting.AppSubURL + "/user/settings/email",
  278. })
  279. }
  280. // SettingsKeys render user's SSH/GPG public keys page
  281. func SettingsKeys(ctx *context.Context) {
  282. ctx.Data["Title"] = ctx.Tr("settings")
  283. ctx.Data["PageIsSettingsKeys"] = true
  284. keys, err := models.ListPublicKeys(ctx.User.ID)
  285. if err != nil {
  286. ctx.Handle(500, "ListPublicKeys", err)
  287. return
  288. }
  289. ctx.Data["Keys"] = keys
  290. gpgkeys, err := models.ListGPGKeys(ctx.User.ID)
  291. if err != nil {
  292. ctx.Handle(500, "ListGPGKeys", err)
  293. return
  294. }
  295. ctx.Data["GPGKeys"] = gpgkeys
  296. ctx.HTML(200, tplSettingsKeys)
  297. }
  298. // SettingsKeysPost response for change user's SSH/GPG keys
  299. func SettingsKeysPost(ctx *context.Context, form auth.AddKeyForm) {
  300. ctx.Data["Title"] = ctx.Tr("settings")
  301. ctx.Data["PageIsSettingsKeys"] = true
  302. keys, err := models.ListPublicKeys(ctx.User.ID)
  303. if err != nil {
  304. ctx.Handle(500, "ListPublicKeys", err)
  305. return
  306. }
  307. ctx.Data["Keys"] = keys
  308. gpgkeys, err := models.ListGPGKeys(ctx.User.ID)
  309. if err != nil {
  310. ctx.Handle(500, "ListGPGKeys", err)
  311. return
  312. }
  313. ctx.Data["GPGKeys"] = gpgkeys
  314. if ctx.HasError() {
  315. ctx.HTML(200, tplSettingsKeys)
  316. return
  317. }
  318. switch form.Type {
  319. case "gpg":
  320. key, err := models.AddGPGKey(ctx.User.ID, form.Content)
  321. if err != nil {
  322. ctx.Data["HasGPGError"] = true
  323. switch {
  324. case models.IsErrGPGKeyParsing(err):
  325. ctx.Flash.Error(ctx.Tr("form.invalid_gpg_key", err.Error()))
  326. ctx.Redirect(setting.AppSubURL + "/user/settings/keys")
  327. case models.IsErrGPGKeyIDAlreadyUsed(err):
  328. ctx.Data["Err_Content"] = true
  329. ctx.RenderWithErr(ctx.Tr("settings.gpg_key_id_used"), tplSettingsKeys, &form)
  330. case models.IsErrGPGNoEmailFound(err):
  331. ctx.Data["Err_Content"] = true
  332. ctx.RenderWithErr(ctx.Tr("settings.gpg_no_key_email_found"), tplSettingsKeys, &form)
  333. default:
  334. ctx.Handle(500, "AddPublicKey", err)
  335. }
  336. return
  337. }
  338. ctx.Flash.Success(ctx.Tr("settings.add_gpg_key_success", key.KeyID))
  339. ctx.Redirect(setting.AppSubURL + "/user/settings/keys")
  340. case "ssh":
  341. content, err := models.CheckPublicKeyString(form.Content)
  342. if err != nil {
  343. if models.IsErrKeyUnableVerify(err) {
  344. ctx.Flash.Info(ctx.Tr("form.unable_verify_ssh_key"))
  345. } else {
  346. ctx.Flash.Error(ctx.Tr("form.invalid_ssh_key", err.Error()))
  347. ctx.Redirect(setting.AppSubURL + "/user/settings/keys")
  348. return
  349. }
  350. }
  351. if _, err = models.AddPublicKey(ctx.User.ID, form.Title, content); err != nil {
  352. ctx.Data["HasSSHError"] = true
  353. switch {
  354. case models.IsErrKeyAlreadyExist(err):
  355. ctx.Data["Err_Content"] = true
  356. ctx.RenderWithErr(ctx.Tr("settings.ssh_key_been_used"), tplSettingsKeys, &form)
  357. case models.IsErrKeyNameAlreadyUsed(err):
  358. ctx.Data["Err_Title"] = true
  359. ctx.RenderWithErr(ctx.Tr("settings.ssh_key_name_used"), tplSettingsKeys, &form)
  360. default:
  361. ctx.Handle(500, "AddPublicKey", err)
  362. }
  363. return
  364. }
  365. ctx.Flash.Success(ctx.Tr("settings.add_key_success", form.Title))
  366. ctx.Redirect(setting.AppSubURL + "/user/settings/keys")
  367. default:
  368. ctx.Flash.Warning("Function not implemented")
  369. ctx.Redirect(setting.AppSubURL + "/user/settings/keys")
  370. }
  371. }
  372. // DeleteKey response for delete user's SSH/GPG key
  373. func DeleteKey(ctx *context.Context) {
  374. switch ctx.Query("type") {
  375. case "gpg":
  376. if err := models.DeleteGPGKey(ctx.User, ctx.QueryInt64("id")); err != nil {
  377. ctx.Flash.Error("DeleteGPGKey: " + err.Error())
  378. } else {
  379. ctx.Flash.Success(ctx.Tr("settings.gpg_key_deletion_success"))
  380. }
  381. case "ssh":
  382. if err := models.DeletePublicKey(ctx.User, ctx.QueryInt64("id")); err != nil {
  383. ctx.Flash.Error("DeletePublicKey: " + err.Error())
  384. } else {
  385. ctx.Flash.Success(ctx.Tr("settings.ssh_key_deletion_success"))
  386. }
  387. default:
  388. ctx.Flash.Warning("Function not implemented")
  389. ctx.Redirect(setting.AppSubURL + "/user/settings/keys")
  390. }
  391. ctx.JSON(200, map[string]interface{}{
  392. "redirect": setting.AppSubURL + "/user/settings/keys",
  393. })
  394. }
  395. // SettingsApplications render user's access tokens page
  396. func SettingsApplications(ctx *context.Context) {
  397. ctx.Data["Title"] = ctx.Tr("settings")
  398. ctx.Data["PageIsSettingsApplications"] = true
  399. tokens, err := models.ListAccessTokens(ctx.User.ID)
  400. if err != nil {
  401. ctx.Handle(500, "ListAccessTokens", err)
  402. return
  403. }
  404. ctx.Data["Tokens"] = tokens
  405. ctx.HTML(200, tplSettingsApplications)
  406. }
  407. // SettingsApplicationsPost response for add user's access token
  408. func SettingsApplicationsPost(ctx *context.Context, form auth.NewAccessTokenForm) {
  409. ctx.Data["Title"] = ctx.Tr("settings")
  410. ctx.Data["PageIsSettingsApplications"] = true
  411. if ctx.HasError() {
  412. tokens, err := models.ListAccessTokens(ctx.User.ID)
  413. if err != nil {
  414. ctx.Handle(500, "ListAccessTokens", err)
  415. return
  416. }
  417. ctx.Data["Tokens"] = tokens
  418. ctx.HTML(200, tplSettingsApplications)
  419. return
  420. }
  421. t := &models.AccessToken{
  422. UID: ctx.User.ID,
  423. Name: form.Name,
  424. }
  425. if err := models.NewAccessToken(t); err != nil {
  426. ctx.Handle(500, "NewAccessToken", err)
  427. return
  428. }
  429. ctx.Flash.Success(ctx.Tr("settings.generate_token_success"))
  430. ctx.Flash.Info(t.Sha1)
  431. ctx.Redirect(setting.AppSubURL + "/user/settings/applications")
  432. }
  433. // SettingsDeleteApplication response for delete user access token
  434. func SettingsDeleteApplication(ctx *context.Context) {
  435. if err := models.DeleteAccessTokenByID(ctx.QueryInt64("id"), ctx.User.ID); err != nil {
  436. ctx.Flash.Error("DeleteAccessTokenByID: " + err.Error())
  437. } else {
  438. ctx.Flash.Success(ctx.Tr("settings.delete_token_success"))
  439. }
  440. ctx.JSON(200, map[string]interface{}{
  441. "redirect": setting.AppSubURL + "/user/settings/applications",
  442. })
  443. }
  444. // SettingsTwoFactor renders the 2FA page.
  445. func SettingsTwoFactor(ctx *context.Context) {
  446. ctx.Data["Title"] = ctx.Tr("settings")
  447. ctx.Data["PageIsSettingsTwofa"] = true
  448. enrolled := true
  449. _, err := models.GetTwoFactorByUID(ctx.User.ID)
  450. if err != nil {
  451. if models.IsErrTwoFactorNotEnrolled(err) {
  452. enrolled = false
  453. } else {
  454. ctx.Handle(500, "SettingsTwoFactor", err)
  455. return
  456. }
  457. }
  458. ctx.Data["TwofaEnrolled"] = enrolled
  459. ctx.HTML(200, tplSettingsTwofa)
  460. }
  461. // SettingsTwoFactorRegenerateScratch regenerates the user's 2FA scratch code.
  462. func SettingsTwoFactorRegenerateScratch(ctx *context.Context) {
  463. ctx.Data["Title"] = ctx.Tr("settings")
  464. ctx.Data["PageIsSettingsTwofa"] = true
  465. t, err := models.GetTwoFactorByUID(ctx.User.ID)
  466. if err != nil {
  467. ctx.Handle(500, "SettingsTwoFactor", err)
  468. return
  469. }
  470. if err = t.GenerateScratchToken(); err != nil {
  471. ctx.Handle(500, "SettingsTwoFactor", err)
  472. return
  473. }
  474. if err = models.UpdateTwoFactor(t); err != nil {
  475. ctx.Handle(500, "SettingsTwoFactor", err)
  476. return
  477. }
  478. ctx.Flash.Success(ctx.Tr("settings.twofa_scratch_token_regenerated", t.ScratchToken))
  479. ctx.Redirect(setting.AppSubURL + "/user/settings/two_factor")
  480. }
  481. // SettingsTwoFactorDisable deletes the user's 2FA settings.
  482. func SettingsTwoFactorDisable(ctx *context.Context) {
  483. ctx.Data["Title"] = ctx.Tr("settings")
  484. ctx.Data["PageIsSettingsTwofa"] = true
  485. t, err := models.GetTwoFactorByUID(ctx.User.ID)
  486. if err != nil {
  487. ctx.Handle(500, "SettingsTwoFactor", err)
  488. return
  489. }
  490. if err = models.DeleteTwoFactorByID(t.ID, ctx.User.ID); err != nil {
  491. ctx.Handle(500, "SettingsTwoFactor", err)
  492. return
  493. }
  494. ctx.Flash.Success(ctx.Tr("settings.twofa_disabled"))
  495. ctx.Redirect(setting.AppSubURL + "/user/settings/two_factor")
  496. }
  497. func twofaGenerateSecretAndQr(ctx *context.Context) bool {
  498. var otpKey *otp.Key
  499. var err error
  500. uri := ctx.Session.Get("twofaUri")
  501. if uri != nil {
  502. otpKey, err = otp.NewKeyFromURL(uri.(string))
  503. }
  504. if otpKey == nil {
  505. err = nil // clear the error, in case the URL was invalid
  506. otpKey, err = totp.Generate(totp.GenerateOpts{
  507. Issuer: setting.AppName + " (" + strings.TrimRight(setting.AppURL, "/") + ")",
  508. AccountName: ctx.User.Name,
  509. })
  510. if err != nil {
  511. ctx.Handle(500, "SettingsTwoFactor", err)
  512. return false
  513. }
  514. }
  515. ctx.Data["TwofaSecret"] = otpKey.Secret()
  516. img, err := otpKey.Image(320, 240)
  517. if err != nil {
  518. ctx.Handle(500, "SettingsTwoFactor", err)
  519. return false
  520. }
  521. var imgBytes bytes.Buffer
  522. if err = png.Encode(&imgBytes, img); err != nil {
  523. ctx.Handle(500, "SettingsTwoFactor", err)
  524. return false
  525. }
  526. ctx.Data["QrUri"] = template.URL("data:image/png;base64," + base64.StdEncoding.EncodeToString(imgBytes.Bytes()))
  527. ctx.Session.Set("twofaSecret", otpKey.Secret())
  528. ctx.Session.Set("twofaUri", otpKey.String())
  529. return true
  530. }
  531. // SettingsTwoFactorEnroll shows the page where the user can enroll into 2FA.
  532. func SettingsTwoFactorEnroll(ctx *context.Context) {
  533. ctx.Data["Title"] = ctx.Tr("settings")
  534. ctx.Data["PageIsSettingsTwofa"] = true
  535. t, err := models.GetTwoFactorByUID(ctx.User.ID)
  536. if t != nil {
  537. // already enrolled
  538. ctx.Handle(500, "SettingsTwoFactor", err)
  539. return
  540. }
  541. if err != nil && !models.IsErrTwoFactorNotEnrolled(err) {
  542. ctx.Handle(500, "SettingsTwoFactor", err)
  543. return
  544. }
  545. if !twofaGenerateSecretAndQr(ctx) {
  546. return
  547. }
  548. ctx.HTML(200, tplSettingsTwofaEnroll)
  549. }
  550. // SettingsTwoFactorEnrollPost handles enrolling the user into 2FA.
  551. func SettingsTwoFactorEnrollPost(ctx *context.Context, form auth.TwoFactorAuthForm) {
  552. ctx.Data["Title"] = ctx.Tr("settings")
  553. ctx.Data["PageIsSettingsTwofa"] = true
  554. t, err := models.GetTwoFactorByUID(ctx.User.ID)
  555. if t != nil {
  556. // already enrolled
  557. ctx.Handle(500, "SettingsTwoFactor", err)
  558. return
  559. }
  560. if err != nil && !models.IsErrTwoFactorNotEnrolled(err) {
  561. ctx.Handle(500, "SettingsTwoFactor", err)
  562. return
  563. }
  564. if ctx.HasError() {
  565. if !twofaGenerateSecretAndQr(ctx) {
  566. return
  567. }
  568. ctx.HTML(200, tplSettingsTwofaEnroll)
  569. return
  570. }
  571. secret := ctx.Session.Get("twofaSecret").(string)
  572. if !totp.Validate(form.Passcode, secret) {
  573. if !twofaGenerateSecretAndQr(ctx) {
  574. return
  575. }
  576. ctx.Flash.Error(ctx.Tr("settings.passcode_invalid"))
  577. ctx.HTML(200, tplSettingsTwofaEnroll)
  578. return
  579. }
  580. t = &models.TwoFactor{
  581. UID: ctx.User.ID,
  582. }
  583. err = t.SetSecret(secret)
  584. if err != nil {
  585. ctx.Handle(500, "SettingsTwoFactor", err)
  586. return
  587. }
  588. err = t.GenerateScratchToken()
  589. if err != nil {
  590. ctx.Handle(500, "SettingsTwoFactor", err)
  591. return
  592. }
  593. if err = models.NewTwoFactor(t); err != nil {
  594. ctx.Handle(500, "SettingsTwoFactor", err)
  595. return
  596. }
  597. ctx.Session.Delete("twofaSecret")
  598. ctx.Session.Delete("twofaUri")
  599. ctx.Flash.Success(ctx.Tr("settings.twofa_enrolled", t.ScratchToken))
  600. ctx.Redirect(setting.AppSubURL + "/user/settings/two_factor")
  601. }
  602. // SettingsAccountLinks render the account links settings page
  603. func SettingsAccountLinks(ctx *context.Context) {
  604. ctx.Data["Title"] = ctx.Tr("settings")
  605. ctx.Data["PageIsSettingsAccountLink"] = true
  606. accountLinks, err := models.ListAccountLinks(ctx.User)
  607. if err != nil {
  608. ctx.Handle(500, "ListAccountLinks", err)
  609. return
  610. }
  611. // map the provider display name with the LoginSource
  612. sources := make(map[*models.LoginSource]string)
  613. for _, externalAccount := range accountLinks {
  614. if loginSource, err := models.GetLoginSourceByID(externalAccount.LoginSourceID); err == nil {
  615. var providerDisplayName string
  616. if loginSource.IsOAuth2() {
  617. providerTechnicalName := loginSource.OAuth2().Provider
  618. providerDisplayName = models.OAuth2Providers[providerTechnicalName].DisplayName
  619. } else {
  620. providerDisplayName = loginSource.Name
  621. }
  622. sources[loginSource] = providerDisplayName
  623. }
  624. }
  625. ctx.Data["AccountLinks"] = sources
  626. ctx.HTML(200, tplSettingsAccountLink)
  627. }
  628. // SettingsDeleteAccountLink delete a single account link
  629. func SettingsDeleteAccountLink(ctx *context.Context) {
  630. if _, err := models.RemoveAccountLink(ctx.User, ctx.QueryInt64("loginSourceID")); err != nil {
  631. ctx.Flash.Error("RemoveAccountLink: " + err.Error())
  632. } else {
  633. ctx.Flash.Success(ctx.Tr("settings.remove_account_link_success"))
  634. }
  635. ctx.JSON(200, map[string]interface{}{
  636. "redirect": setting.AppSubURL + "/user/settings/account_link",
  637. })
  638. }
  639. // SettingsDelete render user suicide page and response for delete user himself
  640. func SettingsDelete(ctx *context.Context) {
  641. ctx.Data["Title"] = ctx.Tr("settings")
  642. ctx.Data["PageIsSettingsDelete"] = true
  643. ctx.Data["Email"] = ctx.User.Email
  644. if ctx.Req.Method == "POST" {
  645. if _, err := models.UserSignIn(ctx.User.Name, ctx.Query("password")); err != nil {
  646. if models.IsErrUserNotExist(err) {
  647. ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_password"), tplSettingsDelete, nil)
  648. } else {
  649. ctx.Handle(500, "UserSignIn", err)
  650. }
  651. return
  652. }
  653. if err := models.DeleteUser(ctx.User); err != nil {
  654. switch {
  655. case models.IsErrUserOwnRepos(err):
  656. ctx.Flash.Error(ctx.Tr("form.still_own_repo"))
  657. ctx.Redirect(setting.AppSubURL + "/user/settings/delete")
  658. case models.IsErrUserHasOrgs(err):
  659. ctx.Flash.Error(ctx.Tr("form.still_has_org"))
  660. ctx.Redirect(setting.AppSubURL + "/user/settings/delete")
  661. default:
  662. ctx.Handle(500, "DeleteUser", err)
  663. }
  664. } else {
  665. log.Trace("Account deleted: %s", ctx.User.Name)
  666. ctx.Redirect(setting.AppSubURL + "/")
  667. }
  668. return
  669. }
  670. ctx.HTML(200, tplSettingsDelete)
  671. }
  672. // SettingsOrganization render all the organization of the user
  673. func SettingsOrganization(ctx *context.Context) {
  674. ctx.Data["Title"] = ctx.Tr("settings")
  675. ctx.Data["PageIsSettingsOrganization"] = true
  676. orgs, err := models.GetOrgsByUserID(ctx.User.ID, ctx.IsSigned)
  677. if err != nil {
  678. ctx.Handle(500, "GetOrgsByUserID", err)
  679. return
  680. }
  681. ctx.Data["Orgs"] = orgs
  682. ctx.HTML(200, tplSettingsOrganization)
  683. }
  684. // SettingsRepos display a list of all repositories of the user
  685. func SettingsRepos(ctx *context.Context) {
  686. ctx.Data["Title"] = ctx.Tr("settings")
  687. ctx.Data["PageIsSettingsRepos"] = true
  688. ctxUser := ctx.User
  689. var err error
  690. if err = ctxUser.GetRepositories(1, setting.UI.User.RepoPagingNum); err != nil {
  691. ctx.Handle(500, "GetRepositories", err)
  692. return
  693. }
  694. repos := ctxUser.Repos
  695. for i := range repos {
  696. if repos[i].IsFork {
  697. err := repos[i].GetBaseRepo()
  698. if err != nil {
  699. ctx.Handle(500, "GetBaseRepo", err)
  700. return
  701. }
  702. err = repos[i].BaseRepo.GetOwner()
  703. if err != nil {
  704. ctx.Handle(500, "GetOwner", err)
  705. return
  706. }
  707. }
  708. }
  709. ctx.Data["Owner"] = ctxUser
  710. ctx.Data["Repos"] = repos
  711. ctx.HTML(200, tplSettingsRepositories)
  712. }