mirror of
https://github.com/goharbor/harbor.git
synced 2024-11-26 04:05:40 +01:00
Use new logger for api package
This commit is contained in:
parent
51f7998d97
commit
474ae23cdb
@ -22,7 +22,7 @@ import (
|
||||
"github.com/vmware/harbor/auth"
|
||||
"github.com/vmware/harbor/dao"
|
||||
"github.com/vmware/harbor/models"
|
||||
log "github.com/vmware/harbor/utils/log"
|
||||
"github.com/vmware/harbor/utils/log"
|
||||
|
||||
"github.com/astaxie/beego"
|
||||
)
|
||||
|
@ -21,6 +21,7 @@ import (
|
||||
|
||||
"github.com/vmware/harbor/dao"
|
||||
"github.com/vmware/harbor/models"
|
||||
"github.com/vmware/harbor/utils/log"
|
||||
|
||||
"github.com/astaxie/beego"
|
||||
)
|
||||
@ -43,18 +44,18 @@ type memberReq struct {
|
||||
func (pma *ProjectMemberAPI) Prepare() {
|
||||
pid, err := strconv.ParseInt(pma.Ctx.Input.Param(":pid"), 10, 64)
|
||||
if err != nil {
|
||||
beego.Error("Error parsing project id:", pid, ", error:", err)
|
||||
log.Errorf("Error parsing project id: %d, error: %v", pid, err)
|
||||
pma.CustomAbort(http.StatusBadRequest, "invalid project Id")
|
||||
return
|
||||
}
|
||||
p, err := dao.GetProjectByID(pid)
|
||||
if err != nil {
|
||||
beego.Error("Error occurred in GetProjectById:", err)
|
||||
log.Errorf("Error occurred in GetProjectById, error: %v", err)
|
||||
pma.CustomAbort(http.StatusInternalServerError, "Internal error.")
|
||||
}
|
||||
|
||||
if p == nil {
|
||||
beego.Warning("Project with id:", pid, "does not exist.")
|
||||
log.Warningf("Project with id: %d does not exist.", pid)
|
||||
pma.CustomAbort(http.StatusNotFound, "Project does not exist")
|
||||
}
|
||||
pma.project = p
|
||||
@ -67,7 +68,7 @@ func (pma *ProjectMemberAPI) Prepare() {
|
||||
} else if len(mid) > 0 {
|
||||
memberID, err := strconv.Atoi(mid)
|
||||
if err != nil {
|
||||
beego.Error("Invalid member Id, error:", err)
|
||||
log.Errorf("Invalid member Id, error: %v", err)
|
||||
pma.CustomAbort(http.StatusBadRequest, "Invalid member id")
|
||||
}
|
||||
pma.memberID = memberID
|
||||
@ -78,7 +79,7 @@ func (pma *ProjectMemberAPI) Prepare() {
|
||||
func (pma *ProjectMemberAPI) Get() {
|
||||
pid := pma.project.ProjectID
|
||||
if !checkProjectPermission(pma.currentUserID, pid) {
|
||||
beego.Warning("Current user, user id :", pma.currentUserID, "does not have permission for project, id:", pid)
|
||||
log.Warningf("Current user, user id: %d does not have permission for project, id: %d", pma.currentUserID, pid)
|
||||
pma.RenderError(http.StatusForbidden, "")
|
||||
return
|
||||
}
|
||||
@ -87,7 +88,7 @@ func (pma *ProjectMemberAPI) Get() {
|
||||
queryUser := models.User{Username: "%" + username + "%"}
|
||||
userList, err := dao.GetUserByProject(pid, queryUser)
|
||||
if err != nil {
|
||||
beego.Error("Failed to query database for member list, error:", err)
|
||||
log.Errorf("Failed to query database for member list, error: %v", err)
|
||||
pma.RenderError(http.StatusInternalServerError, "Internal Server Error")
|
||||
return
|
||||
}
|
||||
@ -95,14 +96,14 @@ func (pma *ProjectMemberAPI) Get() {
|
||||
} else { //return detail of a member
|
||||
roleList, err := dao.GetUserProjectRoles(models.User{UserID: pma.memberID}, pid)
|
||||
if err != nil {
|
||||
beego.Error("Error occurred in GetUserProjectRoles:", err)
|
||||
log.Errorf("Error occurred in GetUserProjectRoles, error: %v", err)
|
||||
pma.CustomAbort(http.StatusInternalServerError, "Internal error.")
|
||||
}
|
||||
//return empty role list to indicate if a user is not a member
|
||||
result := make(map[string]interface{})
|
||||
user, err := dao.GetUser(models.User{UserID: pma.memberID})
|
||||
if err != nil {
|
||||
beego.Error("Error occurred in GetUser:", err)
|
||||
log.Errorf("Error occurred in GetUser, error: %v", err)
|
||||
pma.CustomAbort(http.StatusInternalServerError, "Internal error.")
|
||||
}
|
||||
result["user_name"] = user.Username
|
||||
@ -119,11 +120,11 @@ func (pma *ProjectMemberAPI) Post() {
|
||||
userQuery := models.User{UserID: pma.currentUserID, RoleID: models.PROJECTADMIN}
|
||||
rolelist, err := dao.GetUserProjectRoles(userQuery, pid)
|
||||
if err != nil {
|
||||
beego.Error("Error occurred in GetUserProjectRoles:", err)
|
||||
log.Errorf("Error occurred in GetUserProjectRoles, error: %v", err)
|
||||
pma.CustomAbort(http.StatusInternalServerError, "Internal error.")
|
||||
}
|
||||
if len(rolelist) == 0 {
|
||||
beego.Warning("Current user, id:", pma.currentUserID, "does not have project admin role for project, id:", pid)
|
||||
log.Warningf("Current user, id: %d does not have project admin role for project, id: %d", pma.currentUserID, pid)
|
||||
pma.RenderError(http.StatusForbidden, "")
|
||||
return
|
||||
}
|
||||
@ -132,17 +133,17 @@ func (pma *ProjectMemberAPI) Post() {
|
||||
username := req.Username
|
||||
userID := checkUserExists(username)
|
||||
if userID <= 0 {
|
||||
beego.Warning("User does not exist, user name:", username)
|
||||
log.Warningf("User does not exist, user name: %s", username)
|
||||
pma.RenderError(http.StatusNotFound, "User does not exist")
|
||||
return
|
||||
}
|
||||
rolelist, err = dao.GetUserProjectRoles(models.User{UserID: userID}, pid)
|
||||
if err != nil {
|
||||
beego.Error("Error occurred in GetUserProjectRoles:", err)
|
||||
log.Errorf("Error occurred in GetUserProjectRoles, error: %v", err)
|
||||
pma.CustomAbort(http.StatusInternalServerError, "Internal error.")
|
||||
}
|
||||
if len(rolelist) > 0 {
|
||||
beego.Warning("user is already added to project, user id:", userID, ", project id:", pid)
|
||||
log.Warningf("user is already added to project, user id: %d, project id: %d", userID, pid)
|
||||
pma.RenderError(http.StatusConflict, "user is ready in project")
|
||||
return
|
||||
}
|
||||
@ -150,7 +151,7 @@ func (pma *ProjectMemberAPI) Post() {
|
||||
for _, rid := range req.Roles {
|
||||
err = dao.AddUserProjectRole(userID, pid, int(rid))
|
||||
if err != nil {
|
||||
beego.Error("Failed to update DB to add project user role, project id:", pid, ", user id:", userID, ", role id:", rid)
|
||||
log.Errorf("Failed to update DB to add project user role, project id: %d, user id: %d, role id: %d", pid, userID, rid)
|
||||
pma.RenderError(http.StatusInternalServerError, "Failed to update data in database")
|
||||
return
|
||||
}
|
||||
@ -164,11 +165,11 @@ func (pma *ProjectMemberAPI) Put() {
|
||||
userQuery := models.User{UserID: pma.currentUserID, RoleID: models.PROJECTADMIN}
|
||||
rolelist, err := dao.GetUserProjectRoles(userQuery, pid)
|
||||
if err != nil {
|
||||
beego.Error("Error occurred in GetUserProjectRoles:", err)
|
||||
log.Errorf("Error occurred in GetUserProjectRoles, error: %v", err)
|
||||
pma.CustomAbort(http.StatusInternalServerError, "Internal error.")
|
||||
}
|
||||
if len(rolelist) == 0 {
|
||||
beego.Warning("Current user, id:", pma.currentUserID, ", does not have project admin role for project, id:", pid)
|
||||
log.Warningf("Current user, id: %d does not have project admin role for project, id: %d", pma.currentUserID, pid)
|
||||
pma.RenderError(http.StatusForbidden, "")
|
||||
return
|
||||
}
|
||||
@ -176,7 +177,7 @@ func (pma *ProjectMemberAPI) Put() {
|
||||
pma.DecodeJSONReq(&req)
|
||||
roleList, err := dao.GetUserProjectRoles(models.User{UserID: mid}, pid)
|
||||
if len(roleList) == 0 {
|
||||
beego.Warning("User is not in project, user id:", mid, ", project id:", pid)
|
||||
log.Warningf("User is not in project, user id: %d, project id: %d", mid, pid)
|
||||
pma.RenderError(http.StatusNotFound, "user not exist in project")
|
||||
return
|
||||
}
|
||||
@ -184,7 +185,7 @@ func (pma *ProjectMemberAPI) Put() {
|
||||
//delete user project role record for the given user
|
||||
err = dao.DeleteUserProjectRoles(mid, pid)
|
||||
if err != nil {
|
||||
beego.Error("Failed to delete project roles for user, user id:", mid, ", project id: ", pid, ", error: ", err)
|
||||
log.Errorf("Failed to delete project roles for user, user id: %d, project id: %d, error: %v", mid, pid, err)
|
||||
pma.RenderError(http.StatusInternalServerError, "Failed to update data in DB")
|
||||
return
|
||||
}
|
||||
@ -192,7 +193,7 @@ func (pma *ProjectMemberAPI) Put() {
|
||||
for _, rid := range req.Roles {
|
||||
err = dao.AddUserProjectRole(mid, pid, int(rid))
|
||||
if err != nil {
|
||||
beego.Error("Failed to update DB to add project user role, project id:", pid, ", user id:", mid, ", role id:", rid)
|
||||
log.Errorf("Failed to update DB to add project user role, project id: %d, user id: %d, role id: %d", pid, mid, rid)
|
||||
pma.RenderError(http.StatusInternalServerError, "Failed to update data in database")
|
||||
return
|
||||
}
|
||||
@ -206,13 +207,13 @@ func (pma *ProjectMemberAPI) Delete() {
|
||||
userQuery := models.User{UserID: pma.currentUserID, RoleID: models.PROJECTADMIN}
|
||||
rolelist, err := dao.GetUserProjectRoles(userQuery, pid)
|
||||
if len(rolelist) == 0 {
|
||||
beego.Warning("Current user, id:", pma.currentUserID, ", does not have project admin role for project, id:", pid)
|
||||
log.Warningf("Current user, id: %d does not have project admin role for project, id: %d", pma.currentUserID, pid)
|
||||
pma.RenderError(http.StatusForbidden, "")
|
||||
return
|
||||
}
|
||||
err = dao.DeleteUserProjectRoles(mid, pid)
|
||||
if err != nil {
|
||||
beego.Error("Failed to delete project roles for user, user id:", mid, ", project id:", pid, ", error:", err)
|
||||
log.Errorf("Failed to delete project roles for user, user id: %d, project id: %d, error: %v", mid, pid, err)
|
||||
pma.RenderError(http.StatusInternalServerError, "Failed to update data in DB")
|
||||
return
|
||||
}
|
||||
|
@ -17,11 +17,11 @@ package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/vmware/harbor/dao"
|
||||
"github.com/vmware/harbor/models"
|
||||
"github.com/vmware/harbor/utils/log"
|
||||
|
||||
"strconv"
|
||||
"time"
|
||||
@ -51,12 +51,12 @@ func (p *ProjectAPI) Prepare() {
|
||||
var err error
|
||||
p.projectID, err = strconv.ParseInt(idStr, 10, 64)
|
||||
if err != nil {
|
||||
log.Printf("Error parsing project id: %s, error: %v", idStr, err)
|
||||
log.Errorf("Error parsing project id: %s, error: %v", idStr, err)
|
||||
p.CustomAbort(http.StatusBadRequest, "invalid project id")
|
||||
}
|
||||
exist, err := dao.ProjectExists(p.projectID)
|
||||
if err != nil {
|
||||
log.Printf("Error occurred in ProjectExists: %v", err)
|
||||
log.Errorf("Error occurred in ProjectExists, error: %v", err)
|
||||
p.CustomAbort(http.StatusInternalServerError, "Internal error.")
|
||||
}
|
||||
if !exist {
|
||||
@ -75,14 +75,14 @@ func (p *ProjectAPI) Post() {
|
||||
}
|
||||
err := validateProjectReq(req)
|
||||
if err != nil {
|
||||
beego.Error("Invalid project request, error: ", err)
|
||||
log.Errorf("Invalid project request, error: %v", err)
|
||||
p.RenderError(http.StatusBadRequest, "Invalid request for creating project")
|
||||
return
|
||||
}
|
||||
projectName := req.ProjectName
|
||||
exist, err := dao.ProjectExists(projectName)
|
||||
if err != nil {
|
||||
beego.Error("Error happened checking project existence in db:", err, ", project name:", projectName)
|
||||
log.Errorf("Error happened checking project existence in db, error: %v, project name: %s", err, projectName)
|
||||
}
|
||||
if exist {
|
||||
p.RenderError(http.StatusConflict, "")
|
||||
@ -91,7 +91,7 @@ func (p *ProjectAPI) Post() {
|
||||
project := models.Project{OwnerID: p.userID, Name: projectName, CreationTime: time.Now(), Public: public}
|
||||
err = dao.AddProject(project)
|
||||
if err != nil {
|
||||
beego.Error("Failed to add project, error: ", err)
|
||||
log.Errorf("Failed to add project, error: %v", err)
|
||||
p.RenderError(http.StatusInternalServerError, "Failed to add project")
|
||||
}
|
||||
}
|
||||
@ -101,7 +101,7 @@ func (p *ProjectAPI) Head() {
|
||||
projectName := p.GetString("project_name")
|
||||
result, err := dao.ProjectExists(projectName)
|
||||
if err != nil {
|
||||
beego.Error("Error while communicating with DB: ", err)
|
||||
log.Errorf("Error while communicating with DB, error: %v", err)
|
||||
p.RenderError(http.StatusInternalServerError, "Error while communicating with DB")
|
||||
return
|
||||
}
|
||||
@ -123,7 +123,7 @@ func (p *ProjectAPI) Get() {
|
||||
|
||||
projectList, err := dao.QueryProject(queryProject)
|
||||
if err != nil {
|
||||
beego.Error("Error occurred in QueryProject:", err)
|
||||
log.Errorf("Error occurred in QueryProject, error: %v", err)
|
||||
p.CustomAbort(http.StatusInternalServerError, "Internal error.")
|
||||
}
|
||||
for i := 0; i < len(projectList); i++ {
|
||||
@ -142,7 +142,7 @@ func (p *ProjectAPI) Put() {
|
||||
|
||||
projectID, err := strconv.ParseInt(p.Ctx.Input.Param(":id"), 10, 64)
|
||||
if err != nil {
|
||||
beego.Error("Error parsing project id:", projectID, ", error: ", err)
|
||||
log.Errorf("Error parsing project id: %d, error: %v", projectID, err)
|
||||
p.RenderError(http.StatusBadRequest, "invalid project id")
|
||||
return
|
||||
}
|
||||
@ -152,13 +152,13 @@ func (p *ProjectAPI) Put() {
|
||||
public = 1
|
||||
}
|
||||
if !isProjectAdmin(p.userID, projectID) {
|
||||
beego.Warning("Current user, id:", p.userID, ", does not have project admin role for project, id:", projectID)
|
||||
log.Warningf("Current user, id: %d does not have project admin role for project, id: %d", p.userID, projectID)
|
||||
p.RenderError(http.StatusForbidden, "")
|
||||
return
|
||||
}
|
||||
err = dao.ToggleProjectPublicity(p.projectID, public)
|
||||
if err != nil {
|
||||
beego.Error("Error while updating project, project id:", projectID, ", error:", err)
|
||||
log.Errorf("Error while updating project, project id: %d, error: %v", projectID, err)
|
||||
p.RenderError(http.StatusInternalServerError, "Failed to update project")
|
||||
}
|
||||
}
|
||||
@ -177,11 +177,11 @@ func (p *ProjectAPI) FilterAccessLog() {
|
||||
|
||||
query := models.AccessLog{ProjectID: p.projectID, Username: "%" + username + "%", Keywords: keywords, BeginTime: beginTime, BeginTimestamp: filter.BeginTimestamp, EndTime: endTime, EndTimestamp: filter.EndTimestamp}
|
||||
|
||||
log.Printf("Query AccessLog: begin: %v, end: %v, keywords: %s", query.BeginTime, query.EndTime, query.Keywords)
|
||||
log.Infof("Query AccessLog: begin: %v, end: %v, keywords: %s", query.BeginTime, query.EndTime, query.Keywords)
|
||||
|
||||
accessLogList, err := dao.GetAccessLogs(query)
|
||||
if err != nil {
|
||||
log.Printf("Error occurred in GetAccessLogs: %v", err)
|
||||
log.Errorf("Error occurred in GetAccessLogs, error: %v", err)
|
||||
p.CustomAbort(http.StatusInternalServerError, "Internal error.")
|
||||
}
|
||||
p.Data["json"] = accessLogList
|
||||
@ -192,7 +192,7 @@ func isProjectAdmin(userID int, pid int64) bool {
|
||||
userQuery := models.User{UserID: userID, RoleID: models.PROJECTADMIN}
|
||||
rolelist, err := dao.GetUserProjectRoles(userQuery, pid)
|
||||
if err != nil {
|
||||
beego.Error("Error occurred in GetUserProjectRoles:", err, ", returning false")
|
||||
log.Errorf("Error occurred in GetUserProjectRoles, returning false, error: %v", err)
|
||||
return false
|
||||
}
|
||||
return len(rolelist) > 0
|
||||
|
@ -25,6 +25,7 @@ import (
|
||||
"github.com/vmware/harbor/dao"
|
||||
"github.com/vmware/harbor/models"
|
||||
svc_utils "github.com/vmware/harbor/service/utils"
|
||||
"github.com/vmware/harbor/utils/log"
|
||||
|
||||
"github.com/astaxie/beego"
|
||||
)
|
||||
@ -49,7 +50,7 @@ func (ra *RepositoryAPI) Prepare() {
|
||||
}
|
||||
username, ok := ra.GetSession("username").(string)
|
||||
if !ok {
|
||||
beego.Warning("failed to get username from session")
|
||||
log.Warning("failed to get username from session")
|
||||
ra.username = ""
|
||||
} else {
|
||||
ra.username = username
|
||||
@ -60,17 +61,17 @@ func (ra *RepositoryAPI) Prepare() {
|
||||
func (ra *RepositoryAPI) Get() {
|
||||
projectID, err0 := ra.GetInt64("project_id")
|
||||
if err0 != nil {
|
||||
beego.Error("Failed to get project id, error:", err0)
|
||||
log.Errorf("Failed to get project id, error: %v", err0)
|
||||
ra.RenderError(http.StatusBadRequest, "Invalid project id")
|
||||
return
|
||||
}
|
||||
p, err := dao.GetProjectByID(projectID)
|
||||
if err != nil {
|
||||
beego.Error("Error occurred in GetProjectById:", err)
|
||||
log.Errorf("Error occurred in GetProjectById, error: %v", err)
|
||||
ra.CustomAbort(http.StatusInternalServerError, "Internal error.")
|
||||
}
|
||||
if p == nil {
|
||||
beego.Warning("Project with Id:", projectID, ", does not exist")
|
||||
log.Warningf("Project with Id: %d does not exist", projectID)
|
||||
ra.RenderError(http.StatusNotFound, "")
|
||||
return
|
||||
}
|
||||
@ -80,7 +81,7 @@ func (ra *RepositoryAPI) Get() {
|
||||
}
|
||||
repoList, err := svc_utils.GetRepoFromCache()
|
||||
if err != nil {
|
||||
beego.Error("Failed to get repo from cache, error:", err)
|
||||
log.Errorf("Failed to get repo from cache, error: %v", err)
|
||||
ra.RenderError(http.StatusInternalServerError, "internal sever error")
|
||||
}
|
||||
projectName := p.Name
|
||||
@ -131,7 +132,7 @@ func (ra *RepositoryAPI) GetTags() {
|
||||
repoName := ra.GetString("repo_name")
|
||||
result, err := svc_utils.RegistryAPIGet(svc_utils.BuildRegistryURL(repoName, "tags", "list"), ra.username)
|
||||
if err != nil {
|
||||
beego.Error("Failed to get repo tags, repo name:", repoName, ", error: ", err)
|
||||
log.Errorf("Failed to get repo tags, repo name: %s, error: %v", repoName, err)
|
||||
ra.RenderError(http.StatusInternalServerError, "Failed to get repo tags")
|
||||
} else {
|
||||
t := tag{}
|
||||
@ -151,14 +152,14 @@ func (ra *RepositoryAPI) GetManifests() {
|
||||
|
||||
result, err := svc_utils.RegistryAPIGet(svc_utils.BuildRegistryURL(repoName, "manifests", tag), ra.username)
|
||||
if err != nil {
|
||||
beego.Error("Failed to get manifests for repo, repo name:", repoName, ", tag:", tag, ", error:", err)
|
||||
log.Errorf("Failed to get manifests for repo, repo name: %s, tag: %s, error: %v", repoName, tag, err)
|
||||
ra.RenderError(http.StatusInternalServerError, "Internal Server Error")
|
||||
return
|
||||
}
|
||||
mani := manifest{}
|
||||
err = json.Unmarshal(result, &mani)
|
||||
if err != nil {
|
||||
beego.Error("Failed to decode json from response for manifests, repo name:", repoName, ", tag:", tag, ", error:", err)
|
||||
log.Errorf("Failed to decode json from response for manifests, repo name: %s, tag: %s, error: %v", repoName, tag, err)
|
||||
ra.RenderError(http.StatusInternalServerError, "Internal Server Error")
|
||||
return
|
||||
}
|
||||
@ -166,7 +167,7 @@ func (ra *RepositoryAPI) GetManifests() {
|
||||
|
||||
err = json.Unmarshal([]byte(v1Compatibility), &item)
|
||||
if err != nil {
|
||||
beego.Error("Failed to decode V1 field for repo, repo name:", repoName, ", tag:", tag, ", error:", err)
|
||||
log.Errorf("Failed to decode V1 field for repo, repo name: %s, tag: %s, error: %v", repoName, tag, err)
|
||||
ra.RenderError(http.StatusInternalServerError, "Internal Server Error")
|
||||
return
|
||||
}
|
||||
|
@ -24,6 +24,7 @@ import (
|
||||
"github.com/vmware/harbor/models"
|
||||
svc_utils "github.com/vmware/harbor/service/utils"
|
||||
"github.com/vmware/harbor/utils"
|
||||
"github.com/vmware/harbor/utils/log"
|
||||
|
||||
"github.com/astaxie/beego"
|
||||
)
|
||||
@ -47,7 +48,7 @@ func (n *SearchAPI) Get() {
|
||||
keyword := n.GetString("q")
|
||||
projects, err := dao.QueryRelevantProjects(userID)
|
||||
if err != nil {
|
||||
beego.Error("Failed to get projects of user id:", userID, ", error:", err)
|
||||
log.Errorf("Failed to get projects of user id: %d, error: %v", userID, err)
|
||||
n.CustomAbort(http.StatusInternalServerError, "Failed to get project search result")
|
||||
}
|
||||
projectSorter := &utils.ProjectSorter{Projects: projects}
|
||||
@ -69,7 +70,7 @@ func (n *SearchAPI) Get() {
|
||||
|
||||
repositories, err2 := svc_utils.GetRepoFromCache()
|
||||
if err2 != nil {
|
||||
beego.Error("Failed to get repos from cache, error :", err2)
|
||||
log.Errorf("Failed to get repos from cache, error: %v", err2)
|
||||
n.CustomAbort(http.StatusInternalServerError, "Failed to get repositories search result")
|
||||
}
|
||||
sort.Strings(repositories)
|
||||
|
27
api/user.go
27
api/user.go
@ -21,6 +21,7 @@ import (
|
||||
|
||||
"github.com/vmware/harbor/dao"
|
||||
"github.com/vmware/harbor/models"
|
||||
"github.com/vmware/harbor/utils/log"
|
||||
|
||||
"github.com/astaxie/beego"
|
||||
)
|
||||
@ -43,17 +44,17 @@ func (ua *UserAPI) Prepare() {
|
||||
var err error
|
||||
ua.userID, err = strconv.Atoi(id)
|
||||
if err != nil {
|
||||
beego.Error("Invalid user id, error:", err)
|
||||
log.Errorf("Invalid user id, error: %v", err)
|
||||
ua.CustomAbort(http.StatusBadRequest, "Invalid user Id")
|
||||
}
|
||||
userQuery := models.User{UserID: ua.userID}
|
||||
u, err := dao.GetUser(userQuery)
|
||||
if err != nil {
|
||||
beego.Error("Error occurred in GetUser:", err)
|
||||
log.Errorf("Error occurred in GetUser, error: %v", err)
|
||||
ua.CustomAbort(http.StatusInternalServerError, "Internal error.")
|
||||
}
|
||||
if u == nil {
|
||||
beego.Error("User with Id:", ua.userID, "does not exist")
|
||||
log.Errorf("User with Id: %d does not exist", ua.userID)
|
||||
ua.CustomAbort(http.StatusNotFound, "")
|
||||
}
|
||||
}
|
||||
@ -63,13 +64,13 @@ func (ua *UserAPI) Prepare() {
|
||||
func (ua *UserAPI) Get() {
|
||||
exist, err := dao.IsAdminRole(ua.currentUserID)
|
||||
if err != nil {
|
||||
beego.Error("Error occurred in IsAdminRole:", err)
|
||||
log.Errorf("Error occurred in IsAdminRole, error: %v", err)
|
||||
ua.CustomAbort(http.StatusInternalServerError, "Internal error.")
|
||||
}
|
||||
|
||||
if ua.userID == 0 { //list users
|
||||
if !exist {
|
||||
beego.Error("Current user, id:", ua.currentUserID, ", does not have admin role, can not list users")
|
||||
log.Errorf("Current user, id: %d does not have admin role, can not list users", ua.currentUserID)
|
||||
ua.RenderError(http.StatusForbidden, "User does not have admin role")
|
||||
return
|
||||
}
|
||||
@ -80,7 +81,7 @@ func (ua *UserAPI) Get() {
|
||||
}
|
||||
userList, err := dao.ListUsers(userQuery)
|
||||
if err != nil {
|
||||
beego.Error("Failed to get data from database, error:", err)
|
||||
log.Errorf("Failed to get data from database, error: %v", err)
|
||||
ua.RenderError(http.StatusInternalServerError, "Failed to query from database")
|
||||
return
|
||||
}
|
||||
@ -90,12 +91,12 @@ func (ua *UserAPI) Get() {
|
||||
userQuery := models.User{UserID: ua.userID}
|
||||
u, err := dao.GetUser(userQuery)
|
||||
if err != nil {
|
||||
beego.Error("Error occurred in GetUser:", err)
|
||||
log.Errorf("Error occurred in GetUser, error: %v", err)
|
||||
ua.CustomAbort(http.StatusInternalServerError, "Internal error.")
|
||||
}
|
||||
ua.Data["json"] = u
|
||||
} else {
|
||||
beego.Error("Current user, id:", ua.currentUserID, "does not have admin role, can not view other user's detail")
|
||||
log.Errorf("Current user, id: %d does not have admin role, can not view other user's detail", ua.currentUserID)
|
||||
ua.RenderError(http.StatusForbidden, "User does not have admin role")
|
||||
return
|
||||
}
|
||||
@ -106,11 +107,11 @@ func (ua *UserAPI) Get() {
|
||||
func (ua *UserAPI) Put() { //currently only for toggle admin, so no request body
|
||||
exist, err := dao.IsAdminRole(ua.currentUserID)
|
||||
if err != nil {
|
||||
beego.Error("Error occurred in IsAdminRole:", err)
|
||||
log.Errorf("Error occurred in IsAdminRole, error: %v", err)
|
||||
ua.CustomAbort(http.StatusInternalServerError, "Internal error.")
|
||||
}
|
||||
if !exist {
|
||||
beego.Warning("current user, id:", ua.currentUserID, ", does not have admin role, can not update other user's role")
|
||||
log.Warningf("current user, id: %d does not have admin role, can not update other user's role", ua.currentUserID)
|
||||
ua.RenderError(http.StatusForbidden, "User does not have admin role")
|
||||
return
|
||||
}
|
||||
@ -122,17 +123,17 @@ func (ua *UserAPI) Put() { //currently only for toggle admin, so no request body
|
||||
func (ua *UserAPI) Delete() {
|
||||
exist, err := dao.IsAdminRole(ua.currentUserID)
|
||||
if err != nil {
|
||||
beego.Error("Error occurred in IsAdminRole:", err)
|
||||
log.Errorf("Error occurred in IsAdminRole, error: %v", err)
|
||||
ua.CustomAbort(http.StatusInternalServerError, "Internal error.")
|
||||
}
|
||||
if !exist {
|
||||
beego.Warning("current user, id:", ua.currentUserID, ", does not have admin role, can not remove user")
|
||||
log.Warningf("current user, id: %d does not have admin role, can not remove user", ua.currentUserID)
|
||||
ua.RenderError(http.StatusForbidden, "User does not have admin role")
|
||||
return
|
||||
}
|
||||
err = dao.DeleteUser(ua.userID)
|
||||
if err != nil {
|
||||
beego.Error("Failed to delete data from database, error:", err)
|
||||
log.Errorf("Failed to delete data from database, error: %v", err)
|
||||
ua.RenderError(http.StatusInternalServerError, "Failed to delete User")
|
||||
return
|
||||
}
|
||||
|
@ -18,6 +18,7 @@ package api
|
||||
import (
|
||||
"github.com/vmware/harbor/dao"
|
||||
"github.com/vmware/harbor/models"
|
||||
"github.com/vmware/harbor/utils/log"
|
||||
|
||||
"github.com/astaxie/beego"
|
||||
)
|
||||
@ -25,7 +26,7 @@ import (
|
||||
func checkProjectPermission(userID int, projectID int64) bool {
|
||||
exist, err := dao.IsAdminRole(userID)
|
||||
if err != nil {
|
||||
beego.Error("Error occurred in IsAdminRole:", err)
|
||||
log.Errorf("Error occurred in IsAdminRole, error: %v", err)
|
||||
return false
|
||||
}
|
||||
if exist {
|
||||
@ -33,7 +34,7 @@ func checkProjectPermission(userID int, projectID int64) bool {
|
||||
}
|
||||
roleList, err := dao.GetUserProjectRoles(models.User{UserID: userID}, projectID)
|
||||
if err != nil {
|
||||
beego.Error("Error occurred in GetUserProjectRoles:", err)
|
||||
log.Errorf("Error occurred in GetUserProjectRoles, error: %v", err)
|
||||
return false
|
||||
}
|
||||
return len(roleList) > 0
|
||||
@ -42,7 +43,7 @@ func checkProjectPermission(userID int, projectID int64) bool {
|
||||
func checkUserExists(name string) int {
|
||||
u, err := dao.GetUser(models.User{Username: name})
|
||||
if err != nil {
|
||||
beego.Error("Error occurred in GetUser:", err)
|
||||
log.Errorf("Error occurred in GetUser, error: %v", err)
|
||||
return 0
|
||||
}
|
||||
if u != nil {
|
||||
|
Loading…
Reference in New Issue
Block a user