mirror of
https://github.com/goharbor/harbor.git
synced 2024-12-20 07:37:38 +01:00
fix(quota-driver): owner name of project quota reference object
Signed-off-by: He Weiwei <hweiwei@vmware.com>
This commit is contained in:
parent
f930786050
commit
8eb17be13c
@ -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 {
|
||||||
@ -202,7 +208,7 @@ func DeleteUser(userID int) error {
|
|||||||
name := fmt.Sprintf("%s#%d", user.Username, user.UserID)
|
name := fmt.Sprintf("%s#%d", user.Username, user.UserID)
|
||||||
email := fmt.Sprintf("%s#%d", user.Email, 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 = ?
|
set deleted = true, username = ?, email = ?
|
||||||
where user_id = ?`, name, email, userID).Exec()
|
where user_id = ?`, name, email, userID).Exec()
|
||||||
return err
|
return err
|
||||||
|
@ -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
|
||||||
|
@ -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,
|
||||||
|
@ -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)
|
||||||
|
Loading…
Reference in New Issue
Block a user