harbor/controllers/itemdetail.go

107 lines
2.9 KiB
Go
Raw Normal View History

2016-02-01 12:59:10 +01:00
/*
Copyright (c) 2016 VMware, Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
2016-02-26 11:54:14 +01:00
2016-02-01 12:59:10 +01:00
package controllers
import (
"net/http"
2016-02-01 12:59:10 +01:00
"net/url"
"os"
"github.com/vmware/harbor/dao"
"github.com/vmware/harbor/models"
"github.com/astaxie/beego"
)
2016-02-26 11:35:55 +01:00
// ItemDetailController handles requet to /registry/detail, which shows the detail of a project.
2016-02-01 12:59:10 +01:00
type ItemDetailController struct {
BaseController
}
2016-02-26 11:35:55 +01:00
// Get will check if user has permission to view a certain project, if not user will be redirected to signin or his homepage.
// If the check is passed it renders the project detail page.
2016-02-01 12:59:10 +01:00
func (idc *ItemDetailController) Get() {
projectID, _ := idc.GetInt64("project_id")
2016-02-01 12:59:10 +01:00
if projectID <= 0 {
beego.Error("Invalid project id:", projectID)
2016-02-24 13:13:54 +01:00
idc.Redirect("/signIn", http.StatusFound)
2016-02-25 04:48:22 +01:00
return
2016-02-01 12:59:10 +01:00
}
2016-02-26 04:26:54 +01:00
project, err := dao.GetProjectByID(projectID)
2016-02-01 12:59:10 +01:00
if err != nil {
beego.Error("Error occurred in GetProjectById:", err)
idc.CustomAbort(http.StatusInternalServerError, "Internal error.")
2016-02-01 12:59:10 +01:00
}
if project == nil {
idc.Redirect("/signIn", http.StatusFound)
2016-02-25 04:48:22 +01:00
return
2016-02-01 12:59:10 +01:00
}
sessionUserID := idc.GetSession("userId")
if project.Public != 1 && sessionUserID == nil {
2016-02-24 13:13:54 +01:00
idc.Redirect("/signIn?uri="+url.QueryEscape(idc.Ctx.Input.URI()), http.StatusFound)
2016-02-25 04:48:22 +01:00
return
}
2016-02-01 12:59:10 +01:00
if sessionUserID != nil {
2016-03-07 11:40:26 +01:00
userID := sessionUserID.(int)
2016-02-01 12:59:10 +01:00
idc.Data["Username"] = idc.GetSession("username")
2016-03-08 04:17:09 +01:00
idc.Data["UserId"] = userID
2016-03-08 04:17:09 +01:00
roleList, err := dao.GetUserProjectRoles(models.User{UserID: userID}, projectID)
2016-02-01 12:59:10 +01:00
if err != nil {
beego.Error("Error occurred in GetUserProjectRoles:", err)
idc.CustomAbort(http.StatusInternalServerError, "Internal error.")
2016-02-01 12:59:10 +01:00
}
2016-03-07 11:40:26 +01:00
isAdmin, err := dao.IsAdminRole(userID)
if err != nil {
beego.Error("Error occurred in IsAdminRole:", err)
idc.CustomAbort(http.StatusInternalServerError, "Internal error.")
}
if !isAdmin && (project.Public == 0 && len(roleList) == 0) {
2016-02-25 04:18:40 +01:00
idc.Redirect("/registry/project", http.StatusFound)
2016-02-25 04:48:22 +01:00
return
2016-02-25 04:18:40 +01:00
}
if isAdmin {
idc.Data["RoleId"] = models.SYSADMIN
} else if len(roleList) > 0 {
2016-02-26 03:15:01 +01:00
idc.Data["RoleId"] = roleList[0].RoleID
2016-02-01 12:59:10 +01:00
}
}
2016-02-26 03:15:01 +01:00
idc.Data["ProjectId"] = project.ProjectID
idc.Data["ProjectName"] = project.Name
idc.Data["OwnerName"] = project.OwnerName
2016-02-26 03:15:01 +01:00
idc.Data["OwnerId"] = project.OwnerID
2016-02-01 12:59:10 +01:00
idc.Data["HarborRegUrl"] = os.Getenv("HARBOR_REG_URL")
idc.Data["RepoName"] = idc.GetString("repo_name")
idc.ForwardTo("page_title_item_details", "item-detail")
}