From 3fdc0fd9badd66fbf72ca579105c354f47a9c860 Mon Sep 17 00:00:00 2001 From: stonezdj Date: Mon, 4 Mar 2019 11:22:37 +0800 Subject: [PATCH] Search local DB first when adding a project member with username Signed-off-by: stonezdj --- src/core/api/projectmember.go | 12 +++++++++++- src/core/auth/ldap/ldap_test.go | 27 ++++++++++++++++++++++++--- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/src/core/api/projectmember.go b/src/core/api/projectmember.go index f4e52d672..27fc4252d 100644 --- a/src/core/api/projectmember.go +++ b/src/core/api/projectmember.go @@ -22,6 +22,7 @@ import ( "strings" "github.com/goharbor/harbor/src/common" + "github.com/goharbor/harbor/src/common/dao" "github.com/goharbor/harbor/src/common/dao/project" "github.com/goharbor/harbor/src/common/models" "github.com/goharbor/harbor/src/common/rbac" @@ -220,11 +221,20 @@ func AddProjectMember(projectID int64, request models.MemberReq) (int, error) { member.EntityID = request.MemberGroup.ID member.EntityType = common.GroupMember } else if len(request.MemberUser.Username) > 0 { + var userID int member.EntityType = common.UserMember - userID, err := auth.SearchAndOnBoardUser(request.MemberUser.Username) + u, err := dao.GetUser(models.User{Username: request.MemberUser.Username}) if err != nil { return 0, err } + if u != nil { + userID = u.UserID + } else { + userID, err = auth.SearchAndOnBoardUser(request.MemberUser.Username) + if err != nil { + return 0, err + } + } member.EntityID = userID } else if len(request.MemberGroup.LdapGroupDN) > 0 { diff --git a/src/core/auth/ldap/ldap_test.go b/src/core/auth/ldap/ldap_test.go index 93acb8606..210795e97 100644 --- a/src/core/auth/ldap/ldap_test.go +++ b/src/core/auth/ldap/ldap_test.go @@ -32,7 +32,7 @@ import ( coreConfig "github.com/goharbor/harbor/src/core/config" ) -var adminServerLdapTestConfig = map[string]interface{}{ +var ldapTestConfig = map[string]interface{}{ common.ExtEndpoint: "host01.com", common.AUTHMode: "ldap_auth", common.DatabaseType: "postgresql", @@ -75,7 +75,7 @@ var adminServerLdapTestConfig = map[string]interface{}{ func TestMain(m *testing.M) { test.InitDatabaseFromEnv() - coreConfig.InitWithSettings(adminServerLdapTestConfig) + coreConfig.InitWithSettings(ldapTestConfig) secretKeyPath := "/tmp/secretkey" _, err := test.GenerateKey(secretKeyPath) @@ -93,7 +93,8 @@ func TestMain(m *testing.M) { initSqls := []string{ "insert into harbor_user (username, email, password, realname) values ('member_test_01', 'member_test_01@example.com', '123456', 'member_test_01')", "insert into project (name, owner_id) values ('member_test_01', 1)", - "insert into user_group (group_name, group_type, group_property) values ('test_group_01', 1, 'CN=harbor_users,OU=sample,OU=vmware,DC=harbor,DC=com')", + "insert into project (name, owner_id) values ('member_test_02', 1)", + "insert into user_group (group_name, group_type, ldap_group_dn) values ('test_group_01', 1, 'CN=harbor_users,OU=sample,OU=vmware,DC=harbor,DC=com')", "update project set owner_id = (select user_id from harbor_user where username = 'member_test_01') where name = 'member_test_01'", "insert into project_member (project_id, entity_id, entity_type, role) values ( (select project_id from project where name = 'member_test_01') , (select user_id from harbor_user where username = 'member_test_01'), 'u', 1)", "insert into project_member (project_id, entity_id, entity_type, role) values ( (select project_id from project where name = 'member_test_01') , (select id from user_group where group_name = 'test_group_01'), 'g', 1)", @@ -101,6 +102,7 @@ func TestMain(m *testing.M) { clearSqls := []string{ "delete from project where name='member_test_01'", + "delete from project where name='member_test_02'", "delete from harbor_user where username='member_test_01' or username='pm_sample'", "delete from user_group", "delete from project_member", @@ -389,6 +391,25 @@ func TestAddProjectMemberWithLdapUser(t *testing.T) { if pmid == 0 { t.Errorf("Error occurred in AddOrUpdateProjectMember: pmid:%v", pmid) } + + currentProject, err = dao.GetProjectByName("member_test_02") + if err != nil { + t.Errorf("Error occurred when GetProjectByName: %v", err) + } + member2 := models.MemberReq{ + ProjectID: currentProject.ProjectID, + MemberUser: models.User{ + Username: "mike", + }, + Role: models.PROJECTADMIN, + } + pmid, err = api.AddProjectMember(currentProject.ProjectID, member2) + if err != nil { + t.Errorf("Error occurred in AddOrUpdateProjectMember: %v", err) + } + if pmid == 0 { + t.Errorf("Error occurred in AddOrUpdateProjectMember: pmid:%v", pmid) + } } func TestAddProjectMemberWithLdapGroup(t *testing.T) { currentProject, err := dao.GetProjectByName("member_test_01")