Merge pull request #4674 from stonezdj/filter_not_work

Add project member search by name
This commit is contained in:
Daniel Jiang 2018-04-17 10:04:06 +08:00 committed by GitHub
commit 62a4bac48e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 100 additions and 4 deletions

View File

@ -459,6 +459,10 @@ paths:
format: int64
required: true
description: Relevant project ID.
- name: entityname
in: query
type: string
description: The entity name to search.
tags:
- Products
responses:

View File

@ -118,3 +118,33 @@ func DeleteProjectMemberByID(pmid int) error {
}
return nil
}
// SearchMemberByName search members of the project by entity_name
func SearchMemberByName(projectID int64, entityName string) ([]*models.Member, error) {
o := dao.GetOrmer()
sql := `(select pm.id, pm.project_id,
u.username as entity_name,
r.name as rolename,
pm.role, pm.entity_id, pm.entity_type
from project_member pm
left join user u on pm.entity_id = u.user_id and pm.entity_type = 'u'
left join role r on pm.role = r.role_id
where u.deleted = 0 and pm.project_id = ? and u.username like ? order by entity_name )
union
(select pm.id, pm.project_id,
ug.group_name as entity_name,
r.name as rolename,
pm.role, pm.entity_id, pm.entity_type
from project_member pm
left join user_group ug on pm.entity_id = ug.id and pm.entity_type = 'g'
left join role r on pm.role = r.role_id
where pm.project_id = ? and ug.group_name like ? order by entity_name ) `
queryParam := make([]interface{}, 4)
queryParam = append(queryParam, projectID)
queryParam = append(queryParam, "%"+dao.Escape(entityName)+"%")
queryParam = append(queryParam, projectID)
queryParam = append(queryParam, "%"+dao.Escape(entityName)+"%")
members := []*models.Member{}
_, err := o.Raw(sql, queryParam).QueryRows(&members)
return members, err
}

View File

@ -149,6 +149,25 @@ func TestAddProjectMember(t *testing.T) {
if len(memberList) == 0 {
t.Errorf("Failed to query project member, %v", queryMember)
}
_, err = AddProjectMember(models.Member{
ProjectID: -1,
EntityID: 1,
EntityType: common.UserMember,
Role: models.PROJECTADMIN,
})
if err == nil {
t.Fatal("Should failed with negative projectID")
}
_, err = AddProjectMember(models.Member{
ProjectID: 1,
EntityID: -1,
EntityType: common.UserMember,
Role: models.PROJECTADMIN,
})
if err == nil {
t.Fatal("Should failed with negative entityID")
}
}
func TestUpdateProjectMemberRole(t *testing.T) {
currentProject, err := dao.GetProjectByName("member_test_01")
@ -195,6 +214,23 @@ func TestUpdateProjectMemberRole(t *testing.T) {
t.Errorf("member doesn't match!")
}
memberList2, err := SearchMemberByName(currentProject.ProjectID, "pm_sample")
if err != nil {
t.Errorf("Error occurred when SearchMemberByName: %v", err)
}
if len(memberList2) == 0 {
t.Errorf("Failed to search user pm_sample, project_id:%v, entityname:%v",
currentProject.ProjectID, "pm_sample")
}
memberList3, err := SearchMemberByName(currentProject.ProjectID, "")
if err != nil {
t.Errorf("Error occurred when SearchMemberByName: %v", err)
}
if len(memberList3) == 0 {
t.Errorf("Failed to search user pm_sample, project_id:%v, entityname is empty",
currentProject.ProjectID)
}
}
func TestGetProjectMember(t *testing.T) {

View File

@ -118,7 +118,7 @@ func (l *LdapAPI) ImportUser() {
}
if len(ldapFailedImportUsers) > 0 {
l.HandleNotFound("Import LDAP user have internal error")
l.HandleNotFound("Import LDAP user is not found")
l.Data["json"] = ldapFailedImportUsers
l.ServeJSON()
return

View File

@ -73,7 +73,7 @@ func (pma *ProjectMemberAPI) Prepare() {
pmid, err := pma.GetInt64FromPath(":pmid")
if err != nil {
log.Errorf("Failed to get pmid from path, error %v", err)
log.Warningf("Failed to get pmid from path, error %v", err)
}
if pmid <= 0 && (pma.Ctx.Input.IsPut() || pma.Ctx.Input.IsDelete()) {
pma.HandleBadRequest(fmt.Sprintf("The project member id is invalid, pmid:%s", pma.GetStringFromPath(":pmid")))
@ -89,8 +89,8 @@ func (pma *ProjectMemberAPI) Get() {
queryMember.ProjectID = projectID
pma.Data["json"] = make([]models.Member, 0)
if pma.id == 0 {
//member id not set, return all member of current project
memberList, err := project.GetProjectMember(queryMember)
entityname := pma.GetString("entityname")
memberList, err := project.SearchMemberByName(projectID, entityname)
if err != nil {
pma.HandleInternalServerError(fmt.Sprintf("Failed to query database for member list, error: %v", err))
return
@ -98,6 +98,7 @@ func (pma *ProjectMemberAPI) Get() {
if len(memberList) > 0 {
pma.Data["json"] = memberList
}
} else {
//return a specific member
queryMember.ID = pma.id

View File

@ -61,6 +61,15 @@ func TestProjectMemberAPI_Get(t *testing.T) {
},
code: http.StatusNotFound,
},
// 404
&codeCheckingCase{
request: &testingRequest{
method: http.MethodGet,
url: "/api/projects/99999/members/121",
credential: admin,
},
code: http.StatusNotFound,
},
}
runCodeCheckingCases(t, cases...)
}
@ -119,6 +128,22 @@ func TestProjectMemberAPI_Post(t *testing.T) {
},
code: http.StatusInternalServerError,
},
&codeCheckingCase{
request: &testingRequest{
method: http.MethodGet,
url: "/api/projects/1/members?entityname=restuser",
credential: admin,
},
code: http.StatusOK,
},
&codeCheckingCase{
request: &testingRequest{
method: http.MethodGet,
url: "/api/projects/1/members",
credential: admin,
},
code: http.StatusOK,
},
}
runCodeCheckingCases(t, cases...)
}