Skip to content

Commit 93cec4c

Browse files
committed
Only show Followers that current user can access
Users who are following or being followed by a user should only be displayed if the viewing user can see them. Signed-off-by: Andrew Thornton <[email protected]>
1 parent 9d9bf66 commit 93cec4c

File tree

3 files changed

+62
-17
lines changed

3 files changed

+62
-17
lines changed

models/user/user.go

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -316,37 +316,45 @@ func (u *User) GenerateEmailActivateCode(email string) string {
316316
}
317317

318318
// GetUserFollowers returns range of user's followers.
319-
func GetUserFollowers(u *User, listOptions db.ListOptions) ([]*User, error) {
320-
sess := db.GetEngine(db.DefaultContext).
319+
func GetUserFollowers(ctx context.Context, u, viewer *User, listOptions db.ListOptions) ([]*User, int64, error) {
320+
sess := db.GetEngine(ctx).
321+
Select("`user`.*").
322+
Join("LEFT", "follow", "`user`.id=follow.user_id").
321323
Where("follow.follow_id=?", u.ID).
322-
Join("LEFT", "follow", "`user`.id=follow.user_id")
324+
And(isUserVisibleToViewerCond(viewer))
323325

324326
if listOptions.Page != 0 {
325327
sess = db.SetSessionPagination(sess, &listOptions)
326328

327329
users := make([]*User, 0, listOptions.PageSize)
328-
return users, sess.Find(&users)
330+
count, err := sess.FindAndCount(&users)
331+
return users, count, err
329332
}
330333

331334
users := make([]*User, 0, 8)
332-
return users, sess.Find(&users)
335+
count, err := sess.FindAndCount(&users)
336+
return users, count, err
333337
}
334338

335339
// GetUserFollowing returns range of user's following.
336-
func GetUserFollowing(u *User, listOptions db.ListOptions) ([]*User, error) {
340+
func GetUserFollowing(ctx context.Context, u, viewer *User, listOptions db.ListOptions) ([]*User, int64, error) {
337341
sess := db.GetEngine(db.DefaultContext).
342+
Select("`user`.*").
343+
Join("LEFT", "follow", "`user`.id=follow.follow_id").
338344
Where("follow.user_id=?", u.ID).
339-
Join("LEFT", "follow", "`user`.id=follow.follow_id")
345+
And(isUserVisibleToViewerCond(viewer))
340346

341347
if listOptions.Page != 0 {
342348
sess = db.SetSessionPagination(sess, &listOptions)
343349

344350
users := make([]*User, 0, listOptions.PageSize)
345-
return users, sess.Find(&users)
351+
count, err := sess.FindAndCount(&users)
352+
return users, count, err
346353
}
347354

348355
users := make([]*User, 0, 8)
349-
return users, sess.Find(&users)
356+
count, err := sess.FindAndCount(&users)
357+
return users, count, err
350358
}
351359

352360
// NewGitSig generates and returns the signature of given user.
@@ -1219,6 +1227,43 @@ func GetAdminUser() (*User, error) {
12191227
return &admin, nil
12201228
}
12211229

1230+
func isUserVisibleToViewerCond(viewer *User) builder.Cond {
1231+
cond := builder.NewCond()
1232+
if viewer != nil && viewer.IsAdmin {
1233+
return cond
1234+
}
1235+
cond = builder.Eq{
1236+
"`user`.Visibility": structs.VisibleTypePublic,
1237+
}
1238+
1239+
if viewer == nil || viewer.IsRestricted {
1240+
return cond
1241+
}
1242+
1243+
cond = builder.Not{builder.Eq{
1244+
"`user`.Visibility": structs.VisibleTypePrivate,
1245+
}}.Or(
1246+
builder.In("`user`.id",
1247+
builder.
1248+
Select("`follow`.user_id").
1249+
From("follow").
1250+
Where(builder.Eq{"`follow`.follow_id": viewer.ID})),
1251+
builder.In("`user`.id",
1252+
builder.
1253+
Select("`team_user`.uid").
1254+
From("team_user").
1255+
Join("INNER", "`team_user` AS t2", "`team_user`.id = `t2`.id").
1256+
Where(builder.Eq{"`t2`.uid": viewer.ID})),
1257+
builder.In("`user`.id",
1258+
builder.
1259+
Select("`team_user`.uid").
1260+
From("team_user").
1261+
Join("INNER", "`team_user` AS t2", "`team_user`.org_id = `t2`.org_id").
1262+
Where(builder.Eq{"`t2`.uid": viewer.ID})))
1263+
1264+
return cond
1265+
}
1266+
12221267
// IsUserVisibleToViewer check if viewer is able to see user profile
12231268
func IsUserVisibleToViewer(ctx context.Context, u, viewer *User) bool {
12241269
if viewer != nil && viewer.IsAdmin {

routers/api/v1/user/follower.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ func responseAPIUsers(ctx *context.APIContext, users []*user_model.User) {
2424
}
2525

2626
func listUserFollowers(ctx *context.APIContext, u *user_model.User) {
27-
users, err := user_model.GetUserFollowers(u, utils.GetListOptions(ctx))
27+
users, count, err := user_model.GetUserFollowers(ctx, u, ctx.Doer, utils.GetListOptions(ctx))
2828
if err != nil {
2929
ctx.Error(http.StatusInternalServerError, "GetUserFollowers", err)
3030
return
3131
}
3232

33-
ctx.SetTotalCountHeader(int64(u.NumFollowers))
33+
ctx.SetTotalCountHeader(count)
3434
responseAPIUsers(ctx, users)
3535
}
3636

@@ -86,13 +86,13 @@ func ListFollowers(ctx *context.APIContext) {
8686
}
8787

8888
func listUserFollowing(ctx *context.APIContext, u *user_model.User) {
89-
users, err := user_model.GetUserFollowing(u, utils.GetListOptions(ctx))
89+
users, count, err := user_model.GetUserFollowing(ctx, u, ctx.Doer, utils.GetListOptions(ctx))
9090
if err != nil {
9191
ctx.Error(http.StatusInternalServerError, "GetUserFollowing", err)
9292
return
9393
}
9494

95-
ctx.SetTotalCountHeader(int64(u.NumFollowing))
95+
ctx.SetTotalCountHeader(count)
9696
responseAPIUsers(ctx, users)
9797
}
9898

routers/web/user/profile.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ func Profile(ctx *context.Context) {
157157

158158
switch tab {
159159
case "followers":
160-
items, err := user_model.GetUserFollowers(ctx.ContextUser, db.ListOptions{
160+
items, count, err := user_model.GetUserFollowers(ctx, ctx.ContextUser, ctx.Doer, db.ListOptions{
161161
PageSize: setting.UI.User.RepoPagingNum,
162162
Page: page,
163163
})
@@ -167,9 +167,9 @@ func Profile(ctx *context.Context) {
167167
}
168168
ctx.Data["Cards"] = items
169169

170-
total = ctx.ContextUser.NumFollowers
170+
total = int(count)
171171
case "following":
172-
items, err := user_model.GetUserFollowing(ctx.ContextUser, db.ListOptions{
172+
items, count, err := user_model.GetUserFollowing(ctx, ctx.ContextUser, ctx.Doer, db.ListOptions{
173173
PageSize: setting.UI.User.RepoPagingNum,
174174
Page: page,
175175
})
@@ -179,7 +179,7 @@ func Profile(ctx *context.Context) {
179179
}
180180
ctx.Data["Cards"] = items
181181

182-
total = ctx.ContextUser.NumFollowing
182+
total = int(count)
183183
case "activity":
184184
ctx.Data["Feeds"], err = models.GetFeeds(ctx, models.GetFeedsOptions{
185185
RequestedUser: ctx.ContextUser,

0 commit comments

Comments
 (0)