Skip to content

Commit 1b53a9e

Browse files
Gustedlunny
andauthored
Don't return duplicated users who can create org repo (#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 keyed by user id in order to drop duplicates. --------- Co-authored-by: Lunny Xiao <[email protected]>
1 parent be315c7 commit 1b53a9e

File tree

6 files changed

+28
-9
lines changed

6 files changed

+28
-9
lines changed

Diff for: models/activities/notification.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ func CreateRepoTransferNotification(ctx context.Context, doer, newOwner *user_mo
151151
}
152152
for i := range users {
153153
notify = append(notify, &Notification{
154-
UserID: users[i].ID,
154+
UserID: i,
155155
RepoID: repo.ID,
156156
Status: NotificationStatusUnread,
157157
UpdatedBy: doer.ID,

Diff for: 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

Diff for: 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

Diff for: 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

Diff for: models/organization/org.go

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

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

409410
// SearchOrganizationsOptions options to filter organizations

Diff for: models/organization/org_test.go

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

@@ -292,7 +293,7 @@ func TestUser_GetUserTeamIDs(t *testing.T) {
292293
assert.NoError(t, err)
293294
assert.Equal(t, expected, teamIDs)
294295
}
295-
testSuccess(2, []int64{1, 2})
296+
testSuccess(2, []int64{1, 2, 14})
296297
testSuccess(4, []int64{2})
297298
testSuccess(unittest.NonexistentID, []int64{})
298299
}
@@ -447,7 +448,7 @@ func TestGetUsersWhoCanCreateOrgRepo(t *testing.T) {
447448
users, err = organization.GetUsersWhoCanCreateOrgRepo(db.DefaultContext, 7)
448449
assert.NoError(t, err)
449450
assert.Len(t, users, 1)
450-
assert.EqualValues(t, 5, users[0].ID)
451+
assert.NotNil(t, users[5])
451452
}
452453

453454
func TestUser_RemoveOrgRepo(t *testing.T) {

0 commit comments

Comments
 (0)