Skip to content

Commit e8b5972

Browse files
author
Gusted
committed
Fix SQL Query for SearchTeam
- Currently the function takes in the `UserID` option, but isn't being used within the SQL query. This patch fixes that by checking that only teams are being returned that the user belongs to. - Resolves go-gitea#20829
1 parent 03df7d0 commit e8b5972

File tree

5 files changed

+55
-14
lines changed

5 files changed

+55
-14
lines changed

integrations/api_team_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ func TestAPITeamSearch(t *testing.T) {
223223
defer prepareTestEnv(t)()
224224

225225
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
226-
org := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 3})
226+
org := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 17})
227227

228228
var results TeamSearchResults
229229

integrations/api_user_orgs_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,19 @@ func TestUserOrgs(t *testing.T) {
2626
orgs := getUserOrgs(t, adminUsername, normalUsername)
2727

2828
user3 := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "user3"})
29+
user17 := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "user17"})
2930

3031
assert.Equal(t, []*api.Organization{
32+
{
33+
ID: 17,
34+
UserName: user17.Name,
35+
FullName: user17.FullName,
36+
AvatarURL: user17.AvatarLink(),
37+
Description: "",
38+
Website: "",
39+
Location: "",
40+
Visibility: "public",
41+
},
3142
{
3243
ID: 3,
3344
UserName: user3.Name,
@@ -82,8 +93,19 @@ func TestMyOrgs(t *testing.T) {
8293
var orgs []*api.Organization
8394
DecodeJSON(t, resp, &orgs)
8495
user3 := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "user3"})
96+
user17 := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "user172"})
8597

8698
assert.Equal(t, []*api.Organization{
99+
{
100+
ID: 17,
101+
UserName: user17.Name,
102+
FullName: user17.FullName,
103+
AvatarURL: user17.AvatarLink(),
104+
Description: "",
105+
Website: "",
106+
Location: "",
107+
Visibility: "public",
108+
},
87109
{
88110
ID: 3,
89111
UserName: user3.Name,

models/fixtures/org_user.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,9 @@
6363
uid: 29
6464
org_id: 17
6565
is_public: true
66+
67+
-
68+
id: 12
69+
uid: 2
70+
org_id: 17
71+
is_public: true

models/fixtures/user.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@
309309
avatar_email: [email protected]
310310
num_repos: 2
311311
is_active: true
312-
num_members: 3
312+
num_members: 4
313313
num_teams: 3
314314

315315
-

models/organization/team.go

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -96,16 +96,7 @@ type SearchTeamOptions struct {
9696
IncludeDesc bool
9797
}
9898

99-
// SearchTeam search for teams. Caller is responsible to check permissions.
100-
func SearchTeam(opts *SearchTeamOptions) ([]*Team, int64, error) {
101-
if opts.Page <= 0 {
102-
opts.Page = 1
103-
}
104-
if opts.PageSize == 0 {
105-
// Default limit
106-
opts.PageSize = 10
107-
}
108-
99+
func (opts *SearchTeamOptions) toCond() builder.Cond {
109100
cond := builder.NewCond()
110101

111102
if len(opts.Keyword) > 0 {
@@ -117,18 +108,39 @@ func SearchTeam(opts *SearchTeamOptions) ([]*Team, int64, error) {
117108
cond = cond.And(keywordCond)
118109
}
119110

120-
cond = cond.And(builder.Eq{"org_id": opts.OrgID})
111+
if opts.OrgID > 0 {
112+
cond = cond.And(builder.Eq{"`team`.org_id": opts.OrgID})
113+
}
114+
115+
if opts.UserID > 0 {
116+
cond = cond.And(builder.Eq{"team_user.uid": opts.UserID})
117+
}
118+
119+
return cond
120+
}
121121

122+
// SearchTeam search for teams. Caller is responsible to check permissions.
123+
func SearchTeam(opts *SearchTeamOptions) ([]*Team, int64, error) {
122124
sess := db.GetEngine(db.DefaultContext)
123125

126+
opts.SetDefaultValues()
127+
cond := opts.toCond()
128+
129+
if opts.UserID > 0 {
130+
sess = sess.Join("INNER", "team_user", "team_user.team_id = team.id")
131+
}
132+
124133
count, err := sess.
125134
Where(cond).
126135
Count(new(Team))
127136
if err != nil {
128137
return nil, 0, err
129138
}
130139

131-
sess = sess.Where(cond)
140+
if opts.UserID > 0 {
141+
sess = sess.Join("INNER", "team_user", "team_user.team_id = team.id")
142+
}
143+
132144
if opts.PageSize == -1 {
133145
opts.PageSize = int(count)
134146
} else {
@@ -137,6 +149,7 @@ func SearchTeam(opts *SearchTeamOptions) ([]*Team, int64, error) {
137149

138150
teams := make([]*Team, 0, opts.PageSize)
139151
if err = sess.
152+
Where(cond).
140153
OrderBy("lower_name").
141154
Find(&teams); err != nil {
142155
return nil, 0, err

0 commit comments

Comments
 (0)