mirror of
https://github.com/goharbor/harbor.git
synced 2024-12-23 00:57:44 +01:00
Merge pull request #4415 from stonezdj/user_group_and_project_member
Add DAO for user group and project member
This commit is contained in:
commit
203b1b52bb
@ -119,7 +119,7 @@ create table user_group
|
|||||||
id int NOT NULL AUTO_INCREMENT,
|
id int NOT NULL AUTO_INCREMENT,
|
||||||
group_name varchar(255) NOT NULL,
|
group_name varchar(255) NOT NULL,
|
||||||
group_type int default 0,
|
group_type int default 0,
|
||||||
group_property varchar(512) NOT NULL,
|
ldap_group_dn varchar(512) NOT NULL,
|
||||||
creation_time timestamp default CURRENT_TIMESTAMP,
|
creation_time timestamp default CURRENT_TIMESTAMP,
|
||||||
update_time timestamp default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
|
update_time timestamp default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
|
||||||
PRIMARY KEY (id)
|
PRIMARY KEY (id)
|
||||||
|
@ -64,7 +64,7 @@ create table user_group (
|
|||||||
id INTEGER PRIMARY KEY,
|
id INTEGER PRIMARY KEY,
|
||||||
group_name varchar(255) NOT NULL,
|
group_name varchar(255) NOT NULL,
|
||||||
group_type int default 0,
|
group_type int default 0,
|
||||||
group_property varchar(512) NOT NULL,
|
ldap_group_dn varchar(512) NOT NULL,
|
||||||
creation_time timestamp default CURRENT_TIMESTAMP,
|
creation_time timestamp default CURRENT_TIMESTAMP,
|
||||||
update_time timestamp default CURRENT_TIMESTAMP
|
update_time timestamp default CURRENT_TIMESTAMP
|
||||||
);
|
);
|
||||||
|
94
src/common/dao/group/usergroup.go
Normal file
94
src/common/dao/group/usergroup.go
Normal file
@ -0,0 +1,94 @@
|
|||||||
|
// Copyright (c) 2017 VMware, Inc. All Rights Reserved.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package group
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/vmware/harbor/src/common/dao"
|
||||||
|
"github.com/vmware/harbor/src/common/models"
|
||||||
|
"github.com/vmware/harbor/src/common/utils/log"
|
||||||
|
)
|
||||||
|
|
||||||
|
// AddUserGroup - Add User Group
|
||||||
|
func AddUserGroup(userGroup models.UserGroup) (int, error) {
|
||||||
|
o := dao.GetOrmer()
|
||||||
|
id, err := o.Insert(&userGroup)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return int(id), err
|
||||||
|
}
|
||||||
|
|
||||||
|
// QueryUserGroup - Query User Group
|
||||||
|
func QueryUserGroup(query models.UserGroup) ([]*models.UserGroup, error) {
|
||||||
|
o := dao.GetOrmer()
|
||||||
|
sql := `select id, group_name, group_type, ldap_group_dn from user_group where 1=1 `
|
||||||
|
sqlParam := make([]interface{}, 1)
|
||||||
|
groups := []*models.UserGroup{}
|
||||||
|
if len(query.GroupName) != 0 {
|
||||||
|
sql += ` and group_name like ? `
|
||||||
|
sqlParam = append(sqlParam, `%`+dao.Escape(query.GroupName)+`%`)
|
||||||
|
}
|
||||||
|
|
||||||
|
if query.GroupType != 0 {
|
||||||
|
sql += ` and group_type = ? `
|
||||||
|
sqlParam = append(sqlParam, query.GroupType)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(query.LdapGroupDN) != 0 {
|
||||||
|
sql += ` and ldap_group_dn = ? `
|
||||||
|
sqlParam = append(sqlParam, query.LdapGroupDN)
|
||||||
|
}
|
||||||
|
_, err := o.Raw(sql, sqlParam).QueryRows(&groups)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return groups, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetUserGroup ...
|
||||||
|
func GetUserGroup(id int) (*models.UserGroup, error) {
|
||||||
|
userGroup := models.UserGroup{ID: id}
|
||||||
|
o := dao.GetOrmer()
|
||||||
|
err := o.Read(&userGroup)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &userGroup, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeleteUserGroup ...
|
||||||
|
func DeleteUserGroup(id int) error {
|
||||||
|
userGroup := models.UserGroup{ID: id}
|
||||||
|
o := dao.GetOrmer()
|
||||||
|
_, err := o.Delete(&userGroup)
|
||||||
|
if err == nil {
|
||||||
|
//Delete all related project members
|
||||||
|
sql := `delete from project_member where entity_id = ? and entity_type='g'`
|
||||||
|
_, err := o.Raw(sql, id).Exec()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateUserGroupName ...
|
||||||
|
func UpdateUserGroupName(id int, groupName string) error {
|
||||||
|
log.Debugf("Updating user_group with id:%v, name:%v", id, groupName)
|
||||||
|
o := dao.GetOrmer()
|
||||||
|
sql := "update user_group set group_name = ? where id = ? "
|
||||||
|
_, err := o.Raw(sql, groupName, id).Exec()
|
||||||
|
return err
|
||||||
|
}
|
218
src/common/dao/group/usergroup_test.go
Normal file
218
src/common/dao/group/usergroup_test.go
Normal file
@ -0,0 +1,218 @@
|
|||||||
|
// Copyright (c) 2017 VMware, Inc. All Rights Reserved.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package group
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/vmware/harbor/src/common/dao"
|
||||||
|
"github.com/vmware/harbor/src/common/models"
|
||||||
|
"github.com/vmware/harbor/src/common/utils/log"
|
||||||
|
)
|
||||||
|
|
||||||
|
var createdUserGroupID int
|
||||||
|
|
||||||
|
func TestMain(m *testing.M) {
|
||||||
|
|
||||||
|
//databases := []string{"mysql", "sqlite"}
|
||||||
|
databases := []string{"mysql"}
|
||||||
|
for _, database := range databases {
|
||||||
|
log.Infof("run test cases for database: %s", database)
|
||||||
|
|
||||||
|
result := 1
|
||||||
|
switch database {
|
||||||
|
case "mysql":
|
||||||
|
dao.PrepareTestForMySQL()
|
||||||
|
case "sqlite":
|
||||||
|
dao.PrepareTestForSQLite()
|
||||||
|
default:
|
||||||
|
log.Fatalf("invalid database: %s", database)
|
||||||
|
}
|
||||||
|
|
||||||
|
//Extract to test utils
|
||||||
|
initSqls := []string{
|
||||||
|
"insert into 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, 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 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 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)",
|
||||||
|
}
|
||||||
|
|
||||||
|
clearSqls := []string{
|
||||||
|
"delete from project where name='member_test_01'",
|
||||||
|
"delete from user where username='member_test_01' or username='pm_sample'",
|
||||||
|
"delete from user_group",
|
||||||
|
"delete from project_member",
|
||||||
|
}
|
||||||
|
dao.PrepareTestData(clearSqls, initSqls)
|
||||||
|
|
||||||
|
result = m.Run()
|
||||||
|
|
||||||
|
if result != 0 {
|
||||||
|
os.Exit(result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAddUserGroup(t *testing.T) {
|
||||||
|
type args struct {
|
||||||
|
userGroup models.UserGroup
|
||||||
|
}
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
args args
|
||||||
|
want int
|
||||||
|
wantErr bool
|
||||||
|
}{
|
||||||
|
{"Insert an ldap user group", args{userGroup: models.UserGroup{GroupName: "sample_group", GroupType: 1, LdapGroupDN: "sample_ldap_dn_string"}}, 0, false},
|
||||||
|
{"Insert other user group", args{userGroup: models.UserGroup{GroupName: "other_group", GroupType: 3, LdapGroupDN: "other information"}}, 0, false},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
got, err := AddUserGroup(tt.args.userGroup)
|
||||||
|
if (err != nil) != tt.wantErr {
|
||||||
|
t.Errorf("AddUserGroup() error = %v, wantErr %v", err, tt.wantErr)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if got <= 0 {
|
||||||
|
t.Errorf("Failed to add user group")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestQueryUserGroup(t *testing.T) {
|
||||||
|
type args struct {
|
||||||
|
query models.UserGroup
|
||||||
|
}
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
args args
|
||||||
|
want int
|
||||||
|
wantErr bool
|
||||||
|
}{
|
||||||
|
{"Query all user group", args{query: models.UserGroup{GroupName: "test_group_01"}}, 1, false},
|
||||||
|
{"Query all ldap group", args{query: models.UserGroup{GroupType: 1}}, 2, false},
|
||||||
|
{"Query ldap group with group property", args{query: models.UserGroup{GroupType: 1, LdapGroupDN: "CN=harbor_users,OU=sample,OU=vmware,DC=harbor,DC=com"}}, 1, false},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
got, err := QueryUserGroup(tt.args.query)
|
||||||
|
if (err != nil) != tt.wantErr {
|
||||||
|
t.Errorf("QueryUserGroup() error = %v, wantErr %v", err, tt.wantErr)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if len(got) != tt.want {
|
||||||
|
t.Errorf("QueryUserGroup() = %v, want %v", len(got), tt.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetUserGroup(t *testing.T) {
|
||||||
|
userGroup := models.UserGroup{GroupName: "insert_group", GroupType: 1, LdapGroupDN: "ldap_dn_string"}
|
||||||
|
result, err := AddUserGroup(userGroup)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Error occurred when AddUserGroup: %v", err)
|
||||||
|
}
|
||||||
|
createdUserGroupID = result
|
||||||
|
type args struct {
|
||||||
|
id int
|
||||||
|
}
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
args args
|
||||||
|
want string
|
||||||
|
wantErr bool
|
||||||
|
}{
|
||||||
|
{"Get User Group", args{id: result}, "insert_group", false},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
got, err := GetUserGroup(tt.args.id)
|
||||||
|
if (err != nil) != tt.wantErr {
|
||||||
|
t.Errorf("GetUserGroup() error = %v, wantErr %v", err, tt.wantErr)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if got.GroupName != tt.want {
|
||||||
|
t.Errorf("GetUserGroup() = %v, want %v", got.GroupName, tt.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func TestUpdateUserGroup(t *testing.T) {
|
||||||
|
if createdUserGroupID == 0 {
|
||||||
|
fmt.Println("User group doesn't created, skip to test!")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
type args struct {
|
||||||
|
id int
|
||||||
|
groupName string
|
||||||
|
}
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
args args
|
||||||
|
wantErr bool
|
||||||
|
}{
|
||||||
|
{"Update user group", args{id: createdUserGroupID, groupName: "updated_groupname"}, false},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
fmt.Printf("id=%v", createdUserGroupID)
|
||||||
|
if err := UpdateUserGroupName(tt.args.id, tt.args.groupName); (err != nil) != tt.wantErr {
|
||||||
|
t.Errorf("UpdateUserGroup() error = %v, wantErr %v", err, tt.wantErr)
|
||||||
|
userGroup, err := GetUserGroup(tt.args.id)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Error occurred when GetUserGroup: %v", err)
|
||||||
|
}
|
||||||
|
if userGroup == nil {
|
||||||
|
t.Fatalf("Failed to get updated user group")
|
||||||
|
}
|
||||||
|
if userGroup.GroupName != tt.args.groupName {
|
||||||
|
t.Fatalf("Failed to update user group")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDeleteUserGroup(t *testing.T) {
|
||||||
|
if createdUserGroupID == 0 {
|
||||||
|
fmt.Println("User group doesn't created, skip to test!")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
type args struct {
|
||||||
|
id int
|
||||||
|
}
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
args args
|
||||||
|
wantErr bool
|
||||||
|
}{
|
||||||
|
{"Delete existing user group", args{id: createdUserGroupID}, false},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
if err := DeleteUserGroup(tt.args.id); (err != nil) != tt.wantErr {
|
||||||
|
t.Errorf("DeleteUserGroup() error = %v, wantErr %v", err, tt.wantErr)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
118
src/common/dao/project/projectmember.go
Normal file
118
src/common/dao/project/projectmember.go
Normal file
@ -0,0 +1,118 @@
|
|||||||
|
// Copyright (c) 2017 VMware, Inc. All Rights Reserved.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package project
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/vmware/harbor/src/common/dao"
|
||||||
|
"github.com/vmware/harbor/src/common/models"
|
||||||
|
"github.com/vmware/harbor/src/common/utils/log"
|
||||||
|
)
|
||||||
|
|
||||||
|
// GetProjectMember gets all members of the project.
|
||||||
|
func GetProjectMember(queryMember models.Member) ([]*models.Member, error) {
|
||||||
|
log.Debugf("Query condition %+v", queryMember)
|
||||||
|
if queryMember.ProjectID == 0 {
|
||||||
|
return nil, fmt.Errorf("Failed to query project member, query condition %v", queryMember)
|
||||||
|
}
|
||||||
|
|
||||||
|
o := dao.GetOrmer()
|
||||||
|
sql := ` select a.* from ((select pm.id as id, pm.project_id as project_id, ug.id as entity_id, ug.group_name as entity_name, ug.creation_time, ug.update_time, r.name as rolename,
|
||||||
|
r.role_id as role, pm.entity_type as entity_type from user_group ug join project_member pm
|
||||||
|
on pm.project_id = ? and ug.id = pm.entity_id join role r on pm.role = r.role_id where pm.entity_type = 'g')
|
||||||
|
union
|
||||||
|
(select pm.id as id, pm.project_id as project_id, u.user_id as entity_id, u.username as entity_name, u.creation_time, u.update_time, r.name as rolename,
|
||||||
|
r.role_id as role, pm.entity_type as entity_type from user u join project_member pm
|
||||||
|
on pm.project_id = ? and u.user_id = pm.entity_id
|
||||||
|
join role r on pm.role = r.role_id where u.deleted = 0 and pm.entity_type = 'u')) as a where a.project_id = ? `
|
||||||
|
|
||||||
|
queryParam := make([]interface{}, 1)
|
||||||
|
// used ProjectID already
|
||||||
|
queryParam = append(queryParam, queryMember.ProjectID)
|
||||||
|
queryParam = append(queryParam, queryMember.ProjectID)
|
||||||
|
queryParam = append(queryParam, queryMember.ProjectID)
|
||||||
|
|
||||||
|
if len(queryMember.Entityname) > 0 {
|
||||||
|
sql += " and a.entity_name = ? "
|
||||||
|
queryParam = append(queryParam, queryMember.Entityname)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(queryMember.EntityType) == 1 {
|
||||||
|
sql += " and a.entity_type = ? "
|
||||||
|
queryParam = append(queryParam, queryMember.EntityType)
|
||||||
|
}
|
||||||
|
|
||||||
|
if queryMember.EntityID > 0 {
|
||||||
|
sql += " and a.entity_id = ? "
|
||||||
|
queryParam = append(queryParam, queryMember.EntityID)
|
||||||
|
}
|
||||||
|
if queryMember.ID > 0 {
|
||||||
|
sql += " and a.id = ? "
|
||||||
|
queryParam = append(queryParam, queryMember.ID)
|
||||||
|
}
|
||||||
|
sql += ` order by a.entity_name `
|
||||||
|
members := []*models.Member{}
|
||||||
|
_, err := o.Raw(sql, queryParam).QueryRows(&members)
|
||||||
|
|
||||||
|
return members, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddProjectMember inserts a record to table project_member
|
||||||
|
func AddProjectMember(member models.Member) (int, error) {
|
||||||
|
|
||||||
|
log.Debugf("Adding project member %+v", member)
|
||||||
|
o := dao.GetOrmer()
|
||||||
|
|
||||||
|
if member.EntityID <= 0 {
|
||||||
|
return 0, fmt.Errorf("Invalid entity_id, member: %+v", member)
|
||||||
|
}
|
||||||
|
|
||||||
|
if member.ProjectID <= 0 {
|
||||||
|
return 0, fmt.Errorf("Invalid project_id, member: %+v", member)
|
||||||
|
}
|
||||||
|
|
||||||
|
sql := "insert into project_member (project_id, entity_id , role, entity_type) values (?, ?, ?, ?)"
|
||||||
|
r, err := o.Raw(sql, member.ProjectID, member.EntityID, member.Role, member.EntityType).Exec()
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
pmid, err := r.LastInsertId()
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return int(pmid), err
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateProjectMemberRole updates the record in table project_member, only role can be changed
|
||||||
|
func UpdateProjectMemberRole(pmID int, role int) error {
|
||||||
|
if role <= 0 || role >= 3 {
|
||||||
|
return fmt.Errorf("Failed to update project member, role is not in 0,1,2, role:%v", role)
|
||||||
|
}
|
||||||
|
o := dao.GetOrmer()
|
||||||
|
sql := "update project_member set role = ? where id = ? "
|
||||||
|
_, err := o.Raw(sql, role, pmID).Exec()
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeleteProjectMemberByID - Delete Project Member by ID
|
||||||
|
func DeleteProjectMemberByID(pmid int) error {
|
||||||
|
o := dao.GetOrmer()
|
||||||
|
sql := "delete from project_member where id = ?"
|
||||||
|
if _, err := o.Raw(sql, pmid).Exec(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
253
src/common/dao/project/projectmember_test.go
Normal file
253
src/common/dao/project/projectmember_test.go
Normal file
@ -0,0 +1,253 @@
|
|||||||
|
// Copyright (c) 2017 VMware, Inc. All Rights Reserved.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package project
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/vmware/harbor/src/common"
|
||||||
|
"github.com/vmware/harbor/src/common/dao"
|
||||||
|
"github.com/vmware/harbor/src/common/models"
|
||||||
|
"github.com/vmware/harbor/src/common/utils/log"
|
||||||
|
_ "github.com/vmware/harbor/src/ui/auth/db"
|
||||||
|
_ "github.com/vmware/harbor/src/ui/auth/ldap"
|
||||||
|
cfg "github.com/vmware/harbor/src/ui/config"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestMain(m *testing.M) {
|
||||||
|
|
||||||
|
//databases := []string{"mysql", "sqlite"}
|
||||||
|
databases := []string{"mysql"}
|
||||||
|
for _, database := range databases {
|
||||||
|
log.Infof("run test cases for database: %s", database)
|
||||||
|
|
||||||
|
result := 1
|
||||||
|
switch database {
|
||||||
|
case "mysql":
|
||||||
|
dao.PrepareTestForMySQL()
|
||||||
|
case "sqlite":
|
||||||
|
dao.PrepareTestForSQLite()
|
||||||
|
default:
|
||||||
|
log.Fatalf("invalid database: %s", database)
|
||||||
|
}
|
||||||
|
|
||||||
|
//Extract to test utils
|
||||||
|
initSqls := []string{
|
||||||
|
"insert into 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, 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 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 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)",
|
||||||
|
}
|
||||||
|
|
||||||
|
clearSqls := []string{
|
||||||
|
"delete from project where name='member_test_01'",
|
||||||
|
"delete from user where username='member_test_01' or username='pm_sample'",
|
||||||
|
"delete from user_group",
|
||||||
|
"delete from project_member",
|
||||||
|
}
|
||||||
|
dao.PrepareTestData(clearSqls, initSqls)
|
||||||
|
cfg.Init()
|
||||||
|
result = m.Run()
|
||||||
|
|
||||||
|
if result != 0 {
|
||||||
|
os.Exit(result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDeleteProjectMemberByID(t *testing.T) {
|
||||||
|
currentProject, err := dao.GetProjectByName("member_test_01")
|
||||||
|
|
||||||
|
if currentProject == nil || err != nil {
|
||||||
|
fmt.Println("Failed to load project!")
|
||||||
|
} else {
|
||||||
|
fmt.Printf("Load project %+v", currentProject)
|
||||||
|
}
|
||||||
|
var addMember = models.Member{
|
||||||
|
ProjectID: currentProject.ProjectID,
|
||||||
|
EntityID: 1,
|
||||||
|
EntityType: common.UserMember,
|
||||||
|
Role: models.DEVELOPER,
|
||||||
|
}
|
||||||
|
|
||||||
|
pmid, err := AddProjectMember(addMember)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed to add project member error: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
type args struct {
|
||||||
|
pmid int
|
||||||
|
}
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
args args
|
||||||
|
wantErr bool
|
||||||
|
}{
|
||||||
|
{"Delete created", args{pmid}, false},
|
||||||
|
{"Delete non exist", args{-13}, false},
|
||||||
|
{"Delete non exist", args{13}, false},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
if err := DeleteProjectMemberByID(tt.args.pmid); (err != nil) != tt.wantErr {
|
||||||
|
t.Errorf("DeleteProjectMemberByID() error = %v, wantErr %v", err, tt.wantErr)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
func TestAddProjectMember(t *testing.T) {
|
||||||
|
|
||||||
|
currentProject, err := dao.GetProjectByName("member_test_01")
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Error occurred when GetProjectByName: %v", err)
|
||||||
|
}
|
||||||
|
member := models.Member{
|
||||||
|
ProjectID: currentProject.ProjectID,
|
||||||
|
EntityID: 1,
|
||||||
|
EntityType: common.UserMember,
|
||||||
|
Role: models.PROJECTADMIN,
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Debugf("Current project id %v", currentProject.ProjectID)
|
||||||
|
pmid, err := AddProjectMember(member)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Error occurred in AddProjectMember: %v", err)
|
||||||
|
}
|
||||||
|
if pmid == 0 {
|
||||||
|
t.Errorf("Error add project member, pmid=0")
|
||||||
|
}
|
||||||
|
|
||||||
|
queryMember := models.Member{
|
||||||
|
ProjectID: currentProject.ProjectID,
|
||||||
|
ID: pmid,
|
||||||
|
}
|
||||||
|
|
||||||
|
memberList, err := GetProjectMember(queryMember)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Failed to query project member, %v, error: %v", queryMember, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(memberList) == 0 {
|
||||||
|
t.Errorf("Failed to query project member, %v", queryMember)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func TestUpdateProjectMemberRole(t *testing.T) {
|
||||||
|
currentProject, err := dao.GetProjectByName("member_test_01")
|
||||||
|
user := models.User{
|
||||||
|
Username: "pm_sample",
|
||||||
|
Email: "pm_sample@example.com",
|
||||||
|
Realname: "pm_sample",
|
||||||
|
Password: "1234567d",
|
||||||
|
}
|
||||||
|
o := dao.GetOrmer()
|
||||||
|
userID, err := o.Insert(&user)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Error occurred when add user: %v", err)
|
||||||
|
}
|
||||||
|
member := models.Member{
|
||||||
|
ProjectID: currentProject.ProjectID,
|
||||||
|
EntityID: int(userID),
|
||||||
|
EntityType: common.UserMember,
|
||||||
|
Role: models.PROJECTADMIN,
|
||||||
|
}
|
||||||
|
|
||||||
|
pmid, err := AddProjectMember(member)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Error occurred in UpdateProjectMember: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
UpdateProjectMemberRole(pmid, models.DEVELOPER)
|
||||||
|
|
||||||
|
queryMember := models.Member{
|
||||||
|
ProjectID: currentProject.ProjectID,
|
||||||
|
EntityID: int(userID),
|
||||||
|
EntityType: common.UserMember,
|
||||||
|
}
|
||||||
|
|
||||||
|
memberList, err := GetProjectMember(queryMember)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Error occurred in GetProjectMember: %v", err)
|
||||||
|
}
|
||||||
|
if len(memberList) != 1 {
|
||||||
|
t.Errorf("Error occurred in Failed, size: %d, condition:%+v", len(memberList), queryMember)
|
||||||
|
}
|
||||||
|
memberItem := memberList[0]
|
||||||
|
if memberItem.Role != models.DEVELOPER || memberItem.Entityname != user.Username {
|
||||||
|
t.Errorf("member doesn't match!")
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetProjectMember(t *testing.T) {
|
||||||
|
currentProject, err := dao.GetProjectByName("member_test_01")
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Error occurred when GetProjectByName: %v", err)
|
||||||
|
}
|
||||||
|
var memberList1 = []*models.Member{
|
||||||
|
&models.Member{
|
||||||
|
ID: 346,
|
||||||
|
Entityname: "admin",
|
||||||
|
Rolename: "projectAdmin",
|
||||||
|
Role: 1,
|
||||||
|
EntityID: 1,
|
||||||
|
EntityType: "u"},
|
||||||
|
}
|
||||||
|
var memberList2 = []*models.Member{
|
||||||
|
&models.Member{
|
||||||
|
ID: 398,
|
||||||
|
Entityname: "test_group_01",
|
||||||
|
Rolename: "projectAdmin",
|
||||||
|
Role: 1,
|
||||||
|
EntityType: "g"},
|
||||||
|
}
|
||||||
|
type args struct {
|
||||||
|
queryMember models.Member
|
||||||
|
}
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
args args
|
||||||
|
want []*models.Member
|
||||||
|
wantErr bool
|
||||||
|
}{
|
||||||
|
{"Query default project member", args{models.Member{ProjectID: currentProject.ProjectID, Entityname: "admin"}}, memberList1, false},
|
||||||
|
{"Query default project member group", args{models.Member{ProjectID: currentProject.ProjectID, Entityname: "test_group_01"}}, memberList2, false},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
got, err := GetProjectMember(tt.args.queryMember)
|
||||||
|
if (err != nil) != tt.wantErr {
|
||||||
|
t.Errorf("GetProjectMember() error = %v, wantErr %v", err, tt.wantErr)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if len(got) != 1 {
|
||||||
|
t.Errorf("Error occurred when query project member")
|
||||||
|
}
|
||||||
|
itemGot := got[0]
|
||||||
|
itemWant := tt.want[0]
|
||||||
|
|
||||||
|
if itemGot.Entityname != itemWant.Entityname || itemGot.Role != itemWant.Role || itemGot.EntityType != itemWant.EntityType {
|
||||||
|
t.Errorf("test failed, got:%+v, want:%+v", itemGot, itemWant)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -15,6 +15,7 @@
|
|||||||
package dao
|
package dao
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
@ -103,3 +104,23 @@ func initDatabaseForTest(db *models.Database) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PrepareTestData -- Clean and Create data
|
||||||
|
func PrepareTestData(clearSqls []string, initSqls []string) {
|
||||||
|
o := GetOrmer()
|
||||||
|
|
||||||
|
for _, sql := range clearSqls {
|
||||||
|
fmt.Printf("Exec sql:%v\n", sql)
|
||||||
|
_, err := o.Raw(sql).Exec()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("failed to clear database, sql:%v, error: %v", sql, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, sql := range initSqls {
|
||||||
|
_, err := o.Raw(sql).Exec()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("failed to init database, sql:%v, error: %v", sql, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -34,5 +34,6 @@ func init() {
|
|||||||
new(ProjectMetadata),
|
new(ProjectMetadata),
|
||||||
new(ConfigEntry),
|
new(ConfigEntry),
|
||||||
new(Label),
|
new(Label),
|
||||||
new(ResourceLabel))
|
new(ResourceLabel),
|
||||||
|
new(UserGroup))
|
||||||
}
|
}
|
||||||
|
@ -32,3 +32,11 @@ type UserMember struct {
|
|||||||
Rolename string `json:"role_name"`
|
Rolename string `json:"role_name"`
|
||||||
Role int `json:"role_id"`
|
Role int `json:"role_id"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MemberReq - Create Project Member Request
|
||||||
|
type MemberReq struct {
|
||||||
|
ProjectID int64 `json:"project_id"`
|
||||||
|
Role int `json:"role_id,omitempty"`
|
||||||
|
MemberUser User `json:"member_user,omitempty"`
|
||||||
|
MemberGroup UserGroup `json:"member_group,omitempty"`
|
||||||
|
}
|
||||||
|
31
src/common/models/usergroup.go
Normal file
31
src/common/models/usergroup.go
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
// Copyright (c) 2017 VMware, Inc. All Rights Reserved.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package models
|
||||||
|
|
||||||
|
// UserGroupTable is the name of table in DB that holds the user object
|
||||||
|
const UserGroupTable = "user_group"
|
||||||
|
|
||||||
|
// UserGroup ...
|
||||||
|
type UserGroup struct {
|
||||||
|
ID int `orm:"pk;auto;column(id)" json:"id,omitempty"`
|
||||||
|
GroupName string `orm:"column(group_name)" json:"group_name,omitempty"`
|
||||||
|
GroupType int `orm:"column(group_type)" json:"group_type,omitempty"`
|
||||||
|
LdapGroupDN string `orm:"column(ldap_group_dn)" json:"ldap_group_dn,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// TableName ...
|
||||||
|
func (u *UserGroup) TableName() string {
|
||||||
|
return UserGroupTable
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user