mirror of
https://github.com/goharbor/harbor.git
synced 2025-01-02 22:18:29 +01:00
refactored codes in item_detail controller
This commit is contained in:
parent
273aea3c6d
commit
67b4415717
@ -28,47 +28,12 @@ type ItemDetailController struct {
|
|||||||
BaseController
|
BaseController
|
||||||
}
|
}
|
||||||
|
|
||||||
var SYS_ADMIN int = 1
|
|
||||||
var PROJECT_ADMIN int = 2
|
|
||||||
var DEVELOPER int = 3
|
|
||||||
var GUEST int = 4
|
|
||||||
|
|
||||||
func CheckProjectRole(userId int, projectId int64) bool {
|
|
||||||
if projectId == 0 {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
userQuery := models.User{UserId: int(userId)}
|
|
||||||
if userId == SYS_ADMIN {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
roleList, err := dao.GetUserProjectRoles(userQuery, projectId)
|
|
||||||
if err != nil {
|
|
||||||
beego.Error("Error occurred in GetUserProjectRoles:", err)
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return len(roleList) > 0
|
|
||||||
}
|
|
||||||
|
|
||||||
func CheckPublicProject(projectId int64) bool {
|
|
||||||
projectQuery := models.Project{ProjectId: projectId}
|
|
||||||
project, err := dao.GetProjectById(projectQuery)
|
|
||||||
if err != nil {
|
|
||||||
beego.Error("Error occurred in GetProjectById:", err)
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
if project != nil && project.Public == 1 {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func (idc *ItemDetailController) Get() {
|
func (idc *ItemDetailController) Get() {
|
||||||
|
|
||||||
sessionUserId := idc.GetSession("userId")
|
|
||||||
projectId, _ := idc.GetInt64("project_id")
|
projectId, _ := idc.GetInt64("project_id")
|
||||||
|
if projectId <= 0 {
|
||||||
if CheckPublicProject(projectId) == false && (sessionUserId == nil || !CheckProjectRole(sessionUserId.(int), projectId)) {
|
beego.Error("Invalid project id:", projectId)
|
||||||
idc.Redirect("/signIn?uri="+url.QueryEscape(idc.Ctx.Input.URI()), 302)
|
idc.Redirect("/signIn", 302)
|
||||||
}
|
}
|
||||||
|
|
||||||
projectQuery := models.Project{ProjectId: projectId}
|
projectQuery := models.Project{ProjectId: projectId}
|
||||||
@ -83,24 +48,35 @@ func (idc *ItemDetailController) Get() {
|
|||||||
idc.Redirect("/signIn", 302)
|
idc.Redirect("/signIn", 302)
|
||||||
}
|
}
|
||||||
|
|
||||||
idc.Data["ProjectId"] = project.ProjectId
|
sessionUserId := idc.GetSession("userId")
|
||||||
idc.Data["ProjectName"] = project.Name
|
|
||||||
idc.Data["OwnerName"] = project.OwnerName
|
if project.Public != 1 && sessionUserId == nil {
|
||||||
idc.Data["OwnerId"] = project.OwnerId
|
idc.Redirect("/signIn?uri="+url.QueryEscape(idc.Ctx.Input.URI()), 302)
|
||||||
|
}
|
||||||
|
|
||||||
if sessionUserId != nil {
|
if sessionUserId != nil {
|
||||||
|
|
||||||
idc.Data["Username"] = idc.GetSession("username")
|
idc.Data["Username"] = idc.GetSession("username")
|
||||||
idc.Data["UserId"] = sessionUserId.(int)
|
idc.Data["UserId"] = sessionUserId.(int)
|
||||||
|
|
||||||
roleList, err := dao.GetUserProjectRoles(models.User{UserId: sessionUserId.(int)}, projectId)
|
roleList, err := dao.GetUserProjectRoles(models.User{UserId: sessionUserId.(int)}, projectId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
beego.Error("Error occurred in GetUserProjectRoles:", err)
|
beego.Error("Error occurred in GetUserProjectRoles:", err)
|
||||||
idc.CustomAbort(500, "Internal error.")
|
idc.CustomAbort(500, "Internal error.")
|
||||||
}
|
}
|
||||||
if len(roleList) > 0 {
|
|
||||||
|
if project.Public == 0 && len(roleList) == 0 {
|
||||||
|
idc.Redirect("/signIn?uri="+url.QueryEscape(idc.Ctx.Input.URI()), 302)
|
||||||
|
} else if len(roleList) > 0 {
|
||||||
idc.Data["RoleId"] = roleList[0].RoleId
|
idc.Data["RoleId"] = roleList[0].RoleId
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
idc.Data["ProjectId"] = project.ProjectId
|
||||||
|
idc.Data["ProjectName"] = project.Name
|
||||||
|
idc.Data["OwnerName"] = project.OwnerName
|
||||||
|
idc.Data["OwnerId"] = project.OwnerId
|
||||||
|
|
||||||
idc.Data["HarborRegUrl"] = os.Getenv("HARBOR_REG_URL")
|
idc.Data["HarborRegUrl"] = os.Getenv("HARBOR_REG_URL")
|
||||||
idc.Data["RepoName"] = idc.GetString("repo_name")
|
idc.Data["RepoName"] = idc.GetString("repo_name")
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user