mirror of
https://github.com/goharbor/harbor.git
synced 2024-12-27 11:07:46 +01:00
change code according to comment
This commit is contained in:
parent
97b5d16e26
commit
0aade05222
38
api/log.go
38
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 {
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user