diff --git a/models/organization/org_list.go b/models/organization/org_list.go index 72ebf6f178..fccbfe39d0 100644 --- a/models/organization/org_list.go +++ b/models/organization/org_list.go @@ -57,11 +57,11 @@ func (opts FindOrgOptions) ToOrders() string { func GetOrgsCanCreateRepoByUserID(ctx context.Context, userID int64) ([]*Organization, error) { orgs := make([]*Organization, 0, 10) - return orgs, db.GetEngine(ctx).Where(builder.In("id", builder.Select("`user`.id").From("`user`"). + return orgs, db.GetEngine(ctx).Select("DISTINCT `user`.id, `user`.*").Table("`user`"). Join("INNER", "`team_user`", "`team_user`.org_id = `user`.id"). Join("INNER", "`team`", "`team`.id = `team_user`.team_id"). Where(builder.Eq{"`team_user`.uid": userID}). - And(builder.Eq{"`team`.authorize": perm.AccessModeOwner}.Or(builder.Eq{"`team`.can_create_org_repo": true})))). + And(builder.Eq{"`team`.authorize": perm.AccessModeOwner}.Or(builder.Eq{"`team`.can_create_org_repo": true})). Asc("`user`.name"). Find(&orgs) } diff --git a/models/organization/org_list_test.go b/models/organization/org_list_test.go index 7d02c6c73f..2b4d493cf2 100644 --- a/models/organization/org_list_test.go +++ b/models/organization/org_list_test.go @@ -51,6 +51,19 @@ func TestFindOrgs(t *testing.T) { assert.EqualValues(t, 1, total) } +func TestGetOrgsCanCreateRepoByUserID(t *testing.T) { + require.NoError(t, unittest.PrepareTestDatabase()) + orgs, err := organization.GetOrgsCanCreateRepoByUserID(db.DefaultContext, 2) + require.NoError(t, err) + assert.Len(t, orgs, 1) + assert.EqualValues(t, 3, orgs[0].ID) + orgs, err = organization.GetOrgsCanCreateRepoByUserID(db.DefaultContext, 1) + require.NoError(t, err) + assert.Len(t, orgs, 2) + assert.EqualValues(t, 36, orgs[0].ID) + assert.EqualValues(t, 35, orgs[1].ID) +} + func TestGetUserOrgsList(t *testing.T) { require.NoError(t, unittest.PrepareTestDatabase()) orgs, err := organization.GetUserOrgsList(db.DefaultContext, &user_model.User{ID: 4})