Merge pull request #1121 from wknet123/dev-show-download-cert

Add download cert to UI.
This commit is contained in:
Daniel Jiang 2016-11-18 16:30:43 +08:00 committed by GitHub
commit 3a23c38aaf
7 changed files with 56 additions and 10 deletions

View File

@ -2,6 +2,7 @@ package api
import (
"net/http"
"os"
"path/filepath"
"syscall"
@ -18,6 +19,7 @@ type SystemInfoAPI struct {
}
const harborStoragePath = "/harbor_storage"
const defaultRootCert = "/harbor_storage/ca-download-dir/ca.crt"
//SystemInfo models for system info.
type SystemInfo struct {
@ -66,3 +68,17 @@ func (sia *SystemInfoAPI) GetVolumeInfo() {
sia.Data["json"] = systemInfo
sia.ServeJSON()
}
//GetCert gets default self-signed certificate.
func (sia *SystemInfoAPI) GetCert() {
if sia.isAdmin {
if _, err := os.Stat(defaultRootCert); !os.IsNotExist(err) {
sia.Ctx.Output.Header("Content-Disposition", "attachment; filename=ca.crt")
http.ServeFile(sia.Ctx.ResponseWriter, sia.Ctx.Request, defaultRootCert)
} else {
log.Error("No certificate found.")
sia.CustomAbort(http.StatusNotFound, "No certificate found.")
}
}
sia.CustomAbort(http.StatusUnauthorized, "")
}

View File

@ -31,9 +31,10 @@ type langType struct {
}
const (
viewPath = "sections"
prefixNg = ""
defaultLang = "en-US"
viewPath = "sections"
prefixNg = ""
defaultLang = "en-US"
defaultRootCert = "/harbor_storage/ca-download-dir/ca.crt"
)
var supportLanguages map[string]langType
@ -45,6 +46,8 @@ func (b *BaseController) Prepare() {
var lang string
var langHasChanged bool
var showDownloadCert bool
langRequest := b.GetString("lang")
if langRequest != "" {
lang = langRequest
@ -120,6 +123,20 @@ func (b *BaseController) Prepare() {
b.SelfRegistration = config.SelfRegistration()
b.Data["SelfRegistration"] = config.SelfRegistration()
sessionUserID := b.GetSession("userId")
if sessionUserID != nil {
isAdmin, err := dao.IsAdminRole(sessionUserID.(int))
if err != nil {
log.Errorf("Error occurred in IsAdminRole: %v", err)
}
if isAdmin {
if _, err := os.Stat(defaultRootCert); !os.IsNotExist(err) {
showDownloadCert = true
}
}
}
b.Data["ShowDownloadCert"] = showDownloadCert
}
// Forward to setup layout and template for content for a page.

View File

@ -86,6 +86,7 @@ func initRouters() {
beego.Router("/api/logs", &api.LogAPI{})
beego.Router("/api/systeminfo/volumes", &api.SystemInfoAPI{}, "get:GetVolumeInfo")
beego.Router("/api/systeminfo/getcert", &api.SystemInfoAPI{}, "get:GetCert")
//external service that hosted on harbor process:
beego.Router("/service/notifications", &service.NotificationHandler{})
beego.Router("/service/token", &token.Handler{})

View File

@ -35,7 +35,7 @@
vm.setLanguage = setLanguage;
vm.logOut = logOut;
vm.about = about;
function setLanguage(language) {
vm.languageName = i18n.getLanguageName(vm.language);
var hash = $window.location.hash;
@ -62,10 +62,12 @@
function about() {
$scope.$emit('modalTitle', $filter('tr')('about_harbor'));
vm.modalMessage = $filter('tr')('current_version', [vm.version || 'Unknown']);
vm.modalMessage = $filter('tr')('current_version', [vm.version || 'Unknown']);
if(vm.showDownloadCert === 'true') {
appendDownloadCertLink();
}
GetVolumeInfoService("data")
.then(getVolumeInfoSuccess, getVolumeInfoFailed);
}
function getVolumeInfoSuccess(response) {
var storage = response.data;
@ -79,10 +81,15 @@
$scope.$emit('modalMessage', vm.modalMessage);
$scope.$emit('raiseInfo', raiseInfo);
}
function toGigaBytes(val) {
return Math.round(val / (1024 * 1024 * 1024));
}
function appendDownloadCertLink() {
vm.modalMessage += '<br/>' + $filter('tr')('default_root_cert', ['/api/systeminfo/getcert', $filter('tr')('download')]);
}
}
function optionalMenu() {
@ -91,7 +98,8 @@
'templateUrl': '/optional_menu?timestamp=' + new Date().getTime(),
'scope': {
'version': '@',
'language': '@'
'language': '@',
'showDownloadCert': '@'
},
'controller': OptionalMenuController,
'controllerAs': 'vm',

View File

@ -233,6 +233,8 @@ var locale_messages = {
'about_harbor': 'About Harbor',
'current_version': '<label>Version</label>&nbsp;&nbsp;<span>$0</span>',
'current_storage': '<label>Storage</label>&nbsp;&nbsp;<span>$0 GB available of $1 GB.</span>',
'default_root_cert': '<label>Default Root Cert.</label>&nbsp;&nbsp;<span><a href="$0">$1</a></span>',
'download': 'Download',
'failed_to_get_project_member': 'Failed to get current project member.',
'failed_to_delete_repo': 'Failed to delete repository. ',
'failed_to_delete_repo_insuffient_permissions': 'Failed to delete repository, insuffient permissions.',
@ -285,4 +287,4 @@ var locale_messages = {
'confirm_to_toggle_disabled_policy_title': 'Disable Policy',
'confirm_to_toggle_disabled_policy': 'After disabling the policy, all unfinished replication jobs of this policy will be stopped and canceled. Please confirm to continue.',
'begin_date_is_later_than_end_date': 'Begin date should not be later than end date.'
};
};

View File

@ -233,6 +233,8 @@ var locale_messages = {
'about_harbor': '关于 Harbor',
'current_version': '<label>当前版本</label>&nbsp;&nbsp;<span>$0</span>',
'current_storage': '<label>存储情况</label>&nbsp;&nbsp;<span>可用: $0 GB总共 $1 GB。</span>',
'default_root_cert': '<label>默认根证书</label>&nbsp;&nbsp;<span><a href="$0">$1</a></span>',
'download': '下载',
'failed_to_get_project_member': '无法获取当前项目成员。',
'failed_to_delete_repo': '无法删除镜像仓库。',
'failed_to_delete_repo_insuffient_permissions': '无法删除镜像仓库,权限不足。',
@ -287,4 +289,4 @@ var locale_messages = {
'confirm_to_toggle_disabled_policy_title': '停用策略',
'confirm_to_toggle_disabled_policy': '停用策略后,所有未完成的复制任务将被终止和取消。请确认继续。',
'begin_date_is_later_than_end_date': '起始日期不能晚于结束日期。'
};
};

View File

@ -25,7 +25,7 @@
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-harbor-navbar-collapse-1">
<optional-menu version="{{.Version}}" language="{{.Lang}}" login-status="//vm.loginStatus//"></optional-menu>
<optional-menu version="{{.Version}}" language="{{.Lang}}" show-download-cert="{{.ShowDownloadCert}}" login-status="//vm.loginStatus//"></optional-menu>
<ul class="nav navbar-nav navbar-right">
<li>
<navigation-header></navigation-header>