fix visibility of top repos for admin (#1306)

This commit is contained in:
Dmitry Lazurkin 2017-01-21 19:05:20 +03:00
parent 733a9fd628
commit 444b7f97bf
2 changed files with 53 additions and 27 deletions

View File

@ -110,13 +110,17 @@ func GetTopRepos(userID int, count int) ([]models.TopRepo, error) {
where ( where (
p.deleted = 0 and ( p.deleted = 0 and (
p.public = 1 or ( p.public = 1 or (
? <> ? and exists ( ? <> ? and (
exists (
select 1 from user u
where u.user_id = ? and u.sysadmin_flag = 1
) or exists (
select 1 from project_member pm select 1 from project_member pm
where pm.project_id = p.project_id and pm.user_id = ? where pm.project_id = p.project_id and pm.user_id = ?
)))) )))))
order by r.pull_count desc, r.name limit ?` order by r.pull_count desc, r.name limit ?`
repositories := []*models.RepoRecord{} repositories := []*models.RepoRecord{}
_, err := GetOrmer().Raw(sql, userID, NonExistUserID, userID, count).QueryRows(&repositories) _, err := GetOrmer().Raw(sql, userID, NonExistUserID, userID, userID, count).QueryRows(&repositories)
if err != nil { if err != nil {
return topRepos, err return topRepos, err
} }

View File

@ -202,17 +202,15 @@ func TestGetTopRepos(t *testing.T) {
require.NoError(err) require.NoError(err)
project2 := models.Project{ project2 := models.Project{
OwnerID: admin.UserID, OwnerID: user.UserID,
Name: "project2", Name: "project2",
CreationTime: time.Now(), CreationTime: time.Now(),
OwnerName: admin.Username, OwnerName: user.Username,
Public: 0, Public: 0,
} }
project2.ProjectID, err = AddProject(project2) project2.ProjectID, err = AddProject(project2)
require.NoError(err) require.NoError(err)
require.NoError(AddProjectMember(project2.ProjectID, user.UserID, models.PROJECTADMIN))
err = AddRepository(*repository) err = AddRepository(*repository)
require.NoError(err) require.NoError(err)
@ -268,12 +266,13 @@ func TestGetTopRepos(t *testing.T) {
} }
err = AddRepository(*deletedPublicRepository1) err = AddRepository(*deletedPublicRepository1)
require.NoError(err) require.NoError(err)
DeleteProject(deletedPublicProject.ProjectID) err = DeleteProject(deletedPublicProject.ProjectID)
require.NoError(err)
var topRepos []models.TopRepo var topRepos []models.TopRepo
// anonymous should retrieve public non-deleted repositories // anonymous should retrieve public non-deleted repositories
topRepos, err = GetTopRepos(NonExistUserID, 3) topRepos, err = GetTopRepos(NonExistUserID, 100)
require.NoError(err) require.NoError(err)
require.Len(topRepos, 1) require.Len(topRepos, 1)
require.Equal(topRepos, []models.TopRepo{ require.Equal(topRepos, []models.TopRepo{
@ -283,7 +282,45 @@ func TestGetTopRepos(t *testing.T) {
}, },
}) })
// admin should retrieve all visible repositories limited by count // admin should retrieve all repositories
topRepos, err = GetTopRepos(admin.UserID, 100)
require.NoError(err)
require.Len(topRepos, 4)
require.Equal(topRepos, []models.TopRepo{
models.TopRepo{
RepoName: repository3.Name,
AccessCount: repository3.PullCount,
},
models.TopRepo{
RepoName: repository2.Name,
AccessCount: repository2.PullCount,
},
models.TopRepo{
RepoName: repository1.Name,
AccessCount: repository1.PullCount,
},
models.TopRepo{
RepoName: repository.Name,
AccessCount: repository.PullCount,
},
})
// user should retrieve visible repositories
topRepos, err = GetTopRepos(user.UserID, 100)
require.NoError(err)
require.Len(topRepos, 2)
require.Equal(topRepos, []models.TopRepo{
models.TopRepo{
RepoName: repository3.Name,
AccessCount: repository3.PullCount,
},
models.TopRepo{
RepoName: repository.Name,
AccessCount: repository.PullCount,
},
})
// limit by count
topRepos, err = GetTopRepos(admin.UserID, 3) topRepos, err = GetTopRepos(admin.UserID, 3)
require.NoError(err) require.NoError(err)
require.Len(topRepos, 3) require.Len(topRepos, 3)
@ -301,21 +338,6 @@ func TestGetTopRepos(t *testing.T) {
AccessCount: repository1.PullCount, AccessCount: repository1.PullCount,
}, },
}) })
// user should retrieve all visible repositories
topRepos, err = GetTopRepos(user.UserID, 3)
require.NoError(err)
require.Len(topRepos, 2)
require.Equal(topRepos, []models.TopRepo{
models.TopRepo{
RepoName: repository3.Name,
AccessCount: repository3.PullCount,
},
models.TopRepo{
RepoName: repository.Name,
AccessCount: repository.PullCount,
},
})
} }
func addRepository(repository *models.RepoRecord) error { func addRepository(repository *models.RepoRecord) error {