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