浏览代码

HUE-6248 [sentry] Avoid leaking of the viewModel variable in Hue 4

Enrico Berti 8 年之前
父节点
当前提交
bba3517

+ 78 - 77
apps/security/src/security/templates/hdfs.mako

@@ -295,92 +295,93 @@ ${ tree.import_templates(itemClick='$root.assist.setPath', iconClick='$root.assi
 <script src="${ static('security/js/hdfs.ko.js') }" type="text/javascript" charset="utf-8"></script>
 
 <script type="text/javascript">
+  (function () {
+    var viewModel = new HdfsViewModel(${ initial | n,unicode });
+    ko.applyBindings(viewModel, $('#securityHdfsComponents')[0]);
 
-  var viewModel = new HdfsViewModel(${ initial | n,unicode });
-  ko.applyBindings(viewModel, $('#securityHdfsComponents')[0]);
+    $(document).ready(function () {
 
-  $(document).ready(function () {
+      $(document).on("loaded.users", function(){
+        $(".user-list").select2("val", viewModel.doAs());
+      });
 
-    $(document).on("loaded.users", function(){
-      $(".user-list").select2("val", viewModel.doAs());
-    });
-
-    var _initialPath = "/";
-    if (window.location.hash != "") {
-      _initialPath = window.location.hash.substr(1).replace(/(<([^>]+)>)/ig, "");
-    }
-    viewModel.init(_initialPath);
-
-    $("#path").jHueHdfsAutocomplete({
-      home: viewModel.assist.path(),
-      skipKeydownEvents: true,
-      onPathChange: function (path) {
-        viewModel.assist.path(path);
-      },
-      onEnter: function (el) {
-        viewModel.assist.path(el.val());
-      },
-      smartTooltip: "${_('Did you know? You can use the tab key or CTRL + Space to autocomplete file and folder names')}"
-    });
-
-    function resizeComponents () {
-      $("#path").width($(".tree-toolbar").width() - 64);
-      $("#expandableTree").height($(window).height() - 260);
-      $(".acl-panel-content").height($(window).height() - 260);
-    }
-
-    resizeComponents();
-
-    $(document).on("rendered.tree", function() {
-      var _path = viewModel.assist.path();
-      if (_path[_path.length-1] == "/"){
-        _path = _path.substr(0, _path.length - 1);
+      var _initialPath = "/";
+      if (window.location.hash != "") {
+        _initialPath = window.location.hash.substr(1).replace(/(<([^>]+)>)/ig, "");
+      }
+      viewModel.init(_initialPath);
+
+      $("#path").jHueHdfsAutocomplete({
+        home: viewModel.assist.path(),
+        skipKeydownEvents: true,
+        onPathChange: function (path) {
+          viewModel.assist.path(path);
+        },
+        onEnter: function (el) {
+          viewModel.assist.path(el.val());
+        },
+        smartTooltip: "${_('Did you know? You can use the tab key or CTRL + Space to autocomplete file and folder names')}"
+      });
+
+      function resizeComponents () {
+        $("#path").width($(".tree-toolbar").width() - 64);
+        $("#expandableTree").height($(window).height() - 260);
+        $(".acl-panel-content").height($(window).height() - 260);
       }
-      window.setTimeout(function(){
-        if ($("a.anchor[href^='"+_path+"']").length > 0){
-          $("#expandableTree").animate({
-            scrollTop: ($("a.anchor[href^='"+_path+"']:first").position().top + $("#expandableTree").scrollTop() - $("#expandableTree").position().top - 4)+"px"
-          }, 200);
-        }
-      }, 200)
-    });
-
-    $(document).on("updated.acls", function() {
-      $(document).trigger("info", "${ _('The selected ACLs have been successfully updated.') }");
-    });
-
-    $(document).on("added.bulk.acls", function() {
-      $(document).trigger("info", "${ _('The current ACLs have been successfully added to the checked paths.') }");
-      $("#bulkActionsModal").modal("hide");
-    });
-
-    $(document).on("deleted.bulk.acls", function() {
-      $(document).trigger("info", "${ _('All the ACLs have been successfully removed from the checked paths.') }");
-      $("#bulkActionsModal").modal("hide");
-    });
 
-    $(document).on("syncd.bulk.acls", function() {
-      $(document).trigger("info", "${ _('All the ACLs for the checked items have been replaced with the current selection.') }");
-      $("#bulkActionsModal").modal("hide");
-    });
+      resizeComponents();
 
-    var _resizeTimeout = -1;
-    $(window).resize(function(){
-      window.clearTimeout(_resizeTimeout);
-      _resizeTimeout = window.setTimeout(resizeComponents, 100);
-    });
+      $(document).on("rendered.tree", function() {
+        var _path = viewModel.assist.path();
+        if (_path[_path.length-1] == "/"){
+          _path = _path.substr(0, _path.length - 1);
+        }
+        window.setTimeout(function(){
+          if ($("a.anchor[href^='"+_path+"']").length > 0){
+            $("#expandableTree").animate({
+              scrollTop: ($("a.anchor[href^='"+_path+"']:first").position().top + $("#expandableTree").scrollTop() - $("#expandableTree").position().top - 4)+"px"
+            }, 200);
+          }
+        }, 200)
+      });
+
+      $(document).on("updated.acls", function() {
+        $(document).trigger("info", "${ _('The selected ACLs have been successfully updated.') }");
+      });
+
+      $(document).on("added.bulk.acls", function() {
+        $(document).trigger("info", "${ _('The current ACLs have been successfully added to the checked paths.') }");
+        $("#bulkActionsModal").modal("hide");
+      });
+
+      $(document).on("deleted.bulk.acls", function() {
+        $(document).trigger("info", "${ _('All the ACLs have been successfully removed from the checked paths.') }");
+        $("#bulkActionsModal").modal("hide");
+      });
+
+      $(document).on("syncd.bulk.acls", function() {
+        $(document).trigger("info", "${ _('All the ACLs for the checked items have been replaced with the current selection.') }");
+        $("#bulkActionsModal").modal("hide");
+      });
+
+      var _resizeTimeout = -1;
+      $(window).resize(function(){
+        window.clearTimeout(_resizeTimeout);
+        _resizeTimeout = window.setTimeout(resizeComponents, 100);
+      });
+
+      window.onpopstate = function() {
+        if (window.location.pathname.indexOf('/security') > -1) {
+          viewModel.assist.path(window.location.hash.substr(1));
+        }
+      };
 
-    window.onpopstate = function() {
-      if (window.location.pathname.indexOf('/security') > -1) {
-        viewModel.assist.path(window.location.hash.substr(1));
-      }
-    };
+      $("#bulkActionsModal").modal({
+        show: false
+      });
 
-    $("#bulkActionsModal").modal({
-      show: false
     });
-
-  });
+  })();
 </script>
 </span>
 

+ 2 - 1
apps/security/src/security/templates/hive.mako

@@ -571,7 +571,7 @@ ${ tree.import_templates(itemClick='$root.assist.setPath', iconClick='$root.assi
 <script src="${ static('desktop/js/jquery.filechooser.js') }" type="text/javascript" charset="utf-8"></script>
 
 <script type="text/javascript">
-
+  (function () {
     ko.options.deferUpdates = true;
 
     function deletePrivilegeModal(role) {
@@ -799,6 +799,7 @@ ${ tree.import_templates(itemClick='$root.assist.setPath', iconClick='$root.assi
         show: false
       });
     });
+  })();
 </script>
 </span>
 

+ 30 - 28
apps/security/src/security/templates/sentry.mako

@@ -628,12 +628,13 @@ ${ tree.import_templates(itemClick='$root.assist.setPath', iconClick='$root.assi
 
 
 <script type="text/javascript">
-
+  (function () {
     function deletePrivilegeModal(role) {
-      var cascadeDeletes = $.grep(role.privilegesChanged(), function(privilege) {
-        return privilege.status() == 'deleted' && (privilege.privilegeType() == 'SERVER' || privilege.privilegeType() == 'DATABASE'); }
+      var cascadeDeletes = $.grep(role.privilegesChanged(), function (privilege) {
+            return privilege.status() == 'deleted' && (privilege.privilegeType() == 'SERVER' || privilege.privilegeType() == 'DATABASE');
+          }
       );
-      if (cascadeDeletes.length > 0 ) {
+      if (cascadeDeletes.length > 0) {
         viewModel.roleToUpdate(role);
         $('#deletePrivilegeModal').modal('show');
       } else {
@@ -657,8 +658,8 @@ ${ tree.import_templates(itemClick='$root.assist.setPath', iconClick='$root.assi
         'html': true
       });
 
-      function setPathFromAutocomplete(path){
-        if (path.lastIndexOf(".") == path.length -1){
+      function setPathFromAutocomplete(path) {
+        if (path.lastIndexOf(".") == path.length - 1) {
           path = path.substring(0, path.length - 1);
         }
         viewModel.assist.path(path);
@@ -689,34 +690,34 @@ ${ tree.import_templates(itemClick='$root.assist.setPath', iconClick='$root.assi
 
       resizeComponents();
 
-      $(document).on("rendered.tree", function() {
+      $(document).on("rendered.tree", function () {
         var _path = viewModel.assist.path();
-        if (_path[_path.length-1] == "/"){
+        if (_path[_path.length - 1] == "/") {
           _path = _path.substr(0, _path.length - 1);
         }
-        if ($("a.anchor[href^='"+_path+"']").length > 0){
+        if ($("a.anchor[href^='" + _path + "']").length > 0) {
           $("#expandableTree").animate({
-            scrollTop: ($("a.anchor[href^='"+_path+"']:first").position().top + $("#expandableTree").scrollTop() - $("#expandableTree").position().top - 4) + "px"
+            scrollTop: ($("a.anchor[href^='" + _path + "']:first").position().top + $("#expandableTree").scrollTop() - $("#expandableTree").position().top - 4) + "px"
           });
         }
       });
 
-      $(document).on("created.role", function(){
+      $(document).on("created.role", function () {
         $("#createRoleModal").modal("hide");
         $("#grantPrivilegeModal").modal("hide");
         $("#deletePrivilegeModal").modal("hide");
         viewModel.clearTempRoles();
-        window.setTimeout(function(){
+        window.setTimeout(function () {
           viewModel.refreshExpandedRoles();
         }, 500);
       });
 
-      $(document).on("deleted.role", function(){
+      $(document).on("deleted.role", function () {
         $("#deleteRoleModal").modal("hide");
       });
 
-      $(document).on("changed.path", function(){
-        if ($("#path").val() != viewModel.assist.path()){
+      $(document).on("changed.path", function () {
+        if ($("#path").val() != viewModel.assist.path()) {
           $("#path").val(viewModel.assist.path());
         }
       });
@@ -736,18 +737,18 @@ ${ tree.import_templates(itemClick='$root.assist.setPath', iconClick='$root.assi
         $("a[data-toggleSection='" + mainSection + "']").parent().addClass("active");
       }
 
-      $("[data-toggleSection]").on("click", function(){
+      $("[data-toggleSection]").on("click", function () {
         showMainSection($(this).attr("data-toggleSection"));
       });
 
       showMainSection(viewModel.getSectionHash());
 
-      $(document).on("show.mainSection", function(){
+      $(document).on("show.mainSection", function () {
         showMainSection(viewModel.getSectionHash());
       });
 
-      $(document).on("show.role", function(e, role) {
-        if (typeof role != "undefined" && role.name != null){
+      $(document).on("show.role", function (e, role) {
+        if (typeof role != "undefined" && role.name != null) {
           $("#bulkActionsModal").modal("hide");
           showMainSection("roles");
           $("html, body").animate({
@@ -757,12 +758,12 @@ ${ tree.import_templates(itemClick='$root.assist.setPath', iconClick='$root.assi
       });
 
       var _resizeTimeout = -1;
-      $(window).resize(function(){
+      $(window).resize(function () {
         window.clearTimeout(_resizeTimeout);
         _resizeTimeout = window.setTimeout(resizeComponents, 100);
       });
 
-      window.onpopstate = function() {
+      window.onpopstate = function () {
         if (window.location.pathname.indexOf('/security') > -1) {
           viewModel.assist.path(viewModel.getPathHash());
         }
@@ -795,22 +796,22 @@ ${ tree.import_templates(itemClick='$root.assist.setPath', iconClick='$root.assi
       });
 
       $("#selectedGroup").select2("val", "");
-      $("#selectedGroup").change(function() {
+      $("#selectedGroup").change(function () {
         viewModel.list_sentry_privileges_by_authorizable();
         viewModel.list_sentry_roles_by_group();
       });
 
-      $(document).on("added.bulk.privileges", function() {
+      $(document).on("added.bulk.privileges", function () {
         $(document).trigger("info", "${ _('The current privileges have been successfully added to the checked items.') }");
         $("#bulkActionsModal").modal("hide");
       });
 
-      $(document).on("deleted.bulk.privileges", function() {
+      $(document).on("deleted.bulk.privileges", function () {
         $(document).trigger("info", "${ _('All the privileges have been successfully removed from the checked items.') }");
         $("#bulkActionsModal").modal("hide");
       });
 
-      $(document).on("syncd.bulk.privileges", function() {
+      $(document).on("syncd.bulk.privileges", function () {
         $(document).trigger("info", "${ _('All the privileges for the checked items have been replaced with the current selection.') }");
         $("#bulkActionsModal").modal("hide");
       });
@@ -819,7 +820,7 @@ ${ tree.import_templates(itemClick='$root.assist.setPath', iconClick='$root.assi
         show: false
       });
 
-      $("#bulkActionsModal").on("hidden", function(){
+      $("#bulkActionsModal").on("hidden", function () {
         viewModel.isApplyingBulk(false);
       });
 
@@ -841,11 +842,11 @@ ${ tree.import_templates(itemClick='$root.assist.setPath', iconClick='$root.assi
         });
       });
       $(document).on('focus', '#createRoleName', function () {
-        if ($("#createRoleName").data('typeahead')){
+        if ($("#createRoleName").data('typeahead')) {
           $("#createRoleName").data('typeahead').lookup();
         }
       });
-      $(document).on("destroy.typeahead", function(){
+      $(document).on("destroy.typeahead", function () {
         $('.typeahead').unbind();
         $("ul.typeahead").hide();
       });
@@ -856,6 +857,7 @@ ${ tree.import_templates(itemClick='$root.assist.setPath', iconClick='$root.assi
         show: false
       });
     });
+  })();
 </script>
 </span>
 

+ 1 - 1
desktop/core/src/desktop/static/desktop/js/ko.hue-bindings.js

@@ -2735,7 +2735,7 @@
 
 
   ko.bindingHandlers.select2 = {
-    init: function (element, valueAccessor, allBindingsAccessor, vm) {
+    init: function (element, valueAccessor, allBindingsAccessor, viewModel) {
       var options = ko.toJS(valueAccessor()) || {};
       var $element = $(element);