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 (
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/vmware/harbor/models"
|
|
|
|
|
|
|
|
"github.com/astaxie/beego/orm"
|
|
|
|
)
|
|
|
|
|
2016-02-26 11:13:13 +01:00
|
|
|
// AddAccessLog persists the access logs
|
2016-02-01 12:59:10 +01:00
|
|
|
func AddAccessLog(accessLog models.AccessLog) error {
|
|
|
|
o := orm.NewOrm()
|
|
|
|
p, err := o.Raw(`insert into access_log
|
|
|
|
(user_id, project_id, repo_name, guid, operation, op_time)
|
|
|
|
values (?, ?, ?, ?, ?, now())`).Prepare()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer p.Close()
|
|
|
|
|
2016-02-26 03:15:01 +01:00
|
|
|
_, err = p.Exec(accessLog.UserID, accessLog.ProjectID, accessLog.RepoName, accessLog.GUID, accessLog.Operation)
|
2016-02-01 12:59:10 +01:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-02-26 11:13:13 +01:00
|
|
|
//GetAccessLogs gets access logs according to different conditions
|
2016-02-01 12:59:10 +01:00
|
|
|
func GetAccessLogs(accessLog models.AccessLog) ([]models.AccessLog, error) {
|
|
|
|
|
|
|
|
o := orm.NewOrm()
|
|
|
|
sql := `select a.log_id, u.username, a.repo_name, a.operation, a.op_time
|
|
|
|
from access_log a left join user u on a.user_id = u.user_id
|
|
|
|
where a.project_id = ? `
|
|
|
|
queryParam := make([]interface{}, 1)
|
2016-02-26 03:15:01 +01:00
|
|
|
queryParam = append(queryParam, accessLog.ProjectID)
|
2016-02-01 12:59:10 +01:00
|
|
|
|
2016-02-26 03:15:01 +01:00
|
|
|
if accessLog.UserID != 0 {
|
2016-02-01 12:59:10 +01:00
|
|
|
sql += ` and a.user_id = ? `
|
2016-02-26 03:15:01 +01:00
|
|
|
queryParam = append(queryParam, accessLog.UserID)
|
2016-02-01 12:59:10 +01:00
|
|
|
}
|
|
|
|
if accessLog.Operation != "" {
|
|
|
|
sql += ` and a.operation = ? `
|
|
|
|
queryParam = append(queryParam, accessLog.Operation)
|
|
|
|
}
|
|
|
|
if accessLog.Username != "" {
|
|
|
|
sql += ` and u.username like ? `
|
|
|
|
queryParam = append(queryParam, accessLog.Username)
|
|
|
|
}
|
|
|
|
if accessLog.Keywords != "" {
|
|
|
|
sql += ` and a.operation in ( `
|
|
|
|
keywordList := strings.Split(accessLog.Keywords, "/")
|
|
|
|
num := len(keywordList)
|
|
|
|
for i := 0; i < num; i++ {
|
|
|
|
if keywordList[i] != "" {
|
|
|
|
if i == num-1 {
|
|
|
|
sql += `?)`
|
|
|
|
} else {
|
|
|
|
sql += `?,`
|
|
|
|
}
|
|
|
|
queryParam = append(queryParam, keywordList[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-02-19 05:34:18 +01:00
|
|
|
if accessLog.BeginTimestamp > 0 {
|
|
|
|
sql += ` and a.op_time >= ? `
|
|
|
|
queryParam = append(queryParam, accessLog.BeginTime)
|
2016-02-01 12:59:10 +01:00
|
|
|
}
|
2016-02-19 05:34:18 +01:00
|
|
|
if accessLog.EndTimestamp > 0 {
|
|
|
|
sql += ` and a.op_time <= ? `
|
|
|
|
queryParam = append(queryParam, accessLog.EndTime)
|
2016-02-01 12:59:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sql += ` order by a.op_time desc `
|
|
|
|
|
|
|
|
var accessLogList []models.AccessLog
|
|
|
|
_, err := o.Raw(sql, queryParam).QueryRows(&accessLogList)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return accessLogList, nil
|
|
|
|
}
|
|
|
|
|
2016-02-26 11:13:13 +01:00
|
|
|
// AccessLog ...
|
2016-02-01 12:59:10 +01:00
|
|
|
func AccessLog(username, projectName, repoName, action string) error {
|
|
|
|
o := orm.NewOrm()
|
|
|
|
sql := "insert into access_log (user_id, project_id, repo_name, operation, op_time) " +
|
|
|
|
"select (select user_id as user_id from user where username=?), " +
|
|
|
|
"(select project_id as project_id from project where name=?), ?, ?, now() "
|
|
|
|
_, err := o.Raw(sql, username, projectName, repoName, action).Exec()
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|