From 02c182d1781cd37182c4ccb3f5d7e81bd428240f Mon Sep 17 00:00:00 2001 From: kunw Date: Sat, 21 May 2016 11:26:19 +0800 Subject: [PATCH] updates for header components rendering checked from backend of UI. --- controllers/ng/navigationheader.go | 37 +++++++++++++++++++ controllers/ng/optionalmenu.go | 37 +++++++++++++++++++ .../optional-menu/optional-menu.directive.js | 12 +----- .../navigation-header.directive.html | 5 --- .../navigation/navigation-header.directive.js | 18 +-------- ui/ngrouter.go | 3 ++ views/ng/navigation-header.htm | 9 +++++ .../ng/optional-menu.htm | 11 ++++-- views/ng/sections/header-include.htm | 12 +++--- 9 files changed, 103 insertions(+), 41 deletions(-) create mode 100644 controllers/ng/navigationheader.go create mode 100644 controllers/ng/optionalmenu.go delete mode 100644 static/ng/resources/js/layout/navigation/navigation-header.directive.html create mode 100644 views/ng/navigation-header.htm rename static/ng/resources/js/components/optional-menu/optional-menu.directive.html => views/ng/optional-menu.htm (88%) diff --git a/controllers/ng/navigationheader.go b/controllers/ng/navigationheader.go new file mode 100644 index 000000000..69276bbb9 --- /dev/null +++ b/controllers/ng/navigationheader.go @@ -0,0 +1,37 @@ +package ng + +import ( + "net/http" + + "github.com/vmware/harbor/dao" + "github.com/vmware/harbor/models" + "github.com/vmware/harbor/utils/log" +) + +type NavigationHeaderController struct { + BaseController +} + +func (nhc *NavigationHeaderController) Get() { + sessionUserID := nhc.GetSession("userId") + var hasLoggedIn bool + var isAdmin int + if sessionUserID != nil { + hasLoggedIn = true + userID := sessionUserID.(int) + u, err := dao.GetUser(models.User{UserID: userID}) + if err != nil { + log.Errorf("Error occurred in GetUser, error: %v", err) + nhc.CustomAbort(http.StatusInternalServerError, "Internal error.") + } + if u == nil { + log.Warningf("User was deleted already, user id: %d, canceling request.", userID) + nhc.CustomAbort(http.StatusUnauthorized, "") + } + isAdmin = u.HasAdminRole + } + nhc.Data["HasLoggedIn"] = hasLoggedIn + nhc.Data["IsAdmin"] = isAdmin + nhc.TplName = "ng/navigation-header.htm" + nhc.Render() +} diff --git a/controllers/ng/optionalmenu.go b/controllers/ng/optionalmenu.go new file mode 100644 index 000000000..6945681dc --- /dev/null +++ b/controllers/ng/optionalmenu.go @@ -0,0 +1,37 @@ +package ng + +import ( + "net/http" + + "github.com/vmware/harbor/dao" + "github.com/vmware/harbor/models" + "github.com/vmware/harbor/utils/log" +) + +type OptionalMenuController struct { + BaseController +} + +func (omc *OptionalMenuController) Get() { + sessionUserID := omc.GetSession("userId") + + var hasLoggedIn bool + if sessionUserID != nil { + hasLoggedIn = true + userID := sessionUserID.(int) + u, err := dao.GetUser(models.User{UserID: userID}) + if err != nil { + log.Errorf("Error occurred in GetUser, error: %v", err) + omc.CustomAbort(http.StatusInternalServerError, "Internal error.") + } + if u == nil { + log.Warningf("User was deleted already, user id: %d, canceling request.", userID) + omc.CustomAbort(http.StatusUnauthorized, "") + } + omc.Data["Username"] = u.Username + } + omc.Data["HasLoggedIn"] = hasLoggedIn + omc.TplName = "ng/optional-menu.htm" + omc.Render() + +} diff --git a/static/ng/resources/js/components/optional-menu/optional-menu.directive.js b/static/ng/resources/js/components/optional-menu/optional-menu.directive.js index 13b863044..37f57650b 100644 --- a/static/ng/resources/js/components/optional-menu/optional-menu.directive.js +++ b/static/ng/resources/js/components/optional-menu/optional-menu.directive.js @@ -14,15 +14,7 @@ vm.currentLanguage = I18nService().getCurrentLanguage(); vm.languageName = I18nService().getLanguageName(vm.currentLanguage); console.log('current language:' + I18nService().getCurrentLanguage()); - - vm.isLoggedIn = false; - $timeout(function() { - vm.user = currentUser.get(); - console.log('current user:' + angular.toJson(vm.user)); - if(angular.isDefined(vm.user)) { - vm.isLoggedIn = true; - } - }, 300); + vm.user = currentUser.get(); vm.setLanguage = setLanguage; vm.logOut = logOut; @@ -49,7 +41,7 @@ function optionalMenu() { var directive = { 'restrict': 'E', - 'templateUrl': '/static/ng/resources/js/components/optional-menu/optional-menu.directive.html', + 'templateUrl': '/ng/optional_menu', 'scope': true, 'controller': OptionalMenuController, 'controllerAs': 'vm', diff --git a/static/ng/resources/js/layout/navigation/navigation-header.directive.html b/static/ng/resources/js/layout/navigation/navigation-header.directive.html deleted file mode 100644 index 2a28ef684..000000000 --- a/static/ng/resources/js/layout/navigation/navigation-header.directive.html +++ /dev/null @@ -1,5 +0,0 @@ - \ No newline at end of file diff --git a/static/ng/resources/js/layout/navigation/navigation-header.directive.js b/static/ng/resources/js/layout/navigation/navigation-header.directive.js index b9e31065c..974f19d22 100644 --- a/static/ng/resources/js/layout/navigation/navigation-header.directive.js +++ b/static/ng/resources/js/layout/navigation/navigation-header.directive.js @@ -10,26 +10,13 @@ function NavigationHeaderController($window, $scope, currentUser, $timeout) { var vm = this; - - vm.isShow = false; - vm.isAdmin = false; - - $timeout(function() { - vm.user = currentUser.get(); - if(angular.isDefined(vm.user)) { - vm.isShow = true; - if(vm.user.HasAdminRole === 1) { - vm.isAdmin = true; - } - } - }, 300); vm.url = $window.location.pathname; } function navigationHeader() { var directive = { restrict: 'E', - templateUrl: '/static/ng/resources/js/layout/navigation/navigation-header.directive.html', + templateUrl: '/ng/navigation_header', link: link, scope: true, controller: NavigationHeaderController, @@ -40,12 +27,11 @@ return directive; function link(scope, element, attrs, ctrl) { - element.find('a').removeClass('active'); var visited = ctrl.url; if (visited != "/ng") { element.find('a[href*="' + visited + '"]').addClass('active'); } - element.on('click', click); + element.find('a').on('click', click); function click(event) { element.find('a').removeClass('active'); $(event.target).not('span').addClass('active'); diff --git a/ui/ngrouter.go b/ui/ngrouter.go index a460f2607..f28ff4d45 100644 --- a/ui/ngrouter.go +++ b/ui/ngrouter.go @@ -20,4 +20,7 @@ func initNgRouters() { beego.Router("/ng/log_out", &ng.CommonController{}, "get:LogOut") beego.Router("/ng/reset", &ng.CommonController{}, "post:ResetPassword") beego.Router("/ng/sendEmail", &ng.CommonController{}, "get:SendEmail") + + beego.Router("/ng/optional_menu", &ng.OptionalMenuController{}) + beego.Router("/ng/navigation_header", &ng.NavigationHeaderController{}) } diff --git a/views/ng/navigation-header.htm b/views/ng/navigation-header.htm new file mode 100644 index 000000000..1046715a0 --- /dev/null +++ b/views/ng/navigation-header.htm @@ -0,0 +1,9 @@ +{{ if eq .HasLoggedIn true }} + +{{ end }} \ No newline at end of file diff --git a/static/ng/resources/js/components/optional-menu/optional-menu.directive.html b/views/ng/optional-menu.htm similarity index 88% rename from static/ng/resources/js/components/optional-menu/optional-menu.directive.html rename to views/ng/optional-menu.htm index 4f88d21b5..9aabee197 100644 --- a/static/ng/resources/js/components/optional-menu/optional-menu.directive.html +++ b/views/ng/optional-menu.htm @@ -1,6 +1,7 @@ -