mirror of
https://github.com/goharbor/harbor.git
synced 2025-02-18 21:01:27 +01:00
Merge pull request #2367 from ywk253100/170524_search
Refactor search API
This commit is contained in:
commit
d00aba8de8
@ -504,7 +504,7 @@ func TestChangeUserPasswordWithIncorrectOldPassword(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestQueryRelevantProjectsWhenNoProjectAdded(t *testing.T) {
|
func TestQueryRelevantProjectsWhenNoProjectAdded(t *testing.T) {
|
||||||
projects, err := SearchProjects(currentUser.UserID)
|
projects, err := GetHasReadPermProjects(currentUser.Username)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Error occurred in QueryRelevantProjects: %v", err)
|
t.Errorf("Error occurred in QueryRelevantProjects: %v", err)
|
||||||
}
|
}
|
||||||
|
@ -156,11 +156,18 @@ func ToggleProjectPublicity(projectID int64, publicity int) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// SearchProjects returns a project list,
|
// GetHasReadPermProjects returns a project list,
|
||||||
// which satisfies the following conditions:
|
// which satisfies the following conditions:
|
||||||
// 1. the project is not deleted
|
// 1. the project is not deleted
|
||||||
// 2. the prject is public or the user is a member of the project
|
// 2. the prject is public or the user is a member of the project
|
||||||
func SearchProjects(userID int) ([]*models.Project, error) {
|
func GetHasReadPermProjects(username string) ([]*models.Project, error) {
|
||||||
|
user, err := GetUser(models.User{
|
||||||
|
Username: username,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
o := GetOrmer()
|
o := GetOrmer()
|
||||||
|
|
||||||
sql :=
|
sql :=
|
||||||
@ -174,7 +181,7 @@ func SearchProjects(userID int) ([]*models.Project, error) {
|
|||||||
|
|
||||||
var projects []*models.Project
|
var projects []*models.Project
|
||||||
|
|
||||||
if _, err := o.Raw(sql, userID).QueryRows(&projects); err != nil {
|
if _, err := o.Raw(sql, user.UserID).QueryRows(&projects); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -110,6 +110,11 @@ func (f *fakePM) GetAll(*models.QueryParam) ([]*models.Project, error) {
|
|||||||
return []*models.Project{}, nil
|
return []*models.Project{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// nil implement
|
||||||
|
func (f *fakePM) GetHasReadPerm(username ...string) ([]*models.Project, error) {
|
||||||
|
return []*models.Project{}, nil
|
||||||
|
}
|
||||||
|
|
||||||
// nil implement
|
// nil implement
|
||||||
func (f *fakePM) GetTotal(*models.QueryParam) (int64, error) {
|
func (f *fakePM) GetTotal(*models.QueryParam) (int64, error) {
|
||||||
return 0, nil
|
return 0, nil
|
||||||
|
@ -15,11 +15,12 @@
|
|||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/vmware/harbor/src/common/api"
|
"github.com/vmware/harbor/src/common"
|
||||||
"github.com/vmware/harbor/src/common/dao"
|
"github.com/vmware/harbor/src/common/dao"
|
||||||
"github.com/vmware/harbor/src/common/models"
|
"github.com/vmware/harbor/src/common/models"
|
||||||
"github.com/vmware/harbor/src/common/utils"
|
"github.com/vmware/harbor/src/common/utils"
|
||||||
@ -29,7 +30,7 @@ import (
|
|||||||
|
|
||||||
// SearchAPI handles requesst to /api/search
|
// SearchAPI handles requesst to /api/search
|
||||||
type SearchAPI struct {
|
type SearchAPI struct {
|
||||||
api.BaseAPI
|
BaseController
|
||||||
}
|
}
|
||||||
|
|
||||||
type searchResult struct {
|
type searchResult struct {
|
||||||
@ -39,33 +40,25 @@ type searchResult struct {
|
|||||||
|
|
||||||
// Get ...
|
// Get ...
|
||||||
func (s *SearchAPI) Get() {
|
func (s *SearchAPI) Get() {
|
||||||
userID, _, ok := s.GetUserIDForRequest()
|
|
||||||
if !ok {
|
|
||||||
userID = dao.NonExistUserID
|
|
||||||
}
|
|
||||||
|
|
||||||
keyword := s.GetString("q")
|
keyword := s.GetString("q")
|
||||||
|
isAuthenticated := s.SecurityCtx.IsAuthenticated()
|
||||||
isSysAdmin, err := dao.IsAdminRole(userID)
|
username := s.SecurityCtx.GetUsername()
|
||||||
if err != nil {
|
isSysAdmin := s.SecurityCtx.IsSysAdmin()
|
||||||
log.Errorf("failed to check whether the user %d is system admin: %v", userID, err)
|
|
||||||
s.CustomAbort(http.StatusInternalServerError, "internal error")
|
|
||||||
}
|
|
||||||
|
|
||||||
var projects []*models.Project
|
var projects []*models.Project
|
||||||
|
var err error
|
||||||
|
|
||||||
if isSysAdmin {
|
if !isAuthenticated {
|
||||||
projects, err = dao.GetProjects(nil)
|
projects, err = s.ProjectMgr.GetPublic()
|
||||||
if err != nil {
|
} else if isSysAdmin {
|
||||||
log.Errorf("failed to get all projects: %v", err)
|
projects, err = s.ProjectMgr.GetAll(nil)
|
||||||
s.CustomAbort(http.StatusInternalServerError, "internal error")
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
projects, err = dao.SearchProjects(userID)
|
projects, err = s.ProjectMgr.GetHasReadPerm(username)
|
||||||
if err != nil {
|
}
|
||||||
log.Errorf("failed to get user %d 's relevant projects: %v", userID, err)
|
if err != nil {
|
||||||
s.CustomAbort(http.StatusInternalServerError, "internal error")
|
s.HandleInternalServerError(fmt.Sprintf(
|
||||||
}
|
"failed to get projects: %v", err))
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
projectSorter := &models.ProjectSorter{Projects: projects}
|
projectSorter := &models.ProjectSorter{Projects: projects}
|
||||||
@ -76,17 +69,19 @@ func (s *SearchAPI) Get() {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if userID != dao.NonExistUserID {
|
if isAuthenticated {
|
||||||
roles, err := dao.GetUserProjectRoles(userID, p.ProjectID)
|
roles, err := s.ProjectMgr.GetRoles(username, p.ProjectID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("failed to get user's project role: %v", err)
|
s.HandleInternalServerError(fmt.Sprintf("failed to get roles of user %s to project %d: %v",
|
||||||
s.CustomAbort(http.StatusInternalServerError, "")
|
username, p.ProjectID, err))
|
||||||
}
|
return
|
||||||
if len(roles) != 0 {
|
|
||||||
p.Role = roles[0].RoleID
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if p.Role == models.PROJECTADMIN || isSysAdmin {
|
if len(roles) != 0 {
|
||||||
|
p.Role = roles[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
if p.Role == common.RoleProjectAdmin || isSysAdmin {
|
||||||
p.Togglable = true
|
p.Togglable = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -200,3 +200,13 @@ func (p *ProjectManager) GetTotal(query *models.QueryParam) (
|
|||||||
int64, error) {
|
int64, error) {
|
||||||
return dao.GetTotalOfProjects(query)
|
return dao.GetTotalOfProjects(query)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetHasReadPerm returns projects which are public or the user is a member of
|
||||||
|
func (p *ProjectManager) GetHasReadPerm(username ...string) (
|
||||||
|
[]*models.Project, error) {
|
||||||
|
if len(username) == 0 || len(username[0]) == 0 {
|
||||||
|
return p.GetPublic()
|
||||||
|
}
|
||||||
|
|
||||||
|
return dao.GetHasReadPermProjects(username[0])
|
||||||
|
}
|
||||||
|
@ -298,3 +298,58 @@ func TestGetAll(t *testing.T) {
|
|||||||
}
|
}
|
||||||
assert.True(t, exist)
|
assert.True(t, exist)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGetHasReadPerm(t *testing.T) {
|
||||||
|
pm := &ProjectManager{}
|
||||||
|
|
||||||
|
// do not pass username
|
||||||
|
projects, err := pm.GetHasReadPerm()
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assert.NotEqual(t, 0, len(projects))
|
||||||
|
exist := false
|
||||||
|
for _, project := range projects {
|
||||||
|
if project.ProjectID == 1 {
|
||||||
|
exist = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assert.True(t, exist)
|
||||||
|
|
||||||
|
// username is nil
|
||||||
|
projects, err = pm.GetHasReadPerm("")
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assert.NotEqual(t, 0, len(projects))
|
||||||
|
exist = false
|
||||||
|
for _, project := range projects {
|
||||||
|
if project.ProjectID == 1 {
|
||||||
|
exist = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assert.True(t, exist)
|
||||||
|
|
||||||
|
// valid username
|
||||||
|
id, err := pm.Create(&models.Project{
|
||||||
|
Name: "get_has_read_perm_test",
|
||||||
|
OwnerID: 1,
|
||||||
|
Public: 0,
|
||||||
|
})
|
||||||
|
assert.Nil(t, err)
|
||||||
|
defer pm.Delete(id)
|
||||||
|
|
||||||
|
projects, err = pm.GetHasReadPerm("admin")
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assert.NotEqual(t, 0, len(projects))
|
||||||
|
exist1 := false
|
||||||
|
exist2 := false
|
||||||
|
for _, project := range projects {
|
||||||
|
if project.ProjectID == 1 {
|
||||||
|
exist1 = true
|
||||||
|
}
|
||||||
|
if project.ProjectID == id {
|
||||||
|
exist2 = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assert.True(t, exist1)
|
||||||
|
assert.True(t, exist2)
|
||||||
|
}
|
||||||
|
@ -36,4 +36,8 @@ type ProjectManager interface {
|
|||||||
GetAll(query *models.QueryParam) ([]*models.Project, error)
|
GetAll(query *models.QueryParam) ([]*models.Project, error)
|
||||||
// GetTotal returns the total count according to the query parameters
|
// GetTotal returns the total count according to the query parameters
|
||||||
GetTotal(query *models.QueryParam) (int64, error)
|
GetTotal(query *models.QueryParam) (int64, error)
|
||||||
|
// GetHasReadPerm returns a project list which the user has read
|
||||||
|
// permission of. The list should contains all public projects and
|
||||||
|
// projects which the user is a member of if the username is not nil
|
||||||
|
GetHasReadPerm(username ...string) ([]*models.Project, error)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user