Browse Source

AMBARI-9529 Ambari view - View iframe pointing to invalid link when ambari url contains query parameters. (atkach)

Andrii Tkach 10 năm trước cách đây
mục cha
commit
032bbd760f

+ 1 - 0
ambari-web/app/assets/test/tests.js

@@ -255,6 +255,7 @@ var files = ['test/init_model_test',
   'test/models/user_test',
   'test/models/host_stack_version_test',
   'test/models/upgrade_entity_test',
+  'test/routes/views_test',
   //contains test with fake timers that affect Date
   'test/utils/lazy_loading_test'
 

+ 31 - 2
ambari-web/app/routes/views.js

@@ -35,10 +35,39 @@ module.exports = Em.Route.extend({
     route: '/:viewName/:version/:instanceName',
     connectOutlets: function (router, params) {
       // find and set content for `mainViewsDetails` and associated controller
+
+      var href = ['/views', params.viewName, params.version, params.instanceName].join('/');
+      var viewPath = this.parseViewPath(params.instanceName);
+      if (viewPath) {
+        href = ['/views', params.viewName, params.version, params.instanceName.slice(0, params.instanceName.lastIndexOf('?'))].join('/');
+      }
+
       router.get('mainViewsController').dataLoading().done(function() {
-        router.get('mainController').connectOutlet('mainViewsDetails', App.router.get('mainViewsController.ambariViews')
-          .findProperty('href', ['/views', params.viewName, params.version, params.instanceName].join('/')));
+        var content = App.router.get('mainViewsController.ambariViews').findProperty('href', href);
+        if (content) content.set('viewPath', viewPath);
+        router.get('mainController').connectOutlet('mainViewsDetails', content);
       });
+    },
+    /**
+     * parse internal view path
+     * "viewPath" - used as a key of additional path
+     * Example:
+     *   origin URL: viewName?viewPath=%2Fuser%2Fadmin%2Faddress&foo=bar&count=1
+     * should be translated to
+     *   view path: /user/admin/address?foo=bar&count=1
+     *
+     * @param {string} instanceName
+     * @returns {string}
+     */
+    parseViewPath: function (instanceName) {
+      var path = '';
+      if (instanceName.contains('?')) {
+        path = instanceName.slice(instanceName.indexOf('?'));
+        if (path.contains('viewPath')) {
+          path = decodeURIComponent(path.slice((path.lastIndexOf('?viewPath=') + 10))).replace('&', '?');
+        }
+      }
+      return path;
     }
   })
 });

+ 1 - 1
ambari-web/app/views/main/views/details.js

@@ -87,7 +87,7 @@ App.MainViewsDetailsView = Em.View.extend({
 
   src: function() {
     // can't use window.location.origin because IE doesn't support it
-    return window.location.protocol + '//' + window.location.host + this.get('controller.content.href');
+    return window.location.protocol + '//' + window.location.host + this.get('controller.content.href') + this.get('controller.content.viewPath');
   }.property('controller.content')
 
 });

+ 50 - 0
ambari-web/test/routes/views_test.js

@@ -0,0 +1,50 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+var routeClass = require('routes/views');
+
+describe('routes/views', function() {
+
+  var route = routeClass.create().get('viewDetails').create();
+
+  describe("#parseViewPath", function() {
+    var testCases = [
+      {
+        url: 'viewName',
+        result: ''
+      },
+      {
+        url: 'viewName?foo=bar&count=1',
+        result: '?foo=bar&count=1'
+      },
+      {
+        url: 'viewName?viewPath=%2Fuser%2Fadmin%2Faddress',
+        result: '/user/admin/address'
+      },
+      {
+        url: 'viewName?viewPath=%2Fuser%2Fadmin%2Faddress&foo=bar&count=1',
+        result: '/user/admin/address?foo=bar&count=1'
+      }
+    ].forEach(function(test){
+        it("url = " + test.url, function() {
+          expect(route.parseViewPath(test.url)).to.equal(test.result);
+        });
+      });
+  });
+
+});