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.

org_user.go 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Copyright 2022 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package organization
  4. import (
  5. "context"
  6. "fmt"
  7. "code.gitea.io/gitea/models/db"
  8. "code.gitea.io/gitea/models/perm"
  9. user_model "code.gitea.io/gitea/models/user"
  10. "code.gitea.io/gitea/modules/log"
  11. "xorm.io/builder"
  12. )
  13. // ________ ____ ___
  14. // \_____ \_______ ____ | | \______ ___________
  15. // / | \_ __ \/ ___\| | / ___// __ \_ __ \
  16. // / | \ | \/ /_/ > | /\___ \\ ___/| | \/
  17. // \_______ /__| \___ /|______//____ >\___ >__|
  18. // \/ /_____/ \/ \/
  19. // OrgUser represents an organization-user relation.
  20. type OrgUser struct {
  21. ID int64 `xorm:"pk autoincr"`
  22. UID int64 `xorm:"INDEX UNIQUE(s)"`
  23. OrgID int64 `xorm:"INDEX UNIQUE(s)"`
  24. IsPublic bool `xorm:"INDEX"`
  25. }
  26. func init() {
  27. db.RegisterModel(new(OrgUser))
  28. }
  29. // GetOrganizationCount returns count of membership of organization of the user.
  30. func GetOrganizationCount(ctx context.Context, u *user_model.User) (int64, error) {
  31. return db.GetEngine(ctx).
  32. Where("uid=?", u.ID).
  33. Count(new(OrgUser))
  34. }
  35. // IsOrganizationOwner returns true if given user is in the owner team.
  36. func IsOrganizationOwner(ctx context.Context, orgID, uid int64) (bool, error) {
  37. ownerTeam, err := GetOwnerTeam(ctx, orgID)
  38. if err != nil {
  39. if IsErrTeamNotExist(err) {
  40. log.Error("Organization does not have owner team: %d", orgID)
  41. return false, nil
  42. }
  43. return false, err
  44. }
  45. return IsTeamMember(ctx, orgID, ownerTeam.ID, uid)
  46. }
  47. // IsOrganizationAdmin returns true if given user is in the owner team or an admin team.
  48. func IsOrganizationAdmin(ctx context.Context, orgID, uid int64) (bool, error) {
  49. teams, err := GetUserOrgTeams(ctx, orgID, uid)
  50. if err != nil {
  51. return false, err
  52. }
  53. for _, t := range teams {
  54. if t.AccessMode >= perm.AccessModeAdmin {
  55. return true, nil
  56. }
  57. }
  58. return false, nil
  59. }
  60. // IsOrganizationMember returns true if given user is member of organization.
  61. func IsOrganizationMember(ctx context.Context, orgID, uid int64) (bool, error) {
  62. return db.GetEngine(ctx).
  63. Where("uid=?", uid).
  64. And("org_id=?", orgID).
  65. Table("org_user").
  66. Exist()
  67. }
  68. // IsPublicMembership returns true if the given user's membership of given org is public.
  69. func IsPublicMembership(ctx context.Context, orgID, uid int64) (bool, error) {
  70. return db.GetEngine(ctx).
  71. Where("uid=?", uid).
  72. And("org_id=?", orgID).
  73. And("is_public=?", true).
  74. Table("org_user").
  75. Exist()
  76. }
  77. // CanCreateOrgRepo returns true if user can create repo in organization
  78. func CanCreateOrgRepo(ctx context.Context, orgID, uid int64) (bool, error) {
  79. return db.GetEngine(ctx).
  80. Where(builder.Eq{"team.can_create_org_repo": true}).
  81. Join("INNER", "team_user", "team_user.team_id = team.id").
  82. And("team_user.uid = ?", uid).
  83. And("team_user.org_id = ?", orgID).
  84. Exist(new(Team))
  85. }
  86. // IsUserOrgOwner returns true if user is in the owner team of given organization.
  87. func IsUserOrgOwner(ctx context.Context, users user_model.UserList, orgID int64) map[int64]bool {
  88. results := make(map[int64]bool, len(users))
  89. for _, user := range users {
  90. results[user.ID] = false // Set default to false
  91. }
  92. ownerMaps, err := loadOrganizationOwners(ctx, users, orgID)
  93. if err == nil {
  94. for _, owner := range ownerMaps {
  95. results[owner.UID] = true
  96. }
  97. }
  98. return results
  99. }
  100. func loadOrganizationOwners(ctx context.Context, users user_model.UserList, orgID int64) (map[int64]*TeamUser, error) {
  101. if len(users) == 0 {
  102. return nil, nil
  103. }
  104. ownerTeam, err := GetOwnerTeam(ctx, orgID)
  105. if err != nil {
  106. if IsErrTeamNotExist(err) {
  107. log.Error("Organization does not have owner team: %d", orgID)
  108. return nil, nil
  109. }
  110. return nil, err
  111. }
  112. userIDs := users.GetUserIDs()
  113. ownerMaps := make(map[int64]*TeamUser)
  114. err = db.GetEngine(ctx).In("uid", userIDs).
  115. And("org_id=?", orgID).
  116. And("team_id=?", ownerTeam.ID).
  117. Find(&ownerMaps)
  118. if err != nil {
  119. return nil, fmt.Errorf("find team users: %w", err)
  120. }
  121. return ownerMaps, nil
  122. }