Skip to content

Commit c5e88fb

Browse files
authored
[API] teamSearch show teams with no members if user is admin (#21204)
close #21176
1 parent c87e6a8 commit c5e88fb

File tree

6 files changed

+48
-24
lines changed

6 files changed

+48
-24
lines changed

build/generate-go-licenses.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ func main() {
6464
}
6565

6666
entries = append(entries, LicenseEntry{
67-
Name: name,
68-
Path: path,
67+
Name: name,
68+
Path: path,
6969
LicenseText: string(licenseText),
7070
})
7171
}

models/organization/team.go

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -129,29 +129,11 @@ func SearchTeam(opts *SearchTeamOptions) ([]*Team, int64, error) {
129129
if opts.UserID > 0 {
130130
sess = sess.Join("INNER", "team_user", "team_user.team_id = team.id")
131131
}
132-
133-
count, err := sess.
134-
Where(cond).
135-
Count(new(Team))
136-
if err != nil {
137-
return nil, 0, err
138-
}
139-
140-
if opts.UserID > 0 {
141-
sess = sess.Join("INNER", "team_user", "team_user.team_id = team.id")
142-
}
143-
144-
if opts.PageSize == -1 {
145-
opts.PageSize = int(count)
146-
} else {
147-
sess = sess.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize)
148-
}
132+
sess = db.SetSessionPagination(sess, opts)
149133

150134
teams := make([]*Team, 0, opts.PageSize)
151-
if err = sess.
152-
Where(cond).
153-
OrderBy("lower_name").
154-
Find(&teams); err != nil {
135+
count, err := sess.Where(cond).OrderBy("lower_name").FindAndCount(&teams)
136+
if err != nil {
155137
return nil, 0, err
156138
}
157139

modules/markup/markdown/meta.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"unicode/utf8"
1212

1313
"code.gitea.io/gitea/modules/log"
14+
1415
"gopkg.in/yaml.v3"
1516
)
1617

modules/markup/markdown/renderconfig.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"strings"
99

1010
"code.gitea.io/gitea/modules/log"
11+
1112
"github.com/yuin/goldmark/ast"
1213
"gopkg.in/yaml.v3"
1314
)

routers/api/v1/org/team.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -759,13 +759,17 @@ func SearchTeam(ctx *context.APIContext) {
759759
listOptions := utils.GetListOptions(ctx)
760760

761761
opts := &organization.SearchTeamOptions{
762-
UserID: ctx.Doer.ID,
763762
Keyword: ctx.FormTrim("q"),
764763
OrgID: ctx.Org.Organization.ID,
765764
IncludeDesc: ctx.FormString("include_desc") == "" || ctx.FormBool("include_desc"),
766765
ListOptions: listOptions,
767766
}
768767

768+
// Only admin is allowd to search for all teams
769+
if !ctx.Doer.IsAdmin {
770+
opts.UserID = ctx.Doer.ID
771+
}
772+
769773
teams, maxResults, err := organization.SearchTeam(opts)
770774
if err != nil {
771775
log.Error("SearchTeam failed: %v", err)

tests/integration/api_org_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package integration
66

77
import (
8+
"fmt"
89
"net/http"
910
"net/url"
1011
"strings"
@@ -151,3 +152,38 @@ func TestAPIGetAll(t *testing.T) {
151152
assert.Equal(t, "org25", apiOrgList[0].FullName)
152153
assert.Equal(t, "public", apiOrgList[0].Visibility)
153154
}
155+
156+
func TestAPIOrgSearchEmptyTeam(t *testing.T) {
157+
onGiteaRun(t, func(*testing.T, *url.URL) {
158+
token := getUserToken(t, "user1")
159+
orgName := "org_with_empty_team"
160+
161+
// create org
162+
req := NewRequestWithJSON(t, "POST", "/api/v1/orgs?token="+token, &api.CreateOrgOption{
163+
UserName: orgName,
164+
})
165+
MakeRequest(t, req, http.StatusCreated)
166+
167+
// create team with no member
168+
req = NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/orgs/%s/teams?token=%s", orgName, token), &api.CreateTeamOption{
169+
Name: "Empty",
170+
IncludesAllRepositories: true,
171+
Permission: "read",
172+
Units: []string{"repo.code", "repo.issues", "repo.ext_issues", "repo.wiki", "repo.pulls"},
173+
})
174+
MakeRequest(t, req, http.StatusCreated)
175+
176+
// case-insensitive search for teams that have no members
177+
req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/orgs/%s/teams/search?q=%s&token=%s", orgName, "empty", token))
178+
resp := MakeRequest(t, req, http.StatusOK)
179+
data := struct {
180+
Ok bool
181+
Data []*api.Team
182+
}{}
183+
DecodeJSON(t, resp, &data)
184+
assert.True(t, data.Ok)
185+
if assert.Len(t, data.Data, 1) {
186+
assert.EqualValues(t, "Empty", data.Data[0].Name)
187+
}
188+
})
189+
}

0 commit comments

Comments
 (0)