fix(quota-driver): owner name of project quota reference object

Signed-off-by: He Weiwei <hweiwei@vmware.com>
This commit is contained in:
He Weiwei 2019-08-20 07:02:53 +00:00
parent f930786050
commit 8eb17be13c
4 changed files with 30 additions and 6 deletions

View File

@ -117,12 +117,18 @@ func ListUsers(query *models.UserQuery) ([]models.User, error) {
} }
func userQueryConditions(query *models.UserQuery) orm.QuerySeter { func userQueryConditions(query *models.UserQuery) orm.QuerySeter {
qs := GetOrmer().QueryTable(&models.User{}). qs := GetOrmer().QueryTable(&models.User{}).Filter("deleted", 0)
Filter("deleted", 0).
Filter("user_id__gt", 1)
if query == nil { if query == nil {
return qs // Exclude admin account, see https://github.com/goharbor/harbor/issues/2527
return qs.Filter("user_id__gt", 1)
}
if len(query.UserIDs) > 0 {
qs = qs.Filter("user_id__in", query.UserIDs)
} else {
// Exclude admin account when not filter by UserIDs, see https://github.com/goharbor/harbor/issues/2527
qs = qs.Filter("user_id__gt", 1)
} }
if len(query.Username) > 0 { if len(query.Username) > 0 {

View File

@ -46,6 +46,7 @@ type User struct {
// UserQuery ... // UserQuery ...
type UserQuery struct { type UserQuery struct {
UserIDs []int
Username string Username string
Email string Email string
Pagination *Pagination Pagination *Pagination

View File

@ -55,11 +55,23 @@ func getProjectsBatchFn(ctx context.Context, keys dataloader.Keys) []*dataloader
return handleError(err) return handleError(err)
} }
var ownerIDs []int
var projectsMap = make(map[int64]*models.Project, len(projectIDs)) var projectsMap = make(map[int64]*models.Project, len(projectIDs))
for _, project := range projects { for _, project := range projects {
ownerIDs = append(ownerIDs, project.OwnerID)
projectsMap[project.ProjectID] = project projectsMap[project.ProjectID] = project
} }
owners, err := dao.ListUsers(&models.UserQuery{UserIDs: ownerIDs})
if err != nil {
return handleError(err)
}
var ownersMap = make(map[int]*models.User, len(owners))
for i, owner := range owners {
ownersMap[owner.UserID] = &owners[i]
}
var results []*dataloader.Result var results []*dataloader.Result
for _, projectID := range projectIDs { for _, projectID := range projectIDs {
project, ok := projectsMap[projectID] project, ok := projectsMap[projectID]
@ -67,6 +79,11 @@ func getProjectsBatchFn(ctx context.Context, keys dataloader.Keys) []*dataloader
return handleError(fmt.Errorf("project not found, "+"project_id: %d", projectID)) return handleError(fmt.Errorf("project not found, "+"project_id: %d", projectID))
} }
owner, ok := ownersMap[project.OwnerID]
if ok {
project.OwnerName = owner.Username
}
result := dataloader.Result{ result := dataloader.Result{
Data: project, Data: project,
Error: nil, Error: nil,

View File

@ -41,7 +41,7 @@ func (suite *DriverSuite) TestLoad() {
obj := dr.RefObject{ obj := dr.RefObject{
"id": int64(1), "id": int64(1),
"name": "library", "name": "library",
"owner_name": "", "owner_name": "admin",
} }
suite.Equal(obj, ref) suite.Equal(obj, ref)