Skip to content

Commit fd2c250

Browse files
authored
Don't return duplicated users who can create org repo (go-gitea#22560) (go-gitea#22562)
- Backport of go-gitea#22560 - Currently the function `GetUsersWhoCanCreateOrgRepo` uses a query that is able to have duplicated users in the result, this is can happen under the condition that a user is in team that either is the owner team or has permission to create organization repositories. - Add test code to simulate the above condition for user 3, [`TestGetUsersWhoCanCreateOrgRepo`](https://github.com/go-gitea/gitea/blob/a1fcb1cfb84fd6b36c8fe9fd56588119fa4377bc/models/organization/org_test.go#L435) is the test function that tests for this. - The fix is quite trivial, use a map as a set to get distinct orgs.
1 parent e6d6bce commit fd2c250

File tree

6 files changed

+28
-9
lines changed

6 files changed

+28
-9
lines changed

models/activities/notification.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ func CreateRepoTransferNotification(doer, newOwner *user_model.User, repo *repo_
157157
}
158158
for i := range users {
159159
notify = append(notify, &Notification{
160-
UserID: users[i].ID,
160+
UserID: i,
161161
RepoID: repo.ID,
162162
Status: NotificationStatusUnread,
163163
UpdatedBy: doer.ID,

models/fixtures/team.yml

+11
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,14 @@
140140
num_members: 1
141141
includes_all_repositories: false
142142
can_create_org_repo: false
143+
144+
-
145+
id: 14
146+
org_id: 3
147+
lower_name: teamcreaterepo
148+
name: teamCreateRepo
149+
authorize: 2 # write
150+
num_repos: 0
151+
num_members: 1
152+
includes_all_repositories: false
153+
can_create_org_repo: true

models/fixtures/team_user.yml

+6
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,9 @@
9393
org_id: 19
9494
team_id: 6
9595
uid: 31
96+
97+
-
98+
id: 17
99+
org_id: 3
100+
team_id: 14
101+
uid: 2

models/fixtures/user.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@
104104
num_following: 0
105105
num_stars: 0
106106
num_repos: 3
107-
num_teams: 4
107+
num_teams: 5
108108
num_members: 3
109109
visibility: 0
110110
repo_admin_change_team_access: false

models/organization/org.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -396,13 +396,14 @@ func (org *Organization) GetOrgUserMaxAuthorizeLevel(uid int64) (perm.AccessMode
396396
}
397397

398398
// GetUsersWhoCanCreateOrgRepo returns users which are able to create repo in organization
399-
func GetUsersWhoCanCreateOrgRepo(ctx context.Context, orgID int64) ([]*user_model.User, error) {
400-
users := make([]*user_model.User, 0, 10)
399+
func GetUsersWhoCanCreateOrgRepo(ctx context.Context, orgID int64) (map[int64]*user_model.User, error) {
400+
// Use a map, in order to de-duplicate users.
401+
users := make(map[int64]*user_model.User)
401402
return users, db.GetEngine(ctx).
402403
Join("INNER", "`team_user`", "`team_user`.uid=`user`.id").
403404
Join("INNER", "`team`", "`team`.id=`team_user`.team_id").
404405
Where(builder.Eq{"team.can_create_org_repo": true}.Or(builder.Eq{"team.authorize": perm.AccessModeOwner})).
405-
And("team_user.org_id = ?", orgID).Asc("`user`.name").Find(&users)
406+
And("team_user.org_id = ?", orgID).Find(&users)
406407
}
407408

408409
// SearchOrganizationsOptions options to filter organizations

models/organization/org_test.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,12 @@ func TestUser_GetTeams(t *testing.T) {
9292
org := unittest.AssertExistsAndLoadBean(t, &organization.Organization{ID: 3})
9393
teams, err := org.LoadTeams()
9494
assert.NoError(t, err)
95-
if assert.Len(t, teams, 4) {
95+
if assert.Len(t, teams, 5) {
9696
assert.Equal(t, int64(1), teams[0].ID)
9797
assert.Equal(t, int64(2), teams[1].ID)
9898
assert.Equal(t, int64(12), teams[2].ID)
99-
assert.Equal(t, int64(7), teams[3].ID)
99+
assert.Equal(t, int64(14), teams[3].ID)
100+
assert.Equal(t, int64(7), teams[4].ID)
100101
}
101102
}
102103

@@ -293,7 +294,7 @@ func TestUser_GetUserTeamIDs(t *testing.T) {
293294
assert.NoError(t, err)
294295
assert.Equal(t, expected, teamIDs)
295296
}
296-
testSuccess(2, []int64{1, 2})
297+
testSuccess(2, []int64{1, 2, 14})
297298
testSuccess(4, []int64{2})
298299
testSuccess(unittest.NonexistentID, []int64{})
299300
}
@@ -448,7 +449,7 @@ func TestGetUsersWhoCanCreateOrgRepo(t *testing.T) {
448449
users, err = organization.GetUsersWhoCanCreateOrgRepo(db.DefaultContext, 7)
449450
assert.NoError(t, err)
450451
assert.Len(t, users, 1)
451-
assert.EqualValues(t, 5, users[0].ID)
452+
assert.NotNil(t, users[5])
452453
}
453454

454455
func TestUser_RemoveOrgRepo(t *testing.T) {

0 commit comments

Comments
 (0)