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

  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. var result []*api.User
  88. for _, u := range users {
  89. result = append(result, u.APIFormat())
  90. }
  91. return result
  92. }