Skip to content

Commit 9e456b5

Browse files
authored
HotFix: Hide private partisipation in Orgs (#13994)
* HotFix: Hide private partisipation in Orgs * refactor & add node to fuc GetOrganizations
1 parent 069acf6 commit 9e456b5

File tree

3 files changed

+40
-11
lines changed

3 files changed

+40
-11
lines changed

models/user.go

+1
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,7 @@ func (u *User) GetOwnedOrganizations() (err error) {
538538
}
539539

540540
// GetOrganizations returns paginated organizations that user belongs to.
541+
// TODO: does not respect All and show orgs you privately participate
541542
func (u *User) GetOrganizations(opts *SearchOrganizationsOptions) error {
542543
sess := x.NewSession()
543544
defer sess.Close()

routers/api/v1/org/org.go

+20-11
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,28 @@ import (
1717
"code.gitea.io/gitea/routers/api/v1/utils"
1818
)
1919

20-
func listUserOrgs(ctx *context.APIContext, u *models.User, all bool) {
21-
if err := u.GetOrganizations(&models.SearchOrganizationsOptions{
22-
ListOptions: utils.GetListOptions(ctx),
23-
All: all,
24-
}); err != nil {
25-
ctx.Error(http.StatusInternalServerError, "GetOrganizations", err)
20+
func listUserOrgs(ctx *context.APIContext, u *models.User) {
21+
22+
listOptions := utils.GetListOptions(ctx)
23+
showPrivate := ctx.IsSigned && (ctx.User.IsAdmin || ctx.User.ID == u.ID)
24+
25+
orgs, err := models.GetOrgsByUserID(u.ID, showPrivate)
26+
if err != nil {
27+
ctx.Error(http.StatusInternalServerError, "GetOrgsByUserID", err)
2628
return
2729
}
30+
maxResults := len(orgs)
31+
32+
orgs = utils.PaginateUserSlice(orgs, listOptions.Page, listOptions.PageSize)
2833

29-
apiOrgs := make([]*api.Organization, len(u.Orgs))
30-
for i := range u.Orgs {
31-
apiOrgs[i] = convert.ToOrganization(u.Orgs[i])
34+
apiOrgs := make([]*api.Organization, len(orgs))
35+
for i := range orgs {
36+
apiOrgs[i] = convert.ToOrganization(orgs[i])
3237
}
38+
39+
ctx.SetLinkHeader(int(maxResults), listOptions.PageSize)
40+
ctx.Header().Set("X-Total-Count", fmt.Sprintf("%d", maxResults))
41+
ctx.Header().Set("Access-Control-Expose-Headers", "X-Total-Count, Link")
3342
ctx.JSON(http.StatusOK, &apiOrgs)
3443
}
3544

@@ -53,7 +62,7 @@ func ListMyOrgs(ctx *context.APIContext) {
5362
// "200":
5463
// "$ref": "#/responses/OrganizationList"
5564

56-
listUserOrgs(ctx, ctx.User, true)
65+
listUserOrgs(ctx, ctx.User)
5766
}
5867

5968
// ListUserOrgs list user's orgs
@@ -85,7 +94,7 @@ func ListUserOrgs(ctx *context.APIContext) {
8594
if ctx.Written() {
8695
return
8796
}
88-
listUserOrgs(ctx, u, ctx.User != nil && (ctx.User.IsAdmin || ctx.User.ID == u.ID))
97+
listUserOrgs(ctx, u)
8998
}
9099

91100
// GetAll return list of all public organizations

routers/api/v1/utils/utils.go

+19
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,22 @@ func GetListOptions(ctx *context.APIContext) models.ListOptions {
6666
PageSize: convert.ToCorrectPageSize(ctx.QueryInt("limit")),
6767
}
6868
}
69+
70+
// PaginateUserSlice cut a slice of Users as per pagination options
71+
// TODO: make it generic
72+
func PaginateUserSlice(items []*models.User, page, pageSize int) []*models.User {
73+
if page != 0 {
74+
page--
75+
}
76+
77+
if page*pageSize >= len(items) {
78+
return items[len(items):]
79+
}
80+
81+
items = items[page*pageSize:]
82+
83+
if len(items) > pageSize {
84+
return items[:pageSize]
85+
}
86+
return items
87+
}

0 commit comments

Comments
 (0)