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.

applications.go 2.8 kB

API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Copyright 2018 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package setting
  6. import (
  7. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/modules/auth"
  9. "code.gitea.io/gitea/modules/base"
  10. "code.gitea.io/gitea/modules/context"
  11. "code.gitea.io/gitea/modules/setting"
  12. )
  13. const (
  14. tplSettingsApplications base.TplName = "user/settings/applications"
  15. )
  16. // Applications render manage access token page
  17. func Applications(ctx *context.Context) {
  18. ctx.Data["Title"] = ctx.Tr("settings")
  19. ctx.Data["PageIsSettingsApplications"] = true
  20. loadApplicationsData(ctx)
  21. ctx.HTML(200, tplSettingsApplications)
  22. }
  23. // ApplicationsPost response for add user's access token
  24. func ApplicationsPost(ctx *context.Context, form auth.NewAccessTokenForm) {
  25. ctx.Data["Title"] = ctx.Tr("settings")
  26. ctx.Data["PageIsSettingsApplications"] = true
  27. if ctx.HasError() {
  28. loadApplicationsData(ctx)
  29. ctx.HTML(200, tplSettingsApplications)
  30. return
  31. }
  32. t := &models.AccessToken{
  33. UID: ctx.User.ID,
  34. Name: form.Name,
  35. }
  36. exist, err := models.AccessTokenByNameExists(t)
  37. if err != nil {
  38. ctx.ServerError("AccessTokenByNameExists", err)
  39. return
  40. }
  41. if exist {
  42. ctx.Flash.Error(ctx.Tr("settings.generate_token_name_duplicate", t.Name))
  43. ctx.Redirect(setting.AppSubURL + "/user/settings/applications")
  44. return
  45. }
  46. if err := models.NewAccessToken(t); err != nil {
  47. ctx.ServerError("NewAccessToken", err)
  48. return
  49. }
  50. ctx.Flash.Success(ctx.Tr("settings.generate_token_success"))
  51. ctx.Flash.Info(t.Token)
  52. ctx.Redirect(setting.AppSubURL + "/user/settings/applications")
  53. }
  54. // DeleteApplication response for delete user access token
  55. func DeleteApplication(ctx *context.Context) {
  56. if err := models.DeleteAccessTokenByID(ctx.QueryInt64("id"), ctx.User.ID); err != nil {
  57. ctx.Flash.Error("DeleteAccessTokenByID: " + err.Error())
  58. } else {
  59. ctx.Flash.Success(ctx.Tr("settings.delete_token_success"))
  60. }
  61. ctx.JSON(200, map[string]interface{}{
  62. "redirect": setting.AppSubURL + "/user/settings/applications",
  63. })
  64. }
  65. func loadApplicationsData(ctx *context.Context) {
  66. tokens, err := models.ListAccessTokens(ctx.User.ID, models.ListOptions{})
  67. if err != nil {
  68. ctx.ServerError("ListAccessTokens", err)
  69. return
  70. }
  71. ctx.Data["Tokens"] = tokens
  72. ctx.Data["EnableOAuth2"] = setting.OAuth2.Enable
  73. if setting.OAuth2.Enable {
  74. ctx.Data["Applications"], err = models.GetOAuth2ApplicationsByUserID(ctx.User.ID)
  75. if err != nil {
  76. ctx.ServerError("GetOAuth2ApplicationsByUserID", err)
  77. return
  78. }
  79. ctx.Data["Grants"], err = models.GetOAuth2GrantsByUserID(ctx.User.ID)
  80. if err != nil {
  81. ctx.ServerError("GetOAuth2GrantsByUserID", err)
  82. return
  83. }
  84. }
  85. }