A fork of Gitea (see branch `mj`) adding Majority Judgment Polls 𐄷 over Issues and Merge Requests. https://git.mieuxvoter.fr
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

136 lines
3.8 KiB

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>
3 years ago
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>
3 years ago
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>
3 years ago
7 years ago
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Copyright 2019 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 models
  6. import (
  7. "crypto/subtle"
  8. "time"
  9. "code.gitea.io/gitea/modules/base"
  10. "code.gitea.io/gitea/modules/generate"
  11. "code.gitea.io/gitea/modules/timeutil"
  12. gouuid "github.com/google/uuid"
  13. )
  14. // AccessToken represents a personal access token.
  15. type AccessToken struct {
  16. ID int64 `xorm:"pk autoincr"`
  17. UID int64 `xorm:"INDEX"`
  18. Name string
  19. Token string `xorm:"-"`
  20. TokenHash string `xorm:"UNIQUE"` // sha256 of token
  21. TokenSalt string
  22. TokenLastEight string `xorm:"token_last_eight"`
  23. CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
  24. UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
  25. HasRecentActivity bool `xorm:"-"`
  26. HasUsed bool `xorm:"-"`
  27. }
  28. // AfterLoad is invoked from XORM after setting the values of all fields of this object.
  29. func (t *AccessToken) AfterLoad() {
  30. t.HasUsed = t.UpdatedUnix > t.CreatedUnix
  31. t.HasRecentActivity = t.UpdatedUnix.AddDuration(7*24*time.Hour) > timeutil.TimeStampNow()
  32. }
  33. // NewAccessToken creates new access token.
  34. func NewAccessToken(t *AccessToken) error {
  35. salt, err := generate.GetRandomString(10)
  36. if err != nil {
  37. return err
  38. }
  39. t.TokenSalt = salt
  40. t.Token = base.EncodeSha1(gouuid.New().String())
  41. t.TokenHash = hashToken(t.Token, t.TokenSalt)
  42. t.TokenLastEight = t.Token[len(t.Token)-8:]
  43. _, err = x.Insert(t)
  44. return err
  45. }
  46. // GetAccessTokenBySHA returns access token by given token value
  47. func GetAccessTokenBySHA(token string) (*AccessToken, error) {
  48. if token == "" {
  49. return nil, ErrAccessTokenEmpty{}
  50. }
  51. // A token is defined as being SHA1 sum these are 40 hexadecimal bytes long
  52. if len(token) != 40 {
  53. return nil, ErrAccessTokenNotExist{token}
  54. }
  55. for _, x := range []byte(token) {
  56. if x < '0' || (x > '9' && x < 'a') || x > 'f' {
  57. return nil, ErrAccessTokenNotExist{token}
  58. }
  59. }
  60. var tokens []AccessToken
  61. lastEight := token[len(token)-8:]
  62. err := x.Table(&AccessToken{}).Where("token_last_eight = ?", lastEight).Find(&tokens)
  63. if err != nil {
  64. return nil, err
  65. } else if len(tokens) == 0 {
  66. return nil, ErrAccessTokenNotExist{token}
  67. }
  68. for _, t := range tokens {
  69. tempHash := hashToken(token, t.TokenSalt)
  70. if subtle.ConstantTimeCompare([]byte(t.TokenHash), []byte(tempHash)) == 1 {
  71. return &t, nil
  72. }
  73. }
  74. return nil, ErrAccessTokenNotExist{token}
  75. }
  76. // AccessTokenByNameExists checks if a token name has been used already by a user.
  77. func AccessTokenByNameExists(token *AccessToken) (bool, error) {
  78. return x.Table("access_token").Where("name = ?", token.Name).And("uid = ?", token.UID).Exist()
  79. }
  80. // ListAccessTokensOptions contain filter options
  81. type ListAccessTokensOptions struct {
  82. ListOptions
  83. Name string
  84. UserID int64
  85. }
  86. // ListAccessTokens returns a list of access tokens belongs to given user.
  87. func ListAccessTokens(opts ListAccessTokensOptions) ([]*AccessToken, error) {
  88. sess := x.Where("uid=?", opts.UserID)
  89. if len(opts.Name) != 0 {
  90. sess = sess.Where("name=?", opts.Name)
  91. }
  92. sess = sess.Desc("id")
  93. if opts.Page != 0 {
  94. sess = opts.setSessionPagination(sess)
  95. tokens := make([]*AccessToken, 0, opts.PageSize)
  96. return tokens, sess.Find(&tokens)
  97. }
  98. tokens := make([]*AccessToken, 0, 5)
  99. return tokens, sess.Find(&tokens)
  100. }
  101. // UpdateAccessToken updates information of access token.
  102. func UpdateAccessToken(t *AccessToken) error {
  103. _, err := x.ID(t.ID).AllCols().Update(t)
  104. return err
  105. }
  106. // DeleteAccessTokenByID deletes access token by given ID.
  107. func DeleteAccessTokenByID(id, userID int64) error {
  108. cnt, err := x.ID(id).Delete(&AccessToken{
  109. UID: userID,
  110. })
  111. if err != nil {
  112. return err
  113. } else if cnt != 1 {
  114. return ErrAccessTokenNotExist{}
  115. }
  116. return nil
  117. }