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.

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