Skip to content

Commit f6bec85

Browse files
authored
rework heatmap permissions (#14080)
* now uses the same permission model as for the activity feed: only include activities in repos, that the doer has access to. this might be somewhat slower. * also improves handling of user.KeepActivityPrivate (still shows the heatmap to self & admins) * extend tests * adjust integration test to new behaviour * add access to actions for admins * extend heatmap unit tests
1 parent 2c9dd71 commit f6bec85

File tree

8 files changed

+104
-60
lines changed

8 files changed

+104
-60
lines changed

integrations/privateactivity_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ func TestPrivateActivityYesHeatmapHasNoContentForUserItself(t *testing.T) {
388388
session := loginUser(t, privateActivityTestUser)
389389
hasContent := testPrivateActivityHelperHasHeatmapContentFromSession(t, session)
390390

391-
assert.False(t, hasContent, "user should have no heatmap content")
391+
assert.True(t, hasContent, "user should see their own heatmap content")
392392
}
393393

394394
func TestPrivateActivityYesHeatmapHasNoContentForOtherUser(t *testing.T) {
@@ -399,7 +399,7 @@ func TestPrivateActivityYesHeatmapHasNoContentForOtherUser(t *testing.T) {
399399
session := loginUser(t, privateActivityTestOtherUser)
400400
hasContent := testPrivateActivityHelperHasHeatmapContentFromSession(t, session)
401401

402-
assert.False(t, hasContent, "user should have no heatmap content")
402+
assert.False(t, hasContent, "other user should not see heatmap content")
403403
}
404404

405405
func TestPrivateActivityYesHeatmapHasNoContentForAdmin(t *testing.T) {
@@ -410,5 +410,5 @@ func TestPrivateActivityYesHeatmapHasNoContentForAdmin(t *testing.T) {
410410
session := loginUser(t, privateActivityTestAdmin)
411411
hasContent := testPrivateActivityHelperHasHeatmapContentFromSession(t, session)
412412

413-
assert.False(t, hasContent, "user should have no heatmap content")
413+
assert.True(t, hasContent, "heatmap should show content for admin")
414414
}

models/action.go

Lines changed: 49 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -298,32 +298,63 @@ type GetFeedsOptions struct {
298298

299299
// GetFeeds returns actions according to the provided options
300300
func GetFeeds(opts GetFeedsOptions) ([]*Action, error) {
301-
cond := builder.NewCond()
301+
if !activityReadable(opts.RequestedUser, opts.Actor) {
302+
return make([]*Action, 0), nil
303+
}
302304

303-
var repoIDs []int64
304-
var actorID int64
305+
cond, err := activityQueryCondition(opts)
306+
if err != nil {
307+
return nil, err
308+
}
305309

306-
if opts.Actor != nil {
307-
actorID = opts.Actor.ID
310+
actions := make([]*Action, 0, setting.UI.FeedPagingNum)
311+
312+
if err := x.Limit(setting.UI.FeedPagingNum).Desc("id").Where(cond).Find(&actions); err != nil {
313+
return nil, fmt.Errorf("Find: %v", err)
308314
}
309315

310-
if opts.RequestedUser.IsOrganization() {
311-
env, err := opts.RequestedUser.AccessibleReposEnv(actorID)
312-
if err != nil {
313-
return nil, fmt.Errorf("AccessibleReposEnv: %v", err)
314-
}
315-
if repoIDs, err = env.RepoIDs(1, opts.RequestedUser.NumRepos); err != nil {
316-
return nil, fmt.Errorf("GetUserRepositories: %v", err)
316+
if err := ActionList(actions).LoadAttributes(); err != nil {
317+
return nil, fmt.Errorf("LoadAttributes: %v", err)
318+
}
319+
320+
return actions, nil
321+
}
322+
323+
func activityReadable(user *User, doer *User) bool {
324+
var doerID int64
325+
if doer != nil {
326+
doerID = doer.ID
327+
}
328+
if doer == nil || !doer.IsAdmin {
329+
if user.KeepActivityPrivate && doerID != user.ID {
330+
return false
317331
}
332+
}
333+
return true
334+
}
318335

319-
cond = cond.And(builder.In("repo_id", repoIDs))
320-
} else {
321-
cond = cond.And(builder.In("repo_id", AccessibleRepoIDsQuery(opts.Actor)))
336+
func activityQueryCondition(opts GetFeedsOptions) (builder.Cond, error) {
337+
cond := builder.NewCond()
338+
339+
var repoIDs []int64
340+
var actorID int64
341+
if opts.Actor != nil {
342+
actorID = opts.Actor.ID
322343
}
323344

345+
// check readable repositories by doer/actor
324346
if opts.Actor == nil || !opts.Actor.IsAdmin {
325-
if opts.RequestedUser.KeepActivityPrivate && actorID != opts.RequestedUser.ID {
326-
return make([]*Action, 0), nil
347+
if opts.RequestedUser.IsOrganization() {
348+
env, err := opts.RequestedUser.AccessibleReposEnv(actorID)
349+
if err != nil {
350+
return nil, fmt.Errorf("AccessibleReposEnv: %v", err)
351+
}
352+
if repoIDs, err = env.RepoIDs(1, opts.RequestedUser.NumRepos); err != nil {
353+
return nil, fmt.Errorf("GetUserRepositories: %v", err)
354+
}
355+
cond = cond.And(builder.In("repo_id", repoIDs))
356+
} else {
357+
cond = cond.And(builder.In("repo_id", AccessibleRepoIDsQuery(opts.Actor)))
327358
}
328359
}
329360

@@ -335,20 +366,9 @@ func GetFeeds(opts GetFeedsOptions) ([]*Action, error) {
335366
if !opts.IncludePrivate {
336367
cond = cond.And(builder.Eq{"is_private": false})
337368
}
338-
339369
if !opts.IncludeDeleted {
340370
cond = cond.And(builder.Eq{"is_deleted": false})
341371
}
342372

343-
actions := make([]*Action, 0, setting.UI.FeedPagingNum)
344-
345-
if err := x.Limit(setting.UI.FeedPagingNum).Desc("id").Where(cond).Find(&actions); err != nil {
346-
return nil, fmt.Errorf("Find: %v", err)
347-
}
348-
349-
if err := ActionList(actions).LoadAttributes(); err != nil {
350-
return nil, fmt.Errorf("LoadAttributes: %v", err)
351-
}
352-
353-
return actions, nil
373+
return cond, nil
354374
}

models/fixtures/action.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,12 @@
2323
act_user_id: 11
2424
repo_id: 9
2525
is_private: false
26+
27+
-
28+
id: 4
29+
user_id: 16
30+
op_type: 12 # close issue
31+
act_user_id: 16
32+
repo_id: 22
33+
is_private: true
34+
created_unix: 1603267920

models/user_heatmap.go

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ type UserHeatmapData struct {
1616
}
1717

1818
// GetUserHeatmapDataByUser returns an array of UserHeatmapData
19-
func GetUserHeatmapDataByUser(user *User) ([]*UserHeatmapData, error) {
19+
func GetUserHeatmapDataByUser(user *User, doer *User) ([]*UserHeatmapData, error) {
2020
hdata := make([]*UserHeatmapData, 0)
2121

22-
if user.KeepActivityPrivate {
22+
if !activityReadable(user, doer) {
2323
return hdata, nil
2424
}
2525

@@ -37,22 +37,26 @@ func GetUserHeatmapDataByUser(user *User) ([]*UserHeatmapData, error) {
3737
groupByName = groupBy
3838
}
3939

40-
sess := x.Select(groupBy+" AS timestamp, count(user_id) as contributions").
41-
Table("action").
42-
Where("user_id = ?", user.ID).
43-
And("created_unix > ?", (timeutil.TimeStampNow() - 31536000))
44-
45-
// * Heatmaps for individual users only include actions that the user themself
46-
// did.
47-
// * For organizations actions by all users that were made in owned
48-
// repositories are counted.
49-
if user.Type == UserTypeIndividual {
50-
sess = sess.And("act_user_id = ?", user.ID)
40+
cond, err := activityQueryCondition(GetFeedsOptions{
41+
RequestedUser: user,
42+
Actor: doer,
43+
IncludePrivate: true, // don't filter by private, as we already filter by repo access
44+
IncludeDeleted: true,
45+
// * Heatmaps for individual users only include actions that the user themself did.
46+
// * For organizations actions by all users that were made in owned
47+
// repositories are counted.
48+
OnlyPerformedBy: !user.IsOrganization(),
49+
})
50+
if err != nil {
51+
return nil, err
5152
}
5253

53-
err := sess.GroupBy(groupByName).
54+
return hdata, x.
55+
Select(groupBy+" AS timestamp, count(user_id) as contributions").
56+
Table("action").
57+
Where(cond).
58+
And("created_unix > ?", (timeutil.TimeStampNow() - 31536000)).
59+
GroupBy(groupByName).
5460
OrderBy("timestamp").
5561
Find(&hdata)
56-
57-
return hdata, err
5862
}

models/user_heatmap_test.go

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package models
66

77
import (
88
"encoding/json"
9+
"fmt"
910
"testing"
1011

1112
"github.com/stretchr/testify/assert"
@@ -14,35 +15,45 @@ import (
1415
func TestGetUserHeatmapDataByUser(t *testing.T) {
1516
testCases := []struct {
1617
userID int64
18+
doerID int64
1719
CountResult int
1820
JSONResult string
1921
}{
20-
{2, 1, `[{"timestamp":1603152000,"contributions":1}]`},
21-
{3, 0, `[]`},
22+
{2, 2, 1, `[{"timestamp":1603152000,"contributions":1}]`}, // self looks at action in private repo
23+
{2, 1, 1, `[{"timestamp":1603152000,"contributions":1}]`}, // admin looks at action in private repo
24+
{2, 3, 0, `[]`}, // other user looks at action in private repo
25+
{2, 0, 0, `[]`}, // nobody looks at action in private repo
26+
{16, 15, 1, `[{"timestamp":1603238400,"contributions":1}]`}, // collaborator looks at action in private repo
27+
{3, 3, 0, `[]`}, // no action action not performed by target user
2228
}
2329
// Prepare
2430
assert.NoError(t, PrepareTestDatabase())
2531

26-
for _, tc := range testCases {
27-
28-
// Insert some action
32+
for i, tc := range testCases {
2933
user := AssertExistsAndLoadBean(t, &User{ID: tc.userID}).(*User)
3034

35+
doer := &User{ID: tc.doerID}
36+
_, err := loadBeanIfExists(doer)
37+
assert.NoError(t, err)
38+
if tc.doerID == 0 {
39+
doer = nil
40+
}
41+
3142
// get the action for comparison
3243
actions, err := GetFeeds(GetFeedsOptions{
3344
RequestedUser: user,
34-
Actor: user,
45+
Actor: doer,
3546
IncludePrivate: true,
36-
OnlyPerformedBy: false,
47+
OnlyPerformedBy: true,
3748
IncludeDeleted: true,
3849
})
3950
assert.NoError(t, err)
4051

4152
// Get the heatmap and compare
42-
heatmap, err := GetUserHeatmapDataByUser(user)
53+
heatmap, err := GetUserHeatmapDataByUser(user, doer)
4354
assert.NoError(t, err)
4455
assert.Equal(t, len(actions), len(heatmap), "invalid action count: did the test data became too old?")
45-
assert.Equal(t, tc.CountResult, len(heatmap))
56+
assert.Equal(t, tc.CountResult, len(heatmap), fmt.Sprintf("testcase %d", i))
4657

4758
//Test JSON rendering
4859
jsonData, err := json.Marshal(heatmap)

routers/api/v1/user/user.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ func GetUserHeatmapData(ctx *context.APIContext) {
166166
return
167167
}
168168

169-
heatmap, err := models.GetUserHeatmapDataByUser(user)
169+
heatmap, err := models.GetUserHeatmapDataByUser(user, ctx.User)
170170
if err != nil {
171171
ctx.Error(http.StatusInternalServerError, "GetUserHeatmapDataByUser", err)
172172
return

routers/user/home.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ func Dashboard(ctx *context.Context) {
115115
// no heatmap access for admins; GetUserHeatmapDataByUser ignores the calling user
116116
// so everyone would get the same empty heatmap
117117
if setting.Service.EnableUserHeatmap && !ctxUser.KeepActivityPrivate {
118-
data, err := models.GetUserHeatmapDataByUser(ctxUser)
118+
data, err := models.GetUserHeatmapDataByUser(ctxUser, ctx.User)
119119
if err != nil {
120120
ctx.ServerError("GetUserHeatmapDataByUser", err)
121121
return

routers/user/profile.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func Profile(ctx *context.Context) {
9898
// no heatmap access for admins; GetUserHeatmapDataByUser ignores the calling user
9999
// so everyone would get the same empty heatmap
100100
if setting.Service.EnableUserHeatmap && !ctxUser.KeepActivityPrivate {
101-
data, err := models.GetUserHeatmapDataByUser(ctxUser)
101+
data, err := models.GetUserHeatmapDataByUser(ctxUser, ctx.User)
102102
if err != nil {
103103
ctx.ServerError("GetUserHeatmapDataByUser", err)
104104
return

0 commit comments

Comments
 (0)