refactored codes in item_detail controller

This commit is contained in:
wknet123 2016-02-24 20:07:00 +08:00
parent 273aea3c6d
commit 67b4415717
1 changed files with 19 additions and 43 deletions

View File

@ -28,47 +28,12 @@ type ItemDetailController struct {
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() {
sessionUserId := idc.GetSession("userId")
projectId, _ := idc.GetInt64("project_id")
if CheckPublicProject(projectId) == false && (sessionUserId == nil || !CheckProjectRole(sessionUserId.(int), projectId)) {
idc.Redirect("/signIn?uri="+url.QueryEscape(idc.Ctx.Input.URI()), 302)
if projectId <= 0 {
beego.Error("Invalid project id:", projectId)
idc.Redirect("/signIn", 302)
}
projectQuery := models.Project{ProjectId: projectId}
@ -83,24 +48,35 @@ func (idc *ItemDetailController) Get() {
idc.Redirect("/signIn", 302)
}
idc.Data["ProjectId"] = project.ProjectId
idc.Data["ProjectName"] = project.Name
idc.Data["OwnerName"] = project.OwnerName
idc.Data["OwnerId"] = project.OwnerId
sessionUserId := idc.GetSession("userId")
if project.Public != 1 && sessionUserId == nil {
idc.Redirect("/signIn?uri="+url.QueryEscape(idc.Ctx.Input.URI()), 302)
}
if sessionUserId != nil {
idc.Data["Username"] = idc.GetSession("username")
idc.Data["UserId"] = sessionUserId.(int)
roleList, err := dao.GetUserProjectRoles(models.User{UserId: sessionUserId.(int)}, projectId)
if err != nil {
beego.Error("Error occurred in GetUserProjectRoles:", err)
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["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["RepoName"] = idc.GetString("repo_name")