mirror of
https://github.com/goharbor/harbor.git
synced 2024-11-30 06:03:45 +01:00
Merge pull request #1554 from ywk253100/170309_bug_fix
bug fix in search API: do not return duplicate projects when user does not login
This commit is contained in:
commit
6eb2912aad
@ -171,7 +171,7 @@ func SearchProjects(userID int) ([]models.Project, error) {
|
|||||||
|
|
||||||
sql :=
|
sql :=
|
||||||
`select distinct p.project_id, p.name, p.public,
|
`select distinct p.project_id, p.name, p.public,
|
||||||
p.owner_id, p.creation_time, p.update_time, pm.role role
|
p.owner_id, p.creation_time, p.update_time
|
||||||
from project p
|
from project p
|
||||||
left join project_member pm
|
left join project_member pm
|
||||||
on p.project_id = pm.project_id
|
on p.project_id = pm.project_id
|
||||||
|
@ -73,15 +73,22 @@ func (s *SearchAPI) Get() {
|
|||||||
sort.Sort(projectSorter)
|
sort.Sort(projectSorter)
|
||||||
projectResult := []models.Project{}
|
projectResult := []models.Project{}
|
||||||
for _, p := range projects {
|
for _, p := range projects {
|
||||||
match := true
|
|
||||||
if len(keyword) > 0 && !strings.Contains(p.Name, keyword) {
|
if len(keyword) > 0 && !strings.Contains(p.Name, keyword) {
|
||||||
match = false
|
continue
|
||||||
}
|
}
|
||||||
if match {
|
|
||||||
if userID != dao.NonExistUserID {
|
if userID != dao.NonExistUserID {
|
||||||
if isSysAdmin {
|
if isSysAdmin {
|
||||||
p.Role = models.PROJECTADMIN
|
p.Role = models.PROJECTADMIN
|
||||||
|
} else {
|
||||||
|
roles, err := dao.GetUserProjectRoles(userID, p.ProjectID)
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("failed to get user's project role: %v", err)
|
||||||
|
s.CustomAbort(http.StatusInternalServerError, "")
|
||||||
}
|
}
|
||||||
|
p.Role = roles[0].RoleID
|
||||||
|
}
|
||||||
|
|
||||||
if p.Role == models.PROJECTADMIN {
|
if p.Role == models.PROJECTADMIN {
|
||||||
p.Togglable = true
|
p.Togglable = true
|
||||||
}
|
}
|
||||||
@ -97,7 +104,6 @@ func (s *SearchAPI) Get() {
|
|||||||
|
|
||||||
projectResult = append(projectResult, p)
|
projectResult = append(projectResult, p)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
repositoryResult, err := filterRepositories(projects, keyword)
|
repositoryResult, err := filterRepositories(projects, keyword)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -28,7 +28,19 @@ func TestSearch(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//--------case 2 : Response Code = 200, sysAdmin and search repo--------//
|
//--------case 2 : Response Code = 200, sysAdmin and search repo--------//
|
||||||
httpStatusCode, result, err = apiTest.SearchGet("docker", *admin)
|
httpStatusCode, result, err = apiTest.SearchGet("library", *admin)
|
||||||
|
if err != nil {
|
||||||
|
t.Error("Error while search project or repository", err.Error())
|
||||||
|
t.Log(err)
|
||||||
|
} else {
|
||||||
|
assert.Equal(int(200), httpStatusCode, "httpStatusCode should be 200")
|
||||||
|
assert.Equal("library", result.Repositories[0].ProjectName, "Project name should be library")
|
||||||
|
assert.Equal("library/docker", result.Repositories[0].RepositoryName, "Repository name should be library/docker")
|
||||||
|
assert.Equal(int32(1), result.Repositories[0].ProjectPublic, "Project public status should be 1 (true)")
|
||||||
|
}
|
||||||
|
|
||||||
|
//--------case 3 : Response Code = 200, normal user and search repo--------//
|
||||||
|
httpStatusCode, result, err = apiTest.SearchGet("library", *testUser)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error("Error while search project or repository", err.Error())
|
t.Error("Error while search project or repository", err.Error())
|
||||||
t.Log(err)
|
t.Log(err)
|
||||||
|
Loading…
Reference in New Issue
Block a user