modify code to search logs only relevant to the user

This commit is contained in:
wemeya 2016-05-30 17:11:39 +08:00
parent b29a41034c
commit 53edb50eef
3 changed files with 48 additions and 34 deletions

View File

@ -17,6 +17,7 @@ package api
import (
"net/http"
"strconv"
"github.com/vmware/harbor/dao"
"github.com/vmware/harbor/models"
@ -36,27 +37,31 @@ func (l *LogAPI) Prepare() {
//Get returns the recent logs according to parameters
func (l *LogAPI) Get() {
lines, err := l.GetInt("lines")
startTime := l.GetString("start_time")
endTime := l.GetString("end_time")
var linesNum int
var err error
lines := l.GetString("lines")
if len(lines) == 0 {
linesNum = 0
} else {
linesNum, err = strconv.Atoi(lines)
if err != nil {
log.Errorf("Get parameters error--lines, err: %v", err)
l.CustomAbort(http.StatusBadRequest, "bad request of lines")
}
}
if err != nil {
log.Errorf("Get parameters error--lines, err: %v", err)
l.CustomAbort(http.StatusBadRequest, "bad request of lines")
startTime := l.GetString("start_time")
if len(startTime) == 0 {
startTime = ""
}
if lines <= 0 {
lines = 10
}
if len(startTime) <= 0 {
log.Errorf("Get parameters error--startTime: %s", startTime)
l.CustomAbort(http.StatusBadRequest, "bad request of startTime")
}
if len(endTime) <= 0 {
log.Errorf("Get parameters error--endTime: %s", endTime)
l.CustomAbort(http.StatusBadRequest, "bad request of endTime")
endTime := l.GetString("end_time")
if len(endTime) == 0 {
endTime = ""
}
var logList []models.AccessLog
logList, err = dao.GetRecentLogs(lines, startTime, endTime)
logList, err = dao.GetRecentLogs(l.userID, linesNum, startTime, endTime)
if err != nil {
l.CustomAbort(http.StatusInternalServerError, "Internal error")
return

View File

@ -40,6 +40,7 @@ type projectReq struct {
}
const projectNameMaxLen int = 30
const projectNameMinLen int = 4
// Prepare validates the URL and the user
func (p *ProjectAPI) Prepare() {
@ -286,10 +287,7 @@ func validateProjectReq(req projectReq) error {
if len(pn) == 0 {
return fmt.Errorf("Project name can not be empty")
}
if len(pn) > projectNameMaxLen {
return fmt.Errorf("Project name is too long")
}
if isIllegalLength(req.ProjectName, 4, 30) {
if isIllegalLength(req.ProjectName, projectNameMinLen, projectNameMaxLen) {
return fmt.Errorf("project name is illegal in length. (greater than 4 or less than 30)")
}
if isContainIllegalChar(req.ProjectName, []string{"~", "-", "$", "\\", "[", "]", "{", "}", "(", ")", "&", "^", "%", "*", "<", ">", "\"", "'", "/", "?", "@"}) {

View File

@ -18,8 +18,6 @@ package dao
import (
"strings"
"github.com/astaxie/beego/orm"
"github.com/vmware/harbor/models"
"github.com/vmware/harbor/utils/log"
)
@ -119,19 +117,32 @@ func AccessLog(username, projectName, repoName, repoTag, action string) error {
}
//GetRecentLogs returns recent logs according to parameters
func GetRecentLogs(lines int, startTime, endTime string) ([]models.AccessLog, error) {
func GetRecentLogs(userID, linesNum int, startTime, endTime string) ([]models.AccessLog, error) {
var recentLogList []models.AccessLog
queryParam := make([]interface{}, 1)
queryParam = append(queryParam, startTime)
queryParam = append(queryParam, endTime)
qb, _ := orm.NewQueryBuilder("mysql")
qb.Select("log_id",
"user_id", "project_id", "repo_name", "repo_tag", "GUID", "operation", "op_time").
From("access_log").
Where("op_time BETWEEN ? AND ? ").
OrderBy("op_time").Desc().
Limit(lines)
sql := qb.String()
sql := "select user_id, project_id, repo_name, repo_tag, GUID, operation, op_time from access_log where user_id = ?"
queryParam = append(queryParam, userID)
if startTime != "" || len(startTime) > 0 {
sql += " and op_time >= ?"
queryParam = append(queryParam, startTime)
}
if endTime != "" || len(endTime) > 0 {
sql += " and op_time <= ?"
queryParam = append(queryParam, endTime)
}
sql += " order by op_time desc"
if linesNum > 0 {
sql += " limit ?"
queryParam = append(queryParam, linesNum)
} else if startTime == "" || endTime == "" {
linesNum = 10
sql += " limit ?"
queryParam = append(queryParam, linesNum)
}
o := GetOrmer()
_, err := o.Raw(sql, queryParam).QueryRows(&recentLogList)
if err != nil {