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 {
qs := GetOrmer().QueryTable(&models.User{}).
Filter("deleted", 0).
Filter("user_id__gt", 1)
qs := GetOrmer().QueryTable(&models.User{}).Filter("deleted", 0)
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 {
@ -202,7 +208,7 @@ func DeleteUser(userID int) error {
name := fmt.Sprintf("%s#%d", user.Username, user.UserID)
email := fmt.Sprintf("%s#%d", user.Email, user.UserID)
_, err = o.Raw(`update harbor_user
_, err = o.Raw(`update harbor_user
set deleted = true, username = ?, email = ?
where user_id = ?`, name, email, userID).Exec()
return err

View File

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

View File

@ -55,11 +55,23 @@ func getProjectsBatchFn(ctx context.Context, keys dataloader.Keys) []*dataloader
return handleError(err)
}
var ownerIDs []int
var projectsMap = make(map[int64]*models.Project, len(projectIDs))
for _, project := range projects {
ownerIDs = append(ownerIDs, project.OwnerID)
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
for _, projectID := range projectIDs {
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))
}
owner, ok := ownersMap[project.OwnerID]
if ok {
project.OwnerName = owner.Username
}
result := dataloader.Result{
Data: project,
Error: nil,

View File

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