2016-02-01 12:59:10 +01:00
|
|
|
/*
|
|
|
|
Copyright (c) 2016 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.
|
|
|
|
*/
|
2016-02-26 11:54:14 +01:00
|
|
|
|
2016-02-01 12:59:10 +01:00
|
|
|
package dao
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/vmware/harbor/models"
|
|
|
|
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/astaxie/beego/orm"
|
2016-03-29 07:33:49 +02:00
|
|
|
"github.com/vmware/harbor/utils/log"
|
2016-02-01 12:59:10 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
//TODO:transaction, return err
|
2016-02-26 11:13:13 +01:00
|
|
|
|
|
|
|
// AddProject adds a project to the database along with project roles information and access log records.
|
2016-04-22 11:57:39 +02:00
|
|
|
func AddProject(project models.Project) (int64, error) {
|
2016-02-01 12:59:10 +01:00
|
|
|
|
|
|
|
if isIllegalLength(project.Name, 4, 30) {
|
2016-04-22 11:57:39 +02:00
|
|
|
return 0, errors.New("project name is illegal in length. (greater than 4 or less than 30)")
|
2016-02-01 12:59:10 +01:00
|
|
|
}
|
|
|
|
if isContainIllegalChar(project.Name, []string{"~", "-", "$", "\\", "[", "]", "{", "}", "(", ")", "&", "^", "%", "*", "<", ">", "\"", "'", "/", "?", "@"}) {
|
2016-04-22 11:57:39 +02:00
|
|
|
return 0, errors.New("project name contains illegal characters")
|
2016-02-01 12:59:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
o := orm.NewOrm()
|
|
|
|
|
2016-03-28 09:34:41 +02:00
|
|
|
p, err := o.Raw("insert into project (owner_id, name, creation_time, update_time, deleted, public) values (?, ?, ?, ?, ?, ?)").Prepare()
|
2016-02-01 12:59:10 +01:00
|
|
|
if err != nil {
|
2016-04-22 11:57:39 +02:00
|
|
|
return 0, err
|
2016-02-01 12:59:10 +01:00
|
|
|
}
|
|
|
|
|
2016-03-28 09:34:41 +02:00
|
|
|
now := time.Now()
|
|
|
|
r, err := p.Exec(project.OwnerID, project.Name, now, now, project.Deleted, project.Public)
|
2016-02-01 12:59:10 +01:00
|
|
|
if err != nil {
|
2016-04-22 11:57:39 +02:00
|
|
|
return 0, err
|
2016-02-01 12:59:10 +01:00
|
|
|
}
|
|
|
|
|
2016-02-26 04:26:54 +01:00
|
|
|
projectID, err := r.LastInsertId()
|
2016-02-01 12:59:10 +01:00
|
|
|
if err != nil {
|
2016-04-22 11:57:39 +02:00
|
|
|
return 0, err
|
2016-02-01 12:59:10 +01:00
|
|
|
}
|
|
|
|
|
2016-03-29 06:09:27 +02:00
|
|
|
if err = AddProjectMember(projectID, project.OwnerID, models.PROJECTADMIN); err != nil {
|
2016-04-22 11:57:39 +02:00
|
|
|
return projectID, err
|
2016-02-01 12:59:10 +01:00
|
|
|
}
|
|
|
|
|
2016-04-21 18:28:59 +02:00
|
|
|
accessLog := models.AccessLog{UserID: project.OwnerID, ProjectID: projectID, RepoName: project.Name + "/", RepoTag: "N/A", GUID: "N/A", Operation: "create", OpTime: time.Now()}
|
2016-02-01 12:59:10 +01:00
|
|
|
err = AddAccessLog(accessLog)
|
|
|
|
|
2016-04-22 11:57:39 +02:00
|
|
|
return projectID, err
|
2016-02-01 12:59:10 +01:00
|
|
|
}
|
|
|
|
|
2016-02-26 11:13:13 +01:00
|
|
|
// IsProjectPublic ...
|
2016-02-01 12:59:10 +01:00
|
|
|
func IsProjectPublic(projectName string) bool {
|
|
|
|
project, err := GetProjectByName(projectName)
|
|
|
|
if err != nil {
|
2016-03-29 07:33:49 +02:00
|
|
|
log.Errorf("Error occurred in GetProjectByName: %v", err)
|
2016-02-01 12:59:10 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
if project == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return project.Public == 1
|
|
|
|
}
|
|
|
|
|
2016-02-26 11:13:13 +01:00
|
|
|
//ProjectExists returns whether the project exists according to its name of ID.
|
2016-02-26 04:26:54 +01:00
|
|
|
func ProjectExists(nameOrID interface{}) (bool, error) {
|
2016-02-01 12:59:10 +01:00
|
|
|
o := orm.NewOrm()
|
|
|
|
type dummy struct{}
|
|
|
|
sql := `select project_id from project where deleted = 0 and `
|
2016-02-26 04:26:54 +01:00
|
|
|
switch nameOrID.(type) {
|
2016-02-01 12:59:10 +01:00
|
|
|
case int64:
|
|
|
|
sql += `project_id = ?`
|
|
|
|
case string:
|
|
|
|
sql += `name = ?`
|
|
|
|
default:
|
2016-02-26 04:26:54 +01:00
|
|
|
return false, fmt.Errorf("Invalid nameOrId: %v", nameOrID)
|
2016-02-01 12:59:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var d []dummy
|
2016-02-26 04:26:54 +01:00
|
|
|
num, err := o.Raw(sql, nameOrID).QueryRows(&d)
|
2016-02-01 12:59:10 +01:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return num > 0, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-02-26 11:13:13 +01:00
|
|
|
// GetProjectByID ...
|
2016-03-28 09:34:41 +02:00
|
|
|
func GetProjectByID(id int64) (*models.Project, error) {
|
2016-02-01 12:59:10 +01:00
|
|
|
o := orm.NewOrm()
|
|
|
|
|
2016-03-28 09:34:41 +02:00
|
|
|
sql := `select p.project_id, p.name, u.username as owner_name, p.owner_id, p.creation_time, p.update_time, p.public
|
2016-02-01 12:59:10 +01:00
|
|
|
from project p left join user u on p.owner_id = u.user_id where p.deleted = 0 and p.project_id = ?`
|
|
|
|
queryParam := make([]interface{}, 1)
|
2016-03-28 09:34:41 +02:00
|
|
|
queryParam = append(queryParam, id)
|
2016-02-01 12:59:10 +01:00
|
|
|
|
|
|
|
p := []models.Project{}
|
|
|
|
count, err := o.Raw(sql, queryParam).QueryRows(&p)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-03-28 09:34:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if count == 0 {
|
2016-02-01 12:59:10 +01:00
|
|
|
return nil, nil
|
|
|
|
}
|
2016-03-28 09:34:41 +02:00
|
|
|
|
|
|
|
return &p[0], nil
|
2016-02-01 12:59:10 +01:00
|
|
|
}
|
|
|
|
|
2016-02-26 11:13:13 +01:00
|
|
|
// GetProjectByName ...
|
2016-03-28 09:34:41 +02:00
|
|
|
func GetProjectByName(name string) (*models.Project, error) {
|
2016-02-01 12:59:10 +01:00
|
|
|
o := orm.NewOrm()
|
|
|
|
var p []models.Project
|
2016-03-28 09:34:41 +02:00
|
|
|
n, err := o.Raw(`select * from project where name = ? and deleted = 0`, name).QueryRows(&p)
|
2016-02-01 12:59:10 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-03-28 09:34:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if n == 0 {
|
2016-02-01 12:59:10 +01:00
|
|
|
return nil, nil
|
|
|
|
}
|
2016-03-28 09:34:41 +02:00
|
|
|
|
|
|
|
return &p[0], nil
|
2016-02-01 12:59:10 +01:00
|
|
|
}
|
|
|
|
|
2016-02-26 11:13:13 +01:00
|
|
|
// GetPermission gets roles that the user has according to the project.
|
2016-02-01 12:59:10 +01:00
|
|
|
func GetPermission(username, projectName string) (string, error) {
|
|
|
|
o := orm.NewOrm()
|
|
|
|
|
2016-03-28 09:34:41 +02:00
|
|
|
sql := `select r.role_code from role as r
|
|
|
|
inner join project_member as pm on r.role_id = pm.role
|
|
|
|
inner join user as u on u.user_id = pm.user_id
|
|
|
|
inner join project p on p.project_id = pm.project_id
|
|
|
|
where u.username = ? and p.name = ? and u.deleted = 0 and p.deleted = 0`
|
2016-02-01 12:59:10 +01:00
|
|
|
|
|
|
|
var r []models.Role
|
|
|
|
n, err := o.Raw(sql, username, projectName).QueryRows(&r)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2016-03-28 09:34:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if n == 0 {
|
2016-02-01 12:59:10 +01:00
|
|
|
return "", nil
|
|
|
|
}
|
2016-03-28 09:34:41 +02:00
|
|
|
|
|
|
|
return r[0].RoleCode, nil
|
2016-02-01 12:59:10 +01:00
|
|
|
}
|
|
|
|
|
2016-02-26 11:13:13 +01:00
|
|
|
// ToggleProjectPublicity toggles the publicity of the project.
|
2016-02-26 04:26:54 +01:00
|
|
|
func ToggleProjectPublicity(projectID int64, publicity int) error {
|
2016-02-01 12:59:10 +01:00
|
|
|
o := orm.NewOrm()
|
|
|
|
sql := "update project set public = ? where project_id = ?"
|
2016-02-26 04:26:54 +01:00
|
|
|
_, err := o.Raw(sql, publicity, projectID).Exec()
|
2016-02-01 12:59:10 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-05-16 11:31:38 +02:00
|
|
|
// SearchProjects returns a project list,
|
2016-05-09 10:48:59 +02:00
|
|
|
// which satisfies the following conditions:
|
|
|
|
// 1. the project is not deleted
|
|
|
|
// 2. the prject is public or the user is a member of the project
|
2016-05-16 11:31:38 +02:00
|
|
|
func SearchProjects(userID int) ([]models.Project, error) {
|
2016-02-01 12:59:10 +01:00
|
|
|
o := orm.NewOrm()
|
2016-03-28 09:34:41 +02:00
|
|
|
sql := `select distinct p.project_id, p.name, p.public
|
|
|
|
from project p
|
|
|
|
left join project_member pm on p.project_id = pm.project_id
|
2016-05-16 12:02:39 +02:00
|
|
|
where (pm.user_id = ? or p.public = 1) and p.deleted = 0`
|
2016-05-09 10:48:59 +02:00
|
|
|
|
|
|
|
var projects []models.Project
|
|
|
|
|
|
|
|
if _, err := o.Raw(sql, userID).QueryRows(&projects); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return projects, nil
|
|
|
|
}
|
|
|
|
|
2016-05-18 04:30:55 +02:00
|
|
|
// GetUserRelevantProjects returns the projects of the user which are not deleted and name like projectName
|
2016-05-17 11:10:31 +02:00
|
|
|
func GetUserRelevantProjects(userID int, projectName string) ([]models.Project, error) {
|
2016-05-16 11:31:38 +02:00
|
|
|
o := orm.NewOrm()
|
|
|
|
sql := `select distinct
|
|
|
|
p.project_id, p.owner_id, p.name,p.creation_time, p.update_time, p.public, pm.role role
|
|
|
|
from project p
|
|
|
|
left join project_member pm on p.project_id = pm.project_id
|
|
|
|
where p.deleted = 0 and pm.user_id= ?`
|
|
|
|
|
|
|
|
queryParam := make([]interface{}, 1)
|
2016-05-17 11:10:31 +02:00
|
|
|
queryParam = append(queryParam, userID)
|
2016-05-17 11:03:40 +02:00
|
|
|
if projectName != "" {
|
2016-05-16 11:31:38 +02:00
|
|
|
sql += " and p.name like ? "
|
2016-05-17 11:03:40 +02:00
|
|
|
queryParam = append(queryParam, projectName)
|
2016-05-16 11:31:38 +02:00
|
|
|
}
|
|
|
|
sql += " order by p.name "
|
|
|
|
var r []models.Project
|
|
|
|
_, err := o.Raw(sql, queryParam).QueryRows(&r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
2016-05-18 04:30:55 +02:00
|
|
|
//GetPublicProjects returns all public projects whose name like projectName
|
2016-05-17 11:03:40 +02:00
|
|
|
func GetPublicProjects(projectName string) ([]models.Project, error) {
|
2016-05-18 11:48:05 +02:00
|
|
|
publicProjects, err := getProjects(1, projectName)
|
2016-05-17 11:03:40 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return publicProjects, nil
|
|
|
|
}
|
|
|
|
|
2016-05-18 04:30:55 +02:00
|
|
|
// GetAllProjects returns all projects which are not deleted and name like projectName
|
2016-05-17 11:03:40 +02:00
|
|
|
func GetAllProjects(projectName string) ([]models.Project, error) {
|
2016-05-18 11:48:05 +02:00
|
|
|
allProjects, err := getProjects(0, projectName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return allProjects, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getProjects(public int, projectName string) ([]models.Project, error) {
|
2016-05-09 10:48:59 +02:00
|
|
|
o := orm.NewOrm()
|
2016-05-18 04:30:55 +02:00
|
|
|
sql := `select project_id, owner_id, creation_time, update_time, name, public
|
2016-05-09 10:48:59 +02:00
|
|
|
from project
|
|
|
|
where deleted = 0`
|
2016-05-18 08:36:20 +02:00
|
|
|
queryParam := make([]interface{}, 1)
|
2016-05-18 11:48:05 +02:00
|
|
|
if public == 1 {
|
2016-05-19 08:40:09 +02:00
|
|
|
sql += " and public = ? "
|
2016-05-18 11:48:05 +02:00
|
|
|
queryParam = append(queryParam, public)
|
|
|
|
}
|
2016-05-17 11:03:40 +02:00
|
|
|
if len(projectName) > 0 {
|
2016-05-18 08:36:20 +02:00
|
|
|
sql += " and name like ? "
|
|
|
|
queryParam = append(queryParam, projectName)
|
2016-05-17 11:03:40 +02:00
|
|
|
}
|
|
|
|
sql += " order by name "
|
2016-05-09 10:48:59 +02:00
|
|
|
var projects []models.Project
|
2016-05-18 08:36:20 +02:00
|
|
|
if _, err := o.Raw(sql, queryParam).QueryRows(&projects); err != nil {
|
2016-02-01 12:59:10 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
2016-05-09 10:48:59 +02:00
|
|
|
return projects, nil
|
2016-02-01 12:59:10 +01:00
|
|
|
}
|