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.

105 lines
2.6 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>
4 years ago
  1. // Copyright 2019 The Gitea 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 models
  5. import (
  6. "fmt"
  7. "code.gitea.io/gitea/modules/log"
  8. api "code.gitea.io/gitea/modules/structs"
  9. )
  10. //UserList is a list of user.
  11. // This type provide valuable methods to retrieve information for a group of users efficiently.
  12. type UserList []*User
  13. func (users UserList) getUserIDs() []int64 {
  14. userIDs := make([]int64, len(users))
  15. for _, user := range users {
  16. userIDs = append(userIDs, user.ID) //Considering that user id are unique in the list
  17. }
  18. return userIDs
  19. }
  20. // IsUserOrgOwner returns true if user is in the owner team of given organization.
  21. func (users UserList) IsUserOrgOwner(orgID int64) map[int64]bool {
  22. results := make(map[int64]bool, len(users))
  23. for _, user := range users {
  24. results[user.ID] = false //Set default to false
  25. }
  26. ownerMaps, err := users.loadOrganizationOwners(x, orgID)
  27. if err == nil {
  28. for _, owner := range ownerMaps {
  29. results[owner.UID] = true
  30. }
  31. }
  32. return results
  33. }
  34. func (users UserList) loadOrganizationOwners(e Engine, orgID int64) (map[int64]*TeamUser, error) {
  35. if len(users) == 0 {
  36. return nil, nil
  37. }
  38. ownerTeam, err := getOwnerTeam(e, orgID)
  39. if err != nil {
  40. if IsErrTeamNotExist(err) {
  41. log.Error("Organization does not have owner team: %d", orgID)
  42. return nil, nil
  43. }
  44. return nil, err
  45. }
  46. userIDs := users.getUserIDs()
  47. ownerMaps := make(map[int64]*TeamUser)
  48. err = e.In("uid", userIDs).
  49. And("org_id=?", orgID).
  50. And("team_id=?", ownerTeam.ID).
  51. Find(&ownerMaps)
  52. if err != nil {
  53. return nil, fmt.Errorf("find team users: %v", err)
  54. }
  55. return ownerMaps, nil
  56. }
  57. // GetTwoFaStatus return state of 2FA enrollement
  58. func (users UserList) GetTwoFaStatus() map[int64]bool {
  59. results := make(map[int64]bool, len(users))
  60. for _, user := range users {
  61. results[user.ID] = false //Set default to false
  62. }
  63. tokenMaps, err := users.loadTwoFactorStatus(x)
  64. if err == nil {
  65. for _, token := range tokenMaps {
  66. results[token.UID] = true
  67. }
  68. }
  69. return results
  70. }
  71. func (users UserList) loadTwoFactorStatus(e Engine) (map[int64]*TwoFactor, error) {
  72. if len(users) == 0 {
  73. return nil, nil
  74. }
  75. userIDs := users.getUserIDs()
  76. tokenMaps := make(map[int64]*TwoFactor, len(userIDs))
  77. err := e.
  78. In("uid", userIDs).
  79. Find(&tokenMaps)
  80. if err != nil {
  81. return nil, fmt.Errorf("find two factor: %v", err)
  82. }
  83. return tokenMaps, nil
  84. }
  85. //APIFormat return list of users in api format
  86. func (users UserList) APIFormat() []*api.User {
  87. result := make([]*api.User, 0, len(users))
  88. for _, u := range users {
  89. result = append(result, u.APIFormat())
  90. }
  91. return result
  92. }