From 0aade052229a5b8b619bd7cb2a4909cc643ae7c1 Mon Sep 17 00:00:00 2001 From: wemeya Date: Tue, 31 May 2016 17:38:51 +0800 Subject: [PATCH] change code according to comment --- api/log.go | 38 +++++++++++++++++++++++++++----------- api/project.go | 6 ++++++ dao/accesslog.go | 13 ++++--------- 3 files changed, 37 insertions(+), 20 deletions(-) diff --git a/api/log.go b/api/log.go index bcee419c2..3ea85cd8e 100644 --- a/api/log.go +++ b/api/log.go @@ -18,6 +18,7 @@ package api import ( "net/http" "strconv" + "time" "github.com/vmware/harbor/dao" "github.com/vmware/harbor/models" @@ -37,11 +38,36 @@ func (l *LogAPI) Prepare() { //Get returns the recent logs according to parameters func (l *LogAPI) Get() { - var linesNum int var err error + startTime := l.GetString("start_time") + if len(startTime) == 0 { + startTime = "" + } else { + i, err := strconv.ParseInt(startTime, 10, 64) + if err != nil { + l.CustomAbort(http.StatusInternalServerError, "Internal error") + } + startTime = time.Unix(i, 0).String() + } + + endTime := l.GetString("end_time") + if len(endTime) == 0 { + endTime = "" + } else { + j, err := strconv.ParseInt(endTime, 10, 64) + if err != nil { + l.CustomAbort(http.StatusInternalServerError, "Internal error") + } + endTime = time.Unix(j, 0).String() + } + + var linesNum int lines := l.GetString("lines") if len(lines) == 0 { linesNum = 0 + if startTime == "" || endTime == "" { + linesNum = 10 + } } else { linesNum, err = strconv.Atoi(lines) if err != nil { @@ -50,16 +76,6 @@ func (l *LogAPI) Get() { } } - startTime := l.GetString("start_time") - if len(startTime) == 0 { - startTime = "" - } - - endTime := l.GetString("end_time") - if len(endTime) == 0 { - endTime = "" - } - var logList []models.AccessLog logList, err = dao.GetRecentLogs(l.userID, linesNum, startTime, endTime) if err != nil { diff --git a/api/project.go b/api/project.go index 8eb84f476..84cde81bc 100644 --- a/api/project.go +++ b/api/project.go @@ -18,6 +18,7 @@ package api import ( "fmt" "net/http" + "unicode" "github.com/vmware/harbor/dao" "github.com/vmware/harbor/models" @@ -293,5 +294,10 @@ func validateProjectReq(req projectReq) error { if isContainIllegalChar(req.ProjectName, []string{"~", "-", "$", "\\", "[", "]", "{", "}", "(", ")", "&", "^", "%", "*", "<", ">", "\"", "'", "/", "?", "@"}) { return fmt.Errorf("project name contains illegal characters") } + for _, v := range pn { + if !unicode.IsLower(v) { + return fmt.Errorf("project name must be in lower case") + } + } return nil } diff --git a/dao/accesslog.go b/dao/accesslog.go index 677335d80..56f591eb8 100644 --- a/dao/accesslog.go +++ b/dao/accesslog.go @@ -121,28 +121,23 @@ func GetRecentLogs(userID, linesNum int, startTime, endTime string) ([]models.Ac var recentLogList []models.AccessLog queryParam := make([]interface{}, 1) - sql := "select user_id, project_id, repo_name, repo_tag, GUID, operation, op_time from access_log where user_id = ?" + sql := "select log_id, access_log.user_id, project_id, repo_name, repo_tag, GUID, operation, op_time, username from access_log left join user on access_log.user_id=user.user_id where project_id in (select distinct project_id from access_log where user_id = ?)" queryParam = append(queryParam, userID) - if startTime != "" || len(startTime) > 0 { + if startTime != "" { sql += " and op_time >= ?" queryParam = append(queryParam, startTime) } - if endTime != "" || len(endTime) > 0 { + if endTime != "" { 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 + if linesNum != 0 { sql += " limit ?" queryParam = append(queryParam, linesNum) } - o := GetOrmer() _, err := o.Raw(sql, queryParam).QueryRows(&recentLogList) if err != nil {